pt_core.c 558 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501
  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 "pt_regs.h"
  30. #include <linux/kthread.h>
  31. #include <linux/i2c.h>
  32. #include <linux/gpio.h>
  33. #ifdef PT_PTSBC_SUPPORT
  34. #define PT_CORE_PROBE_STARTUP_DELAY_MS 500
  35. #endif /* PT_PTSBC_SUPPORT */
  36. #define PT_CORE_STARTUP_RETRY_COUNT 3
  37. MODULE_FIRMWARE(PT_FW_FILE_NAME);
  38. static const char *pt_driver_core_name = PT_CORE_NAME;
  39. static const char *pt_driver_core_version = PT_DRIVER_VERSION;
  40. static const char *pt_driver_core_date = PT_DRIVER_DATE;
  41. struct pt_hid_field {
  42. int report_count;
  43. int report_size;
  44. int size; /* report_count * report_size */
  45. int offset;
  46. int data_type;
  47. int logical_min;
  48. int logical_max;
  49. /* Usage Page (Hi 16 bit) + Usage (Lo 16 bit) */
  50. u32 usage_page;
  51. u32 collection_usage_pages[PT_HID_MAX_COLLECTIONS];
  52. struct pt_hid_report *report;
  53. bool record_field;
  54. };
  55. struct pt_hid_report {
  56. u8 id;
  57. u8 type;
  58. int size;
  59. struct pt_hid_field *fields[PT_HID_MAX_FIELDS];
  60. int num_fields;
  61. int record_field_index;
  62. int header_size;
  63. int record_size;
  64. u32 usage_page;
  65. int log_collection_num;
  66. };
  67. struct atten_node {
  68. struct list_head node;
  69. char *id;
  70. struct device *dev;
  71. int (*func)(struct device *dev);
  72. int mode;
  73. };
  74. struct param_node {
  75. struct list_head node;
  76. u8 id;
  77. u32 value;
  78. u8 size;
  79. };
  80. struct module_node {
  81. struct list_head node;
  82. struct pt_module *module;
  83. void *data;
  84. };
  85. struct pt_hid_cmd {
  86. __le16 descriptor;
  87. u8 opcode;
  88. u8 report_type;
  89. union {
  90. u8 report_id;
  91. u8 power_state;
  92. };
  93. u8 has_data_register;
  94. size_t write_length;
  95. size_t read_length;
  96. u8 *write_buf;
  97. u8 *read_buf;
  98. u8 wait_interrupt;
  99. u8 reset_cmd;
  100. u16 timeout_ms;
  101. };
  102. struct pt_hid_output {
  103. u8 cmd_type;
  104. u16 length;
  105. u8 command_code;
  106. size_t write_length;
  107. u8 *write_buf;
  108. u8 novalidate;
  109. u8 reset_expected;
  110. u16 timeout_ms;
  111. };
  112. #define SET_CMD_OPCODE(byte, opcode) SET_CMD_LOW(byte, opcode)
  113. #define SET_CMD_REPORT_TYPE(byte, type) SET_CMD_HIGH(byte, ((type) << 4))
  114. #define SET_CMD_REPORT_ID(byte, id) SET_CMD_LOW(byte, id)
  115. #define CREATE_PIP1_FW_CMD(command) \
  116. .cmd_type = PIP1_CMD_TYPE_FW, \
  117. .command_code = command
  118. #define CREATE_PIP1_BL_CMD(command) \
  119. .cmd_type = PIP1_CMD_TYPE_BL, \
  120. .command_code = command
  121. #define PT_MAX_PR_BUF_SIZE 2048
  122. /*******************************************************************************
  123. * FUNCTION: pt_pr_buf
  124. *
  125. * SUMMARY: Print out the contents of a buffer to kmsg based on the debug level
  126. *
  127. * RETURN: Void
  128. *
  129. * PARAMETERS:
  130. * *dev - pointer to Device structure
  131. * debug_level - requested debug level to print at
  132. * *buf - pointer to buffer to print
  133. * buf_len - size of buf
  134. * *data_name - Descriptive name of data prefixed to data
  135. ******************************************************************************/
  136. void pt_pr_buf(struct device *dev, u8 debug_level, u8 *buf,
  137. u16 buf_len, const char *data_name)
  138. {
  139. struct pt_core_data *cd = dev_get_drvdata(dev);
  140. int i;
  141. int pr_buf_index = 0;
  142. int max_size;
  143. /* only proceed if valid debug level and there is data to print */
  144. if (debug_level <= cd->debug_level && buf_len > 0) {
  145. char *pr_buf = kzalloc(PT_MAX_PR_BUF_SIZE, GFP_KERNEL);
  146. if (!pr_buf)
  147. return;
  148. /*
  149. * With spaces each printed char takes 3 bytes, subtract
  150. * the length of the data_name and length prefix and divide 3
  151. */
  152. pr_buf_index += scnprintf(pr_buf, PT_MAX_PR_BUF_SIZE, "%s [0..%d]: ",
  153. data_name, buf_len);
  154. max_size = (PT_MAX_PR_BUF_SIZE - pr_buf_index) / 3;
  155. for (i = 0; i < buf_len && i < max_size; i++)
  156. pr_buf_index += scnprintf(pr_buf + pr_buf_index,
  157. PT_MAX_PR_BUF_SIZE, "%02X ", buf[i]);
  158. pt_debug(dev, debug_level, "%s\n", pr_buf);
  159. kfree(pr_buf);
  160. }
  161. }
  162. EXPORT_SYMBOL_GPL(pt_pr_buf);
  163. #ifdef TTHE_TUNER_SUPPORT
  164. /*******************************************************************************
  165. * FUNCTION: tthe_print
  166. *
  167. * SUMMARY: Format data name and time stamp as the header and format the
  168. * content of input buffer with hex base to "tthe_buf". And then wake up event
  169. * semaphore for tthe debugfs node.
  170. *
  171. * RETURN:
  172. * 0 = success
  173. * !0 = failure
  174. *
  175. * PARAMETERS:
  176. * *cd - pointer to core data
  177. * *buf - pointer to input buffer
  178. * buf_len - size of input buffer
  179. * *data_name - pointer to data name
  180. ******************************************************************************/
  181. static int tthe_print(struct pt_core_data *cd, u8 *buf, int buf_len,
  182. const u8 *data_name)
  183. {
  184. int name_len = strlen(data_name);
  185. int i, n;
  186. u8 *p;
  187. int remain;
  188. u8 data_name_with_time_stamp[100];
  189. /* Prepend timestamp, if requested, to data_name */
  190. if (cd->show_timestamp) {
  191. scnprintf(data_name_with_time_stamp,
  192. sizeof(data_name_with_time_stamp),
  193. "[%u] %s", pt_get_time_stamp(), data_name);
  194. data_name = data_name_with_time_stamp;
  195. name_len = strlen(data_name);
  196. }
  197. mutex_lock(&cd->tthe_lock);
  198. if (!cd->tthe_buf)
  199. goto exit;
  200. /* Add 1 due to the '\n' that is appended at the end */
  201. if (cd->tthe_buf_len + name_len + buf_len + 1 > cd->tthe_buf_size)
  202. goto exit;
  203. if (name_len + buf_len == 0)
  204. goto exit;
  205. remain = cd->tthe_buf_size - cd->tthe_buf_len;
  206. if (remain < name_len)
  207. name_len = remain;
  208. p = cd->tthe_buf + cd->tthe_buf_len;
  209. memcpy(p, data_name, name_len);
  210. cd->tthe_buf_len += name_len;
  211. p += name_len;
  212. remain -= name_len;
  213. *p = 0;
  214. for (i = 0; i < buf_len; i++) {
  215. n = scnprintf(p, remain, "%02X ", buf[i]);
  216. if (n <= 0)
  217. break;
  218. p += n;
  219. remain -= n;
  220. cd->tthe_buf_len += n;
  221. }
  222. n = scnprintf(p, remain, "\n");
  223. cd->tthe_buf_len += n;
  224. exit:
  225. wake_up(&cd->wait_q);
  226. mutex_unlock(&cd->tthe_lock);
  227. return 0;
  228. }
  229. /*******************************************************************************
  230. * FUNCTION: _pt_request_tthe_print
  231. *
  232. * SUMMARY: Function pointer included in core_cmds to allow other modules
  233. * to request to print data to the "tthe_buffer".
  234. *
  235. * RETURN:
  236. * 0 = success
  237. * !0 = failure
  238. *
  239. * PARAMETERS:
  240. * *dev - pointer to device structure
  241. ******************************************************************************/
  242. static int _pt_request_tthe_print(struct device *dev, u8 *buf,
  243. int buf_len, const u8 *data_name)
  244. {
  245. struct pt_core_data *cd = dev_get_drvdata(dev);
  246. return tthe_print(cd, buf, buf_len, data_name);
  247. }
  248. #endif
  249. /*******************************************************************************
  250. * FUNCTION: pt_platform_detect_read
  251. *
  252. * SUMMARY: To be passed to platform dectect function to perform a read
  253. * operation.
  254. *
  255. * RETURN:
  256. * 0 = success
  257. * !0 = failure
  258. *
  259. * PARAMETERS:
  260. * *dev - pointer to Device structure
  261. * *buf - pointer to buffer where the data read will be stored
  262. * size - size to be read
  263. ******************************************************************************/
  264. static int pt_platform_detect_read(struct device *dev, void *buf, int size)
  265. {
  266. struct pt_core_data *cd = dev_get_drvdata(dev);
  267. return pt_adap_read_default(cd, buf, size);
  268. }
  269. /*******************************************************************************
  270. * FUNCTION: pt_add_parameter
  271. *
  272. * SUMMARY: Adds a parameter that has been altered to the parameter linked list.
  273. * On every reset of the DUT this linked list is traversed and all
  274. * parameters in it are restored to the DUT.
  275. *
  276. * RETURN:
  277. * 0 = success
  278. * !0 = failure
  279. *
  280. * PARAMETERS:
  281. * *cd - pointer to core data
  282. * param_id - parameter ID to add
  283. * param_value - Value corresponding to the ID
  284. * param_size - Size of param_value
  285. ******************************************************************************/
  286. static int pt_add_parameter(struct pt_core_data *cd,
  287. u8 param_id, u32 param_value, u8 param_size)
  288. {
  289. struct param_node *param, *param_new;
  290. /* Check if parameter already exists in the list */
  291. spin_lock(&cd->spinlock);
  292. list_for_each_entry(param, &cd->param_list, node) {
  293. if (param->id == param_id) {
  294. /* Update parameter */
  295. param->value = param_value;
  296. pt_debug(cd->dev, DL_INFO,
  297. "%s: Update parameter id:%d value:%d size:%d\n",
  298. __func__, param_id, param_value, param_size);
  299. goto exit_unlock;
  300. }
  301. }
  302. spin_unlock(&cd->spinlock);
  303. param_new = kzalloc(sizeof(*param_new), GFP_KERNEL);
  304. if (!param_new)
  305. return -ENOMEM;
  306. param_new->id = param_id;
  307. param_new->value = param_value;
  308. param_new->size = param_size;
  309. pt_debug(cd->dev, DL_INFO,
  310. "%s: Add parameter id:%d value:%d size:%d\n",
  311. __func__, param_id, param_value, param_size);
  312. spin_lock(&cd->spinlock);
  313. list_add(&param_new->node, &cd->param_list);
  314. exit_unlock:
  315. spin_unlock(&cd->spinlock);
  316. return 0;
  317. }
  318. #ifndef TTDL_KERNEL_SUBMISSION
  319. #ifdef TTDL_DIAGNOSTICS
  320. /*******************************************************************************
  321. * FUNCTION: pt_erase_parameter_list
  322. *
  323. * SUMMARY: Empty out the entire parameter linked list of all parameter/value
  324. * pairs. In some test cases this functionality is needed to ensure DUT
  325. * returns to a virgin state after a reset and no parameters are restored.
  326. *
  327. * RETURN:
  328. * 0 = success
  329. * !0 = failure
  330. *
  331. * PARAMETERS:
  332. * *cd - pointer to core data
  333. ******************************************************************************/
  334. static int pt_erase_parameter_list(struct pt_core_data *cd)
  335. {
  336. struct param_node *pos, *temp;
  337. spin_lock(&cd->spinlock);
  338. list_for_each_entry_safe(pos, temp, &cd->param_list, node) {
  339. pt_debug(cd->dev, DL_INFO,
  340. "%s: Parameter Restore List - remove 0x%02x\n",
  341. __func__, pos->id);
  342. list_del(&pos->node);
  343. kfree(pos);
  344. }
  345. spin_unlock(&cd->spinlock);
  346. return 0;
  347. }
  348. /*******************************************************************************
  349. * FUNCTION: pt_count_parameter_list
  350. *
  351. * SUMMARY: Count the items in the RAM parameter restor list
  352. *
  353. * RETURN:
  354. * 0 = success
  355. * !0 = failure
  356. *
  357. * PARAMETERS:
  358. * *cd - pointer to core data
  359. ******************************************************************************/
  360. static int pt_count_parameter_list(struct pt_core_data *cd)
  361. {
  362. struct param_node *pos, *temp;
  363. int entries = 0;
  364. spin_lock(&cd->spinlock);
  365. list_for_each_entry_safe(pos, temp, &cd->param_list, node)
  366. entries++;
  367. spin_unlock(&cd->spinlock);
  368. return entries;
  369. }
  370. #endif /* TTDL_DIAGNOSTICS */
  371. #endif /* !TTDL_KERNEL_SUBMISSION */
  372. /*******************************************************************************
  373. * FUNCTION: request_exclusive
  374. *
  375. * SUMMARY: Request exclusive access to the DUT
  376. *
  377. * RETURN:
  378. * 0 = success
  379. * !0 = failure
  380. *
  381. * PARAMETERS:
  382. * *cd - pointer to core data
  383. * *ownptr - pointer to device
  384. * timeout_ms - Timeout value
  385. ******************************************************************************/
  386. int request_exclusive(struct pt_core_data *cd, void *ownptr,
  387. int timeout_ms)
  388. {
  389. int t = msecs_to_jiffies(timeout_ms);
  390. bool with_timeout = (timeout_ms != 0);
  391. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Request EXCLUSIVE t=%d\n",
  392. __func__, timeout_ms);
  393. mutex_lock(&cd->system_lock);
  394. if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
  395. cd->exclusive_dev = ownptr;
  396. goto exit;
  397. }
  398. cd->exclusive_waits++;
  399. wait:
  400. mutex_unlock(&cd->system_lock);
  401. if (with_timeout) {
  402. t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
  403. if (IS_TMO(t)) {
  404. pt_debug(cd->dev, DL_ERROR,
  405. "%s: tmo waiting exclusive access\n", __func__);
  406. return -ETIME;
  407. }
  408. } else {
  409. wait_event(cd->wait_q, !cd->exclusive_dev);
  410. }
  411. mutex_lock(&cd->system_lock);
  412. if (cd->exclusive_dev)
  413. goto wait;
  414. cd->exclusive_dev = ownptr;
  415. cd->exclusive_waits--;
  416. exit:
  417. mutex_unlock(&cd->system_lock);
  418. pt_debug(cd->dev, DL_DEBUG, "%s: request exclusive ok=%p\n",
  419. __func__, ownptr);
  420. return 0;
  421. }
  422. /*******************************************************************************
  423. * FUNCTION: release_exclusive_
  424. *
  425. * SUMMARY: Release exclusive access to the DUT
  426. *
  427. * RETURN:
  428. * 0 = success
  429. *
  430. * PARAMETERS:
  431. * *cd - pointer to core data
  432. * *ownptr - pointer to device
  433. ******************************************************************************/
  434. static int release_exclusive_(struct pt_core_data *cd, void *ownptr)
  435. {
  436. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Release EXCLUSIVE\n",
  437. __func__);
  438. if (cd->exclusive_dev != ownptr)
  439. return -EINVAL;
  440. pt_debug(cd->dev, DL_DEBUG, "%s: exclusive_dev %p freed\n",
  441. __func__, cd->exclusive_dev);
  442. cd->exclusive_dev = NULL;
  443. wake_up(&cd->wait_q);
  444. return 0;
  445. }
  446. /*******************************************************************************
  447. * FUNCTION: release_exclusive
  448. *
  449. * SUMMARY: Protected wrapper to release_exclusive_()
  450. *
  451. * RETURN:
  452. * 0 = success
  453. * !0 = failure
  454. *
  455. * PARAMETERS:
  456. * *cd - pointer to core data
  457. * *ownptr - pointer to device
  458. ******************************************************************************/
  459. int release_exclusive(struct pt_core_data *cd, void *ownptr)
  460. {
  461. int rc;
  462. mutex_lock(&cd->system_lock);
  463. rc = release_exclusive_(cd, ownptr);
  464. mutex_unlock(&cd->system_lock);
  465. return rc;
  466. }
  467. /*******************************************************************************
  468. * FUNCTION: pt_hid_create_cmd_and_send_
  469. *
  470. * SUMMARY: Send the HID command to the DUT
  471. *
  472. * RETURN:
  473. * 0 = success
  474. * !0 = failure
  475. *
  476. * PARAMETERS:
  477. * *cd - pointer to core data
  478. * *hid_cmd - pointer to the HID command to send
  479. ******************************************************************************/
  480. static int pt_hid_create_cmd_and_send_(struct pt_core_data *cd,
  481. struct pt_hid_cmd *hid_cmd)
  482. {
  483. int rc = 0;
  484. u8 *cmd;
  485. u16 cmd_length;
  486. u8 cmd_offset = 0;
  487. if (hid_cmd->descriptor) {
  488. cmd_length = 2; /* hid or report register */
  489. } else {
  490. cmd_length =
  491. 2 /* command register */
  492. + 2 /* command */
  493. + (hid_cmd->report_id >= 0XF ? 1 : 0) /* Report ID */
  494. + (hid_cmd->has_data_register ? 2 : 0) /* Data register */
  495. + hid_cmd->write_length; /* Data length */
  496. }
  497. cmd = kzalloc(cmd_length, GFP_KERNEL);
  498. if (!cmd)
  499. return -ENOMEM;
  500. /* hid & report descriptor doesn't require other field */
  501. if (hid_cmd->descriptor) {
  502. memcpy(&cmd[cmd_offset], &hid_cmd->descriptor, cmd_length);
  503. goto skip_other_field;
  504. }
  505. /* Set Command register */
  506. memcpy(&cmd[cmd_offset], &cd->hid_desc.command_register,
  507. sizeof(cd->hid_desc.command_register));
  508. cmd_offset += sizeof(cd->hid_desc.command_register);
  509. /* Set Command */
  510. SET_CMD_REPORT_TYPE(cmd[cmd_offset], hid_cmd->report_type);
  511. if (hid_cmd->report_id >= 0XF)
  512. SET_CMD_REPORT_ID(cmd[cmd_offset], 0xF);
  513. else
  514. SET_CMD_REPORT_ID(cmd[cmd_offset], hid_cmd->report_id);
  515. cmd_offset++;
  516. SET_CMD_OPCODE(cmd[cmd_offset], hid_cmd->opcode);
  517. cmd_offset++;
  518. if (hid_cmd->report_id >= 0XF) {
  519. cmd[cmd_offset] = hid_cmd->report_id;
  520. cmd_offset++;
  521. }
  522. /* Set Data register */
  523. if (hid_cmd->has_data_register) {
  524. memcpy(&cmd[cmd_offset], &cd->hid_desc.data_register,
  525. sizeof(cd->hid_desc.data_register));
  526. cmd_offset += sizeof(cd->hid_desc.data_register);
  527. }
  528. /* Set Data */
  529. if (hid_cmd->write_length && hid_cmd->write_buf) {
  530. memcpy(&cmd[cmd_offset], hid_cmd->write_buf,
  531. hid_cmd->write_length);
  532. cmd_offset += hid_cmd->write_length;
  533. }
  534. pt_debug(cd->dev, DL_INFO,
  535. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  536. __func__, cmd_length, hid_cmd->report_id);
  537. skip_other_field:
  538. pt_pr_buf(cd->dev, DL_DEBUG, cmd, cmd_length, ">>> CMD");
  539. rc = pt_adap_write_read_specific(cd, cmd_length, cmd, hid_cmd->read_buf,
  540. hid_cmd->read_length);
  541. if (rc)
  542. pt_debug(cd->dev, DL_ERROR,
  543. "%s: Fail pt_adap_transfer\n", __func__);
  544. kfree(cmd);
  545. return rc;
  546. }
  547. #ifdef TTDL_DIAGNOSTICS
  548. /*******************************************************************************
  549. * FUNCTION: pt_toggle_err_gpio
  550. *
  551. * SUMMARY: Toggles the pre-defined error GPIO
  552. *
  553. * RETURN: n/a
  554. *
  555. * PARAMETERS:
  556. * *cd - pointer to core data
  557. * type - type of err that occurred
  558. ******************************************************************************/
  559. void pt_toggle_err_gpio(struct pt_core_data *cd, u8 type)
  560. {
  561. pt_debug(cd->dev, DL_DEBUG, "%s called with type = %d\n",
  562. __func__, type);
  563. if (cd->err_gpio && type == cd->err_gpio_type) {
  564. pt_debug(cd->dev, DL_WARN, "%s: Toggle ERR GPIO\n", __func__);
  565. gpio_direction_output(cd->err_gpio,
  566. !gpio_get_value(cd->err_gpio));
  567. }
  568. }
  569. /*******************************************************************************
  570. * FUNCTION: _pt_request_toggle_err_gpio
  571. *
  572. * SUMMARY: Function pointer included in core_cmds to allow other modules
  573. * to request to toggle the err_gpio
  574. *
  575. * RETURN: n/a
  576. *
  577. * PARAMETERS:
  578. * *cd - pointer to core data
  579. * type - type of err that occurred
  580. ******************************************************************************/
  581. void _pt_request_toggle_err_gpio(struct device *dev, u8 type)
  582. {
  583. struct pt_core_data *cd = dev_get_drvdata(dev);
  584. pt_toggle_err_gpio(cd, type);
  585. }
  586. #endif /* TTDL_DIAGNOSTICS */
  587. /*******************************************************************************
  588. * FUNCTION: pt_hid_exec_cmd_and_wait_
  589. *
  590. * SUMMARY: Send the HID command to the DUT and wait for the response
  591. *
  592. * RETURN:
  593. * 0 = success
  594. * !0 = failure
  595. *
  596. * PARAMETERS:
  597. * *cd - pointer to core data
  598. * *hid_cmd - pointer to the HID command to send
  599. ******************************************************************************/
  600. static int pt_hid_exec_cmd_and_wait_(struct pt_core_data *cd,
  601. struct pt_hid_cmd *hid_cmd)
  602. {
  603. int rc = 0;
  604. int t;
  605. u16 timeout_ms;
  606. int *cmd_state;
  607. if (hid_cmd->reset_cmd)
  608. cmd_state = &cd->hid_reset_cmd_state;
  609. else
  610. cmd_state = &cd->hid_cmd_state;
  611. mutex_lock(&cd->system_lock);
  612. *cmd_state = 1;
  613. mutex_unlock(&cd->system_lock);
  614. rc = pt_hid_create_cmd_and_send_(cd, hid_cmd);
  615. if (rc)
  616. goto error;
  617. if (hid_cmd->timeout_ms)
  618. timeout_ms = hid_cmd->timeout_ms;
  619. else
  620. timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT;
  621. t = wait_event_timeout(cd->wait_q, (*cmd_state == 0),
  622. msecs_to_jiffies(timeout_ms));
  623. if (IS_TMO(t)) {
  624. #ifdef TTDL_DIAGNOSTICS
  625. cd->bus_transmit_error_count++;
  626. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  627. #endif /* TTDL_DIAGNOSTICS */
  628. pt_debug(cd->dev, DL_ERROR,
  629. "%s: HID output cmd execution timed out\n",
  630. __func__);
  631. rc = -ETIME;
  632. goto error;
  633. }
  634. goto exit;
  635. error:
  636. mutex_lock(&cd->system_lock);
  637. *cmd_state = 0;
  638. mutex_unlock(&cd->system_lock);
  639. exit:
  640. return rc;
  641. }
  642. /*******************************************************************************
  643. * FUNCTION: pt_hid_exec_cmd_no_wait_
  644. *
  645. * SUMMARY: The function works to send HID command and can read response
  646. * directly instead of waiting it to be received in interrupt function. It
  647. * assgins the read buffer to receive response in following condition:
  648. * 1) descriptor is assigned to get hid descriptor or report descripter
  649. * 2) output register is assigned for vendor-defined commands (TBD)
  650. *
  651. * NOTE: If no read buffer is assigned, it only perform send action.
  652. *
  653. * RETURN:
  654. * 0 = success
  655. * !0 = failure
  656. *
  657. * PARAMETERS:
  658. * *cd - pointer to core data
  659. * *hid_cmd - pointer to the HID command to send
  660. ******************************************************************************/
  661. static int pt_hid_exec_cmd_no_wait_(struct pt_core_data *cd,
  662. struct pt_hid_cmd *hid_cmd)
  663. {
  664. int rc = 0;
  665. if (hid_cmd->descriptor)
  666. hid_cmd->read_buf = cd->response_buf;
  667. rc = pt_hid_create_cmd_and_send_(cd, hid_cmd);
  668. return rc;
  669. }
  670. /*******************************************************************************
  671. * FUNCTION: pt_hid_send_command
  672. *
  673. * SUMMARY: Wrapper function to call pt_hid_exec_cmd_no_wait_() for HID protocol
  674. * and pt_hid_exec_cmd_and_wait_() for PIP protocol.
  675. *
  676. * RETURN:
  677. * 0 = success
  678. * !0 = failure
  679. *
  680. * PARAMETERS:
  681. * *cd - pointer to core data
  682. * *hid_cmd - pointer to the HID command to send
  683. ******************************************************************************/
  684. static int pt_hid_send_command(struct pt_core_data *cd,
  685. struct pt_hid_cmd *hid_cmd)
  686. {
  687. if (cd->dut_status.protocol_mode == PT_PROTOCOL_MODE_HID)
  688. return pt_hid_exec_cmd_no_wait_(cd, hid_cmd);
  689. else
  690. return pt_hid_exec_cmd_and_wait_(cd, hid_cmd);
  691. }
  692. /*******************************************************************************
  693. * FUNCTION: pt_hid_cmd_reset_
  694. *
  695. * SUMMARY: Send the HID RESET command to the DUT
  696. *
  697. * RETURN:
  698. * 0 = success
  699. * !0 = failure
  700. *
  701. * PARAMETERS:
  702. * *cd - pointer to core data
  703. ******************************************************************************/
  704. static int pt_hid_cmd_reset_(struct pt_core_data *cd)
  705. {
  706. struct pt_hid_cmd hid_cmd = {
  707. .opcode = HID_CMD_RESET,
  708. .reset_cmd = 1,
  709. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  710. };
  711. return pt_hid_send_command(cd, &hid_cmd);
  712. }
  713. /*******************************************************************************
  714. * FUNCTION: pt_hid_cmd_reset
  715. *
  716. * SUMMARY: Wrapper function for pt_hid_cmd_reset_ that guarantees exclusive
  717. * access.
  718. *
  719. * RETURN:
  720. * 0 = success
  721. * !0 = failure
  722. *
  723. * PARAMETERS:
  724. * *cd - pointer to core data
  725. ******************************************************************************/
  726. static int pt_hid_cmd_reset(struct pt_core_data *cd)
  727. {
  728. int rc = 0;
  729. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  730. if (rc < 0) {
  731. pt_debug(cd->dev, DL_ERROR,
  732. "%s: fail get exclusive ex=%p own=%p\n",
  733. __func__, cd->exclusive_dev, cd->dev);
  734. return rc;
  735. }
  736. pt_debug(cd->dev, DL_INFO, "%s: Send HID Reset command\n", __func__);
  737. rc = pt_hid_cmd_reset_(cd);
  738. if (release_exclusive(cd, cd->dev) < 0)
  739. pt_debug(cd->dev, DL_ERROR,
  740. "%s: fail to release exclusive\n", __func__);
  741. return rc;
  742. }
  743. /*******************************************************************************
  744. * FUNCTION: pt_hid_cmd_set_power_
  745. *
  746. * SUMMARY: Send hid cmd to set power state for the DUT and wait for response
  747. *
  748. * RETURN:
  749. * 0 = success
  750. * !0 = failure
  751. *
  752. * PARAMETERS:
  753. * *cd - pointer to core data
  754. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  755. ******************************************************************************/
  756. static int pt_hid_cmd_set_power_(struct pt_core_data *cd,
  757. u8 power_state)
  758. {
  759. int rc = 0;
  760. struct pt_hid_cmd hid_cmd = {
  761. .opcode = HID_CMD_SET_POWER,
  762. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  763. };
  764. hid_cmd.power_state = power_state;
  765. /* The chip won't give response if goes to Deep Standby */
  766. if (power_state == HID_POWER_STANDBY) {
  767. rc = pt_hid_exec_cmd_no_wait_(cd, &hid_cmd);
  768. if (rc)
  769. pt_debug(cd->dev, DL_ERROR,
  770. "%s: Failed to set power to state:%d\n",
  771. __func__, power_state);
  772. else
  773. cd->fw_sys_mode_in_standby_state = true;
  774. return rc;
  775. }
  776. cd->fw_sys_mode_in_standby_state = false;
  777. rc = pt_hid_send_command(cd, &hid_cmd);
  778. if (rc) {
  779. pt_debug(cd->dev, DL_ERROR,
  780. "%s: Failed to set power to state:%d\n",
  781. __func__, power_state);
  782. return rc;
  783. }
  784. /* HID COMMAND doesn't have a response */
  785. if (cd->dut_status.protocol_mode == PT_PROTOCOL_MODE_HID)
  786. return rc;
  787. /* validate */
  788. if ((cd->response_buf[2] != HID_RESPONSE_REPORT_ID)
  789. || ((cd->response_buf[3] & 0x3) != power_state)
  790. || ((cd->response_buf[4] & 0xF) != HID_CMD_SET_POWER))
  791. rc = -EINVAL;
  792. return rc;
  793. }
  794. #ifndef TTDL_KERNEL_SUBMISSION
  795. /*******************************************************************************
  796. * FUNCTION: pt_hid_cmd_set_power
  797. *
  798. * SUMMARY: Wrapper function for pt_hid_cmd_set_power_ that guarantees
  799. * exclusive access.
  800. *
  801. * RETURN:
  802. * 0 = success
  803. * !0 = failure
  804. *
  805. * PARAMETERS:
  806. * *cd - pointer to core data
  807. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  808. ******************************************************************************/
  809. static int pt_hid_cmd_set_power(struct pt_core_data *cd,
  810. u8 power_state)
  811. {
  812. int rc;
  813. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  814. if (rc < 0) {
  815. pt_debug(cd->dev, DL_ERROR,
  816. "%s: fail get exclusive ex=%p own=%p\n",
  817. __func__, cd->exclusive_dev, cd->dev);
  818. return rc;
  819. }
  820. rc = pt_hid_cmd_set_power_(cd, power_state);
  821. if (release_exclusive(cd, cd->dev) < 0)
  822. pt_debug(cd->dev, DL_ERROR,
  823. "%s: fail to release exclusive\n", __func__);
  824. return rc;
  825. }
  826. #endif /* !TTDL_KERNEL_SUBMISSION */
  827. static const u16 crc_table[16] = {
  828. 0x0000, 0x1021, 0x2042, 0x3063,
  829. 0x4084, 0x50a5, 0x60c6, 0x70e7,
  830. 0x8108, 0x9129, 0xa14a, 0xb16b,
  831. 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  832. };
  833. /*******************************************************************************
  834. * FUNCTION: _pt_compute_crc
  835. *
  836. * SUMMARY: Calculate CRC by CRC table.
  837. *
  838. * RETURN:
  839. * CRC calculation result
  840. *
  841. * PARAMETERS:
  842. * *buf - pointer to the data array to be calculated
  843. * size - size of data array
  844. ******************************************************************************/
  845. static u16 _pt_compute_crc(u8 *buf, u32 size)
  846. {
  847. u16 remainder = 0xFFFF;
  848. u16 xor_mask = 0x0000;
  849. u32 index;
  850. u32 byte_value;
  851. u32 table_index;
  852. u32 crc_bit_width = sizeof(u16) * 8;
  853. /* Divide the message by polynomial, via the table. */
  854. for (index = 0; index < size; index++) {
  855. byte_value = buf[index];
  856. table_index = ((byte_value >> 4) & 0x0F)
  857. ^ (remainder >> (crc_bit_width - 4));
  858. remainder = crc_table[table_index] ^ (remainder << 4);
  859. table_index = (byte_value & 0x0F)
  860. ^ (remainder >> (crc_bit_width - 4));
  861. remainder = crc_table[table_index] ^ (remainder << 4);
  862. }
  863. /* Perform the final remainder CRC. */
  864. return remainder ^ xor_mask;
  865. }
  866. u16 ccitt_Table[] = {
  867. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  868. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  869. 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  870. 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  871. 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  872. 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  873. 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  874. 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  875. 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  876. 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  877. 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  878. 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  879. 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  880. 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  881. 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  882. 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  883. 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  884. 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  885. 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  886. 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  887. 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  888. 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  889. 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  890. 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  891. 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  892. 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  893. 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  894. 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  895. 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  896. 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  897. 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  898. 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0,
  899. };
  900. /*******************************************************************************
  901. * FUNCTION: crc_ccitt_calculate
  902. *
  903. * SUMMARY: Calculate CRC with ccitt standard by CRC table.
  904. *
  905. * RETURN:
  906. * CRC calculation result
  907. *
  908. * PARAMETERS:
  909. * *q - pointer to the data array to be calculated
  910. * len - size of data array
  911. ******************************************************************************/
  912. static unsigned short crc_ccitt_calculate(unsigned char *q, int len)
  913. {
  914. unsigned short crc = 0xffff;
  915. while (len-- > 0)
  916. crc = ccitt_Table[(crc >> 8 ^ *q++) & 0xff] ^ (crc << 8);
  917. return crc;
  918. }
  919. /*******************************************************************************
  920. * FUNCTION: pt_pip2_cmd_calculate_crc
  921. *
  922. * SUMMARY: Calculate the CRC of a command packet
  923. *
  924. * RETURN: void
  925. *
  926. * PARAMETERS:
  927. * *cmd - pointer to command data
  928. * extra_bytes - Extra bytes included in command length
  929. ******************************************************************************/
  930. static void pt_pip2_cmd_calculate_crc(struct pip2_cmd_structure *cmd,
  931. u8 extra_bytes)
  932. {
  933. u8 buf[PT_MAX_PIP2_MSG_SIZE + 1] = {0};
  934. unsigned short crc;
  935. buf[0] = cmd->len & 0xff;
  936. buf[1] = (cmd->len & 0xff00) >> 8;
  937. buf[2] = cmd->seq;
  938. buf[3] = cmd->id;
  939. memcpy(&buf[4], cmd->data, cmd->len - extra_bytes);
  940. /* Calculate the CRC for the first 4 bytes above and the data payload */
  941. crc = crc_ccitt_calculate(buf, 4 + (cmd->len - extra_bytes));
  942. cmd->crc[0] = (crc & 0xff00) >> 8;
  943. cmd->crc[1] = (crc & 0xff);
  944. }
  945. /*******************************************************************************
  946. * FUNCTION: pt_pip2_get_next_cmd_seq
  947. *
  948. * SUMMARY: Gets the next sequence number for a PIP2 command. The sequence
  949. * number is a 3 bit value (bits [0-2]) but because TTDL will always have
  950. * the TAG bit set (bit 3), the counter starts at 0x08 and goes to 0x0F.
  951. * If the "force_pip2_seq" holds a valid seq value (0x08-0x0F) then do not
  952. * increment, just use the forced value.
  953. *
  954. * RETURN: Next command sequence number [0x08-0x0F]
  955. *
  956. * PARAMETERS:
  957. * *cd - pointer to core data
  958. ******************************************************************************/
  959. static u8 pt_pip2_get_next_cmd_seq(struct pt_core_data *cd)
  960. {
  961. #ifdef TTDL_DIAGNOSTICS
  962. if (cd->force_pip2_seq <= 0x07) {
  963. cd->pip2_cmd_tag_seq++;
  964. if (cd->pip2_cmd_tag_seq > 0x0F)
  965. cd->pip2_cmd_tag_seq = 0x08;
  966. } else {
  967. cd->pip2_cmd_tag_seq = cd->force_pip2_seq;
  968. }
  969. #else
  970. cd->pip2_cmd_tag_seq++;
  971. if (cd->pip2_cmd_tag_seq > 0x0F)
  972. cd->pip2_cmd_tag_seq = 0x08;
  973. #endif
  974. return cd->pip2_cmd_tag_seq;
  975. }
  976. /*
  977. * Following macros are to define the response time (the interval between PIP2
  978. * command finishes sending and INT pin falls). The unit is in microsecond.
  979. * It has different time settings between the solution GPIO polling and Bus
  980. * polling due to the considration for system load.
  981. */
  982. #ifdef PT_POLL_RESP_BY_BUS
  983. #define POLL_RETRY_DEFAULT_INTERVAL 50
  984. #define PIP2_RESP_DEFAULT_TIME_MIN 50
  985. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  986. #define PIP2_RESP_FILE_WRITE_TIME_MIN 220
  987. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  988. #else
  989. #define POLL_RETRY_DEFAULT_INTERVAL 20
  990. #define PIP2_RESP_DEFAULT_TIME_MIN 20
  991. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  992. #define PIP2_RESP_FILE_WRITE_TIME_MIN 20
  993. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  994. #endif
  995. /*
  996. * id: the command id defined in PIP2
  997. * response_len: the (maximum) length of response.
  998. * response_time_min: minimum response time in microsecond
  999. * response_time_max: maximum response time in microsecond
  1000. */
  1001. static const struct pip2_cmd_response_structure pip2_cmd_response[] = {
  1002. {.id = PIP2_CMD_ID_PING,
  1003. .response_len = 255,
  1004. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1005. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1006. {.id = PIP2_CMD_ID_STATUS,
  1007. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  1008. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1009. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1010. {.id = PIP2_CMD_ID_CTRL,
  1011. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1012. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1013. .response_time_max = PT_PIP2_CMD_FILE_ERASE_TIMEOUT},
  1014. {.id = PIP2_CMD_ID_CONFIG,
  1015. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1016. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1017. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1018. {.id = PIP2_CMD_ID_CLEAR,
  1019. .response_len = PIP2_EXTRA_BYTES_NUM + 0,
  1020. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1021. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1022. {.id = PIP2_CMD_ID_RESET,
  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_ID_VERSION,
  1027. .response_len = PIP2_EXTRA_BYTES_NUM + 23,
  1028. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1029. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1030. {.id = PIP2_CMD_ID_FILE_OPEN,
  1031. .response_len = PIP2_EXTRA_BYTES_NUM + 2,
  1032. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1033. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1034. {.id = PIP2_CMD_ID_FILE_CLOSE,
  1035. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1036. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1037. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1038. {.id = PIP2_CMD_ID_FILE_READ,
  1039. .response_len = 255,
  1040. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1041. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1042. {.id = PIP2_CMD_ID_FILE_WRITE,
  1043. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1044. .response_time_min = PIP2_RESP_FILE_WRITE_TIME_MIN,
  1045. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1046. {.id = PIP2_CMD_ID_FILE_IOCTL,
  1047. .response_len = PIP2_EXTRA_BYTES_NUM + 10,
  1048. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1049. .response_time_max = PIP2_RESP_FILE_IOCTL_TIME_MAX},
  1050. {.id = PIP2_CMD_ID_FLASH_INFO,
  1051. .response_len = PIP2_EXTRA_BYTES_NUM + 17,
  1052. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1053. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1054. {.id = PIP2_CMD_ID_EXECUTE,
  1055. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1056. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1057. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1058. {.id = PIP2_CMD_ID_GET_LAST_ERRNO,
  1059. .response_len = PIP2_EXTRA_BYTES_NUM + 3,
  1060. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1061. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1062. {.id = PIP2_CMD_ID_EXIT_HOST_MODE,
  1063. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1064. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1065. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1066. {.id = PIP2_CMD_ID_READ_GPIO,
  1067. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  1068. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1069. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1070. {.id = PIP2_CMD_EXECUTE_SCAN,
  1071. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1072. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1073. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1074. {.id = PIP2_CMD_SET_PARAMETER,
  1075. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1076. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1077. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1078. {.id = PIP2_CMD_GET_PARAMETER,
  1079. .response_len = PIP2_EXTRA_BYTES_NUM + 7,
  1080. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1081. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1082. {.id = PIP2_CMD_SET_DDI_REG,
  1083. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1084. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1085. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1086. {.id = PIP2_CMD_GET_DDI_REG,
  1087. .response_len = PIP2_EXTRA_BYTES_NUM + 249,
  1088. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1089. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1090. {.id = PIP2_CMD_ID_END,
  1091. .response_len = 255,
  1092. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1093. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX}
  1094. };
  1095. /*******************************************************************************
  1096. * FUNCTION: pt_pip2_get_cmd_response_len
  1097. *
  1098. * SUMMARY: Gets the expected response length based on the command ID
  1099. *
  1100. * RETURN: Expected response length
  1101. *
  1102. * PARAMETERS:
  1103. * id - Command ID (-1 means input ID is not in list of PIP2 command)
  1104. ******************************************************************************/
  1105. static int pt_pip2_get_cmd_response_len(u8 id)
  1106. {
  1107. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1108. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1109. p++;
  1110. if (p->id != PIP2_CMD_ID_END)
  1111. return p->response_len;
  1112. else
  1113. return -1;
  1114. }
  1115. /*******************************************************************************
  1116. * FUNCTION: pt_pip2_get_cmd_resp_time_min
  1117. *
  1118. * SUMMARY: Gets the minimum response time (the interval between PIP2 command
  1119. * finishes sending and INT pin falls) based on the command ID
  1120. *
  1121. * RETURN: Estimated minimum response time in microsecond
  1122. *
  1123. * PARAMETERS:
  1124. * id - Command ID
  1125. ******************************************************************************/
  1126. static u32 pt_pip2_get_cmd_resp_time_min(u8 id)
  1127. {
  1128. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1129. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1130. p++;
  1131. if (p->id != PIP2_CMD_ID_END)
  1132. return p->response_time_min;
  1133. else
  1134. return PIP2_RESP_DEFAULT_TIME_MIN;
  1135. }
  1136. /*******************************************************************************
  1137. * FUNCTION: pt_pip2_get_cmd_resp_time_max
  1138. *
  1139. * SUMMARY: Gets the maximum response time (the interval between PIP2 command
  1140. * finishes sending and INT pin falls) based on the command ID
  1141. *
  1142. * RETURN: Estimated maximum response time in microsecond
  1143. *
  1144. * PARAMETERS:
  1145. * id - Command ID
  1146. ******************************************************************************/
  1147. static u32 pt_pip2_get_cmd_resp_time_max(u8 id)
  1148. {
  1149. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1150. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1151. p++;
  1152. if (p->id != PIP2_CMD_ID_END)
  1153. return p->response_time_max;
  1154. else
  1155. return PIP2_RESP_DEFAULT_TIME_MAX;
  1156. }
  1157. /*******************************************************************************
  1158. * FUNCTION: pt_pip2_validate_response
  1159. *
  1160. * SUMMARY: Validate the response of PIP2 command.
  1161. *
  1162. * RETURN:
  1163. * 0 = success
  1164. * !0 = failure
  1165. *
  1166. * PARAMETERS:
  1167. * *cd - pointer to core data
  1168. * *pip2_cmd - pointer to PIP2 command to send
  1169. * *read_buf - pointer to response buffer
  1170. * actual_read_len - actual read length of the response
  1171. ******************************************************************************/
  1172. static int pt_pip2_validate_response(struct pt_core_data *cd,
  1173. struct pip2_cmd_structure *pip2_cmd, u8 *read_buf,
  1174. u16 actual_read_len)
  1175. {
  1176. int rc = 0;
  1177. u8 response_seq = 0;
  1178. u8 reserved_bits = 0;
  1179. u8 cmd_id = 0;
  1180. u8 response_bit = 0;
  1181. unsigned short calc_crc = 0;
  1182. unsigned short resp_crc = 0;
  1183. /* Verify the length of response buffer */
  1184. if (actual_read_len < PT_MIN_PIP2_PACKET_SIZE) {
  1185. pt_debug(cd->dev, DL_ERROR,
  1186. "%s cmd[0x%02X] read length ERR: read_len = %d\n",
  1187. __func__, pip2_cmd->id, actual_read_len);
  1188. rc = -EINVAL;
  1189. goto exit;
  1190. }
  1191. /* Verify the CRC */
  1192. calc_crc = crc_ccitt_calculate(read_buf, actual_read_len - 2);
  1193. resp_crc = read_buf[actual_read_len - 2] << 8;
  1194. resp_crc |= read_buf[actual_read_len - 1];
  1195. if (resp_crc != calc_crc) {
  1196. pt_debug(cd->dev, DL_ERROR,
  1197. "%s: cmd[0x%02X] CRC ERR: calc=0x%04X rsp=0x%04X\n",
  1198. __func__, pip2_cmd->id, calc_crc, resp_crc);
  1199. #ifdef TTDL_DIAGNOSTICS
  1200. cd->pip2_crc_error_count++;
  1201. #endif /* TTDL_DIAGNOSTICS */
  1202. rc = -EINVAL;
  1203. goto exit;
  1204. }
  1205. /* Verify the response bit is set */
  1206. response_bit = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x80;
  1207. if (!response_bit) {
  1208. pt_debug(cd->dev, DL_ERROR,
  1209. "%s cmd[0x%02X] response bit ERR: response_bit = %d\n",
  1210. __func__, pip2_cmd->id, response_bit);
  1211. rc = -EINVAL;
  1212. goto exit;
  1213. }
  1214. /* Verify the command ID matches from command to response */
  1215. cmd_id = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x7F;
  1216. if (cmd_id != pip2_cmd->id) {
  1217. pt_debug(cd->dev, DL_ERROR,
  1218. "%s cmd[0x%02X] command ID ERR: cmd_id = 0x%02X\n",
  1219. __func__, pip2_cmd->id, cmd_id);
  1220. rc = -EINVAL;
  1221. goto exit;
  1222. }
  1223. /* Verify the SEQ number matches from command to response */
  1224. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F;
  1225. if ((pip2_cmd->seq & 0x0F) != response_seq) {
  1226. pt_debug(cd->dev, DL_ERROR,
  1227. "%s cmd[0x%02X] send_seq = 0x%02X, resp_seq = 0x%02X\n",
  1228. __func__, pip2_cmd->id,
  1229. pip2_cmd->seq, response_seq);
  1230. rc = -EINVAL;
  1231. goto exit;
  1232. }
  1233. /* Verify the reserved bits are 0 */
  1234. reserved_bits = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0xF0;
  1235. if (reserved_bits)
  1236. pt_debug(cd->dev, DL_WARN,
  1237. "%s cmd[0x%02X] reserved_bits = 0x%02X\n",
  1238. __func__, pip2_cmd->id, reserved_bits);
  1239. exit:
  1240. if (rc)
  1241. pt_pr_buf(cd->dev, DL_WARN, cd->input_buf, actual_read_len,
  1242. "PIP RSP:");
  1243. return rc;
  1244. }
  1245. /*******************************************************************************
  1246. * FUNCTION: pt_hid_output_validate_bl_response
  1247. *
  1248. * SUMMARY: Validate the response of bootloader command.
  1249. *
  1250. * RETURN:
  1251. * 0 = success
  1252. * !0 = failure
  1253. *
  1254. * PARAMETERS:
  1255. * *cd - pointer to core data
  1256. * *hid_output - pointer to hid output data structure
  1257. ******************************************************************************/
  1258. static int pt_hid_output_validate_bl_response(
  1259. struct pt_core_data *cd,
  1260. struct pt_hid_output *hid_output)
  1261. {
  1262. u16 size;
  1263. u16 crc;
  1264. u8 status;
  1265. size = get_unaligned_le16(&cd->response_buf[0]);
  1266. if (hid_output->reset_expected && !size)
  1267. return 0;
  1268. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1269. != PT_PIP_BL_RESPONSE_REPORT_ID) {
  1270. pt_debug(cd->dev, DL_ERROR,
  1271. "%s: BL output response, wrong report_id\n", __func__);
  1272. return -EPROTO;
  1273. }
  1274. if (cd->response_buf[4] != PIP1_BL_SOP) {
  1275. pt_debug(cd->dev, DL_ERROR,
  1276. "%s: BL output response, wrong SOP\n", __func__);
  1277. return -EPROTO;
  1278. }
  1279. if (cd->response_buf[size - 1] != PIP1_BL_EOP) {
  1280. pt_debug(cd->dev, DL_ERROR,
  1281. "%s: BL output response, wrong EOP\n", __func__);
  1282. return -EPROTO;
  1283. }
  1284. crc = _pt_compute_crc(&cd->response_buf[4], size - 7);
  1285. if (cd->response_buf[size - 3] != LOW_BYTE(crc)
  1286. || cd->response_buf[size - 2] != HI_BYTE(crc)) {
  1287. pt_debug(cd->dev, DL_ERROR,
  1288. "%s: BL output response, wrong CRC 0x%X\n",
  1289. __func__, crc);
  1290. return -EPROTO;
  1291. }
  1292. status = cd->response_buf[5];
  1293. if (status) {
  1294. pt_debug(cd->dev, DL_ERROR,
  1295. "%s: BL output response, ERROR:%d\n",
  1296. __func__, status);
  1297. return -EPROTO;
  1298. }
  1299. return 0;
  1300. }
  1301. /*******************************************************************************
  1302. * FUNCTION: pt_hid_output_validate_app_response
  1303. *
  1304. * SUMMARY: Validate the response of application command.
  1305. *
  1306. * RETURN:
  1307. * 0 = success
  1308. * !0 = failure
  1309. *
  1310. * PARAMETERS:
  1311. * *cd - pointer to core data
  1312. * *hid_output - pointer to hid output data structure
  1313. ******************************************************************************/
  1314. static int pt_hid_output_validate_app_response(
  1315. struct pt_core_data *cd,
  1316. struct pt_hid_output *hid_output)
  1317. {
  1318. int command_code;
  1319. u16 size;
  1320. size = get_unaligned_le16(&cd->response_buf[0]);
  1321. if (hid_output->reset_expected && !size)
  1322. return 0;
  1323. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1324. != PT_PIP_NON_HID_RESPONSE_ID) {
  1325. pt_debug(cd->dev, DL_ERROR,
  1326. "%s: APP output response, wrong report_id\n", __func__);
  1327. return -EPROTO;
  1328. }
  1329. command_code = cd->response_buf[PIP1_RESP_COMMAND_ID_OFFSET]
  1330. & PIP1_RESP_COMMAND_ID_MASK;
  1331. if (command_code != hid_output->command_code) {
  1332. pt_debug(cd->dev, DL_ERROR,
  1333. "%s: APP output response, wrong command_code:%X\n",
  1334. __func__, command_code);
  1335. return -EPROTO;
  1336. }
  1337. return 0;
  1338. }
  1339. /*******************************************************************************
  1340. * FUNCTION: pt_check_set_parameter
  1341. *
  1342. * SUMMARY: Check command input and response for Set Parameter command.And
  1343. * store the parameter to the list for resume work if pass the check.
  1344. *
  1345. * PARAMETERS:
  1346. * *cd - pointer to core data
  1347. * *hid_output - pointer to hid output data structure
  1348. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1349. ******************************************************************************/
  1350. static void pt_check_set_parameter(struct pt_core_data *cd,
  1351. struct pt_hid_output *hid_output, bool raw)
  1352. {
  1353. u8 *param_buf;
  1354. u32 param_value = 0;
  1355. u8 param_size;
  1356. u8 param_id;
  1357. int i = 0;
  1358. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  1359. return;
  1360. /* Check command input for Set Parameter command */
  1361. if (raw && hid_output->length >= 10 && hid_output->length <= 13
  1362. && !memcmp(&hid_output->write_buf[0],
  1363. &cd->hid_desc.output_register,
  1364. sizeof(cd->hid_desc.output_register))
  1365. && hid_output->write_buf[4] ==
  1366. PT_PIP_NON_HID_COMMAND_ID
  1367. && hid_output->write_buf[6] ==
  1368. PIP1_CMD_ID_SET_PARAM)
  1369. param_buf = &hid_output->write_buf[7];
  1370. else if (!raw && hid_output->cmd_type == PIP1_CMD_TYPE_FW
  1371. && hid_output->command_code == PIP1_CMD_ID_SET_PARAM
  1372. && hid_output->write_length >= 3
  1373. && hid_output->write_length <= 6)
  1374. param_buf = &hid_output->write_buf[0];
  1375. else
  1376. return;
  1377. /* Get parameter ID, size and value */
  1378. param_id = param_buf[0];
  1379. param_size = param_buf[1];
  1380. if (param_size > 4) {
  1381. pt_debug(cd->dev, DL_ERROR,
  1382. "%s: Invalid parameter size\n", __func__);
  1383. return;
  1384. }
  1385. param_buf = &param_buf[2];
  1386. while (i < param_size)
  1387. param_value += *(param_buf++) << (8 * i++);
  1388. /* Check command response for Set Parameter command */
  1389. if (cd->response_buf[2] != PT_PIP_NON_HID_RESPONSE_ID
  1390. || (cd->response_buf[4] &
  1391. PIP1_RESP_COMMAND_ID_MASK) !=
  1392. PIP1_CMD_ID_SET_PARAM
  1393. || cd->response_buf[5] != param_id
  1394. || cd->response_buf[6] != param_size) {
  1395. pt_debug(cd->dev, DL_ERROR,
  1396. "%s: Set Parameter command not successful\n",
  1397. __func__);
  1398. return;
  1399. }
  1400. pt_add_parameter(cd, param_id, param_value, param_size);
  1401. }
  1402. /*******************************************************************************
  1403. * FUNCTION: pt_check_command
  1404. *
  1405. * SUMMARY: Check the output command. The function pt_check_set_parameter() is
  1406. * called here to check output command and store parameter to the list.
  1407. *
  1408. * PARAMETERS:
  1409. * *cd - pointer to core data
  1410. * *hid_output - pointer to hid output data structure
  1411. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1412. ******************************************************************************/
  1413. static void pt_check_command(struct pt_core_data *cd,
  1414. struct pt_hid_output *hid_output, bool raw)
  1415. {
  1416. pt_check_set_parameter(cd, hid_output, raw);
  1417. }
  1418. /*******************************************************************************
  1419. * FUNCTION: pt_hid_output_validate_response
  1420. *
  1421. * SUMMARY: Validate the response of application or bootloader command.
  1422. *
  1423. * RETURN:
  1424. * 0 = success
  1425. * !0 = failure
  1426. *
  1427. * PARAMETERS:
  1428. * *cd - pointer to core data
  1429. * *hid_output - pointer to hid output data structure
  1430. ******************************************************************************/
  1431. static int pt_hid_output_validate_response(struct pt_core_data *cd,
  1432. struct pt_hid_output *hid_output)
  1433. {
  1434. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1435. return pt_hid_output_validate_bl_response(cd, hid_output);
  1436. return pt_hid_output_validate_app_response(cd, hid_output);
  1437. }
  1438. /*******************************************************************************
  1439. * FUNCTION: pt_hid_send_output_user_
  1440. *
  1441. * SUMMARY: Blindly send user data to the DUT.
  1442. *
  1443. * RETURN:
  1444. * 0 = success
  1445. * !0 = failure
  1446. *
  1447. * PARAMETERS:
  1448. * *cd - pointer to core data
  1449. * *hid_output - pointer to the command to send
  1450. ******************************************************************************/
  1451. static int pt_hid_send_output_user_(struct pt_core_data *cd,
  1452. struct pt_hid_output *hid_output)
  1453. {
  1454. int rc = 0;
  1455. int cmd;
  1456. if (!hid_output->length || !hid_output->write_buf)
  1457. return -EINVAL;
  1458. if (cd->pip2_prot_active) {
  1459. cmd = hid_output->write_buf[PIP2_CMD_COMMAND_ID_OFFSET];
  1460. cmd &= PIP2_CMD_COMMAND_ID_MASK;
  1461. } else
  1462. cmd = hid_output->write_buf[PIP1_CMD_COMMAND_ID_OFFSET];
  1463. pt_debug(cd->dev, DL_INFO,
  1464. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1465. __func__, hid_output->length, cmd);
  1466. pt_pr_buf(cd->dev, DL_DEBUG, hid_output->write_buf,
  1467. hid_output->length, ">>> User CMD");
  1468. rc = pt_adap_write_read_specific(cd, hid_output->length,
  1469. hid_output->write_buf, NULL, 0);
  1470. if (rc)
  1471. pt_debug(cd->dev, DL_ERROR,
  1472. "%s: Fail pt_adap_transfer\n", __func__);
  1473. return rc;
  1474. }
  1475. /*******************************************************************************
  1476. * FUNCTION: pt_hid_send_output_user_and_wait_
  1477. *
  1478. * SUMMARY: Blindly send user data to the DUT and wait for the response.
  1479. *
  1480. * RETURN:
  1481. * 0 = success
  1482. * !0 = failure
  1483. *
  1484. * PARAMETERS:
  1485. * *cd - pointer to core data
  1486. * *hid_output - pointer to the command to send
  1487. ******************************************************************************/
  1488. static int pt_hid_send_output_user_and_wait_(struct pt_core_data *cd,
  1489. struct pt_hid_output *hid_output)
  1490. {
  1491. int rc = 0;
  1492. int t;
  1493. mutex_lock(&cd->system_lock);
  1494. cd->hid_cmd_state = PIP1_CMD_ID_USER_CMD + 1;
  1495. mutex_unlock(&cd->system_lock);
  1496. rc = pt_hid_send_output_user_(cd, hid_output);
  1497. if (rc)
  1498. goto error;
  1499. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1500. msecs_to_jiffies(cd->pip_cmd_timeout));
  1501. if (IS_TMO(t)) {
  1502. #ifdef TTDL_DIAGNOSTICS
  1503. cd->bus_transmit_error_count++;
  1504. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1505. #endif /* TTDL_DIAGNOSTICS */
  1506. pt_debug(cd->dev, DL_ERROR,
  1507. "%s: HID output cmd execution timed out\n",
  1508. __func__);
  1509. rc = -ETIME;
  1510. goto error;
  1511. }
  1512. pt_check_command(cd, hid_output, true);
  1513. goto exit;
  1514. error:
  1515. mutex_lock(&cd->system_lock);
  1516. cd->hid_cmd_state = 0;
  1517. mutex_unlock(&cd->system_lock);
  1518. exit:
  1519. return rc;
  1520. }
  1521. /*******************************************************************************
  1522. * FUNCTION: pt_check_irq_asserted
  1523. *
  1524. * SUMMARY: Checks if the IRQ GPIO is asserted or not. There are times when
  1525. * the FW can hold the INT line low ~150us after the read is complete.
  1526. * NOTE: if irq_stat is not defined this function will return false
  1527. *
  1528. * RETURN:
  1529. * true = IRQ asserted
  1530. * false = IRQ not asserted
  1531. *
  1532. * PARAMETERS:
  1533. * *cd - pointer to core data
  1534. ******************************************************************************/
  1535. static bool pt_check_irq_asserted(struct pt_core_data *cd)
  1536. {
  1537. #ifdef ENABLE_WORKAROUND_FOR_GLITCH_AFTER_BL_LAUNCH_APP
  1538. /*
  1539. * Workaround for FW defect, CDT165308
  1540. * bl_launch app creates a glitch in IRQ line
  1541. */
  1542. if (cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1
  1543. && cd->cpdata->irq_stat) {
  1544. /*
  1545. * in X1S panel and GC1546 panel, the width for the INT
  1546. * glitch is about 4us,the normal INT width of response
  1547. * will last more than 200us, so use 10us delay
  1548. * for distinguish the glitch the normal INT is enough.
  1549. */
  1550. udelay(10);
  1551. }
  1552. #endif
  1553. if (cd->cpdata->irq_stat) {
  1554. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1555. == PT_IRQ_ASSERTED_VALUE) {
  1556. /* Debounce to allow FW to release INT */
  1557. usleep_range(100, 200);
  1558. }
  1559. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1560. == PT_IRQ_ASSERTED_VALUE)
  1561. return true;
  1562. else
  1563. return false;
  1564. }
  1565. return true;
  1566. }
  1567. /*******************************************************************************
  1568. * FUNCTION: pt_flush_bus
  1569. *
  1570. * SUMMARY: Force flushing the bus by reading len bytes or forced 255 bytes
  1571. * Used if IRQ is found to be stuck low
  1572. *
  1573. * RETURN: Length of bytes read from bus
  1574. *
  1575. * PARAMETERS:
  1576. * *cd - pointer to core data
  1577. * flush_type - type of flush
  1578. * - PT_FLUSH_BUS_BASED_ON_LEN (two reads)
  1579. * - PT_FLUSH_BUS_FULL_256_READ
  1580. * *read_buf - pointer to store read data
  1581. ******************************************************************************/
  1582. static ssize_t pt_flush_bus(struct pt_core_data *cd,
  1583. u8 flush_type, u8 *read_buf)
  1584. {
  1585. u8 buf[PT_MAX_PIP2_MSG_SIZE];
  1586. u16 pip_len;
  1587. int bytes_read;
  1588. int rc = 0;
  1589. if (flush_type == PT_FLUSH_BUS_BASED_ON_LEN) {
  1590. #ifdef TTDL_PTVIRTDUT_SUPPORT
  1591. if (cd->route_bus_virt_dut)
  1592. rc = pt_adap_read_default(cd, buf,
  1593. PT_MAX_PIP2_MSG_SIZE);
  1594. else
  1595. rc = pt_adap_read_default(cd, buf, 2);
  1596. #else
  1597. rc = pt_adap_read_default(cd, buf, 2);
  1598. #endif /* TTDL_PTVIRTDUT_SUPPORT */
  1599. if (rc) {
  1600. bytes_read = 0;
  1601. goto exit;
  1602. }
  1603. pip_len = get_unaligned_le16(&buf[0]);
  1604. if (pip_len == 2 || pip_len >= PT_PIP_1P7_EMPTY_BUF) {
  1605. #ifdef TTDL_DIAGNOSTICS
  1606. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EMPTY_PACKET);
  1607. #endif
  1608. bytes_read = 2;
  1609. pt_debug(cd->dev, DL_INFO,
  1610. "%s: Empty buf detected - len=0x%04X\n",
  1611. __func__, pip_len);
  1612. } else if (pip_len == 0) {
  1613. bytes_read = 0;
  1614. pt_debug(cd->dev, DL_INFO,
  1615. "%s: Sentinel detected\n", __func__);
  1616. } else if (pip_len > PT_MAX_PIP2_MSG_SIZE) {
  1617. pt_debug(cd->dev, DL_ERROR,
  1618. "%s: Illegal len=0x%04x, force %d byte read\n",
  1619. __func__, pip_len, PT_MAX_PIP2_MSG_SIZE);
  1620. rc = pt_adap_read_default(cd, buf,
  1621. PT_MAX_PIP2_MSG_SIZE);
  1622. if (!rc)
  1623. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1624. else
  1625. bytes_read = 0;
  1626. } else {
  1627. pt_debug(cd->dev, DL_INFO,
  1628. "%s: Flush read of %d bytes...\n",
  1629. __func__, pip_len);
  1630. #ifdef TTDL_PTVIRTDUT_SUPPORT
  1631. rc = 0;
  1632. if (cd->route_bus_virt_dut)
  1633. bytes_read = pip_len;
  1634. else
  1635. rc = pt_adap_read_default(cd, buf, pip_len);
  1636. #else
  1637. rc = pt_adap_read_default(cd, buf, pip_len);
  1638. #endif /* TTDL_PTVIRTDUT_SUPPORT */
  1639. if (!rc)
  1640. bytes_read = pip_len;
  1641. else
  1642. bytes_read = 0;
  1643. }
  1644. } else {
  1645. pt_debug(cd->dev, DL_INFO,
  1646. "%s: Forced flush of max %d bytes...\n",
  1647. __func__, PT_MAX_PIP2_MSG_SIZE);
  1648. rc = pt_adap_read_default(cd, buf, PT_MAX_PIP2_MSG_SIZE);
  1649. if (!rc)
  1650. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1651. else
  1652. bytes_read = 0;
  1653. }
  1654. if (read_buf && (bytes_read > 3))
  1655. memcpy(read_buf, buf, bytes_read);
  1656. exit:
  1657. return bytes_read;
  1658. }
  1659. /*******************************************************************************
  1660. * FUNCTION: pt_flush_bus_if_irq_asserted
  1661. *
  1662. * SUMMARY: This function will flush the active bus if the INT is found to be
  1663. * asserted.
  1664. *
  1665. * RETURN: bytes cleared from bus
  1666. *
  1667. * PARAMETERS:
  1668. * *cd - pointer the core data structure
  1669. * flush_type - type of flush
  1670. * - PT_FLUSH_BUS_BASED_ON_LEN
  1671. * - PT_FLUSH_BUS_FULL_256_READ
  1672. ******************************************************************************/
  1673. static int pt_flush_bus_if_irq_asserted(struct pt_core_data *cd, u8 flush_type)
  1674. {
  1675. int count = 0;
  1676. int bytes_read = 0;
  1677. while (pt_check_irq_asserted(cd) && count < 5) {
  1678. count++;
  1679. bytes_read = pt_flush_bus(cd, flush_type, NULL);
  1680. if (bytes_read) {
  1681. pt_debug(cd->dev, DL_WARN,
  1682. "%s: Cleared %d bytes off bus\n",
  1683. __func__, bytes_read);
  1684. }
  1685. }
  1686. if (pt_check_irq_asserted(cd)) {
  1687. pt_debug(cd->dev, DL_ERROR,
  1688. "%s: IRQ still asserted, %d bytes read\n",
  1689. __func__, bytes_read);
  1690. } else {
  1691. pt_debug(cd->dev, DL_INFO,
  1692. "%s: IRQ cleared, %d bytes read\n",
  1693. __func__, bytes_read);
  1694. }
  1695. return bytes_read;
  1696. }
  1697. /*******************************************************************************
  1698. * FUNCTION: pt_hid_send_output_
  1699. *
  1700. * SUMMARY: Send a touch application command to the DUT
  1701. *
  1702. * RETURN:
  1703. * 0 = success
  1704. * !0 = failure
  1705. *
  1706. * PARAMETERS:
  1707. * *cd - pointer to core data
  1708. * *hid_output - pointer to the command to send
  1709. ******************************************************************************/
  1710. static int pt_hid_send_output_(struct pt_core_data *cd,
  1711. struct pt_hid_output *hid_output)
  1712. {
  1713. int rc = 0;
  1714. u8 *cmd;
  1715. u16 length;
  1716. u16 crc;
  1717. u8 report_id;
  1718. u8 cmd_offset = 0;
  1719. u8 cmd_allocated = 0;
  1720. #ifdef FUTURE
  1721. /*
  1722. * *** TODO - Determine side effects of adding this safety net ***
  1723. * If IRQ is already asserted due to a pending report, it must be
  1724. * cleared before sending command.
  1725. */
  1726. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  1727. #endif
  1728. switch (hid_output->cmd_type) {
  1729. case PIP1_CMD_TYPE_FW:
  1730. report_id = PT_PIP_NON_HID_COMMAND_ID;
  1731. length = 5;
  1732. break;
  1733. case PIP1_CMD_TYPE_BL:
  1734. report_id = PT_PIP_BL_COMMAND_REPORT_ID;
  1735. length = 11 /* 5 + SOP + LEN(2) + CRC(2) + EOP */;
  1736. break;
  1737. default:
  1738. return -EINVAL;
  1739. }
  1740. length += hid_output->write_length;
  1741. if (length + 2 > PT_PREALLOCATED_CMD_BUFFER) {
  1742. cmd = kzalloc(length + 2, GFP_KERNEL);
  1743. if (!cmd)
  1744. return -ENOMEM;
  1745. cmd_allocated = 1;
  1746. } else {
  1747. cmd = cd->cmd_buf;
  1748. }
  1749. /* Set Output register */
  1750. memcpy(&cmd[cmd_offset], &cd->hid_desc.output_register,
  1751. sizeof(cd->hid_desc.output_register));
  1752. cmd_offset += sizeof(cd->hid_desc.output_register);
  1753. cmd[cmd_offset++] = LOW_BYTE(length);
  1754. cmd[cmd_offset++] = HI_BYTE(length);
  1755. cmd[cmd_offset++] = report_id;
  1756. cmd[cmd_offset++] = 0x0; /* reserved */
  1757. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1758. cmd[cmd_offset++] = PIP1_BL_SOP;
  1759. cmd[cmd_offset++] = hid_output->command_code;
  1760. /* Set Data Length for bootloader */
  1761. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1762. cmd[cmd_offset++] = LOW_BYTE(hid_output->write_length);
  1763. cmd[cmd_offset++] = HI_BYTE(hid_output->write_length);
  1764. }
  1765. /* Set Data */
  1766. if (hid_output->write_length && hid_output->write_buf) {
  1767. memcpy(&cmd[cmd_offset], hid_output->write_buf,
  1768. hid_output->write_length);
  1769. cmd_offset += hid_output->write_length;
  1770. }
  1771. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1772. crc = _pt_compute_crc(&cmd[6],
  1773. hid_output->write_length + 4);
  1774. cmd[cmd_offset++] = LOW_BYTE(crc);
  1775. cmd[cmd_offset++] = HI_BYTE(crc);
  1776. cmd[cmd_offset++] = PIP1_BL_EOP;
  1777. }
  1778. pt_debug(cd->dev, DL_INFO,
  1779. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1780. __func__, length + 2, hid_output->command_code);
  1781. pt_pr_buf(cd->dev, DL_DEBUG, cmd, length + 2, ">>> CMD");
  1782. rc = pt_adap_write_read_specific(cd, length + 2, cmd, NULL, 0);
  1783. if (rc)
  1784. pt_debug(cd->dev, DL_ERROR,
  1785. "%s: Fail pt_adap_transfer rc=%d\n", __func__, rc);
  1786. if (cmd_allocated)
  1787. kfree(cmd);
  1788. return rc;
  1789. }
  1790. /*******************************************************************************
  1791. * FUNCTION: pt_pip1_send_output_and_wait_
  1792. *
  1793. * SUMMARY: Send valid PIP1 command to the DUT and wait for the response.
  1794. *
  1795. * RETURN:
  1796. * 0 = success
  1797. * !0 = failure
  1798. *
  1799. * PARAMETERS:
  1800. * *cd - pointer to core data
  1801. * *hid_output - pointer to the command to send
  1802. ******************************************************************************/
  1803. static int pt_pip1_send_output_and_wait_(struct pt_core_data *cd,
  1804. struct pt_hid_output *hid_output)
  1805. {
  1806. int rc = 0;
  1807. int t;
  1808. u16 timeout_ms;
  1809. mutex_lock(&cd->system_lock);
  1810. cd->hid_cmd_state = hid_output->command_code + 1;
  1811. mutex_unlock(&cd->system_lock);
  1812. if (hid_output->timeout_ms)
  1813. timeout_ms = hid_output->timeout_ms;
  1814. else
  1815. timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT;
  1816. rc = pt_hid_send_output_(cd, hid_output);
  1817. if (rc)
  1818. goto error;
  1819. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1820. msecs_to_jiffies(timeout_ms));
  1821. if (IS_TMO(t)) {
  1822. #ifdef TTDL_DIAGNOSTICS
  1823. cd->bus_transmit_error_count++;
  1824. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1825. #endif /* TTDL_DIAGNOSTICS */
  1826. pt_debug(cd->dev, DL_ERROR,
  1827. "%s: HID output cmd execution timed out (%dms)\n",
  1828. __func__, timeout_ms);
  1829. rc = -ETIME;
  1830. goto error;
  1831. }
  1832. if (!hid_output->novalidate)
  1833. rc = pt_hid_output_validate_response(cd, hid_output);
  1834. pt_check_command(cd, hid_output, false);
  1835. goto exit;
  1836. error:
  1837. mutex_lock(&cd->system_lock);
  1838. cd->hid_cmd_state = 0;
  1839. mutex_unlock(&cd->system_lock);
  1840. exit:
  1841. return rc;
  1842. }
  1843. /*******************************************************************************
  1844. * FUNCTION: pt_hid_output_user_cmd_
  1845. *
  1846. * SUMMARY: Load the write buffer into a HID structure and send it as a HID cmd
  1847. * to the DUT waiting for the response and loading it into the read buffer
  1848. *
  1849. * RETURN:
  1850. * 0 = success
  1851. * !0 = failure
  1852. *
  1853. * PARAMETERS:
  1854. * *cd - pointer to core data
  1855. * read_len - expected read length of the response
  1856. * *read_buf - pointer to where the response will be loaded
  1857. * write_len - length of the write buffer
  1858. * *write_buf - pointer to the write buffer
  1859. * *actual_read_len - pointer to the actual amount of data read back
  1860. ******************************************************************************/
  1861. static int pt_hid_output_user_cmd_(struct pt_core_data *cd,
  1862. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1863. u16 *actual_read_len)
  1864. {
  1865. int rc = 0;
  1866. u16 size;
  1867. struct pt_hid_output hid_output = {
  1868. .length = write_len,
  1869. .write_buf = write_buf,
  1870. };
  1871. #ifdef TTHE_TUNER_SUPPORT
  1872. if (!cd->pip2_send_user_cmd) {
  1873. int command_code = 0;
  1874. int len;
  1875. /* Print up to cmd ID */
  1876. len = PIP1_CMD_COMMAND_ID_OFFSET + 1;
  1877. if (write_len < len)
  1878. len = write_len;
  1879. else
  1880. command_code = write_buf[PIP1_CMD_COMMAND_ID_OFFSET]
  1881. & PIP1_CMD_COMMAND_ID_MASK;
  1882. /* Don't print EXEC_PANEL_SCAN & RETRIEVE_PANEL_SCAN commands */
  1883. if (command_code != PIP1_CMD_ID_EXEC_PANEL_SCAN &&
  1884. command_code != PIP1_CMD_ID_RETRIEVE_PANEL_SCAN)
  1885. tthe_print(cd, write_buf, len, "CMD=");
  1886. }
  1887. #endif
  1888. rc = pt_hid_send_output_user_and_wait_(cd, &hid_output);
  1889. if (rc)
  1890. return rc;
  1891. /* Get the response size from the first 2 bytes in the response */
  1892. size = get_unaligned_le16(&cd->response_buf[0]);
  1893. /* Ensure size is not greater than max buffer size */
  1894. if (size > PT_MAX_PIP2_MSG_SIZE)
  1895. size = PT_MAX_PIP2_MSG_SIZE;
  1896. /* Minimum size to read is the 2 byte len field */
  1897. if (size == 0)
  1898. size = 2;
  1899. if (size > read_len) {
  1900. pt_debug(cd->dev, DL_ERROR,
  1901. "%s: PIP2 len field=%d, requested read_len=%d\n",
  1902. __func__, size, read_len);
  1903. *actual_read_len = 0;
  1904. return -EIO;
  1905. }
  1906. memcpy(read_buf, cd->response_buf, size);
  1907. *actual_read_len = size;
  1908. return 0;
  1909. }
  1910. /*******************************************************************************
  1911. * FUNCTION: pt_hid_output_user_cmd
  1912. *
  1913. * SUMMARY: Protected call to pt_hid_output_user_cmd_ by exclusive access to
  1914. * the DUT.
  1915. *
  1916. * RETURN:
  1917. * 0 = success
  1918. * !0 = failure
  1919. *
  1920. * PARAMETERS:
  1921. * *cd - pointer to core data
  1922. * read_len - length of data to read
  1923. * *read_buf - pointer to store read data
  1924. * write_len - length of data to write
  1925. * *write_buf - pointer to buffer to write
  1926. * *actual_read_len - pointer to store data length actually read
  1927. ******************************************************************************/
  1928. static int pt_hid_output_user_cmd(struct pt_core_data *cd,
  1929. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1930. u16 *actual_read_len)
  1931. {
  1932. int rc;
  1933. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  1934. if (rc < 0) {
  1935. pt_debug(cd->dev, DL_ERROR,
  1936. "%s: fail get exclusive ex=%p own=%p\n",
  1937. __func__, cd->exclusive_dev, cd->dev);
  1938. return rc;
  1939. }
  1940. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1941. write_len, write_buf, actual_read_len);
  1942. if (release_exclusive(cd, cd->dev) < 0)
  1943. pt_debug(cd->dev, DL_ERROR,
  1944. "%s: fail to release exclusive\n", __func__);
  1945. return rc;
  1946. }
  1947. /*******************************************************************************
  1948. * FUNCTION: _pt_request_pip2_send_cmd
  1949. *
  1950. * SUMMARY: Writes a PIP2 command packet to DUT, then waits for the
  1951. * interrupt and reads response data to read_buf
  1952. *
  1953. * RETURN:
  1954. * 0 = success
  1955. * !0 = failure
  1956. *
  1957. * PARAMETERS:
  1958. * *dev - pointer to device structure
  1959. * protect - flag to run in protected mode
  1960. * id - ID of PIP command
  1961. * *data - pointer to PIP data payload
  1962. * report_body_len - report length
  1963. * *read_buf - pointer to response buffer
  1964. * *actual_read_len - pointer to response buffer length
  1965. ******************************************************************************/
  1966. static int _pt_request_pip2_send_cmd(struct device *dev,
  1967. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  1968. u16 *actual_read_len)
  1969. {
  1970. struct pt_core_data *cd = dev_get_drvdata(dev);
  1971. struct pip2_cmd_structure pip2_cmd;
  1972. int rc = 0;
  1973. int i = 0;
  1974. int j = 0;
  1975. u16 write_len;
  1976. u8 *write_buf = NULL;
  1977. u16 read_len;
  1978. u8 extra_bytes;
  1979. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  1980. /* Hard coded register for PIP2.x */
  1981. pip2_cmd.reg[0] = 0x01;
  1982. pip2_cmd.reg[1] = 0x01;
  1983. /*
  1984. * For PIP2.1+ the length field value includes itself:
  1985. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  1986. *
  1987. * The overall write length must include only the register:
  1988. * ADD 2: 2 (Register)
  1989. */
  1990. extra_bytes = 6;
  1991. write_len = 2;
  1992. /* PIP2 the CMD ID is a 7bit field */
  1993. if (id > PIP2_CMD_ID_END) {
  1994. pt_debug(dev, DL_WARN, "%s: Invalid PIP2 CMD ID 0x%02X\n",
  1995. __func__, id);
  1996. rc = -EINVAL;
  1997. goto exit;
  1998. }
  1999. pip2_cmd.len = report_body_len + extra_bytes;
  2000. pip2_cmd.id = id & PIP2_CMD_COMMAND_ID_MASK;
  2001. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  2002. pip2_cmd.data = data;
  2003. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  2004. /* Add the command length to the extra bytes based on PIP version */
  2005. write_len += pip2_cmd.len;
  2006. pt_debug(dev, DL_INFO, "%s Length Field: %d, Write Len: %d",
  2007. __func__, pip2_cmd.len, write_len);
  2008. write_buf = kzalloc(write_len, GFP_KERNEL);
  2009. if (write_buf == NULL) {
  2010. rc = -ENOMEM;
  2011. goto exit;
  2012. }
  2013. write_buf[i++] = pip2_cmd.reg[0];
  2014. write_buf[i++] = pip2_cmd.reg[1];
  2015. write_buf[i++] = pip2_cmd.len & 0xff;
  2016. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  2017. write_buf[i++] = pip2_cmd.seq;
  2018. write_buf[i++] = pip2_cmd.id;
  2019. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  2020. write_buf[j] = pip2_cmd.data[j-i];
  2021. write_buf[j++] = pip2_cmd.crc[0];
  2022. write_buf[j++] = pip2_cmd.crc[1];
  2023. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  2024. if (read_len < 0)
  2025. read_len = 255;
  2026. pt_debug(dev, DL_INFO,
  2027. "%s cmd_id[0x%02X] expected response length:%d ",
  2028. __func__, pip2_cmd.id, read_len);
  2029. /*
  2030. * All PIP2 commands come through this function.
  2031. * Set flag for PIP2.x interface to allow response parsing to know
  2032. * how to decode the protocol header.
  2033. */
  2034. mutex_lock(&cd->system_lock);
  2035. cd->pip2_prot_active = true;
  2036. cd->pip2_send_user_cmd = true;
  2037. mutex_unlock(&cd->system_lock);
  2038. if (protect == PT_CORE_CMD_PROTECTED)
  2039. rc = pt_hid_output_user_cmd(cd, read_len, read_buf,
  2040. write_len, write_buf, actual_read_len);
  2041. else
  2042. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  2043. write_len, write_buf, actual_read_len);
  2044. if (rc) {
  2045. pt_debug(dev, DL_ERROR,
  2046. "%s: nonhid_cmd->user_cmd() Error = %d\n",
  2047. __func__, rc);
  2048. goto exit;
  2049. }
  2050. rc = pt_pip2_validate_response(cd, &pip2_cmd, read_buf,
  2051. *actual_read_len);
  2052. exit:
  2053. mutex_lock(&cd->system_lock);
  2054. cd->pip2_prot_active = false;
  2055. cd->pip2_send_user_cmd = false;
  2056. mutex_unlock(&cd->system_lock);
  2057. kfree(write_buf);
  2058. return rc;
  2059. }
  2060. /*******************************************************************************
  2061. * FUNCTION: _pt_pip2_send_cmd_no_int
  2062. *
  2063. * SUMMARY: Writes a PIP2 command packet to DUT, then poll the response and
  2064. * reads response data to read_buf if response is available.
  2065. *
  2066. * NOTE:
  2067. * Interrupt MUST be disabled before to call this function.
  2068. *
  2069. * RETURN:
  2070. * 0 = success
  2071. * !0 = failure
  2072. *
  2073. * PARAMETERS:
  2074. * *dev - pointer to device structure
  2075. * protect - flag to run in protected mode
  2076. * id - ID of PIP command
  2077. * *data - pointer to PIP data payload
  2078. * report_body_len - report length
  2079. * *read_buf - pointer to response buffer
  2080. * *actual_read_len - pointer to response buffer length
  2081. ******************************************************************************/
  2082. static int _pt_pip2_send_cmd_no_int(struct device *dev,
  2083. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  2084. u16 *actual_read_len)
  2085. {
  2086. int max_retry = 0;
  2087. int retry = 0;
  2088. int rc = 0;
  2089. int i = 0;
  2090. int j = 0;
  2091. u16 write_len;
  2092. u8 *write_buf = NULL;
  2093. u16 read_len;
  2094. u16 size = 0;
  2095. u8 response_seq = 0;
  2096. u8 extra_bytes;
  2097. u32 retry_interval = 0;
  2098. u32 retry_total_time = 0;
  2099. u32 resp_time_min = pt_pip2_get_cmd_resp_time_min(id);
  2100. u32 resp_time_max = pt_pip2_get_cmd_resp_time_max(id);
  2101. struct pt_core_data *cd = dev_get_drvdata(dev);
  2102. struct pip2_cmd_structure pip2_cmd;
  2103. if (protect == PT_CORE_CMD_PROTECTED) {
  2104. rc = request_exclusive(cd,
  2105. cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2106. if (rc < 0) {
  2107. pt_debug(cd->dev, DL_ERROR,
  2108. "%s: fail get exclusive ex=%p own=%p\n",
  2109. __func__, cd->exclusive_dev, cd->dev);
  2110. return rc;
  2111. }
  2112. }
  2113. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  2114. /* Hard coded register for PIP2.x */
  2115. pip2_cmd.reg[0] = 0x01;
  2116. pip2_cmd.reg[1] = 0x01;
  2117. /*
  2118. * For PIP2.1+ the length field value includes itself:
  2119. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  2120. *
  2121. * The overall write length must include only the register:
  2122. * ADD 2: 2 (Register)
  2123. */
  2124. extra_bytes = 6;
  2125. write_len = 2;
  2126. pip2_cmd.len = report_body_len + extra_bytes;
  2127. pip2_cmd.id = id;
  2128. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  2129. pip2_cmd.data = data;
  2130. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  2131. /* Add the command length to the extra bytes based on PIP version */
  2132. write_len += pip2_cmd.len;
  2133. write_buf = kzalloc(write_len, GFP_KERNEL);
  2134. if (write_buf == NULL) {
  2135. rc = -ENOMEM;
  2136. goto exit;
  2137. }
  2138. write_buf[i++] = pip2_cmd.reg[0];
  2139. write_buf[i++] = pip2_cmd.reg[1];
  2140. write_buf[i++] = pip2_cmd.len & 0xff;
  2141. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  2142. write_buf[i++] = pip2_cmd.seq;
  2143. write_buf[i++] = pip2_cmd.id;
  2144. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  2145. write_buf[j] = pip2_cmd.data[j-i];
  2146. write_buf[j++] = pip2_cmd.crc[0];
  2147. write_buf[j++] = pip2_cmd.crc[1];
  2148. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  2149. if (read_len < 0)
  2150. read_len = 255;
  2151. pt_debug(dev, DL_INFO,
  2152. "%s: ATM - cmd_id[0x%02X] expected response length:%d ",
  2153. __func__, pip2_cmd.id, read_len);
  2154. pt_pr_buf(cd->dev, DL_DEBUG, write_buf, write_len, ">>> NO_INT CMD");
  2155. rc = pt_adap_write_read_specific(cd, write_len, write_buf, NULL, 0);
  2156. if (rc) {
  2157. pt_debug(dev, DL_ERROR,
  2158. "%s: SPI write Error = %d\n",
  2159. __func__, rc);
  2160. goto exit;
  2161. }
  2162. #ifdef PT_POLL_RESP_BY_BUS
  2163. /*
  2164. * Frequent bus read can increase system load obviously. The expected
  2165. * first bus read should be valid and timely. The tollerance for
  2166. * usleep_range should be limited. The minimum response delay (between
  2167. * command finishes sending and INT pin falls) is less than 50
  2168. * microseconds. So the 10 microseconds should be maximum tollerance
  2169. * with the consideration that the unit to calculate the response delay
  2170. * is 10 microseconds and more precise is not necessary. Every
  2171. * additional 10 microseconds only contribute less than 3 milliseconds
  2172. * for whole BL.
  2173. */
  2174. usleep_range(resp_time_min, resp_time_min+10);
  2175. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2176. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2177. rc = pt_adap_read_default(cd, read_buf, read_len);
  2178. if (rc) {
  2179. pt_debug(dev, DL_ERROR, "%s: SPI read Error = %d\n",
  2180. __func__, rc);
  2181. break;
  2182. }
  2183. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET];
  2184. size = get_unaligned_le16(&read_buf[0]);
  2185. if ((size <= read_len) &&
  2186. (size >= PIP2_EXTRA_BYTES_NUM) &&
  2187. (pip2_cmd.seq & 0x07) == (response_seq & 0x07)) {
  2188. break;
  2189. }
  2190. /*
  2191. * To reduce the bus and system load, increase the sleep
  2192. * step gradually:
  2193. * 1 ~ 19 : step=50 us, sleep_us=[50, 100, 150, 200, ..950]
  2194. * 20 ~ 39 : step=1000 us, sleep_us=[1950, 2950, ...20950]
  2195. * 40 ~ MAX: step=50 ms, sleep_ms=[71, 121, 191,..]
  2196. */
  2197. retry++;
  2198. if (retry < 20) {
  2199. retry_interval += POLL_RETRY_DEFAULT_INTERVAL;
  2200. usleep_range(retry_interval,
  2201. retry_interval + POLL_RETRY_DEFAULT_INTERVAL);
  2202. } else if (retry < 40) {
  2203. retry_interval += 1000;
  2204. usleep_range(retry_interval, retry_interval + 1000);
  2205. } else {
  2206. retry_interval += 50000;
  2207. msleep(retry_interval/1000);
  2208. }
  2209. retry_total_time += retry_interval;
  2210. }
  2211. #else
  2212. /*
  2213. * Frequent GPIO read will not increase CPU/system load heavily if the
  2214. * interval is longer than 10 us, so it is safe to poll GPIO with a
  2215. * fixed interval: 20 us.
  2216. */
  2217. usleep_range(resp_time_min, resp_time_min+10);
  2218. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2219. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2220. if (!gpio_get_value(cd->cpdata->irq_gpio)) {
  2221. rc = pt_adap_read_default(cd, read_buf, read_len);
  2222. size = get_unaligned_le16(&read_buf[0]);
  2223. if (rc)
  2224. pt_debug(dev, DL_ERROR,
  2225. "%s: SPI read Error = %d\n",
  2226. __func__, rc);
  2227. else if (size > read_len) {
  2228. pt_debug(cd->dev, DL_ERROR,
  2229. "%s: PIP2 len field=%d, requested read_len=%d\n",
  2230. __func__, size, read_len);
  2231. rc = -EIO;
  2232. }
  2233. break;
  2234. }
  2235. /*
  2236. * Poll GPIO with fixed interval 20 us, and tollerance is
  2237. * limited to 10 us to speed up the process.
  2238. */
  2239. retry_interval = POLL_RETRY_DEFAULT_INTERVAL;
  2240. usleep_range(retry_interval, retry_interval+10);
  2241. retry_total_time += retry_interval;
  2242. }
  2243. #endif
  2244. *actual_read_len = size;
  2245. if (rc || (retry >= max_retry) || (retry_total_time >= resp_time_max)) {
  2246. pt_debug(dev, DL_ERROR,
  2247. "%s cmd[0x%02X] timed out, send_seq=0x%02X, resp_seq=0x%02X\n",
  2248. __func__, pip2_cmd.id, pip2_cmd.seq, response_seq);
  2249. *actual_read_len = 0;
  2250. rc = -EINVAL;
  2251. }
  2252. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, *actual_read_len,
  2253. "<<< NO_INT Read");
  2254. exit:
  2255. kfree(write_buf);
  2256. if (protect == PT_CORE_CMD_PROTECTED) {
  2257. if (release_exclusive(cd, cd->dev) < 0)
  2258. pt_debug(cd->dev, DL_ERROR,
  2259. "%s: fail to release exclusive\n", __func__);
  2260. }
  2261. return rc;
  2262. }
  2263. /*******************************************************************************
  2264. * FUNCTION: pt_pip_null_
  2265. *
  2266. * SUMMARY: Send the PIP "ping"(0x00) command to the DUT and wait for response.
  2267. * This function is used by watchdog to check if the fw corrupts.
  2268. *
  2269. * RETURN:
  2270. * 0 = success
  2271. * !0 = failure
  2272. *
  2273. * PARAMETERS:
  2274. * *cd - pointer to core data
  2275. ******************************************************************************/
  2276. static int pt_pip_null_(struct pt_core_data *cd)
  2277. {
  2278. struct pt_hid_output hid_output = {
  2279. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_NULL),
  2280. };
  2281. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  2282. }
  2283. #ifndef TTDL_KERNEL_SUBMISSION
  2284. /*******************************************************************************
  2285. * FUNCTION: pt_pip_null
  2286. *
  2287. * SUMMARY: Wrapper function for pt_pip_null_ that guarantees exclusive access.
  2288. *
  2289. * RETURN:
  2290. * 0 = success
  2291. * !0 = failure
  2292. *
  2293. * PARAMETERS:
  2294. * *cd - pointer to core data
  2295. ******************************************************************************/
  2296. static int pt_pip_null(struct pt_core_data *cd)
  2297. {
  2298. int rc;
  2299. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2300. if (rc < 0) {
  2301. pt_debug(cd->dev, DL_ERROR,
  2302. "%s: fail get exclusive ex=%p own=%p\n",
  2303. __func__, cd->exclusive_dev, cd->dev);
  2304. return rc;
  2305. }
  2306. rc = pt_pip_null_(cd);
  2307. if (release_exclusive(cd, cd->dev) < 0)
  2308. pt_debug(cd->dev, DL_ERROR,
  2309. "%s: fail to release exclusive\n", __func__);
  2310. return rc;
  2311. }
  2312. #endif /*!TTDL_KERNEL_SUBMISSION */
  2313. static void pt_stop_wd_timer(struct pt_core_data *cd);
  2314. /*******************************************************************************
  2315. * FUNCTION: pt_pip_start_bootloader_
  2316. *
  2317. * SUMMARY: Sends the PIP command start_bootloader [PIP cmd 0x01] to the DUT
  2318. *
  2319. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  2320. * the WD active could cause this function to fail!
  2321. *
  2322. * RETURN:
  2323. * 0 = success
  2324. * !0 = failure
  2325. *
  2326. * PARAMETERS:
  2327. * *cd - pointer to core data
  2328. ******************************************************************************/
  2329. static int pt_pip_start_bootloader_(struct pt_core_data *cd)
  2330. {
  2331. int rc = 0;
  2332. struct pt_hid_output hid_output = {
  2333. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_START_BOOTLOADER),
  2334. .timeout_ms = PT_PIP1_START_BOOTLOADER_TIMEOUT,
  2335. .reset_expected = 1,
  2336. };
  2337. if (cd->watchdog_enabled) {
  2338. pt_debug(cd->dev, DL_WARN,
  2339. "%s: watchdog isn't stopped before enter bl\n",
  2340. __func__);
  2341. goto exit;
  2342. }
  2343. /* Reset startup status after entering BL, new DUT enum required */
  2344. cd->startup_status = STARTUP_STATUS_START;
  2345. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  2346. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2347. if (rc) {
  2348. pt_debug(cd->dev, DL_ERROR,
  2349. "%s: Start BL PIP cmd failed. rc = %d\n",
  2350. __func__, rc);
  2351. }
  2352. exit:
  2353. return rc;
  2354. }
  2355. /*******************************************************************************
  2356. * FUNCTION: pt_pip_start_bootloader
  2357. *
  2358. * SUMMARY: Protected function to force DUT to enter the BL
  2359. *
  2360. * RETURN:
  2361. * 0 = success
  2362. * !0 = failure
  2363. *
  2364. * PARAMETERS:
  2365. * *cd - pointer to core data structure
  2366. ******************************************************************************/
  2367. static int pt_pip_start_bootloader(struct pt_core_data *cd)
  2368. {
  2369. int rc;
  2370. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2371. if (rc < 0) {
  2372. pt_debug(cd->dev, DL_ERROR,
  2373. "%s: fail get exclusive ex=%p own=%p\n",
  2374. __func__, cd->exclusive_dev, cd->dev);
  2375. return rc;
  2376. }
  2377. rc = pt_pip_start_bootloader_(cd);
  2378. if (release_exclusive(cd, cd->dev) < 0)
  2379. pt_debug(cd->dev, DL_ERROR,
  2380. "%s: fail to release exclusive\n", __func__);
  2381. return rc;
  2382. }
  2383. /*******************************************************************************
  2384. * FUNCTION: _pt_request_pip_start_bl
  2385. *
  2386. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other
  2387. * modules to request the DUT to enter the BL
  2388. *
  2389. * RETURN:
  2390. * 0 = success
  2391. * !0 = failure
  2392. *
  2393. * PARAMETERS:
  2394. * *dev - pointer to device structure
  2395. * protect - flag to run in protected mode
  2396. ******************************************************************************/
  2397. static int _pt_request_pip_start_bl(struct device *dev, int protect)
  2398. {
  2399. struct pt_core_data *cd = dev_get_drvdata(dev);
  2400. if (protect)
  2401. return pt_pip_start_bootloader(cd);
  2402. return pt_pip_start_bootloader_(cd);
  2403. }
  2404. /*******************************************************************************
  2405. * FUNCTION: pt_pip2_ver_load_ttdata
  2406. *
  2407. * SUMMARY: Function to load the Version information from the PIP2 VERSION
  2408. * command into the core data struct.
  2409. *
  2410. * RETURN: n/a
  2411. *
  2412. * PARAMETERS:
  2413. * *cd - pointer to core data structure
  2414. * len - Length of data in response_buf
  2415. ******************************************************************************/
  2416. static void pt_pip2_ver_load_ttdata(struct pt_core_data *cd, u16 len)
  2417. {
  2418. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2419. struct pt_pip2_version_full *full_ver;
  2420. struct pt_pip2_version *ver;
  2421. /*
  2422. * The PIP2 VERSION command can return different lengths of data.
  2423. * The additional LOT fields are included when the packet
  2424. * size is >= 29 bytes. Older FW sends a reduced packet size.
  2425. * NOTE:
  2426. * - The FW would swap the BL and FW versions when reporting
  2427. * the small packet.
  2428. * - Sub Lot bytes 16 and 17 are reserved.
  2429. */
  2430. if (len >= 0x1D) {
  2431. full_ver = (struct pt_pip2_version_full *)
  2432. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2433. ttdata->pip_ver_major = full_ver->pip2_version_msb;
  2434. ttdata->pip_ver_minor = full_ver->pip2_version_lsb;
  2435. ttdata->bl_ver_major = full_ver->bl_version_msb;
  2436. ttdata->bl_ver_minor = full_ver->bl_version_lsb;
  2437. ttdata->fw_ver_major = full_ver->fw_version_msb;
  2438. ttdata->fw_ver_minor = full_ver->fw_version_lsb;
  2439. /*
  2440. * BL PIP 2.02 and greater the version fields are
  2441. * swapped
  2442. */
  2443. if (ttdata->pip_ver_major >= 2 && ttdata->pip_ver_minor >= 2) {
  2444. ttdata->chip_rev =
  2445. get_unaligned_le16(&full_ver->chip_rev);
  2446. ttdata->chip_id =
  2447. get_unaligned_le16(&full_ver->chip_id);
  2448. } else {
  2449. ttdata->chip_rev =
  2450. get_unaligned_le16(&full_ver->chip_id);
  2451. ttdata->chip_id =
  2452. get_unaligned_le16(&full_ver->chip_rev);
  2453. }
  2454. memcpy(ttdata->uid, full_ver->uid, PT_UID_SIZE);
  2455. pt_pr_buf(cd->dev, DL_INFO, (u8 *)full_ver,
  2456. sizeof(struct pt_pip2_version_full),
  2457. "PIP2 VERSION FULL");
  2458. } else {
  2459. ver = (struct pt_pip2_version *)
  2460. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2461. ttdata->pip_ver_major = ver->pip2_version_msb;
  2462. ttdata->pip_ver_minor = ver->pip2_version_lsb;
  2463. ttdata->bl_ver_major = ver->bl_version_msb;
  2464. ttdata->bl_ver_minor = ver->bl_version_lsb;
  2465. ttdata->fw_ver_major = ver->fw_version_msb;
  2466. ttdata->fw_ver_minor = ver->fw_version_lsb;
  2467. ttdata->chip_rev = get_unaligned_le16(&ver->chip_rev);
  2468. ttdata->chip_id = get_unaligned_le16(&ver->chip_id);
  2469. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ver,
  2470. sizeof(struct pt_pip2_version), "PIP2 VERSION");
  2471. }
  2472. }
  2473. /*******************************************************************************
  2474. * FUNCTION: pt_si_get_ttdata
  2475. *
  2476. * SUMMARY: Function to load the version information from the system information
  2477. * PIP command into the core data struct.
  2478. *
  2479. * RETURN: n/a
  2480. *
  2481. * PARAMETERS:
  2482. * *cd - pointer to core data structure
  2483. ******************************************************************************/
  2484. static void pt_si_get_ttdata(struct pt_core_data *cd)
  2485. {
  2486. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2487. struct pt_ttdata_dev *ttdata_dev =
  2488. (struct pt_ttdata_dev *)
  2489. &cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  2490. ttdata->pip_ver_major = ttdata_dev->pip_ver_major;
  2491. ttdata->pip_ver_minor = ttdata_dev->pip_ver_minor;
  2492. ttdata->bl_ver_major = ttdata_dev->bl_ver_major;
  2493. ttdata->bl_ver_minor = ttdata_dev->bl_ver_minor;
  2494. ttdata->fw_ver_major = ttdata_dev->fw_ver_major;
  2495. ttdata->fw_ver_minor = ttdata_dev->fw_ver_minor;
  2496. ttdata->fw_pid = get_unaligned_le16(&ttdata_dev->fw_pid);
  2497. ttdata->fw_ver_conf = get_unaligned_le16(&ttdata_dev->fw_ver_conf);
  2498. ttdata->post_code = get_unaligned_le16(&ttdata_dev->post_code);
  2499. ttdata->revctrl = get_unaligned_le32(&ttdata_dev->revctrl);
  2500. ttdata->jtag_id_l = get_unaligned_le16(&ttdata_dev->jtag_si_id_l);
  2501. ttdata->jtag_id_h = get_unaligned_le16(&ttdata_dev->jtag_si_id_h);
  2502. memcpy(ttdata->mfg_id, ttdata_dev->mfg_id, PT_NUM_MFGID);
  2503. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ttdata_dev,
  2504. sizeof(struct pt_ttdata_dev), "sysinfo_ttdata");
  2505. }
  2506. /*******************************************************************************
  2507. * FUNCTION: pt_si_get_sensing_conf_data
  2508. *
  2509. * SUMMARY: Function to load the sensing information from the system information
  2510. * PIP command into the core data struct.
  2511. *
  2512. * RETURN: n/a
  2513. *
  2514. * PARAMETERS:
  2515. * *cd - pointer to core data structure
  2516. ******************************************************************************/
  2517. static void pt_si_get_sensing_conf_data(struct pt_core_data *cd)
  2518. {
  2519. struct pt_sensing_conf_data *scd = &cd->sysinfo.sensing_conf_data;
  2520. struct pt_sensing_conf_data_dev *scd_dev =
  2521. (struct pt_sensing_conf_data_dev *)
  2522. &cd->response_buf[PIP1_SYSINFO_SENSING_OFFSET];
  2523. scd->electrodes_x = scd_dev->electrodes_x;
  2524. scd->electrodes_y = scd_dev->electrodes_y;
  2525. scd->origin_x = scd_dev->origin_x;
  2526. scd->origin_y = scd_dev->origin_y;
  2527. /* PIP 1.4 (001-82649 *Q) add X_IS_TX bit in X_ORG */
  2528. if (scd->origin_x & 0x02) {
  2529. scd->tx_num = scd->electrodes_x;
  2530. scd->rx_num = scd->electrodes_y;
  2531. } else {
  2532. scd->tx_num = scd->electrodes_y;
  2533. scd->rx_num = scd->electrodes_x;
  2534. }
  2535. /*
  2536. * When the Panel ID is coming from an XY pin and not a dedicated
  2537. * GPIO, store the PID in pid_for_loader. This cannot be done for all
  2538. * other DUTs as the loader will use cd->pid_for_loader to generate
  2539. * the bin file name but will ignore it if pid_for_loader is still
  2540. * set to PANEL_ID_NOT_ENABLED
  2541. */
  2542. if (cd->panel_id_support &
  2543. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  2544. mutex_lock(&cd->system_lock);
  2545. cd->pid_for_loader = scd_dev->panel_id;
  2546. mutex_unlock(&cd->system_lock);
  2547. }
  2548. scd->panel_id = scd_dev->panel_id;
  2549. scd->btn = scd_dev->btn;
  2550. scd->scan_mode = scd_dev->scan_mode;
  2551. scd->max_tch = scd_dev->max_num_of_tch_per_refresh_cycle;
  2552. scd->res_x = get_unaligned_le16(&scd_dev->res_x);
  2553. scd->res_y = get_unaligned_le16(&scd_dev->res_y);
  2554. scd->max_z = get_unaligned_le16(&scd_dev->max_z);
  2555. scd->len_x = get_unaligned_le16(&scd_dev->len_x);
  2556. scd->len_y = get_unaligned_le16(&scd_dev->len_y);
  2557. pt_pr_buf(cd->dev, DL_INFO, (u8 *)scd_dev,
  2558. sizeof(struct pt_sensing_conf_data_dev),
  2559. "sensing_conf_data");
  2560. }
  2561. /*******************************************************************************
  2562. * FUNCTION: pt_si_setup
  2563. *
  2564. * SUMMARY: Setup the xy_data and xy_mode by allocating the needed memory
  2565. *
  2566. * RETURN:
  2567. * 0 = success
  2568. * !0 = failure
  2569. *
  2570. * PARAMETERS:
  2571. * *cd - pointer to core data structure
  2572. ******************************************************************************/
  2573. static int pt_si_setup(struct pt_core_data *cd)
  2574. {
  2575. struct pt_sysinfo *si = &cd->sysinfo;
  2576. int max_tch = si->sensing_conf_data.max_tch;
  2577. if (!si->xy_data)
  2578. si->xy_data = kzalloc(max_tch * si->desc.tch_record_size,
  2579. GFP_KERNEL);
  2580. if (!si->xy_data)
  2581. return -ENOMEM;
  2582. if (!si->xy_mode)
  2583. si->xy_mode = kzalloc(si->desc.tch_header_size, GFP_KERNEL);
  2584. if (!si->xy_mode) {
  2585. kfree(si->xy_data);
  2586. return -ENOMEM;
  2587. }
  2588. return 0;
  2589. }
  2590. /*******************************************************************************
  2591. * FUNCTION: pt_si_get_btn_data
  2592. *
  2593. * SUMMARY: Setup the core data button information based on the response of the
  2594. * System Information PIP command.
  2595. *
  2596. * RETURN:
  2597. * 0 = success
  2598. * !0 = failure
  2599. *
  2600. * PARAMETERS:
  2601. * *cd - pointer to core data structure
  2602. ******************************************************************************/
  2603. static int pt_si_get_btn_data(struct pt_core_data *cd)
  2604. {
  2605. struct pt_sysinfo *si = &cd->sysinfo;
  2606. int num_btns = 0;
  2607. int num_defined_keys;
  2608. u16 *key_table;
  2609. int btn;
  2610. int i;
  2611. int rc = 0;
  2612. unsigned int btns = cd->response_buf[PIP1_SYSINFO_BTN_OFFSET]
  2613. & PIP1_SYSINFO_BTN_MASK;
  2614. size_t btn_keys_size;
  2615. pt_debug(cd->dev, DL_INFO, "%s: get btn data\n", __func__);
  2616. for (i = 0; i < PIP1_SYSINFO_MAX_BTN; i++) {
  2617. if (btns & (1 << i))
  2618. num_btns++;
  2619. }
  2620. si->num_btns = num_btns;
  2621. if (num_btns) {
  2622. btn_keys_size = num_btns * sizeof(struct pt_btn);
  2623. if (!si->btn)
  2624. si->btn = kzalloc(btn_keys_size, GFP_KERNEL);
  2625. if (!si->btn)
  2626. return -ENOMEM;
  2627. if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS] == NULL)
  2628. num_defined_keys = 0;
  2629. else if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS]->data == NULL)
  2630. num_defined_keys = 0;
  2631. else
  2632. num_defined_keys = cd->cpdata->sett
  2633. [PT_IC_GRPNUM_BTN_KEYS]->size;
  2634. for (btn = 0; btn < num_btns && btn < num_defined_keys; btn++) {
  2635. key_table = (u16 *)cd->cpdata->sett
  2636. [PT_IC_GRPNUM_BTN_KEYS]->data;
  2637. si->btn[btn].key_code = key_table[btn];
  2638. si->btn[btn].enabled = true;
  2639. }
  2640. for (; btn < num_btns; btn++) {
  2641. si->btn[btn].key_code = KEY_RESERVED;
  2642. si->btn[btn].enabled = true;
  2643. }
  2644. return rc;
  2645. }
  2646. kfree(si->btn);
  2647. si->btn = NULL;
  2648. return rc;
  2649. }
  2650. /*******************************************************************************
  2651. * FUNCTION: pt_si_put_log_data
  2652. *
  2653. * SUMMARY: Prints all sys info data to kmsg log
  2654. *
  2655. * RETURN: n/a
  2656. *
  2657. * PARAMETERS:
  2658. * *cd - pointer to core data structure
  2659. ******************************************************************************/
  2660. static void pt_si_put_log_data(struct pt_core_data *cd)
  2661. {
  2662. struct pt_sysinfo *si = &cd->sysinfo;
  2663. struct pt_ttdata *ttdata = &si->ttdata;
  2664. struct pt_sensing_conf_data *scd = &si->sensing_conf_data;
  2665. int i;
  2666. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_major = 0x%02X (%d)\n",
  2667. __func__, ttdata->pip_ver_major, ttdata->pip_ver_major);
  2668. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_minor = 0x%02X (%d)\n",
  2669. __func__, ttdata->pip_ver_minor, ttdata->pip_ver_minor);
  2670. pt_debug(cd->dev, DL_DEBUG, "%s: fw_pid = 0x%04X (%d)\n",
  2671. __func__, ttdata->fw_pid, ttdata->fw_pid);
  2672. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_major = 0x%02X (%d)\n",
  2673. __func__, ttdata->fw_ver_major, ttdata->fw_ver_major);
  2674. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_minor = 0x%02X (%d)\n",
  2675. __func__, ttdata->fw_ver_minor, ttdata->fw_ver_minor);
  2676. pt_debug(cd->dev, DL_DEBUG, "%s: revctrl = 0x%08X (%d)\n",
  2677. __func__, ttdata->revctrl, ttdata->revctrl);
  2678. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_conf = 0x%04X (%d)\n",
  2679. __func__, ttdata->fw_ver_conf, ttdata->fw_ver_conf);
  2680. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_major = 0x%02X (%d)\n",
  2681. __func__, ttdata->bl_ver_major, ttdata->bl_ver_major);
  2682. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_minor = 0x%02X (%d)\n",
  2683. __func__, ttdata->bl_ver_minor, ttdata->bl_ver_minor);
  2684. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_h = 0x%04X (%d)\n",
  2685. __func__, ttdata->jtag_id_h, ttdata->jtag_id_h);
  2686. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_l = 0x%04X (%d)\n",
  2687. __func__, ttdata->jtag_id_l, ttdata->jtag_id_l);
  2688. for (i = 0; i < PT_NUM_MFGID; i++)
  2689. pt_debug(cd->dev, DL_DEBUG,
  2690. "%s: mfg_id[%d] = 0x%02X (%d)\n",
  2691. __func__, i, ttdata->mfg_id[i],
  2692. ttdata->mfg_id[i]);
  2693. pt_debug(cd->dev, DL_DEBUG, "%s: post_code = 0x%04X (%d)\n",
  2694. __func__, ttdata->post_code, ttdata->post_code);
  2695. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_x = 0x%02X (%d)\n",
  2696. __func__, scd->electrodes_x, scd->electrodes_x);
  2697. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_y = 0x%02X (%d)\n",
  2698. __func__, scd->electrodes_y, scd->electrodes_y);
  2699. pt_debug(cd->dev, DL_DEBUG, "%s: len_x = 0x%04X (%d)\n",
  2700. __func__, scd->len_x, scd->len_x);
  2701. pt_debug(cd->dev, DL_DEBUG, "%s: len_y = 0x%04X (%d)\n",
  2702. __func__, scd->len_y, scd->len_y);
  2703. pt_debug(cd->dev, DL_DEBUG, "%s: res_x = 0x%04X (%d)\n",
  2704. __func__, scd->res_x, scd->res_x);
  2705. pt_debug(cd->dev, DL_DEBUG, "%s: res_y = 0x%04X (%d)\n",
  2706. __func__, scd->res_y, scd->res_y);
  2707. pt_debug(cd->dev, DL_DEBUG, "%s: max_z = 0x%04X (%d)\n",
  2708. __func__, scd->max_z, scd->max_z);
  2709. pt_debug(cd->dev, DL_DEBUG, "%s: origin_x = 0x%02X (%d)\n",
  2710. __func__, scd->origin_x, scd->origin_x);
  2711. pt_debug(cd->dev, DL_DEBUG, "%s: origin_y = 0x%02X (%d)\n",
  2712. __func__, scd->origin_y, scd->origin_y);
  2713. pt_debug(cd->dev, DL_DEBUG, "%s: panel_id = 0x%02X (%d)\n",
  2714. __func__, scd->panel_id, scd->panel_id);
  2715. pt_debug(cd->dev, DL_DEBUG, "%s: btn =0x%02X (%d)\n",
  2716. __func__, scd->btn, scd->btn);
  2717. pt_debug(cd->dev, DL_DEBUG, "%s: scan_mode = 0x%02X (%d)\n",
  2718. __func__, scd->scan_mode, scd->scan_mode);
  2719. pt_debug(cd->dev, DL_DEBUG,
  2720. "%s: max_num_of_tch_per_refresh_cycle = 0x%02X (%d)\n",
  2721. __func__, scd->max_tch, scd->max_tch);
  2722. pt_debug(cd->dev, DL_DEBUG, "%s: xy_mode = %p\n",
  2723. __func__, si->xy_mode);
  2724. pt_debug(cd->dev, DL_DEBUG, "%s: xy_data = %p\n",
  2725. __func__, si->xy_data);
  2726. }
  2727. /*******************************************************************************
  2728. * FUNCTION: pt_get_sysinfo_regs
  2729. *
  2730. * SUMMARY: Setup all the core data System information based on the response
  2731. * of the System Information PIP command.
  2732. *
  2733. * RETURN:
  2734. * 0 = success
  2735. * !0 = failure
  2736. *
  2737. * PARAMETERS:
  2738. * *cd - pointer to core data structure
  2739. ******************************************************************************/
  2740. static int pt_get_sysinfo_regs(struct pt_core_data *cd)
  2741. {
  2742. struct pt_sysinfo *si = &cd->sysinfo;
  2743. int rc;
  2744. rc = pt_si_get_btn_data(cd);
  2745. if (rc < 0)
  2746. return rc;
  2747. pt_si_get_ttdata(cd);
  2748. pt_si_get_sensing_conf_data(cd);
  2749. pt_si_setup(cd);
  2750. pt_si_put_log_data(cd);
  2751. si->ready = true;
  2752. return rc;
  2753. }
  2754. /*******************************************************************************
  2755. * FUNCTION: pt_free_si_ptrs
  2756. *
  2757. * SUMMARY: Frees all memory associated with the System Information within
  2758. * core data
  2759. *
  2760. * RETURN: n/a
  2761. *
  2762. * PARAMETERS:
  2763. * *cd - pointer to core data structure
  2764. ******************************************************************************/
  2765. static void pt_free_si_ptrs(struct pt_core_data *cd)
  2766. {
  2767. struct pt_sysinfo *si = &cd->sysinfo;
  2768. kfree(si->btn);
  2769. kfree(si->xy_mode);
  2770. kfree(si->xy_data);
  2771. }
  2772. /*******************************************************************************
  2773. * FUNCTION: pt_hid_output_get_sysinfo_
  2774. *
  2775. * SUMMARY: Sends the PIP Get SYS INFO command to the DUT and waits for the
  2776. * response.
  2777. *
  2778. * RETURN::
  2779. * 0 = success
  2780. * !0 = failure
  2781. *
  2782. * PARAMETERS:
  2783. * *cd - pointer to core data structure
  2784. ******************************************************************************/
  2785. static int pt_hid_output_get_sysinfo_(struct pt_core_data *cd)
  2786. {
  2787. int rc = 0;
  2788. struct pt_hid_output hid_output = {
  2789. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  2790. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  2791. };
  2792. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2793. if (rc)
  2794. return rc;
  2795. /* Parse the sysinfo data */
  2796. rc = pt_get_sysinfo_regs(cd);
  2797. if (rc)
  2798. pt_free_si_ptrs(cd);
  2799. return rc;
  2800. }
  2801. #ifndef TTDL_KERNEL_SUBMISSION
  2802. /*******************************************************************************
  2803. * FUNCTION: pt_hid_output_get_sysinfo
  2804. *
  2805. * SUMMARY: Protected call to pt_hid_output_get_sysinfo_
  2806. *
  2807. * RETURN::
  2808. * 0 = success
  2809. * !0 = failure
  2810. *
  2811. * PARAMETERS:
  2812. * *cd - pointer to core data structure
  2813. ******************************************************************************/
  2814. static int pt_hid_output_get_sysinfo(struct pt_core_data *cd)
  2815. {
  2816. int rc;
  2817. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2818. if (rc < 0) {
  2819. pt_debug(cd->dev, DL_ERROR,
  2820. "%s: fail get exclusive ex=%p own=%p\n",
  2821. __func__, cd->exclusive_dev, cd->dev);
  2822. return rc;
  2823. }
  2824. rc = pt_hid_output_get_sysinfo_(cd);
  2825. if (release_exclusive(cd, cd->dev) < 0)
  2826. pt_debug(cd->dev, DL_ERROR,
  2827. "%s: fail to release exclusive\n", __func__);
  2828. return rc;
  2829. }
  2830. #endif /*!TTDL_KERNEL_SUBMISSION */
  2831. /*******************************************************************************
  2832. * FUNCTION: pt_pip_suspend_scanning_
  2833. *
  2834. * SUMMARY: Sends the PIP Suspend Scanning command to the DUT
  2835. *
  2836. * RETURN::
  2837. * 0 = success
  2838. * !0 = failure
  2839. *
  2840. * PARAMETERS:
  2841. * *cd - pointer to core data structure
  2842. ******************************************************************************/
  2843. static int pt_pip_suspend_scanning_(struct pt_core_data *cd)
  2844. {
  2845. int rc = 0;
  2846. struct pt_hid_output hid_output = {
  2847. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SUSPEND_SCANNING),
  2848. };
  2849. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2850. if (rc) {
  2851. pt_debug(cd->dev, DL_ERROR,
  2852. "%s: Suspend Scan PIP cmd failed. rc = %d\n",
  2853. __func__, rc);
  2854. }
  2855. return rc;
  2856. }
  2857. /*******************************************************************************
  2858. * FUNCTION: pt_pip_suspend_scanning
  2859. *
  2860. * SUMMARY: Protected wrapper for calling pt_hid_output_suspend_scanning_
  2861. *
  2862. * RETURN::
  2863. * 0 = success
  2864. * !0 = failure
  2865. *
  2866. * PARAMETERS:
  2867. * *cd - pointer to core data structure
  2868. ******************************************************************************/
  2869. static int pt_pip_suspend_scanning(struct pt_core_data *cd)
  2870. {
  2871. int rc;
  2872. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2873. if (rc < 0) {
  2874. pt_debug(cd->dev, DL_ERROR,
  2875. "%s: fail get exclusive ex=%p own=%p\n",
  2876. __func__, cd->exclusive_dev, cd->dev);
  2877. return rc;
  2878. }
  2879. rc = pt_pip_suspend_scanning_(cd);
  2880. if (release_exclusive(cd, cd->dev) < 0)
  2881. pt_debug(cd->dev, DL_ERROR,
  2882. "%s: fail to release exclusive\n", __func__);
  2883. return rc;
  2884. }
  2885. /*******************************************************************************
  2886. * FUNCTION: _pt_request_pip_suspend_scanning
  2887. *
  2888. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2889. * calls to the protected or unprotected call to pt_pip_suspend_scanning
  2890. *
  2891. * RETURN:
  2892. * 0 = success
  2893. * !0 = failure
  2894. *
  2895. * PARAMETERS:
  2896. * *dev - pointer to device structure
  2897. * protect - 0 = call non-protected function
  2898. * 1 = call protected function
  2899. ******************************************************************************/
  2900. static int _pt_request_pip_suspend_scanning(struct device *dev,
  2901. int protect)
  2902. {
  2903. struct pt_core_data *cd = dev_get_drvdata(dev);
  2904. if (protect)
  2905. return pt_pip_suspend_scanning(cd);
  2906. return pt_pip_suspend_scanning_(cd);
  2907. }
  2908. /*******************************************************************************
  2909. * FUNCTION: pt_pip_resume_scanning_
  2910. *
  2911. * SUMMARY: Sends the PIP Resume Scanning command to the DUT
  2912. *
  2913. * RETURN::
  2914. * 0 = success
  2915. * !0 = failure
  2916. *
  2917. * PARAMETERS:
  2918. * *cd - pointer to core data structure
  2919. ******************************************************************************/
  2920. static int pt_pip_resume_scanning_(struct pt_core_data *cd)
  2921. {
  2922. int rc = 0;
  2923. struct pt_hid_output hid_output = {
  2924. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RESUME_SCANNING),
  2925. };
  2926. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2927. if (rc) {
  2928. pt_debug(cd->dev, DL_ERROR,
  2929. "%s: Resume Scan PIP cmd failed. rc = %d\n",
  2930. __func__, rc);
  2931. }
  2932. return rc;
  2933. }
  2934. /*******************************************************************************
  2935. * FUNCTION: pt_pip_resume_scanning
  2936. *
  2937. * SUMMARY: Protected wrapper for calling pt_pip_resume_scanning_
  2938. *
  2939. * RETURN::
  2940. * 0 = success
  2941. * !0 = failure
  2942. *
  2943. * PARAMETERS:
  2944. * *cd - pointer to core data structure
  2945. ******************************************************************************/
  2946. static int pt_pip_resume_scanning(struct pt_core_data *cd)
  2947. {
  2948. int rc;
  2949. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2950. if (rc < 0) {
  2951. pt_debug(cd->dev, DL_ERROR,
  2952. "%s: fail get exclusive ex=%p own=%p\n",
  2953. __func__, cd->exclusive_dev, cd->dev);
  2954. return rc;
  2955. }
  2956. rc = pt_pip_resume_scanning_(cd);
  2957. if (release_exclusive(cd, cd->dev) < 0)
  2958. pt_debug(cd->dev, DL_ERROR,
  2959. "%s: fail to release exclusive\n", __func__);
  2960. return rc;
  2961. }
  2962. /*******************************************************************************
  2963. * FUNCTION: _pt_request_pip_resume_scanning
  2964. *
  2965. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2966. * calls to the protected or unprotected call to pt_pip_resume_scanning
  2967. *
  2968. * RETURN::
  2969. * 0 = success
  2970. * !0 = failure
  2971. *
  2972. * PARAMETERS:
  2973. * *dev - pointer to device structure
  2974. * protect - 0 = call non-protected function
  2975. * 1 = call protected function
  2976. ******************************************************************************/
  2977. static int _pt_request_pip_resume_scanning(struct device *dev,
  2978. int protect)
  2979. {
  2980. struct pt_core_data *cd = dev_get_drvdata(dev);
  2981. if (protect)
  2982. return pt_pip_resume_scanning(cd);
  2983. return pt_pip_resume_scanning_(cd);
  2984. }
  2985. /*******************************************************************************
  2986. * FUNCTION: pt_pip_get_param_
  2987. *
  2988. * SUMMARY: Sends a PIP command 0x05 Get Parameter to the DUT and returns
  2989. * the 32bit parameter value
  2990. *
  2991. * RETURN:
  2992. * 0 = success
  2993. * !0 = failure
  2994. *
  2995. * PARAMETERS:
  2996. * *cd - pointer to core data
  2997. * param_id - parameter ID to retrieve
  2998. * *value - value of DUT parameter
  2999. ******************************************************************************/
  3000. static int pt_pip_get_param_(struct pt_core_data *cd,
  3001. u8 param_id, u32 *value)
  3002. {
  3003. int write_length = 1;
  3004. u8 param[1] = { param_id };
  3005. u8 read_param_id;
  3006. int param_size;
  3007. u8 *ptr;
  3008. int rc = 0;
  3009. int i;
  3010. struct pt_hid_output hid_output = {
  3011. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_PARAM),
  3012. .write_length = write_length,
  3013. .write_buf = param,
  3014. };
  3015. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3016. if (rc)
  3017. return rc;
  3018. read_param_id = cd->response_buf[5];
  3019. if (read_param_id != param_id)
  3020. return -EPROTO;
  3021. param_size = cd->response_buf[6];
  3022. ptr = &cd->response_buf[7];
  3023. *value = 0;
  3024. for (i = 0; i < param_size; i++)
  3025. *value += ptr[i] << (i * 8);
  3026. return 0;
  3027. }
  3028. /*******************************************************************************
  3029. * FUNCTION: pt_pip_get_param
  3030. *
  3031. * SUMMARY: Protected call to pt_hid_output_get_param_ by a request exclusive
  3032. *
  3033. * RETURN:
  3034. * 0 = success
  3035. * !0 = failure
  3036. *
  3037. * PARAMETERS:
  3038. * *cd - pointer to core data
  3039. * param_id - parameter ID to retrieve
  3040. * *value - value of DUT parameter
  3041. ******************************************************************************/
  3042. static int pt_pip_get_param(struct pt_core_data *cd,
  3043. u8 param_id, u32 *value)
  3044. {
  3045. int rc;
  3046. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3047. if (rc < 0) {
  3048. pt_debug(cd->dev, DL_ERROR,
  3049. "%s: fail get exclusive ex=%p own=%p\n",
  3050. __func__, cd->exclusive_dev, cd->dev);
  3051. return rc;
  3052. }
  3053. rc = pt_pip_get_param_(cd, param_id, value);
  3054. if (release_exclusive(cd, cd->dev) < 0)
  3055. pt_debug(cd->dev, DL_ERROR,
  3056. "%s: fail to release exclusive\n", __func__);
  3057. return rc;
  3058. }
  3059. /*******************************************************************************
  3060. * FUNCTION: _pt_request_pip_get_param
  3061. *
  3062. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3063. * calls to the protected or unprotected call to pt_pip_get_param
  3064. *
  3065. * RETURN:
  3066. * 0 = success
  3067. * !0 = failure
  3068. *
  3069. * PARAMETERS:
  3070. * *dev - pointer to device structure
  3071. * protect - flag to call protected or non protected function
  3072. * param_id - parameter ID to retrieve
  3073. * *value - value of DUT parameter
  3074. ******************************************************************************/
  3075. int _pt_request_pip_get_param(struct device *dev,
  3076. int protect, u8 param_id, u32 *value)
  3077. {
  3078. struct pt_core_data *cd = dev_get_drvdata(dev);
  3079. if (protect)
  3080. return pt_pip_get_param(cd, param_id, value);
  3081. return pt_pip_get_param_(cd, param_id, value);
  3082. }
  3083. /*******************************************************************************
  3084. * FUNCTION: pt_pip_set_param_
  3085. *
  3086. * SUMMARY: Sends a PIP command 0x06 Set Parameter to the DUT writing the
  3087. * passed in value to flash
  3088. *
  3089. * RETURN:
  3090. * 0 = success
  3091. * !0 = failure
  3092. *
  3093. * PARAMETERS:
  3094. * *cd - pointer to core data
  3095. * param_id - parameter ID to set
  3096. * value - value to write
  3097. * size - size to write
  3098. ******************************************************************************/
  3099. static int pt_pip_set_param_(struct pt_core_data *cd,
  3100. u8 param_id, u32 value, u8 size)
  3101. {
  3102. u8 write_buf[6];
  3103. u8 *ptr = &write_buf[2];
  3104. int rc = 0;
  3105. int i;
  3106. struct pt_hid_output hid_output = {
  3107. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SET_PARAM),
  3108. .write_buf = write_buf,
  3109. };
  3110. write_buf[0] = param_id;
  3111. write_buf[1] = size;
  3112. for (i = 0; i < size; i++) {
  3113. ptr[i] = value & 0xFF;
  3114. value = value >> 8;
  3115. }
  3116. hid_output.write_length = 2 + size;
  3117. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3118. if (rc)
  3119. return rc;
  3120. if (param_id != cd->response_buf[5] || size != cd->response_buf[6])
  3121. return -EPROTO;
  3122. return 0;
  3123. }
  3124. /*******************************************************************************
  3125. * FUNCTION: pt_pip_set_param
  3126. *
  3127. * SUMMARY: Protected call to pt_hid_output_set_param_ by a request exclusive
  3128. *
  3129. * RETURN:
  3130. * 0 = success
  3131. * !0 = failure
  3132. *
  3133. * PARAMETERS:
  3134. * *cd - pointer to core data
  3135. * param_id - parameter ID to set
  3136. * value - value to write
  3137. * size - size to write
  3138. ******************************************************************************/
  3139. static int pt_pip_set_param(struct pt_core_data *cd,
  3140. u8 param_id, u32 value, u8 size)
  3141. {
  3142. int rc;
  3143. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3144. if (rc < 0) {
  3145. pt_debug(cd->dev, DL_ERROR,
  3146. "%s: fail get exclusive ex=%p own=%p\n",
  3147. __func__, cd->exclusive_dev, cd->dev);
  3148. return rc;
  3149. }
  3150. rc = pt_pip_set_param_(cd, param_id, value, size);
  3151. if (release_exclusive(cd, cd->dev) < 0)
  3152. pt_debug(cd->dev, DL_ERROR,
  3153. "%s: fail to release exclusive\n", __func__);
  3154. return rc;
  3155. }
  3156. /*******************************************************************************
  3157. * FUNCTION: _pt_request_pip_set_param
  3158. *
  3159. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3160. * calls to the protected or unprotected call to pt_pip_set_param
  3161. *
  3162. * RETURN:
  3163. * 0 = success
  3164. * !0 = failure
  3165. *
  3166. * PARAMETERS:
  3167. * *dev - pointer to device structure
  3168. * protect - flag to call protected or non-protected
  3169. * param_id - parameter ID to set
  3170. * value - value to write
  3171. * size - size to write
  3172. ******************************************************************************/
  3173. int _pt_request_pip_set_param(struct device *dev, int protect,
  3174. u8 param_id, u32 value, u8 size)
  3175. {
  3176. struct pt_core_data *cd = dev_get_drvdata(dev);
  3177. if (protect)
  3178. return pt_pip_set_param(cd, param_id, value, size);
  3179. return pt_pip_set_param_(cd, param_id, value, size);
  3180. }
  3181. /*******************************************************************************
  3182. * FUNCTION: _pt_pip_enter_easywake_state_
  3183. *
  3184. * SUMMARY: Sends a PIP command 0x09 Enter EasyWake State to the DUT
  3185. *
  3186. * RETURN:
  3187. * 0 = success
  3188. * !0 = failure
  3189. *
  3190. * PARAMETERS:
  3191. * *dev - pointer to device structure
  3192. * data - easywake guesture (Only used for PIP1.6 and earlier)
  3193. * *return_data - return status if easywake was entered
  3194. ******************************************************************************/
  3195. static int pt_hid_output_enter_easywake_state_(
  3196. struct pt_core_data *cd, u8 data, u8 *return_data)
  3197. {
  3198. int write_length = 1;
  3199. u8 param[1] = { data };
  3200. int rc = 0;
  3201. struct pt_hid_output hid_output = {
  3202. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_ENTER_EASYWAKE_STATE),
  3203. .write_length = write_length,
  3204. .write_buf = param,
  3205. };
  3206. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3207. if (rc)
  3208. return rc;
  3209. *return_data = cd->response_buf[5];
  3210. return rc;
  3211. }
  3212. /*******************************************************************************
  3213. * FUNCTION: pt_pip_verify_config_block_crc_
  3214. *
  3215. * SUMMARY: Sends the PIP "Verify Data Block CRC" (0x20) command to the DUT
  3216. *
  3217. * RETURN:
  3218. * 0 = success
  3219. * !0 = failure
  3220. *
  3221. * PARAMETERS:
  3222. * *cd - pointer the core data structure
  3223. * ebid - enumerated block ID
  3224. * *status - PIP command status
  3225. * calculated_crc - calculated CRC
  3226. * stored_crc - stored CRC in config area
  3227. ******************************************************************************/
  3228. static int pt_pip_verify_config_block_crc_(
  3229. struct pt_core_data *cd, u8 ebid, u8 *status,
  3230. u16 *calculated_crc, u16 *stored_crc)
  3231. {
  3232. int write_length = 1;
  3233. u8 param[1] = { ebid };
  3234. u8 *ptr;
  3235. int rc = 0;
  3236. struct pt_hid_output hid_output = {
  3237. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  3238. .write_length = write_length,
  3239. .write_buf = param,
  3240. };
  3241. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3242. if (rc)
  3243. return rc;
  3244. ptr = &cd->response_buf[5];
  3245. *status = ptr[0];
  3246. *calculated_crc = get_unaligned_le16(&ptr[1]);
  3247. *stored_crc = get_unaligned_le16(&ptr[3]);
  3248. return 0;
  3249. }
  3250. /*******************************************************************************
  3251. * FUNCTION: pt_pip_verify_config_block_crc
  3252. *
  3253. * SUMMARY: Protected call to pt_hid_output_verify_config_block_crc_() within
  3254. * an exclusive access to the DUT.
  3255. *
  3256. * RETURN:
  3257. * 0 = success
  3258. * !0 = failure
  3259. *
  3260. * PARAMETERS:
  3261. * *cd - pointer the core data structure
  3262. * ebid - enumerated block ID
  3263. * *status - PIP command status
  3264. * calculated_crc - calculated CRC
  3265. * stored_crc - stored CRC in config area
  3266. ******************************************************************************/
  3267. static int pt_pip_verify_config_block_crc(
  3268. struct pt_core_data *cd, u8 ebid, u8 *status,
  3269. u16 *calculated_crc, u16 *stored_crc)
  3270. {
  3271. int rc;
  3272. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3273. if (rc < 0) {
  3274. pt_debug(cd->dev, DL_ERROR,
  3275. "%s: fail get exclusive ex=%p own=%p\n",
  3276. __func__, cd->exclusive_dev, cd->dev);
  3277. return rc;
  3278. }
  3279. rc = pt_pip_verify_config_block_crc_(cd, ebid, status,
  3280. calculated_crc, stored_crc);
  3281. if (release_exclusive(cd, cd->dev) < 0)
  3282. pt_debug(cd->dev, DL_ERROR,
  3283. "%s: fail to release exclusive\n", __func__);
  3284. return rc;
  3285. }
  3286. /*******************************************************************************
  3287. * FUNCTION: _pt_request_pip_verify_config_block_crc
  3288. *
  3289. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3290. * calls to the protected or unprotected call to
  3291. * pt_pip_verify_config_block_crc_
  3292. *
  3293. * RETURN:
  3294. * 0 = success
  3295. * !0 = failure
  3296. *
  3297. * PARAMETERS:
  3298. * *dev - pointer to device structure
  3299. * protect - flag to call protected or non-protected
  3300. * ebid - enumerated block ID
  3301. * *status - PIP command status
  3302. * calculated_crc - calculated CRC
  3303. * stored_crc - stored CRC in config area
  3304. ******************************************************************************/
  3305. static int _pt_request_pip_verify_config_block_crc(
  3306. struct device *dev, int protect, u8 ebid, u8 *status,
  3307. u16 *calculated_crc, u16 *stored_crc)
  3308. {
  3309. struct pt_core_data *cd = dev_get_drvdata(dev);
  3310. if (protect)
  3311. return pt_pip_verify_config_block_crc(cd, ebid,
  3312. status, calculated_crc, stored_crc);
  3313. return pt_pip_verify_config_block_crc_(cd, ebid,
  3314. status, calculated_crc, stored_crc);
  3315. }
  3316. /*******************************************************************************
  3317. * FUNCTION: pt_pip_get_config_row_size_
  3318. *
  3319. * SUMMARY: Sends the PIP "Get Data Row Size" (0x21) command to the DUT
  3320. *
  3321. * RETURN:
  3322. * 0 = success
  3323. * !0 = failure
  3324. *
  3325. * PARAMETERS:
  3326. * *cd - pointer to core data
  3327. * protect - flag to call protected or non-protected
  3328. * *row_size - pointer to store the retrieved row size
  3329. ******************************************************************************/
  3330. static int pt_pip_get_config_row_size_(struct pt_core_data *cd,
  3331. u16 *row_size)
  3332. {
  3333. int rc = 0;
  3334. struct pt_hid_output hid_output = {
  3335. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_CONFIG_ROW_SIZE),
  3336. };
  3337. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3338. if (rc)
  3339. return rc;
  3340. *row_size = get_unaligned_le16(&cd->response_buf[5]);
  3341. return 0;
  3342. }
  3343. /*******************************************************************************
  3344. * FUNCTION: pt_pip_get_config_row_size
  3345. *
  3346. * SUMMARY: Protected call to pt_hid_output_get_config_row_size_ within
  3347. * an exclusive access to the DUT.
  3348. *
  3349. * RETURN:
  3350. * 0 = success
  3351. * !0 = failure
  3352. *
  3353. * PARAMETERS:
  3354. * *cd - pointer to core data
  3355. * protect - flag to call protected or non-protected
  3356. * *row_size - pointer to store the retrieved row size
  3357. ******************************************************************************/
  3358. static int pt_pip_get_config_row_size(struct pt_core_data *cd,
  3359. u16 *row_size)
  3360. {
  3361. int rc;
  3362. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3363. if (rc < 0) {
  3364. pt_debug(cd->dev, DL_ERROR,
  3365. "%s: fail get exclusive ex=%p own=%p\n",
  3366. __func__, cd->exclusive_dev, cd->dev);
  3367. return rc;
  3368. }
  3369. rc = pt_pip_get_config_row_size_(cd, row_size);
  3370. if (release_exclusive(cd, cd->dev) < 0)
  3371. pt_debug(cd->dev, DL_ERROR,
  3372. "%s: fail to release exclusive\n", __func__);
  3373. return rc;
  3374. }
  3375. /*******************************************************************************
  3376. * FUNCTION: _pt_request_pip_get_config_row_size
  3377. *
  3378. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3379. * calls to the protected or unprotected call to
  3380. * pt_pip_get_config_row_size_
  3381. *
  3382. * RETURN:
  3383. * 0 = success
  3384. * !0 = failure
  3385. *
  3386. * PARAMETERS:
  3387. * *dev - pointer to device structure
  3388. * protect - flag to call protected or non-protected
  3389. * *row_size - pointer to store the retrieved row size
  3390. ******************************************************************************/
  3391. static int _pt_request_pip_get_config_row_size(struct device *dev,
  3392. int protect, u16 *row_size)
  3393. {
  3394. struct pt_core_data *cd = dev_get_drvdata(dev);
  3395. if (protect)
  3396. return pt_pip_get_config_row_size(cd, row_size);
  3397. return pt_pip_get_config_row_size_(cd, row_size);
  3398. }
  3399. /*******************************************************************************
  3400. * FUNCTION: pt_pip1_read_data_block_
  3401. *
  3402. * SUMMARY: Sends the PIP "Read Data Block" (0x22) command to the DUT and print
  3403. * output data to the "read_buf" and update "crc".
  3404. *
  3405. * RETURN:
  3406. * 0 = success
  3407. * !0 = failure
  3408. *
  3409. * PARAMETERS:
  3410. * *cd - pointer to core data
  3411. * row_number - row number
  3412. * length - length of data to read
  3413. * ebid - block id
  3414. * *actual_read_len - Actual data length read
  3415. * *read_buf - pointer to the buffer to store read data
  3416. * read_buf_size - size of read_buf
  3417. * *crc - pointer to store CRC of row data
  3418. ******************************************************************************/
  3419. static int pt_pip1_read_data_block_(struct pt_core_data *cd,
  3420. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3421. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3422. {
  3423. int read_ebid;
  3424. int status;
  3425. int rc = 0;
  3426. int write_length = 5;
  3427. u8 write_buf[5];
  3428. u8 cmd_offset = 0;
  3429. u16 calc_crc;
  3430. struct pt_hid_output hid_output = {
  3431. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_READ_DATA_BLOCK),
  3432. .write_length = write_length,
  3433. .write_buf = write_buf,
  3434. };
  3435. write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3436. write_buf[cmd_offset++] = HI_BYTE(row_number);
  3437. write_buf[cmd_offset++] = LOW_BYTE(length);
  3438. write_buf[cmd_offset++] = HI_BYTE(length);
  3439. write_buf[cmd_offset++] = ebid;
  3440. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3441. if (rc)
  3442. return rc;
  3443. status = cd->response_buf[5];
  3444. if (status)
  3445. return status;
  3446. read_ebid = cd->response_buf[6];
  3447. if ((read_ebid != ebid) || (cd->response_buf[9] != 0))
  3448. return -EPROTO;
  3449. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  3450. if (length == 0 || *actual_read_len == 0)
  3451. return 0;
  3452. if (read_buf_size >= *actual_read_len)
  3453. memcpy(read_buf, &cd->response_buf[10], *actual_read_len);
  3454. else
  3455. return -EPROTO;
  3456. *crc = get_unaligned_le16(&cd->response_buf[*actual_read_len + 10]);
  3457. /* Validate Row Data CRC */
  3458. calc_crc = _pt_compute_crc(read_buf, *actual_read_len);
  3459. if (*crc == calc_crc) {
  3460. return 0;
  3461. } else {
  3462. pt_debug(cd->dev, DL_ERROR,
  3463. "%s: CRC Mismatch packet=0x%04X calc=0x%04X\n",
  3464. __func__, *crc, calc_crc);
  3465. return -EPROTO;
  3466. }
  3467. }
  3468. /*******************************************************************************
  3469. * FUNCTION: _pt_request_pip_read_data_block
  3470. *
  3471. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3472. * calls to pt_pip1_read_data_block_
  3473. *
  3474. * RETURN:
  3475. * 0 = success
  3476. * !0 = failure
  3477. *
  3478. * PARAMETERS:
  3479. * *dev - pointer to device structure
  3480. * row_number - row number
  3481. * length - length of data to read
  3482. * ebid - block id
  3483. * *actual_read_len - Actual data length read
  3484. * *read_buf - pointer to the buffer to store read data
  3485. * *crc - pointer to store CRC of row data
  3486. ******************************************************************************/
  3487. static int _pt_request_pip_read_data_block(struct device *dev,
  3488. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3489. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3490. {
  3491. struct pt_core_data *cd = dev_get_drvdata(dev);
  3492. return pt_pip1_read_data_block_(cd, row_number, length,
  3493. ebid, actual_read_len, read_buf, read_buf_size, crc);
  3494. }
  3495. /*******************************************************************************
  3496. * FUNCTION: pt_pip1_write_data_block_
  3497. *
  3498. * SUMMARY: Sends the PIP "Write Data Block" (0x23) command to the DUT and
  3499. * write data to the data block.
  3500. *
  3501. * RETURN:
  3502. * 0 = success
  3503. * !0 = failure
  3504. *
  3505. * PARAMETERS:
  3506. * *cd - pointer to core data
  3507. * row_number - row in config block to write to
  3508. * write_length - length of data to write
  3509. * ebid - enumerated block ID
  3510. * *write_buf - pointer to buffer to write
  3511. * *security_key - pointer to security key to allow write
  3512. * *actual_write_len - pointer to store data length actually written
  3513. ******************************************************************************/
  3514. static int pt_pip1_write_data_block_(struct pt_core_data *cd,
  3515. u16 row_number, u16 write_length, u8 ebid, u8 *write_buf,
  3516. u8 *security_key, u16 *actual_write_len)
  3517. {
  3518. /* row_number + write_len + ebid + security_key + crc */
  3519. int full_write_length = 2 + 2 + 1 + write_length + 8 + 2;
  3520. u8 *full_write_buf;
  3521. u8 cmd_offset = 0;
  3522. u16 crc;
  3523. int status;
  3524. int rc = 0;
  3525. int read_ebid;
  3526. u8 *data;
  3527. struct pt_hid_output hid_output = {
  3528. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_WRITE_DATA_BLOCK),
  3529. .write_length = full_write_length,
  3530. .timeout_ms = PT_PIP1_CMD_WRITE_CONF_BLOCK_TIMEOUT,
  3531. };
  3532. full_write_buf = kzalloc(full_write_length, GFP_KERNEL);
  3533. if (!full_write_buf)
  3534. return -ENOMEM;
  3535. hid_output.write_buf = full_write_buf;
  3536. full_write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3537. full_write_buf[cmd_offset++] = HI_BYTE(row_number);
  3538. full_write_buf[cmd_offset++] = LOW_BYTE(write_length);
  3539. full_write_buf[cmd_offset++] = HI_BYTE(write_length);
  3540. full_write_buf[cmd_offset++] = ebid;
  3541. data = &full_write_buf[cmd_offset];
  3542. memcpy(data, write_buf, write_length);
  3543. cmd_offset += write_length;
  3544. memcpy(&full_write_buf[cmd_offset], security_key, 8);
  3545. cmd_offset += 8;
  3546. crc = _pt_compute_crc(data, write_length);
  3547. full_write_buf[cmd_offset++] = LOW_BYTE(crc);
  3548. full_write_buf[cmd_offset++] = HI_BYTE(crc);
  3549. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3550. if (rc)
  3551. goto exit;
  3552. status = cd->response_buf[5];
  3553. if (status) {
  3554. rc = -EINVAL;
  3555. goto exit;
  3556. }
  3557. read_ebid = cd->response_buf[6];
  3558. if (read_ebid != ebid) {
  3559. rc = -EPROTO;
  3560. goto exit;
  3561. }
  3562. *actual_write_len = get_unaligned_le16(&cd->response_buf[7]);
  3563. exit:
  3564. kfree(full_write_buf);
  3565. return rc;
  3566. }
  3567. /*******************************************************************************
  3568. * FUNCTION: _pt_request_pip_write_data_block
  3569. *
  3570. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3571. * calls to the protected or unprotected call to pt_pip1_write_data_block_
  3572. *
  3573. * RETURN:
  3574. * 0 = success
  3575. * !0 = failure
  3576. *
  3577. * PARAMETERS:
  3578. * *dev - pointer to device structure
  3579. * protect - flag to call protected or non-protected
  3580. * row_number - row in config block to write to
  3581. * write_length - length of data to write
  3582. * ebid - enumerated block ID
  3583. * *write_buf - pointer to buffer to write
  3584. * *security_key - pointer to security key to allow write
  3585. * *actual_write_len - pointer to store data length actually written
  3586. ******************************************************************************/
  3587. static int _pt_request_pip_write_data_block(struct device *dev,
  3588. u16 row_number, u16 write_length, u8 ebid,
  3589. u8 *write_buf, u8 *security_key, u16 *actual_write_len)
  3590. {
  3591. struct pt_core_data *cd = dev_get_drvdata(dev);
  3592. return pt_pip1_write_data_block_(cd, row_number,
  3593. write_length, ebid, write_buf, security_key,
  3594. actual_write_len);
  3595. }
  3596. /*******************************************************************************
  3597. * FUNCTION: pt_pip_get_data_structure_
  3598. *
  3599. * SUMMARY: Sends the PIP "Retrieve Data Structure" (0x24) command to the DUT
  3600. * returning a structure of data defined by data_id
  3601. *
  3602. * RETURN:
  3603. * 0 = success
  3604. * !0 = failure
  3605. *
  3606. * PARAMETERS:
  3607. * *cd - pointer to core data
  3608. * read_offset - read pointer offset
  3609. * read_length - length of data to read
  3610. * data_id - data ID to read
  3611. * *status - pointer to store the read response status
  3612. * *data_format - pointer to store format of data read
  3613. * *actual_read_len - pointer to store data length actually read
  3614. * *data - pointer to store data read
  3615. ******************************************************************************/
  3616. static int pt_pip_get_data_structure_(
  3617. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3618. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3619. u8 *data)
  3620. {
  3621. int rc = 0;
  3622. u16 total_read_len = 0;
  3623. u16 read_len;
  3624. u16 off_buf = 0;
  3625. u8 write_buf[5];
  3626. u8 read_data_id;
  3627. struct pt_hid_output hid_output = {
  3628. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_DATA_STRUCTURE),
  3629. .write_length = 5,
  3630. .write_buf = write_buf,
  3631. };
  3632. again:
  3633. write_buf[0] = LOW_BYTE(read_offset);
  3634. write_buf[1] = HI_BYTE(read_offset);
  3635. write_buf[2] = LOW_BYTE(read_length);
  3636. write_buf[3] = HI_BYTE(read_length);
  3637. write_buf[4] = data_id;
  3638. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3639. if (rc)
  3640. return rc;
  3641. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  3642. goto set_status;
  3643. read_data_id = cd->response_buf[6];
  3644. if (read_data_id != data_id)
  3645. return -EPROTO;
  3646. read_len = get_unaligned_le16(&cd->response_buf[7]);
  3647. if (read_len && data) {
  3648. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  3649. total_read_len += read_len;
  3650. if (read_len < read_length) {
  3651. read_offset += read_len;
  3652. off_buf += read_len;
  3653. read_length -= read_len;
  3654. goto again;
  3655. }
  3656. }
  3657. if (data_format)
  3658. *data_format = cd->response_buf[9];
  3659. if (actual_read_len)
  3660. *actual_read_len = total_read_len;
  3661. set_status:
  3662. if (status)
  3663. *status = cd->response_buf[5];
  3664. return rc;
  3665. }
  3666. /*******************************************************************************
  3667. * FUNCTION: pt_pip_get_data_structure
  3668. *
  3669. * SUMMARY: Protected call to pt_hid_output_get_data_structure within
  3670. * an exclusive access to the DUT.
  3671. *
  3672. * RETURN:
  3673. * 0 = success
  3674. * !0 = failure
  3675. *
  3676. * PARAMETERS:
  3677. * *cd - pointer to core data
  3678. * read_offset - read pointer offset
  3679. * read_length - length of data to read
  3680. * data_id - data ID to read
  3681. * *status - pointer to store the read response status
  3682. * *data_format - pointer to store format of data read
  3683. * *actual_read_len - pointer to store data length actually read
  3684. * *data - pointer to store data read
  3685. ******************************************************************************/
  3686. static int pt_pip_get_data_structure(
  3687. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3688. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3689. u8 *data)
  3690. {
  3691. int rc;
  3692. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3693. if (rc < 0) {
  3694. pt_debug(cd->dev, DL_ERROR,
  3695. "%s: fail get exclusive ex=%p own=%p\n",
  3696. __func__, cd->exclusive_dev, cd->dev);
  3697. return rc;
  3698. }
  3699. rc = pt_pip_get_data_structure_(cd, read_offset,
  3700. read_length, data_id, status, data_format,
  3701. actual_read_len, data);
  3702. if (release_exclusive(cd, cd->dev) < 0)
  3703. pt_debug(cd->dev, DL_ERROR,
  3704. "%s: fail to release exclusive\n", __func__);
  3705. return rc;
  3706. }
  3707. /*******************************************************************************
  3708. * FUNCTION: _pt_request_pip_get_data_structure
  3709. *
  3710. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3711. * calls to the protected or unprotected call to
  3712. * pt_pip_get_data_structure
  3713. *
  3714. * RETURN:
  3715. * 0 = success
  3716. * !0 = failure
  3717. *
  3718. * PARAMETERS:
  3719. * *dev - pointer to device structure
  3720. * protect - flag to call protected or non-protected
  3721. * read_offset - read pointer offset
  3722. * read_length - length of data to read
  3723. * data_id - data ID to read
  3724. * *status - pointer to store the read response status
  3725. * *data_format - pointer to store format of data read
  3726. * *actual_read_len - pointer to store data length actually read
  3727. * *data - pointer to store data read
  3728. ******************************************************************************/
  3729. static int _pt_request_pip_get_data_structure(struct device *dev,
  3730. int protect, u16 read_offset, u16 read_length, u8 data_id,
  3731. u8 *status, u8 *data_format, u16 *actual_read_len, u8 *data)
  3732. {
  3733. struct pt_core_data *cd = dev_get_drvdata(dev);
  3734. if (protect)
  3735. return pt_pip_get_data_structure(cd,
  3736. read_offset, read_length, data_id, status,
  3737. data_format, actual_read_len, data);
  3738. return pt_pip_get_data_structure_(cd,
  3739. read_offset, read_length, data_id, status,
  3740. data_format, actual_read_len, data);
  3741. }
  3742. /*******************************************************************************
  3743. * FUNCTION: _pt_manage_local_cal_data
  3744. *
  3745. * SUMMARY: This function manages storing or restoring a copy of the Firmware
  3746. * CALIBRATION data. It stores it in a local static array and can be
  3747. * cleared, loaded or used to restore the CAL data back to the running FW.
  3748. * The CAL data is read or restored by use of the PIP1 commands:
  3749. * - READ_DATA_BLOCK (0x22)
  3750. * - WRITE_DATA_BLOCK (0x23)
  3751. *
  3752. * RETURN:
  3753. * 0 = success
  3754. * !0 = failure
  3755. *
  3756. * PARAMETERS:
  3757. * *dev - pointer to device structure
  3758. * action - One of the following actions:
  3759. * - PT_CAL_DATA_SAVE
  3760. * - PT_CAL_DATA_RESTORE
  3761. * - PT_CAL_DATA_CLEAR
  3762. * - PT_CAL_DATA_SIZE
  3763. * *size - pointer to the number of bytes transferred
  3764. * *crc - pointer to Chip ID CRC that the CAL data was retrieved from
  3765. ******************************************************************************/
  3766. static int _pt_manage_local_cal_data(struct device *dev, u8 action, u16 *size,
  3767. unsigned short *crc)
  3768. {
  3769. struct pt_core_data *cd = dev_get_drvdata(dev);
  3770. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  3771. unsigned short calc_id_crc = 0;
  3772. static u8 *cal_cache_data;
  3773. static u16 cal_cache_len;
  3774. static unsigned short cal_cache_chip_id;
  3775. int rc = 0;
  3776. u8 *tmp_data = NULL;
  3777. u8 row_number = 0;
  3778. u8 prefix[20];
  3779. u16 cal_size = 0;
  3780. u16 transfer_size;
  3781. u16 act_trans_len = 0;
  3782. u16 byte_offset = 0;
  3783. u16 cal_blk_size;
  3784. u16 total_rows;
  3785. u16 remain_bytes;
  3786. u16 data_block_crc;
  3787. u16 buf_size = 12;
  3788. pt_debug(dev, DL_INFO, "%s: ATM - CAL Cache action=%d\n",
  3789. __func__, action);
  3790. switch (action) {
  3791. case PT_CAL_DATA_SAVE:
  3792. /* Read the size of the CAL block and calculate # rows */
  3793. tmp_data = kzalloc(buf_size, GFP_KERNEL);
  3794. if (!tmp_data) {
  3795. rc = -ENOMEM;
  3796. goto exit;
  3797. }
  3798. /*
  3799. * Don't check rc as doing a read size will give a false
  3800. * error on the CRC check.
  3801. */
  3802. rc = pt_pip1_read_data_block_(cd, row_number, 0, PT_CAL_EBID,
  3803. &act_trans_len, tmp_data, buf_size, &data_block_crc);
  3804. cal_blk_size = act_trans_len;
  3805. kfree(tmp_data);
  3806. pt_debug(dev, DL_INFO,
  3807. "%s: CAL Cache size=%d FW CAL Size=%d\n",
  3808. __func__, cal_cache_len, cal_blk_size);
  3809. /* Safety net to ensure we didn't read incorrect size */
  3810. if (cal_blk_size > PT_CAL_DATA_MAX_SIZE) {
  3811. pt_debug(dev, DL_ERROR, "%s: Alloc struct Failed\n",
  3812. __func__);
  3813. rc = 1;
  3814. goto exit;
  3815. }
  3816. /* Panels could have diff CAL sizes, Re-allocate the cache */
  3817. if (cal_blk_size != cal_cache_len) {
  3818. kfree(cal_cache_data);
  3819. cal_cache_data = kzalloc(cal_blk_size + 2,
  3820. GFP_KERNEL);
  3821. if (!cal_cache_data) {
  3822. rc = -ENOMEM;
  3823. goto exit;
  3824. }
  3825. pt_debug(dev, DL_INFO, "%s: CAL Cache Allocated\n",
  3826. __func__);
  3827. }
  3828. memset(&cal_cache_data[0], 0, cal_blk_size + 2);
  3829. /* Calculate how many rows [0-n] (PIP Transactions) */
  3830. total_rows = (cal_blk_size / PT_CAL_DATA_ROW_SIZE) - 1;
  3831. remain_bytes = cal_blk_size % PT_CAL_DATA_ROW_SIZE;
  3832. /* Add row if we have a last partial row */
  3833. if (remain_bytes > 0)
  3834. total_rows++;
  3835. pt_debug(dev, DL_INFO,
  3836. "%s: CAL size=%d rows=[0-%d] partial row bytes=%d\n",
  3837. __func__, cal_blk_size, total_rows, remain_bytes);
  3838. /* Read all rows unless an error occurs */
  3839. rc = 0;
  3840. while (rc == 0 && row_number <= total_rows) {
  3841. act_trans_len = 0;
  3842. if (remain_bytes > 0 && row_number == total_rows)
  3843. transfer_size = remain_bytes;
  3844. else
  3845. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3846. rc = pt_pip1_read_data_block_(cd, row_number,
  3847. transfer_size, PT_CAL_EBID,
  3848. &act_trans_len,
  3849. &cal_cache_data[byte_offset], cal_blk_size + 2,
  3850. &data_block_crc);
  3851. if (rc) {
  3852. /* Error occurred, exit loop */
  3853. cal_size = 0;
  3854. break;
  3855. }
  3856. pt_debug(dev, DL_INFO,
  3857. "%s: CAL read rc=%d actual read len=%d\n",
  3858. __func__, rc, act_trans_len);
  3859. byte_offset += act_trans_len;
  3860. cal_size = byte_offset;
  3861. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3862. pt_pr_buf(dev, DL_INFO,
  3863. &cal_cache_data[byte_offset - act_trans_len],
  3864. act_trans_len, prefix);
  3865. row_number++;
  3866. }
  3867. if (cal_size > 0) {
  3868. /* Save a CRC of the chip info the CAL was saved from */
  3869. calc_id_crc = crc_ccitt_calculate(
  3870. (u8 *)&ttdata->chip_rev, 4 + PT_UID_SIZE);
  3871. cal_cache_chip_id = calc_id_crc;
  3872. cal_cache_len = cal_size;
  3873. pt_debug(dev, DL_INFO,
  3874. "%s: CAL Cache: CRC=0x%04X Total Size=%d\n",
  3875. __func__, calc_id_crc, cal_size);
  3876. }
  3877. *size = cal_size;
  3878. *crc = calc_id_crc;
  3879. break;
  3880. case PT_CAL_DATA_RESTORE:
  3881. cal_size = cal_cache_len;
  3882. while ((rc == 0) && (byte_offset < cal_size)) {
  3883. if (cal_size - byte_offset > PT_CAL_DATA_ROW_SIZE)
  3884. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3885. else
  3886. transfer_size = cal_size - byte_offset;
  3887. rc = pt_pip1_write_data_block_(cd, row_number,
  3888. transfer_size, PT_CAL_EBID,
  3889. &cal_cache_data[byte_offset],
  3890. (u8 *)pt_data_block_security_key,
  3891. &act_trans_len);
  3892. byte_offset += act_trans_len;
  3893. pt_debug(dev, DL_INFO, "%s: CAL write byte offset=%d\n",
  3894. __func__, byte_offset);
  3895. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3896. pt_pr_buf(dev, DL_INFO,
  3897. &cal_cache_data[byte_offset - act_trans_len],
  3898. act_trans_len, prefix);
  3899. if ((byte_offset > cal_size) ||
  3900. (act_trans_len != transfer_size))
  3901. rc = -EIO;
  3902. row_number++;
  3903. }
  3904. *size = byte_offset;
  3905. *crc = cal_cache_chip_id;
  3906. break;
  3907. case PT_CAL_DATA_CLEAR:
  3908. if (cal_cache_data)
  3909. memset(&cal_cache_data[0], 0, cal_cache_len);
  3910. cal_cache_len = 0;
  3911. cal_cache_chip_id = 0;
  3912. *size = 0;
  3913. *crc = 0;
  3914. break;
  3915. case PT_CAL_DATA_INFO:
  3916. default:
  3917. *size = cal_cache_len;
  3918. *crc = cal_cache_chip_id;
  3919. pt_debug(dev, DL_INFO,
  3920. "%s: CAL Cache: CRC=%04X Total Size=%d\n",
  3921. __func__, cal_cache_chip_id,
  3922. cal_cache_len);
  3923. break;
  3924. }
  3925. exit:
  3926. pt_debug(dev, DL_INFO,
  3927. "%s: CAL Cache exit: rc=%d CRC=0x%04X Total Size=%d\n",
  3928. __func__, rc, *crc, *size);
  3929. return rc;
  3930. }
  3931. /*******************************************************************************
  3932. * FUNCTION: pt_pip_run_selftest_
  3933. *
  3934. * SUMMARY: Sends the PIP "Run Self Test" (0x26) command to the DUT
  3935. * to execute a FW built in self test
  3936. *
  3937. * RETURN:
  3938. * 0 = success
  3939. * !0 = failure
  3940. *
  3941. * PARAMETERS:
  3942. * *cd - pointer to core data
  3943. * test_id - enumerated test ID to run
  3944. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3945. * *status - pointer to store the read response status
  3946. * *summary_results - pointer to store the results summary
  3947. * *results_available - pointer to store if results are available
  3948. *****************************************************************************/
  3949. static int pt_pip_run_selftest_(
  3950. struct pt_core_data *cd, u8 test_id,
  3951. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3952. u8 *results_available)
  3953. {
  3954. int rc = 0;
  3955. u8 write_buf[2];
  3956. struct pt_hid_output hid_output = {
  3957. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RUN_SELF_TEST),
  3958. .write_length = 2,
  3959. .write_buf = write_buf,
  3960. .timeout_ms = PT_PIP1_CMD_RUN_SELF_TEST_TIMEOUT,
  3961. };
  3962. write_buf[0] = test_id;
  3963. write_buf[1] = write_idacs_to_flash;
  3964. if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  3965. hid_output.write_length = 1;
  3966. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3967. if (rc)
  3968. return rc;
  3969. if (status)
  3970. *status = cd->response_buf[5];
  3971. if (summary_result)
  3972. *summary_result = cd->response_buf[6];
  3973. /* results_available only available before PIP 1.03 */
  3974. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 3)) {
  3975. if (results_available)
  3976. *results_available = cd->response_buf[7];
  3977. }
  3978. return rc;
  3979. }
  3980. /*******************************************************************************
  3981. * FUNCTION: pt_pip_run_selftest
  3982. *
  3983. * SUMMARY: Protected call to pt_hid_output_run_selftest within
  3984. * an exclusive access to the DUT.
  3985. *
  3986. * RETURN:
  3987. * 0 = success
  3988. * !0 = failure
  3989. *
  3990. * PARAMETERS:
  3991. * *cd - pointer to core data
  3992. * test_id - enumerated test ID to run
  3993. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3994. * *status - pointer to store the read response status
  3995. * *summary_results - pointer to store the results summary
  3996. * *results_available - pointer to store if results are available
  3997. ******************************************************************************/
  3998. static int pt_pip_run_selftest(
  3999. struct pt_core_data *cd, u8 test_id,
  4000. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  4001. u8 *results_available)
  4002. {
  4003. int rc;
  4004. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4005. if (rc < 0) {
  4006. pt_debug(cd->dev, DL_ERROR,
  4007. "%s: fail get exclusive ex=%p own=%p\n",
  4008. __func__, cd->exclusive_dev, cd->dev);
  4009. return rc;
  4010. }
  4011. rc = pt_pip_run_selftest_(cd, test_id,
  4012. write_idacs_to_flash, status, summary_result,
  4013. results_available);
  4014. if (release_exclusive(cd, cd->dev) < 0)
  4015. pt_debug(cd->dev, DL_ERROR,
  4016. "%s: fail to release exclusive\n", __func__);
  4017. return rc;
  4018. }
  4019. /*******************************************************************************
  4020. * FUNCTION: _pt_request_pip_run_selftest
  4021. *
  4022. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4023. * calls to the protected or unprotected call to pt_pip_run_selftest
  4024. *
  4025. * RETURN:
  4026. * 0 = success
  4027. * !0 = failure
  4028. *
  4029. * PARAMETERS:
  4030. * *dev - pointer to device structure
  4031. * protect - flag to call protected or non-protected
  4032. * test_id - enumerated test ID to run
  4033. * write_idacs_to_flash - flag whether to write new IDACS to flash
  4034. * *status - pointer to store the read response status
  4035. * *summary_results - pointer to store the results summary
  4036. * *results_available - pointer to store if results are available
  4037. ******************************************************************************/
  4038. static int _pt_request_pip_run_selftest(struct device *dev,
  4039. int protect, u8 test_id, u8 write_idacs_to_flash, u8 *status,
  4040. u8 *summary_result, u8 *results_available)
  4041. {
  4042. struct pt_core_data *cd = dev_get_drvdata(dev);
  4043. if (protect)
  4044. return pt_pip_run_selftest(cd, test_id,
  4045. write_idacs_to_flash, status, summary_result,
  4046. results_available);
  4047. return pt_pip_run_selftest_(cd, test_id,
  4048. write_idacs_to_flash, status, summary_result,
  4049. results_available);
  4050. }
  4051. /*******************************************************************************
  4052. * FUNCTION: _pt_pip_get_selftest_result_
  4053. *
  4054. * SUMMARY: Sends the PIP "Get Self Test Results" (0x27) command to the DUT
  4055. * to retrieve the self test results from the self test already executed
  4056. *
  4057. * RETURN:
  4058. * 0 = success
  4059. * !0 = failure
  4060. *
  4061. * PARAMETERS:
  4062. * *cd - pointer to core data
  4063. * read_offset - read pointer offset
  4064. * read_length - length of data to read
  4065. * test_id - enumerated test ID to read selftest results from
  4066. * *status - pointer to store the read response status
  4067. * *actual_read_len - pointer to store data length actually read
  4068. * *status - pointer to where the cmd response statas is stored
  4069. ******************************************************************************/
  4070. static int pt_pip_get_selftest_result_(
  4071. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4072. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4073. {
  4074. int rc = 0;
  4075. u16 total_read_len = 0;
  4076. u16 read_len;
  4077. u16 off_buf = 0;
  4078. u8 write_buf[5];
  4079. u8 read_test_id;
  4080. bool repeat;
  4081. struct pt_hid_output hid_output = {
  4082. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SELF_TEST_RESULT),
  4083. .write_length = 5,
  4084. .write_buf = write_buf,
  4085. };
  4086. /*
  4087. * Do not repeat reading for Auto Shorts test
  4088. * when PIP version < 1.3
  4089. */
  4090. repeat = IS_PIP_VER_GE(&cd->sysinfo, 1, 3)
  4091. || test_id != PT_ST_ID_AUTOSHORTS;
  4092. again:
  4093. write_buf[0] = LOW_BYTE(read_offset);
  4094. write_buf[1] = HI_BYTE(read_offset);
  4095. write_buf[2] = LOW_BYTE(read_length);
  4096. write_buf[3] = HI_BYTE(read_length);
  4097. write_buf[4] = test_id;
  4098. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4099. if (rc)
  4100. return rc;
  4101. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  4102. goto set_status;
  4103. read_test_id = cd->response_buf[6];
  4104. if (read_test_id != test_id)
  4105. return -EPROTO;
  4106. read_len = get_unaligned_le16(&cd->response_buf[7]);
  4107. if (read_len && data) {
  4108. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  4109. total_read_len += read_len;
  4110. if (repeat && read_len < read_length) {
  4111. read_offset += read_len;
  4112. off_buf += read_len;
  4113. read_length -= read_len;
  4114. goto again;
  4115. }
  4116. }
  4117. if (actual_read_len)
  4118. *actual_read_len = total_read_len;
  4119. set_status:
  4120. if (status)
  4121. *status = cd->response_buf[5];
  4122. return rc;
  4123. }
  4124. /*******************************************************************************
  4125. * FUNCTION: _pt_pip_get_selftest_result
  4126. *
  4127. * SUMMARY: Protected call to pt_hid_output_get_selftest_result by exclusive
  4128. * access to the DUT
  4129. *
  4130. * RETURN:
  4131. * 0 = success
  4132. * !0 = failure
  4133. *
  4134. * PARAMETERS:
  4135. * *cd - pointer to core data
  4136. * read_offset - read pointer offset
  4137. * read_length - length of data to read
  4138. * test_id - enumerated test ID to read selftest results from
  4139. * *status - pointer to store the read response status
  4140. * *actual_read_len - pointer to store data length actually read
  4141. * *status - pointer to where the cmd response statas is stored
  4142. ******************************************************************************/
  4143. static int pt_pip_get_selftest_result(
  4144. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4145. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4146. {
  4147. int rc;
  4148. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4149. if (rc < 0) {
  4150. pt_debug(cd->dev, DL_ERROR,
  4151. "%s: fail get exclusive ex=%p own=%p\n",
  4152. __func__, cd->exclusive_dev, cd->dev);
  4153. return rc;
  4154. }
  4155. rc = pt_pip_get_selftest_result_(cd, read_offset,
  4156. read_length, test_id, status, actual_read_len, data);
  4157. if (release_exclusive(cd, cd->dev) < 0)
  4158. pt_debug(cd->dev, DL_ERROR,
  4159. "%s: fail to release exclusive\n", __func__);
  4160. return rc;
  4161. }
  4162. /*******************************************************************************
  4163. * FUNCTION: _pt_request_pip_get_selftest_result
  4164. *
  4165. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4166. * calls to the protected or unprotected call to pt_pip_get_selftest_result
  4167. *
  4168. * RETURN:
  4169. * 0 = success
  4170. * !0 = failure
  4171. *
  4172. * PARAMETERS:
  4173. * *dev - pointer to device structure
  4174. * protect - flag to call protected or non-protected
  4175. * read_offset - read pointer offset
  4176. * read_length - length of data to read
  4177. * test_id - enumerated test ID to read selftest results from
  4178. * *status - pointer to store the read response status
  4179. * *actual_read_len - pointer to store data length actually read
  4180. * *data - pointer to where the data read is stored
  4181. ******************************************************************************/
  4182. static int _pt_request_pip_get_selftest_result(struct device *dev,
  4183. int protect, u16 read_offset, u16 read_length, u8 test_id,
  4184. u8 *status, u16 *actual_read_len, u8 *data)
  4185. {
  4186. struct pt_core_data *cd = dev_get_drvdata(dev);
  4187. if (protect)
  4188. return pt_pip_get_selftest_result(cd, read_offset,
  4189. read_length, test_id, status, actual_read_len,
  4190. data);
  4191. return pt_pip_get_selftest_result_(cd, read_offset,
  4192. read_length, test_id, status, actual_read_len,
  4193. data);
  4194. }
  4195. /*******************************************************************************
  4196. * FUNCTION: _pt_pip_load_self_test_param
  4197. *
  4198. * SUMMARY: Sends the PIP "Load Self Test Parameters" (0x25) command to the DUT
  4199. * to load parameters needed by a self test
  4200. *
  4201. * RETURN:
  4202. * 0 = success
  4203. * !0 = failure
  4204. *
  4205. * PARAMETERS:
  4206. * *cd - pointer to core data
  4207. * self_test_id - enumerated test ID for which the parmeters belong
  4208. * load_offset - mem offset to where to load parameters
  4209. * load_length - length of parameter data to load
  4210. * *parameters - pointer to list of parameter data
  4211. * *status - pointer to store the response status
  4212. * *ret_test_id - pointer to returned test id the parameters were stored
  4213. * *act_load_len - pointer to store the actual load length that was writen
  4214. ******************************************************************************/
  4215. static int pt_pip_load_self_test_param_(struct pt_core_data *cd,
  4216. u8 self_test_id, u16 load_offset, u16 load_length,
  4217. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4218. {
  4219. int rc = 0;
  4220. int i;
  4221. u8 write_buf[PT_MAX_PIP1_MSG_SIZE];
  4222. struct pt_hid_output hid_output = {
  4223. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_LOAD_SELF_TEST_PARAM),
  4224. .write_length = 5 + load_length,
  4225. .write_buf = write_buf,
  4226. .timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT,
  4227. };
  4228. write_buf[0] = LOW_BYTE(load_offset);
  4229. write_buf[1] = HI_BYTE(load_offset);
  4230. write_buf[2] = LOW_BYTE(load_length);
  4231. write_buf[3] = HI_BYTE(load_length);
  4232. write_buf[4] = self_test_id;
  4233. for (i = 0; i < load_length; i++)
  4234. write_buf[i + 5] = parameters[i];
  4235. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4236. if (rc)
  4237. return rc;
  4238. if (status)
  4239. *status = cd->response_buf[5];
  4240. if (ret_test_id)
  4241. *ret_test_id = cd->response_buf[6];
  4242. if (act_load_len)
  4243. *act_load_len = get_unaligned_le16(&cd->response_buf[7]);
  4244. return rc;
  4245. }
  4246. /*******************************************************************************
  4247. * FUNCTION: pt_pip_load_self_test_param
  4248. *
  4249. * SUMMARY: Protected call to pt_pip_load_self_test_param_ within an exclusive
  4250. * access to the DUT.
  4251. *
  4252. * RETURN:
  4253. * 0 = success
  4254. * !0 = failure
  4255. *
  4256. * PARAMETERS:
  4257. * *cd - pointer to core data
  4258. * self_test_id - enumerated test ID for which the parmeters belong
  4259. * load_offset - mem offset to where to load parameters
  4260. * load_length - length of parameter data to load
  4261. * *parameters - pointer to list of parameter data
  4262. * *status - pointer to store the response status
  4263. * *ret_test_id - pointer to returned test id the parameters were stored
  4264. * *act_load_len - pointer to store the actual load length that was writen
  4265. ******************************************************************************/
  4266. static int pt_pip_load_self_test_param(struct pt_core_data *cd,
  4267. u8 self_test_id, u16 load_offset, u16 load_length,
  4268. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4269. {
  4270. int rc;
  4271. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4272. if (rc < 0) {
  4273. pt_debug(cd->dev, DL_ERROR,
  4274. "%s: fail get exclusive ex=%p own=%p\n",
  4275. __func__, cd->exclusive_dev, cd->dev);
  4276. return rc;
  4277. }
  4278. rc = pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4279. load_length, parameters, status, ret_test_id, act_load_len);
  4280. if (release_exclusive(cd, cd->dev) < 0)
  4281. pt_debug(cd->dev, DL_ERROR,
  4282. "%s: fail to release exclusive\n", __func__);
  4283. return rc;
  4284. }
  4285. /*******************************************************************************
  4286. * FUNCTION: _pt_request_pip_load_self_test_param
  4287. *
  4288. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4289. * calls to the protected or unprotected call to
  4290. * pt_pip_load_self_test_param
  4291. *
  4292. * RETURN:
  4293. * 0 = success
  4294. * !0 = failure
  4295. *
  4296. * PARAMETERS:
  4297. * *dev - pointer to device structure
  4298. * protect - flag to call protected or non-protected
  4299. * self_test_id - enumerated test ID for which the parmeters belong
  4300. * load_offset - mem offset to where to load parameters
  4301. * load_length - length of parameter data to load
  4302. * *parameters - pointer to list of parameter data
  4303. * *status - pointer to store the response status
  4304. * *ret_test_id - pointer to returned test id the parameters were stored
  4305. * *act_load_len - pointer to store the actual load length that was writen
  4306. ******************************************************************************/
  4307. static int _pt_request_pip_load_self_test_param(struct device *dev,
  4308. int protect, u8 self_test_id, u16 load_offset, u16 load_length,
  4309. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4310. {
  4311. struct pt_core_data *cd = dev_get_drvdata(dev);
  4312. if (protect)
  4313. return pt_pip_load_self_test_param(cd, self_test_id,
  4314. load_offset, load_length, parameters, status, ret_test_id,
  4315. act_load_len);
  4316. return pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4317. load_length, parameters, status, ret_test_id, act_load_len);
  4318. }
  4319. /*******************************************************************************
  4320. * FUNCTION: pt_pip_calibrate_ext_
  4321. *
  4322. * SUMMARY: Send the PIP1 Extended Calibrate command (0x30) to the DUT waiting
  4323. * for the response
  4324. *
  4325. * NOTE: This calibrate command requires the DUT to support PIP version >= 1.10
  4326. *
  4327. * RETURN:
  4328. * 0 = success
  4329. * !0 = failure
  4330. *
  4331. * PARAMETERS:
  4332. * *cd - pointer to core data
  4333. * *cal_data - pointer to extended calibration data structure
  4334. * *status - pointer to where the command response status is stored
  4335. ******************************************************************************/
  4336. static int pt_pip_calibrate_ext_(struct pt_core_data *cd,
  4337. struct pt_cal_ext_data *cal_data, u8 *status)
  4338. {
  4339. int rc = 0;
  4340. int write_length = 4;
  4341. u8 write_buf[4];
  4342. u16 size = 0;
  4343. unsigned short crc = 0;
  4344. struct pt_hid_output hid_output = {
  4345. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_DEVICE_EXTENDED),
  4346. .write_length = write_length,
  4347. .write_buf = write_buf,
  4348. .timeout_ms = PT_PIP1_CMD_CALIBRATE_EXT_TIMEOUT,
  4349. };
  4350. if (cal_data == NULL)
  4351. return -EINVAL;
  4352. memcpy(write_buf, cal_data, sizeof(struct pt_cal_ext_data));
  4353. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4354. if (rc)
  4355. return rc;
  4356. if (status)
  4357. *status = cd->response_buf[5];
  4358. /*
  4359. * When doing a calibration on a flashless DUT, save CAL data in
  4360. * the TTDL cache on any successful calibration
  4361. */
  4362. if (*status == 0 && cd->cal_cache_in_host) {
  4363. pt_debug(cd->dev, DL_INFO, "%s: Retrieve and Save CAL\n",
  4364. __func__);
  4365. rc = _pt_manage_local_cal_data(cd->dev, PT_CAL_DATA_SAVE,
  4366. &size, &crc);
  4367. if (rc)
  4368. pt_debug(cd->dev, DL_ERROR,
  4369. "%s: Error Saving CAL rc=%d\n", __func__, rc);
  4370. else
  4371. pt_debug(cd->dev, DL_INFO,
  4372. "%s: Saved CAL: chip ID=0x%04X size=%d\n",
  4373. __func__, crc, size);
  4374. }
  4375. return 0;
  4376. }
  4377. /*******************************************************************************
  4378. * FUNCTION: pt_pip_calibrate_ext
  4379. *
  4380. * SUMMARY: Protected call to pt_pip_calibrate_ext_ by exclusive access to the
  4381. * DUT
  4382. *
  4383. * RETURN:
  4384. * 0 = success
  4385. * !0 = failure
  4386. *
  4387. * PARAMETERS:
  4388. * *cd - pointer to core data
  4389. * *cal_data - pointer to extended calibration data structure
  4390. * *status - pointer to where the command response status is stored
  4391. ******************************************************************************/
  4392. static int pt_pip_calibrate_ext(struct pt_core_data *cd,
  4393. struct pt_cal_ext_data *cal_data, u8 *status)
  4394. {
  4395. int rc;
  4396. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4397. if (rc < 0) {
  4398. pt_debug(cd->dev, DL_ERROR,
  4399. "%s: fail get exclusive ex=%p own=%p\n",
  4400. __func__, cd->exclusive_dev, cd->dev);
  4401. return rc;
  4402. }
  4403. rc = pt_pip_calibrate_ext_(cd, cal_data, status);
  4404. if (release_exclusive(cd, cd->dev) < 0)
  4405. pt_debug(cd->dev, DL_ERROR,
  4406. "%s: fail to release exclusive\n", __func__);
  4407. return rc;
  4408. }
  4409. /*******************************************************************************
  4410. * FUNCTION: _pt_request_pip_calibrate_ext
  4411. *
  4412. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4413. * calls to the protected or unprotected call to pt_pip_calibrate_ext
  4414. *
  4415. * RETURN:
  4416. * 0 = success
  4417. * !0 = failure
  4418. *
  4419. * PARAMETERS:
  4420. * *dev - pointer to device structure
  4421. * protect - flag to call protected or non-protected
  4422. * *cal_data - pointer to extended calibration data structure
  4423. * *status - pointer to where the command response status is stored
  4424. ******************************************************************************/
  4425. static int _pt_request_pip_calibrate_ext(struct device *dev,
  4426. int protect, struct pt_cal_ext_data *cal_data, u8 *status)
  4427. {
  4428. struct pt_core_data *cd = dev_get_drvdata(dev);
  4429. if (protect)
  4430. return pt_pip_calibrate_ext(cd, cal_data, status);
  4431. return pt_pip_calibrate_ext_(cd, cal_data, status);
  4432. }
  4433. /*******************************************************************************
  4434. * FUNCTION: pt_pip_calibrate_idacs_
  4435. *
  4436. * SUMMARY: Send the PIP Calibrate IDACs command (0x28) to the DUT waiting
  4437. * for the response
  4438. *
  4439. * RETURN:
  4440. * 0 = success
  4441. * !0 = failure
  4442. *
  4443. * PARAMETERS:
  4444. * *cd - pointer to core data
  4445. * mode - sense mode to calibrate (0-5)
  4446. * *status - pointer to where the command response status is stored
  4447. ******************************************************************************/
  4448. static int pt_pip_calibrate_idacs_(struct pt_core_data *cd,
  4449. u8 mode, u8 *status)
  4450. {
  4451. int rc = 0;
  4452. int write_length = 1;
  4453. u8 write_buf[1];
  4454. u8 cmd_offset = 0;
  4455. struct pt_hid_output hid_output = {
  4456. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_IDACS),
  4457. .write_length = write_length,
  4458. .write_buf = write_buf,
  4459. .timeout_ms = PT_PIP1_CMD_CALIBRATE_IDAC_TIMEOUT,
  4460. };
  4461. write_buf[cmd_offset++] = mode;
  4462. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4463. if (rc)
  4464. return rc;
  4465. *status = cd->response_buf[5];
  4466. if (*status)
  4467. return -EINVAL;
  4468. return 0;
  4469. }
  4470. /*******************************************************************************
  4471. * FUNCTION: pt_pip_calibrate_idacs
  4472. *
  4473. * SUMMARY: Protected call to pt_hid_output_calibrate_idacs_ by exclusive
  4474. * access to the DUT
  4475. *
  4476. * RETURN:
  4477. * 0 = success
  4478. * !0 = failure
  4479. *
  4480. * PARAMETERS:
  4481. * *cd - pointer to core data
  4482. * mode - sense mode to calibrate (0-5)
  4483. * *status - pointer to where the command response status is stored
  4484. ******************************************************************************/
  4485. static int pt_pip_calibrate_idacs(struct pt_core_data *cd,
  4486. u8 mode, u8 *status)
  4487. {
  4488. int rc;
  4489. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4490. if (rc < 0) {
  4491. pt_debug(cd->dev, DL_ERROR,
  4492. "%s: fail get exclusive ex=%p own=%p\n",
  4493. __func__, cd->exclusive_dev, cd->dev);
  4494. return rc;
  4495. }
  4496. rc = pt_pip_calibrate_idacs_(cd, mode, status);
  4497. if (release_exclusive(cd, cd->dev) < 0)
  4498. pt_debug(cd->dev, DL_ERROR,
  4499. "%s: fail to release exclusive\n", __func__);
  4500. return rc;
  4501. }
  4502. /*******************************************************************************
  4503. * FUNCTION: _pt_request_pip_calibrate_idacs
  4504. *
  4505. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4506. * calls to the protected or unprotected call to pt_pip_calibrate_idacs
  4507. *
  4508. * RETURN:
  4509. * 0 = success
  4510. * !0 = failure
  4511. *
  4512. * PARAMETERS:
  4513. * *dev - pointer to device structure
  4514. * protect - flag to call protected or non-protected
  4515. * mode - sense mode to calibrate (0-5)
  4516. * *status - pointer to where the command response status is stored
  4517. ******************************************************************************/
  4518. static int _pt_request_pip_calibrate_idacs(struct device *dev,
  4519. int protect, u8 mode, u8 *status)
  4520. {
  4521. struct pt_core_data *cd = dev_get_drvdata(dev);
  4522. if (protect)
  4523. return pt_pip_calibrate_idacs(cd, mode, status);
  4524. return pt_pip_calibrate_idacs_(cd, mode, status);
  4525. }
  4526. /*******************************************************************************
  4527. * FUNCTION: pt_hid_output_initialize_baselines_
  4528. *
  4529. * SUMMARY: Send the PIP "Initialize Baselines" command (0x29) to the DUT
  4530. * waiting for the response.
  4531. *
  4532. * RETURN:
  4533. * 0 = success
  4534. * !0 = failure
  4535. *
  4536. * PARAMETERS:
  4537. * *cd - pointer to core data
  4538. * test_id - bit type flag to allow initialize baseline MUT,BTN,SELG
  4539. * each or together with a single command.
  4540. * *status - pointer to where the command response status is stored
  4541. ******************************************************************************/
  4542. static int pt_hid_output_initialize_baselines_(
  4543. struct pt_core_data *cd, u8 test_id, u8 *status)
  4544. {
  4545. int rc = 0;
  4546. int write_length = 1;
  4547. u8 write_buf[1];
  4548. u8 cmd_offset = 0;
  4549. struct pt_hid_output hid_output = {
  4550. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_INITIALIZE_BASELINES),
  4551. .write_length = write_length,
  4552. .write_buf = write_buf,
  4553. };
  4554. write_buf[cmd_offset++] = test_id;
  4555. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4556. if (rc)
  4557. return rc;
  4558. *status = cd->response_buf[5];
  4559. if (*status)
  4560. return -EINVAL;
  4561. return rc;
  4562. }
  4563. /*******************************************************************************
  4564. * FUNCTION: pt_hid_output_initialize_baselines
  4565. *
  4566. * SUMMARY: Protected call to pt_hid_output_initialize_baselines_ by exclusive
  4567. * access to the DUT
  4568. *
  4569. * RETURN:
  4570. * 0 = success
  4571. * !0 = failure
  4572. *
  4573. * PARAMETERS:
  4574. * *cd - pointer to core data
  4575. * test_id - enumerated ID against which to initialize the baseline
  4576. * *status - pointer to where the command response status is stored
  4577. ******************************************************************************/
  4578. static int pt_hid_output_initialize_baselines(struct pt_core_data *cd,
  4579. u8 test_id, u8 *status)
  4580. {
  4581. int rc;
  4582. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4583. if (rc < 0) {
  4584. pt_debug(cd->dev, DL_ERROR,
  4585. "%s: fail get exclusive ex=%p own=%p\n",
  4586. __func__, cd->exclusive_dev, cd->dev);
  4587. return rc;
  4588. }
  4589. rc = pt_hid_output_initialize_baselines_(cd, test_id, status);
  4590. if (release_exclusive(cd, cd->dev) < 0)
  4591. pt_debug(cd->dev, DL_ERROR,
  4592. "%s: fail to release exclusive\n", __func__);
  4593. return rc;
  4594. }
  4595. /*******************************************************************************
  4596. * FUNCTION: _pt_request_pip_initialize_baselines
  4597. *
  4598. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4599. * calls to the protected or unprotected call to
  4600. * pt_pip_initialize_baselines
  4601. *
  4602. * RETURN:
  4603. * 0 = success
  4604. * !0 = failure
  4605. *
  4606. * PARAMETERS:
  4607. * *dev - pointer to device structure
  4608. * protect - flag to call protected or non-protected
  4609. * test_id - enumerated ID against which to initialize the baseline
  4610. * *status - pointer to where the command response status is stored
  4611. ******************************************************************************/
  4612. static int _pt_request_pip_initialize_baselines(struct device *dev,
  4613. int protect, u8 test_id, u8 *status)
  4614. {
  4615. struct pt_core_data *cd = dev_get_drvdata(dev);
  4616. if (protect)
  4617. return pt_hid_output_initialize_baselines(cd, test_id,
  4618. status);
  4619. return pt_hid_output_initialize_baselines_(cd, test_id, status);
  4620. }
  4621. /*******************************************************************************
  4622. * FUNCTION: pt_hid_output_exec_panel_scan_
  4623. *
  4624. * SUMMARY: Sends the PIP "Execute Panel Scan" (0x2A) to the DUT and waits for
  4625. * the response
  4626. *
  4627. * RETURN:
  4628. * 0 = success
  4629. * !0 = failure
  4630. *
  4631. * PARAMETERS:
  4632. * *cd - pointer to core data
  4633. ******************************************************************************/
  4634. static int pt_hid_output_exec_panel_scan_(struct pt_core_data *cd)
  4635. {
  4636. struct pt_hid_output hid_output = {
  4637. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_EXEC_PANEL_SCAN),
  4638. };
  4639. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  4640. }
  4641. /*******************************************************************************
  4642. * FUNCTION: pt_hid_output_exec_panel_scan
  4643. *
  4644. * SUMMARY: Protected call to pt_hid_output_exec_panel_scan_ by exclusive
  4645. * access to the DUT.
  4646. *
  4647. * RETURN:
  4648. * 0 = success
  4649. * !0 = failure
  4650. *
  4651. * PARAMETERS:
  4652. * *cd - pointer to core data
  4653. ******************************************************************************/
  4654. static int pt_hid_output_exec_panel_scan(struct pt_core_data *cd)
  4655. {
  4656. int rc;
  4657. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4658. if (rc < 0) {
  4659. pt_debug(cd->dev, DL_ERROR,
  4660. "%s: fail get exclusive ex=%p own=%p\n",
  4661. __func__, cd->exclusive_dev, cd->dev);
  4662. return rc;
  4663. }
  4664. rc = pt_hid_output_exec_panel_scan_(cd);
  4665. if (release_exclusive(cd, cd->dev) < 0)
  4666. pt_debug(cd->dev, DL_ERROR,
  4667. "%s: fail to release exclusive\n", __func__);
  4668. return rc;
  4669. }
  4670. /*******************************************************************************
  4671. * FUNCTION: pt_pip2_exec_panel_scan_
  4672. *
  4673. * SUMMARY: Send the PIP2 "Execute Panel Scan" (0x21) to the DUT and waits for
  4674. * the response
  4675. *
  4676. * RETURN:
  4677. * 0 = success
  4678. * !0 = failure
  4679. *
  4680. * PARAMETERS:
  4681. * *cd - pointer to core data
  4682. * scan_type - type of panel scan to perform (PIP2 only)
  4683. ******************************************************************************/
  4684. static int pt_pip2_exec_panel_scan_(struct pt_core_data *cd, u8 scan_type)
  4685. {
  4686. int rc = 0;
  4687. u8 data[2];
  4688. u8 read_buf[10];
  4689. u16 actual_read_len;
  4690. pt_debug(cd->dev, DL_DEBUG, "%s: PIP2 Execute Scan %d\n",
  4691. __func__, scan_type);
  4692. data[0] = scan_type;
  4693. rc = _pt_request_pip2_send_cmd(cd->dev,
  4694. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_EXECUTE_SCAN,
  4695. data, 1, read_buf, &actual_read_len);
  4696. if (rc) {
  4697. pt_debug(cd->dev, DL_ERROR,
  4698. "%s EXECUTE_SCAN command for type %d failed. rc=%d\n",
  4699. __func__, scan_type, rc);
  4700. }
  4701. return rc;
  4702. }
  4703. /*******************************************************************************
  4704. * FUNCTION: pt_pip2_exec_panel_scan
  4705. *
  4706. * SUMMARY: Protected call to pt_pip2_exec_panel_scan_ by exclusive
  4707. * access to the DUT.
  4708. *
  4709. * RETURN:
  4710. * 0 = success
  4711. * !0 = failure
  4712. *
  4713. * PARAMETERS:
  4714. * *cd - pointer to core data
  4715. * scan_type - type of panel scan to perform (PIP2 only)
  4716. ******************************************************************************/
  4717. static int pt_pip2_exec_panel_scan(struct pt_core_data *cd, u8 scan_type)
  4718. {
  4719. int rc;
  4720. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4721. if (rc < 0) {
  4722. pt_debug(cd->dev, DL_ERROR,
  4723. "%s: fail get exclusive ex=%p own=%p\n",
  4724. __func__, cd->exclusive_dev, cd->dev);
  4725. return rc;
  4726. }
  4727. rc = pt_pip2_exec_panel_scan_(cd, scan_type);
  4728. if (release_exclusive(cd, cd->dev) < 0)
  4729. pt_debug(cd->dev, DL_ERROR,
  4730. "%s: fail to release exclusive\n", __func__);
  4731. return rc;
  4732. }
  4733. /*******************************************************************************
  4734. * FUNCTION: _pt_request_pip_exec_panel_scan
  4735. *
  4736. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4737. * calls to the protected or unprotected call to
  4738. * pt_pip2_exec_panel_scan or pt_hid_output_exec_panel_scan
  4739. *
  4740. * RETURN:
  4741. * 0 = success
  4742. * !0 = failure
  4743. *
  4744. * PARAMETERS:
  4745. * *dev - pointer to device structure
  4746. * protect - flag to call protected or non-protected
  4747. * scan_type - type of panel scan to perform (PIP2 only)
  4748. ******************************************************************************/
  4749. static int _pt_request_pip_exec_panel_scan(struct device *dev,
  4750. int protect, u8 scan_type)
  4751. {
  4752. struct pt_core_data *cd = dev_get_drvdata(dev);
  4753. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 12)) {
  4754. if (protect)
  4755. return pt_pip2_exec_panel_scan(cd, scan_type);
  4756. return pt_pip2_exec_panel_scan_(cd, scan_type);
  4757. }
  4758. if (protect)
  4759. return pt_hid_output_exec_panel_scan(cd);
  4760. return pt_hid_output_exec_panel_scan_(cd);
  4761. }
  4762. /*******************************************************************************
  4763. * FUNCTION: pt_hid_output_retrieve_panel_scan_
  4764. *
  4765. * SUMMARY: Sends the PIP "Retrieve Panel Scan" (0x2B) command to the DUT
  4766. * to retrieve the specified data type for a the last successful Execute
  4767. * Panel Scan command.
  4768. *
  4769. * RETURN:
  4770. * 0 = success
  4771. * !0 = failure
  4772. *
  4773. * PARAMETERS:
  4774. * *dev - pointer to device structure
  4775. * protect - flag to call protected or non-protected
  4776. * read_offset - read pointer offset
  4777. * read_count - length of data to read
  4778. * data_id - enumerated test ID to read selftest results from
  4779. * *response - pointer to store the read response status
  4780. * *config - pointer to store config data
  4781. * *actual_read_len - pointer to store data length actually read
  4782. * *read_buf - pointer to the read buffer
  4783. ******************************************************************************/
  4784. static int pt_hid_output_retrieve_panel_scan_(
  4785. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4786. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4787. u8 *read_buf)
  4788. {
  4789. int status;
  4790. u8 read_data_id;
  4791. int rc = 0;
  4792. int write_length = 5;
  4793. u8 write_buf[5];
  4794. u8 cmd_offset = 0;
  4795. u8 data_elem_size;
  4796. int size;
  4797. int data_size;
  4798. struct pt_hid_output hid_output = {
  4799. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RETRIEVE_PANEL_SCAN),
  4800. .write_length = write_length,
  4801. .write_buf = write_buf,
  4802. };
  4803. write_buf[cmd_offset++] = LOW_BYTE(read_offset);
  4804. write_buf[cmd_offset++] = HI_BYTE(read_offset);
  4805. write_buf[cmd_offset++] = LOW_BYTE(read_count);
  4806. write_buf[cmd_offset++] = HI_BYTE(read_count);
  4807. write_buf[cmd_offset++] = data_id;
  4808. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4809. if (rc)
  4810. return rc;
  4811. status = cd->response_buf[5];
  4812. if (status)
  4813. return -EINVAL;
  4814. read_data_id = cd->response_buf[6];
  4815. if (read_data_id != data_id)
  4816. return -EPROTO;
  4817. size = get_unaligned_le16(&cd->response_buf[0]);
  4818. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  4819. *config = cd->response_buf[9];
  4820. data_elem_size = *config & 0x07;
  4821. data_size = *actual_read_len * data_elem_size;
  4822. if (read_buf)
  4823. memcpy(read_buf, &cd->response_buf[10], data_size);
  4824. if (response)
  4825. memcpy(response, cd->response_buf, size);
  4826. return rc;
  4827. }
  4828. /*******************************************************************************
  4829. * FUNCTION: pt_hid_output_retrieve_panel_scan
  4830. *
  4831. * SUMMARY: Protected call to pt_hid_output_retrieve_panel_scan_ by exclusive
  4832. * access to the DUT.
  4833. *
  4834. * RETURN:
  4835. * 0 = success
  4836. * !0 = failure
  4837. *
  4838. * PARAMETERS:
  4839. * *dev - pointer to device structure
  4840. * protect - flag to call protected or non-protected
  4841. * read_offset - read pointer offset
  4842. * read_count - length of data to read
  4843. * data_id - enumerated test ID to read selftest results from
  4844. * *response - pointer to store the read response status
  4845. * *config - pointer to store config data
  4846. * *actual_read_len - pointer to store data length actually read
  4847. * *read_buf - pointer to the read buffer
  4848. ******************************************************************************/
  4849. static int pt_hid_output_retrieve_panel_scan(
  4850. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4851. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4852. u8 *read_buf)
  4853. {
  4854. int rc;
  4855. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4856. if (rc < 0) {
  4857. pt_debug(cd->dev, DL_ERROR,
  4858. "%s: fail get exclusive ex=%p own=%p\n",
  4859. __func__, cd->exclusive_dev, cd->dev);
  4860. return rc;
  4861. }
  4862. rc = pt_hid_output_retrieve_panel_scan_(cd, read_offset,
  4863. read_count, data_id, response, config,
  4864. actual_read_len, read_buf);
  4865. if (release_exclusive(cd, cd->dev) < 0)
  4866. pt_debug(cd->dev, DL_ERROR,
  4867. "%s: fail to release exclusive\n", __func__);
  4868. return rc;
  4869. }
  4870. /*******************************************************************************
  4871. * FUNCTION: _pt_request_pip_retrieve_panel_scan
  4872. *
  4873. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4874. * calls to the protected or unprotected call to
  4875. * pt_hid_output_retrieve_panel_scan
  4876. *
  4877. * RETURN:
  4878. * 0 = success
  4879. * !0 = failure
  4880. *
  4881. * PARAMETERS:
  4882. * *dev - pointer to device structure
  4883. * protect - flag to call protected or non-protected
  4884. * read_offset - read pointer offset
  4885. * read_count - length of data to read
  4886. * data_id - enumerated test ID to read selftest results from
  4887. * *response - pointer to store the read response status
  4888. * *config - pointer to store config data
  4889. * *actual_read_len - pointer to store data length actually read
  4890. * *read_buf - pointer to the read buffer
  4891. ******************************************************************************/
  4892. static int _pt_request_pip_retrieve_panel_scan(struct device *dev,
  4893. int protect, u16 read_offset, u16 read_count, u8 data_id,
  4894. u8 *response, u8 *config, u16 *actual_read_len, u8 *read_buf)
  4895. {
  4896. struct pt_core_data *cd = dev_get_drvdata(dev);
  4897. if (protect)
  4898. return pt_hid_output_retrieve_panel_scan(cd,
  4899. read_offset, read_count, data_id, response,
  4900. config, actual_read_len, read_buf);
  4901. return pt_hid_output_retrieve_panel_scan_(cd,
  4902. read_offset, read_count, data_id, response,
  4903. config, actual_read_len, read_buf);
  4904. }
  4905. /*******************************************************************************
  4906. * FUNCTION: _pt_request_pip_user_cmd
  4907. *
  4908. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4909. * calls to the protected or unprotected call to
  4910. * pt_hid_output_user_cmd
  4911. *
  4912. * RETURN:
  4913. * 0 = success
  4914. * !0 = failure
  4915. *
  4916. * PARAMETERS:
  4917. * *dev - pointer to device structure
  4918. * protect - flag to call protected or non-protected
  4919. * read_len - length of data to read
  4920. * *read_buf - pointer to store read data
  4921. * write_len - length of data to write
  4922. * *write_buf - pointer to buffer to write
  4923. * *actual_read_len - pointer to store data length actually read
  4924. ******************************************************************************/
  4925. static int _pt_request_pip_user_cmd(struct device *dev,
  4926. int protect, u16 read_len, u8 *read_buf, u16 write_len,
  4927. u8 *write_buf, u16 *actual_read_len)
  4928. {
  4929. struct pt_core_data *cd = dev_get_drvdata(dev);
  4930. if (protect)
  4931. return pt_hid_output_user_cmd(cd, read_len, read_buf,
  4932. write_len, write_buf, actual_read_len);
  4933. return pt_hid_output_user_cmd_(cd, read_len, read_buf,
  4934. write_len, write_buf, actual_read_len);
  4935. }
  4936. /*******************************************************************************
  4937. * FUNCTION: pt_hid_output_bl_get_information_
  4938. *
  4939. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x38) command to the
  4940. * DUT to retrieve bootloader version and chip identification information.
  4941. *
  4942. * RETURN:
  4943. * 0 = success
  4944. * !0 = failure
  4945. *
  4946. * PARAMETERS:
  4947. * *cd - pointer to core data
  4948. * *return_data - pointer to store the return data
  4949. *****************************************************************************/
  4950. static int pt_hid_output_bl_get_information_(struct pt_core_data *cd,
  4951. u8 *return_data)
  4952. {
  4953. int rc;
  4954. int data_len;
  4955. struct pt_hid_output hid_output = {
  4956. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_INFO),
  4957. };
  4958. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4959. if (rc)
  4960. return rc;
  4961. data_len = get_unaligned_le16(&cd->input_buf[6]);
  4962. if (!data_len)
  4963. return -EPROTO;
  4964. memcpy(return_data, &cd->response_buf[8], data_len);
  4965. return 0;
  4966. }
  4967. /*******************************************************************************
  4968. * FUNCTION: pt_hid_output_bl_get_information
  4969. *
  4970. * SUMMARY: Protected call to pt_hid_output_bl_get_information_ by exclusive
  4971. * access to the DUT.
  4972. *
  4973. * RETURN:
  4974. * 0 = success
  4975. * !0 = failure
  4976. *
  4977. * PARAMETERS:
  4978. * *cd - pointer to core data
  4979. * *return_data - pointer to store the return data
  4980. ******************************************************************************/
  4981. static int pt_hid_output_bl_get_information(struct pt_core_data *cd,
  4982. u8 *return_data)
  4983. {
  4984. int rc;
  4985. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4986. if (rc < 0) {
  4987. pt_debug(cd->dev, DL_ERROR,
  4988. "%s: fail get exclusive ex=%p own=%p\n",
  4989. __func__, cd->exclusive_dev, cd->dev);
  4990. return rc;
  4991. }
  4992. rc = pt_hid_output_bl_get_information_(cd, return_data);
  4993. if (release_exclusive(cd, cd->dev) < 0)
  4994. pt_debug(cd->dev, DL_ERROR,
  4995. "%s: fail to release exclusive\n", __func__);
  4996. return rc;
  4997. }
  4998. /*******************************************************************************
  4999. * FUNCTION: _pt_request_pip_bl_get_information
  5000. *
  5001. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  5002. * calls to the protected or unprotected call to
  5003. * pt_hid_output_bl_get_information
  5004. *
  5005. * RETURN:
  5006. * 0 = success
  5007. * !0 = failure
  5008. *
  5009. * PARAMETERS:
  5010. * *dev - pointer to device structure
  5011. * protect - flag to call protected or non-protected
  5012. * *return_data - pointer to store bl data
  5013. ******************************************************************************/
  5014. static int _pt_request_pip_bl_get_information(struct device *dev,
  5015. int protect, u8 *return_data)
  5016. {
  5017. struct pt_core_data *cd = dev_get_drvdata(dev);
  5018. if (protect)
  5019. return pt_hid_output_bl_get_information(cd, return_data);
  5020. return pt_hid_output_bl_get_information_(cd, return_data);
  5021. }
  5022. /*******************************************************************************
  5023. * FUNCTION: pt_hid_output_bl_initiate_bl_
  5024. *
  5025. * SUMMARY: Sends the PIP "Initiate Bootload" (0x48) command to the
  5026. * DUT to erases the entire TrueTouch application, Configuration Data block,
  5027. * and Design Data block in flash and enables the host to execute the Program
  5028. * and Verify Row command to bootload the application image and data.
  5029. *
  5030. * RETURN:
  5031. * 0 = success
  5032. * !0 = failure
  5033. *
  5034. * PARAMETERS:
  5035. * *cd - pointer to core data
  5036. * protect - flag to call protected or non-protected
  5037. * key_size - size of key
  5038. * *key_buf - pointer to key data to allow operation
  5039. * row_size - size of the meta data row
  5040. * *metadata_row_buf - pointer to meta data to write
  5041. ******************************************************************************/
  5042. static int pt_hid_output_bl_initiate_bl_(struct pt_core_data *cd,
  5043. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  5044. {
  5045. u16 write_length = key_size + row_size;
  5046. u8 *write_buf;
  5047. int rc = 0;
  5048. struct pt_hid_output hid_output = {
  5049. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_INITIATE_BL),
  5050. .write_length = write_length,
  5051. .timeout_ms = PT_PIP1_CMD_INITIATE_BL_TIMEOUT,
  5052. };
  5053. write_buf = kzalloc(write_length, GFP_KERNEL);
  5054. if (!write_buf)
  5055. return -ENOMEM;
  5056. hid_output.write_buf = write_buf;
  5057. if (key_size)
  5058. memcpy(write_buf, key_buf, key_size);
  5059. if (row_size)
  5060. memcpy(&write_buf[key_size], metadata_row_buf, row_size);
  5061. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5062. kfree(write_buf);
  5063. return rc;
  5064. }
  5065. /*******************************************************************************
  5066. * FUNCTION: pt_hid_output_bl_initiate_bl
  5067. *
  5068. * SUMMARY: Protected call to pt_hid_output_bl_initiate_bl_ by exclusive
  5069. * access to the DUT.
  5070. *
  5071. * RETURN:
  5072. * 0 = success
  5073. * !0 = failure
  5074. *
  5075. * PARAMETERS:
  5076. * *cd - pointer to core data
  5077. * key_size - size of key
  5078. * *key_buf - pointer to key data to allow operation
  5079. * row_size - size of the meta data row
  5080. * *metadata_row_buf - pointer to meta data to write
  5081. ******************************************************************************/
  5082. static int pt_hid_output_bl_initiate_bl(struct pt_core_data *cd,
  5083. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  5084. {
  5085. int rc;
  5086. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5087. if (rc < 0) {
  5088. pt_debug(cd->dev, DL_ERROR,
  5089. "%s: fail get exclusive ex=%p own=%p\n",
  5090. __func__, cd->exclusive_dev, cd->dev);
  5091. return rc;
  5092. }
  5093. rc = pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5094. row_size, metadata_row_buf);
  5095. if (release_exclusive(cd, cd->dev) < 0)
  5096. pt_debug(cd->dev, DL_ERROR,
  5097. "%s: fail to release exclusive\n", __func__);
  5098. return rc;
  5099. }
  5100. /*******************************************************************************
  5101. * FUNCTION: _pt_request_pip_bl_initiate_bl
  5102. *
  5103. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  5104. * calls to the protected or unprotected call to
  5105. * pt_hid_output_bl_initiate_bl
  5106. *
  5107. * RETURN:
  5108. * 0 = success
  5109. * !0 = failure
  5110. *
  5111. * PARAMETERS:
  5112. * *dev - pointer to device structure
  5113. * protect - flag to call protected or non-protected
  5114. * key_size - size of key
  5115. * *key_buf - pointer to key data to allow operation
  5116. * row_size - size of the meta data row
  5117. * *metadata_row_buf - pointer to meta data to write
  5118. ******************************************************************************/
  5119. static int _pt_request_pip_bl_initiate_bl(struct device *dev,
  5120. int protect, u16 key_size, u8 *key_buf, u16 row_size,
  5121. u8 *metadata_row_buf)
  5122. {
  5123. struct pt_core_data *cd = dev_get_drvdata(dev);
  5124. if (protect)
  5125. return pt_hid_output_bl_initiate_bl(cd, key_size, key_buf,
  5126. row_size, metadata_row_buf);
  5127. return pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5128. row_size, metadata_row_buf);
  5129. }
  5130. /*******************************************************************************
  5131. * FUNCTION: pt_hid_output_bl_program_and_verify_
  5132. *
  5133. * SUMMARY: Sends the PIP "Program and Verify" (0x39) command to upload
  5134. * and program a 128-byte row into the flash, and then verifies written data.
  5135. *
  5136. * RETURN:
  5137. * 0 = success
  5138. * !0 = failure
  5139. *
  5140. * PARAMETERS:
  5141. * *cd - pointer to core data
  5142. * data_len - length of data_buf
  5143. * *data_buf - firmware image to program
  5144. ******************************************************************************/
  5145. static int pt_hid_output_bl_program_and_verify_(
  5146. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5147. {
  5148. struct pt_hid_output hid_output = {
  5149. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY),
  5150. .write_length = data_len,
  5151. .write_buf = data_buf,
  5152. .timeout_ms = PT_PIP1_CMD_PROGRAM_AND_VERIFY_TIMEOUT,
  5153. };
  5154. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5155. }
  5156. /*******************************************************************************
  5157. * FUNCTION: pt_hid_output_bl_program_and_verify
  5158. *
  5159. * SUMMARY: Protected call to pt_hid_output_bl_program_and_verify_ by exclusive
  5160. * access to the DUT.
  5161. *
  5162. * RETURN:
  5163. * 0 = success
  5164. * !0 = failure
  5165. *
  5166. * PARAMETERS:
  5167. * *cd - pointer to core data
  5168. * data_len - length of data_buf
  5169. * *data_buf - firmware image to program
  5170. ******************************************************************************/
  5171. static int pt_hid_output_bl_program_and_verify(
  5172. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5173. {
  5174. int rc;
  5175. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5176. if (rc < 0) {
  5177. pt_debug(cd->dev, DL_ERROR,
  5178. "%s: fail get exclusive ex=%p own=%p\n",
  5179. __func__, cd->exclusive_dev, cd->dev);
  5180. return rc;
  5181. }
  5182. rc = pt_hid_output_bl_program_and_verify_(cd, data_len, data_buf);
  5183. if (release_exclusive(cd, cd->dev) < 0)
  5184. pt_debug(cd->dev, DL_ERROR,
  5185. "%s: fail to release exclusive\n", __func__);
  5186. return rc;
  5187. }
  5188. /*******************************************************************************
  5189. * FUNCTION: _pt_request_pip_bl_program_and_verify
  5190. *
  5191. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5192. * to request to have the BL program and verify a FW image
  5193. *
  5194. * RETURN:
  5195. * 0 = success
  5196. * !0 = failure
  5197. *
  5198. * PARAMETERS:
  5199. * *dev - pointer to device structure
  5200. * protect - boolean to determine to call the protected function
  5201. * data_len - length of data_buf
  5202. * *data_buf - firmware image to program
  5203. ******************************************************************************/
  5204. static int _pt_request_pip_bl_program_and_verify(
  5205. struct device *dev, int protect, u16 data_len, u8 *data_buf)
  5206. {
  5207. struct pt_core_data *cd = dev_get_drvdata(dev);
  5208. if (protect)
  5209. return pt_hid_output_bl_program_and_verify(cd, data_len,
  5210. data_buf);
  5211. return pt_hid_output_bl_program_and_verify_(cd, data_len,
  5212. data_buf);
  5213. }
  5214. /*******************************************************************************
  5215. * FUNCTION: pt_hid_output_bl_verify_app_integrity_
  5216. *
  5217. * SUMMARY: Sends the PIP "Verify Application Integrity" (0x31) command to
  5218. * perform a full verification of the application integrity by calculating the
  5219. * CRC of the image in flash and compare it to the expected CRC stored in the
  5220. * Metadata row.
  5221. *
  5222. * RETURN:
  5223. * 0 = success
  5224. * !0 = failure
  5225. *
  5226. * PARAMETERS:
  5227. * *cd - pointer to core data
  5228. * *result - pointer to store result
  5229. ******************************************************************************/
  5230. static int pt_hid_output_bl_verify_app_integrity_(
  5231. struct pt_core_data *cd, u8 *result)
  5232. {
  5233. int rc;
  5234. struct pt_hid_output hid_output = {
  5235. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY),
  5236. };
  5237. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5238. if (rc) {
  5239. *result = 0;
  5240. return rc;
  5241. }
  5242. *result = cd->response_buf[8];
  5243. return 0;
  5244. }
  5245. /*******************************************************************************
  5246. * FUNCTION: pt_hid_output_bl_verify_app_integrity
  5247. *
  5248. * SUMMARY: Protected call to pt_hid_output_bl_verify_app_integrity_ by
  5249. * exclusive access to the DUT.
  5250. *
  5251. * RETURN:
  5252. * 0 = success
  5253. * !0 = failure
  5254. *
  5255. * PARAMETERS:
  5256. * *cd - pointer to core data
  5257. * *result - pointer to store result
  5258. ******************************************************************************/
  5259. static int pt_hid_output_bl_verify_app_integrity(
  5260. struct pt_core_data *cd, u8 *result)
  5261. {
  5262. int rc;
  5263. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5264. if (rc < 0) {
  5265. pt_debug(cd->dev, DL_ERROR,
  5266. "%s: fail get exclusive ex=%p own=%p\n",
  5267. __func__, cd->exclusive_dev, cd->dev);
  5268. return rc;
  5269. }
  5270. rc = pt_hid_output_bl_verify_app_integrity_(cd, result);
  5271. if (release_exclusive(cd, cd->dev) < 0)
  5272. pt_debug(cd->dev, DL_ERROR,
  5273. "%s: fail to release exclusive\n", __func__);
  5274. return rc;
  5275. }
  5276. /*******************************************************************************
  5277. * FUNCTION: _pt_request_pip_bl_verify_app_integrity
  5278. *
  5279. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5280. * to request to have the BL verify the application integrity (PIP1.x only)
  5281. *
  5282. * RETURN:
  5283. * 0 = success
  5284. * !0 = failure
  5285. *
  5286. * PARAMETERS:
  5287. * *dev - pointer to device structure
  5288. * protect - boolean to determine to call the protected function
  5289. * *result - pointer to store result
  5290. ******************************************************************************/
  5291. static int _pt_request_pip_bl_verify_app_integrity(
  5292. struct device *dev, int protect, u8 *result)
  5293. {
  5294. struct pt_core_data *cd = dev_get_drvdata(dev);
  5295. if (protect)
  5296. return pt_hid_output_bl_verify_app_integrity(cd, result);
  5297. return pt_hid_output_bl_verify_app_integrity_(cd, result);
  5298. }
  5299. /*******************************************************************************
  5300. * FUNCTION: pt_hid_output_bl_launch_app_
  5301. *
  5302. * SUMMARY: Sends the PIP "Launch Application" (0x3B) command to launch the
  5303. * application from bootloader (PIP1.x only).
  5304. *
  5305. * RETURN:
  5306. * 0 = success
  5307. * !0 = failure
  5308. *
  5309. * PARAMETERS:
  5310. * *cd - pointer to core data
  5311. ******************************************************************************/
  5312. static int pt_hid_output_bl_launch_app_(struct pt_core_data *cd)
  5313. {
  5314. struct pt_hid_output hid_output = {
  5315. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_LAUNCH_APP),
  5316. .reset_expected = 1,
  5317. };
  5318. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5319. }
  5320. /*******************************************************************************
  5321. * FUNCTION: pt_hid_output_bl_launch_app
  5322. *
  5323. * SUMMARY: Protected call to pt_hid_output_bl_launch_app_ by exclusive access
  5324. * to the DUT.
  5325. *
  5326. * RETURN:
  5327. * 0 = success
  5328. * !0 = failure
  5329. *
  5330. * PARAMETERS:
  5331. * *cd - pointer to core data
  5332. ******************************************************************************/
  5333. static int pt_hid_output_bl_launch_app(struct pt_core_data *cd)
  5334. {
  5335. int rc;
  5336. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5337. if (rc < 0) {
  5338. pt_debug(cd->dev, DL_ERROR,
  5339. "%s: fail get exclusive ex=%p own=%p\n",
  5340. __func__, cd->exclusive_dev, cd->dev);
  5341. return rc;
  5342. }
  5343. rc = pt_hid_output_bl_launch_app_(cd);
  5344. if (release_exclusive(cd, cd->dev) < 0)
  5345. pt_debug(cd->dev, DL_ERROR,
  5346. "%s: fail to release exclusive\n", __func__);
  5347. return rc;
  5348. }
  5349. /*******************************************************************************
  5350. * FUNCTION: _pt_request_pip_launch_app
  5351. *
  5352. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5353. * to request to have the BL launch the application. (PIP1.x only)
  5354. *
  5355. * RETURN:
  5356. * 0 = success
  5357. * !0 = failure
  5358. *
  5359. * PARAMETERS:
  5360. * *dev - pointer to device structure
  5361. * protect - boolean to determine to call the protected function
  5362. ******************************************************************************/
  5363. static int _pt_request_pip_launch_app(struct device *dev,
  5364. int protect)
  5365. {
  5366. struct pt_core_data *cd = dev_get_drvdata(dev);
  5367. if (protect)
  5368. return pt_hid_output_bl_launch_app(cd);
  5369. return pt_hid_output_bl_launch_app_(cd);
  5370. }
  5371. /*******************************************************************************
  5372. * FUNCTION: pt_hid_output_bl_get_panel_id_
  5373. *
  5374. * SUMMARY: Sends the PIP "Get Panel ID" (0x3E) command to return the Panel ID
  5375. * value store in the System Information.
  5376. *
  5377. * RETURN:
  5378. * 0 = success
  5379. * !0 = failure
  5380. *
  5381. * PARAMETERS:
  5382. * *cd - pointer to core data
  5383. * *panel_id - pointer to where the panel ID will be stored
  5384. ******************************************************************************/
  5385. static int pt_hid_output_bl_get_panel_id_(
  5386. struct pt_core_data *cd, u8 *panel_id)
  5387. {
  5388. int rc;
  5389. struct pt_hid_output hid_output = {
  5390. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_PANEL_ID),
  5391. };
  5392. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5393. if (rc == -EPROTO && cd->response_buf[5] == ERROR_COMMAND) {
  5394. pt_debug(cd->dev, DL_ERROR,
  5395. "%s: Get Panel ID command not supported\n",
  5396. __func__);
  5397. *panel_id = PANEL_ID_NOT_ENABLED;
  5398. return 0;
  5399. } else if (rc < 0) {
  5400. pt_debug(cd->dev, DL_ERROR,
  5401. "%s: Error on Get Panel ID command\n", __func__);
  5402. return rc;
  5403. }
  5404. *panel_id = cd->response_buf[8];
  5405. return 0;
  5406. }
  5407. /*******************************************************************************
  5408. * FUNCTION: pt_hid_output_bl_get_panel_id
  5409. *
  5410. * SUMMARY: Protected call to pt_hid_output_bl_get_panel_id_ by exclusive access
  5411. * to the DUT.
  5412. *
  5413. * RETURN:
  5414. * 0 = success
  5415. * !0 = failure
  5416. *
  5417. * PARAMETERS:
  5418. * *cd - pointer to core data
  5419. * *panel_id - pointer to where the panel ID will be stored
  5420. ******************************************************************************/
  5421. static int pt_hid_output_bl_get_panel_id(
  5422. struct pt_core_data *cd, u8 *panel_id)
  5423. {
  5424. int rc;
  5425. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5426. if (rc < 0) {
  5427. pt_debug(cd->dev, DL_ERROR,
  5428. "%s: fail get exclusive ex=%p own=%p\n",
  5429. __func__, cd->exclusive_dev, cd->dev);
  5430. return rc;
  5431. }
  5432. rc = pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5433. if (release_exclusive(cd, cd->dev) < 0)
  5434. pt_debug(cd->dev, DL_ERROR,
  5435. "%s: fail to release exclusive\n", __func__);
  5436. return rc;
  5437. }
  5438. /*******************************************************************************
  5439. * FUNCTION: _pt_request_pip_bl_get_panel_id
  5440. *
  5441. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5442. * to have the BL retrieve the panel ID
  5443. *
  5444. * RETURN:
  5445. * 0 = success
  5446. * !0 = failure
  5447. *
  5448. * PARAMETERS:
  5449. * *dev - pointer to device structure
  5450. * protect - flag to run in protected mode
  5451. * *panel_id - pointer to where the panel ID will be stored
  5452. ******************************************************************************/
  5453. static int _pt_request_pip_bl_get_panel_id(
  5454. struct device *dev, int protect, u8 *panel_id)
  5455. {
  5456. struct pt_core_data *cd = dev_get_drvdata(dev);
  5457. if (protect)
  5458. return pt_hid_output_bl_get_panel_id(cd, panel_id);
  5459. return pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5460. }
  5461. /*******************************************************************************
  5462. * FUNCTION: pt_pip2_get_status_
  5463. *
  5464. * SUMMARY: Sends a PIP2 STATUS command to the DUT and stores the data in
  5465. * cd status_data.
  5466. *
  5467. * RETURN:
  5468. * 0 = success
  5469. * !0 = failure
  5470. *
  5471. * PARAMETERS:
  5472. * *cd - pointer to core data structure
  5473. ******************************************************************************/
  5474. static int pt_pip2_get_status_(struct pt_core_data *cd)
  5475. {
  5476. u16 actual_read_len;
  5477. u8 read_buf[12];
  5478. u8 status, boot;
  5479. int rc = 0;
  5480. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  5481. PIP2_CMD_ID_STATUS, NULL, 0, read_buf, &actual_read_len);
  5482. if (rc) {
  5483. pt_debug(cd->dev, DL_ERROR, "%s: PIP2 STATUS command rc = %d\n",
  5484. __func__, rc);
  5485. return rc;
  5486. }
  5487. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, actual_read_len,
  5488. "PIP2 STATUS");
  5489. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5490. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  5491. cd->dut_status.fw_system_mode =
  5492. read_buf[PIP2_RESP_BODY_OFFSET + 1];
  5493. if (status == PIP2_RSP_ERR_NONE && boot == 0x00)
  5494. cd->dut_status.mode = PT_MODE_BOOTLOADER;
  5495. else if (status == PIP2_RSP_ERR_NONE && boot == 0x01) {
  5496. cd->dut_status.mode = PT_MODE_OPERATIONAL;
  5497. cd->dut_status.protocol_mode =
  5498. read_buf[PIP2_RESP_BODY_OFFSET + 2];
  5499. } else
  5500. cd->dut_status.mode = PT_MODE_UNKNOWN;
  5501. return rc;
  5502. }
  5503. /*******************************************************************************
  5504. * FUNCTION: pt_pip2_get_mode_sysmode_
  5505. *
  5506. * SUMMARY: Determine the current mode and system mode of the DUT by use of the
  5507. * PIP2 STATUS command.
  5508. *
  5509. * RETURN:
  5510. * 0 = success
  5511. * !0 = failure
  5512. *
  5513. * PARAMETERS:
  5514. * *cd - pointer to core data structure
  5515. * *mode - pointer to store the retrieved mode
  5516. * *sys_mode - pointer to store the FW system mode
  5517. ******************************************************************************/
  5518. static int pt_pip2_get_mode_sysmode_(struct pt_core_data *cd,
  5519. u8 *mode, u8 *sys_mode)
  5520. {
  5521. int rc = 0;
  5522. rc = pt_pip2_get_status_(cd);
  5523. if (!rc) {
  5524. if (sys_mode)
  5525. *sys_mode = cd->dut_status.fw_system_mode;
  5526. if (mode)
  5527. *mode = cd->dut_status.mode;
  5528. } else {
  5529. if (mode)
  5530. *mode = PT_MODE_UNKNOWN;
  5531. if (sys_mode)
  5532. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5533. pt_debug(cd->dev, DL_WARN,
  5534. "%s: Mode and sys_mode could not be determined\n",
  5535. __func__);
  5536. }
  5537. return rc;
  5538. }
  5539. /*******************************************************************************
  5540. * FUNCTION: pt_pip2_get_mode_sysmode
  5541. *
  5542. * SUMMARY: Protected call to pt_pip2_get_mode_sysmode_ by exclusive
  5543. * access to the DUT.
  5544. *
  5545. * RETURN:
  5546. * 0 = success
  5547. * !0 = failure
  5548. *
  5549. * PARAMETERS:
  5550. * *cd - pointer to core data structure
  5551. * *mode - pointer to store the retrieved mode
  5552. * *sys_mode - pointer to store the FW system mode
  5553. ******************************************************************************/
  5554. static int pt_pip2_get_mode_sysmode(struct pt_core_data *cd,
  5555. u8 *mode, u8 *sys_mode)
  5556. {
  5557. int rc;
  5558. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5559. if (rc < 0) {
  5560. pt_debug(cd->dev, DL_ERROR,
  5561. "%s: fail get exclusive ex=%p own=%p\n",
  5562. __func__, cd->exclusive_dev, cd->dev);
  5563. return rc;
  5564. }
  5565. rc = pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5566. if (release_exclusive(cd, cd->dev) < 0)
  5567. pt_debug(cd->dev, DL_ERROR,
  5568. "%s: fail to release exclusive\n", __func__);
  5569. return rc;
  5570. }
  5571. /*******************************************************************************
  5572. * FUNCTION: _pt_request_pip2_get_mode_sysmode
  5573. *
  5574. * SUMMARY: Function pointer included in core_commands struct for external
  5575. * calls to the protected or unprotected call to
  5576. * pt_pip2_get_mode_sysmode
  5577. *
  5578. * RETURN:
  5579. * 0 = success
  5580. * !0 = failure
  5581. *
  5582. * PARAMETERS:
  5583. * *dev - pointer to device structure
  5584. * *mode - pointer to store the retrieved mode
  5585. * *sys_mode - pointer to store the FW system mode
  5586. ******************************************************************************/
  5587. static int _pt_request_pip2_get_mode_sysmode(struct device *dev,
  5588. int protect, u8 *mode, u8 *sys_mode)
  5589. {
  5590. struct pt_core_data *cd = dev_get_drvdata(dev);
  5591. if (protect)
  5592. return pt_pip2_get_mode_sysmode(cd, mode, sys_mode);
  5593. return pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5594. }
  5595. /*******************************************************************************
  5596. * FUNCTION: _pt_poll_for_fw_exit_boot_mode
  5597. *
  5598. * SUMMARY: Verify and or poll for the FW to exit BOOT mode. During the FW BOOT
  5599. * mode only the following PIP commands will be serviced, any other PIP
  5600. * command the FW will respond with an "Invalid PIP Command" response.
  5601. * - Get HID Descriptor (Register 0x0001, no command ID)
  5602. * - Reset (Register 0x0005, RESET HID request)
  5603. * - Ping (Register 0x0004, Command ID 0x00
  5604. * - Get System Information (Register 0x0004, Command ID 0x02)
  5605. * - PIP2 Status (Register 0x0101, Command ID 0x01)
  5606. * - PIP2 Version (Register 0x0101, Command ID 0x07)
  5607. * This function will loop on the results of the STATUS command until
  5608. * the FW reports it is out of BOOT mode.
  5609. *
  5610. * NOTE:
  5611. * - This function will update cd->fw_system_mode
  5612. * - The STATUS cmd only supports this functionality for PIP 1.11+
  5613. *
  5614. * RETURN:
  5615. * 0 = success
  5616. * !0 = failure
  5617. *
  5618. * PARAMETERS:
  5619. * *cd - pointer to core data
  5620. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5621. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5622. ******************************************************************************/
  5623. static int _pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5624. int *actual_wait)
  5625. {
  5626. int loop = 0;
  5627. u8 sys_mode = cd->fw_system_mode;
  5628. u8 pause = 10; /* in ms */
  5629. int rc = 0;
  5630. int max_loop = (timeout / pause) + 1; /* Add 1 due to int math */
  5631. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5632. /*
  5633. * For PIP <1.11, no support for polling wait so do a hard
  5634. * coded wait and assume the FW is out of BOOT. Added 1 to
  5635. * timeout to make it clear in kmsg if non polling was done.
  5636. */
  5637. *actual_wait = PT_FW_EXIT_BOOT_MODE_TIMEOUT + 1;
  5638. pt_debug(cd->dev, DL_ERROR,
  5639. "%s: PIP %d.%d no support for ext STATUS, sleep %d\n",
  5640. __func__,
  5641. cd->sysinfo.ttdata.pip_ver_major,
  5642. cd->sysinfo.ttdata.pip_ver_minor, *actual_wait);
  5643. msleep(*actual_wait);
  5644. sys_mode = FW_SYS_MODE_SCANNING;
  5645. }
  5646. if (sys_mode == FW_SYS_MODE_BOOT) {
  5647. while (!rc && loop <= max_loop &&
  5648. (sys_mode == FW_SYS_MODE_BOOT)) {
  5649. loop++;
  5650. usleep_range(9000, pause * 1000);
  5651. rc = pt_pip2_get_mode_sysmode_(cd, NULL, &sys_mode);
  5652. pt_debug(cd->dev, DL_DEBUG,
  5653. "%s: FW in BOOT mode-sleep %dms, sys_mode=%d\n",
  5654. __func__, loop * pause, sys_mode);
  5655. }
  5656. *actual_wait = (int)(loop * pause);
  5657. pt_debug(cd->dev, DL_WARN,
  5658. "%s: FW exited BOOT mode in %dms, sys_mode=%d\n",
  5659. __func__, *actual_wait, sys_mode);
  5660. if (rc)
  5661. sys_mode = FW_SYS_MODE_UNDEFINED;
  5662. else if (sys_mode == FW_SYS_MODE_BOOT ||
  5663. sys_mode == FW_SYS_MODE_UNDEFINED)
  5664. rc = -EBUSY;
  5665. }
  5666. mutex_lock(&cd->system_lock);
  5667. cd->fw_system_mode = sys_mode;
  5668. mutex_unlock(&cd->system_lock);
  5669. return rc;
  5670. }
  5671. /*******************************************************************************
  5672. * FUNCTION: pt_poll_for_fw_exit_boot_mode
  5673. *
  5674. * SUMMARY: Protected call to _pt_poll_for_fw_exit_boot_mode by exclusive
  5675. * access to the DUT.
  5676. *
  5677. * RETURN:
  5678. * 0 = success
  5679. * !0 = failure
  5680. *
  5681. * PARAMETERS:
  5682. * *cd - pointer to core data
  5683. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5684. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5685. ******************************************************************************/
  5686. static int pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5687. int *actual_wait)
  5688. {
  5689. int rc;
  5690. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5691. if (rc < 0) {
  5692. pt_debug(cd->dev, DL_ERROR,
  5693. "%s: fail get exclusive ex=%p own=%p\n",
  5694. __func__, cd->exclusive_dev, cd->dev);
  5695. return rc;
  5696. }
  5697. rc = _pt_poll_for_fw_exit_boot_mode(cd, timeout, actual_wait);
  5698. if (release_exclusive(cd, cd->dev) < 0)
  5699. pt_debug(cd->dev, DL_ERROR,
  5700. "%s: fail to release exclusive\n", __func__);
  5701. return rc;
  5702. }
  5703. /*******************************************************************************
  5704. * FUNCTION: _pt_get_fw_sys_mode
  5705. *
  5706. * SUMMARY: Determine the FW system mode. For PIP 1.11+ the
  5707. * PIP2 STATUS command is used to directly query the FW system mode. For older
  5708. * PIP versions, there is no direct PIP commamnd that will directly provide this
  5709. * information but any PIP command above 0x1F requires scanning to be disabled
  5710. * before it will be operational. If scanning was not disabled before sending
  5711. * these PIP commands the FW will respond with a 6 byte error response. So to
  5712. * safely determine the scanning state, a PIP message that will not affect the
  5713. * operation of the FW was chosen:
  5714. * "Verify Data Block CRC (ID 0x20)" is sent and if a 6 byte error code is
  5715. * received scanning is active.
  5716. *
  5717. * RETURN:
  5718. * 0 = success
  5719. * !0 = failure
  5720. *
  5721. * PARAMETERS:
  5722. * *cd - pointer to core data
  5723. * *sys_mode - pointer to FW System mode
  5724. * *mode - pointer to mode (BL/FW)
  5725. ******************************************************************************/
  5726. static int _pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5727. {
  5728. int write_length = 1;
  5729. int report_length;
  5730. int rc = 0;
  5731. u8 tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5732. u8 tmp_mode = PT_MODE_UNKNOWN;
  5733. u8 param[1] = { PT_TCH_PARM_EBID };
  5734. struct pt_hid_output hid_output = {
  5735. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  5736. .write_length = write_length,
  5737. .write_buf = param,
  5738. .novalidate = true,
  5739. };
  5740. /* AFter PIP1.11 the preferred method is using STATUS cmd */
  5741. if (IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5742. rc = pt_pip2_get_mode_sysmode_(cd, &tmp_mode, &tmp_sys_mode);
  5743. pt_debug(cd->dev, DL_DEBUG, "%s: tmp_sys_mode=%d tmp_mode=%d\n",
  5744. __func__, tmp_sys_mode, tmp_mode);
  5745. if (!rc) {
  5746. if (tmp_mode != PT_MODE_OPERATIONAL)
  5747. tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5748. }
  5749. goto exit;
  5750. }
  5751. /* Older systems use PIP1 CONFIG_BLOCK_CRC to best determine sys_mode */
  5752. if (cd->mode != PT_MODE_OPERATIONAL) {
  5753. tmp_mode = cd->mode;
  5754. goto exit;
  5755. }
  5756. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5757. if (rc)
  5758. goto exit;
  5759. report_length = (cd->response_buf[1] << 8) | (cd->response_buf[0]);
  5760. if ((report_length == 0x06) &&
  5761. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) == 0x00) &&
  5762. (cd->response_buf[5] == PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5763. tmp_mode = PIP2_STATUS_APP_EXEC;
  5764. tmp_sys_mode = FW_SYS_MODE_SCANNING;
  5765. } else if ((report_length == 0x0A) &&
  5766. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) ==
  5767. PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5768. tmp_mode = PIP2_STATUS_APP_EXEC;
  5769. tmp_sys_mode = FW_SYS_MODE_TEST;
  5770. }
  5771. exit:
  5772. if (mode)
  5773. *mode = tmp_mode;
  5774. if (sys_mode)
  5775. *sys_mode = tmp_sys_mode;
  5776. pt_debug(cd->dev, DL_INFO, "%s: Return Mode=%d sys_mode=%d\n",
  5777. __func__, tmp_mode, tmp_sys_mode);
  5778. return rc;
  5779. }
  5780. /*******************************************************************************
  5781. * FUNCTION: pt_get_fw_sys_mode
  5782. *
  5783. * SUMMARY: Protected call to _pt_get_fw_sys_mode() to determine if FW scanning
  5784. * is active or not.
  5785. *
  5786. * RETURN:
  5787. * 0 = success
  5788. * !0 = failure
  5789. *
  5790. * PARAMETERS:
  5791. * *cd - pointer to core data
  5792. * *sys_mode - pointer to fw system mode
  5793. * *mode - pointer to mode
  5794. ******************************************************************************/
  5795. static int pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5796. {
  5797. int rc;
  5798. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5799. if (rc < 0) {
  5800. pt_debug(cd->dev, DL_ERROR,
  5801. "%s: fail get exclusive ex=%p own=%p\n",
  5802. __func__, cd->exclusive_dev, cd->dev);
  5803. return rc;
  5804. }
  5805. rc = _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5806. if (release_exclusive(cd, cd->dev) < 0)
  5807. pt_debug(cd->dev, DL_ERROR,
  5808. "%s: fail to release exclusive\n", __func__);
  5809. return rc;
  5810. }
  5811. /*******************************************************************************
  5812. * FUNCTION: _pt_request_get_fw_sys_mode
  5813. *
  5814. * SUMMARY: Function pointer included in core_cmds to allow other modules
  5815. * to request to get scan state
  5816. *
  5817. * RETURN:
  5818. * 0 = success
  5819. * !0 = failure
  5820. *
  5821. * PARAMETERS:
  5822. * *dev - pointer to device structure
  5823. * protect - flag to call protected or non-protected
  5824. * *sys_mode - pointer to FW system mode
  5825. * *mode - pointer to mode
  5826. ******************************************************************************/
  5827. static int _pt_request_get_fw_sys_mode(struct device *dev, int protect,
  5828. u8 *sys_mode, u8 *mode)
  5829. {
  5830. struct pt_core_data *cd = dev_get_drvdata(dev);
  5831. if (protect)
  5832. return pt_get_fw_sys_mode(cd, sys_mode, mode);
  5833. return _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5834. }
  5835. /* Default hid descriptor to provide basic register map */
  5836. const struct pt_hid_desc hid_desc_default = {
  5837. 230, /* hid_desc_len */
  5838. HID_APP_REPORT_ID, /* packet_id */
  5839. 0x00, /* reserved_byte */
  5840. 0x0100, /* bcd_version */
  5841. 0x00EC, /* report_desc_len */
  5842. 0x0002, /* report_desc_register */
  5843. 0x0003, /* input_register */
  5844. 0x00FE, /* max_input_len */
  5845. 0x0004, /* output_register */
  5846. 0x00FE, /* max_output_len */
  5847. 0x0005, /* command_register */
  5848. 0x0006, /* data_register */
  5849. 0x04B4, /* vendor_id */
  5850. 0xC101, /* product_id */
  5851. 0x0100, /* version_id */
  5852. {0x00, 0x00, 0x00, 0x00} /* reserved[4] */
  5853. };
  5854. /*******************************************************************************
  5855. * FUNCTION: pt_init_hid_descriptor
  5856. *
  5857. * SUMMARY: Setup default values for HID descriptor structure
  5858. *
  5859. *
  5860. * PARAMETERS:
  5861. * *desc - pointer to the HID descriptor data read back from DUT
  5862. ******************************************************************************/
  5863. static inline void pt_init_hid_descriptor(struct pt_hid_desc *desc)
  5864. {
  5865. memcpy(desc, &hid_desc_default, sizeof(hid_desc_default));
  5866. }
  5867. /*******************************************************************************
  5868. * FUNCTION: pt_get_hid_descriptor_
  5869. *
  5870. * SUMMARY: Send the get HID descriptor command to the DUT and load the response
  5871. * into the HID descriptor structure
  5872. *
  5873. * RETURN:
  5874. * 0 = success
  5875. * !0 = failure
  5876. *
  5877. * PARAMETERS:
  5878. * *cd - pointer to core data
  5879. * *desc - pointer to the HID descriptor data read back from DUT
  5880. ******************************************************************************/
  5881. static int pt_get_hid_descriptor_(struct pt_core_data *cd,
  5882. struct pt_hid_desc *desc)
  5883. {
  5884. struct device *dev = cd->dev;
  5885. struct pt_hid_cmd hid_cmd = {
  5886. .descriptor = cd->hid_core.hid_desc_register,
  5887. .read_length = 0,
  5888. };
  5889. int rc = 0;
  5890. u16 hid_len;
  5891. /*
  5892. * During startup the HID descriptor is required for all future
  5893. * processing. If IRQ is already asserted due to an early touch report
  5894. * the report must be cleared before sending command.
  5895. */
  5896. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5897. rc = pt_hid_send_command(cd, &hid_cmd);
  5898. if (rc) {
  5899. pt_debug(dev, DL_ERROR,
  5900. "%s: failed to get HID descriptor, rc=%d\n",
  5901. __func__, rc);
  5902. goto exit;
  5903. }
  5904. hid_len = get_unaligned_le16(&cd->response_buf[0]);
  5905. pt_pr_buf(cd->dev, DL_DEBUG, cd->response_buf, hid_len, "<<< HIDDesc");
  5906. /*
  5907. * HID doesn't have packet_id and reserve_byte in struct struct
  5908. * pt_hid_desc and assign fixed value to packet_id.
  5909. */
  5910. if (cd->dut_status.protocol_mode == PT_PROTOCOL_MODE_HID) {
  5911. if ((hid_len + 2) != sizeof(*desc)) {
  5912. pt_debug(dev, DL_ERROR, "%s: Unsupported HID length: %X\n",
  5913. __func__, hid_len);
  5914. rc = -ENODEV;
  5915. goto exit;
  5916. }
  5917. /* Copy length filed */
  5918. memcpy((u8 *)desc, &cd->response_buf[0], 2);
  5919. /* Assign fixed value to packet_id */
  5920. desc->packet_id = HID_APP_REPORT_ID;
  5921. /* Skip 2 bytes in desc and Copy to left fields */
  5922. memcpy((u8 *)desc + 4, &cd->response_buf[2], hid_len - 2);
  5923. } else {
  5924. if (hid_len != sizeof(*desc)) {
  5925. pt_debug(dev, DL_ERROR, "%s: Unsupported HID length: %X\n",
  5926. __func__, hid_len);
  5927. rc = -ENODEV;
  5928. goto exit;
  5929. }
  5930. /* Load the HID descriptor including all registers */
  5931. memcpy((u8 *)desc, cd->response_buf, hid_len);
  5932. }
  5933. /* Check HID descriptor length and version */
  5934. pt_debug(dev, DL_INFO, "%s: HID len:%X HID ver:%X\n", __func__,
  5935. le16_to_cpu(desc->hid_desc_len),
  5936. le16_to_cpu(desc->bcd_version));
  5937. cd->hid_core.hid_report_desc_len =
  5938. le16_to_cpu(desc->report_desc_len);
  5939. pt_debug(dev, DL_INFO, "%s: report descriptor len:%d\n", __func__,
  5940. cd->hid_core.hid_report_desc_len);
  5941. if (le16_to_cpu(desc->bcd_version) != HID_VERSION) {
  5942. pt_debug(dev, DL_ERROR, "%s: Unsupported HID version\n",
  5943. __func__);
  5944. rc = -ENODEV;
  5945. goto exit;
  5946. }
  5947. #ifdef TTHE_TUNER_SUPPORT
  5948. tthe_print(cd, cd->response_buf, hid_len, "HIDDesc=");
  5949. #endif
  5950. exit:
  5951. return rc;
  5952. }
  5953. /*******************************************************************************
  5954. * FUNCTION: pt_get_hid_descriptor
  5955. *
  5956. * SUMMARY: Protected call to pt_get_hid_descriptor_()
  5957. *
  5958. * RETURN:
  5959. * 0 = success
  5960. * !0 = failure
  5961. *
  5962. * PARAMETERS:
  5963. * *cd - pointer to core data
  5964. * *desc - pointer to the HID descriptor data read back from DUT
  5965. ******************************************************************************/
  5966. static int pt_get_hid_descriptor(struct pt_core_data *cd,
  5967. struct pt_hid_desc *desc)
  5968. {
  5969. int rc;
  5970. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5971. if (rc < 0) {
  5972. pt_debug(cd->dev, DL_ERROR,
  5973. "%s: fail get exclusive ex=%p own=%p\n",
  5974. __func__, cd->exclusive_dev, cd->dev);
  5975. return rc;
  5976. }
  5977. rc = pt_get_hid_descriptor_(cd, desc);
  5978. if (release_exclusive(cd, cd->dev) < 0)
  5979. pt_debug(cd->dev, DL_ERROR,
  5980. "%s: fail to release exclusive\n", __func__);
  5981. return rc;
  5982. }
  5983. /*******************************************************************************
  5984. * FUNCTION: pt_pip2_get_version_
  5985. *
  5986. * SUMMARY: Sends a PIP2 VERSION command to the DUT and stores the data in
  5987. * cd-ttdata
  5988. *
  5989. * RETURN:
  5990. * 0 = success
  5991. * !0 = failure
  5992. *
  5993. * PARAMETERS:
  5994. * *cd - pointer to core data
  5995. ******************************************************************************/
  5996. static int pt_pip2_get_version_(struct pt_core_data *cd)
  5997. {
  5998. int rc = 0;
  5999. int status;
  6000. u8 read_buf[64];
  6001. u16 actual_read_len;
  6002. rc = _pt_request_pip2_send_cmd(cd->dev,
  6003. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  6004. NULL, 0, read_buf, &actual_read_len);
  6005. if (rc) {
  6006. pt_debug(cd->dev, DL_ERROR,
  6007. "%s: Error Sending PIP2 VERSION Cmd rc=%d\n",
  6008. __func__, rc);
  6009. return rc;
  6010. }
  6011. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  6012. if (status == 0) {
  6013. /* Parse the PIP2 VERSION response into ttdata */
  6014. pt_pip2_ver_load_ttdata(cd, actual_read_len);
  6015. } else {
  6016. pt_debug(cd->dev, DL_ERROR,
  6017. "%s: Error in PIP2 VERSION Cmd status=%d\n",
  6018. __func__, status);
  6019. return status;
  6020. }
  6021. return rc;
  6022. }
  6023. /*******************************************************************************
  6024. * FUNCTION: pt_pip2_get_version
  6025. *
  6026. * SUMMARY: Protected call to pt_pip2_get_version_ by exclusive
  6027. * access to the DUT.
  6028. *
  6029. * RETURN:
  6030. * 0 = success
  6031. * !0 = failure
  6032. *
  6033. * PARAMETERS:
  6034. * *cd - pointer to core data structure
  6035. ******************************************************************************/
  6036. static int pt_pip2_get_version(struct pt_core_data *cd)
  6037. {
  6038. int rc;
  6039. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6040. if (rc < 0) {
  6041. pt_debug(cd->dev, DL_ERROR,
  6042. "%s: fail get exclusive ex=%p own=%p\n",
  6043. __func__, cd->exclusive_dev, cd->dev);
  6044. return rc;
  6045. }
  6046. rc = pt_pip2_get_version_(cd);
  6047. if (release_exclusive(cd, cd->dev) < 0)
  6048. pt_debug(cd->dev, DL_ERROR,
  6049. "%s: fail to release exclusive\n", __func__);
  6050. return rc;
  6051. }
  6052. /*******************************************************************************
  6053. * FUNCTION: _pt_request_active_pip_protocol
  6054. *
  6055. * SUMMARY: Get active PIP protocol version using the PIP2 version command.
  6056. * Function will return PIP version of BL or application based on
  6057. * when it's called.
  6058. *
  6059. * RETURN:
  6060. * 0 = success
  6061. * !0 = failure
  6062. *
  6063. * PARAMETERS:
  6064. * *dev - pointer to device structure
  6065. * protect - flag to run in protected mode
  6066. * *pip_version_major - pointer to store PIP major version
  6067. * *pip_version_minor - pointer to store PIP minor version
  6068. ******************************************************************************/
  6069. int _pt_request_active_pip_protocol(struct device *dev, int protect,
  6070. u8 *pip_version_major, u8 *pip_version_minor)
  6071. {
  6072. struct pt_core_data *cd = dev_get_drvdata(dev);
  6073. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6074. int rc = 0;
  6075. struct pt_hid_output sys_info = {
  6076. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  6077. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  6078. };
  6079. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  6080. /* Skip PIP2 command if DUT generation is confirmed */
  6081. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  6082. goto skip_pip2_command;
  6083. rc = pt_pip2_get_version_(cd);
  6084. if (!rc) {
  6085. *pip_version_major = ttdata->pip_ver_major;
  6086. *pip_version_minor = ttdata->pip_ver_minor;
  6087. pt_debug(dev, DL_INFO,
  6088. "%s: pip_version = %d.%d\n", __func__,
  6089. *pip_version_major, *pip_version_minor);
  6090. } else {
  6091. /*
  6092. * Legacy products do not support the pip2 protocol to get
  6093. * pip version. However, they do support the "get sysinfo"
  6094. * command to get pip version from FW, but the bootloader
  6095. * does not support it. This function will try "get sysinfo"
  6096. * command if the pip2 command failed but this cmd could also
  6097. * fail if DUT is stuck in bootloader mode.
  6098. */
  6099. pt_debug(dev, DL_INFO,
  6100. "%s: PIP2 no response rc = %d, try legacy cmd\n",
  6101. __func__, rc);
  6102. skip_pip2_command:
  6103. rc = pt_pip1_send_output_and_wait_(cd, &sys_info);
  6104. if (!rc) {
  6105. *pip_version_minor =
  6106. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET + 1];
  6107. *pip_version_major =
  6108. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  6109. pt_debug(dev, DL_INFO,
  6110. "%s: pip_version = %d.%d\n", __func__,
  6111. *pip_version_major, *pip_version_minor);
  6112. } else {
  6113. *pip_version_major = 0;
  6114. *pip_version_minor = 0;
  6115. pt_debug(dev, DL_ERROR,
  6116. "%s: pip_version Not Detected\n", __func__);
  6117. }
  6118. }
  6119. return rc;
  6120. }
  6121. EXPORT_SYMBOL_GPL(_pt_request_active_pip_protocol);
  6122. /*******************************************************************************
  6123. * FUNCTION: _pt_detect_dut_generation
  6124. *
  6125. * SUMMARY: Determine the generation of device that we are communicating with:
  6126. * DUT_PIP1_ONLY (Gen5 or Gen6)
  6127. * DUT_PIP2_CAPABLE (TC33xx or TT7xxx)
  6128. * The HID_DESC command is supported in Gen5/6 BL and FW as well as
  6129. * TT/TC FW. The packet ID in the descriptor, however, is unique when
  6130. * coming form the BL or the FW:
  6131. * Packet_ID in BL = HID_BL_REPORT_ID (0xFF)
  6132. * Packet_ID in FW = HID_APP_REPORT_ID (0xF7)
  6133. * This function will return a modified status if it detects the DUT
  6134. * is in the BL. In the case of a Gen5/6 BL, which also sends out a FW
  6135. * reset sentinel, the status is "corrected" from a FW to BL sentinel.
  6136. *
  6137. * RETURN:
  6138. * 0 = success
  6139. * !0 = failure
  6140. *
  6141. * PARAMETERS:
  6142. * *dev - pointer to device structure
  6143. * *status - pointer to status bitmask
  6144. * *dut_gen - pointer to store the dut_generation
  6145. * *mode - pointer to store the PT_MODE
  6146. ******************************************************************************/
  6147. static int _pt_detect_dut_generation(struct device *dev,
  6148. u32 *status, u8 *dut_gen, enum pt_mode *mode)
  6149. {
  6150. int rc = 0;
  6151. u8 dut_gen_tmp = DUT_UNKNOWN;
  6152. u8 mode_tmp = PT_MODE_UNKNOWN;
  6153. u8 attempt = 1;
  6154. u32 status_tmp = STARTUP_STATUS_START;
  6155. struct pt_core_data *cd = dev_get_drvdata(dev);
  6156. struct pt_hid_desc hid_desc;
  6157. /* protocol_mode must be known before dut gen */
  6158. rc = pt_pip2_get_status_(cd);
  6159. if (rc) {
  6160. /* PIP prot assumed if error */
  6161. cd->dut_status.protocol_mode = PT_PROTOCOL_MODE_PIP;
  6162. }
  6163. memset(&hid_desc, 0, sizeof(hid_desc));
  6164. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6165. while (rc && attempt < 3) {
  6166. attempt++;
  6167. usleep_range(2000, 5000);
  6168. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6169. }
  6170. if (!rc && hid_desc.packet_id == HID_BL_REPORT_ID) {
  6171. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 BL */
  6172. mode_tmp = PT_MODE_BOOTLOADER;
  6173. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6174. } else if (!rc && hid_desc.packet_id == HID_APP_REPORT_ID) {
  6175. rc = pt_pip2_get_version_(cd);
  6176. if (!rc)
  6177. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC FW */
  6178. else
  6179. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 FW */
  6180. mode_tmp = PT_MODE_OPERATIONAL;
  6181. status_tmp = STARTUP_STATUS_FW_RESET_SENTINEL;
  6182. rc = 0; /* To return success instead of error code */
  6183. } else if (rc) {
  6184. rc = pt_pip2_get_version_(cd);
  6185. if (!rc) {
  6186. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC BL */
  6187. mode_tmp = PT_MODE_BOOTLOADER;
  6188. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6189. }
  6190. }
  6191. mutex_lock(&cd->system_lock);
  6192. if (dut_gen)
  6193. *dut_gen = dut_gen_tmp;
  6194. if (mode)
  6195. *mode = mode_tmp;
  6196. if (status)
  6197. *status = status_tmp;
  6198. mutex_unlock(&cd->system_lock);
  6199. #ifdef TTDL_DIAGNOSTICS
  6200. pt_debug(cd->dev, DL_INFO, "%s: Generation=%d Mode=%d\n",
  6201. __func__, dut_gen_tmp, mode_tmp);
  6202. #endif /* TTDL_DIAGNOSTICS */
  6203. return rc;
  6204. }
  6205. /*******************************************************************************
  6206. * FUNCTION: _pt_request_dut_generation
  6207. *
  6208. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6209. * to get current dut generation.
  6210. *
  6211. * NOTE: This function WILL NOT try to determine dut generation.
  6212. *
  6213. * RETURN:
  6214. * The current dut generation.
  6215. *
  6216. * PARAMETERS:
  6217. * *dev - pointer to device structure
  6218. ******************************************************************************/
  6219. static int _pt_request_dut_generation(struct device *dev)
  6220. {
  6221. struct pt_core_data *cd = dev_get_drvdata(dev);
  6222. return cd->active_dut_generation;
  6223. }
  6224. #define HW_VERSION_LEN_MAX 13
  6225. /*******************************************************************************
  6226. * FUNCTION: _legacy_generate_hw_version
  6227. *
  6228. * SUMMARY: Format chip information from struct ttdata (maintained by PIP1
  6229. * SYSINFO command) or struct bl_info (maintained by PIP1 BL INFORMATION
  6230. * command) to the hw_version.
  6231. *
  6232. * RETURN:
  6233. * 0 = success
  6234. * !0 = failure
  6235. *
  6236. * PARAMETERS:
  6237. * *cd - pointer to core data
  6238. * *hw_version - pointer to store the hardware version
  6239. ******************************************************************************/
  6240. static int _legacy_generate_hw_version(struct pt_core_data *cd,
  6241. char *hw_version)
  6242. {
  6243. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6244. if (cd->sysinfo.ready) {
  6245. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6246. ttdata->jtag_id_h, cd->pid_for_loader);
  6247. return 0;
  6248. } else if (cd->bl_info.ready) {
  6249. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6250. cd->bl_info.chip_id, cd->pid_for_loader);
  6251. return 0;
  6252. } else {
  6253. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6254. pt_debug(cd->dev, DL_ERROR,
  6255. "%s: SYSINFO and BL_INFO are not ready\n", __func__);
  6256. return -ENODATA;
  6257. }
  6258. }
  6259. /*******************************************************************************
  6260. * FUNCTION: _pip2_generate_hw_version
  6261. *
  6262. * SUMMARY: Format chip information from struct ttdata (maintained by PIP2
  6263. * VERSION command) to the hw_version.
  6264. *
  6265. * RETURN:
  6266. * 0 = success
  6267. * !0 = failure
  6268. *
  6269. * PARAMETERS:
  6270. * *cd - pointer to core data
  6271. * *hw_version - pointer to store the hardware version
  6272. ******************************************************************************/
  6273. static int _pip2_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6274. {
  6275. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6276. if (cd->app_pip_ver_ready | cd->bl_pip_ver_ready) {
  6277. snprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.%04X.%02X",
  6278. ttdata->chip_id, ttdata->chip_rev, cd->pid_for_loader);
  6279. return 0;
  6280. } else {
  6281. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6282. pt_debug(cd->dev, DL_ERROR,
  6283. "%s: PIP Version are not ready\n", __func__);
  6284. return -ENODATA;
  6285. }
  6286. }
  6287. /*******************************************************************************
  6288. * FUNCTION: pt_generate_hw_version
  6289. *
  6290. * SUMMARY: Wraaper function for both legacy and TT/TC products generate the
  6291. * hw_version from static data.
  6292. *
  6293. * RETURN:
  6294. * 0 = success
  6295. * !0 = failure
  6296. *
  6297. * PARAMETERS:
  6298. * *cd - pointer to core data
  6299. * *hw_version - pointer to store the hardware version
  6300. ******************************************************************************/
  6301. static int pt_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6302. {
  6303. int rc = 0;
  6304. if (!hw_version)
  6305. return -ENOMEM;
  6306. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  6307. rc = _legacy_generate_hw_version(cd, hw_version);
  6308. else if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  6309. rc = _pip2_generate_hw_version(cd, hw_version);
  6310. else {
  6311. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6312. rc = -ENODATA;
  6313. }
  6314. return rc;
  6315. }
  6316. /*******************************************************************************
  6317. * SUMMARY: Attempt to retrieve the HW version of the connected DUT
  6318. *
  6319. * NOTE: The calling function must ensure to free *hw_version
  6320. *
  6321. * RETURN:
  6322. * 0 = success
  6323. * !0 = Failure
  6324. *
  6325. * PARAMETERS:
  6326. * *dev - pointer to device structure
  6327. * *hw_version - pointer to where the hw_version string will be stored
  6328. ******************************************************************************/
  6329. static int _pt_request_hw_version(struct device *dev, char *hw_version)
  6330. {
  6331. int rc = 0;
  6332. u16 actual_read_len;
  6333. u16 pip_ver;
  6334. u8 rd_buf[256];
  6335. u8 status;
  6336. u8 index = PIP2_RESP_STATUS_OFFSET;
  6337. u8 return_data[8];
  6338. u8 panel_id;
  6339. struct pt_core_data *cd = dev_get_drvdata(dev);
  6340. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6341. if (!hw_version)
  6342. return -ENOMEM;
  6343. if (!cd->hw_detected) {
  6344. /* No HW detected */
  6345. rc = -ENODEV;
  6346. pt_debug(dev, DL_ERROR, "%s: no hardware is detected!\n",
  6347. __func__);
  6348. goto exit_error;
  6349. }
  6350. /* For Parade TC or TT parts */
  6351. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  6352. rc = _pt_request_pip2_send_cmd(dev,
  6353. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  6354. NULL, 0, rd_buf, &actual_read_len);
  6355. if (rc) {
  6356. pt_debug(dev, DL_ERROR,
  6357. "%s: Failed to send PIP2 VERSION cmd\n",
  6358. __func__);
  6359. goto exit_error;
  6360. }
  6361. status = rd_buf[index];
  6362. if (status == 0) {
  6363. pip_ver = 256 * rd_buf[index + 2] + rd_buf[index + 1];
  6364. /*
  6365. * BL PIP 2.02 and greater the version fields are
  6366. * swapped
  6367. */
  6368. if (pip_ver >= 0x0202) {
  6369. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6370. "%02X%02X.%02X%02X.FF",
  6371. rd_buf[index + 10], rd_buf[index + 9],
  6372. rd_buf[index + 8], rd_buf[index + 7]);
  6373. } else {
  6374. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6375. "%02X%02X.%02X%02X.FF",
  6376. rd_buf[index + 8], rd_buf[index + 7],
  6377. rd_buf[index + 10], rd_buf[index + 9]);
  6378. }
  6379. return STATUS_SUCCESS;
  6380. } else {
  6381. rc = status;
  6382. pt_debug(dev, DL_WARN,
  6383. "%s: PIP2 VERSION cmd response error\n",
  6384. __func__);
  6385. }
  6386. } else if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  6387. /*
  6388. * For Parade/Cypress legacy parts the RevID and FamilyID are
  6389. * hard coded to FFFF
  6390. */
  6391. if (cd->mode == PT_MODE_OPERATIONAL) {
  6392. rc = pt_hid_output_get_sysinfo(cd);
  6393. if (!rc) {
  6394. panel_id =
  6395. cd->sysinfo.sensing_conf_data.panel_id;
  6396. } else {
  6397. panel_id = PANEL_ID_NOT_ENABLED;
  6398. }
  6399. /* In FW - simply retrieve from ttdata struct */
  6400. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6401. "%04X.FFFF.%02X",
  6402. ttdata->jtag_id_h,
  6403. panel_id);
  6404. return STATUS_SUCCESS;
  6405. } else {
  6406. /*
  6407. * Return the stored value if PT_PANEL_ID_BY_BL
  6408. * is not supported while other feature is.
  6409. */
  6410. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  6411. rc = pt_hid_output_bl_get_information(
  6412. cd, return_data);
  6413. if (!rc) {
  6414. rc = pt_hid_output_bl_get_panel_id(
  6415. cd, &panel_id);
  6416. }
  6417. } else
  6418. panel_id = cd->pid_for_loader;
  6419. if (!rc) {
  6420. snprintf(hw_version,
  6421. HW_VERSION_LEN_MAX,
  6422. "%02X%02X.FFFF.%02X",
  6423. return_data[3], return_data[2],
  6424. panel_id);
  6425. return STATUS_SUCCESS;
  6426. }
  6427. }
  6428. } else {
  6429. /* Unknown generation */
  6430. rc = -ENODEV;
  6431. pt_debug(dev, DL_ERROR, "%s: generation is unknown!\n",
  6432. __func__);
  6433. }
  6434. exit_error:
  6435. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6436. return rc;
  6437. }
  6438. /*******************************************************************************
  6439. * FUNCTION: pt_start_wd_timer
  6440. *
  6441. * SUMMARY: Starts the TTDL watchdog timer if the timer interval is > 0
  6442. *
  6443. * RETURN: void
  6444. *
  6445. * PARAMETERS:
  6446. * *cd - pointer to core data
  6447. ******************************************************************************/
  6448. static void pt_start_wd_timer(struct pt_core_data *cd)
  6449. {
  6450. if (cd->watchdog_interval < 100) {
  6451. pt_debug(cd->dev, DL_ERROR,
  6452. "%s: WARNING: Invalid watchdog interval: %d\n",
  6453. __func__, cd->watchdog_interval);
  6454. return;
  6455. }
  6456. if (cd->watchdog_force_stop) {
  6457. pt_debug(cd->dev, DL_INFO,
  6458. "%s: TTDL WD Forced stop\n", __func__);
  6459. return;
  6460. }
  6461. mod_timer(&cd->watchdog_timer, jiffies +
  6462. msecs_to_jiffies(cd->watchdog_interval));
  6463. cd->watchdog_enabled = 1;
  6464. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Started\n", __func__);
  6465. }
  6466. /*******************************************************************************
  6467. * FUNCTION: pt_stop_wd_timer
  6468. *
  6469. * SUMMARY: Stops the TTDL watchdog timer if the timer interval is > 0
  6470. *
  6471. * RETURN: void
  6472. *
  6473. * PARAMETERS:
  6474. * *cd - pointer to core data
  6475. ******************************************************************************/
  6476. static void pt_stop_wd_timer(struct pt_core_data *cd)
  6477. {
  6478. if (!cd->watchdog_interval)
  6479. return;
  6480. /*
  6481. * Ensure we wait until the watchdog timer
  6482. * running on a different CPU finishes
  6483. */
  6484. del_timer_sync(&cd->watchdog_timer);
  6485. cancel_work_sync(&cd->watchdog_work);
  6486. del_timer_sync(&cd->watchdog_timer);
  6487. cd->watchdog_enabled = 0;
  6488. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Stopped\n", __func__);
  6489. }
  6490. /*******************************************************************************
  6491. * FUNCTION: pt_hw_soft_reset
  6492. *
  6493. * SUMMARY: Sends a PIP reset command to the DUT. Disable/re-enable the
  6494. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6495. * schedule an enum if it fires when the DUT is being reset.
  6496. * This can cause a double reset.
  6497. *
  6498. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6499. * the WD active could cause this function to fail!
  6500. *
  6501. * RETURN:
  6502. * 0 = success
  6503. * !0 = failure
  6504. *
  6505. * PARAMETERS:
  6506. * *cd - pointer to core data struct
  6507. * protect - flag to call protected or non-protected
  6508. ******************************************************************************/
  6509. static int pt_hw_soft_reset(struct pt_core_data *cd, int protect)
  6510. {
  6511. int rc = 0;
  6512. mutex_lock(&cd->system_lock);
  6513. cd->startup_status = STARTUP_STATUS_START;
  6514. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  6515. mutex_unlock(&cd->system_lock);
  6516. if (protect)
  6517. rc = pt_hid_cmd_reset(cd);
  6518. else
  6519. rc = pt_hid_cmd_reset_(cd);
  6520. if (rc < 0) {
  6521. pt_debug(cd->dev, DL_ERROR,
  6522. "%s: FAILED to execute SOFT reset\n", __func__);
  6523. return rc;
  6524. }
  6525. pt_debug(cd->dev, DL_INFO, "%s: SOFT reset successful\n",
  6526. __func__);
  6527. return 0;
  6528. }
  6529. /*******************************************************************************
  6530. * FUNCTION: pt_hw_hard_reset
  6531. *
  6532. * SUMMARY: Calls the platform xres function if it exists to perform a hard
  6533. * reset on the DUT by toggling the XRES gpio. Disable/re-enable the
  6534. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6535. * schedule an enum if it fires when the DUT is being reset.
  6536. * This can cause a double reset.
  6537. *
  6538. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6539. * the WD active could cause this function to fail!
  6540. *
  6541. * RETURN:
  6542. * 0 = success
  6543. * !0 = failure
  6544. *
  6545. * PARAMETERS:
  6546. * *cd - pointer to core data struct
  6547. ******************************************************************************/
  6548. static int pt_hw_hard_reset(struct pt_core_data *cd)
  6549. {
  6550. if (cd->cpdata->xres) {
  6551. cd->startup_status = STARTUP_STATUS_START;
  6552. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n",
  6553. __func__);
  6554. cd->cpdata->xres(cd->cpdata, cd->dev);
  6555. pt_debug(cd->dev, DL_WARN, "%s: executed HARD reset\n",
  6556. __func__);
  6557. return 0;
  6558. }
  6559. pt_debug(cd->dev, DL_ERROR,
  6560. "%s: FAILED to execute HARD reset\n", __func__);
  6561. return -ENODEV;
  6562. }
  6563. /*******************************************************************************
  6564. * FUNCTION: pt_dut_reset
  6565. *
  6566. * SUMMARY: Attempts to reset the DUT by a hard reset and if that fails a
  6567. * soft reset.
  6568. *
  6569. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6570. * the WD active could cause this function to fail!
  6571. * NOTE: "protect" flag is only used for soft reset.
  6572. *
  6573. * RETURN:
  6574. * 0 = success
  6575. * !0 = failure
  6576. *
  6577. * PARAMETERS:
  6578. * *cd - pointer to core data structure
  6579. * protect - flag to call protected or non-protected
  6580. ******************************************************************************/
  6581. static int pt_dut_reset(struct pt_core_data *cd, int protect)
  6582. {
  6583. int rc = 0;
  6584. pt_debug(cd->dev, DL_INFO, "%s: reset hw...\n", __func__);
  6585. mutex_lock(&cd->system_lock);
  6586. cd->hid_reset_cmd_state = 1;
  6587. rc = pt_hw_hard_reset(cd);
  6588. mutex_unlock(&cd->system_lock);
  6589. if (rc == -ENODEV) {
  6590. mutex_lock(&cd->system_lock);
  6591. cd->hid_reset_cmd_state = 0;
  6592. mutex_unlock(&cd->system_lock);
  6593. pt_debug(cd->dev, DL_ERROR,
  6594. "%s: Hard reset failed, try soft reset\n", __func__);
  6595. rc = pt_hw_soft_reset(cd, protect);
  6596. }
  6597. if (rc)
  6598. pt_debug(cd->dev, DL_ERROR, "%s: %s dev='%s' r=%d\n",
  6599. __func__, "Fail hw reset", dev_name(cd->dev), rc);
  6600. return rc;
  6601. }
  6602. /*******************************************************************************
  6603. * FUNCTION: pt_dut_reset_and_wait
  6604. *
  6605. * SUMMARY: Wrapper function for pt_dut_reset that waits for the reset to
  6606. * complete
  6607. *
  6608. * RETURN:
  6609. * 0 = success
  6610. * !0 = failure
  6611. *
  6612. * PARAMETERS:
  6613. * *cd - pointer to core data structure
  6614. ******************************************************************************/
  6615. static int pt_dut_reset_and_wait(struct pt_core_data *cd)
  6616. {
  6617. int rc = 0;
  6618. int t;
  6619. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  6620. if (rc < 0)
  6621. goto exit;
  6622. t = wait_event_timeout(cd->wait_q,
  6623. (cd->hid_reset_cmd_state == 0),
  6624. msecs_to_jiffies(PT_HID_CMD_DEFAULT_TIMEOUT));
  6625. if (IS_TMO(t)) {
  6626. #ifdef TTDL_DIAGNOSTICS
  6627. cd->bus_transmit_error_count++;
  6628. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  6629. #endif /* TTDL_DIAGNOSTICS */
  6630. pt_debug(cd->dev, DL_ERROR, "%s: reset timed out\n",
  6631. __func__);
  6632. rc = -ETIME;
  6633. goto exit;
  6634. }
  6635. exit:
  6636. return rc;
  6637. }
  6638. /*
  6639. * touch default parameters (from report descriptor) to resolve protocol for
  6640. * touch report
  6641. */
  6642. const struct pt_tch_abs_params tch_hdr_default[PT_TCH_NUM_HDR] = {
  6643. /* byte offset, size, min, max, bit offset, report */
  6644. {0x00, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* SCAN TIME */
  6645. {0x02, 0x01, 0x00, 0x20, 0x00, 0x01}, /* NUMBER OF RECORDS */
  6646. {0x02, 0x01, 0x00, 0x02, 0x05, 0x01}, /* LARGE OBJECT */
  6647. {0x03, 0x01, 0x00, 0x08, 0x00, 0x01}, /* NOISE EFFECT */
  6648. {0x03, 0x01, 0x00, 0x04, 0x06, 0x01}, /* REPORT_COUNTER */
  6649. };
  6650. /*
  6651. * button default parameters (from report descriptor) to resolve protocol for
  6652. * button report
  6653. */
  6654. const struct pt_tch_abs_params tch_abs_default[PT_TCH_NUM_ABS] = {
  6655. /* byte offset, size, min, max, bit offset, report */
  6656. {0x02, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* X */
  6657. {0x04, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* Y */
  6658. {0x06, 0x01, 0x00, 0x100, 0x00, 0x01}, /* P (Z) */
  6659. {0x01, 0x01, 0x00, 0x20, 0x00, 0x01}, /* TOUCH ID */
  6660. {0x01, 0x01, 0x00, 0x04, 0x05, 0x01}, /* EVENT ID */
  6661. {0x00, 0x01, 0x00, 0x08, 0x00, 0x01}, /* OBJECT ID */
  6662. {0x01, 0x01, 0x00, 0x02, 0x07, 0x01}, /* LIFTOFF */
  6663. {0x07, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MAJOR */
  6664. {0x08, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MINOR */
  6665. {0x09, 0x01, 0x00, 0x100, 0x00, 0x01}, /* ORIENTATION */
  6666. };
  6667. /*******************************************************************************
  6668. * FUNCTION: pt_init_pip_report_fields
  6669. *
  6670. * SUMMARY: Setup default values for touch/button report parsing.
  6671. *
  6672. * PARAMETERS:
  6673. * *cd - pointer to core data structure
  6674. ******************************************************************************/
  6675. static void pt_init_pip_report_fields(struct pt_core_data *cd)
  6676. {
  6677. struct pt_sysinfo *si = &cd->sysinfo;
  6678. memcpy(si->tch_hdr, tch_hdr_default, sizeof(tch_hdr_default));
  6679. memcpy(si->tch_abs, tch_abs_default, sizeof(tch_abs_default));
  6680. si->desc.tch_report_id = PT_PIP_TOUCH_REPORT_ID;
  6681. si->desc.tch_record_size = TOUCH_REPORT_SIZE;
  6682. si->desc.tch_header_size = TOUCH_INPUT_HEADER_SIZE;
  6683. si->desc.btn_report_id = PT_PIP_CAPSENSE_BTN_REPORT_ID;
  6684. si->desc.pen_report_id = PT_HID_PEN_REPORT_ID;
  6685. si->desc.max_touch_num = MAX_TOUCH_NUM;
  6686. cd->features.easywake = 1;
  6687. cd->features.noise_metric = 1;
  6688. cd->features.tracking_heatmap = 1;
  6689. cd->features.sensor_data = 1;
  6690. }
  6691. /*******************************************************************************
  6692. * FUNCTION: pt_get_hid_report_
  6693. *
  6694. * SUMMARY: Get or create report. Must be called with cd->hid_report_lock
  6695. * acquired.
  6696. *
  6697. * RETURN:
  6698. * 0 = success
  6699. * !0 = failure
  6700. *
  6701. * PARAMETERS:
  6702. * *cd - pointer to core data structure
  6703. * *index - pointer to report index
  6704. * report_type - type of hid report
  6705. * report_id - id of hid report
  6706. * create - true: will create a new report
  6707. * false: will not create a new report
  6708. ******************************************************************************/
  6709. static int pt_get_hid_report_(struct pt_core_data *cd, u8 *index,
  6710. u8 report_type, u8 report_id, bool create)
  6711. {
  6712. struct pt_hid_report *report = NULL;
  6713. int i;
  6714. int rc = 0;
  6715. /* Look for created reports */
  6716. for (i = 0; i < cd->num_hid_reports; i++) {
  6717. if (cd->hid_reports[i]->type == report_type &&
  6718. cd->hid_reports[i]->id == report_id) {
  6719. *index = i;
  6720. goto exit;
  6721. }
  6722. }
  6723. if (create && cd->num_hid_reports >= PT_HID_MAX_REPORTS) {
  6724. pt_debug(cd->dev, DL_WARN,
  6725. "%s: num_hid_reports=%d max=%d\n", __func__,
  6726. cd->num_hid_reports, PT_HID_MAX_REPORTS);
  6727. rc = -EINVAL;
  6728. } else if (create && cd->num_hid_reports < PT_HID_MAX_REPORTS) {
  6729. /* Create a new report */
  6730. report = kzalloc(sizeof(struct pt_hid_report),
  6731. GFP_KERNEL);
  6732. if (!report)
  6733. rc = -ENOMEM;
  6734. else {
  6735. report->type = report_type;
  6736. report->id = report_id;
  6737. *index = cd->num_hid_reports;
  6738. cd->hid_reports[cd->num_hid_reports++] = report;
  6739. }
  6740. }
  6741. exit:
  6742. return rc;
  6743. }
  6744. /*******************************************************************************
  6745. * FUNCTION: pt_free_hid_reports_
  6746. *
  6747. * SUMMARY: Free HID report. Must be called with cd->hid_report_lock acquired.
  6748. *
  6749. * PARAMETERS:
  6750. * *cd - pointer to core data structure
  6751. ******************************************************************************/
  6752. static void pt_free_hid_reports_(struct pt_core_data *cd)
  6753. {
  6754. struct pt_hid_report *report;
  6755. int i, j;
  6756. for (i = 0; i < cd->num_hid_reports; i++) {
  6757. report = cd->hid_reports[i];
  6758. for (j = 0; j < report->num_fields; j++)
  6759. kfree(report->fields[j]);
  6760. kfree(report);
  6761. cd->hid_reports[i] = NULL;
  6762. }
  6763. cd->num_hid_reports = 0;
  6764. }
  6765. /*******************************************************************************
  6766. * FUNCTION: pt_free_hid_reports
  6767. *
  6768. * SUMMARY: Protected call to pt_free_hid_reports_() by a mutex lock.
  6769. *
  6770. * PARAMETERS:
  6771. * *cd - pointer to core data structure
  6772. ******************************************************************************/
  6773. static void pt_free_hid_reports(struct pt_core_data *cd)
  6774. {
  6775. mutex_lock(&cd->hid_report_lock);
  6776. pt_free_hid_reports_(cd);
  6777. mutex_unlock(&cd->hid_report_lock);
  6778. }
  6779. /*******************************************************************************
  6780. * FUNCTION: pt_create_hid_field_
  6781. *
  6782. * SUMMARY: Create field for HID report.Must be called with cd->hid_report_lock
  6783. * acquired.
  6784. *
  6785. * RETURN:
  6786. * pointer to hid field structure
  6787. *
  6788. * PARAMETERS:
  6789. * *report - pointer to hid report structure
  6790. ******************************************************************************/
  6791. static struct pt_hid_field *pt_create_hid_field_(
  6792. struct pt_hid_report *report)
  6793. {
  6794. struct pt_hid_field *field;
  6795. if (!report)
  6796. return NULL;
  6797. if (report->num_fields == PT_HID_MAX_FIELDS)
  6798. return NULL;
  6799. field = kzalloc(sizeof(struct pt_hid_field), GFP_KERNEL);
  6800. if (!field)
  6801. return NULL;
  6802. field->report = report;
  6803. report->fields[report->num_fields++] = field;
  6804. return field;
  6805. }
  6806. /*******************************************************************************
  6807. * FUNCTION: get_hid_item_data
  6808. *
  6809. * SUMMARY: Get hid item data according to the item size.
  6810. *
  6811. * RETURN:
  6812. * 0 = no data
  6813. * !0 = data
  6814. *
  6815. * PARAMETERS:
  6816. * *data - pointer to item data
  6817. * item_size - the size of the item
  6818. ******************************************************************************/
  6819. static inline int get_hid_item_data(u8 *data, int item_size)
  6820. {
  6821. if (item_size == 1)
  6822. return (int)*data;
  6823. else if (item_size == 2)
  6824. return (int)get_unaligned_le16(data);
  6825. else if (item_size == 4)
  6826. return (int)get_unaligned_le32(data);
  6827. return 0;
  6828. }
  6829. /*******************************************************************************
  6830. * FUNCTION: parse_report_descriptor
  6831. *
  6832. * SUMMARY: Parse report descriptor.
  6833. *
  6834. * RETURN:
  6835. * 0 = success
  6836. * !0 = failure
  6837. *
  6838. * PARAMETERS:
  6839. * *cd - pointer to core data structure
  6840. * *report_desc - pointer to report descriptor structure
  6841. * len - length of the data buffer to be parsed
  6842. ******************************************************************************/
  6843. static int parse_report_descriptor(struct pt_core_data *cd,
  6844. u8 *report_desc, size_t len)
  6845. {
  6846. struct pt_hid_report *report;
  6847. struct pt_hid_field *field;
  6848. u8 *buf = report_desc;
  6849. u8 *end = buf + len;
  6850. int rc = 0;
  6851. int offset = 0;
  6852. int i, j;
  6853. u8 report_type;
  6854. u32 up_usage;
  6855. /* Global items */
  6856. u8 report_id = 0;
  6857. u16 usage_page = 0;
  6858. int report_count = 0;
  6859. int report_size = 0;
  6860. int logical_min = 0;
  6861. int logical_max = 0;
  6862. /* Local items */
  6863. u16 usage = 0;
  6864. /* Main items - Collection stack */
  6865. u32 collection_usages[PT_HID_MAX_NESTED_COLLECTIONS] = {0};
  6866. u8 collection_types[PT_HID_MAX_NESTED_COLLECTIONS] = {0};
  6867. u32 usages_pen[PT_HID_MAX_CONTINUOUS_USAGES] = {0};
  6868. /* First collection for header, second for report */
  6869. int logical_collection_count = 0;
  6870. int app_collection_count = 0;
  6871. int collection_nest = 0;
  6872. int usage_cnt = 0;
  6873. u8 report_index = 0;
  6874. pt_debug(cd->dev, DL_DEBUG, "%s: Report descriptor length: %u\n",
  6875. __func__, (u32)len);
  6876. mutex_lock(&cd->hid_report_lock);
  6877. pt_free_hid_reports_(cd);
  6878. while (buf < end) {
  6879. int item_type;
  6880. int item_size;
  6881. int item_tag;
  6882. u8 *data;
  6883. /* Get Item */
  6884. item_size = HID_GET_ITEM_SIZE(buf[0]);
  6885. if (item_size == 3)
  6886. item_size = 4;
  6887. item_type = HID_GET_ITEM_TYPE(buf[0]);
  6888. item_tag = HID_GET_ITEM_TAG(buf[0]);
  6889. data = ++buf;
  6890. buf += item_size;
  6891. /* Process current item */
  6892. switch (item_type) {
  6893. case HID_ITEM_TYPE_GLOBAL: /* 1 */
  6894. switch (item_tag) {
  6895. case HID_GLOBAL_ITEM_TAG_USAGE_PAGE: /* 0 */
  6896. if (item_size == 0 || item_size == 4) {
  6897. rc = -EINVAL;
  6898. goto exit;
  6899. }
  6900. usage_page = (u16)get_hid_item_data(data,
  6901. item_size);
  6902. break;
  6903. case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM: /* 1 */
  6904. if (item_size == 0) {
  6905. rc = -EINVAL;
  6906. goto exit;
  6907. }
  6908. logical_min = get_hid_item_data(data,
  6909. item_size);
  6910. break;
  6911. case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM: /* 2 */
  6912. if (item_size == 0) {
  6913. rc = -EINVAL;
  6914. goto exit;
  6915. }
  6916. logical_max = get_hid_item_data(data,
  6917. item_size);
  6918. break;
  6919. case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM: /* 3 */
  6920. pt_debug(cd->dev, DL_INFO,
  6921. "%s: TAG Ignored - Physical Min\n",
  6922. __func__);
  6923. break;
  6924. case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM: /* 4 */
  6925. pt_debug(cd->dev, DL_INFO,
  6926. "%s: TAG Ignored - Physical Max\n",
  6927. __func__);
  6928. break;
  6929. case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT: /* 5 */
  6930. pt_debug(cd->dev, DL_INFO,
  6931. "%s: TAG Ignored - Unit Exponent\n",
  6932. __func__);
  6933. break;
  6934. case HID_GLOBAL_ITEM_TAG_UNIT: /* 6 */
  6935. pt_debug(cd->dev, DL_INFO,
  6936. "%s: TAG Ignored - Unit\n",
  6937. __func__);
  6938. break;
  6939. case HID_GLOBAL_ITEM_TAG_REPORT_SIZE: /* 7 */
  6940. if (item_size == 0) {
  6941. rc = -EINVAL;
  6942. goto exit;
  6943. }
  6944. report_size = get_hid_item_data(data,
  6945. item_size);
  6946. break;
  6947. case HID_GLOBAL_ITEM_TAG_REPORT_ID: /* 8 */
  6948. if (item_size != 1) {
  6949. rc = -EINVAL;
  6950. goto exit;
  6951. }
  6952. report_id = get_hid_item_data(data, item_size);
  6953. offset = 0;
  6954. logical_collection_count = 0;
  6955. break;
  6956. case HID_GLOBAL_ITEM_TAG_REPORT_COUNT: /* 9 */
  6957. if (item_size == 0) {
  6958. rc = -EINVAL;
  6959. goto exit;
  6960. }
  6961. report_count = get_hid_item_data(data,
  6962. item_size);
  6963. break;
  6964. case HID_GLOBAL_ITEM_TAG_PUSH: /* A */
  6965. pt_debug(cd->dev, DL_INFO,
  6966. "%s: TAG Ignored - Push\n",
  6967. __func__);
  6968. break;
  6969. case HID_GLOBAL_ITEM_TAG_POP: /* B */
  6970. pt_debug(cd->dev, DL_INFO,
  6971. "%s: TAG Ignored - Pop\n",
  6972. __func__);
  6973. break;
  6974. default:
  6975. pt_debug(cd->dev, DL_INFO,
  6976. "%s: Unrecognized Global tag 0x%X\n",
  6977. __func__, item_tag);
  6978. }
  6979. break;
  6980. case HID_ITEM_TYPE_LOCAL: /* 2 */
  6981. switch (item_tag) {
  6982. case HID_LOCAL_ITEM_TAG_USAGE:
  6983. if (item_size == 0 || item_size == 4) {
  6984. rc = -EINVAL;
  6985. goto exit;
  6986. }
  6987. usage = (u16)get_hid_item_data(data,
  6988. item_size);
  6989. if (report_id == PT_HID_PEN_REPORT_ID) {
  6990. usages_pen[usage_cnt++] =
  6991. usage_page << 16 | usage;
  6992. }
  6993. break;
  6994. case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
  6995. if (item_size == 0) {
  6996. rc = -EINVAL;
  6997. goto exit;
  6998. }
  6999. /* usage_min = */
  7000. get_hid_item_data(data, item_size);
  7001. break;
  7002. case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
  7003. if (item_size == 0) {
  7004. rc = -EINVAL;
  7005. goto exit;
  7006. }
  7007. /* usage_max = */
  7008. get_hid_item_data(data, item_size);
  7009. break;
  7010. default:
  7011. pt_debug(cd->dev, DL_INFO,
  7012. "%s: Unrecognized Local tag %d\n",
  7013. __func__, item_tag);
  7014. }
  7015. break;
  7016. case HID_ITEM_TYPE_MAIN: /* 0 */
  7017. switch (item_tag) {
  7018. case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
  7019. usage_cnt = 0;
  7020. if (item_size != 1) {
  7021. rc = -EINVAL;
  7022. goto exit;
  7023. }
  7024. if (PT_HID_MAX_NESTED_COLLECTIONS ==
  7025. collection_nest) {
  7026. rc = -EINVAL;
  7027. goto exit;
  7028. }
  7029. up_usage = usage_page << 16 | usage;
  7030. /* Update collection stack */
  7031. collection_usages[collection_nest] = up_usage;
  7032. collection_types[collection_nest] =
  7033. get_hid_item_data(data, item_size);
  7034. if (collection_types[collection_nest] ==
  7035. HID_COLLECTION_LOGICAL) {
  7036. logical_collection_count++;
  7037. app_collection_count = 0;
  7038. }
  7039. if (collection_types[collection_nest] ==
  7040. HID_COLLECTION_APPLICATION)
  7041. app_collection_count++;
  7042. collection_nest++;
  7043. break;
  7044. case HID_MAIN_ITEM_TAG_END_COLLECTION:
  7045. if (item_size != 0) {
  7046. rc = -EINVAL;
  7047. goto exit;
  7048. }
  7049. if (--collection_nest < 0) {
  7050. rc = -EINVAL;
  7051. goto exit;
  7052. }
  7053. break;
  7054. case HID_MAIN_ITEM_TAG_INPUT:
  7055. report_type = HID_INPUT_REPORT;
  7056. goto continue_main_item;
  7057. case HID_MAIN_ITEM_TAG_OUTPUT:
  7058. report_type = HID_OUTPUT_REPORT;
  7059. goto continue_main_item;
  7060. case HID_MAIN_ITEM_TAG_FEATURE:
  7061. report_type = HID_FEATURE_REPORT;
  7062. continue_main_item:
  7063. if (item_size != 1) {
  7064. pt_debug(cd->dev, DL_WARN,
  7065. "%s: %s=%d\n",
  7066. __func__,
  7067. "item_size", item_size);
  7068. rc = -EINVAL;
  7069. goto exit;
  7070. }
  7071. up_usage = usage_page << 16 | usage;
  7072. /* Get or create report */
  7073. rc = pt_get_hid_report_(cd, &report_index,
  7074. report_type, report_id, true);
  7075. if (rc) {
  7076. pt_debug(cd->dev, DL_WARN,
  7077. "%s: %s rc=%d\n",
  7078. __func__,
  7079. "get_hid_report failed", rc);
  7080. goto exit;
  7081. } else
  7082. report = cd->hid_reports[report_index];
  7083. if (!report->usage_page && collection_nest > 0)
  7084. report->usage_page =
  7085. collection_usages
  7086. [collection_nest - 1];
  7087. if (report->id == PT_HID_PEN_REPORT_ID
  7088. && report_count > 1)
  7089. goto continue_pen_usages;
  7090. /* Create field */
  7091. field = pt_create_hid_field_(report);
  7092. if (!field) {
  7093. pt_debug(cd->dev, DL_WARN,
  7094. "%s: %s\n",
  7095. __func__,
  7096. "create field failed");
  7097. rc = -ENOMEM;
  7098. goto exit;
  7099. }
  7100. field->report_count = report_count;
  7101. field->report_size = report_size;
  7102. field->size = report_count
  7103. * report_size;
  7104. field->offset = offset;
  7105. field->data_type =
  7106. get_hid_item_data(data,
  7107. item_size);
  7108. field->logical_min = logical_min;
  7109. field->logical_max = logical_max;
  7110. field->usage_page = up_usage;
  7111. usages_pen[0] = 0;
  7112. for (j = 0; j < collection_nest; j++) {
  7113. field->collection_usage_pages
  7114. [collection_types[j]] =
  7115. collection_usages[j];
  7116. }
  7117. goto exit_main_item;
  7118. continue_pen_usages:
  7119. for (i = 0; i < report_count; i++) {
  7120. /* Create field */
  7121. field = pt_create_hid_field_(
  7122. report);
  7123. if (!field) {
  7124. pt_debug(cd->dev, DL_WARN,
  7125. "%s: Pen - %s\n",
  7126. __func__,
  7127. "create field failed");
  7128. rc = -ENOMEM;
  7129. goto exit;
  7130. }
  7131. field->report_size = report_size;
  7132. field->size = report_count *
  7133. report_size;
  7134. field->report_count = 1;
  7135. field->offset = offset + i*report_size;
  7136. field->data_type =
  7137. get_hid_item_data(data,
  7138. item_size);
  7139. field->logical_min = logical_min;
  7140. field->logical_max = logical_max;
  7141. field->usage_page = usages_pen[i];
  7142. usages_pen[i] = 0;
  7143. for (j = 0; j < collection_nest; j++) {
  7144. field->collection_usage_pages
  7145. [collection_types[j]] =
  7146. collection_usages[j];
  7147. }
  7148. }
  7149. exit_main_item:
  7150. /* Update report's header or record size */
  7151. if (app_collection_count == 1) {
  7152. report->header_size += field->size;
  7153. } else if (logical_collection_count == 1) {
  7154. field->record_field = true;
  7155. field->offset -= report->header_size;
  7156. /* Set record field index */
  7157. if (report->record_field_index == 0)
  7158. report->record_field_index =
  7159. report->num_fields - 1;
  7160. report->record_size += field->size;
  7161. }
  7162. report->size += field->size;
  7163. report->log_collection_num =
  7164. logical_collection_count;
  7165. offset += field->size;
  7166. usage_cnt = 0;
  7167. break;
  7168. default:
  7169. pt_debug(cd->dev, DL_INFO,
  7170. "%s: Unrecognized Main tag %d\n",
  7171. __func__, item_tag);
  7172. }
  7173. /* Reset all local items */
  7174. usage = 0;
  7175. break;
  7176. }
  7177. }
  7178. if (buf != end) {
  7179. pt_debug(cd->dev, DL_ERROR,
  7180. "%s: Report descriptor length invalid\n",
  7181. __func__);
  7182. rc = -EINVAL;
  7183. goto exit;
  7184. }
  7185. if (collection_nest) {
  7186. pt_debug(cd->dev, DL_ERROR,
  7187. "%s: Unbalanced collection items (%d)\n",
  7188. __func__, collection_nest);
  7189. rc = -EINVAL;
  7190. goto exit;
  7191. }
  7192. exit:
  7193. if (rc)
  7194. pt_free_hid_reports_(cd);
  7195. mutex_unlock(&cd->hid_report_lock);
  7196. return rc;
  7197. }
  7198. /*******************************************************************************
  7199. * FUNCTION: find_report_desc_field
  7200. *
  7201. * SUMMARY: Find the corresponding field from report according to the usage page
  7202. * and collection usage page.
  7203. *
  7204. * RETURN:
  7205. * pointer to hid field structure
  7206. *
  7207. * PARAMETERS:
  7208. * *cd - pointer to core data structure
  7209. * usage_page - hid usage page
  7210. * collection_usage_page - hid collection usage page
  7211. * collection_type - hid collection type
  7212. ******************************************************************************/
  7213. static struct pt_hid_field *find_report_desc_field(
  7214. struct pt_core_data *cd, u32 usage_page,
  7215. u32 collection_usage_page, u8 collection_type)
  7216. {
  7217. struct pt_hid_report *report = NULL;
  7218. struct pt_hid_field *field = NULL;
  7219. int i;
  7220. int j;
  7221. u32 field_cup;
  7222. u32 field_up;
  7223. for (i = 0; i < cd->num_hid_reports; i++) {
  7224. report = cd->hid_reports[i];
  7225. for (j = 0; j < report->num_fields; j++) {
  7226. field = report->fields[j];
  7227. field_cup = field->collection_usage_pages
  7228. [collection_type];
  7229. field_up = field->usage_page;
  7230. if (field_cup == collection_usage_page
  7231. && field_up == usage_page) {
  7232. return field;
  7233. }
  7234. }
  7235. }
  7236. return NULL;
  7237. }
  7238. /*******************************************************************************
  7239. * FUNCTION: fill_tch_abs
  7240. *
  7241. * SUMMARY: Fill touch abs with hid field.
  7242. *
  7243. * PARAMETERS:
  7244. * *cd - pointer to core data structure
  7245. * *field - pointer to hid field structure
  7246. ******************************************************************************/
  7247. static void fill_tch_abs(struct pt_tch_abs_params *tch_abs,
  7248. struct pt_hid_field *field)
  7249. {
  7250. tch_abs->ofs = field->offset / 8;
  7251. tch_abs->size = field->report_size / 8;
  7252. if (field->report_size % 8)
  7253. tch_abs->size += 1;
  7254. tch_abs->min = 0;
  7255. tch_abs->max = 1 << field->report_size;
  7256. tch_abs->bofs = field->offset - (tch_abs->ofs << 3);
  7257. tch_abs->logical_max = field->logical_max;
  7258. }
  7259. /*******************************************************************************
  7260. * FUNCTION: find_report_desc
  7261. *
  7262. * SUMMARY: Find out the corresponding report based on the usage page.
  7263. *
  7264. * RETURN:
  7265. * pointer to hid report structure
  7266. *
  7267. * PARAMETERS:
  7268. * *cd - pointer to core data structure
  7269. * id - report id
  7270. * usage_page - hid usage page
  7271. ******************************************************************************/
  7272. static struct pt_hid_report *find_report_desc(struct pt_core_data *cd,
  7273. u8 id, u32 usage_page)
  7274. {
  7275. struct pt_hid_report *report = NULL;
  7276. int i;
  7277. for (i = 0; i < cd->num_hid_reports; i++) {
  7278. if (cd->hid_reports[i]->usage_page == usage_page &&
  7279. cd->hid_reports[i]->id == id) {
  7280. report = cd->hid_reports[i];
  7281. break;
  7282. }
  7283. }
  7284. return report;
  7285. }
  7286. /*******************************************************************************
  7287. * FUNCTION: setup_pen_report_from_report_desc
  7288. *
  7289. * SUMMARY: Setup values for pen report according to report descriptor.
  7290. *
  7291. * PARAMETERS:
  7292. * *cd - pointer to core data structure
  7293. ******************************************************************************/
  7294. static void setup_pen_report_from_report_desc(
  7295. struct pt_core_data *cd)
  7296. {
  7297. struct pt_sysinfo *si = &cd->sysinfo;
  7298. struct pt_hid_report *report;
  7299. struct pt_hid_field *field;
  7300. int i;
  7301. u32 pen_collection_usage_page = HID_PT_PEN_COL_USAGE_PG;
  7302. u8 id = PT_HID_PEN_REPORT_ID;
  7303. /*
  7304. * Search each pen abs field. If found, fill the values into the
  7305. * pen struct.If not, mark this pen field as invalid (report = 0).
  7306. */
  7307. for (i = PT_PEN_X; i < PT_PEN_NUM_ABS; i++) {
  7308. field = find_report_desc_field(cd,
  7309. pt_pen_abs_field_map[i],
  7310. pen_collection_usage_page,
  7311. HID_COLLECTION_PHYSICAL);
  7312. if (field) {
  7313. pt_debug(cd->dev, DL_DEBUG,
  7314. " Field %p: rep_cnt:%d rep_sz:%d off:%d data:%02X min:%d max:%d usage_page:%08X\n",
  7315. field, field->report_count, field->report_size,
  7316. field->offset, field->data_type,
  7317. field->logical_min, field->logical_max,
  7318. field->usage_page);
  7319. fill_tch_abs(&si->pen_abs[i], field);
  7320. si->pen_abs[i].report = 2;
  7321. pt_debug(cd->dev, DL_DEBUG, "%s: ofs:%u size:%u min:%u max:%u bofs:%u report:%d",
  7322. pt_pen_abs_string[i],
  7323. (u32)si->pen_abs[i].ofs,
  7324. (u32)si->pen_abs[i].size,
  7325. (u32)si->pen_abs[i].min,
  7326. (u32)si->pen_abs[i].max,
  7327. (u32)si->pen_abs[i].bofs,
  7328. si->pen_abs[i].report);
  7329. } else {
  7330. si->pen_abs[i].report = 0;
  7331. pt_debug(cd->dev, DL_DEBUG, "%s: report:%d\n",
  7332. pt_pen_abs_string[i], si->pen_abs[i].report);
  7333. }
  7334. }
  7335. report = find_report_desc(cd, id, pen_collection_usage_page);
  7336. if (report)
  7337. si->desc.pen_report_id = report->id;
  7338. else
  7339. si->desc.pen_report_id = PT_HID_PEN_REPORT_ID;
  7340. }
  7341. /*******************************************************************************
  7342. * FUNCTION: setup_finger_report_from_report_desc
  7343. *
  7344. * SUMMARY: Setup values for finger report according to report descriptor.
  7345. *
  7346. * PARAMETERS:
  7347. * *cd - pointer to core data structure
  7348. ******************************************************************************/
  7349. static void setup_finger_report_from_report_desc(
  7350. struct pt_core_data *cd)
  7351. {
  7352. struct pt_sysinfo *si = &cd->sysinfo;
  7353. struct pt_hid_report *report;
  7354. struct pt_hid_field *field;
  7355. u32 tch_collection_usage_page = HID_PT_TCH_COL_USAGE_PG;
  7356. u8 id = PT_HID_FINGER_REPORT_ID;
  7357. int i;
  7358. /*
  7359. * Search each touch abs field. If found, fill the values into the
  7360. * abs struct. If not, mark this abs field as invalid (report = 0).
  7361. */
  7362. for (i = PT_TCH_X; i < PT_TCH_NUM_ABS; i++) {
  7363. field = find_report_desc_field(cd,
  7364. pt_tch_abs_field_map[i],
  7365. tch_collection_usage_page,
  7366. HID_COLLECTION_APPLICATION);
  7367. if (field) {
  7368. pt_debug(cd->dev, DL_DEBUG,
  7369. " Field %p: rep_cnt:%d rep_sz:%d off:%d data:%02X min:%d max:%d usage_page:%08X\n",
  7370. field, field->report_count, field->report_size,
  7371. field->offset, field->data_type,
  7372. field->logical_min, field->logical_max,
  7373. field->usage_page);
  7374. fill_tch_abs(&si->tch_abs[i], field);
  7375. si->tch_abs[i].report = 1;
  7376. pt_debug(cd->dev, DL_DEBUG, "%s: ofs:%u size:%u min:%u max:%u bofs:%u report:%d",
  7377. pt_tch_abs_string[i],
  7378. (u32)si->tch_abs[i].ofs,
  7379. (u32)si->tch_abs[i].size,
  7380. (u32)si->tch_abs[i].min,
  7381. (u32)si->tch_abs[i].max,
  7382. (u32)si->tch_abs[i].bofs,
  7383. si->tch_abs[i].report);
  7384. } else {
  7385. si->tch_abs[i].report = 0;
  7386. }
  7387. }
  7388. /*
  7389. * Search each touch header field. If found, fill the values into
  7390. * the header struct. If not, mark this header field as invalid
  7391. * (report = 0).
  7392. */
  7393. for (i = PT_TCH_TIME; i < PT_TCH_NUM_HDR; i++) {
  7394. field = find_report_desc_field(cd,
  7395. pt_tch_hdr_field_map[i],
  7396. tch_collection_usage_page,
  7397. HID_COLLECTION_APPLICATION);
  7398. if (field) {
  7399. pt_debug(cd->dev, DL_DEBUG,
  7400. " Field %p: rep_cnt:%d rep_sz:%d off:%d data:%02X min:%d max:%d usage_page:%08X\n",
  7401. field, field->report_count, field->report_size,
  7402. field->offset, field->data_type,
  7403. field->logical_min, field->logical_max,
  7404. field->usage_page);
  7405. fill_tch_abs(&si->tch_hdr[i], field);
  7406. si->tch_hdr[i].report = 1;
  7407. pt_debug(cd->dev, DL_DEBUG, "%s: ofs:%u size:%u min:%u max:%u bofs:%u report:%d",
  7408. pt_tch_hdr_string[i],
  7409. (u32)si->tch_hdr[i].ofs,
  7410. (u32)si->tch_hdr[i].size,
  7411. (u32)si->tch_hdr[i].min,
  7412. (u32)si->tch_hdr[i].max,
  7413. (u32)si->tch_hdr[i].bofs,
  7414. si->tch_hdr[i].report);
  7415. } else {
  7416. si->tch_hdr[i].report = 0;
  7417. }
  7418. }
  7419. si->desc.max_touch_num = si->tch_hdr[PT_TCH_NUM].max;
  7420. report = find_report_desc(cd, id, tch_collection_usage_page);
  7421. if (report) {
  7422. si->desc.tch_report_id = report->id;
  7423. /* First, report->record_size and report->header_size are based
  7424. * on 'BIT', they need to be divided by 8 to get tch_record_size
  7425. * and tch_header_size, which are based on 'BYTE'.
  7426. * Second, tch_header_size needs to add 3 bytes: 2 bytes length,
  7427. * 1 byte report id.
  7428. */
  7429. si->desc.tch_record_size = report->record_size / 8;
  7430. si->desc.tch_header_size = (report->header_size / 8) + 3;
  7431. si->desc.max_tch_per_packet = report->log_collection_num;
  7432. } else {
  7433. si->desc.tch_report_id = PT_HID_FINGER_REPORT_ID;
  7434. si->desc.tch_record_size = TOUCH_REPORT_SIZE;
  7435. si->desc.tch_header_size = TOUCH_INPUT_HEADER_SIZE;
  7436. si->desc.max_tch_per_packet = MAX_TOUCH_NUM;
  7437. }
  7438. pt_debug(cd->dev, DL_DEBUG,
  7439. "%s: tch_record_size:%d, tch_header_size:%d, max_touch_num:%d\n",
  7440. __func__,
  7441. si->desc.tch_record_size,
  7442. si->desc.tch_header_size,
  7443. si->desc.max_touch_num);
  7444. }
  7445. /*******************************************************************************
  7446. * FUNCTION: publish_report_desc
  7447. *
  7448. * SUMMARY: If TTHE_TUNER_SUPPORT is defined print the report descriptor data
  7449. * into the tthe_tuner sysfs node under the label "HIDRptDesc".
  7450. *
  7451. * PARAMETERS:
  7452. * *cd - pointer to core data
  7453. * len - Report descriptor length
  7454. ******************************************************************************/
  7455. static void publish_report_desc(struct pt_core_data *cd, u16 len)
  7456. {
  7457. int max_bytes = 300;
  7458. int pr_bytes = 0;
  7459. int size = len;
  7460. #ifdef TTHE_TUNER_SUPPORT
  7461. tthe_print(cd, cd->response_buf, size,
  7462. "HIDRptDesc=");
  7463. #endif
  7464. while (size > max_bytes) {
  7465. pt_pr_buf(cd->dev, DL_DEBUG,
  7466. cd->response_buf + pr_bytes,
  7467. max_bytes, "<<< HIDRptDesc");
  7468. pr_bytes += max_bytes;
  7469. size -= max_bytes;
  7470. }
  7471. if (size > 0)
  7472. pt_pr_buf(cd->dev, DL_DEBUG,
  7473. cd->response_buf + pr_bytes,
  7474. size, "<<< HIDRptDesc");
  7475. }
  7476. /*******************************************************************************
  7477. * FUNCTION: pt_get_report_descriptor_
  7478. *
  7479. * SUMMARY: Get and parse report descriptor.
  7480. *
  7481. * RETURN:
  7482. * 0 = success
  7483. * !0 = failure
  7484. *
  7485. * PARAMETERS:
  7486. * *cd - pointer to core data
  7487. ******************************************************************************/
  7488. static int pt_get_report_descriptor_(struct pt_core_data *cd)
  7489. {
  7490. struct device *dev = cd->dev;
  7491. struct pt_hid_cmd hid_cmd = {
  7492. .descriptor = cd->hid_desc.report_desc_register,
  7493. .read_length = cd->hid_core.hid_report_desc_len,
  7494. };
  7495. int rc;
  7496. int t;
  7497. u8 *desc;
  7498. u16 desc_len;
  7499. rc = pt_hid_send_command(cd, &hid_cmd);
  7500. if (rc) {
  7501. pt_debug(dev, DL_ERROR,
  7502. "%s: failed to get HID report descriptor length, rc=%d\n",
  7503. __func__, rc);
  7504. goto exit;
  7505. }
  7506. desc = cd->response_buf;
  7507. desc_len = cd->hid_core.hid_report_desc_len;
  7508. /* Remove length field and report id to prepare parse work */
  7509. if (cd->dut_status.protocol_mode != PT_PROTOCOL_MODE_HID) {
  7510. desc = cd->response_buf + 3;
  7511. desc_len -= 3;
  7512. }
  7513. publish_report_desc(cd, cd->hid_core.hid_report_desc_len);
  7514. rc = parse_report_descriptor(cd, desc, desc_len);
  7515. if (rc) {
  7516. pt_debug(cd->dev, DL_ERROR,
  7517. "%s: Error parsing report descriptor rc=%d\n",
  7518. __func__, rc);
  7519. }
  7520. pt_debug(cd->dev, DL_INFO, "%s: %d reports found in descriptor\n",
  7521. __func__, cd->num_hid_reports);
  7522. for (t = 0; t < cd->num_hid_reports; t++) {
  7523. struct pt_hid_report *report = cd->hid_reports[t];
  7524. int j;
  7525. pt_debug(cd->dev, DL_DEBUG,
  7526. "Report %d: type:%d id:%02X size:%d fields:%d rec_fld_index:%d hdr_sz:%d rec_sz:%d usage_page:%08X\n",
  7527. t, report->type, report->id,
  7528. report->size, report->num_fields,
  7529. report->record_field_index, report->header_size,
  7530. report->record_size, report->usage_page);
  7531. if (report->id == PT_HID_FINGER_REPORT_ID)
  7532. pt_debug(cd->dev, DL_INFO, "%s: logical collection number: %d\n",
  7533. __func__, report->log_collection_num);
  7534. for (j = 0; j < report->num_fields; j++) {
  7535. struct pt_hid_field *field = report->fields[j];
  7536. pt_debug(cd->dev, DL_DEBUG,
  7537. " Field %d: rep_cnt:%d rep_sz:%d off:%d data:%02X min:%d max:%d usage_page:%08X\n",
  7538. j, field->report_count, field->report_size,
  7539. field->offset, field->data_type,
  7540. field->logical_min, field->logical_max,
  7541. field->usage_page);
  7542. pt_debug(cd->dev, DL_DEBUG, " Collections Phys:%08X App:%08X Log:%08X\n",
  7543. field->collection_usage_pages
  7544. [HID_COLLECTION_PHYSICAL],
  7545. field->collection_usage_pages
  7546. [HID_COLLECTION_APPLICATION],
  7547. field->collection_usage_pages
  7548. [HID_COLLECTION_LOGICAL]);
  7549. }
  7550. }
  7551. setup_pen_report_from_report_desc(cd);
  7552. setup_finger_report_from_report_desc(cd);
  7553. /* Free it for now */
  7554. pt_free_hid_reports_(cd);
  7555. pt_debug(cd->dev, DL_INFO, "%s: %d reports found in descriptor\n",
  7556. __func__, cd->num_hid_reports);
  7557. exit:
  7558. return rc;
  7559. }
  7560. /*******************************************************************************
  7561. * FUNCTION: pt_get_report_descriptor
  7562. *
  7563. * SUMMARY: Protected call to pt_get_report_descriptor_()
  7564. *
  7565. * RETURN:
  7566. * 0 = success
  7567. * !0 = failure
  7568. *
  7569. * PARAMETERS:
  7570. * *cd - pointer to core data
  7571. ******************************************************************************/
  7572. static int pt_get_report_descriptor(struct pt_core_data *cd)
  7573. {
  7574. int rc;
  7575. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  7576. if (rc < 0) {
  7577. pt_debug(cd->dev, DL_ERROR,
  7578. "%s: fail get exclusive ex=%p own=%p\n",
  7579. __func__, cd->exclusive_dev, cd->dev);
  7580. return rc;
  7581. }
  7582. rc = pt_get_report_descriptor_(cd);
  7583. if (release_exclusive(cd, cd->dev) < 0)
  7584. pt_debug(cd->dev, DL_ERROR,
  7585. "%s: fail to release exclusive\n", __func__);
  7586. return rc;
  7587. }
  7588. /*******************************************************************************
  7589. * FUNCTION: pt_get_mode
  7590. *
  7591. * SUMMARY: Determine the current mode from the contents of a HID descriptor
  7592. * message
  7593. *
  7594. * RETURN: Enum of the current mode
  7595. *
  7596. * PARAMETERS:
  7597. * *cd - pointer to the Core Data structure
  7598. * protect - run command in protected mode
  7599. * *mode - pointer to store the retrieved mode
  7600. ******************************************************************************/
  7601. static int pt_get_mode(struct pt_core_data *cd, struct pt_hid_desc *desc)
  7602. {
  7603. if (desc->packet_id == HID_APP_REPORT_ID)
  7604. return PT_MODE_OPERATIONAL;
  7605. else if (desc->packet_id == HID_BL_REPORT_ID)
  7606. return PT_MODE_BOOTLOADER;
  7607. return PT_MODE_UNKNOWN;
  7608. }
  7609. /*******************************************************************************
  7610. * FUNCTION: _pt_request_get_mode
  7611. *
  7612. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7613. * to determine the current mode of the DUT by use of the Get HID
  7614. * Descriptor command.
  7615. *
  7616. * RETURN:
  7617. * 0 = success
  7618. * !0 = failure
  7619. *
  7620. * PARAMETERS:
  7621. * *dev - pointer to device structure
  7622. * protect - run command in protected mode
  7623. * *mode - pointer to store the retrieved mode
  7624. ******************************************************************************/
  7625. static int _pt_request_get_mode(struct device *dev, int protect, u8 *mode)
  7626. {
  7627. struct pt_core_data *cd = dev_get_drvdata(dev);
  7628. struct pt_hid_desc hid_desc;
  7629. int rc = 0;
  7630. memset(&hid_desc, 0, sizeof(hid_desc));
  7631. if (protect)
  7632. rc = pt_get_hid_descriptor(cd, &hid_desc);
  7633. else
  7634. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  7635. if (rc)
  7636. *mode = PT_MODE_UNKNOWN;
  7637. else
  7638. *mode = pt_get_mode(cd, &hid_desc);
  7639. return rc;
  7640. }
  7641. /*******************************************************************************
  7642. * FUNCTION: pt_queue_enum_
  7643. *
  7644. * SUMMARY: Queues a TTDL enum by scheduling work with the pt_enum_with_dut()
  7645. * function. It won't try to add/delete sysfs node or modules.
  7646. *
  7647. * RETURN: void
  7648. *
  7649. * PARAMETERS:
  7650. * *cd - pointer to core data
  7651. ******************************************************************************/
  7652. static void pt_queue_enum_(struct pt_core_data *cd)
  7653. {
  7654. if (cd->startup_state == STARTUP_NONE) {
  7655. cd->startup_state = STARTUP_QUEUED;
  7656. #ifdef TTDL_DIAGNOSTICS
  7657. if (!cd->bridge_mode)
  7658. schedule_work(&cd->enum_work);
  7659. else
  7660. cd->startup_state = STARTUP_NONE;
  7661. #else
  7662. schedule_work(&cd->enum_work);
  7663. #endif
  7664. pt_debug(cd->dev, DL_INFO,
  7665. "%s: enum_work queued\n", __func__);
  7666. } else {
  7667. pt_debug(cd->dev, DL_WARN,
  7668. "%s: Enum not queued - startup_state = %d\n",
  7669. __func__, cd->startup_state);
  7670. }
  7671. }
  7672. /*******************************************************************************
  7673. * FUNCTION: pt_queue_enum
  7674. *
  7675. * SUMMARY: Queues a TTDL enum within a mutex lock
  7676. *
  7677. * RETURN: void
  7678. *
  7679. * PARAMETERS:
  7680. * *cd - pointer to core data
  7681. ******************************************************************************/
  7682. static void pt_queue_enum(struct pt_core_data *cd)
  7683. {
  7684. mutex_lock(&cd->system_lock);
  7685. pt_queue_enum_(cd);
  7686. mutex_unlock(&cd->system_lock);
  7687. }
  7688. static void remove_sysfs_and_modules(struct device *dev);
  7689. /*******************************************************************************
  7690. * FUNCTION: pt_queue_restart
  7691. *
  7692. * SUMMARY: Queues a TTDL restart within a mutex lock
  7693. *
  7694. * RETURN: void
  7695. *
  7696. * PARAMETERS:
  7697. * *cd - pointer to core data
  7698. * remove_sysfs_module - True: remove all DUT relative sysfs nodes and modules
  7699. * False: will not perform remove action
  7700. ******************************************************************************/
  7701. static void pt_queue_restart(struct pt_core_data *cd)
  7702. {
  7703. mutex_lock(&cd->system_lock);
  7704. if (cd->startup_state == STARTUP_NONE) {
  7705. cd->startup_state = STARTUP_QUEUED;
  7706. schedule_work(&cd->ttdl_restart_work);
  7707. pt_debug(cd->dev, DL_INFO,
  7708. "%s: pt_ttdl_restart queued\n", __func__);
  7709. } else {
  7710. pt_debug(cd->dev, DL_INFO, "%s: startup_state = %d\n",
  7711. __func__, cd->startup_state);
  7712. }
  7713. mutex_unlock(&cd->system_lock);
  7714. }
  7715. /*******************************************************************************
  7716. * FUNCTION: call_atten_cb
  7717. *
  7718. * SUMMARY: Iterate over attention list call the function that registered.
  7719. *
  7720. * RETURN: void
  7721. *
  7722. * PARAMETERS:
  7723. * *cd - pointer to core data
  7724. * type - type of attention list
  7725. * mode - condition for execution
  7726. ******************************************************************************/
  7727. static void call_atten_cb(struct pt_core_data *cd,
  7728. enum pt_atten_type type, int mode)
  7729. {
  7730. struct atten_node *atten, *atten_n;
  7731. pt_debug(cd->dev, DL_DEBUG, "%s: check list type=%d mode=%d\n",
  7732. __func__, type, mode);
  7733. spin_lock(&cd->spinlock);
  7734. list_for_each_entry_safe(atten, atten_n,
  7735. &cd->atten_list[type], node) {
  7736. if (!mode || atten->mode & mode) {
  7737. spin_unlock(&cd->spinlock);
  7738. pt_debug(cd->dev, DL_DEBUG,
  7739. "%s: attention for '%s'",
  7740. __func__, dev_name(atten->dev));
  7741. atten->func(atten->dev);
  7742. spin_lock(&cd->spinlock);
  7743. }
  7744. }
  7745. spin_unlock(&cd->spinlock);
  7746. }
  7747. /*******************************************************************************
  7748. * FUNCTION: start_fw_upgrade
  7749. *
  7750. * SUMMARY: Calling "PT_ATTEN_LOADER" attention list that loader registered to
  7751. * start firmware upgrade.
  7752. *
  7753. * RETURN:
  7754. * 0 = success
  7755. *
  7756. * PARAMETERS:
  7757. * *data - pointer to core data
  7758. ******************************************************************************/
  7759. static int start_fw_upgrade(void *data)
  7760. {
  7761. struct pt_core_data *cd = (struct pt_core_data *)data;
  7762. call_atten_cb(cd, PT_ATTEN_LOADER, 0);
  7763. return 0;
  7764. }
  7765. /*******************************************************************************
  7766. * FUNCTION: pt_put_device_into_easy_wakeup_
  7767. *
  7768. * SUMMARY: Call the enter_easywake_state function and set the device into easy
  7769. * wake up state.
  7770. *
  7771. * RETURN:
  7772. * 0 = success
  7773. * !0 = failure
  7774. *
  7775. * PARAMETERS:
  7776. * *cd - pointer to core data
  7777. ******************************************************************************/
  7778. static int pt_put_device_into_easy_wakeup_(struct pt_core_data *cd)
  7779. {
  7780. int rc = 0;
  7781. u8 status = 0;
  7782. mutex_lock(&cd->system_lock);
  7783. cd->wait_until_wake = 0;
  7784. mutex_unlock(&cd->system_lock);
  7785. rc = pt_hid_output_enter_easywake_state_(cd,
  7786. cd->easy_wakeup_gesture, &status);
  7787. if (rc || status == 0)
  7788. return -EBUSY;
  7789. return rc;
  7790. }
  7791. /*******************************************************************************
  7792. * FUNCTION: pt_put_device_into_deep_sleep_
  7793. *
  7794. * SUMMARY: Call the set_power function and set the DUT to deep sleep
  7795. *
  7796. * RETURN:
  7797. * 0 = success
  7798. * !0 = error
  7799. *
  7800. * PARAMETERS:
  7801. * *cd - pointer to core data
  7802. ******************************************************************************/
  7803. static int pt_put_device_into_deep_sleep_(struct pt_core_data *cd)
  7804. {
  7805. int rc = 0;
  7806. rc = pt_hid_cmd_set_power_(cd, HID_POWER_SLEEP);
  7807. if (rc)
  7808. rc = -EBUSY;
  7809. return rc;
  7810. }
  7811. /*******************************************************************************
  7812. * FUNCTION: pt_put_device_into_deep_standby_
  7813. *
  7814. * SUMMARY: Call the set_power function and set the DUT to Deep Standby
  7815. *
  7816. * RETURN:
  7817. * 0 = success
  7818. * !0 = error
  7819. *
  7820. * PARAMETERS:
  7821. * *cd - pointer to core data
  7822. ******************************************************************************/
  7823. static int pt_put_device_into_deep_standby_(struct pt_core_data *cd)
  7824. {
  7825. int rc = 0;
  7826. rc = pt_hid_cmd_set_power_(cd, HID_POWER_STANDBY);
  7827. if (rc)
  7828. rc = -EBUSY;
  7829. return rc;
  7830. }
  7831. /*******************************************************************************
  7832. * FUNCTION: pt_core_poweroff_device_
  7833. *
  7834. * SUMMARY: Disable IRQ and HW power down the device.
  7835. *
  7836. * RETURN:
  7837. * 0 = success
  7838. * !0 = failure
  7839. *
  7840. * PARAMETERS:
  7841. * *cd - pointer to core data
  7842. ******************************************************************************/
  7843. static int pt_core_poweroff_device_(struct pt_core_data *cd)
  7844. {
  7845. int rc;
  7846. if (cd->irq_enabled) {
  7847. cd->irq_enabled = false;
  7848. disable_irq_nosync(cd->irq);
  7849. }
  7850. rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, 0);
  7851. if (rc < 0)
  7852. pt_debug(cd->dev, DL_ERROR, "%s: HW Power down fails r=%d\n",
  7853. __func__, rc);
  7854. return rc;
  7855. }
  7856. /*******************************************************************************
  7857. * FUNCTION: pt_core_sleep_
  7858. *
  7859. * SUMMARY: Suspend the device with power off or deep sleep based on the
  7860. * configuration in the core platform data structure.
  7861. *
  7862. * RETURN:
  7863. * 0 = success
  7864. * !0 = failure
  7865. *
  7866. * PARAMETERS:
  7867. * *cd - pointer to core data
  7868. ******************************************************************************/
  7869. static int pt_core_sleep_(struct pt_core_data *cd)
  7870. {
  7871. int rc = 0;
  7872. /*
  7873. * Do nothing if system already sleeping or in progress of
  7874. * entering sleep. Proceed if awake or waking.
  7875. */
  7876. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_SLEEPING)
  7877. goto exit;
  7878. mutex_lock(&cd->system_lock);
  7879. cd->sleep_state = SS_SLEEPING;
  7880. mutex_unlock(&cd->system_lock);
  7881. /* Ensure watchdog and startup works stopped */
  7882. pt_stop_wd_timer(cd);
  7883. cancel_work_sync(&cd->enum_work);
  7884. pt_stop_wd_timer(cd);
  7885. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  7886. rc = pt_put_device_into_easy_wakeup_(cd);
  7887. else if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP)
  7888. rc = pt_core_poweroff_device_(cd);
  7889. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  7890. rc = pt_put_device_into_deep_standby_(cd);
  7891. else
  7892. rc = pt_put_device_into_deep_sleep_(cd);
  7893. mutex_lock(&cd->system_lock);
  7894. if (rc == 0) {
  7895. cd->sleep_state = SS_SLEEP_ON;
  7896. } else {
  7897. cd->sleep_state = SS_SLEEP_OFF;
  7898. pt_start_wd_timer(cd);
  7899. }
  7900. mutex_unlock(&cd->system_lock);
  7901. exit:
  7902. return rc;
  7903. }
  7904. /*******************************************************************************
  7905. * FUNCTION: pt_core_sleep
  7906. *
  7907. * SUMMARY: Protected call to pt_core_sleep_ by exclusive access to the DUT.
  7908. *
  7909. * RETURN:
  7910. * 0 = success
  7911. * !0 = failure
  7912. *
  7913. * PARAMETERS:
  7914. * *cd - pointer to core data
  7915. ******************************************************************************/
  7916. static int pt_core_sleep(struct pt_core_data *cd)
  7917. {
  7918. int rc = 0;
  7919. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  7920. if (rc < 0) {
  7921. pt_debug(cd->dev, DL_ERROR,
  7922. "%s: fail get exclusive ex=%p own=%p\n",
  7923. __func__, cd->exclusive_dev, cd->dev);
  7924. return rc;
  7925. }
  7926. rc = pt_core_sleep_(cd);
  7927. if (release_exclusive(cd, cd->dev) < 0)
  7928. pt_debug(cd->dev, DL_ERROR,
  7929. "%s: fail to release exclusive\n", __func__);
  7930. else
  7931. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  7932. __func__);
  7933. return rc;
  7934. }
  7935. /*******************************************************************************
  7936. * FUNCTION: pt_wakeup_host
  7937. *
  7938. * SUMMARY: Check wake up report and call the PT_ATTEN_WAKE attention list.
  7939. *
  7940. * NOTE: TSG5 EasyWake and TSG6 EasyWake use different protocol.
  7941. *
  7942. * RETURN:
  7943. * 0 = success
  7944. * !0 = failure
  7945. *
  7946. * PARAMETERS:
  7947. * *cd - pointer to core data
  7948. ******************************************************************************/
  7949. static int pt_wakeup_host(struct pt_core_data *cd)
  7950. {
  7951. #ifndef EASYWAKE_TSG6
  7952. /* TSG5 EasyWake */
  7953. int rc = 0;
  7954. int event_id;
  7955. int size = get_unaligned_le16(&cd->input_buf[0]);
  7956. /* Validate report */
  7957. if (size != 4 || cd->input_buf[2] != 4)
  7958. rc = -EINVAL;
  7959. event_id = cd->input_buf[3];
  7960. pt_debug(cd->dev, DL_INFO, "%s: e=%d, rc=%d\n",
  7961. __func__, event_id, rc);
  7962. if (rc) {
  7963. pt_core_sleep_(cd);
  7964. goto exit;
  7965. }
  7966. /* attention WAKE */
  7967. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7968. exit:
  7969. return rc;
  7970. #else
  7971. /* TSG6 FW1.3 EasyWake */
  7972. int rc = 0;
  7973. int i = 0;
  7974. int report_length;
  7975. /* Validate report */
  7976. if (cd->input_buf[2] != PT_PIP_WAKEUP_REPORT_ID) {
  7977. rc = -EINVAL;
  7978. pt_core_sleep_(cd);
  7979. goto exit;
  7980. }
  7981. /* Get gesture id and gesture data length */
  7982. cd->gesture_id = cd->input_buf[3];
  7983. report_length = (cd->input_buf[1] << 8) | (cd->input_buf[0]);
  7984. cd->gesture_data_length = report_length - 4;
  7985. pt_debug(cd->dev, DL_INFO,
  7986. "%s: gesture_id = %d, gesture_data_length = %d\n",
  7987. __func__, cd->gesture_id, cd->gesture_data_length);
  7988. for (i = 0; i < cd->gesture_data_length; i++)
  7989. cd->gesture_data[i] = cd->input_buf[4 + i];
  7990. /* attention WAKE */
  7991. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7992. exit:
  7993. return rc;
  7994. #endif
  7995. }
  7996. /*******************************************************************************
  7997. * FUNCTION: pt_get_touch_field
  7998. *
  7999. * SUMMARY: Function to calculate touch fields. The field refers to each element
  8000. * in the input report, such as "Number of Records", "X-axis". This function
  8001. * can calculate the value of element based on the bit offset, size and the
  8002. * max value of the element.
  8003. *
  8004. * PARAMETERS:
  8005. * *dev - pointer to device structure
  8006. * *field - pointer to field calculation result
  8007. * size - size in bytes
  8008. * max - max value of result
  8009. * *data - pointer to input data to be parsed
  8010. * bofs - bit offset
  8011. ******************************************************************************/
  8012. void pt_get_touch_field(struct device *dev,
  8013. int *field, int size, int max, u8 *data, int bofs)
  8014. {
  8015. int nbyte;
  8016. int next;
  8017. for (nbyte = 0, *field = 0, next = 0; nbyte < size; nbyte++) {
  8018. pt_debug(dev, DL_DEBUG,
  8019. "%s: *field=%02X(%d) size=%d max=%08X data=%p data[%d]=%02X(%d) bofs=%d\n",
  8020. __func__, *field, *field, size, max, data, next,
  8021. data[next], data[next], bofs);
  8022. *field = *field + ((data[next] >> bofs) << (nbyte * 8));
  8023. next++;
  8024. }
  8025. if (max > 0)
  8026. *field &= max - 1;
  8027. pt_debug(dev, DL_DEBUG,
  8028. "%s: *field=%02X(%d) size=%d max=%08X data=%p data[%d]=%02X(%d)\n",
  8029. __func__, *field, *field, size, max, data, next,
  8030. data[next], data[next]);
  8031. }
  8032. /*******************************************************************************
  8033. * FUNCTION: move_tracking_heatmap_data
  8034. *
  8035. * SUMMARY: Move the valid tracking heatmap data from the input buffer into the
  8036. * system information structure, xy_mode and xy_data.
  8037. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  8038. * the tthe_tuner sysfs node under the label "THM"
  8039. *
  8040. * RETURN:
  8041. * 0 = success
  8042. * !0 = failure
  8043. *
  8044. * PARAMETERS:
  8045. * *cd - pointer to core data
  8046. * *si - pointer to the system information structure
  8047. ******************************************************************************/
  8048. static int move_tracking_heatmap_data(struct pt_core_data *cd,
  8049. struct pt_sysinfo *si)
  8050. {
  8051. #ifdef TTHE_TUNER_SUPPORT
  8052. int size = get_unaligned_le16(&cd->input_buf[0]);
  8053. if (size)
  8054. tthe_print(cd, cd->input_buf, size, "THM=");
  8055. #endif
  8056. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  8057. return 0;
  8058. }
  8059. /*******************************************************************************
  8060. * FUNCTION: move_sensor_data
  8061. *
  8062. * SUMMARY: Move the valid sensor data from the input buffer into the system
  8063. * information structure, xy_mode and xy_data.
  8064. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  8065. * the tthe_tuner sysfs node under the label "sensor_monitor"
  8066. *
  8067. * RETURN:
  8068. * 0 = success
  8069. * !0 = failure
  8070. *
  8071. * PARAMETERS:
  8072. * *cd - pointer to core data
  8073. * *si - pointer to the system information structure
  8074. ******************************************************************************/
  8075. static int move_sensor_data(struct pt_core_data *cd,
  8076. struct pt_sysinfo *si)
  8077. {
  8078. #ifdef TTHE_TUNER_SUPPORT
  8079. int size = get_unaligned_le16(&cd->input_buf[0]);
  8080. if (size)
  8081. tthe_print(cd, cd->input_buf, size, "sensor_monitor=");
  8082. #endif
  8083. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  8084. return 0;
  8085. }
  8086. /*******************************************************************************
  8087. * FUNCTION: move_button_data
  8088. *
  8089. * SUMMARY: Move the valid button data from the input buffer into the system
  8090. * information structure, xy_mode and xy_data.
  8091. * - If TTHE_TUNER_SUPPORT is defined print the raw button data into
  8092. * the tthe_tuner sysfs node under the label "OpModeData"
  8093. *
  8094. * RETURN:
  8095. * 0 = success
  8096. * !0 = failure
  8097. *
  8098. * PARAMETERS:
  8099. * *cd - pointer to core data
  8100. * *si - pointer to the system information structure
  8101. ******************************************************************************/
  8102. static int move_button_data(struct pt_core_data *cd,
  8103. struct pt_sysinfo *si)
  8104. {
  8105. #ifdef TTHE_TUNER_SUPPORT
  8106. int size = get_unaligned_le16(&cd->input_buf[0]);
  8107. if (size)
  8108. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  8109. #endif
  8110. memcpy(si->xy_mode, cd->input_buf, BTN_INPUT_HEADER_SIZE);
  8111. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode, BTN_INPUT_HEADER_SIZE,
  8112. "xy_mode");
  8113. memcpy(si->xy_data, &cd->input_buf[BTN_INPUT_HEADER_SIZE],
  8114. BTN_REPORT_SIZE);
  8115. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, BTN_REPORT_SIZE,
  8116. "xy_data");
  8117. return 0;
  8118. }
  8119. /*******************************************************************************
  8120. * FUNCTION: move_touch_data_pip
  8121. *
  8122. * SUMMARY: Move the valid touch data from the input buffer into the system
  8123. * information structure, xy_mode and xy_data.
  8124. * - If TTHE_TUNER_SUPPORT is defined print the raw touch data into
  8125. * the tthe_tuner sysfs node under the label "OpModeData"
  8126. *
  8127. * RETURN:
  8128. * 0 = success
  8129. * !0 = failure
  8130. *
  8131. * PARAMETERS:
  8132. * *cd - pointer to core data
  8133. * *si - pointer to the system information structure
  8134. ******************************************************************************/
  8135. static int move_touch_data_pip(struct pt_core_data *cd, struct pt_sysinfo *si)
  8136. {
  8137. int max_tch = si->sensing_conf_data.max_tch;
  8138. int num_cur_tch = 0;
  8139. int length;
  8140. int actual_tch_num;
  8141. struct pt_tch_abs_params *tch = &si->tch_hdr[PT_TCH_NUM];
  8142. int size = get_unaligned_le16(&cd->input_buf[0]);
  8143. #ifdef TTHE_TUNER_SUPPORT
  8144. if (size > 0)
  8145. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  8146. #endif
  8147. memcpy(si->xy_mode, cd->input_buf, si->desc.tch_header_size);
  8148. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode,
  8149. si->desc.tch_header_size, "xy_mode");
  8150. pt_get_touch_field(cd->dev, &num_cur_tch, tch->size,
  8151. tch->max, si->xy_mode + 3 + tch->ofs, tch->bofs);
  8152. if (unlikely(num_cur_tch > max_tch))
  8153. num_cur_tch = max_tch;
  8154. actual_tch_num = (size - si->desc.tch_header_size)
  8155. / si->desc.tch_record_size;
  8156. if (actual_tch_num < num_cur_tch) {
  8157. pt_debug(cd->dev, DL_ERROR,
  8158. "%s: ATM - Malformed touch packet. actual_tch_num=%d, num_cur_tch=%d\n",
  8159. __func__, actual_tch_num, num_cur_tch);
  8160. num_cur_tch = actual_tch_num;
  8161. }
  8162. length = num_cur_tch * si->desc.tch_record_size;
  8163. memcpy(si->xy_data, &cd->input_buf[si->desc.tch_header_size], length);
  8164. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, length, "xy_data");
  8165. return 0;
  8166. }
  8167. /*******************************************************************************
  8168. * FUNCTION: move_touch_data_hid
  8169. *
  8170. * SUMMARY: Move the valid touch data from the input buffer into the system
  8171. * information structure, xy_mode and xy_data.
  8172. * - If TTHE_TUNER_SUPPORT is defined print the raw touch data into
  8173. * the tthe_tuner sysfs node under the label "HID-USB", "HID-I2C" or
  8174. * "OpModeData"
  8175. *
  8176. * RETURN:
  8177. * 0 = success
  8178. * !0 = failure
  8179. *
  8180. * PARAMETERS:
  8181. * *cd - pointer to core data
  8182. * *si - pointer to the system information structure
  8183. ******************************************************************************/
  8184. static int move_touch_data_hid(struct pt_core_data *cd,
  8185. struct pt_sysinfo *si)
  8186. {
  8187. int max_tch = si->sensing_conf_data.max_tch;
  8188. int num_cur_tch = 0;
  8189. u16 hdr_sz;
  8190. u16 rec_sz;
  8191. int max_tch_per_packet;
  8192. static u8 remain_tch;
  8193. static u8 packet_no;
  8194. static u8 input_sz;
  8195. int length;
  8196. int rc = 0;
  8197. struct pt_tch_abs_params *tch = &si->tch_hdr[PT_TCH_NUM];
  8198. int size = get_unaligned_le16(&cd->input_buf[0]);
  8199. hdr_sz = si->desc.tch_header_size;
  8200. rec_sz = si->desc.tch_record_size;
  8201. max_tch_per_packet = si->desc.max_tch_per_packet;
  8202. #ifdef TTHE_TUNER_SUPPORT
  8203. length = hdr_sz + rec_sz * max_tch_per_packet;
  8204. pt_debug(cd->dev, DL_DEBUG,
  8205. "%s: touch report size=%d, len=%d, record=%d, max_tch=%d\n",
  8206. __func__, size, length, rec_sz, max_tch_per_packet);
  8207. /*
  8208. * HID over USB does not require the two byte length field, so
  8209. * this should print from input_buf[2] but to keep both finger
  8210. * and pen reports the same the length is included
  8211. */
  8212. if (cd->tthe_hid_usb_format == PT_TTHE_TUNER_FORMAT_HID_USB)
  8213. tthe_print(cd, &(cd->input_buf[2]), length - 2,
  8214. "HID-USB=");
  8215. else if (cd->tthe_hid_usb_format ==
  8216. PT_TTHE_TUNER_FORMAT_HID_I2C)
  8217. tthe_print(cd, &(cd->input_buf[0]), length,
  8218. "HID-I2C=");
  8219. #endif
  8220. memcpy(si->xy_mode, cd->input_buf, hdr_sz);
  8221. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode,
  8222. hdr_sz, "xy_mode");
  8223. /*
  8224. * The fifth parameter points to the location of "Number of Records":
  8225. * Pointer "xy_mode" points to start address of touch report header.
  8226. * The byte offset of "Number of Records" is 2 (tch->ofs, retrieved
  8227. * from report descriptor). Then the pointer "xy_mode + 3 (skip the
  8228. * two bytes length and 1 byte report ID) + tch->ofs" points to the
  8229. * location of "Number of Records".
  8230. */
  8231. pt_get_touch_field(cd->dev, &num_cur_tch, tch->size,
  8232. tch->max, si->xy_mode + 3 + tch->ofs, tch->bofs);
  8233. if (unlikely(num_cur_tch > max_tch))
  8234. num_cur_tch = max_tch;
  8235. /* According to Hybrid Mode touch report defined by Microsoft,
  8236. * if the touch number exceeds the max touch per packet (defined
  8237. * in report descriptor), the touch packet will be broken up into
  8238. * to multiple reports. Timestamp will be consistent across all
  8239. * touches in a single frame. The "Number of records" will have the
  8240. * total in the first report and be 0 in all subsequent reports that
  8241. * belong to the same frame.
  8242. */
  8243. pt_debug(cd->dev, DL_INFO, "%s: max_tch=%d, packet_no=%d, num_cur_tch=%d\n",
  8244. __func__, max_tch, packet_no, num_cur_tch);
  8245. if (packet_no == 0) {
  8246. input_sz = hdr_sz + num_cur_tch * rec_sz;
  8247. memset(cd->touch_buf, 0, sizeof(cd->touch_buf));
  8248. memcpy(cd->touch_buf, cd->input_buf, input_sz);
  8249. if (num_cur_tch > max_tch_per_packet) {
  8250. remain_tch = num_cur_tch - max_tch_per_packet;
  8251. packet_no++;
  8252. rc = -EINVAL;
  8253. goto exit;
  8254. }
  8255. } else {
  8256. if (remain_tch <= max_tch_per_packet) {
  8257. memcpy(&cd->touch_buf[hdr_sz +
  8258. max_tch_per_packet * packet_no * rec_sz],
  8259. &(cd->input_buf[hdr_sz]),
  8260. rec_sz * remain_tch);
  8261. remain_tch = 0;
  8262. packet_no = 0;
  8263. rc = 0;
  8264. } else {
  8265. memcpy(&cd->touch_buf[hdr_sz +
  8266. max_tch_per_packet * packet_no * rec_sz],
  8267. &(cd->input_buf[hdr_sz]),
  8268. rec_sz * max_tch_per_packet);
  8269. remain_tch -= max_tch_per_packet;
  8270. packet_no++;
  8271. rc = -EINVAL;
  8272. goto exit;
  8273. }
  8274. }
  8275. #ifdef TTHE_TUNER_SUPPORT
  8276. /* Update pip packet length */
  8277. cd->touch_buf[0] = input_sz & 0xff;
  8278. cd->touch_buf[1] = (input_sz & 0xff00) >> 8;
  8279. /*
  8280. * For PT_TTHE_TUNER_FORMAT_HID_FINGER_TO_PIP and
  8281. * PT_TTHE_TUNER_FORMAT_HID_FINGER_AND_PEN_TO_PIP mode, all touches
  8282. * should be combined into a single row for the tthe_tuner node.
  8283. */
  8284. if (cd->tthe_hid_usb_format ==
  8285. PT_TTHE_TUNER_FORMAT_HID_FINGER_TO_PIP ||
  8286. cd->tthe_hid_usb_format ==
  8287. PT_TTHE_TUNER_FORMAT_HID_FINGER_AND_PEN_TO_PIP)
  8288. tthe_print(cd, cd->touch_buf, input_sz,
  8289. "OpModeData=");
  8290. #endif
  8291. memcpy(si->xy_data, &cd->touch_buf[hdr_sz], input_sz - hdr_sz);
  8292. pt_pr_buf(cd->dev, DL_INFO,
  8293. (u8 *)si->xy_data, input_sz - hdr_sz, "xy_data");
  8294. input_sz = 0;
  8295. exit:
  8296. return rc;
  8297. }
  8298. /*******************************************************************************
  8299. * FUNCTION: move_hid_pen_data
  8300. *
  8301. * SUMMARY: TODO Move the valid pen data from the input buffer into the system
  8302. * information structure, xy_mode and xy_data.
  8303. * - If TTHE_TUNER_SUPPORT is defined print the raw pen data into
  8304. * the tthe_tuner sysfs node under the label "HID" starting with the
  8305. * report ID.
  8306. *
  8307. * RETURN:
  8308. * 0 = success
  8309. * !0 = failure
  8310. *
  8311. * PARAMETERS:
  8312. * *cd - pointer to core data
  8313. * *si - pointer to the system information structure
  8314. ******************************************************************************/
  8315. static int move_hid_pen_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  8316. {
  8317. int size = get_unaligned_le16(&cd->input_buf[0]);
  8318. #ifdef TTHE_TUNER_SUPPORT
  8319. enum pt_pen_abs abs;
  8320. struct pt_pen pen;
  8321. int packet_len = 17;
  8322. int report_id = 1;
  8323. static int report_counter;
  8324. int event_id = 2;
  8325. int touch_id = 0;
  8326. int i = 0;
  8327. if (size) {
  8328. /*
  8329. * HID over USB does not require the two byte length field, so
  8330. * this should print from input_buf[2] but to keep both finger
  8331. * and pen reports the same the length is included
  8332. */
  8333. if (cd->tthe_hid_usb_format == PT_TTHE_TUNER_FORMAT_HID_USB)
  8334. tthe_print(cd, &(cd->input_buf[2]), size - 2,
  8335. "HID-USB=");
  8336. else if (cd->tthe_hid_usb_format ==
  8337. PT_TTHE_TUNER_FORMAT_HID_I2C ||
  8338. cd->tthe_hid_usb_format ==
  8339. PT_TTHE_TUNER_FORMAT_HID_FINGER_TO_PIP)
  8340. tthe_print(cd, &(cd->input_buf[0]), size,
  8341. "HID-I2C=");
  8342. else if (cd->tthe_hid_usb_format ==
  8343. PT_TTHE_TUNER_FORMAT_HID_FINGER_AND_PEN_TO_PIP) {
  8344. memset(cd->touch_buf, 0, sizeof(cd->touch_buf));
  8345. for (abs = PT_PEN_X; abs < PT_PEN_NUM_ABS; abs++) {
  8346. if (!si->pen_abs[abs].report)
  8347. continue;
  8348. pt_get_touch_field(cd->dev, &pen.abs[abs],
  8349. si->pen_abs[abs].size,
  8350. si->pen_abs[abs].max,
  8351. cd->input_buf + 3 +
  8352. si->pen_abs[abs].ofs,
  8353. si->pen_abs[abs].bofs);
  8354. pt_debug(cd->dev, DL_DEBUG, "%s: get %s=%04X(%d)\n",
  8355. __func__, pt_pen_abs_string[abs],
  8356. pen.abs[abs], pen.abs[abs]);
  8357. }
  8358. /* pip packet length: 17 */
  8359. cd->touch_buf[i++] = packet_len & 0xff;
  8360. cd->touch_buf[i++] = (packet_len & 0xff00) >> 8;
  8361. /* pip finger report id: 1*/
  8362. cd->touch_buf[i++] = report_id;
  8363. /* Timestamp: 0 */
  8364. cd->touch_buf[i++] = 0;
  8365. cd->touch_buf[i++] = 0;
  8366. /* LO: 0; Number of Records: 1*/
  8367. cd->touch_buf[i++] = 1;
  8368. /* Report Counter:[0-3]; Noise Effects:0 */
  8369. cd->touch_buf[i++] = (report_counter & 0x03) << 6;
  8370. /* Touch Type: Stylus (2)*/
  8371. cd->touch_buf[i++] = PT_OBJ_STYLUS;
  8372. /* Tip: pen tip switch; Event ID: 2; Touch ID: 0 */
  8373. cd->touch_buf[i++] =
  8374. ((pen.abs[PT_PEN_TS] & 0x01) << 7) |
  8375. ((event_id & 0x03) << 5) |
  8376. (touch_id & 0x1f);
  8377. /* X: Pen X */
  8378. cd->touch_buf[i++] = pen.abs[PT_PEN_X] & 0xff;
  8379. cd->touch_buf[i++] = (pen.abs[PT_PEN_X] & 0xff00) >> 8;
  8380. /* Y: Pen Y */
  8381. cd->touch_buf[i++] = pen.abs[PT_PEN_Y] & 0xff;
  8382. cd->touch_buf[i++] = (pen.abs[PT_PEN_Y] & 0xff00) >> 8;
  8383. /* Pressure: Pen pressure drop the least 4 bits */
  8384. cd->touch_buf[i++] = (pen.abs[PT_PEN_P] & 0xff0) >> 4;
  8385. /* Major: Pen Tilt_X*/
  8386. cd->touch_buf[i++] = pen.abs[PT_PEN_X_TILT] & 0xff;
  8387. /* Minor: Pen Tilt_Y*/
  8388. cd->touch_buf[i++] = pen.abs[PT_PEN_Y_TILT] & 0xff;
  8389. /* Orientation: Btn2, Btn1*/
  8390. cd->touch_buf[i++] =
  8391. (pen.abs[PT_PEN_BS] & 0x01) |
  8392. ((pen.abs[PT_PEN_2ND_BS] & 0x01) << 1);
  8393. if (++report_counter > 3)
  8394. report_counter = 0;
  8395. tthe_print(cd, cd->touch_buf, packet_len,
  8396. "OpModeData=");
  8397. }
  8398. }
  8399. #endif
  8400. memcpy(si->xy_data, cd->input_buf, size);
  8401. pt_pr_buf(cd->dev, DL_INFO, (u8 *)&(cd->input_buf[0]), size, "HID Pen");
  8402. return 0;
  8403. }
  8404. /*******************************************************************************
  8405. * FUNCTION: parse_touch_input
  8406. *
  8407. * SUMMARY: Parse the touch report and take action based on the touch
  8408. * report_id.
  8409. *
  8410. * RETURN:
  8411. * 0 = success
  8412. * !0 = failure
  8413. *
  8414. * PARAMETERS:
  8415. * *cd - pointer to core data
  8416. * size - size of touch record
  8417. ******************************************************************************/
  8418. static int parse_touch_input(struct pt_core_data *cd, int size)
  8419. {
  8420. struct pt_sysinfo *si = &cd->sysinfo;
  8421. int report_id = cd->input_buf[2];
  8422. int rc = -EINVAL;
  8423. pt_debug(cd->dev, DL_DEBUG, "%s: Received touch report\n",
  8424. __func__);
  8425. if (!si->ready) {
  8426. pt_debug(cd->dev, DL_ERROR,
  8427. "%s: Need system information to parse touches\n",
  8428. __func__);
  8429. return 0;
  8430. }
  8431. if (!si->xy_mode || !si->xy_data)
  8432. return rc;
  8433. if (report_id == PT_PIP_TOUCH_REPORT_ID ||
  8434. report_id == PT_HID_VS_FINGER_REPORT_ID) {
  8435. if (cd->dut_status.protocol_mode == PT_PROTOCOL_MODE_PIP)
  8436. rc = move_touch_data_pip(cd, si);
  8437. else {
  8438. rc = move_touch_data_hid(cd, si);
  8439. if (rc) {
  8440. pt_debug(cd->dev, DL_INFO,
  8441. "%s: Skip report for the first touch packet\n",
  8442. __func__);
  8443. return 0;
  8444. }
  8445. }
  8446. } else if ((report_id == PT_HID_PEN_REPORT_ID ||
  8447. report_id == PT_HID_VS_PEN_REPORT_ID) &&
  8448. cd->dut_status.protocol_mode == PT_PROTOCOL_MODE_HID)
  8449. rc = move_hid_pen_data(cd, si);
  8450. else if (report_id == PT_PIP_CAPSENSE_BTN_REPORT_ID)
  8451. rc = move_button_data(cd, si);
  8452. else if (report_id == PT_PIP_SENSOR_DATA_REPORT_ID)
  8453. rc = move_sensor_data(cd, si);
  8454. else if (report_id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  8455. rc = move_tracking_heatmap_data(cd, si);
  8456. if (rc)
  8457. return rc;
  8458. /* attention IRQ */
  8459. call_atten_cb(cd, PT_ATTEN_IRQ, cd->mode);
  8460. return 0;
  8461. }
  8462. /*******************************************************************************
  8463. * FUNCTION: parse_command_input
  8464. *
  8465. * SUMMARY: Move the response data from the input buffer to the response buffer
  8466. *
  8467. * RETURN:
  8468. * 0 = success
  8469. * !0 = failure
  8470. *
  8471. * PARAMETERS:
  8472. * *cd - pointer to core data
  8473. * size - size of response data
  8474. ******************************************************************************/
  8475. static int parse_command_input(struct pt_core_data *cd, int size)
  8476. {
  8477. pt_debug(cd->dev, DL_DEBUG, "%s: Received cmd interrupt\n",
  8478. __func__);
  8479. memcpy(cd->response_buf, cd->input_buf, size);
  8480. #if defined(TTHE_TUNER_SUPPORT) && defined(TTDL_DIAGNOSTICS)
  8481. if (size && cd->show_tt_data) {
  8482. if (cd->pip2_prot_active)
  8483. tthe_print(cd, cd->input_buf, size, "TT_DATA_PIP2=");
  8484. else
  8485. tthe_print(cd, cd->input_buf, size, "TT_DATA=");
  8486. }
  8487. #endif
  8488. mutex_lock(&cd->system_lock);
  8489. cd->hid_cmd_state = 0;
  8490. mutex_unlock(&cd->system_lock);
  8491. wake_up(&cd->wait_q);
  8492. return 0;
  8493. }
  8494. /*******************************************************************************
  8495. * FUNCTION: pt_allow_enumeration
  8496. *
  8497. * SUMMARY: Determine if an enumeration or fully re-probe should perform when
  8498. * FW sentinel is seen.
  8499. *
  8500. * RETURN:
  8501. * true = allow enumeration or fully re-probe
  8502. * false = skip enumeration and fully re-probe
  8503. *
  8504. * PARAMETERS:
  8505. * *cd - pointer to core data
  8506. ******************************************************************************/
  8507. static inline bool pt_allow_enumeration(struct pt_core_data *cd)
  8508. {
  8509. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  8510. (!cd->hid_reset_cmd_state) &&
  8511. (cd->core_probe_complete) &&
  8512. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  8513. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  8514. (cd->mode == PT_MODE_OPERATIONAL)) {
  8515. return true;
  8516. }
  8517. if ((!cd->hid_reset_cmd_state) &&
  8518. (cd->core_probe_complete) &&
  8519. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  8520. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  8521. (cd->active_dut_generation != DUT_PIP1_ONLY)) {
  8522. return true;
  8523. }
  8524. pt_debug(cd->dev, DL_INFO,
  8525. "%s: Dissallow - %s=%d %s=%d %s=0x%02X %s=%d\n",
  8526. __func__,
  8527. "hid_reset_cmd_state(0)", cd->hid_reset_cmd_state,
  8528. "core_probe_complete(1)", cd->core_probe_complete,
  8529. "hid_cmd_state(Not 0x02 or 0x39)", cd->hid_cmd_state,
  8530. "active_dut_gen(0,2)", cd->active_dut_generation);
  8531. return false;
  8532. }
  8533. /*******************************************************************************
  8534. * FUNCTION: pt_is_touch_report
  8535. *
  8536. * SUMMARY: Determine if a report ID should be treated as a touch report
  8537. *
  8538. * RETURN:
  8539. * true = report ID is a touch report
  8540. * false = report ID is not a touch report
  8541. *
  8542. * PARAMETERS:
  8543. * id - Report ID
  8544. ******************************************************************************/
  8545. static bool pt_is_touch_report(int id)
  8546. {
  8547. if (id == PT_PIP_TOUCH_REPORT_ID ||
  8548. id == PT_HID_PEN_REPORT_ID ||
  8549. id == PT_HID_VS_FINGER_REPORT_ID ||
  8550. id == PT_HID_VS_PEN_REPORT_ID ||
  8551. id == PT_PIP_CAPSENSE_BTN_REPORT_ID ||
  8552. id == PT_PIP_SENSOR_DATA_REPORT_ID ||
  8553. id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  8554. return true;
  8555. else
  8556. return false;
  8557. }
  8558. /*******************************************************************************
  8559. * FUNCTION: pt_parse_input
  8560. *
  8561. * SUMMARY: Parse the input data read from DUT due to IRQ. Handle data based
  8562. * on if its a response to a command or asynchronous touch data / reset
  8563. * sentinel. PIP2.x messages have additional error checking that is
  8564. * parsed (SEQ match from cmd to rsp, CRC valid).
  8565. * Look for special packets based on unique lengths:
  8566. * 0 bytes - APP(FW) reset sentinel or Gen5/6 BL sentinel
  8567. * 2 bytes - Empty buffer (PIP 1.7 and earlier)
  8568. * 11 bytes - possible PIP2.x reset sentinel (TAG and SEQ must = 0)
  8569. * 0xFFXX - Empty buffer (PIP 1.7+)
  8570. * Queue a startup after any asynchronous FW reset sentinel is seen, unless
  8571. * the initial probe has not yet been done.
  8572. *
  8573. * RETURN:
  8574. * 0 = success
  8575. * !0 = failure
  8576. *
  8577. * PARAMETERS:
  8578. * *cd - pointer to core data
  8579. ******************************************************************************/
  8580. static int pt_parse_input(struct pt_core_data *cd)
  8581. {
  8582. int report_id = 0;
  8583. int cmd_id;
  8584. int is_command = 0;
  8585. int size;
  8586. int print_size;
  8587. bool touch_report = true;
  8588. unsigned short calc_crc;
  8589. unsigned short resp_crc;
  8590. cd->fw_sys_mode_in_standby_state = false;
  8591. size = get_unaligned_le16(&cd->input_buf[0]);
  8592. print_size = size;
  8593. pt_debug(cd->dev, DL_DEBUG, "<<< %s: IRQ Triggered, read len [%d]\n",
  8594. __func__, print_size);
  8595. if (print_size <= PT_MAX_INPUT)
  8596. pt_pr_buf(cd->dev, DL_DEBUG, cd->input_buf, print_size,
  8597. "<<< Read buf");
  8598. if (size == 0 ||
  8599. (size == 11 &&
  8600. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] &
  8601. PIP2_RESP_SEQUENCE_MASK) == 0 &&
  8602. (cd->input_buf[PIP2_RESP_REPORT_ID_OFFSET] &
  8603. PIP2_CMD_COMMAND_ID_MASK) ==
  8604. PIP2_CMD_ID_STATUS)) {
  8605. touch_report = false;
  8606. cd->hw_detected = true;
  8607. cd->bl_pip_ver_ready = false;
  8608. cd->app_pip_ver_ready = false;
  8609. if (size == 0) {
  8610. mutex_lock(&cd->system_lock);
  8611. cd->pip2_prot_active = false;
  8612. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8613. /*
  8614. * For Gen5/6 this sentinel could be from
  8615. * the BL or FW. Attempt to set the correct
  8616. * mode based on the previous PIP command.
  8617. */
  8618. if (cd->hid_cmd_state ==
  8619. PIP1_BL_CMD_ID_LAUNCH_APP + 1) {
  8620. cd->mode = PT_MODE_OPERATIONAL;
  8621. cd->startup_status =
  8622. STARTUP_STATUS_FW_RESET_SENTINEL;
  8623. } else if (cd->hid_cmd_state ==
  8624. PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  8625. cd->hid_reset_cmd_state) {
  8626. cd->mode = PT_MODE_BOOTLOADER;
  8627. cd->startup_status =
  8628. STARTUP_STATUS_BL_RESET_SENTINEL;
  8629. } else {
  8630. cd->mode = PT_MODE_UNKNOWN;
  8631. cd->startup_status =
  8632. STARTUP_STATUS_START;
  8633. }
  8634. cd->fw_system_mode = FW_SYS_MODE_UNDEFINED;
  8635. pt_debug(cd->dev, DL_INFO,
  8636. "%s: ATM Gen5/6 %s sentinel received\n",
  8637. __func__,
  8638. (cd->mode == PT_MODE_OPERATIONAL ?
  8639. "FW" :
  8640. (cd->mode == PT_MODE_BOOTLOADER ?
  8641. "BL" : "Unknown")));
  8642. } else {
  8643. cd->mode = PT_MODE_OPERATIONAL;
  8644. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  8645. cd->startup_status =
  8646. STARTUP_STATUS_FW_RESET_SENTINEL;
  8647. pt_debug(cd->dev, DL_INFO,
  8648. "%s: ATM PT/TT FW sentinel received\n",
  8649. __func__);
  8650. }
  8651. mutex_unlock(&cd->system_lock);
  8652. if (pt_allow_enumeration(cd)) {
  8653. if (cd->active_dut_generation == DUT_UNKNOWN) {
  8654. pt_debug(cd->dev, DL_INFO,
  8655. "%s: Queue Restart\n", __func__);
  8656. pt_queue_restart(cd);
  8657. } else {
  8658. pt_debug(cd->dev, DL_INFO,
  8659. "%s: Queue Enum\n", __func__);
  8660. pt_queue_enum(cd);
  8661. }
  8662. } else {
  8663. pt_debug(cd->dev, DL_INFO,
  8664. "%s: Sentinel - No Queued Action\n",
  8665. __func__);
  8666. }
  8667. } else {
  8668. /* Sentinel must be from TT/TC BL */
  8669. mutex_lock(&cd->system_lock);
  8670. cd->pip2_prot_active = true;
  8671. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  8672. cd->mode = PT_MODE_BOOTLOADER;
  8673. cd->sysinfo.ready = false;
  8674. mutex_unlock(&cd->system_lock);
  8675. pt_debug(cd->dev, DL_INFO,
  8676. "%s: BL Reset sentinel received\n", __func__);
  8677. if (cd->flashless_dut &&
  8678. cd->flashless_auto_bl == PT_ALLOW_AUTO_BL) {
  8679. pt_debug(cd->dev, DL_INFO,
  8680. "%s: BL to RAM for flashless DUT\n",
  8681. __func__);
  8682. kthread_run(start_fw_upgrade, cd, "pt_loader");
  8683. }
  8684. }
  8685. mutex_lock(&cd->system_lock);
  8686. memcpy(cd->response_buf, cd->input_buf, 2);
  8687. if (!cd->hid_reset_cmd_state && !cd->hid_cmd_state) {
  8688. mutex_unlock(&cd->system_lock);
  8689. pt_debug(cd->dev, DL_WARN,
  8690. "%s: Device Initiated Reset\n", __func__);
  8691. wake_up(&cd->wait_q);
  8692. return 0;
  8693. }
  8694. cd->hid_reset_cmd_state = 0;
  8695. if (cd->hid_cmd_state == PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  8696. cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1 ||
  8697. cd->hid_cmd_state == PIP1_CMD_ID_USER_CMD + 1)
  8698. cd->hid_cmd_state = 0;
  8699. wake_up(&cd->wait_q);
  8700. mutex_unlock(&cd->system_lock);
  8701. return 0;
  8702. } else if (size == 2 || size >= PT_PIP_1P7_EMPTY_BUF) {
  8703. /*
  8704. * This debug message below is used by PBATS to calculate the
  8705. * time from the last lift off IRQ to when FW exits LFT mode.
  8706. */
  8707. touch_report = false;
  8708. pt_debug(cd->dev, DL_WARN,
  8709. "%s: DUT - Empty buffer detected\n", __func__);
  8710. return 0;
  8711. } else if (size > PT_MAX_INPUT) {
  8712. pt_debug(cd->dev, DL_ERROR,
  8713. "%s: DUT - Unexpected len field in active bus data!\n",
  8714. __func__);
  8715. return -EINVAL;
  8716. }
  8717. if (cd->pip2_prot_active) {
  8718. pt_debug(cd->dev, DL_DEBUG,
  8719. "%s: Decode PIP2.x Response\n", __func__);
  8720. /* PIP2 does not have a report id, hard code it */
  8721. report_id = 0x00;
  8722. cmd_id = cd->input_buf[PIP2_RESP_COMMAND_ID_OFFSET];
  8723. calc_crc = crc_ccitt_calculate(cd->input_buf, size - 2);
  8724. resp_crc = cd->input_buf[size - 2] << 8;
  8725. resp_crc |= cd->input_buf[size - 1];
  8726. if ((cd->pip2_cmd_tag_seq !=
  8727. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F)) &&
  8728. (resp_crc != calc_crc) &&
  8729. ((cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  8730. == PT_PIP_TOUCH_REPORT_ID) ||
  8731. (cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  8732. == PT_PIP_CAPSENSE_BTN_REPORT_ID))) {
  8733. report_id = cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET];
  8734. cmd_id = cd->input_buf[PIP1_RESP_COMMAND_ID_OFFSET];
  8735. pt_debug(cd->dev, DL_INFO,
  8736. "%s: %s %d %s\n", __func__,
  8737. "Received PIP1 report id =",
  8738. report_id,
  8739. "when expecting a PIP2 report");
  8740. } else {
  8741. is_command = 1;
  8742. touch_report = false;
  8743. }
  8744. } else {
  8745. report_id = cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET];
  8746. cmd_id = cd->input_buf[PIP1_RESP_COMMAND_ID_OFFSET];
  8747. }
  8748. #ifdef TTDL_DIAGNOSTICS
  8749. pt_debug(cd->dev, DL_DEBUG,
  8750. "%s: pip2 = %d report_id: 0x%02X, cmd_code: 0x%02X\n",
  8751. __func__, cd->pip2_prot_active, report_id,
  8752. (cmd_id & PIP2_CMD_COMMAND_ID_MASK));
  8753. #endif /* TTDL_DIAGNOSTICS */
  8754. if (report_id == PT_PIP_WAKEUP_REPORT_ID) {
  8755. pt_wakeup_host(cd);
  8756. #ifdef TTHE_TUNER_SUPPORT
  8757. tthe_print(cd, cd->input_buf, size, "TT_WAKEUP=");
  8758. #endif
  8759. return 0;
  8760. }
  8761. mod_timer_pending(&cd->watchdog_timer, jiffies +
  8762. msecs_to_jiffies(cd->watchdog_interval));
  8763. /*
  8764. * If it is PIP2 response, the report_id has been set to 0,
  8765. * so it will not be parsed as a touch packet.
  8766. */
  8767. if (!pt_is_touch_report(report_id)) {
  8768. is_command = 1;
  8769. touch_report = false;
  8770. }
  8771. if (unlikely(is_command)) {
  8772. parse_command_input(cd, size);
  8773. return 0;
  8774. }
  8775. if (touch_report)
  8776. parse_touch_input(cd, size);
  8777. return 0;
  8778. }
  8779. /*******************************************************************************
  8780. * FUNCTION: pt_read_input
  8781. *
  8782. * SUMMARY: Reads incoming data off of the active bus
  8783. *
  8784. * RETURN:
  8785. * 0 = success
  8786. * !0 = failure
  8787. *
  8788. * PARAMETERS:
  8789. * *cd - pointer to core data
  8790. ******************************************************************************/
  8791. static int pt_read_input(struct pt_core_data *cd)
  8792. {
  8793. struct device *dev = cd->dev;
  8794. int rc = 0;
  8795. int t;
  8796. int retry = PT_BUS_READ_INPUT_RETRY_COUNT;
  8797. /*
  8798. * Workaround for easywake failure
  8799. * Interrupt for easywake, wait for bus controller to wake
  8800. */
  8801. mutex_lock(&cd->system_lock);
  8802. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  8803. if (cd->sleep_state == SS_SLEEP_ON) {
  8804. mutex_unlock(&cd->system_lock);
  8805. if (!dev->power.is_suspended)
  8806. goto read;
  8807. t = wait_event_timeout(cd->wait_q,
  8808. (cd->wait_until_wake == 1),
  8809. msecs_to_jiffies(2000));
  8810. #ifdef TTDL_DIAGNOSTICS
  8811. if (IS_TMO(t)) {
  8812. cd->bus_transmit_error_count++;
  8813. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  8814. pt_debug(dev, DL_ERROR,
  8815. "%s: !!!I2C Transmission Error %d\n",
  8816. __func__,
  8817. cd->bus_transmit_error_count);
  8818. }
  8819. #else
  8820. if (IS_TMO(t))
  8821. pt_queue_enum(cd);
  8822. #endif /* TTDL_DIAGNOSTICS */
  8823. goto read;
  8824. }
  8825. }
  8826. mutex_unlock(&cd->system_lock);
  8827. read:
  8828. memset(cd->input_buf, 0, sizeof(cd->input_buf));
  8829. /* Try reading up to 'retry' times */
  8830. while (retry-- != 0) {
  8831. rc = pt_adap_read_default_nosize(cd, cd->input_buf,
  8832. PT_MAX_INPUT);
  8833. if (!rc) {
  8834. pt_debug(dev, DL_DEBUG,
  8835. "%s: Read input successfully\n", __func__);
  8836. goto read_exit;
  8837. }
  8838. usleep_range(1000, 2000);
  8839. }
  8840. pt_debug(dev, DL_ERROR,
  8841. "%s: Error getting report, rc=%d\n", __func__, rc);
  8842. read_exit:
  8843. return rc;
  8844. }
  8845. /*******************************************************************************
  8846. * FUNCTION: pt_irq
  8847. *
  8848. * SUMMARY: Process all detected interrupts
  8849. *
  8850. * RETURN:
  8851. * IRQ_HANDLED - Finished processing the interrupt
  8852. *
  8853. * PARAMETERS:
  8854. * irq - IRQ number
  8855. * *handle - pointer to core data struct
  8856. ******************************************************************************/
  8857. irqreturn_t pt_irq(int irq, void *handle)
  8858. {
  8859. struct pt_core_data *cd = handle;
  8860. int rc = 0;
  8861. #ifdef TTDL_PTVIRTDUT_SUPPORT
  8862. if (!cd->route_bus_virt_dut) {
  8863. if (!pt_check_irq_asserted(cd))
  8864. return IRQ_HANDLED;
  8865. }
  8866. #else
  8867. if (!pt_check_irq_asserted(cd))
  8868. return IRQ_HANDLED;
  8869. #endif /* TTDL_PTVIRTDUT_SUPPORT */
  8870. rc = pt_read_input(cd);
  8871. #ifdef TTDL_DIAGNOSTICS
  8872. cd->irq_count++;
  8873. /* Used to calculate T-Refresh */
  8874. if (cd->t_refresh_active) {
  8875. if (cd->t_refresh_count == 0) {
  8876. cd->t_refresh_time = jiffies;
  8877. cd->t_refresh_count++;
  8878. } else if (cd->t_refresh_count < cd->t_refresh_total) {
  8879. cd->t_refresh_count++;
  8880. } else {
  8881. cd->t_refresh_active = 0;
  8882. cd->t_refresh_time =
  8883. jiffies_to_msecs(jiffies - cd->t_refresh_time);
  8884. }
  8885. }
  8886. #endif /* TTDL_DIAGNOSTICS */
  8887. if (!rc)
  8888. pt_parse_input(cd);
  8889. return IRQ_HANDLED;
  8890. }
  8891. #ifdef PT_AUX_BRIDGE_ENABLED
  8892. /*******************************************************************************
  8893. * FUNCTION: pt_trigger_ttdl_irq
  8894. *
  8895. * SUMMARY: API for other kernel drivers to artificially trigger a TTDL IRQ
  8896. *
  8897. * IMPROVE - dpmux should likely call pt_get_core_data once and make a
  8898. * copy of cd so that the function only needs to be called once.
  8899. * RETURN:
  8900. * 0 = success
  8901. * !0 = failure
  8902. *
  8903. * PARAMETERS: none
  8904. *
  8905. ******************************************************************************/
  8906. int pt_trigger_ttdl_irq(void)
  8907. {
  8908. struct pt_core_data *cd;
  8909. char *core_name = PT_DEFAULT_CORE_ID;
  8910. cd = pt_get_core_data(core_name);
  8911. if (!cd) {
  8912. pr_err("%s: No Device\n", __func__);
  8913. return -ENODEV;
  8914. }
  8915. pt_irq(cd->irq, (void *)cd);
  8916. return 0;
  8917. }
  8918. EXPORT_SYMBOL_GPL(pt_trigger_ttdl_irq);
  8919. #endif
  8920. /*******************************************************************************
  8921. * FUNCTION: _pt_subscribe_attention
  8922. *
  8923. * SUMMARY: Function pointer included in core_cmds to allow other modules
  8924. * to subscribe themselves into the TTDL attention list
  8925. *
  8926. * RETURN:
  8927. * 0 = success
  8928. * !0 = failure
  8929. *
  8930. * PARAMETERS:
  8931. * *dev - pointer to device structure
  8932. * type - attention type enum
  8933. * *id - ID of the module calling this function
  8934. * *func - callback function pointer to be called when notified
  8935. * mode - attention mode
  8936. ******************************************************************************/
  8937. int _pt_subscribe_attention(struct device *dev,
  8938. enum pt_atten_type type, char *id, int (*func)(struct device *),
  8939. int mode)
  8940. {
  8941. struct pt_core_data *cd = dev_get_drvdata(dev);
  8942. struct atten_node *atten, *atten_new;
  8943. atten_new = kzalloc(sizeof(*atten_new), GFP_KERNEL);
  8944. if (!atten_new)
  8945. return -ENOMEM;
  8946. pt_debug(cd->dev, DL_INFO, "%s from '%s'\n", __func__,
  8947. dev_name(cd->dev));
  8948. spin_lock(&cd->spinlock);
  8949. list_for_each_entry(atten, &cd->atten_list[type], node) {
  8950. if (atten->id == id && atten->mode == mode) {
  8951. spin_unlock(&cd->spinlock);
  8952. kfree(atten_new);
  8953. pt_debug(cd->dev, DL_INFO, "%s: %s=%p %s=%d\n",
  8954. __func__,
  8955. "already subscribed attention",
  8956. dev, "mode", mode);
  8957. return 0;
  8958. }
  8959. }
  8960. atten_new->id = id;
  8961. atten_new->dev = dev;
  8962. atten_new->mode = mode;
  8963. atten_new->func = func;
  8964. list_add(&atten_new->node, &cd->atten_list[type]);
  8965. spin_unlock(&cd->spinlock);
  8966. return 0;
  8967. }
  8968. /*******************************************************************************
  8969. * FUNCTION: _pt_unsubscribe_attention
  8970. *
  8971. * SUMMARY: Function pointer included in core_cmds to allow other modules
  8972. * to unsubscribe themselves from the TTDL attention list
  8973. *
  8974. * RETURN:
  8975. * 0 = success
  8976. * !0 = failure
  8977. *
  8978. * PARAMETERS:
  8979. * *dev - pointer to device structure
  8980. * type - attention type enum
  8981. * *id - ID of the module calling this function
  8982. * *func - function pointer
  8983. * mode - attention mode
  8984. ******************************************************************************/
  8985. int _pt_unsubscribe_attention(struct device *dev,
  8986. enum pt_atten_type type, char *id, int (*func)(struct device *),
  8987. int mode)
  8988. {
  8989. struct pt_core_data *cd = dev_get_drvdata(dev);
  8990. struct atten_node *atten, *atten_n;
  8991. spin_lock(&cd->spinlock);
  8992. list_for_each_entry_safe(atten, atten_n, &cd->atten_list[type], node) {
  8993. if (atten->id == id && atten->mode == mode) {
  8994. list_del(&atten->node);
  8995. spin_unlock(&cd->spinlock);
  8996. kfree(atten);
  8997. pt_debug(cd->dev, DL_DEBUG, "%s: %s=%p %s=%d\n",
  8998. __func__,
  8999. "unsub for atten->dev", atten->dev,
  9000. "atten->mode", atten->mode);
  9001. return 0;
  9002. }
  9003. }
  9004. spin_unlock(&cd->spinlock);
  9005. return -ENODEV;
  9006. }
  9007. /*******************************************************************************
  9008. * FUNCTION: _pt_request_exclusive
  9009. *
  9010. * SUMMARY: Function pointer included in core_cmds to allow other modules
  9011. * to request exclusive access
  9012. *
  9013. * RETURN:
  9014. * 0 = success
  9015. * !0 = failure
  9016. *
  9017. * PARAMETERS:
  9018. * *dev - pointer to device structure
  9019. * timeout_ms - timeout to wait for exclusive access
  9020. ******************************************************************************/
  9021. static int _pt_request_exclusive(struct device *dev,
  9022. int timeout_ms)
  9023. {
  9024. struct pt_core_data *cd = dev_get_drvdata(dev);
  9025. return request_exclusive(cd, (void *)dev, timeout_ms);
  9026. }
  9027. /*******************************************************************************
  9028. * FUNCTION: _pt_release_exclusive
  9029. *
  9030. * SUMMARY: Function pointer included in core_cmds to allow other modules
  9031. * to release exclusive access
  9032. *
  9033. * RETURN:
  9034. * 0 = success
  9035. * !0 = failure
  9036. *
  9037. * PARAMETERS:
  9038. * *dev - pointer to device structure
  9039. ******************************************************************************/
  9040. static int _pt_release_exclusive(struct device *dev)
  9041. {
  9042. struct pt_core_data *cd = dev_get_drvdata(dev);
  9043. return release_exclusive(cd, (void *)dev);
  9044. }
  9045. /*******************************************************************************
  9046. * FUNCTION: _pt_request_reset
  9047. *
  9048. * SUMMARY: Function pointer included in core_cmds to allow other modules
  9049. * to request the DUT to be reset. Function returns err if refused or
  9050. * timeout occurs (Note: core uses fixed timeout period).
  9051. *
  9052. * NOTE: Function blocks until ISR occurs.
  9053. *
  9054. * RETURN:
  9055. * 0 = success
  9056. * !0 = failure
  9057. *
  9058. * PARAMETERS:
  9059. * *dev - pointer to device structure
  9060. * protect - flag to call protected or non-protected
  9061. ******************************************************************************/
  9062. static int _pt_request_reset(struct device *dev, int protect)
  9063. {
  9064. struct pt_core_data *cd = dev_get_drvdata(dev);
  9065. int rc;
  9066. rc = pt_dut_reset(cd, protect);
  9067. if (rc < 0) {
  9068. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  9069. __func__, rc);
  9070. }
  9071. return rc;
  9072. }
  9073. /*******************************************************************************
  9074. * FUNCTION: _pt_request_enum
  9075. *
  9076. * SUMMARY: Function pointer included in core_cmds to allow other modules
  9077. * to request TTDL to queue an enum. This function will return err
  9078. * if refused; if no error then enum has completed and system is in
  9079. * normal operation mode.
  9080. *
  9081. * RETURN:
  9082. * 0 = success
  9083. * !0 = failure
  9084. *
  9085. * PARAMETERS:
  9086. * *dev - pointer to device structure
  9087. * wait - boolean to determine if to wait for startup event
  9088. ******************************************************************************/
  9089. static int _pt_request_enum(struct device *dev, bool wait)
  9090. {
  9091. struct pt_core_data *cd = dev_get_drvdata(dev);
  9092. pt_queue_enum(cd);
  9093. if (wait)
  9094. wait_event_timeout(cd->wait_q,
  9095. cd->startup_state == STARTUP_NONE,
  9096. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  9097. return 0;
  9098. }
  9099. /*******************************************************************************
  9100. * FUNCTION: _pt_request_sysinfo
  9101. *
  9102. * SUMMARY: Function pointer included in core_cmds to allow other modules
  9103. * to request the pointer to the system information structure. This
  9104. * function will return NULL if sysinfo has not been acquired from the
  9105. * DUT yet.
  9106. *
  9107. * RETURN: Pointer to the system information struct
  9108. *
  9109. * PARAMETERS:
  9110. * *dev - pointer to device structure
  9111. ******************************************************************************/
  9112. struct pt_sysinfo *_pt_request_sysinfo(struct device *dev)
  9113. {
  9114. struct pt_core_data *cd = dev_get_drvdata(dev);
  9115. int rc = 0;
  9116. /* Attempt to get sysinfo if not ready and panel_id is from XY pin */
  9117. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  9118. !cd->sysinfo.ready) {
  9119. rc = pt_hid_output_get_sysinfo_(cd);
  9120. if (rc) {
  9121. pt_debug(cd->dev, DL_ERROR,
  9122. "%s: Error getting sysinfo rc=%d\n",
  9123. __func__, rc);
  9124. }
  9125. }
  9126. if (cd->sysinfo.ready)
  9127. return &cd->sysinfo;
  9128. return NULL;
  9129. }
  9130. /*******************************************************************************
  9131. * FUNCTION: _pt_request_loader_pdata
  9132. *
  9133. * SUMMARY: Function pointer included in core_cmds to allow other modules
  9134. * to request the pointer to the loader platform data
  9135. *
  9136. * RETURN: Pointer to the loader platform data struct
  9137. *
  9138. * PARAMETERS:
  9139. * *dev - pointer to device structure
  9140. ******************************************************************************/
  9141. static struct pt_loader_platform_data *_pt_request_loader_pdata(
  9142. struct device *dev)
  9143. {
  9144. struct pt_core_data *cd = dev_get_drvdata(dev);
  9145. return cd->pdata->loader_pdata;
  9146. }
  9147. /*******************************************************************************
  9148. * FUNCTION: _pt_request_start_wd
  9149. *
  9150. * SUMMARY: Function pointer included in core_cmds to allow other modules
  9151. * to request to start the TTDL watchdog
  9152. *
  9153. * RETURN:
  9154. * 0 = success
  9155. * !0 = failure
  9156. *
  9157. * PARAMETERS:
  9158. * *dev - pointer to device structure
  9159. ******************************************************************************/
  9160. static int _pt_request_start_wd(struct device *dev)
  9161. {
  9162. struct pt_core_data *cd = dev_get_drvdata(dev);
  9163. pt_start_wd_timer(cd);
  9164. return 0;
  9165. }
  9166. /*******************************************************************************
  9167. * FUNCTION: _pt_request_stop_wd
  9168. *
  9169. * SUMMARY: Function pointer included in core_cmds to allow other modules
  9170. * to request to stop the TTDL watchdog
  9171. *
  9172. * RETURN:
  9173. * 0 = success
  9174. * !0 = failure
  9175. *
  9176. * PARAMETERS:
  9177. * *dev - pointer to device structure
  9178. ******************************************************************************/
  9179. static int _pt_request_stop_wd(struct device *dev)
  9180. {
  9181. struct pt_core_data *cd = dev_get_drvdata(dev);
  9182. pt_stop_wd_timer(cd);
  9183. return 0;
  9184. }
  9185. /*******************************************************************************
  9186. * FUNCTION: pt_pip2_launch_app
  9187. *
  9188. * SUMMARY: Sends the PIP2 EXECUTE command to launch the APP and then wait for
  9189. * the FW reset sentinel to indicate the function succeeded.
  9190. *
  9191. * NOTE: Calling this function when the DUT is in Application mode WILL result
  9192. * in a timeout delay and with the DUT being reset with an XRES.
  9193. *
  9194. * RETURN:
  9195. * 0 = success
  9196. * !0 = failure
  9197. *
  9198. * PARAMETERS:
  9199. * *cd - pointer to core data structure
  9200. * protect - flag to call protected or non-protected
  9201. ******************************************************************************/
  9202. static int pt_pip2_launch_app(struct device *dev, int protect)
  9203. {
  9204. struct pt_core_data *cd = dev_get_drvdata(dev);
  9205. u16 actual_read_len;
  9206. u16 tmp_startup_status = cd->startup_status;
  9207. u8 read_buf[12];
  9208. u8 status;
  9209. int time = 0;
  9210. int rc = 0;
  9211. mutex_lock(&cd->system_lock);
  9212. cd->startup_status = STARTUP_STATUS_START;
  9213. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9214. mutex_unlock(&cd->system_lock);
  9215. rc = _pt_request_pip2_send_cmd(dev, protect,
  9216. PIP2_CMD_ID_EXECUTE, NULL, 0, read_buf,
  9217. &actual_read_len);
  9218. if (rc) {
  9219. pt_debug(dev, DL_ERROR,
  9220. "%s: PIP2 EXECUTE cmd failed rc = %d\n",
  9221. __func__, rc);
  9222. } else {
  9223. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  9224. /* Test for no or invalid image in FLASH, no point to reset */
  9225. if (status == PIP2_RSP_ERR_INVALID_IMAGE) {
  9226. rc = status;
  9227. goto exit;
  9228. }
  9229. /* Any other boot failure */
  9230. if (status != 0) {
  9231. pt_debug(dev, DL_ERROR,
  9232. "%s: FW did not EXECUTE, status = %d\n",
  9233. __func__, status);
  9234. rc = status;
  9235. }
  9236. }
  9237. if (rc) {
  9238. pt_debug(dev, DL_ERROR,
  9239. "%s: Failed to launch APP, XRES DUT rc = %d\n",
  9240. __func__, rc);
  9241. goto exit;
  9242. }
  9243. while ((cd->startup_status == STARTUP_STATUS_START) && time < 240) {
  9244. msleep(20);
  9245. pt_debug(cd->dev, DL_INFO, "%s: wait for %d for enum=0x%04X\n",
  9246. __func__, time, cd->startup_status);
  9247. time += 20;
  9248. }
  9249. if (cd->startup_status == STARTUP_STATUS_START) {
  9250. pt_debug(cd->dev, DL_WARN,
  9251. "%s: TMO waiting for FW reset sentinel\n", __func__);
  9252. rc = -ETIME;
  9253. }
  9254. exit:
  9255. if (cd->startup_status == STARTUP_STATUS_START) {
  9256. /* Reset to original state because we could be stuck in BL */
  9257. mutex_lock(&cd->system_lock);
  9258. cd->startup_status = tmp_startup_status;
  9259. mutex_unlock(&cd->system_lock);
  9260. }
  9261. return rc;
  9262. }
  9263. /*******************************************************************************
  9264. * FUNCTION: _pt_request_pip2_launch_app
  9265. *
  9266. * SUMMARY: Calls pt_pip2_launch_app() when configured to. A small delay is
  9267. * inserted to ensure the reset has allowed the BL reset sentinel to be
  9268. * consumed.
  9269. *
  9270. * RETURN:
  9271. * 0 = success
  9272. * !0 = failure
  9273. *
  9274. * PARAMETERS:
  9275. * *cd - pointer to core data structure
  9276. ******************************************************************************/
  9277. static int _pt_request_pip2_launch_app(struct device *dev, int protect)
  9278. {
  9279. return pt_pip2_launch_app(dev, protect);
  9280. }
  9281. /*******************************************************************************
  9282. * FUNCTION: _pt_request_wait_for_enum_state
  9283. *
  9284. * SUMMARY: Loops for up to timeout waiting for the startup_status to reach
  9285. * the state passed in or STARTUP_STATUS_COMPLETE whichever comes first
  9286. *
  9287. * RETURN:
  9288. * 0 = success
  9289. * !0 = failure
  9290. *
  9291. * PARAMETERS:
  9292. * *cd - pointer to core data structure
  9293. * timeout - timeout for how long to wait
  9294. * state - enum state to wait for
  9295. ******************************************************************************/
  9296. static int _pt_request_wait_for_enum_state(struct device *dev, int timeout,
  9297. int state)
  9298. {
  9299. int rc = 0;
  9300. int t;
  9301. struct pt_core_data *cd = dev_get_drvdata(dev);
  9302. t = wait_event_timeout(cd->wait_q,
  9303. (cd->startup_status & state) || (cd->startup_status & 0x0100),
  9304. msecs_to_jiffies(timeout));
  9305. if (IS_TMO(t)) {
  9306. pt_debug(cd->dev, DL_ERROR,
  9307. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  9308. __func__, state, timeout);
  9309. pt_debug(cd->dev, DL_WARN,
  9310. "%s: enum state reached 0x%04X\n",
  9311. __func__, cd->startup_status);
  9312. rc = -ETIME;
  9313. } else if (cd->startup_status & state) {
  9314. pt_debug(cd->dev, DL_INFO,
  9315. "%s: Enum state reached: enum=0x%04X in %dms\n",
  9316. __func__, cd->startup_status,
  9317. (t == 1) ? timeout : (timeout - jiffies_to_msecs(t)));
  9318. } else {
  9319. if (t == 1) {
  9320. pt_debug(
  9321. cd->dev, DL_ERROR,
  9322. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  9323. __func__, state, timeout);
  9324. rc = -ETIME;
  9325. } else {
  9326. pt_debug(
  9327. cd->dev, DL_ERROR,
  9328. "%s: Enum state 0x%04X not reached in %dms\n",
  9329. __func__, state, timeout - jiffies_to_msecs(t));
  9330. rc = -EINVAL;
  9331. }
  9332. pt_debug(cd->dev, DL_INFO, "%s: enum state reached 0x%04X\n",
  9333. __func__, cd->startup_status);
  9334. }
  9335. return rc;
  9336. }
  9337. /*******************************************************************************
  9338. * FUNCTION: pt_core_wake_device_from_deep_sleep_
  9339. *
  9340. * SUMMARY: Call the set_power function and set the DUT to wake up from
  9341. * deep sleep.
  9342. *
  9343. * RETURN:
  9344. * 0 = success
  9345. * !0 = error
  9346. *
  9347. * PARAMETERS:
  9348. * *cd - pointer to core data
  9349. ******************************************************************************/
  9350. static int pt_core_wake_device_from_deep_sleep_(
  9351. struct pt_core_data *cd)
  9352. {
  9353. int rc;
  9354. rc = pt_hid_cmd_set_power_(cd, HID_POWER_ON);
  9355. if (rc)
  9356. rc = -EAGAIN;
  9357. /* Prevent failure on sequential wake/sleep requests from OS */
  9358. msleep(20);
  9359. return rc;
  9360. }
  9361. /*******************************************************************************
  9362. * FUNCTION: pt_core_wake_device_from_easy_wake_
  9363. *
  9364. * SUMMARY: Wake up device from Easy-Wake state.
  9365. *
  9366. * RETURN:
  9367. * 0 = success
  9368. * !0 = failure
  9369. *
  9370. * PARAMETERS:
  9371. * *cd - pointer to core data
  9372. ******************************************************************************/
  9373. static int pt_core_wake_device_from_easy_wake_(struct pt_core_data *cd)
  9374. {
  9375. mutex_lock(&cd->system_lock);
  9376. cd->wait_until_wake = 1;
  9377. mutex_unlock(&cd->system_lock);
  9378. wake_up(&cd->wait_q);
  9379. msleep(20);
  9380. return pt_core_wake_device_from_deep_sleep_(cd);
  9381. }
  9382. /*******************************************************************************
  9383. * FUNCTION: pt_restore_parameters_
  9384. *
  9385. * SUMMARY: This function sends all RAM parameters stored in the linked list
  9386. * back to the DUT
  9387. *
  9388. * RETURN:
  9389. * 0 = success
  9390. * !0 = failure
  9391. *
  9392. * PARAMETERS:
  9393. * *cd - pointer the core data structure
  9394. ******************************************************************************/
  9395. static int pt_restore_parameters_(struct pt_core_data *cd)
  9396. {
  9397. struct param_node *param;
  9398. int rc = 0;
  9399. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  9400. goto exit;
  9401. spin_lock(&cd->spinlock);
  9402. list_for_each_entry(param, &cd->param_list, node) {
  9403. spin_unlock(&cd->spinlock);
  9404. pt_debug(cd->dev, DL_INFO, "%s: Parameter id:%d value:%d\n",
  9405. __func__, param->id, param->value);
  9406. rc = pt_pip_set_param_(cd, param->id,
  9407. param->value, param->size);
  9408. if (rc)
  9409. goto exit;
  9410. spin_lock(&cd->spinlock);
  9411. }
  9412. spin_unlock(&cd->spinlock);
  9413. exit:
  9414. return rc;
  9415. }
  9416. /*******************************************************************************
  9417. * FUNCTION: pt_pip2_exit_bl_
  9418. *
  9419. * SUMMARY: Attempt to exit the BL and run the application, taking into account
  9420. * a DUT that may not have flash and will need FW to be loaded into RAM
  9421. *
  9422. * RETURN:
  9423. * 0 = success
  9424. * !0 = failure
  9425. *
  9426. * PARAMETERS:
  9427. * *cd - pointer the core data structure
  9428. * *status_str - pointer to optional status string buffer
  9429. * buf_size - size of status_str buffer
  9430. ******************************************************************************/
  9431. int pt_pip2_exit_bl_(struct pt_core_data *cd, u8 *status_str, int buf_size)
  9432. {
  9433. int rc;
  9434. int wait_time = 0;
  9435. u8 mode = PT_MODE_UNKNOWN;
  9436. bool load_status_str = false;
  9437. /*
  9438. * Below function has protective call to ensure no enum is still on
  9439. * going, while this kind of protection should be applied widely in
  9440. * future (TODO).
  9441. */
  9442. rc = pt_pip2_get_mode_sysmode(cd, &mode, NULL);
  9443. if (status_str && buf_size <= 50)
  9444. load_status_str = true;
  9445. if (mode == PT_MODE_BOOTLOADER) {
  9446. if (cd->flashless_dut == 1) {
  9447. rc = pt_hw_hard_reset(cd);
  9448. } else {
  9449. rc = pt_pip2_launch_app(cd->dev,
  9450. PT_CORE_CMD_UNPROTECTED);
  9451. if (rc == PIP2_RSP_ERR_INVALID_IMAGE) {
  9452. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  9453. __func__, "Invalid image in FLASH rc", rc);
  9454. } else if (rc) {
  9455. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  9456. __func__, "Failed to launch app rc", rc);
  9457. }
  9458. }
  9459. if (!rc) {
  9460. if (cd->flashless_dut == 1) {
  9461. /* Wait for BL to complete before enum */
  9462. rc = _pt_request_wait_for_enum_state(cd->dev,
  9463. 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  9464. if (rc && load_status_str) {
  9465. strlcpy(status_str,
  9466. "No FW sentinel after BL",
  9467. sizeof(*status_str)*PT_STATUS_STR_LEN);
  9468. goto exit;
  9469. }
  9470. }
  9471. /*
  9472. * If the host wants to interact with the FW or do a
  9473. * forced calibration, the FW must be out of BOOT mode
  9474. * and the system information must have been retrieved.
  9475. * Reaching the FW_OUT_OF_BOOT state guarantees both.
  9476. * If, however, the enumeration does not reach this
  9477. * point, the DUT may still be in APP mode so test
  9478. * for all conditions.
  9479. */
  9480. rc = _pt_request_wait_for_enum_state(cd->dev, 4500,
  9481. STARTUP_STATUS_FW_OUT_OF_BOOT);
  9482. if (!rc || cd->startup_status >=
  9483. STARTUP_STATUS_FW_RESET_SENTINEL) {
  9484. mutex_lock(&cd->system_lock);
  9485. cd->mode = PT_MODE_OPERATIONAL;
  9486. mutex_unlock(&cd->system_lock);
  9487. }
  9488. if (rc) {
  9489. pt_debug(cd->dev, DL_WARN, "%s: %s: 0x%04X\n",
  9490. __func__, "Failed to enum with DUT",
  9491. cd->startup_status);
  9492. if (load_status_str && !(cd->startup_status &
  9493. STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  9494. strlcpy(status_str,
  9495. "FW Stuck in Boot mode",
  9496. sizeof(*status_str)*PT_STATUS_STR_LEN);
  9497. goto exit;
  9498. }
  9499. }
  9500. /*
  9501. * The coming FW sentinel could wake up the event
  9502. * queue, which has chance to be taken by next command
  9503. * wrongly. Following delay is a workaround to avoid
  9504. * this issue for most situations.
  9505. */
  9506. msleep(20);
  9507. pt_start_wd_timer(cd);
  9508. }
  9509. if (load_status_str) {
  9510. if (rc == PIP2_RSP_ERR_INVALID_IMAGE)
  9511. strlcpy(status_str,
  9512. "Failed - Invalid image in FLASH",
  9513. sizeof(*status_str)*PT_STATUS_STR_LEN);
  9514. else if (!rc)
  9515. strlcpy(status_str,
  9516. "Entered APP from BL mode", sizeof(*status_str)*PT_STATUS_STR_LEN);
  9517. else
  9518. strlcpy(status_str,
  9519. "Failed to enter APP from BL mode",
  9520. sizeof(*status_str)*PT_STATUS_STR_LEN);
  9521. }
  9522. } else if (mode == PT_MODE_OPERATIONAL) {
  9523. mutex_lock(&cd->system_lock);
  9524. cd->mode = mode;
  9525. mutex_unlock(&cd->system_lock);
  9526. rc = pt_poll_for_fw_exit_boot_mode(cd, 1500, &wait_time);
  9527. if (load_status_str) {
  9528. if (!rc)
  9529. strlcpy(status_str,
  9530. "Already in APP mode",
  9531. sizeof(*status_str)*PT_STATUS_STR_LEN);
  9532. else
  9533. strlcpy(status_str,
  9534. "Already in APP mode - FW stuck in Boot mode",
  9535. sizeof(*status_str)*PT_STATUS_STR_LEN);
  9536. }
  9537. } else if (rc || mode == PT_MODE_UNKNOWN) {
  9538. mutex_lock(&cd->system_lock);
  9539. cd->mode = mode;
  9540. mutex_unlock(&cd->system_lock);
  9541. if (load_status_str)
  9542. strlcpy(status_str, "Failed to determine active mode",
  9543. sizeof(*status_str)*PT_STATUS_STR_LEN);
  9544. }
  9545. exit:
  9546. if (!rc)
  9547. pt_start_wd_timer(cd);
  9548. return rc;
  9549. }
  9550. /*******************************************************************************
  9551. * FUNCTION: pt_pip2_exit_bl
  9552. *
  9553. * SUMMARY: Wrapper function for _pt_pip2_exit_bl that guarantees exclusive
  9554. * access.
  9555. *
  9556. * RETURN:
  9557. * 0 = success
  9558. * !0 = failure
  9559. *
  9560. * PARAMETERS:
  9561. * *cd - pointer the core data structure
  9562. * *status_str - pointer to optional status string buffer
  9563. * buf_size - size of status_str buffer
  9564. ******************************************************************************/
  9565. int pt_pip2_exit_bl(struct pt_core_data *cd, u8 *status_str, int buf_size)
  9566. {
  9567. int rc;
  9568. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  9569. if (rc < 0) {
  9570. pt_debug(cd->dev, DL_ERROR,
  9571. "%s: fail get exclusive ex=%p own=%p\n", __func__,
  9572. cd->exclusive_dev, cd->dev);
  9573. return rc;
  9574. }
  9575. rc = pt_pip2_exit_bl_(cd, status_str, buf_size);
  9576. if (release_exclusive(cd, cd->dev) < 0)
  9577. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  9578. __func__);
  9579. return rc;
  9580. }
  9581. /*******************************************************************************
  9582. * FUNCTION: _fast_startup
  9583. *
  9584. * SUMMARY: Perform fast startup after resume device by power on/off stratergy.
  9585. *
  9586. * RETURN:
  9587. * 0 = success
  9588. * !0 = failure
  9589. *
  9590. * PARAMETERS:
  9591. * *cd - pointer the core data structure
  9592. ******************************************************************************/
  9593. static int _fast_startup(struct pt_core_data *cd)
  9594. {
  9595. int retry = PT_CORE_STARTUP_RETRY_COUNT;
  9596. int rc = 0;
  9597. u8 mode = PT_MODE_UNKNOWN;
  9598. struct pt_hid_desc hid_desc;
  9599. int wait_time = 0;
  9600. memset(&hid_desc, 0, sizeof(hid_desc));
  9601. reset:
  9602. if (retry != PT_CORE_STARTUP_RETRY_COUNT)
  9603. pt_debug(cd->dev, DL_INFO, "%s: Retry %d\n", __func__,
  9604. PT_CORE_STARTUP_RETRY_COUNT - retry);
  9605. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  9606. pt_debug(cd->dev, DL_INFO, "%s: PIP1 Enumeration start\n",
  9607. __func__);
  9608. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  9609. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  9610. if (rc < 0) {
  9611. pt_debug(cd->dev, DL_ERROR,
  9612. "%s: Error on getting HID descriptor r=%d\n",
  9613. __func__, rc);
  9614. if (retry--)
  9615. goto reset;
  9616. goto exit;
  9617. }
  9618. cd->mode = pt_get_mode(cd, &hid_desc);
  9619. if (cd->mode == PT_MODE_BOOTLOADER) {
  9620. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  9621. __func__);
  9622. rc = pt_hid_output_bl_launch_app_(cd);
  9623. if (rc < 0) {
  9624. pt_debug(cd->dev, DL_ERROR,
  9625. "%s: Error on launch app r=%d\n",
  9626. __func__, rc);
  9627. if (retry--)
  9628. goto reset;
  9629. goto exit;
  9630. }
  9631. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  9632. if (rc < 0) {
  9633. pt_debug(cd->dev, DL_ERROR,
  9634. "%s: Error on getting HID descriptor r=%d\n",
  9635. __func__, rc);
  9636. if (retry--)
  9637. goto reset;
  9638. goto exit;
  9639. }
  9640. cd->mode = pt_get_mode(cd, &hid_desc);
  9641. if (cd->mode == PT_MODE_BOOTLOADER) {
  9642. if (retry--)
  9643. goto reset;
  9644. goto exit;
  9645. }
  9646. }
  9647. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  9648. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9649. } else {
  9650. pt_debug(cd->dev, DL_INFO, "%s: PIP2 Enumeration start\n",
  9651. __func__);
  9652. if (retry == PT_CORE_STARTUP_RETRY_COUNT) {
  9653. /* Wait for any sentinel before first try */
  9654. rc = _pt_request_wait_for_enum_state(
  9655. cd->dev, 150,
  9656. STARTUP_STATUS_BL_RESET_SENTINEL |
  9657. STARTUP_STATUS_FW_RESET_SENTINEL);
  9658. if (rc)
  9659. pt_debug(cd->dev, DL_ERROR,
  9660. "%s: No Sentinel detected rc = %d\n",
  9661. __func__, rc);
  9662. } else
  9663. pt_flush_bus_if_irq_asserted(cd,
  9664. PT_FLUSH_BUS_BASED_ON_LEN);
  9665. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9666. if (rc) {
  9667. pt_debug(cd->dev, DL_ERROR,
  9668. "%s: Get mode failed, mode unknown\n",
  9669. __func__);
  9670. mode = PT_MODE_UNKNOWN;
  9671. }
  9672. cd->mode = mode;
  9673. if (cd->mode == PT_MODE_BOOTLOADER) {
  9674. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  9675. __func__);
  9676. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  9677. if (rc) {
  9678. pt_debug(cd->dev, DL_ERROR,
  9679. "%s Failed to exit bootloader\n",
  9680. __func__);
  9681. msleep(50);
  9682. rc = -ENODEV;
  9683. if (retry--)
  9684. goto reset;
  9685. goto exit;
  9686. } else {
  9687. pt_debug(cd->dev, DL_INFO,
  9688. "%s: Exit bootloader successfully\n",
  9689. __func__);
  9690. }
  9691. if (cd->mode != PT_MODE_OPERATIONAL) {
  9692. pt_debug(cd->dev, DL_WARN,
  9693. "%s: restore mode failure mode = %d\n",
  9694. __func__, cd->mode);
  9695. if (retry--)
  9696. goto reset;
  9697. goto exit;
  9698. }
  9699. }
  9700. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  9701. }
  9702. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  9703. rc = _pt_poll_for_fw_exit_boot_mode(cd, 500, &wait_time);
  9704. if (!rc) {
  9705. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9706. pt_debug(cd->dev, DL_WARN,
  9707. "%s: Exit FW BOOT Mode after %dms\n",
  9708. __func__, wait_time);
  9709. } else {
  9710. pt_debug(cd->dev, DL_WARN,
  9711. "%s: FW stuck in BOOT Mode after %dms\n",
  9712. __func__, wait_time);
  9713. goto exit;
  9714. }
  9715. if (!cd->sysinfo.ready) {
  9716. rc = pt_hid_output_get_sysinfo_(cd);
  9717. if (rc) {
  9718. pt_debug(cd->dev, DL_ERROR,
  9719. "%s: Error on getting sysinfo r=%d\n",
  9720. __func__, rc);
  9721. if (retry--)
  9722. goto reset;
  9723. goto exit;
  9724. }
  9725. }
  9726. cd->startup_status |= STARTUP_STATUS_GET_SYS_INFO;
  9727. rc = pt_restore_parameters_(cd);
  9728. if (rc)
  9729. pt_debug(cd->dev, DL_ERROR,
  9730. "%s: failed to restore parameters rc=%d\n",
  9731. __func__, rc);
  9732. else
  9733. cd->startup_status |= STARTUP_STATUS_RESTORE_PARM;
  9734. exit:
  9735. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  9736. return rc;
  9737. }
  9738. /*******************************************************************************
  9739. * FUNCTION: pt_core_poweron_device_
  9740. *
  9741. * SUMMARY: Power on device, enable IRQ, and then perform a fast startup.
  9742. *
  9743. * RETURN:
  9744. * 0 = success
  9745. * !0 = failure
  9746. *
  9747. * PARAMETERS:
  9748. * *cd - pointer to core data
  9749. ******************************************************************************/
  9750. static int pt_core_poweron_device_(struct pt_core_data *cd)
  9751. {
  9752. struct device *dev = cd->dev;
  9753. int rc = 0;
  9754. /*
  9755. * After power on action, the chip can general FW sentinel. It can
  9756. * trigger an enumeration without hid_reset_cmd_state flag. Since the
  9757. * _fast_startup() can perform a quick enumeration too, here doesn't
  9758. * need another enumeration.
  9759. */
  9760. mutex_lock(&cd->system_lock);
  9761. cd->startup_status = STARTUP_STATUS_START;
  9762. cd->hid_reset_cmd_state = 1;
  9763. mutex_unlock(&cd->system_lock);
  9764. rc = cd->cpdata->power(cd->cpdata, 1, dev, 0);
  9765. if (rc < 0) {
  9766. pt_debug(dev, DL_ERROR, "%s: HW Power up fails r=%d\n",
  9767. __func__, rc);
  9768. goto exit;
  9769. }
  9770. if (!cd->irq_enabled) {
  9771. cd->irq_enabled = true;
  9772. enable_irq(cd->irq);
  9773. }
  9774. /* TBD: following function doesn't update startup_status */
  9775. rc = _fast_startup(cd);
  9776. exit:
  9777. return rc;
  9778. }
  9779. /*******************************************************************************
  9780. * FUNCTION: pt_core_wake_device_from_deep_standby_
  9781. *
  9782. * SUMMARY: Reset device, and then trigger a full enumeration.
  9783. *
  9784. * RETURN:
  9785. * 0 = success
  9786. * !0 = failure
  9787. *
  9788. * PARAMETERS:
  9789. * *cd - pointer to core data
  9790. ******************************************************************************/
  9791. static int pt_core_wake_device_from_deep_standby_(struct pt_core_data *cd)
  9792. {
  9793. int rc;
  9794. rc = pt_dut_reset_and_wait(cd);
  9795. if (rc < 0) {
  9796. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  9797. __func__, rc);
  9798. goto exit;
  9799. }
  9800. rc = _fast_startup(cd);
  9801. exit:
  9802. return rc;
  9803. }
  9804. /*******************************************************************************
  9805. * FUNCTION: pt_core_wake_
  9806. *
  9807. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  9808. * the configuration in the core platform data structure.
  9809. *
  9810. * RETURN:
  9811. * 0 = success
  9812. * !0 = failure
  9813. *
  9814. * PARAMETERS:
  9815. * *cd - pointer to core data
  9816. ******************************************************************************/
  9817. static int pt_core_wake_(struct pt_core_data *cd)
  9818. {
  9819. int rc = 0;
  9820. /*
  9821. * Do nothing if system already awake or in progress of waking.
  9822. * Proceed if sleeping or entering sleep state.
  9823. */
  9824. if (cd->sleep_state == SS_SLEEP_OFF || cd->sleep_state == SS_WAKING)
  9825. goto exit;
  9826. mutex_lock(&cd->system_lock);
  9827. cd->sleep_state = SS_WAKING;
  9828. mutex_unlock(&cd->system_lock);
  9829. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  9830. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9831. rc = pt_core_wake_device_from_easy_wake_(cd);
  9832. else if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP)
  9833. rc = pt_core_poweron_device_(cd);
  9834. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  9835. rc = pt_core_wake_device_from_deep_standby_(cd);
  9836. else /* Default action to exit DeepSleep */
  9837. rc = pt_core_wake_device_from_deep_sleep_(cd);
  9838. }
  9839. mutex_lock(&cd->system_lock);
  9840. if (rc == 0)
  9841. cd->sleep_state = SS_SLEEP_OFF;
  9842. else
  9843. cd->sleep_state = SS_SLEEP_ON;
  9844. mutex_unlock(&cd->system_lock);
  9845. pt_start_wd_timer(cd);
  9846. exit:
  9847. return rc;
  9848. }
  9849. /*******************************************************************************
  9850. * FUNCTION: pt_core_wake
  9851. *
  9852. * SUMMARY: Protected call to pt_core_wake_ by exclusive access to the DUT.
  9853. *
  9854. * RETURN:
  9855. * 0 = success
  9856. * !0 = failure
  9857. *
  9858. * PARAMETERS:
  9859. * *cd - pointer to core data
  9860. ******************************************************************************/
  9861. static int pt_core_wake(struct pt_core_data *cd)
  9862. {
  9863. int rc;
  9864. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  9865. if (rc < 0) {
  9866. pt_debug(cd->dev, DL_ERROR,
  9867. "%s: fail get exclusive ex=%p own=%p\n",
  9868. __func__, cd->exclusive_dev, cd->dev);
  9869. return rc;
  9870. }
  9871. rc = pt_core_wake_(cd);
  9872. if (release_exclusive(cd, cd->dev) < 0)
  9873. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  9874. __func__);
  9875. else
  9876. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  9877. __func__);
  9878. return rc;
  9879. }
  9880. /*******************************************************************************
  9881. * FUNCTION: pt_get_ic_crc_
  9882. *
  9883. * SUMMARY: This function retrieves the config block CRC
  9884. *
  9885. * NOTE: The post condition of calling this function will be that the DUT will
  9886. * be in SCANNINING mode if no failures occur
  9887. *
  9888. * RETURN:
  9889. * 0 = success
  9890. * !0 = failure
  9891. *
  9892. * PARAMETERS:
  9893. * *cd - pointer the core data structure
  9894. * ebid - enumerated block ID
  9895. ******************************************************************************/
  9896. static int pt_get_ic_crc_(struct pt_core_data *cd, u8 ebid)
  9897. {
  9898. struct pt_sysinfo *si = &cd->sysinfo;
  9899. int rc = 0;
  9900. u8 status;
  9901. u16 calculated_crc = 0;
  9902. u16 stored_crc = 0;
  9903. rc = pt_pip_suspend_scanning_(cd);
  9904. if (rc)
  9905. goto error;
  9906. rc = pt_pip_verify_config_block_crc_(cd, ebid, &status,
  9907. &calculated_crc, &stored_crc);
  9908. if (rc)
  9909. goto exit;
  9910. if (status) {
  9911. rc = -EINVAL;
  9912. goto exit;
  9913. }
  9914. si->ttconfig.crc = stored_crc;
  9915. exit:
  9916. pt_pip_resume_scanning_(cd);
  9917. error:
  9918. pt_debug(cd->dev, DL_INFO,
  9919. "%s: CRC: ebid:%d, calc:0x%04X, stored:0x%04X, rc=%d\n",
  9920. __func__, ebid, calculated_crc, stored_crc, rc);
  9921. return rc;
  9922. }
  9923. /*******************************************************************************
  9924. * FUNCTION: pt_pip2_read_gpio
  9925. *
  9926. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and stores the 32 gpio
  9927. * bits into the passed in variable
  9928. *
  9929. * NOTE: PIP2 READ_GPIO command is only supported in bootloader
  9930. *
  9931. * RETURN:
  9932. * 0 = success
  9933. * !0 = failure
  9934. *
  9935. * PARAMETERS:
  9936. * *dev - pointer to device structure
  9937. * *status - pointer to where the command response status is stored
  9938. * *gpio - pointer to device attributes structure
  9939. ******************************************************************************/
  9940. static int pt_pip2_read_gpio(struct device *dev, u8 *status, u32 *gpio)
  9941. {
  9942. int rc = 0;
  9943. u16 actual_read_len;
  9944. u8 read_buf[12];
  9945. u8 tmp_status = 0;
  9946. u8 index = PIP2_RESP_STATUS_OFFSET;
  9947. memset(read_buf, 0, ARRAY_SIZE(read_buf));
  9948. rc = _pt_request_pip2_send_cmd(dev,
  9949. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_READ_GPIO,
  9950. NULL, 0, read_buf, &actual_read_len);
  9951. if (rc) {
  9952. pt_debug(dev, DL_ERROR,
  9953. "%s: Failed to send PIP2 READ_GPIO cmd\n", __func__);
  9954. rc = -ECOMM;
  9955. } else {
  9956. tmp_status = read_buf[index];
  9957. }
  9958. if (status)
  9959. *status = tmp_status;
  9960. if (!rc && gpio && (tmp_status == 0)) {
  9961. *gpio = ((read_buf[index + 4] << 24) |
  9962. (read_buf[index + 3] << 16) |
  9963. (read_buf[index + 2] << 8) |
  9964. (read_buf[index + 1]));
  9965. }
  9966. return rc;
  9967. }
  9968. /*******************************************************************************
  9969. * FUNCTION: _pt_pip2_get_panel_id_by_gpio
  9970. *
  9971. * SUMMARY: Wrapper function to call pt_pip2_read_gpio() to get panel ID
  9972. *
  9973. * RETURN:
  9974. * 0 = success
  9975. * !0 = failure
  9976. *
  9977. * PARAMETERS:
  9978. * *cd - pointer the core data structure
  9979. * *pid - pointer to store panel ID
  9980. ******************************************************************************/
  9981. static int _pt_pip2_get_panel_id_by_gpio(struct pt_core_data *cd, u8 *pid)
  9982. {
  9983. u32 gpio_value = 0;
  9984. u8 status = 0;
  9985. u8 panel_id = PANEL_ID_NOT_ENABLED;
  9986. int rc = 0;
  9987. if (!pid)
  9988. return -ENOMEM;
  9989. rc = pt_pip2_read_gpio(cd->dev, &status, &gpio_value);
  9990. if (!rc) {
  9991. if (status == 0) {
  9992. panel_id = (gpio_value & PT_PANEL_ID_BITMASK) >>
  9993. PT_PANEL_ID_SHIFT;
  9994. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X %s=0x%08X\n",
  9995. __func__,
  9996. "BL mode PID", panel_id, "gpio", gpio_value);
  9997. *pid = panel_id;
  9998. } else {
  9999. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  10000. __func__,
  10001. "BL read gpio failed status", status);
  10002. }
  10003. } else {
  10004. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  10005. __func__,
  10006. "BL read gpio failed status", status);
  10007. }
  10008. return rc;
  10009. }
  10010. /*******************************************************************************
  10011. * FUNCTION: pt_enum_with_dut_
  10012. *
  10013. * SUMMARY: This function does the full enumeration of the DUT with TTDL.
  10014. * The core data (cd) startup_status will store, as a bitmask, each
  10015. * state of the enumeration process. The startup will be attempted
  10016. * PT_CORE_STARTUP_RETRY_COUNT times before giving up.
  10017. *
  10018. * RETURN:
  10019. * 0 = success
  10020. * !0 = failure
  10021. *
  10022. * PARAMETERS:
  10023. * *cd - pointer the core data structure
  10024. * reset - Flag to reset the DUT before attempting to enumerate
  10025. * *status - poionter to store the enum status bitmask flags
  10026. ******************************************************************************/
  10027. static int pt_enum_with_dut_(struct pt_core_data *cd, bool reset,
  10028. u32 *enum_status)
  10029. {
  10030. int try = 1;
  10031. int rc = 0;
  10032. int wait_time = 0;
  10033. bool detected = false;
  10034. u8 return_data[8];
  10035. u8 mode = PT_MODE_UNKNOWN;
  10036. u8 protocol_mode = PT_PROTOCOL_MODE_PIP;
  10037. u8 pid = PANEL_ID_NOT_ENABLED;
  10038. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  10039. struct pt_hid_desc hid_desc;
  10040. memset(&hid_desc, 0, sizeof(hid_desc));
  10041. #ifdef TTHE_TUNER_SUPPORT
  10042. tthe_print(cd, NULL, 0, "enter startup");
  10043. #endif
  10044. pt_debug(cd->dev, DL_INFO, "%s: Start enum... 0x%04X, reset=%d\n",
  10045. __func__, cd->startup_status, reset);
  10046. pt_stop_wd_timer(cd);
  10047. reset:
  10048. if (try > 1)
  10049. pt_debug(cd->dev, DL_WARN, "%s: DUT Enum Attempt %d\n",
  10050. __func__, try);
  10051. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  10052. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  10053. pt_debug(cd->dev, DL_INFO,
  10054. "%s: PIP1 Enumeration start\n", __func__);
  10055. /* Only reset the DUT after the first try */
  10056. if (reset || (try > 1)) {
  10057. /*
  10058. * Reset hardware only for Legacy parts. Skip for TT/TC
  10059. * parts because if the FW image was loaded directly
  10060. * to SRAM issueing a reset ill wipe out what was just
  10061. * loaded.
  10062. */
  10063. rc = pt_dut_reset_and_wait(cd);
  10064. if (rc < 0) {
  10065. pt_debug(cd->dev, DL_ERROR,
  10066. "%s: Error on h/w reset r=%d\n",
  10067. __func__, rc);
  10068. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  10069. goto reset;
  10070. goto exit;
  10071. }
  10072. /* sleep to allow FW to be launched if available */
  10073. msleep(120);
  10074. }
  10075. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  10076. if (rc < 0) {
  10077. pt_debug(cd->dev, DL_ERROR,
  10078. "%s: Error getting HID Descriptor r=%d\n",
  10079. __func__, rc);
  10080. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  10081. goto reset;
  10082. rc = -EIO;
  10083. goto exit;
  10084. }
  10085. detected = true;
  10086. cd->mode = pt_get_mode(cd, &hid_desc);
  10087. /*
  10088. * Most systems do not use an XY pin as the panel_id and so
  10089. * the BL is used to retrieve the panel_id, however for
  10090. * systems that do use an XY pin, the panel_id MUST be
  10091. * retrieved from the system information when running FW
  10092. * (done below) and so this section of code is skipped.
  10093. * Entering the BL here is only needed on XY_PIN systems.
  10094. */
  10095. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  10096. if (cd->mode == PT_MODE_OPERATIONAL) {
  10097. rc = pt_pip_start_bootloader_(cd);
  10098. if (rc < 0) {
  10099. pt_debug(cd->dev, DL_ERROR,
  10100. "%s: Error on start bootloader r=%d\n",
  10101. __func__, rc);
  10102. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  10103. goto reset;
  10104. goto exit;
  10105. }
  10106. cd->mode = PT_MODE_BOOTLOADER;
  10107. pt_debug(cd->dev, DL_INFO,
  10108. "%s: Bootloader mode\n", __func__);
  10109. }
  10110. rc = pt_hid_output_bl_get_information_(cd, return_data);
  10111. if (!rc) {
  10112. cd->bl_info.ready = true;
  10113. cd->bl_info.chip_id =
  10114. get_unaligned_le16(&return_data[2]);
  10115. pt_debug(cd->dev, DL_INFO, "%s: chip ID %04X\n",
  10116. __func__, cd->bl_info.chip_id);
  10117. } else {
  10118. pt_debug(cd->dev, DL_ERROR,
  10119. "%s: failed to get chip ID, r=%d\n",
  10120. __func__, rc);
  10121. }
  10122. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  10123. mutex_lock(&cd->system_lock);
  10124. if (!rc) {
  10125. cd->pid_for_loader = pid;
  10126. pt_debug(cd->dev, DL_INFO,
  10127. "%s: Panel ID: 0x%02X\n",
  10128. __func__, cd->pid_for_loader);
  10129. } else {
  10130. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  10131. pt_debug(cd->dev, DL_WARN,
  10132. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  10133. __func__, cd->pid_for_loader);
  10134. }
  10135. mutex_unlock(&cd->system_lock);
  10136. }
  10137. /* Exit BL due to XY_PIN case or any other cause to be in BL */
  10138. if (cd->mode == PT_MODE_BOOTLOADER) {
  10139. rc = pt_hid_output_bl_launch_app_(cd);
  10140. if (rc < 0) {
  10141. pt_debug(cd->dev, DL_ERROR,
  10142. "%s: Error on launch app r=%d\n",
  10143. __func__, rc);
  10144. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  10145. goto reset;
  10146. rc = -ENODEV;
  10147. goto exit;
  10148. }
  10149. /* sleep to allow FW to be launched if available */
  10150. msleep(120);
  10151. /* Ensure the DUT is now in Application mode */
  10152. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  10153. if (rc < 0) {
  10154. pt_debug(cd->dev, DL_ERROR,
  10155. "%s: Error getting HID Desc r=%d\n",
  10156. __func__, rc);
  10157. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  10158. goto reset;
  10159. rc = -EIO;
  10160. goto exit;
  10161. }
  10162. cd->mode = pt_get_mode(cd, &hid_desc);
  10163. if (cd->mode == PT_MODE_BOOTLOADER) {
  10164. pt_debug(cd->dev, DL_WARN,
  10165. "%s: Error confiming exit BL\n",
  10166. __func__);
  10167. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  10168. goto reset;
  10169. rc = -ENODEV;
  10170. goto exit;
  10171. }
  10172. }
  10173. pt_debug(cd->dev, DL_INFO, "%s: Operational mode\n", __func__);
  10174. cd->mode = PT_MODE_OPERATIONAL;
  10175. *enum_status |= STARTUP_STATUS_GET_DESC;
  10176. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  10177. } else {
  10178. /* Generation is PIP2 Capable */
  10179. pt_debug(cd->dev, DL_INFO,
  10180. "%s: PIP2 Enumeration start\n", __func__);
  10181. rc = pt_pip2_get_status_(cd);
  10182. if (rc) {
  10183. pt_debug(cd->dev, DL_ERROR,
  10184. "%s: Get mode failed, mode unknown\n",
  10185. __func__);
  10186. mode = PT_MODE_UNKNOWN;
  10187. } else {
  10188. mode = cd->dut_status.mode;
  10189. detected = true;
  10190. }
  10191. cd->mode = mode;
  10192. switch (cd->mode) {
  10193. case PT_MODE_OPERATIONAL:
  10194. pt_debug(cd->dev, DL_INFO,
  10195. "%s: Operational mode\n", __func__);
  10196. protocol_mode = cd->dut_status.protocol_mode;
  10197. if (cd->app_pip_ver_ready == false) {
  10198. rc = pt_pip2_get_version_(cd);
  10199. if (!rc)
  10200. cd->app_pip_ver_ready = true;
  10201. else {
  10202. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  10203. goto reset;
  10204. goto exit;
  10205. }
  10206. }
  10207. break;
  10208. case PT_MODE_BOOTLOADER:
  10209. pt_debug(cd->dev, DL_INFO,
  10210. "%s: Bootloader mode\n", __func__);
  10211. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  10212. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  10213. mutex_lock(&cd->system_lock);
  10214. if (!rc) {
  10215. cd->pid_for_loader = pid;
  10216. pt_debug(cd->dev, DL_INFO,
  10217. "%s: Panel ID: 0x%02X\n",
  10218. __func__, cd->pid_for_loader);
  10219. } else {
  10220. cd->pid_for_loader =
  10221. PANEL_ID_NOT_ENABLED;
  10222. pt_debug(cd->dev, DL_WARN,
  10223. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  10224. __func__, cd->pid_for_loader);
  10225. }
  10226. mutex_unlock(&cd->system_lock);
  10227. }
  10228. if (cd->bl_pip_ver_ready == false) {
  10229. rc = pt_pip2_get_version_(cd);
  10230. if (!rc)
  10231. cd->bl_pip_ver_ready = true;
  10232. else {
  10233. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  10234. goto reset;
  10235. goto exit;
  10236. }
  10237. }
  10238. /*
  10239. * Launch app command will fail in flashless mode.
  10240. * Skip launch app command here to save time for
  10241. * enumeration flow.
  10242. */
  10243. if (cd->flashless_dut)
  10244. goto exit;
  10245. /*
  10246. * pt_pip2_launch_app() is needed here instead of
  10247. * pt_pip2_exit_bl() because exit_bl will cause several
  10248. * hw_resets to occur and the auto BL on a flashless
  10249. * DUT will fail.
  10250. */
  10251. rc = pt_pip2_launch_app(cd->dev,
  10252. PT_CORE_CMD_UNPROTECTED);
  10253. if (rc) {
  10254. pt_debug(cd->dev, DL_ERROR,
  10255. "%s: Error on launch app r=%d\n",
  10256. __func__, rc);
  10257. msleep(50);
  10258. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  10259. goto reset;
  10260. goto exit;
  10261. }
  10262. /*
  10263. * IRQ thread can be delayed if the serial log print is
  10264. * enabled. It causes next command to get wrong response
  10265. * Here the delay is to ensure pt_parse_input() to be
  10266. * finished.
  10267. */
  10268. msleep(60);
  10269. /* Check and update the mode */
  10270. rc = pt_pip2_get_status_(cd);
  10271. if (rc) {
  10272. pt_debug(cd->dev, DL_ERROR,
  10273. "%s: Get mode failed, mode unknown\n",
  10274. __func__);
  10275. mode = PT_MODE_UNKNOWN;
  10276. } else
  10277. mode = cd->dut_status.mode;
  10278. cd->mode = mode;
  10279. if (cd->mode == PT_MODE_OPERATIONAL) {
  10280. pt_debug(cd->dev, DL_INFO,
  10281. "%s: Launched to Operational mode\n",
  10282. __func__);
  10283. protocol_mode = cd->dut_status.protocol_mode;
  10284. } else if (cd->mode == PT_MODE_BOOTLOADER) {
  10285. pt_debug(cd->dev, DL_ERROR,
  10286. "%s: Launch failed, Bootloader mode\n",
  10287. __func__);
  10288. goto exit;
  10289. } else if (cd->mode == PT_MODE_UNKNOWN) {
  10290. pt_debug(cd->dev, DL_ERROR,
  10291. "%s: Launch failed, Unknown mode\n",
  10292. __func__);
  10293. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  10294. goto reset;
  10295. goto exit;
  10296. }
  10297. if (cd->app_pip_ver_ready == false) {
  10298. rc = pt_pip2_get_version_(cd);
  10299. if (!rc)
  10300. cd->app_pip_ver_ready = true;
  10301. else {
  10302. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  10303. goto reset;
  10304. goto exit;
  10305. }
  10306. }
  10307. break;
  10308. default:
  10309. pt_debug(cd->dev, DL_ERROR,
  10310. "%s: Unknown mode\n", __func__);
  10311. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  10312. goto reset;
  10313. goto exit;
  10314. }
  10315. if (cd->dut_status.protocol_mode == PT_PROTOCOL_MODE_HID) {
  10316. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  10317. if (!rc)
  10318. *enum_status |= STARTUP_STATUS_GET_DESC;
  10319. } else {
  10320. *enum_status |= STARTUP_STATUS_GET_DESC;
  10321. }
  10322. }
  10323. pt_init_pip_report_fields(cd);
  10324. if (protocol_mode == PT_PROTOCOL_MODE_HID) {
  10325. rc = pt_get_report_descriptor_(cd);
  10326. if (rc < 0) {
  10327. pt_debug(cd->dev, DL_ERROR,
  10328. "%s: Error on getting report descriptor r=%d\n",
  10329. __func__, rc);
  10330. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  10331. goto reset;
  10332. goto exit;
  10333. }
  10334. }
  10335. *enum_status |= STARTUP_STATUS_GET_RPT_DESC;
  10336. if (!cd->features.easywake)
  10337. cd->easy_wakeup_gesture = PT_CORE_EWG_NONE;
  10338. pt_debug(cd->dev, DL_INFO, "%s: Reading sysinfo\n", __func__);
  10339. rc = pt_hid_output_get_sysinfo_(cd);
  10340. if (rc) {
  10341. pt_debug(cd->dev, DL_ERROR,
  10342. "%s: Error on getting sysinfo r=%d\n", __func__, rc);
  10343. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  10344. goto reset;
  10345. goto exit;
  10346. }
  10347. *enum_status |= STARTUP_STATUS_GET_SYS_INFO;
  10348. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  10349. rc = _pt_poll_for_fw_exit_boot_mode(cd, 10000, &wait_time);
  10350. if (!rc) {
  10351. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  10352. pt_debug(cd->dev, DL_WARN,
  10353. "%s: Exit FW BOOT Mode after %dms\n",
  10354. __func__, wait_time);
  10355. } else {
  10356. pt_debug(cd->dev, DL_WARN,
  10357. "%s: FW stuck in BOOT Mode after %dms\n",
  10358. __func__, wait_time);
  10359. goto exit;
  10360. }
  10361. pt_debug(cd->dev, DL_INFO, "%s pt Prot Version: %d.%d\n",
  10362. __func__,
  10363. cd->sysinfo.ttdata.pip_ver_major,
  10364. cd->sysinfo.ttdata.pip_ver_minor);
  10365. rc = pt_get_ic_crc_(cd, PT_TCH_PARM_EBID);
  10366. if (rc) {
  10367. pt_debug(cd->dev, DL_ERROR,
  10368. "%s: DUT Config block CRC failure rc=%d\n",
  10369. __func__, rc);
  10370. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  10371. goto reset;
  10372. } else {
  10373. _pt_get_fw_sys_mode(cd, &sys_mode, NULL);
  10374. if (sys_mode != FW_SYS_MODE_SCANNING) {
  10375. pt_debug(cd->dev, DL_ERROR,
  10376. "%s: scan state: %d, retry: %d\n",
  10377. __func__, sys_mode, try);
  10378. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  10379. goto reset;
  10380. } else
  10381. *enum_status |= STARTUP_STATUS_GET_CFG_CRC;
  10382. }
  10383. rc = pt_restore_parameters_(cd);
  10384. if (rc) {
  10385. pt_debug(cd->dev, DL_ERROR,
  10386. "%s: Failed to restore parameters rc=%d\n",
  10387. __func__, rc);
  10388. } else
  10389. *enum_status |= STARTUP_STATUS_RESTORE_PARM;
  10390. call_atten_cb(cd, PT_ATTEN_STARTUP, 0);
  10391. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  10392. cd->startup_retry_count = 0;
  10393. exit:
  10394. /* Generate the HW Version of the connected DUT and store in cd */
  10395. pt_generate_hw_version(cd, cd->hw_version);
  10396. pt_debug(cd->dev, DL_WARN, "%s: HW Version: %s\n", __func__,
  10397. cd->hw_version);
  10398. pt_start_wd_timer(cd);
  10399. if (!detected)
  10400. rc = -ENODEV;
  10401. #ifdef TTHE_TUNER_SUPPORT
  10402. tthe_print(cd, NULL, 0, "exit startup");
  10403. #endif
  10404. pt_debug(cd->dev, DL_WARN,
  10405. "%s: Completed Enumeration rc=%d On Attempt %d\n",
  10406. __func__, rc, try);
  10407. return rc;
  10408. }
  10409. /*******************************************************************************
  10410. * FUNCTION: pt_enum_with_dut
  10411. *
  10412. * SUMMARY: This is the safe function wrapper for pt_enum_with_dut_() by
  10413. * requesting exclusive access around the call.
  10414. *
  10415. * RETURN:
  10416. * 0 = success
  10417. * !0 = failure
  10418. *
  10419. * PARAMETERS:
  10420. * *cd - pointer the core data structure
  10421. * reset - Flag to reset the DUT before attempting to enumerate
  10422. * *status - pointer to store the enum status bitmask flags
  10423. ******************************************************************************/
  10424. static int pt_enum_with_dut(struct pt_core_data *cd, bool reset, u32 *status)
  10425. {
  10426. int rc = 0;
  10427. mutex_lock(&cd->system_lock);
  10428. cd->startup_state = STARTUP_RUNNING;
  10429. mutex_unlock(&cd->system_lock);
  10430. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  10431. if (rc) {
  10432. pt_debug(cd->dev, DL_ERROR,
  10433. "%s: fail get exclusive ex=%p own=%p\n",
  10434. __func__, cd->exclusive_dev, cd->dev);
  10435. goto exit;
  10436. }
  10437. rc = pt_enum_with_dut_(cd, reset, status);
  10438. if (release_exclusive(cd, cd->dev) < 0)
  10439. /* Don't return fail code, mode is already changed. */
  10440. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  10441. __func__);
  10442. else
  10443. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  10444. __func__);
  10445. exit:
  10446. mutex_lock(&cd->system_lock);
  10447. /* Clear startup state for any tasks waiting for startup completion */
  10448. cd->startup_state = STARTUP_NONE;
  10449. mutex_unlock(&cd->system_lock);
  10450. /* Set STATUS_COMPLETE bit to indicate the status is ready to be read */
  10451. *status |= STARTUP_STATUS_COMPLETE;
  10452. /* Wake the waiters for end of startup */
  10453. wake_up(&cd->wait_q);
  10454. return rc;
  10455. }
  10456. #ifndef TTDL_KERNEL_SUBMISSION
  10457. static int add_sysfs_interfaces(struct device *dev);
  10458. static void remove_sysfs_interfaces(struct device *dev);
  10459. static void remove_sysfs_and_modules(struct device *dev);
  10460. #endif /*!TTDL_KERNEL_SUBMISSION */
  10461. static void pt_release_modules(struct pt_core_data *cd);
  10462. static void pt_probe_modules(struct pt_core_data *cd);
  10463. /*******************************************************************************
  10464. * FUNCTION: _pt_ttdl_restart
  10465. *
  10466. * SUMMARY: Restarts TTDL enumeration with the DUT and re-probes all modules
  10467. *
  10468. * NOTE: The function DOSE NOT remove sysfs and modules. Trying to create
  10469. * existing sysfs nodes will produce an error.
  10470. *
  10471. * RETURN:
  10472. * 0 = success
  10473. * !0 = failure
  10474. *
  10475. * PARAMETERS:
  10476. * *dev - pointer to core device
  10477. ******************************************************************************/
  10478. static int _pt_ttdl_restart(struct device *dev)
  10479. {
  10480. int rc = 0;
  10481. struct pt_core_data *cd = dev_get_drvdata(dev);
  10482. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  10483. struct i2c_client *client =
  10484. (struct i2c_client *)container_of(dev, struct i2c_client, dev);
  10485. #endif
  10486. /*
  10487. * Make sure the device is awake, pt_mt_release function will
  10488. * cause pm sleep function and lead to deadlock.
  10489. */
  10490. pm_runtime_get_sync(dev);
  10491. /* Use ttdl_restart_lock to avoid reentry */
  10492. mutex_lock(&cd->ttdl_restart_lock);
  10493. remove_sysfs_and_modules(cd->dev);
  10494. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  10495. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  10496. pt_debug(dev, DL_ERROR,
  10497. "%s I2C functionality not Supported\n", __func__);
  10498. rc = -EIO;
  10499. goto ttdl_no_error;
  10500. }
  10501. #endif
  10502. if (cd->active_dut_generation == DUT_UNKNOWN) {
  10503. rc = _pt_detect_dut_generation(cd->dev,
  10504. &cd->startup_status, &cd->active_dut_generation,
  10505. &cd->mode);
  10506. if ((cd->active_dut_generation == DUT_UNKNOWN) || (rc)) {
  10507. pt_debug(dev, DL_ERROR,
  10508. "%s: Error, Unknown DUT Generation rc=%d\n",
  10509. __func__, rc);
  10510. }
  10511. }
  10512. rc = add_sysfs_interfaces(cd->dev);
  10513. if (rc < 0)
  10514. pt_debug(cd->dev, DL_ERROR,
  10515. "%s: Error, failed sysfs nodes rc=%d\n",
  10516. __func__, rc);
  10517. if (!(cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL)) {
  10518. pt_debug(dev, DL_INFO, "%s: Call pt_enum_with_dut\n", __func__);
  10519. rc = pt_enum_with_dut(cd, true, &cd->startup_status);
  10520. if (rc < 0)
  10521. pt_debug(dev, DL_ERROR,
  10522. "%s: Error, Failed to Enumerate\n", __func__);
  10523. }
  10524. rc = pt_mt_probe(dev);
  10525. if (rc < 0) {
  10526. pt_debug(dev, DL_ERROR,
  10527. "%s: Error, fail mt probe\n", __func__);
  10528. }
  10529. rc = pt_btn_probe(dev);
  10530. if (rc < 0) {
  10531. pt_debug(dev, DL_ERROR,
  10532. "%s: Error, fail btn probe\n", __func__);
  10533. }
  10534. pt_probe_modules(cd);
  10535. pt_debug(cd->dev, DL_WARN,
  10536. "%s: Well Done! TTDL Restart Completed\n", __func__);
  10537. rc = 0;
  10538. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  10539. ttdl_no_error:
  10540. #endif
  10541. mutex_unlock(&cd->ttdl_restart_lock);
  10542. mutex_lock(&cd->system_lock);
  10543. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  10544. cd->startup_state = STARTUP_NONE;
  10545. mutex_unlock(&cd->system_lock);
  10546. pm_runtime_put(dev);
  10547. return rc;
  10548. }
  10549. /*******************************************************************************
  10550. * FUNCTION: pt_restart_work_function
  10551. *
  10552. * SUMMARY: This is the wrapper function placed in a work queue to call
  10553. * _pt_ttdl_restart()
  10554. *
  10555. * RETURN: none
  10556. *
  10557. * PARAMETERS:
  10558. * *work - pointer to the work_struct
  10559. ******************************************************************************/
  10560. static void pt_restart_work_function(struct work_struct *work)
  10561. {
  10562. struct pt_core_data *cd = container_of(work,
  10563. struct pt_core_data, ttdl_restart_work);
  10564. int rc = 0;
  10565. rc = _pt_ttdl_restart(cd->dev);
  10566. if (rc < 0)
  10567. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  10568. __func__, rc);
  10569. }
  10570. /*******************************************************************************
  10571. * FUNCTION: pt_enum_work_function
  10572. *
  10573. * SUMMARY: This is the wrapper function placed in a work queue to call
  10574. * pt_enum_with_dut()
  10575. *
  10576. * RETURN: none
  10577. *
  10578. * PARAMETERS:
  10579. * *work - pointer to the work_struct
  10580. ******************************************************************************/
  10581. static void pt_enum_work_function(struct work_struct *work)
  10582. {
  10583. struct pt_core_data *cd = container_of(work,
  10584. struct pt_core_data, enum_work);
  10585. int rc;
  10586. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  10587. if (rc < 0)
  10588. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  10589. __func__, rc);
  10590. }
  10591. #if (KERNEL_VERSION(3, 19, 0) <= LINUX_VERSION_CODE)
  10592. #define KERNEL_VER_GT_3_19
  10593. #endif
  10594. #if defined(CONFIG_PM_RUNTIME) || defined(KERNEL_VER_GT_3_19)
  10595. /* CONFIG_PM_RUNTIME option is removed in 3.19.0 */
  10596. #if defined(CONFIG_PM_SLEEP)
  10597. /*******************************************************************************
  10598. * FUNCTION: pt_core_rt_suspend
  10599. *
  10600. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_sleep.
  10601. *
  10602. * RETURN:
  10603. * 0 = success
  10604. * !0 = failure
  10605. *
  10606. * PARAMETERS:
  10607. * *dev - pointer to core device
  10608. ******************************************************************************/
  10609. static int pt_core_rt_suspend(struct device *dev)
  10610. {
  10611. struct pt_core_data *cd = dev_get_drvdata(dev);
  10612. int rc = 0;
  10613. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  10614. return 0;
  10615. rc = pt_core_sleep(cd);
  10616. if (rc < 0) {
  10617. pt_debug(dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  10618. return -EAGAIN;
  10619. }
  10620. return 0;
  10621. }
  10622. /*******************************************************************************
  10623. * FUNCTION: pt_core_rt_resume
  10624. *
  10625. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_wake.
  10626. *
  10627. * RETURN:
  10628. * 0 = success
  10629. * !0 = failure
  10630. *
  10631. * PARAMETERS:
  10632. * *dev - pointer to core device
  10633. ******************************************************************************/
  10634. static int pt_core_rt_resume(struct device *dev)
  10635. {
  10636. struct pt_core_data *cd = dev_get_drvdata(dev);
  10637. int rc = 0;
  10638. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  10639. return 0;
  10640. rc = pt_core_wake(cd);
  10641. if (rc < 0) {
  10642. pt_debug(dev, DL_ERROR, "%s: Error on wake\n", __func__);
  10643. return -EAGAIN;
  10644. }
  10645. return 0;
  10646. }
  10647. #endif /* CONFIG_PM_SLEEP */
  10648. #endif /* CONFIG_PM_RUNTIME || LINUX_VERSION_CODE */
  10649. #if defined(CONFIG_PM_SLEEP)
  10650. /*******************************************************************************
  10651. * FUNCTION: pt_core_suspend_
  10652. *
  10653. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  10654. * pt_core_sleep. This function may disable IRQ during sleep state.
  10655. *
  10656. * RETURN:
  10657. * 0 = success
  10658. * !0 = failure
  10659. *
  10660. * PARAMETERS:
  10661. * *dev - pointer to core device
  10662. ******************************************************************************/
  10663. static int pt_core_suspend_(struct device *dev)
  10664. {
  10665. struct pt_core_data *cd = dev_get_drvdata(dev);
  10666. pt_core_sleep(cd);
  10667. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  10668. return 0;
  10669. /* Required to prevent interrupts before bus awake */
  10670. disable_irq(cd->irq);
  10671. cd->irq_disabled = 1;
  10672. if (device_may_wakeup(dev)) {
  10673. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  10674. __func__);
  10675. if (!enable_irq_wake(cd->irq))
  10676. cd->irq_wake = 1;
  10677. } else {
  10678. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  10679. __func__);
  10680. }
  10681. return 0;
  10682. }
  10683. /*******************************************************************************
  10684. * FUNCTION: pt_core_suspend
  10685. *
  10686. * SUMMARY: Wrapper function of pt_core_suspend_() to help avoid TP from being
  10687. * woke up or put to sleep based on Kernel power state even when the display
  10688. * is off based on the check of TTDL core platform flag.
  10689. *
  10690. * RETURN:
  10691. * 0 = success
  10692. * !0 = failure
  10693. *
  10694. * PARAMETERS:
  10695. * *dev - pointer to core device
  10696. ******************************************************************************/
  10697. static int pt_core_suspend(struct device *dev)
  10698. {
  10699. struct pt_core_data *cd = dev_get_drvdata(dev);
  10700. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  10701. return 0;
  10702. return pt_core_suspend_(dev);
  10703. }
  10704. /*******************************************************************************
  10705. * FUNCTION: pt_core_resume_
  10706. *
  10707. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  10708. * pt_core_wake. This function may enable IRQ before wake up.
  10709. *
  10710. * RETURN:
  10711. * 0 = success
  10712. * !0 = failure
  10713. *
  10714. * PARAMETERS:
  10715. * *dev - pointer to core device
  10716. ******************************************************************************/
  10717. static int pt_core_resume_(struct device *dev)
  10718. {
  10719. struct pt_core_data *cd = dev_get_drvdata(dev);
  10720. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  10721. goto exit;
  10722. /*
  10723. * Bus pm does not call suspend if device runtime suspended
  10724. * This flag is covers that case
  10725. */
  10726. if (cd->irq_disabled) {
  10727. enable_irq(cd->irq);
  10728. cd->irq_disabled = 0;
  10729. }
  10730. if (device_may_wakeup(dev)) {
  10731. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  10732. __func__);
  10733. if (cd->irq_wake) {
  10734. disable_irq_wake(cd->irq);
  10735. cd->irq_wake = 0;
  10736. }
  10737. } else {
  10738. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  10739. __func__);
  10740. }
  10741. exit:
  10742. pt_core_wake(cd);
  10743. return 0;
  10744. }
  10745. /*******************************************************************************
  10746. * FUNCTION: pt_core_resume
  10747. *
  10748. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  10749. * along with kernel power state even the display is off based on the check of
  10750. * TTDL core platform flag.
  10751. *
  10752. * RETURN:
  10753. * 0 = success
  10754. * !0 = failure
  10755. *
  10756. * PARAMETERS:
  10757. * *dev - pointer to core device
  10758. ******************************************************************************/
  10759. static int pt_core_resume(struct device *dev)
  10760. {
  10761. struct pt_core_data *cd = dev_get_drvdata(dev);
  10762. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  10763. return 0;
  10764. return pt_core_resume_(dev);
  10765. }
  10766. #endif
  10767. #ifdef NEED_SUSPEND_NOTIFIER
  10768. /*******************************************************************************
  10769. * FUNCTION: pt_pm_notifier
  10770. *
  10771. * SUMMARY: This function is registered to notifier chain and will perform
  10772. * suspend operation if match event PM_SUSPEND_PREPARE.
  10773. *
  10774. * RETURN:
  10775. * 0 = success
  10776. * !0 = failure
  10777. *
  10778. * PARAMETERS:
  10779. * *nb - pointer to notifier_block structure
  10780. * action - notifier event type
  10781. * *data - void pointer
  10782. ******************************************************************************/
  10783. static int pt_pm_notifier(struct notifier_block *nb,
  10784. unsigned long action, void *data)
  10785. {
  10786. struct pt_core_data *cd = container_of(nb,
  10787. struct pt_core_data, pm_notifier);
  10788. if (action == PM_SUSPEND_PREPARE) {
  10789. pt_debug(cd->dev, DL_INFO, "%s: Suspend prepare\n",
  10790. __func__);
  10791. /*
  10792. * If PM runtime is not suspended, either call runtime
  10793. * PM suspend callback or wait until it finishes
  10794. */
  10795. if (!pm_runtime_suspended(cd->dev))
  10796. pm_runtime_suspend(cd->dev);
  10797. (void) pt_core_suspend(cd->dev);
  10798. }
  10799. return NOTIFY_DONE;
  10800. }
  10801. #endif
  10802. const struct dev_pm_ops pt_pm_ops = {
  10803. SET_SYSTEM_SLEEP_PM_OPS(pt_core_suspend, pt_core_resume)
  10804. SET_RUNTIME_PM_OPS(pt_core_rt_suspend, pt_core_rt_resume,
  10805. NULL)
  10806. };
  10807. EXPORT_SYMBOL_GPL(pt_pm_ops);
  10808. /*******************************************************************************
  10809. * FUNCTION: _pt_request_pip2_enter_bl
  10810. *
  10811. * SUMMARY: Force the DUT to enter the BL by resetting the DUT by use of the
  10812. * XRES pin or a soft reset.
  10813. *
  10814. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  10815. * the WD active could cause this function to fail!
  10816. * NOTE: If start_mode is passed in as PT_MODE_IGNORE, this function
  10817. * will not try to determine the current mode but will proceed with
  10818. * resetting the DUT and entering the BL.
  10819. *
  10820. * NOTE: The definition of result code:
  10821. * PT_ENTER_BL_PASS (0)
  10822. * PT_ENTER_BL_ERROR (1)
  10823. * PT_ENTER_BL_RESET_FAIL (2)
  10824. * PT_ENTER_BL_HID_START_BL_FAIL (3)
  10825. * PT_ENTER_BL_CONFIRM_FAIL (4)
  10826. * PT_ENTER_BL_GET_FLASH_INFO_FAIL (5)
  10827. *
  10828. * RETURNS:
  10829. * 0 = success
  10830. * !0 = failure
  10831. *
  10832. *
  10833. * PARAMETERS:
  10834. * *dev - pointer to device structure
  10835. * *start_mode - pointer to the mode the DUT was in when this function
  10836. * starts
  10837. * *result - pointer to store the result when to enter BL
  10838. ******************************************************************************/
  10839. int _pt_request_pip2_enter_bl(struct device *dev, u8 *start_mode, int *result)
  10840. {
  10841. int rc = 0;
  10842. int t;
  10843. int tmp_result = PT_ENTER_BL_ERROR;
  10844. int flash_info_retry = 2;
  10845. u8 mode = PT_MODE_UNKNOWN;
  10846. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  10847. u8 read_buf[32];
  10848. u16 actual_read_len;
  10849. struct pt_core_data *cd = dev_get_drvdata(dev);
  10850. u8 host_mode_cmd[4] = {0xA5, 0xA5, 0xA5, 0xA5};
  10851. u8 time = 0;
  10852. u8 saved_flashless_auto_bl_mode = cd->flashless_auto_bl;
  10853. if (cd->watchdog_enabled) {
  10854. pt_debug(dev, DL_WARN,
  10855. "%s: Watchdog must be stopped before entering BL\n",
  10856. __func__);
  10857. goto exit;
  10858. }
  10859. cancel_work_sync(&cd->enum_work);
  10860. cancel_work_sync(&cd->watchdog_work);
  10861. /* if undefined assume operational/test to bypass all checks */
  10862. if (*start_mode == PT_MODE_IGNORE) {
  10863. mode = PT_MODE_OPERATIONAL;
  10864. sys_mode = FW_SYS_MODE_TEST;
  10865. pt_debug(dev, DL_INFO, "%s: Assume Mode = %d", __func__, mode);
  10866. } else if (*start_mode == PT_MODE_UNKNOWN) {
  10867. rc = pt_pip2_get_mode_sysmode_(cd, &mode, &sys_mode);
  10868. if (rc) {
  10869. pt_debug(dev, DL_ERROR,
  10870. "%s: Get mode failed, mode unknown\n",
  10871. __func__);
  10872. }
  10873. *start_mode = mode;
  10874. pt_debug(dev, DL_INFO, "%s: Get Mode = %d", __func__, mode);
  10875. } else if (*start_mode == PT_MODE_OPERATIONAL) {
  10876. /* Assume SCANNIING mode to avoid doing an extra get_mode */
  10877. sys_mode = FW_SYS_MODE_SCANNING;
  10878. }
  10879. _retry:
  10880. /* For Flashless DUTs - Suppress auto BL on next BL sentinel */
  10881. pt_debug(dev, DL_INFO, "%s: Flashless Auto_BL - SUPPRESS\n", __func__);
  10882. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  10883. switch (mode) {
  10884. case PT_MODE_UNKNOWN:
  10885. /*
  10886. * When the mode could not be determined the DUT could be
  10887. * in App mode running corrupted FW or FW that is not
  10888. * responding to the mode request, assume no communication
  10889. * and do a hard reset
  10890. */
  10891. mutex_lock(&cd->system_lock);
  10892. cd->startup_status = STARTUP_STATUS_START;
  10893. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  10894. mutex_unlock(&cd->system_lock);
  10895. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  10896. if (rc) {
  10897. tmp_result = PT_ENTER_BL_RESET_FAIL;
  10898. goto exit;
  10899. }
  10900. break;
  10901. case PT_MODE_OPERATIONAL:
  10902. if (sys_mode == FW_SYS_MODE_SCANNING) {
  10903. pt_debug(dev, DL_INFO, "%s: Suspend Scanning\n",
  10904. __func__);
  10905. rc = pt_pip_suspend_scanning_(cd);
  10906. if (rc) {
  10907. /*
  10908. * Print to log but don't exit, the FW could be
  10909. * running but be hung or fail to respond to
  10910. * this request
  10911. */
  10912. pt_debug(dev, DL_ERROR,
  10913. "%s Suspend Scan Failed\n", __func__);
  10914. }
  10915. /* sleep to allow the suspend scan to be processed */
  10916. usleep_range(1000, 2000);
  10917. }
  10918. mutex_lock(&cd->system_lock);
  10919. cd->startup_status = STARTUP_STATUS_START;
  10920. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  10921. mutex_unlock(&cd->system_lock);
  10922. /* Reset device to enter the BL */
  10923. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  10924. if (rc) {
  10925. tmp_result = PT_ENTER_BL_RESET_FAIL;
  10926. goto exit;
  10927. }
  10928. break;
  10929. case PT_MODE_BOOTLOADER:
  10930. /* Do nothing as we are already in the BL */
  10931. tmp_result = PT_ENTER_BL_PASS;
  10932. goto exit;
  10933. default:
  10934. /* Should NEVER get here */
  10935. tmp_result = PT_ENTER_BL_ERROR;
  10936. pt_debug(dev, DL_ERROR, "%s: Unknown mode code\n", __func__);
  10937. goto exit;
  10938. }
  10939. if (!cd->flashless_dut &&
  10940. (mode == PT_MODE_UNKNOWN || mode == PT_MODE_OPERATIONAL)) {
  10941. /*
  10942. * Sending the special "Host Mode" command will instruct the
  10943. * BL to not execute the FW it has loaded into RAM.
  10944. * The command must be sent within a 40ms window from releasing
  10945. * the XRES pin. If the messages is sent too early it will NAK,
  10946. * so keep sending it every 2ms until it is accepted by the BL.
  10947. * A no-flash DUT does not require this command as there is no
  10948. * FW for the BL to load and execute.
  10949. */
  10950. usleep_range(4000, 6000);
  10951. pt_debug(cd->dev, DL_INFO,
  10952. ">>> %s: Write Buffer Size[%d] Stay in BL\n",
  10953. __func__, (int)sizeof(host_mode_cmd));
  10954. pt_pr_buf(cd->dev, DL_DEBUG, host_mode_cmd,
  10955. (int)sizeof(host_mode_cmd), ">>> User CMD");
  10956. rc = 1;
  10957. while (rc && time < 34) {
  10958. rc = pt_adap_write_read_specific(cd, 4,
  10959. host_mode_cmd, NULL, 0);
  10960. usleep_range(1800, 2000);
  10961. time += 2;
  10962. }
  10963. /* Sleep to allow the BL to come up */
  10964. usleep_range(1000, 2000);
  10965. }
  10966. /*
  10967. * To avoid the case that next PIP command can be confused by BL/FW
  10968. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  10969. * followed by "wakeup event" function can lower the failure rate.
  10970. */
  10971. t = wait_event_timeout(cd->wait_q,
  10972. ((cd->startup_status != STARTUP_STATUS_START)
  10973. && (cd->hid_reset_cmd_state == 0)),
  10974. msecs_to_jiffies(300));
  10975. if (IS_TMO(t)) {
  10976. pt_debug(cd->dev, DL_ERROR,
  10977. "%s: TMO waiting for BL sentinel\n", __func__);
  10978. }
  10979. /* Check if device is now in BL mode */
  10980. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  10981. pt_debug(dev, DL_INFO, "%s: Mode=%d, Status=0x%04X\n", __func__, mode,
  10982. cd->startup_status);
  10983. if (!rc && mode == PT_MODE_BOOTLOADER) {
  10984. pt_debug(dev, DL_INFO, "%s In bootloader mode now\n", __func__);
  10985. mutex_lock(&cd->system_lock);
  10986. cd->pip2_prot_active = true;
  10987. cd->mode = PT_MODE_BOOTLOADER;
  10988. mutex_unlock(&cd->system_lock);
  10989. tmp_result = PT_ENTER_BL_PASS;
  10990. } else {
  10991. /*
  10992. * If the device doesn't enter BL mode as expected and rc is
  10993. * tested pass by above function pt_pip2_get_mode_sysmode_(),
  10994. * the function should return an error code to indicate this
  10995. * failure PT_ENTER_BL_CONFIRM_FAIL.
  10996. */
  10997. if (!rc)
  10998. rc = -EINVAL;
  10999. tmp_result = PT_ENTER_BL_CONFIRM_FAIL;
  11000. mutex_lock(&cd->system_lock);
  11001. cd->mode = mode;
  11002. mutex_unlock(&cd->system_lock);
  11003. pt_debug(dev, DL_ERROR,
  11004. "%s ERROR: Not in BL as expected", __func__);
  11005. }
  11006. exit:
  11007. if (!cd->flashless_dut && (tmp_result == PT_ENTER_BL_PASS)) {
  11008. /* Check to get (buffered) flash information */
  11009. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  11010. PIP2_CMD_ID_FLASH_INFO, NULL, 0,
  11011. read_buf, &actual_read_len);
  11012. if (!rc) {
  11013. if (read_buf[PIP2_RESP_BODY_OFFSET] == 0) {
  11014. pt_debug(
  11015. dev, DL_WARN,
  11016. "%s Unavailable Manufacturer ID: 0x%02x\n",
  11017. __func__,
  11018. read_buf[PIP2_RESP_BODY_OFFSET]);
  11019. /*
  11020. * If the BL was unable to cache the correct
  11021. * values when entering the first time due to
  11022. * the Flash part not having been powered up
  11023. * long enough, re-enter the BL to trigger the
  11024. * BL to re-attempt to cache the values.
  11025. */
  11026. if (flash_info_retry-- > 0) {
  11027. mode = PT_MODE_UNKNOWN;
  11028. pt_debug(dev, DL_WARN,
  11029. "%s Assume mode to UNKNOWN to enter BL again, retry=%d\n",
  11030. __func__, flash_info_retry);
  11031. goto _retry;
  11032. } else {
  11033. pt_debug(dev, DL_WARN,
  11034. "%s Manufacturer ID Unknown\n",
  11035. __func__);
  11036. tmp_result = PT_ENTER_BL_PASS;
  11037. }
  11038. }
  11039. } else {
  11040. tmp_result = PT_ENTER_BL_GET_FLASH_INFO_FAIL;
  11041. pt_debug(
  11042. dev, DL_ERROR,
  11043. "%s: Failed to send PIP2 READ_FLASH_INFO cmd\n",
  11044. __func__);
  11045. }
  11046. }
  11047. pt_debug(dev, DL_INFO, "%s: Flashless Auto_BL - %s\n", __func__,
  11048. saved_flashless_auto_bl_mode == PT_ALLOW_AUTO_BL ? "ALLOW" :
  11049. "SUPPRESS");
  11050. cd->flashless_auto_bl = saved_flashless_auto_bl_mode;
  11051. if (result)
  11052. *result = tmp_result;
  11053. return rc;
  11054. }
  11055. /*******************************************************************************
  11056. * FUNCTION: _pt_pip2_file_open
  11057. *
  11058. * SUMMARY: Using the BL PIP2 commands open a file and return the file handle
  11059. *
  11060. * NOTE: The DUT must be in BL mode for this command to work
  11061. *
  11062. * RETURNS:
  11063. * <0 = Error
  11064. * >0 = file handle opened
  11065. *
  11066. * PARAMETERS:
  11067. * *dev - pointer to device structure
  11068. * file_no - PIP2 file number to open
  11069. ******************************************************************************/
  11070. int _pt_pip2_file_open(struct device *dev, u8 file_no)
  11071. {
  11072. int rc = 0;
  11073. u16 status;
  11074. u16 actual_read_len;
  11075. u8 file_handle;
  11076. u8 data[2];
  11077. u8 read_buf[10];
  11078. u8 expected_len = pt_pip2_get_cmd_response_len(PIP2_CMD_ID_FILE_OPEN);
  11079. pt_debug(dev, DL_DEBUG, "%s: OPEN file %d\n", __func__, file_no);
  11080. data[0] = file_no;
  11081. rc = _pt_request_pip2_send_cmd(dev,
  11082. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_OPEN,
  11083. data, 1, read_buf, &actual_read_len);
  11084. if (rc) {
  11085. pt_debug(dev, DL_ERROR,
  11086. "%s: FILE_OPEN timeout for file=%d\n",
  11087. __func__, file_no);
  11088. return -PIP2_RSP_ERR_NOT_OPEN;
  11089. }
  11090. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  11091. if (rc || ((status != PIP2_RSP_ERR_NONE) &&
  11092. (status != PIP2_RSP_ERR_ALREADY_OPEN))) {
  11093. pt_debug(dev, DL_ERROR,
  11094. "%s: %s 0x%02X for file=%d\n",
  11095. __func__, "FILE_OPEN failure:", status, file_no);
  11096. return -status;
  11097. } else if (actual_read_len == expected_len) {
  11098. /* File_open returned a file handle */
  11099. file_handle = read_buf[PIP2_RESP_BODY_OFFSET];
  11100. if (file_handle != file_no) {
  11101. pt_debug(dev, DL_ERROR,
  11102. "%s: %s 0x%02X file=%d returned handle=%d\n",
  11103. __func__, "FILE_OPEN failure:",
  11104. status, file_no, file_handle);
  11105. return -status;
  11106. }
  11107. } else {
  11108. return -status;
  11109. }
  11110. return file_handle;
  11111. }
  11112. /*******************************************************************************
  11113. * FUNCTION: _pt_pip2_file_close
  11114. *
  11115. * SUMMARY: Using the BL PIP2 commands close a file
  11116. *
  11117. * NOTE: The DUT must be in BL mode for this command to work
  11118. *
  11119. * RETURNS:
  11120. * <0 = Error
  11121. * >0 = file handle closed
  11122. *
  11123. * PARAMETERS:
  11124. * *dev - pointer to device structure
  11125. * file_handle - handle to the file to be closed
  11126. ******************************************************************************/
  11127. int _pt_pip2_file_close(struct device *dev, u8 file_handle)
  11128. {
  11129. int ret = 0;
  11130. u16 status;
  11131. u16 actual_read_len;
  11132. u8 data[2];
  11133. u8 read_buf[32];
  11134. pt_debug(dev, DL_DEBUG, "%s: CLOSE file %d\n", __func__, file_handle);
  11135. data[0] = file_handle;
  11136. ret = _pt_request_pip2_send_cmd(dev,
  11137. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_CLOSE,
  11138. data, 1, read_buf, &actual_read_len);
  11139. if (ret) {
  11140. pt_debug(dev, DL_ERROR,
  11141. "%s ROM BL FILE_CLOSE timeout for file = %d\n",
  11142. __func__, file_handle);
  11143. return -ETIME;
  11144. }
  11145. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  11146. if (status != 0x00) {
  11147. pt_debug(dev, DL_ERROR,
  11148. "%s ROM BL FILE_CLOSE failure: 0x%02X for file = %d\n",
  11149. __func__, status, file_handle);
  11150. return -status;
  11151. }
  11152. return file_handle;
  11153. }
  11154. /*******************************************************************************
  11155. * FUNCTION: _pt_pip2_file_erase_by_file_no
  11156. *
  11157. * SUMMARY: Using the BL PIP2 commands erase a file by file number only.
  11158. *
  11159. * NOTE: The DUT must be in BL mode for this command to work
  11160. * NOTE: Some FLASH parts can time out while erasing one or more sectors,
  11161. * one retry is attempted for each sector in a file.
  11162. *
  11163. * RETURNS:
  11164. * <0 = Error
  11165. * >0 = file handle closed
  11166. *
  11167. * PARAMETERS:
  11168. * *dev - pointer to device structure
  11169. * file_handle - handle to the file to be erased
  11170. * *status - PIP2 erase status code
  11171. ******************************************************************************/
  11172. int _pt_pip2_file_erase_by_file_no(struct device *dev, u8 file_handle,
  11173. int *status)
  11174. {
  11175. int ret = 0;
  11176. int max_retry = PT_PIP2_MAX_FILE_SIZE/PT_PIP2_FILE_SECTOR_SIZE;
  11177. int retry = 1;
  11178. u16 actual_read_len;
  11179. u8 data[2];
  11180. u8 read_buf[10];
  11181. struct pt_core_data *cd = dev_get_drvdata(dev);
  11182. pt_debug(dev, DL_DEBUG, "%s: ERASE file %d\n", __func__, file_handle);
  11183. data[0] = file_handle;
  11184. data[1] = PIP2_FILE_IOCTL_CODE_ERASE_FILE;
  11185. *status = PIP2_RSP_ERR_TIMEOUT;
  11186. /* Increase waiting time for large file erase */
  11187. mutex_lock(&cd->system_lock);
  11188. cd->pip_cmd_timeout = PT_PIP2_CMD_FILE_ERASE_TIMEOUT;
  11189. mutex_unlock(&cd->system_lock);
  11190. while (*status == PIP2_RSP_ERR_TIMEOUT && retry < max_retry) {
  11191. ret = _pt_request_pip2_send_cmd(dev,
  11192. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  11193. data, 2, read_buf, &actual_read_len);
  11194. if (ret)
  11195. break;
  11196. *status = read_buf[PIP2_RESP_STATUS_OFFSET];
  11197. if (*status == PIP2_RSP_ERR_TIMEOUT) {
  11198. #ifdef TTDL_DIAGNOSTICS
  11199. cd->file_erase_timeout_count++;
  11200. #endif
  11201. pt_debug(dev, DL_WARN,
  11202. "%s: ERASE timeout %d for file = %d\n",
  11203. __func__, retry, file_handle);
  11204. }
  11205. retry++;
  11206. }
  11207. mutex_lock(&cd->system_lock);
  11208. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  11209. mutex_unlock(&cd->system_lock);
  11210. if (ret) {
  11211. pt_debug(dev, DL_ERROR,
  11212. "%s ROM FILE_ERASE cmd failure: %d for file = %d\n",
  11213. __func__, ret, file_handle);
  11214. return -EIO;
  11215. }
  11216. if (*status != 0x00) {
  11217. pt_debug(dev, DL_ERROR,
  11218. "%s ROM FILE_ERASE failure: 0x%02X for file = %d\n",
  11219. __func__, *status, file_handle);
  11220. return -EIO;
  11221. }
  11222. return file_handle;
  11223. }
  11224. /*******************************************************************************
  11225. * FUNCTION: _pt_pip2_file_erase_by_file_sector
  11226. *
  11227. * SUMMARY: Using the BL PIP2 commands erase a file sector by sector.
  11228. *
  11229. * NOTE: The DUT must be in BL mode for this command to work
  11230. * NOTE: Some FLASH parts can time out while erasing one or more sectors,
  11231. * one retry is attempted for each sector in a file.
  11232. *
  11233. * RETURNS:
  11234. * <0 = Error
  11235. * >0 = file handle closed
  11236. *
  11237. * PARAMETERS:
  11238. * *dev - pointer to device structure
  11239. * file_handle - handle to the file to be erased
  11240. * file_sector - Number of sectors to erase
  11241. * *status - PIP2 erase status code
  11242. ******************************************************************************/
  11243. int _pt_pip2_file_erase_by_file_sector(struct device *dev, u8 file_handle,
  11244. u16 file_sector, int *status)
  11245. {
  11246. int ret = 0, index = 0;
  11247. int max_retry = 3;
  11248. int retry = 0;
  11249. int tmp_status = PIP2_RSP_ERR_NONE;
  11250. u16 sector_to_erase = 1;
  11251. u16 actual_read_len;
  11252. u8 data[6];
  11253. u8 read_buf[10];
  11254. struct pt_core_data *cd = dev_get_drvdata(dev);
  11255. pt_debug(dev, DL_DEBUG, "%s: ERASE file=%d, sector=%d\n", __func__,
  11256. file_handle, file_sector);
  11257. data[0] = file_handle;
  11258. data[1] = PIP2_FILE_IOCTL_CODE_ERASE_FILE;
  11259. /* Set how many sectors to erase*/
  11260. data[4] = LOW_BYTE(sector_to_erase);
  11261. data[5] = HI_BYTE(sector_to_erase);
  11262. /* Increase waiting time for large file erase */
  11263. mutex_lock(&cd->system_lock);
  11264. cd->pip_cmd_timeout = PT_PIP2_CMD_FILE_SECTOR_ERASE_TIMEOUT;
  11265. mutex_unlock(&cd->system_lock);
  11266. for (retry = 0; retry < max_retry; retry++) {
  11267. for (index = 0; index < file_sector; index++) {
  11268. /* Initialize status code */
  11269. tmp_status = PIP2_RSP_ERR_NONE;
  11270. /* Set which sector starts to erase */
  11271. data[2] = LOW_BYTE(index);
  11272. data[3] = HI_BYTE(index);
  11273. ret = _pt_request_pip2_send_cmd(dev,
  11274. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  11275. data, 6, read_buf, &actual_read_len);
  11276. tmp_status = read_buf[PIP2_RESP_STATUS_OFFSET];
  11277. if (ret || tmp_status != PIP2_RSP_ERR_NONE)
  11278. break;
  11279. }
  11280. /*
  11281. * Only retry if response doesn't arrive or the status code is
  11282. * PIP2_RSP_ERR_TIMEOUT.
  11283. */
  11284. if ((ret != -ETIME) || (tmp_status != PIP2_RSP_ERR_TIMEOUT))
  11285. break;
  11286. else {
  11287. #ifdef TTDL_DIAGNOSTICS
  11288. cd->file_erase_timeout_count++;
  11289. #endif
  11290. pt_debug(dev, DL_WARN,
  11291. "%s: ERASE timeout %d for file=%d, sector=%d\n",
  11292. __func__, retry, file_handle, index);
  11293. }
  11294. }
  11295. mutex_lock(&cd->system_lock);
  11296. *status = tmp_status;
  11297. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  11298. mutex_unlock(&cd->system_lock);
  11299. if (ret) {
  11300. pt_debug(dev, DL_ERROR,
  11301. "%s ROM FILE_ERASE cmd failure: %d for file=%d, sector=%d\n",
  11302. __func__, ret, file_handle, index);
  11303. return -EIO;
  11304. }
  11305. if (*status != 0x00) {
  11306. pt_debug(dev, DL_ERROR,
  11307. "%s ROM FILE_ERASE failure: 0x%02X for file=%d, sector=%d\n",
  11308. __func__, *status, file_handle, index);
  11309. return -EIO;
  11310. }
  11311. return file_handle;
  11312. }
  11313. /*******************************************************************************
  11314. * FUNCTION: _pt_pip2_file_erase
  11315. *
  11316. * SUMMARY: Wrapper function to call _pt_pip2_file_erase_by_file_sector() and
  11317. * _pt_pip2_file_erase_by_file_no() by checking file_sector.
  11318. *
  11319. * NOTE: If the file_sector is 0, it will erase the entire file.
  11320. *
  11321. * RETURNS:
  11322. * <0 = Error
  11323. * >0 = file handle closed
  11324. *
  11325. * PARAMETERS:
  11326. * *dev - pointer to device structure
  11327. * file_handle - handle to the file to be erased
  11328. * file_sector - Number of sectors to erase
  11329. * *status - PIP2 erase status code
  11330. ******************************************************************************/
  11331. int _pt_pip2_file_erase(struct device *dev, u8 file_handle, u16 file_sector,
  11332. int *status)
  11333. {
  11334. if (file_sector)
  11335. return _pt_pip2_file_erase_by_file_sector(dev, file_handle,
  11336. file_sector, status);
  11337. else
  11338. return _pt_pip2_file_erase_by_file_no(dev, file_handle, status);
  11339. }
  11340. /*******************************************************************************
  11341. * FUNCTION: _pt_pip2_file_read
  11342. *
  11343. * SUMMARY: Using the BL PIP2 commands read n bytes from a already opened file
  11344. *
  11345. * NOTE: The DUT must be in BL mode for this command to work
  11346. *
  11347. * RETURNS:
  11348. * <0 = Error
  11349. * >0 = number of bytes read
  11350. *
  11351. * PARAMETERS:
  11352. * *dev - pointer to device structure
  11353. * file_handle - File handle to read from
  11354. * num_bytes - number of bytes to read
  11355. ******************************************************************************/
  11356. int _pt_pip2_file_read(struct device *dev, u8 file_handle, u16 num_bytes,
  11357. u8 *read_buf)
  11358. {
  11359. int ret = 0;
  11360. u16 status;
  11361. u16 actual_read_len;
  11362. u8 data[3];
  11363. data[0] = file_handle;
  11364. data[1] = (num_bytes & 0x00FF);
  11365. data[2] = (num_bytes & 0xFF00) >> 8;
  11366. ret = _pt_request_pip2_send_cmd(dev,
  11367. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_READ,
  11368. data, 3, read_buf, &actual_read_len);
  11369. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  11370. if (ret || ((status != 0x00) && (status != 0x03))) {
  11371. pt_debug(dev, DL_ERROR,
  11372. "%s File open failure with error code = %d\n",
  11373. __func__, status);
  11374. return -1;
  11375. }
  11376. ret = num_bytes;
  11377. return ret;
  11378. }
  11379. /*******************************************************************************
  11380. * FUNCTION: _pt_read_us_file
  11381. *
  11382. * SUMMARY: Open a user space file and read 'size' bytes into buf. If size = 0
  11383. * then read the entire file.
  11384. * NOTE: The file size must be less than PT_PIP2_MAX_FILE_SIZE
  11385. *
  11386. * RETURN:
  11387. * 0 = success
  11388. * !0 = failure
  11389. *
  11390. * PARAMETERS:
  11391. * *file_path - pointer to the file path
  11392. * *buf - pointer to the buffer to store the file contents
  11393. * *size - pointer to the size of the file
  11394. ******************************************************************************/
  11395. int _pt_read_us_file(struct device *dev, u8 *file_path, u8 *buf, int *size)
  11396. {
  11397. struct file *filp = NULL;
  11398. struct inode *inode = NULL;
  11399. unsigned int file_len = 0;
  11400. unsigned int read_len = 0;
  11401. mm_segment_t oldfs;
  11402. int rc = 0;
  11403. if (file_path == NULL || buf == NULL) {
  11404. pt_debug(dev, DL_ERROR, "%s: path || buf is NULL.\n", __func__);
  11405. return -EINVAL;
  11406. }
  11407. pt_debug(dev, DL_WARN, "%s: path = %s\n", __func__, file_path);
  11408. oldfs = get_fs();
  11409. set_fs(KERNEL_DS);
  11410. filp = filp_open(file_path, O_RDONLY, 0400);
  11411. if (IS_ERR(filp)) {
  11412. pt_debug(dev, DL_WARN, "%s: Failed to open %s\n", __func__,
  11413. file_path);
  11414. rc = -ENOENT;
  11415. goto err;
  11416. }
  11417. if (filp->f_op == NULL) {
  11418. pt_debug(dev, DL_ERROR, "%s: File Operation Method Error\n",
  11419. __func__);
  11420. rc = -EINVAL;
  11421. goto exit;
  11422. }
  11423. inode = filp->f_path.dentry->d_inode;
  11424. if (inode == NULL) {
  11425. pt_debug(dev, DL_ERROR, "%s: Get inode from filp failed\n",
  11426. __func__);
  11427. rc = -EINVAL;
  11428. goto exit;
  11429. }
  11430. file_len = i_size_read(inode->i_mapping->host);
  11431. if (file_len == 0) {
  11432. pt_debug(dev, DL_ERROR, "%s: file size error,file_len = %d\n",
  11433. __func__, file_len);
  11434. rc = -EINVAL;
  11435. goto exit;
  11436. }
  11437. if (*size == 0)
  11438. read_len = file_len;
  11439. else
  11440. read_len = *size;
  11441. if (read_len > PT_PIP2_MAX_FILE_SIZE) {
  11442. pt_debug(dev, DL_ERROR, "%s: file size ( %d ) exception.\n",
  11443. __func__, read_len);
  11444. rc = -EINVAL;
  11445. goto exit;
  11446. }
  11447. filp->private_data = inode->i_private;
  11448. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0))
  11449. if (kernel_read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  11450. #else
  11451. if (vfs_read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  11452. #endif
  11453. pt_debug(dev, DL_ERROR, "%s: file read error.\n", __func__);
  11454. rc = -EINVAL;
  11455. goto exit;
  11456. }
  11457. *size = read_len;
  11458. exit:
  11459. if (filp_close(filp, NULL) != 0)
  11460. pt_debug(dev, DL_ERROR, "%s: file close error.\n", __func__);
  11461. err:
  11462. set_fs(oldfs);
  11463. return rc;
  11464. }
  11465. /*******************************************************************************
  11466. * FUNCTION: _pt_request_pip2_bin_hdr
  11467. *
  11468. * SUMMARY: Read the stored bin file header from Flash or the User Space file
  11469. * in the case of a flashless DUT, and parse the contents
  11470. *
  11471. * RETURNS:
  11472. * 0 = Success
  11473. * !0 = Error condition
  11474. *
  11475. * PARAMETERS:
  11476. * *dev - pointer to device structure
  11477. ******************************************************************************/
  11478. int _pt_request_pip2_bin_hdr(struct device *dev, struct pt_bin_file_hdr *hdr)
  11479. {
  11480. struct pt_core_data *cd = dev_get_drvdata(dev);
  11481. u8 file_handle;
  11482. u8 read_buf[255];
  11483. u8 hdr_len = 0;
  11484. u8 i;
  11485. int bytes_read;
  11486. int read_size;
  11487. int ret = 0;
  11488. int rc = 0;
  11489. bool load_hdr_struct = false;
  11490. if (cd->flashless_dut) {
  11491. read_size = sizeof(read_buf);
  11492. rc = _pt_read_us_file(dev, cd->pip2_us_file_path,
  11493. read_buf, &read_size);
  11494. if (rc) {
  11495. ret = rc;
  11496. pt_debug(dev, DL_WARN,
  11497. "%s Failed to read fw image from US, rc=%d\n",
  11498. __func__, rc);
  11499. goto exit;
  11500. }
  11501. load_hdr_struct = true;
  11502. hdr_len = read_buf[0];
  11503. i = 0;
  11504. } else {
  11505. if (cd->mode != PT_MODE_BOOTLOADER) {
  11506. ret = -EPERM;
  11507. goto exit;
  11508. }
  11509. /* Open the bin file in Flash */
  11510. pt_debug(dev, DL_INFO, "%s Open File 1\n", __func__);
  11511. file_handle = _pt_pip2_file_open(dev, PIP2_FW_FILE);
  11512. if (file_handle != PIP2_FW_FILE) {
  11513. ret = -ENOENT;
  11514. pt_debug(dev, DL_ERROR,
  11515. "%s Failed to open bin file\n", __func__);
  11516. goto exit;
  11517. }
  11518. /* Read the header length from the file */
  11519. pt_debug(dev, DL_INFO, "%s Read length of header\n", __func__);
  11520. read_size = 1;
  11521. bytes_read = _pt_pip2_file_read(dev, file_handle, read_size,
  11522. read_buf);
  11523. if (bytes_read != read_size) {
  11524. ret = -EX_ERR_FREAD;
  11525. pt_debug(dev, DL_ERROR,
  11526. "%s Failed to bin file header len\n", __func__);
  11527. goto exit_close_file;
  11528. }
  11529. hdr_len = read_buf[PIP2_RESP_BODY_OFFSET];
  11530. if (hdr_len == 0xFF) {
  11531. ret = -EX_ERR_FLEN;
  11532. pt_debug(dev, DL_ERROR,
  11533. "%s Bin header len is invalid\n", __func__);
  11534. goto exit_close_file;
  11535. }
  11536. /* Read the rest of the header from the bin file */
  11537. pt_debug(dev, DL_INFO, "%s Read bin file header\n", __func__);
  11538. memset(read_buf, 0, sizeof(read_buf));
  11539. bytes_read = _pt_pip2_file_read(dev, file_handle, hdr_len,
  11540. read_buf);
  11541. if (bytes_read != hdr_len) {
  11542. ret = -EX_ERR_FREAD;
  11543. pt_debug(dev, DL_ERROR,
  11544. "%s Failed to read bin file\n", __func__);
  11545. goto exit_close_file;
  11546. }
  11547. load_hdr_struct = true;
  11548. exit_close_file:
  11549. /* Close the file */
  11550. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  11551. ret = -EX_ERR_FCLOSE;
  11552. pt_debug(dev, DL_ERROR,
  11553. "%s Failed to close bin file\n", __func__);
  11554. }
  11555. /*
  11556. * The length was already read so subtract 1 to make the rest of
  11557. * the offsets match the spec
  11558. */
  11559. i = PIP2_RESP_BODY_OFFSET - 1;
  11560. }
  11561. if (load_hdr_struct) {
  11562. hdr->length = hdr_len;
  11563. hdr->ttpid = (read_buf[i+1] << 8) | read_buf[i+2];
  11564. hdr->fw_major = (read_buf[i+3]);
  11565. hdr->fw_minor = (read_buf[i+4]);
  11566. hdr->fw_crc = (read_buf[i+5] << 24) |
  11567. (read_buf[i+6] << 16) |
  11568. (read_buf[i+7] << 8) |
  11569. (read_buf[i+8]);
  11570. hdr->fw_rev_ctrl = (read_buf[i+9] << 24) |
  11571. (read_buf[i+10] << 16) |
  11572. (read_buf[i+11] << 8) |
  11573. (read_buf[i+12]);
  11574. hdr->si_rev = (read_buf[i+14] << 8) | (read_buf[i+13]);
  11575. hdr->si_id = (read_buf[i+16] << 8) | (read_buf[i+15]);
  11576. hdr->config_ver = (read_buf[i+17] << 8) | (read_buf[i+18]);
  11577. if (hdr_len >= 22) {
  11578. hdr->hex_file_size = (read_buf[i+19] << 24) |
  11579. (read_buf[i+20] << 16) |
  11580. (read_buf[i+21] << 8) |
  11581. (read_buf[i+22]);
  11582. } else {
  11583. hdr->hex_file_size = 0;
  11584. }
  11585. }
  11586. exit:
  11587. return ret;
  11588. }
  11589. /*******************************************************************************
  11590. * FUNCTION: _pt_pip2_file_get_stats
  11591. *
  11592. * SUMMARY: Using the BL PIP2 commands get file information from an already
  11593. * opened file
  11594. *
  11595. * NOTE: The DUT must be in BL mode for this command to work
  11596. *
  11597. * RETURNS:
  11598. * !0 = Error
  11599. * 0 = Success
  11600. *
  11601. * PARAMETERS:
  11602. * *dev - pointer to device structure
  11603. * file_handle - File handle to read from
  11604. * *address - pointer to store address of file
  11605. * *file_size _ pointer to store size of file
  11606. ******************************************************************************/
  11607. int _pt_pip2_file_get_stats(struct device *dev, u8 file_handle, u32 *address,
  11608. u32 *file_size)
  11609. {
  11610. int ret = 1;
  11611. u16 status;
  11612. u16 actual_read_len;
  11613. u8 data[2];
  11614. u8 read_buf[16];
  11615. data[0] = file_handle;
  11616. data[1] = PIP2_FILE_IOCTL_CODE_FILE_STATS;
  11617. ret = _pt_request_pip2_send_cmd(dev,
  11618. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  11619. data, 2, read_buf, &actual_read_len);
  11620. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  11621. if (ret || (status != 0x00)) {
  11622. pt_debug(dev, DL_ERROR,
  11623. "%s ROM FILE_STATS failure: 0x%02X for file = %d, ret = %d\n",
  11624. __func__, status, file_handle, ret);
  11625. ret = -EIO;
  11626. goto exit;
  11627. }
  11628. pt_debug(dev, DL_DEBUG,
  11629. "%s --- FILE %d Information ---\n", __func__, file_handle);
  11630. if (address) {
  11631. *address = read_buf[PIP2_RESP_BODY_OFFSET] +
  11632. (read_buf[PIP2_RESP_BODY_OFFSET + 1] << 8) +
  11633. (read_buf[PIP2_RESP_BODY_OFFSET + 2] << 16) +
  11634. (read_buf[PIP2_RESP_BODY_OFFSET + 3] << 24);
  11635. pt_debug(dev, DL_DEBUG, "%s Address: 0x%08x\n",
  11636. __func__, *address);
  11637. }
  11638. if (file_size) {
  11639. *file_size = read_buf[PIP2_RESP_BODY_OFFSET + 4] +
  11640. (read_buf[PIP2_RESP_BODY_OFFSET + 5] << 8) +
  11641. (read_buf[PIP2_RESP_BODY_OFFSET + 6] << 16) +
  11642. (read_buf[PIP2_RESP_BODY_OFFSET + 7] << 24);
  11643. pt_debug(dev, DL_DEBUG, "%s Size : 0x%08x\n",
  11644. __func__, *file_size);
  11645. }
  11646. exit:
  11647. return ret;
  11648. }
  11649. /*******************************************************************************
  11650. * FUNCTION: _pt_pip2_file_seek_offset
  11651. *
  11652. * SUMMARY: Using the BL PIP2 commands seek read/write offset for an already
  11653. * opened file
  11654. *
  11655. * NOTE: The DUT must be in BL mode for this command to work
  11656. * NOTE: File open/erase command can reset the offset
  11657. *
  11658. * RETURNS:
  11659. * !0 = Error
  11660. * 0 = Success
  11661. *
  11662. * PARAMETERS:
  11663. * *dev - pointer to device structure
  11664. * file_handle - File handle to read from
  11665. * read_offset - read offset of file
  11666. * write_offset - write offset of file
  11667. ******************************************************************************/
  11668. int _pt_pip2_file_seek_offset(struct device *dev, u8 file_handle,
  11669. u32 read_offset, u32 write_offset)
  11670. {
  11671. int ret = 1;
  11672. u16 status;
  11673. u16 actual_read_len;
  11674. u8 data[10];
  11675. u8 read_buf[16];
  11676. data[0] = file_handle;
  11677. data[1] = PIP2_FILE_IOCTL_CODE_SEEK_POINTER;
  11678. data[2] = 0xff & read_offset;
  11679. data[3] = 0xff & (read_offset >> 8);
  11680. data[4] = 0xff & (read_offset >> 16);
  11681. data[5] = 0xff & (read_offset >> 24);
  11682. data[6] = 0xff & write_offset;
  11683. data[7] = 0xff & (write_offset >> 8);
  11684. data[8] = 0xff & (write_offset >> 16);
  11685. data[9] = 0xff & (write_offset >> 24);
  11686. ret = _pt_request_pip2_send_cmd(dev,
  11687. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  11688. data, 10, read_buf, &actual_read_len);
  11689. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  11690. if (ret || (status != 0x00)) {
  11691. pt_debug(dev, DL_ERROR,
  11692. "%s ROM FILE_SEEK failure: 0x%02X for file = %d, ret = %d\n",
  11693. __func__, status, ret, file_handle);
  11694. ret = -EIO;
  11695. }
  11696. return ret;
  11697. }
  11698. /*******************************************************************************
  11699. * FUNCTION: _pt_pip2_file_crc
  11700. *
  11701. * SUMMARY: Using the BL PIP2 commands to calculate CRC for a file or portion of
  11702. * the file.
  11703. *
  11704. * NOTE: The DUT must be in BL mode for this command to work
  11705. * NOTE: This command only can be used for BL version 1.8 or greater.
  11706. * BL version 1.8 added this change according to PGV-173.
  11707. *
  11708. * RETURNS:
  11709. * !0 = Error
  11710. * 0 = Success
  11711. *
  11712. * PARAMETERS:
  11713. * *dev - pointer to device structure
  11714. * file_handle - File handle to read from
  11715. * offset - start offset for CRC calculation
  11716. * length - number of bytes to calculate CRC over
  11717. * read_buf - pointer to the read buffer
  11718. ******************************************************************************/
  11719. int _pt_pip2_file_crc(struct device *dev, u8 file_handle,
  11720. u32 offset, u32 length, u8 *read_buf)
  11721. {
  11722. int rc = 1;
  11723. u16 actual_read_len;
  11724. u8 data[10];
  11725. data[0] = file_handle;
  11726. data[1] = PIP2_FILE_IOCTL_CODE_FILE_CRC;
  11727. data[2] = 0xff & offset;
  11728. data[3] = 0xff & (offset >> 8);
  11729. data[4] = 0xff & (offset >> 16);
  11730. data[5] = 0xff & (offset >> 24);
  11731. data[6] = 0xff & length;
  11732. data[7] = 0xff & (length >> 8);
  11733. data[8] = 0xff & (length >> 16);
  11734. data[9] = 0xff & (length >> 24);
  11735. rc = _pt_request_pip2_send_cmd(dev,
  11736. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  11737. data, 10, read_buf, &actual_read_len);
  11738. if (rc)
  11739. pt_debug(dev, DL_ERROR,
  11740. "%s Return FILE_CRC failure, rc = %d\n",
  11741. __func__, rc);
  11742. return rc;
  11743. }
  11744. /*******************************************************************************
  11745. * FUNCTION: pt_pip2_ping_test
  11746. *
  11747. * SUMMARY: BIST type test that uses the PIP2 PING command and ramps up the
  11748. * optional payload from 0 bytes to max_bytes and ensures the PIP2
  11749. * response payload matches what was sent.
  11750. * The max payload size is 247:
  11751. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  11752. *
  11753. * RETURN:
  11754. * 0 = success
  11755. * !0 = failure
  11756. *
  11757. * PARAMETERS:
  11758. * *dev - pointer to device structure
  11759. * *attr - pointer to device attributes
  11760. * *buf - pointer to output buffer
  11761. ******************************************************************************/
  11762. int pt_pip2_ping_test(struct device *dev, int max_bytes, int *last_packet_size)
  11763. {
  11764. u16 actual_read_len;
  11765. u8 *read_buf = NULL;
  11766. u8 *data = NULL;
  11767. int data_offset = PIP2_RESP_STATUS_OFFSET;
  11768. int i = 1;
  11769. int j = 0;
  11770. int rc = 0;
  11771. read_buf = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  11772. if (!read_buf)
  11773. goto ping_test_exit;
  11774. data = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  11775. if (!data)
  11776. goto ping_test_exit;
  11777. /* Load data payload with an array of walking 1's */
  11778. for (i = 0; i < 255; i++)
  11779. data[i] = 0x01 << (i % 8);
  11780. /* Send 'max_bytes' PING cmds using 'i' bytes as payload for each */
  11781. for (i = 0; i <= max_bytes; i++) {
  11782. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  11783. PIP2_CMD_ID_PING, data, i, read_buf,
  11784. &actual_read_len);
  11785. if (rc) {
  11786. pt_debug(dev, DL_ERROR,
  11787. "%s: PING failed with %d byte payload\n",
  11788. __func__, i);
  11789. break;
  11790. }
  11791. /* Verify data returned matches data sent */
  11792. for (j = 0; j < i; j++) {
  11793. if (read_buf[data_offset + j] != data[j]) {
  11794. pt_debug(dev, DL_DEBUG,
  11795. "%s: PING packet size %d: sent[%d]=0x%02X recv[%d]=0x%02X\n",
  11796. __func__, i, j, data[j], j,
  11797. read_buf[data_offset + j]);
  11798. goto ping_test_exit;
  11799. }
  11800. }
  11801. }
  11802. ping_test_exit:
  11803. *last_packet_size = i - 1;
  11804. kfree(read_buf);
  11805. kfree(data);
  11806. return rc;
  11807. }
  11808. #ifndef TTDL_KERNEL_SUBMISSION
  11809. /*******************************************************************************
  11810. * FUNCTION: _pt_ic_parse_input_hex
  11811. *
  11812. * SUMMARY: Parse a char data array as space delimited hex values into
  11813. * an int array.
  11814. *
  11815. * NOTE: _pt_ic_parse_input() function may have similar work while the type of
  11816. * buffer to store data is "u32". This function is still needed by the
  11817. * "command" sysfs node because the buffer type to store data is "u8".
  11818. *
  11819. * RETURN: Length of parsed data
  11820. *
  11821. * PARAMETERS:
  11822. * *dev - pointer to device structure
  11823. * *buf - pointer to buffer that holds the input array to parse
  11824. * buf_size - size of buf
  11825. * *ic_buf - pointer to array to store parsed data
  11826. * ic_buf_size - max size of ic_buf
  11827. ******************************************************************************/
  11828. static int _pt_ic_parse_input_hex(struct device *dev, const char *buf,
  11829. size_t buf_size, u8 *ic_buf, size_t ic_buf_size)
  11830. {
  11831. const char *pbuf = buf;
  11832. unsigned long value;
  11833. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  11834. u32 i = 0;
  11835. u32 j;
  11836. int last = 0;
  11837. int ret;
  11838. pt_debug(dev, DL_DEBUG,
  11839. "%s: pbuf=%p buf=%p size=%zu %s=%d buf=%s\n",
  11840. __func__, pbuf, buf, buf_size, "scan buf size",
  11841. PT_MAX_PIP2_MSG_SIZE, buf);
  11842. while (pbuf <= (buf + buf_size)) {
  11843. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  11844. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  11845. __func__, "Max cmd size exceeded", i,
  11846. PT_MAX_PIP2_MSG_SIZE);
  11847. return -EINVAL;
  11848. }
  11849. if (i >= ic_buf_size) {
  11850. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  11851. __func__, "Buffer size exceeded", i,
  11852. ic_buf_size);
  11853. return -EINVAL;
  11854. }
  11855. while (((*pbuf == ' ') || (*pbuf == ','))
  11856. && (pbuf < (buf + buf_size))) {
  11857. last = *pbuf;
  11858. pbuf++;
  11859. }
  11860. if (pbuf >= (buf + buf_size))
  11861. break;
  11862. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  11863. if ((last == ',') && (*pbuf == ',')) {
  11864. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  11865. __func__, "Invalid data format.");
  11866. return -EINVAL;
  11867. }
  11868. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  11869. && (pbuf < (buf + buf_size))
  11870. && (*pbuf != ' ')
  11871. && (*pbuf != ','); j++) {
  11872. last = *pbuf;
  11873. scan_buf[j] = *pbuf++;
  11874. }
  11875. ret = kstrtoul(scan_buf, 16, &value);
  11876. if (ret < 0) {
  11877. pt_debug(dev, DL_ERROR,
  11878. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  11879. "Invalid data format. ", scan_buf,
  11880. "Use \"0xHH,...,0xHH\"", " instead.",
  11881. i, ret);
  11882. return ret;
  11883. }
  11884. ic_buf[i] = value;
  11885. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx",
  11886. __func__, i, value);
  11887. i++;
  11888. }
  11889. return i;
  11890. }
  11891. /*******************************************************************************
  11892. * FUNCTION: _pt_ic_parse_input
  11893. *
  11894. * SUMMARY: Parse user sysfs input data as a space or comma delimited list of
  11895. * hex values or dec values into an int array with the following rules:
  11896. * 1) Hex values must have a "0x" prefix for each element or the first element
  11897. * only
  11898. * 2) Dec values do not have any prefix
  11899. * 3) It is not allowed to have a mix of dec and hex values in the user input
  11900. * string
  11901. *
  11902. * RETURN: Number of values parsed
  11903. *
  11904. * PARAMETERS:
  11905. * *dev - pointer to device structure
  11906. * *buf - pointer to buffer that holds the input array to parse
  11907. * buf_size - size of buf
  11908. * *out_buf - pointer to array to store parsed data
  11909. * out_buf_size - max size of buffer to be stored
  11910. ******************************************************************************/
  11911. static int _pt_ic_parse_input(struct device *dev,
  11912. const char *buf, size_t buf_size,
  11913. u32 *out_buf, size_t out_buf_size)
  11914. {
  11915. const char *pbuf = buf;
  11916. unsigned long value;
  11917. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  11918. u32 i = 0;
  11919. u32 j;
  11920. int last = 0;
  11921. int ret = 0;
  11922. u8 str_base = 0;
  11923. pt_debug(dev, DL_DEBUG,
  11924. "%s: in_buf_size=%zu out_buf_size=%zu %s=%d buf=%s\n",
  11925. __func__, buf_size, out_buf_size, "scan buf size",
  11926. PT_MAX_PIP2_MSG_SIZE, buf);
  11927. while (pbuf <= (buf + buf_size)) {
  11928. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  11929. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  11930. __func__, "Max cmd size exceeded", i,
  11931. PT_MAX_PIP2_MSG_SIZE);
  11932. ret = -EINVAL;
  11933. goto error;
  11934. }
  11935. if (i >= out_buf_size) {
  11936. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  11937. __func__, "Buffer size exceeded", i,
  11938. out_buf_size);
  11939. ret = -EINVAL;
  11940. goto error;
  11941. }
  11942. while (((*pbuf == ' ') || (*pbuf == ','))
  11943. && (pbuf < (buf + buf_size))) {
  11944. last = *pbuf;
  11945. pbuf++;
  11946. }
  11947. if (pbuf >= (buf + buf_size))
  11948. break;
  11949. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  11950. if ((last == ',') && (*pbuf == ',')) {
  11951. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  11952. __func__, "Invalid data format.");
  11953. ret = -EINVAL;
  11954. goto error;
  11955. }
  11956. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  11957. && (pbuf < (buf + buf_size))
  11958. && (*pbuf != ' ')
  11959. && (*pbuf != ','); j++) {
  11960. last = *pbuf;
  11961. scan_buf[j] = *pbuf++;
  11962. }
  11963. if (i == 0) {
  11964. if ((strncmp(scan_buf, "0x", 2) == 0) ||
  11965. (strncmp(scan_buf, "0X", 2) == 0))
  11966. str_base = 16;
  11967. else
  11968. str_base = 10;
  11969. } else {
  11970. if (((strncmp(scan_buf, "0x", 2) == 0) ||
  11971. (strncmp(scan_buf, "0X", 2) == 0)) &&
  11972. (str_base == 10)) {
  11973. pt_debug(dev, DL_ERROR,
  11974. "%s: Decimal and Heximal data mixed\n",
  11975. __func__);
  11976. ret = -EINVAL;
  11977. goto error;
  11978. }
  11979. }
  11980. ret = kstrtoul(scan_buf, str_base, &value);
  11981. if (ret < 0) {
  11982. pt_debug(dev, DL_ERROR,
  11983. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  11984. "Invalid data format. ", scan_buf,
  11985. "Use \"0xHH,...,0xHH\" or \"DD DD DD ... DD\"",
  11986. " instead.", i, ret);
  11987. goto error;
  11988. }
  11989. out_buf[i] = value;
  11990. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx(%lu)",
  11991. __func__, i, value, value);
  11992. i++;
  11993. }
  11994. ret = i;
  11995. error:
  11996. return ret;
  11997. }
  11998. #endif /* !TTDL_KERNEL_SUBMISSION */
  11999. #ifdef TTHE_TUNER_SUPPORT
  12000. /*******************************************************************************
  12001. * FUNCTION: tthe_debugfs_open
  12002. *
  12003. * SUMMARY: Open method for tthe_tuner debugfs node. On some hosts the size of
  12004. * PT_MAX_PRBUF_SIZE (equal to PAGE_SIZE) is not large enough to handle
  12005. * printing a large number of fingers and sensor data without overflowing
  12006. * the buffer. tthe_tuner needs ~4K and so the buffer is sized to some
  12007. * even multiple of PAGE_SIZE
  12008. *
  12009. * RETURN:
  12010. * 0 = success
  12011. * !0 = failure
  12012. *
  12013. * PARAMETERS:
  12014. * *inode - file inode number
  12015. * *filp - file pointer to debugfs file
  12016. ******************************************************************************/
  12017. static int tthe_debugfs_open(struct inode *inode, struct file *filp)
  12018. {
  12019. struct pt_core_data *cd = inode->i_private;
  12020. u32 buf_size = PT_MAX_PRBUF_SIZE;
  12021. filp->private_data = inode->i_private;
  12022. if (cd->tthe_buf)
  12023. return -EBUSY;
  12024. while (buf_size < 4096)
  12025. buf_size = buf_size << 1;
  12026. pt_debug(cd->dev, DL_INFO, "%s:PT_MAX_BRBUF_SIZE=%d buf_size=%d\n",
  12027. __func__, (int)PT_MAX_PRBUF_SIZE, (int)buf_size);
  12028. cd->tthe_buf_size = buf_size;
  12029. cd->tthe_buf = kzalloc(cd->tthe_buf_size, GFP_KERNEL);
  12030. if (!cd->tthe_buf)
  12031. return -ENOMEM;
  12032. return 0;
  12033. }
  12034. /*******************************************************************************
  12035. * FUNCTION: tthe_debugfs_close
  12036. *
  12037. * SUMMARY: Close method for tthe_tuner debugfs node.
  12038. *
  12039. * RETURN:
  12040. * 0 = success
  12041. * !0 = failure
  12042. *
  12043. * PARAMETERS:
  12044. * *inode - file inode number
  12045. * *filp - file pointer to debugfs file
  12046. ******************************************************************************/
  12047. static int tthe_debugfs_close(struct inode *inode, struct file *filp)
  12048. {
  12049. struct pt_core_data *cd = filp->private_data;
  12050. filp->private_data = NULL;
  12051. kfree(cd->tthe_buf);
  12052. cd->tthe_buf = NULL;
  12053. return 0;
  12054. }
  12055. /*******************************************************************************
  12056. * FUNCTION: tthe_debugfs_store
  12057. *
  12058. * SUMMARY: Write method for tthe_tuner debugfs node. This function allows
  12059. * user configuration of some output values via a bitmask.
  12060. * 0x01 = HID USB vs I2C output
  12061. * 0xFE = Reserved
  12062. *
  12063. * RETURN: Size of debugfs data write
  12064. *
  12065. * PARAMETERS:
  12066. * *filp - file pointer to debugfs file
  12067. * *buf - the user space buffer to read to
  12068. * count - the maximum number of bytes to read
  12069. * *ppos - the current position in the buffer
  12070. ******************************************************************************/
  12071. static ssize_t tthe_debugfs_store(struct file *filp, const char __user *buf,
  12072. size_t count, loff_t *ppos)
  12073. {
  12074. struct pt_core_data *cd = filp->private_data;
  12075. ssize_t length;
  12076. u32 input_data[2];
  12077. u8 tmp_buf[4]; /* large enough for 1 32bit value */
  12078. int rc = 0;
  12079. mutex_lock(&cd->tthe_lock);
  12080. /* copy data from user space */
  12081. rc = simple_write_to_buffer(tmp_buf, sizeof(tmp_buf),
  12082. ppos, buf, count);
  12083. if (rc < 0)
  12084. goto exit;
  12085. length = _pt_ic_parse_input(cd->dev, tmp_buf, count,
  12086. input_data, ARRAY_SIZE(input_data));
  12087. if (length == 1) {
  12088. mutex_lock(&(cd->system_lock));
  12089. cd->tthe_hid_usb_format = input_data[0];
  12090. if (input_data[0] == PT_TTHE_TUNER_FORMAT_HID_USB)
  12091. pt_debug(cd->dev, DL_INFO,
  12092. "%s: Enable tthe_tuner HID-USB format\n",
  12093. __func__);
  12094. else if (input_data[0] == PT_TTHE_TUNER_FORMAT_HID_I2C)
  12095. pt_debug(cd->dev, DL_INFO,
  12096. "%s: Enable tthe_tuner HID-I2C format\n",
  12097. __func__);
  12098. else if (input_data[0] ==
  12099. PT_TTHE_TUNER_FORMAT_HID_FINGER_TO_PIP)
  12100. pt_debug(cd->dev, DL_INFO,
  12101. "%s: Enable tthe_tuner HID-FINGER-TO-PIP format\n",
  12102. __func__);
  12103. else if (input_data[0] ==
  12104. PT_TTHE_TUNER_FORMAT_HID_FINGER_AND_PEN_TO_PIP)
  12105. pt_debug(cd->dev, DL_INFO,
  12106. "%s: Enable tthe_tuner HID_FINGER_AND_PEN_TO_PIP format\n",
  12107. __func__);
  12108. else {
  12109. rc = -EINVAL;
  12110. pt_debug(cd->dev, DL_ERROR,
  12111. "%s: Invalid parameter: %d\n",
  12112. __func__, input_data[0]);
  12113. }
  12114. mutex_unlock(&(cd->system_lock));
  12115. } else {
  12116. rc = -EINVAL;
  12117. pt_debug(cd->dev, DL_ERROR,
  12118. "%s: Invalid number of parameters\n", __func__);
  12119. }
  12120. exit:
  12121. mutex_unlock(&cd->tthe_lock);
  12122. if (rc)
  12123. return rc;
  12124. else
  12125. return count;
  12126. }
  12127. /*******************************************************************************
  12128. * FUNCTION: tthe_debugfs_read
  12129. *
  12130. * SUMMARY: Read method for tthe_tuner debugfs node. This function prints
  12131. * tthe_buf to user buffer.
  12132. *
  12133. * RETURN: Size of debugfs data print
  12134. *
  12135. * PARAMETERS:
  12136. * *filp - file pointer to debugfs file
  12137. * *buf - the user space buffer to read to
  12138. * count - the maximum number of bytes to read
  12139. * *ppos - the current position in the buffer
  12140. ******************************************************************************/
  12141. static ssize_t tthe_debugfs_read(struct file *filp, char __user *buf,
  12142. size_t count, loff_t *ppos)
  12143. {
  12144. struct pt_core_data *cd = filp->private_data;
  12145. int size;
  12146. int ret;
  12147. static int partial_read;
  12148. wait_event_interruptible(cd->wait_q,
  12149. cd->tthe_buf_len != 0 || cd->tthe_exit);
  12150. mutex_lock(&cd->tthe_lock);
  12151. if (cd->tthe_exit) {
  12152. mutex_unlock(&cd->tthe_lock);
  12153. return 0;
  12154. }
  12155. if (count > cd->tthe_buf_len)
  12156. size = cd->tthe_buf_len;
  12157. else
  12158. size = count;
  12159. if (!size) {
  12160. mutex_unlock(&cd->tthe_lock);
  12161. return 0;
  12162. }
  12163. if (partial_read) {
  12164. ret = copy_to_user(buf, cd->tthe_buf + partial_read, size);
  12165. partial_read = 0;
  12166. } else {
  12167. ret = copy_to_user(buf, cd->tthe_buf, size);
  12168. }
  12169. /*
  12170. * When size >= tthe_buf_len setting partial_read will cause NULL
  12171. * characters to be printed in the output.
  12172. */
  12173. if (size == count && size < cd->tthe_buf_len)
  12174. partial_read = count;
  12175. if (ret == size) {
  12176. mutex_unlock(&cd->tthe_lock);
  12177. return -EFAULT;
  12178. }
  12179. size -= ret;
  12180. cd->tthe_buf_len -= size;
  12181. mutex_unlock(&cd->tthe_lock);
  12182. *ppos += size;
  12183. return size;
  12184. }
  12185. static const struct file_operations tthe_debugfs_fops = {
  12186. .open = tthe_debugfs_open,
  12187. .release = tthe_debugfs_close,
  12188. .read = tthe_debugfs_read,
  12189. .write = tthe_debugfs_store,
  12190. };
  12191. #endif
  12192. static struct pt_core_nonhid_cmd _pt_core_nonhid_cmd = {
  12193. .start_bl = _pt_request_pip_start_bl,
  12194. .suspend_scanning = _pt_request_pip_suspend_scanning,
  12195. .resume_scanning = _pt_request_pip_resume_scanning,
  12196. .get_param = _pt_request_pip_get_param,
  12197. .set_param = _pt_request_pip_set_param,
  12198. .verify_cfg_block_crc = _pt_request_pip_verify_config_block_crc,
  12199. .get_config_row_size = _pt_request_pip_get_config_row_size,
  12200. .get_data_structure = _pt_request_pip_get_data_structure,
  12201. .run_selftest = _pt_request_pip_run_selftest,
  12202. .get_selftest_result = _pt_request_pip_get_selftest_result,
  12203. .load_self_test_param = _pt_request_pip_load_self_test_param,
  12204. .calibrate_idacs = _pt_request_pip_calibrate_idacs,
  12205. .calibrate_ext = _pt_request_pip_calibrate_ext,
  12206. .initialize_baselines = _pt_request_pip_initialize_baselines,
  12207. .exec_panel_scan = _pt_request_pip_exec_panel_scan,
  12208. .retrieve_panel_scan = _pt_request_pip_retrieve_panel_scan,
  12209. .read_data_block = _pt_request_pip_read_data_block,
  12210. .write_data_block = _pt_request_pip_write_data_block,
  12211. .user_cmd = _pt_request_pip_user_cmd,
  12212. .get_bl_info = _pt_request_pip_bl_get_information,
  12213. .initiate_bl = _pt_request_pip_bl_initiate_bl,
  12214. .launch_app = _pt_request_pip_launch_app,
  12215. .prog_and_verify = _pt_request_pip_bl_program_and_verify,
  12216. .verify_app_integrity = _pt_request_pip_bl_verify_app_integrity,
  12217. .get_panel_id = _pt_request_pip_bl_get_panel_id,
  12218. .pip2_send_cmd = _pt_request_pip2_send_cmd,
  12219. .pip2_send_cmd_no_int = _pt_pip2_send_cmd_no_int,
  12220. .pip2_file_open = _pt_pip2_file_open,
  12221. .pip2_file_close = _pt_pip2_file_close,
  12222. .pip2_file_erase = _pt_pip2_file_erase,
  12223. .read_us_file = _pt_read_us_file,
  12224. .manage_cal_data = _pt_manage_local_cal_data,
  12225. .calc_crc = crc_ccitt_calculate,
  12226. #ifdef TTDL_DIAGNOSTICS
  12227. .pip2_file_read = _pt_pip2_file_read,
  12228. .pip2_file_seek_offset = _pt_pip2_file_seek_offset,
  12229. .pip2_file_get_stats = _pt_pip2_file_get_stats,
  12230. .pip2_file_crc = _pt_pip2_file_crc,
  12231. #endif
  12232. };
  12233. static struct pt_core_commands _pt_core_commands = {
  12234. .subscribe_attention = _pt_subscribe_attention,
  12235. .unsubscribe_attention = _pt_unsubscribe_attention,
  12236. .request_exclusive = _pt_request_exclusive,
  12237. .release_exclusive = _pt_release_exclusive,
  12238. .request_reset = _pt_request_reset,
  12239. .request_pip2_launch_app = _pt_request_pip2_launch_app,
  12240. .request_enum = _pt_request_enum,
  12241. .request_sysinfo = _pt_request_sysinfo,
  12242. .request_loader_pdata = _pt_request_loader_pdata,
  12243. .request_stop_wd = _pt_request_stop_wd,
  12244. .request_start_wd = _pt_request_start_wd,
  12245. .request_get_mode = _pt_request_get_mode,
  12246. .request_active_pip_prot = _pt_request_active_pip_protocol,
  12247. .request_pip2_get_mode_sysmode = _pt_request_pip2_get_mode_sysmode,
  12248. .request_pip2_enter_bl = _pt_request_pip2_enter_bl,
  12249. .request_pip2_bin_hdr = _pt_request_pip2_bin_hdr,
  12250. .request_dut_generation = _pt_request_dut_generation,
  12251. .request_hw_version = _pt_request_hw_version,
  12252. #ifndef TTDL_KERNEL_SUBMISSION
  12253. .parse_sysfs_input = _pt_ic_parse_input,
  12254. #endif
  12255. #ifdef TTHE_TUNER_SUPPORT
  12256. .request_tthe_print = _pt_request_tthe_print,
  12257. #endif
  12258. #ifdef TTDL_DIAGNOSTICS
  12259. .request_toggle_err_gpio = _pt_request_toggle_err_gpio,
  12260. #endif
  12261. .nonhid_cmd = &_pt_core_nonhid_cmd,
  12262. .request_get_fw_mode = _pt_request_get_fw_sys_mode,
  12263. };
  12264. struct pt_core_commands *pt_get_commands(void)
  12265. {
  12266. return &_pt_core_commands;
  12267. }
  12268. EXPORT_SYMBOL_GPL(pt_get_commands);
  12269. static DEFINE_MUTEX(core_list_lock);
  12270. static LIST_HEAD(core_list);
  12271. static DEFINE_MUTEX(module_list_lock);
  12272. static LIST_HEAD(module_list);
  12273. static int core_number;
  12274. /*******************************************************************************
  12275. * FUNCTION: pt_probe_module
  12276. *
  12277. * SUMMARY: Add the module pointer to module_node and call the probe pointer.
  12278. *
  12279. * RETURN:
  12280. * 0 = success
  12281. * !0 = failure
  12282. *
  12283. * PARAMETERS:
  12284. * *cd - pointer to core data
  12285. * *module - pointer to module structure
  12286. ******************************************************************************/
  12287. static int pt_probe_module(struct pt_core_data *cd,
  12288. struct pt_module *module)
  12289. {
  12290. struct module_node *module_node;
  12291. int rc = 0;
  12292. module_node = kzalloc(sizeof(*module_node), GFP_KERNEL);
  12293. if (!module_node)
  12294. return -ENOMEM;
  12295. module_node->module = module;
  12296. mutex_lock(&cd->module_list_lock);
  12297. list_add(&module_node->node, &cd->module_list);
  12298. mutex_unlock(&cd->module_list_lock);
  12299. rc = module->probe(cd->dev, &module_node->data);
  12300. if (rc) {
  12301. /*
  12302. * Remove from the list when probe fails
  12303. * in order not to call release
  12304. */
  12305. mutex_lock(&cd->module_list_lock);
  12306. list_del(&module_node->node);
  12307. mutex_unlock(&cd->module_list_lock);
  12308. kfree(module_node);
  12309. goto exit;
  12310. }
  12311. exit:
  12312. return rc;
  12313. }
  12314. /*******************************************************************************
  12315. * FUNCTION: pt_release_module
  12316. *
  12317. * SUMMARY: Call the release pointer and remove the module pointer from
  12318. * module_list.
  12319. *
  12320. * PARAMETERS:
  12321. * *cd - pointer to core data
  12322. * *module - pointer to module structure
  12323. ******************************************************************************/
  12324. static void pt_release_module(struct pt_core_data *cd,
  12325. struct pt_module *module)
  12326. {
  12327. struct module_node *m, *m_n;
  12328. mutex_lock(&cd->module_list_lock);
  12329. list_for_each_entry_safe(m, m_n, &cd->module_list, node)
  12330. if (m->module == module) {
  12331. module->release(cd->dev, m->data);
  12332. list_del(&m->node);
  12333. kfree(m);
  12334. break;
  12335. }
  12336. mutex_unlock(&cd->module_list_lock);
  12337. }
  12338. /*******************************************************************************
  12339. * FUNCTION: pt_probe_modules
  12340. *
  12341. * SUMMARY: Iterate module_list and probe each module.
  12342. *
  12343. * PARAMETERS:
  12344. * *cd - pointer to core data
  12345. ******************************************************************************/
  12346. static void pt_probe_modules(struct pt_core_data *cd)
  12347. {
  12348. struct pt_module *m;
  12349. int rc = 0;
  12350. mutex_lock(&module_list_lock);
  12351. list_for_each_entry(m, &module_list, node) {
  12352. pt_debug(cd->dev, DL_WARN, "%s: Probe module %s\n",
  12353. __func__, m->name);
  12354. rc = pt_probe_module(cd, m);
  12355. if (rc)
  12356. pt_debug(cd->dev, DL_ERROR,
  12357. "%s: Probe fails for module %s\n",
  12358. __func__, m->name);
  12359. }
  12360. mutex_unlock(&module_list_lock);
  12361. }
  12362. /*******************************************************************************
  12363. * FUNCTION: pt_release_modules
  12364. *
  12365. * SUMMARY: Iterate module_list and remove each module.
  12366. *
  12367. * PARAMETERS:
  12368. * *cd - pointer to core data
  12369. ******************************************************************************/
  12370. static void pt_release_modules(struct pt_core_data *cd)
  12371. {
  12372. struct pt_module *m;
  12373. mutex_lock(&module_list_lock);
  12374. list_for_each_entry(m, &module_list, node)
  12375. pt_release_module(cd, m);
  12376. mutex_unlock(&module_list_lock);
  12377. }
  12378. /*******************************************************************************
  12379. * FUNCTION: pt_get_core_data
  12380. *
  12381. * SUMMARY: Iterate core_list and get core data.
  12382. *
  12383. * RETURN:
  12384. * pointer to core data or null pointer if fail
  12385. *
  12386. * PARAMETERS:
  12387. * *id - pointer to core id
  12388. ******************************************************************************/
  12389. struct pt_core_data *pt_get_core_data(char *id)
  12390. {
  12391. struct pt_core_data *d;
  12392. list_for_each_entry(d, &core_list, node)
  12393. if (!strncmp(d->core_id, id, 20))
  12394. return d;
  12395. return NULL;
  12396. }
  12397. EXPORT_SYMBOL_GPL(pt_get_core_data);
  12398. /*******************************************************************************
  12399. * FUNCTION: pt_add_core
  12400. *
  12401. * SUMMARY: Add core data to the core_list.
  12402. *
  12403. * PARAMETERS:
  12404. * *dev - pointer to device structure
  12405. ******************************************************************************/
  12406. static void pt_add_core(struct device *dev)
  12407. {
  12408. struct pt_core_data *d;
  12409. struct pt_core_data *cd = dev_get_drvdata(dev);
  12410. mutex_lock(&core_list_lock);
  12411. list_for_each_entry(d, &core_list, node)
  12412. if (d->dev == dev)
  12413. goto unlock;
  12414. list_add(&cd->node, &core_list);
  12415. unlock:
  12416. mutex_unlock(&core_list_lock);
  12417. }
  12418. /*******************************************************************************
  12419. * FUNCTION: pt_del_core
  12420. *
  12421. * SUMMARY: Remove core data from the core_list.
  12422. *
  12423. * PARAMETERS:
  12424. * *dev - pointer to device structure
  12425. ******************************************************************************/
  12426. static void pt_del_core(struct device *dev)
  12427. {
  12428. struct pt_core_data *d, *d_n;
  12429. mutex_lock(&core_list_lock);
  12430. list_for_each_entry_safe(d, d_n, &core_list, node)
  12431. if (d->dev == dev) {
  12432. list_del(&d->node);
  12433. goto unlock;
  12434. }
  12435. unlock:
  12436. mutex_unlock(&core_list_lock);
  12437. }
  12438. /*******************************************************************************
  12439. * FUNCTION: pt_register_module
  12440. *
  12441. * SUMMARY: Register the module to module_list and probe the module for each
  12442. * core.
  12443. *
  12444. * RETURN:
  12445. * 0 = success
  12446. * !0 = failure
  12447. *
  12448. * PARAMETERS:
  12449. * *module - pointer to module structure
  12450. ******************************************************************************/
  12451. int pt_register_module(struct pt_module *module)
  12452. {
  12453. struct pt_module *m;
  12454. struct pt_core_data *cd;
  12455. int rc = 0;
  12456. if (!module || !module->probe || !module->release)
  12457. return -EINVAL;
  12458. mutex_lock(&module_list_lock);
  12459. list_for_each_entry(m, &module_list, node)
  12460. if (m == module) {
  12461. rc = -EEXIST;
  12462. goto unlock;
  12463. }
  12464. list_add(&module->node, &module_list);
  12465. /* Probe the module for each core */
  12466. mutex_lock(&core_list_lock);
  12467. list_for_each_entry(cd, &core_list, node)
  12468. pt_probe_module(cd, module);
  12469. mutex_unlock(&core_list_lock);
  12470. unlock:
  12471. mutex_unlock(&module_list_lock);
  12472. return rc;
  12473. }
  12474. EXPORT_SYMBOL_GPL(pt_register_module);
  12475. /*******************************************************************************
  12476. * FUNCTION: pt_unregister_module
  12477. *
  12478. * SUMMARY: Release the module for each core and remove the module from
  12479. * module_list.
  12480. *
  12481. * RETURN:
  12482. * 0 = success
  12483. * !0 = failure
  12484. *
  12485. * PARAMETERS:
  12486. * *module - pointer to module structure
  12487. ******************************************************************************/
  12488. void pt_unregister_module(struct pt_module *module)
  12489. {
  12490. struct pt_module *m, *m_n;
  12491. struct pt_core_data *cd;
  12492. if (!module)
  12493. return;
  12494. mutex_lock(&module_list_lock);
  12495. /* Release the module for each core */
  12496. mutex_lock(&core_list_lock);
  12497. list_for_each_entry(cd, &core_list, node)
  12498. pt_release_module(cd, module);
  12499. mutex_unlock(&core_list_lock);
  12500. list_for_each_entry_safe(m, m_n, &module_list, node)
  12501. if (m == module) {
  12502. list_del(&m->node);
  12503. break;
  12504. }
  12505. mutex_unlock(&module_list_lock);
  12506. }
  12507. EXPORT_SYMBOL_GPL(pt_unregister_module);
  12508. /*******************************************************************************
  12509. * FUNCTION: pt_get_module_data
  12510. *
  12511. * SUMMARY: Get module data from module_node by module_list.
  12512. *
  12513. * RETURN:
  12514. * pointer to module data
  12515. *
  12516. * PARAMETERS:
  12517. * *dev - pointer to device structure
  12518. * *module - pointer to module structure
  12519. ******************************************************************************/
  12520. void *pt_get_module_data(struct device *dev, struct pt_module *module)
  12521. {
  12522. struct pt_core_data *cd = dev_get_drvdata(dev);
  12523. struct module_node *m;
  12524. void *data = NULL;
  12525. mutex_lock(&cd->module_list_lock);
  12526. list_for_each_entry(m, &cd->module_list, node)
  12527. if (m->module == module) {
  12528. data = m->data;
  12529. break;
  12530. }
  12531. mutex_unlock(&cd->module_list_lock);
  12532. return data;
  12533. }
  12534. EXPORT_SYMBOL(pt_get_module_data);
  12535. #ifdef CONFIG_HAS_EARLYSUSPEND
  12536. /*******************************************************************************
  12537. * FUNCTION: pt_early_suspend
  12538. *
  12539. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_SUSPEND"
  12540. * attention list.
  12541. *
  12542. * PARAMETERS:
  12543. * *h - pointer to early_suspend structure
  12544. ******************************************************************************/
  12545. static void pt_early_suspend(struct early_suspend *h)
  12546. {
  12547. struct pt_core_data *cd =
  12548. container_of(h, struct pt_core_data, es);
  12549. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  12550. }
  12551. /*******************************************************************************
  12552. * FUNCTION: pt_late_resume
  12553. *
  12554. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_RESUME"
  12555. * attention list.
  12556. *
  12557. * PARAMETERS:
  12558. * *h - pointer to early_suspend structure
  12559. ******************************************************************************/
  12560. static void pt_late_resume(struct early_suspend *h)
  12561. {
  12562. struct pt_core_data *cd =
  12563. container_of(h, struct pt_core_data, es);
  12564. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  12565. }
  12566. /*******************************************************************************
  12567. * FUNCTION: pt_setup_early_suspend
  12568. *
  12569. * SUMMARY: Register early/suspend function to the system.
  12570. *
  12571. * PARAMETERS:
  12572. * *cd - pointer to core data
  12573. ******************************************************************************/
  12574. static void pt_setup_early_suspend(struct pt_core_data *cd)
  12575. {
  12576. cd->es.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  12577. cd->es.suspend = pt_early_suspend;
  12578. cd->es.resume = pt_late_resume;
  12579. register_early_suspend(&cd->es);
  12580. }
  12581. #elif defined(CONFIG_FB)
  12582. /*******************************************************************************
  12583. * FUNCTION: fb_notifier_callback
  12584. *
  12585. * SUMMARY: Call back function for FrameBuffer notifier to allow to call
  12586. * resume/suspend attention list.
  12587. *
  12588. * RETURN:
  12589. * 0 = success
  12590. *
  12591. * PARAMETERS:
  12592. * *self - pointer to notifier_block structure
  12593. * event - event type of fb notifier
  12594. * *data - pointer to fb_event structure
  12595. ******************************************************************************/
  12596. static int fb_notifier_callback(struct notifier_block *self,
  12597. unsigned long event, void *data)
  12598. {
  12599. struct pt_core_data *cd =
  12600. container_of(self, struct pt_core_data, fb_notifier);
  12601. struct fb_event *evdata = data;
  12602. int *blank;
  12603. if (event != FB_EVENT_BLANK || !evdata)
  12604. goto exit;
  12605. blank = evdata->data;
  12606. if (*blank == FB_BLANK_UNBLANK) {
  12607. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  12608. if (cd->fb_state != FB_ON) {
  12609. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  12610. #if defined(CONFIG_PM_SLEEP)
  12611. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  12612. pt_core_resume_(cd->dev);
  12613. #endif
  12614. cd->fb_state = FB_ON;
  12615. }
  12616. } else if (*blank == FB_BLANK_POWERDOWN) {
  12617. pt_debug(cd->dev, DL_INFO, "%s: POWERDOWN!\n", __func__);
  12618. if (cd->fb_state != FB_OFF) {
  12619. #if defined(CONFIG_PM_SLEEP)
  12620. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  12621. pt_core_suspend_(cd->dev);
  12622. #endif
  12623. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  12624. cd->fb_state = FB_OFF;
  12625. }
  12626. }
  12627. exit:
  12628. return 0;
  12629. }
  12630. /*******************************************************************************
  12631. * FUNCTION: pt_setup_fb_notifier
  12632. *
  12633. * SUMMARY: Set up call back function into fb notifier.
  12634. *
  12635. * PARAMETERS:
  12636. * *cd - pointer to core data
  12637. ******************************************************************************/
  12638. static void pt_setup_fb_notifier(struct pt_core_data *cd)
  12639. {
  12640. int rc = 0;
  12641. cd->fb_state = FB_ON;
  12642. cd->fb_notifier.notifier_call = fb_notifier_callback;
  12643. rc = fb_register_client(&cd->fb_notifier);
  12644. if (rc)
  12645. pt_debug(cd->dev, DL_ERROR,
  12646. "Unable to register fb_notifier: %d\n", rc);
  12647. }
  12648. #endif
  12649. /*******************************************************************************
  12650. * FUNCTION: pt_watchdog_work
  12651. *
  12652. * SUMMARY: This is where the watchdog work is done except if the DUT is
  12653. * sleeping then this function simply returns. If the DUT is awake the
  12654. * first thing is to ensure the IRQ is not stuck asserted meaning that
  12655. * somehow a response is waiting on the DUT that has not been read. If
  12656. * this occurs the message is simply consumed. If or once the IRQ is
  12657. * cleared, a PIP PING message is sent to the DUT and if the response
  12658. * is received the watchdog succeeds and exits, if no response is seen
  12659. * a startup is queued unless the maximum number of startups have already
  12660. * been attempted, in that case a BL is attempted.
  12661. *
  12662. * NOTE: pt_stop_wd_timer() cannot be called within the context of this
  12663. * work thread
  12664. *
  12665. * RETURN: void
  12666. *
  12667. * PARAMETERS:
  12668. * *work - pointer to a work structure for the watchdog work queue
  12669. ******************************************************************************/
  12670. static void pt_watchdog_work(struct work_struct *work)
  12671. {
  12672. int rc = 0;
  12673. struct pt_core_data *cd = container_of(work,
  12674. struct pt_core_data, watchdog_work);
  12675. /*
  12676. * if found the current sleep_state is SS_SLEEPING
  12677. * then no need to request_exclusive, directly return
  12678. */
  12679. if (cd->sleep_state == SS_SLEEPING)
  12680. return;
  12681. #ifdef TTDL_DIAGNOSTICS
  12682. cd->watchdog_count++;
  12683. #endif /* TTDL_DIAGNOSTICS */
  12684. /*
  12685. * The first WD interval was extended to allow DDI to come up.
  12686. * If the WD interval is not the default then adjust timer to the
  12687. * current setting. The user can override value with drv_debug sysfs.
  12688. */
  12689. if (cd->watchdog_interval != PT_WATCHDOG_TIMEOUT) {
  12690. mod_timer_pending(&cd->watchdog_timer, jiffies +
  12691. msecs_to_jiffies(cd->watchdog_interval));
  12692. }
  12693. if (pt_check_irq_asserted(cd)) {
  12694. #ifdef TTDL_DIAGNOSTICS
  12695. cd->watchdog_irq_stuck_count++;
  12696. pt_toggle_err_gpio(cd, PT_ERR_GPIO_IRQ_STUCK);
  12697. #endif /* TTDL_DIAGNOSTICS */
  12698. pt_debug(cd->dev, DL_WARN,
  12699. "%s: TTDL WD found IRQ asserted, attempt to clear\n",
  12700. __func__);
  12701. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  12702. }
  12703. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  12704. if (rc < 0) {
  12705. pt_debug(cd->dev, DL_ERROR,
  12706. "%s: fail get exclusive ex=%p own=%p\n",
  12707. __func__, cd->exclusive_dev, cd->dev);
  12708. goto queue_startup;
  12709. }
  12710. rc = pt_pip_null_(cd);
  12711. if (release_exclusive(cd, cd->dev) < 0)
  12712. pt_debug(cd->dev, DL_ERROR,
  12713. "%s: fail to release exclusive\n", __func__);
  12714. queue_startup:
  12715. if (rc) {
  12716. #ifdef TTDL_DIAGNOSTICS
  12717. cd->watchdog_failed_access_count++;
  12718. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EXCLUSIVE_ACCESS);
  12719. #endif /* TTDL_DIAGNOSTICS */
  12720. pt_debug(cd->dev, DL_ERROR,
  12721. "%s: failed to access device in WD, retry count=%d\n",
  12722. __func__, cd->startup_retry_count);
  12723. /* Already tried FW upgrade because of watchdog but failed */
  12724. if (cd->startup_retry_count > PT_WATCHDOG_RETRY_COUNT)
  12725. return;
  12726. if (cd->startup_retry_count++ < PT_WATCHDOG_RETRY_COUNT) {
  12727. /*
  12728. * Any wrapper function that trys to disable the
  12729. * WD killing this worker cannot be called here.
  12730. */
  12731. rc = request_exclusive(cd, cd->dev,
  12732. PT_REQUEST_EXCLUSIVE_TIMEOUT);
  12733. if (rc < 0) {
  12734. pt_debug(cd->dev, DL_ERROR,
  12735. "%s: fail get exclusive ex=%p own=%p\n",
  12736. __func__, cd->exclusive_dev, cd->dev);
  12737. goto exit;
  12738. }
  12739. cd->hw_detected = false;
  12740. cd->startup_status = STARTUP_STATUS_START;
  12741. pt_debug(cd->dev, DL_DEBUG,
  12742. "%s: Startup Status Reset\n", __func__);
  12743. rc = pt_dut_reset_and_wait(cd);
  12744. if (release_exclusive(cd, cd->dev) < 0)
  12745. pt_debug(cd->dev, DL_ERROR,
  12746. "%s: fail to release exclusive\n",
  12747. __func__);
  12748. if (!rc) {
  12749. cd->hw_detected = true;
  12750. if (!cd->flashless_dut)
  12751. pt_queue_enum(cd);
  12752. }
  12753. #ifdef TTDL_DIAGNOSTICS
  12754. cd->wd_xres_count++;
  12755. pt_debug(cd->dev, DL_WARN,
  12756. "%s: Comm Failed - DUT reset [#%d]\n",
  12757. __func__, cd->wd_xres_count);
  12758. #endif /* TTDL_DIAGNOSTICS */
  12759. } else {
  12760. /*
  12761. * After trying PT_WATCHDOG_RETRY_COUNT times to
  12762. * reset the part to regain communications, try to BL
  12763. */
  12764. pt_debug(cd->dev, DL_WARN,
  12765. "%s: WD DUT access failure, Start FW Upgrade\n",
  12766. __func__);
  12767. #ifdef TTDL_DIAGNOSTICS
  12768. /*
  12769. * When diagnostics is enabled allow TTDL to keep
  12770. * trying to find the DUT. This allows the DUT to be
  12771. * hot swap-able while the host stays running. In
  12772. * production this may not be wanted as a customer
  12773. * may have several touch drivers and any driver
  12774. * that doesn't match the current DUT should give
  12775. * up trying and give up using the bus.
  12776. */
  12777. pt_debug(cd->dev, DL_INFO,
  12778. "%s: Resetting startup_retry_count\n",
  12779. __func__);
  12780. cd->startup_retry_count = 0;
  12781. #endif /* TTDL_DIAGNOSTICS */
  12782. /*
  12783. * Since fw may be broken,reset sysinfo ready flag
  12784. * to let upgrade function work.
  12785. */
  12786. mutex_lock(&cd->system_lock);
  12787. cd->sysinfo.ready = false;
  12788. mutex_unlock(&cd->system_lock);
  12789. if (cd->active_dut_generation == DUT_UNKNOWN) {
  12790. pt_debug(cd->dev, DL_WARN,
  12791. "%s: Queue Restart\n", __func__);
  12792. pt_queue_restart(cd);
  12793. } else
  12794. kthread_run(start_fw_upgrade, cd, "pt_loader");
  12795. }
  12796. } else {
  12797. cd->hw_detected = true;
  12798. if (cd->startup_status <= (STARTUP_STATUS_FW_RESET_SENTINEL |
  12799. STARTUP_STATUS_BL_RESET_SENTINEL)) {
  12800. pt_debug(cd->dev, DL_WARN,
  12801. "%s: HW detected but not enumerated\n",
  12802. __func__);
  12803. pt_queue_enum(cd);
  12804. }
  12805. }
  12806. exit:
  12807. pt_start_wd_timer(cd);
  12808. }
  12809. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  12810. /*******************************************************************************
  12811. * FUNCTION: pt_watchdog_timer
  12812. *
  12813. * SUMMARY: The function that is called when the WD timer expires. If the
  12814. * watchdog work is not already busy schedule the watchdog work queue.
  12815. *
  12816. * RETURN: void
  12817. *
  12818. * PARAMETERS:
  12819. * handle - Handle to the watchdog timer
  12820. ******************************************************************************/
  12821. static void pt_watchdog_timer(unsigned long handle)
  12822. {
  12823. struct pt_core_data *cd = (struct pt_core_data *)handle;
  12824. if (!cd)
  12825. return;
  12826. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  12827. __func__);
  12828. if (!work_pending(&cd->watchdog_work))
  12829. schedule_work(&cd->watchdog_work);
  12830. }
  12831. #else
  12832. /*******************************************************************************
  12833. * FUNCTION: pt_watchdog_timer
  12834. *
  12835. * SUMMARY: The function that is called when the WD timer expires. If the
  12836. * watchdog work is not already busy schedule the watchdog work queue.
  12837. *
  12838. * RETURN: void
  12839. *
  12840. * PARAMETERS:
  12841. * *t - Pointer to timer list
  12842. ******************************************************************************/
  12843. static void pt_watchdog_timer(struct timer_list *t)
  12844. {
  12845. struct pt_core_data *cd = from_timer(cd, t, watchdog_timer);
  12846. if (!cd)
  12847. return;
  12848. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  12849. __func__);
  12850. if (!work_pending(&cd->watchdog_work))
  12851. schedule_work(&cd->watchdog_work);
  12852. }
  12853. #endif
  12854. #ifdef PT_PTSBC_SUPPORT
  12855. /* Required to support the Parade Techologies Development Platform */
  12856. static int pt_probe_complete(struct pt_core_data *cd);
  12857. /*******************************************************************************
  12858. * FUNCTION: pt_probe_work
  12859. *
  12860. * SUMMARY: For the PtSBC the probe functionality is split into two functions;
  12861. * pt_probe() and pt_probe_complete() which is called from here.
  12862. * This function is scheduled as a "work" task in order to launch after
  12863. * I2C/SPI is up.
  12864. *
  12865. * RETURN: Void
  12866. *
  12867. * PARAMETERS:
  12868. * *work - pointer to work structure
  12869. ******************************************************************************/
  12870. static void pt_probe_work(struct work_struct *work)
  12871. {
  12872. struct pt_core_data *cd =
  12873. container_of(work, struct pt_core_data,
  12874. probe_work);
  12875. int rc;
  12876. rc = pt_probe_complete(cd);
  12877. if (rc < 0)
  12878. pr_err("%s: Probe_complete returns rc=%d\n", __func__, rc);
  12879. else
  12880. pt_debug(cd->dev, DL_INFO,
  12881. "%s: Probe_complete returns rc=%d\n", __func__, rc);
  12882. }
  12883. /*******************************************************************************
  12884. * FUNCTION: pt_probe_timer
  12885. *
  12886. * SUMMARY: For the PtSBC the probe functionality is split into two functions;
  12887. * pt_probe() and pt_probe_complete(). This timer shedules the
  12888. * probe_work function.
  12889. *
  12890. * RETURN: Void
  12891. *
  12892. * PARAMETERS:
  12893. * handle - pointer to the core data
  12894. ******************************************************************************/
  12895. static void pt_probe_timer(unsigned long handle)
  12896. {
  12897. struct pt_core_data *cd = (struct pt_core_data *)handle;
  12898. if (!cd)
  12899. return;
  12900. pt_debug(cd->dev, DL_INFO, "%s: Watchdog timer triggered\n",
  12901. __func__);
  12902. if (!work_pending(&cd->probe_work))
  12903. schedule_work(&cd->probe_work);
  12904. }
  12905. #endif /* --- End PT_PTSBC_SUPPORT --- */
  12906. /*******************************************************************************
  12907. * Core sysfs show and store functions
  12908. ******************************************************************************/
  12909. /*******************************************************************************
  12910. * FUNCTION: pt_hw_version_show
  12911. *
  12912. * SUMMARY: Gets the HW version for either PIP1.x or PIP2.x DUTS
  12913. * Output data format: [SiliconID].[RevID FamilyID].[PanelID]
  12914. *
  12915. * RETURN: size of data written to sysfs node
  12916. *
  12917. * PARAMETERS:
  12918. * *dev - pointer to device structure
  12919. * *attr - pointer to device attributes structure
  12920. * *buf - pointer to print output buffer
  12921. ******************************************************************************/
  12922. static ssize_t pt_hw_version_show(struct device *dev,
  12923. struct device_attribute *attr, char *buf)
  12924. {
  12925. struct pt_core_data *cd = dev_get_drvdata(dev);
  12926. _pt_request_hw_version(dev, cd->hw_version);
  12927. return snprintf(buf, PT_MAX_PRBUF_SIZE, "%s\n", cd->hw_version);
  12928. }
  12929. static DEVICE_ATTR(hw_version, 0444, pt_hw_version_show, NULL);
  12930. /*******************************************************************************
  12931. * FUNCTION: pt_drv_version_show
  12932. *
  12933. * SUMMARY: Show method for the drv_version sysfs node that will show the
  12934. * TTDL version information
  12935. *
  12936. * RETURN: Char buffer with printed TTDL version information
  12937. *
  12938. * PARAMETERS:
  12939. * *dev - pointer to device structure
  12940. * *attr - pointer to device attributes
  12941. * *buf - pointer to output buffer
  12942. ******************************************************************************/
  12943. static ssize_t pt_drv_version_show(struct device *dev,
  12944. struct device_attribute *attr, char *buf)
  12945. {
  12946. return snprintf(buf, PT_MAX_PRBUF_SIZE,
  12947. "Driver: %s\nVersion: %s\nDate: %s\n",
  12948. pt_driver_core_name, pt_driver_core_version,
  12949. pt_driver_core_date);
  12950. }
  12951. static DEVICE_ATTR(drv_version, 0444, pt_drv_version_show, NULL);
  12952. static DEVICE_ATTR(drv_ver, 0444, pt_drv_version_show, NULL);
  12953. /*******************************************************************************
  12954. * FUNCTION: pt_fw_version_show
  12955. *
  12956. * SUMMARY: Show method for the fw_version sysfs node that will
  12957. * show the firmware, bootloader and PIP version information
  12958. *
  12959. * RETURN: Size of printed buffer
  12960. *
  12961. * PARAMETERS:
  12962. * *dev - pointer to device structure
  12963. * *attr - pointer to device attributes
  12964. * *buf - pointer to output buffer
  12965. ******************************************************************************/
  12966. static ssize_t pt_fw_version_show(struct device *dev,
  12967. struct device_attribute *attr, char *buf)
  12968. {
  12969. struct pt_core_data *cd = dev_get_drvdata(dev);
  12970. struct pt_ttdata *ttdata;
  12971. int rc = 0;
  12972. if (cd->mode == PT_MODE_OPERATIONAL)
  12973. rc = pt_hid_output_get_sysinfo_(cd);
  12974. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  12975. __func__, cd->mode, cd->sysinfo.ready);
  12976. if (cd->sysinfo.ready)
  12977. ttdata = &cd->sysinfo.ttdata;
  12978. else
  12979. rc = -ENODATA;
  12980. if (!rc) {
  12981. return scnprintf(buf, strlen(buf),
  12982. "Status: %d\n"
  12983. "FW : %d.%d.%d\n"
  12984. "Config: %d\n"
  12985. "BL : %d.%d\n"
  12986. "PIP : %d.%d\n",
  12987. rc,
  12988. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  12989. ttdata->revctrl,
  12990. ttdata->fw_ver_conf,
  12991. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  12992. ttdata->pip_ver_major, ttdata->pip_ver_minor);
  12993. } else {
  12994. return scnprintf(buf, strlen(buf),
  12995. "Status: %d\n"
  12996. "FW : n/a\n"
  12997. "Config: n/a\n"
  12998. "BL : n/a\n"
  12999. "PIP : n/a\n",
  13000. rc);
  13001. }
  13002. }
  13003. static DEVICE_ATTR(fw_version, 0444, pt_fw_version_show, NULL);
  13004. /*******************************************************************************
  13005. * FUNCTION: pt_sysinfo_show
  13006. *
  13007. * SUMMARY: Show method for the sysinfo sysfs node that will
  13008. * show all the information from get system information command.
  13009. *
  13010. * RETURN: Size of printed buffer
  13011. *
  13012. * PARAMETERS:
  13013. * *dev - pointer to device structure
  13014. * *attr - pointer to device attributes
  13015. * *buf - pointer to output buffer
  13016. ******************************************************************************/
  13017. static ssize_t pt_sysinfo_show(struct device *dev,
  13018. struct device_attribute *attr, char *buf)
  13019. {
  13020. struct pt_core_data *cd = dev_get_drvdata(dev);
  13021. struct pt_sysinfo *si;
  13022. struct pt_ttdata *ttdata = NULL;
  13023. struct pt_sensing_conf_data *scd = NULL;
  13024. int rc = 0;
  13025. if (cd->mode == PT_MODE_OPERATIONAL) {
  13026. rc = pt_hid_output_get_sysinfo_(cd);
  13027. if (cd->sysinfo.ready) {
  13028. si = &cd->sysinfo;
  13029. ttdata = &si->ttdata;
  13030. scd = &si->sensing_conf_data;
  13031. } else
  13032. rc = -ENODATA;
  13033. } else
  13034. rc = -EPERM;
  13035. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  13036. __func__, cd->mode, cd->sysinfo.ready);
  13037. if (!rc && ttdata && scd) {
  13038. return scnprintf(buf, strlen(buf),
  13039. "Status: %d\n"
  13040. "pip_ver_major: 0x%02X\n"
  13041. "pip_ver_minor: 0x%02X\n"
  13042. "fw_pid : 0x%04X\n"
  13043. "fw_ver_major : 0x%02X\n"
  13044. "fw_ver_minor : 0x%02X\n"
  13045. "revctrl : 0x%08X\n"
  13046. "fw_ver_conf : 0x%04X\n"
  13047. "bl_ver_major : 0x%02X\n"
  13048. "bl_ver_minor : 0x%02X\n"
  13049. "jtag_id_h : 0x%04X\n"
  13050. "jtag_id_l : 0x%04X\n"
  13051. "mfg_id[0] : 0x%02X\n"
  13052. "mfg_id[1] : 0x%02X\n"
  13053. "mfg_id[2] : 0x%02X\n"
  13054. "mfg_id[3] : 0x%02X\n"
  13055. "mfg_id[4] : 0x%02X\n"
  13056. "mfg_id[5] : 0x%02X\n"
  13057. "mfg_id[6] : 0x%02X\n"
  13058. "mfg_id[7] : 0x%02X\n"
  13059. "post_code : 0x%04X\n"
  13060. "electrodes_x : 0x%02X\n"
  13061. "electrodes_y : 0x%02X\n"
  13062. "len_x : 0x%04X\n"
  13063. "len_y : 0x%04X\n"
  13064. "res_x : 0x%04X\n"
  13065. "res_y : 0x%04X\n"
  13066. "max_z : 0x%04X\n"
  13067. "origin_x : 0x%02X\n"
  13068. "origin_y : 0x%02X\n"
  13069. "panel_id : 0x%02X\n"
  13070. "btn : 0x%02X\n"
  13071. "scan_mode : 0x%02X\n"
  13072. "max_num_of_tch_per_refresh_cycle: 0x%02X\n",
  13073. rc,
  13074. ttdata->pip_ver_major,
  13075. ttdata->pip_ver_minor,
  13076. ttdata->fw_pid,
  13077. ttdata->fw_ver_major,
  13078. ttdata->fw_ver_minor,
  13079. ttdata->revctrl,
  13080. ttdata->fw_ver_conf,
  13081. ttdata->bl_ver_major,
  13082. ttdata->bl_ver_minor,
  13083. ttdata->jtag_id_h,
  13084. ttdata->jtag_id_l,
  13085. ttdata->mfg_id[0],
  13086. ttdata->mfg_id[1],
  13087. ttdata->mfg_id[2],
  13088. ttdata->mfg_id[3],
  13089. ttdata->mfg_id[4],
  13090. ttdata->mfg_id[5],
  13091. ttdata->mfg_id[6],
  13092. ttdata->mfg_id[7],
  13093. ttdata->post_code,
  13094. scd->electrodes_x,
  13095. scd->electrodes_y,
  13096. scd->len_x,
  13097. scd->len_y,
  13098. scd->res_x,
  13099. scd->res_y,
  13100. scd->max_z,
  13101. scd->origin_x,
  13102. scd->origin_y,
  13103. scd->panel_id,
  13104. scd->btn,
  13105. scd->scan_mode,
  13106. scd->max_tch);
  13107. } else {
  13108. return scnprintf(buf, strlen(buf),
  13109. "Status: %d\n",
  13110. rc);
  13111. }
  13112. }
  13113. static DEVICE_ATTR(sysinfo, 0444, pt_sysinfo_show, NULL);
  13114. #ifndef TTDL_KERNEL_SUBMISSION
  13115. /*******************************************************************************
  13116. * FUNCTION: pt_hw_reset_show
  13117. *
  13118. * SUMMARY: The show method for the hw_reset sysfs node that does a hw reset
  13119. * by toggling the XRES line and then calls the startup function to
  13120. * allow TTDL to re-enumerate the DUT.
  13121. * The printed value reflects the status of the full reset/enum.
  13122. *
  13123. * PARAMETERS:
  13124. * *dev - pointer to Device structure
  13125. * *attr - pointer to the device attribute structure
  13126. * *buf - pointer to buffer to print
  13127. ******************************************************************************/
  13128. static ssize_t pt_hw_reset_show(struct device *dev,
  13129. struct device_attribute *attr, char *buf)
  13130. {
  13131. struct pt_core_data *cd = dev_get_drvdata(dev);
  13132. int rc = 0;
  13133. int time = 0;
  13134. u8 reset_status = 0;
  13135. int t;
  13136. struct pt_hid_desc hid_desc;
  13137. memset(&hid_desc, 0, sizeof(hid_desc));
  13138. /* Only allow DUT reset if no active BL in progress */
  13139. mutex_lock(&cd->firmware_class_lock);
  13140. mutex_lock(&cd->system_lock);
  13141. cd->startup_state = STARTUP_NONE;
  13142. mutex_unlock(&(cd->system_lock));
  13143. pt_stop_wd_timer(cd);
  13144. /* ensure no left over exclusive access is still locked */
  13145. release_exclusive(cd, cd->dev);
  13146. rc = pt_dut_reset(cd, PT_CORE_CMD_PROTECTED);
  13147. if (rc) {
  13148. mutex_unlock(&cd->firmware_class_lock);
  13149. pt_debug(cd->dev, DL_ERROR,
  13150. "%s: HW reset failed rc = %d\n", __func__, rc);
  13151. goto exit_hw_reset;
  13152. }
  13153. reset_status |= 0x01 << 0;
  13154. if (cd->flashless_dut) {
  13155. mutex_unlock(&cd->firmware_class_lock);
  13156. t = wait_event_timeout(cd->wait_q, (cd->fw_updating == true),
  13157. msecs_to_jiffies(200));
  13158. if (IS_TMO(t)) {
  13159. pt_debug(dev, DL_WARN,
  13160. "%s: Timeout waiting for FW update",
  13161. __func__);
  13162. rc = -ETIME;
  13163. goto exit_hw_reset;
  13164. } else {
  13165. pt_debug(dev, DL_INFO,
  13166. "%s: ----- Wait FW Loading ----",
  13167. __func__);
  13168. rc = _pt_request_wait_for_enum_state(
  13169. dev, 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  13170. if (rc) {
  13171. pt_debug(cd->dev, DL_ERROR,
  13172. "%s: No FW Sentinel detected rc = %d\n",
  13173. __func__, rc);
  13174. goto exit_hw_reset;
  13175. }
  13176. reset_status |= 0x01 << 1;
  13177. }
  13178. } else {
  13179. /* Wait for any sentinel */
  13180. rc = _pt_request_wait_for_enum_state(dev, 250,
  13181. STARTUP_STATUS_BL_RESET_SENTINEL |
  13182. STARTUP_STATUS_FW_RESET_SENTINEL);
  13183. if (rc) {
  13184. mutex_unlock(&cd->firmware_class_lock);
  13185. pt_debug(cd->dev, DL_ERROR,
  13186. "%s: No Sentinel detected rc = %d\n",
  13187. __func__, rc);
  13188. goto exit_hw_reset;
  13189. }
  13190. /* sleep needed to ensure no cmd is sent while DUT will NAK */
  13191. msleep(30);
  13192. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  13193. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  13194. if (rc < 0) {
  13195. pt_debug(cd->dev, DL_ERROR,
  13196. "%s: Error on getting HID descriptor r=%d\n",
  13197. __func__, rc);
  13198. goto exit_hw_reset;
  13199. }
  13200. cd->mode = pt_get_mode(cd, &hid_desc);
  13201. if (cd->mode == PT_MODE_BOOTLOADER)
  13202. rc = pt_hid_output_bl_launch_app_(cd);
  13203. } else {
  13204. if (cd->mode == PT_MODE_BOOTLOADER)
  13205. rc = pt_pip2_launch_app(dev,
  13206. PT_CORE_CMD_UNPROTECTED);
  13207. }
  13208. if (rc) {
  13209. mutex_unlock(&cd->firmware_class_lock);
  13210. pt_debug(cd->dev, DL_ERROR,
  13211. "%s: PIP launch app failed rc = %d\n",
  13212. __func__, rc);
  13213. goto exit_hw_reset;
  13214. }
  13215. mutex_unlock(&cd->firmware_class_lock);
  13216. reset_status |= 0x01 << 1;
  13217. msleep(20);
  13218. if ((cd->active_dut_generation == DUT_UNKNOWN) ||
  13219. (cd->mode != PT_MODE_OPERATIONAL))
  13220. pt_queue_restart(cd);
  13221. else
  13222. pt_queue_enum(cd);
  13223. }
  13224. while (!(cd->startup_status & STARTUP_STATUS_COMPLETE) && time < 2000) {
  13225. msleep(50);
  13226. pt_debug(cd->dev, DL_INFO,
  13227. "%s: wait %dms for 0x%04X, current enum=0x%04X\n",
  13228. __func__, time, STARTUP_STATUS_COMPLETE,
  13229. cd->startup_status);
  13230. time += 50;
  13231. }
  13232. if (!(cd->startup_status & STARTUP_STATUS_COMPLETE)) {
  13233. rc = -ETIME;
  13234. goto exit_hw_reset;
  13235. }
  13236. pt_debug(cd->dev, DL_INFO, "%s: HW Reset complete. enum=0x%04X\n",
  13237. __func__, cd->startup_status);
  13238. reset_status |= 0x01 << 2;
  13239. pt_start_wd_timer(cd);
  13240. exit_hw_reset:
  13241. return scnprintf(buf, strlen(buf),
  13242. "Status: %d\n"
  13243. "Reset Status: 0x%02X\n", rc, reset_status);
  13244. }
  13245. static DEVICE_ATTR(hw_reset, 0444, pt_hw_reset_show, NULL);
  13246. /*******************************************************************************
  13247. * FUNCTION: pt_pip2_cmd_rsp_store
  13248. *
  13249. * SUMMARY: This is the store method for the raw PIP2 cmd/rsp sysfs node. Any
  13250. * raw PIP2 command echo'd to this node will be sent directly to the DUT.
  13251. * Command byte order:
  13252. * Byte [0] - PIP2 command ID
  13253. * Byte [1-n] - PIP2 command payload
  13254. *
  13255. * RETURN: Size of passed in buffer
  13256. *
  13257. * PARAMETERS:
  13258. * *dev - pointer to device structure
  13259. * *attr - pointer to device attributes
  13260. * *buf - pointer to buffer that hold the command parameters
  13261. * size - size of buf
  13262. ******************************************************************************/
  13263. static ssize_t pt_pip2_cmd_rsp_store(struct device *dev,
  13264. struct device_attribute *attr, const char *buf, size_t size)
  13265. {
  13266. struct pt_core_data *cd = dev_get_drvdata(dev);
  13267. u16 actual_read_len;
  13268. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  13269. u8 read_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  13270. u8 pip2_cmd_id = 0x00;
  13271. u8 *pip2_cmd_data = NULL;
  13272. int data_len = 0;
  13273. int length;
  13274. int rc = 0;
  13275. /* clear shared data */
  13276. mutex_lock(&cd->sysfs_lock);
  13277. cd->raw_cmd_status = 0;
  13278. cd->cmd_rsp_buf_len = 0;
  13279. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  13280. mutex_unlock(&cd->sysfs_lock);
  13281. length = _pt_ic_parse_input_hex(dev, buf, size,
  13282. input_data, PT_MAX_PIP2_MSG_SIZE);
  13283. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  13284. pt_debug(dev, DL_WARN, "%s: Invalid number of arguments\n",
  13285. __func__);
  13286. rc = -EINVAL;
  13287. goto exit;
  13288. }
  13289. /* Send PIP2 command if enough data was provided */
  13290. if (length >= 1) {
  13291. pip2_cmd_id = input_data[0];
  13292. pip2_cmd_data = &input_data[1];
  13293. data_len = length - 1;
  13294. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  13295. pip2_cmd_id, pip2_cmd_data, data_len,
  13296. read_buf, &actual_read_len);
  13297. cd->raw_cmd_status = rc;
  13298. if (rc) {
  13299. pt_debug(dev, DL_ERROR,
  13300. "%s: PIP2 cmd 0x%02x failed rc = %d\n",
  13301. __func__, pip2_cmd_id, rc);
  13302. goto exit;
  13303. } else {
  13304. cd->cmd_rsp_buf_len = actual_read_len;
  13305. memcpy(cd->cmd_rsp_buf, read_buf, actual_read_len);
  13306. pt_debug(dev, DL_WARN,
  13307. "%s: PIP2 actual_read_len = %d\n",
  13308. __func__, actual_read_len);
  13309. }
  13310. } else {
  13311. rc = -EINVAL;
  13312. pt_debug(dev, DL_ERROR,
  13313. "%s: Insufficient data provided for PIP2 cmd\n",
  13314. __func__);
  13315. }
  13316. exit:
  13317. if (rc)
  13318. return rc;
  13319. return size;
  13320. }
  13321. /*******************************************************************************
  13322. * FUNCTION: pt_pip2_cmd_rsp_show
  13323. *
  13324. * SUMMARY: The show method for the raw pip2_cmd_rsp sysfs node. Any PIP2
  13325. * response generated after using the store method of the pip2_cmd_rsp
  13326. * sysfs node, are available to be read here.
  13327. *
  13328. * PARAMETERS:
  13329. * *dev - pointer to Device structure
  13330. * *attr - pointer to the device attribute structure
  13331. * *buf - pointer to buffer to print
  13332. ******************************************************************************/
  13333. static ssize_t pt_pip2_cmd_rsp_show(struct device *dev,
  13334. struct device_attribute *attr, char *buf)
  13335. {
  13336. struct pt_core_data *cd = dev_get_drvdata(dev);
  13337. int i;
  13338. ssize_t data_len;
  13339. int index;
  13340. mutex_lock(&cd->sysfs_lock);
  13341. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13342. "Status: %d\n", cd->raw_cmd_status);
  13343. if (cd->raw_cmd_status)
  13344. goto error;
  13345. /* Remove the CRC from the length of the response */
  13346. data_len = cd->cmd_rsp_buf_len - 2;
  13347. /* Start printing from the data payload */
  13348. for (i = PIP1_RESP_COMMAND_ID_OFFSET; i < data_len; i++)
  13349. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  13350. "%02X ", cd->cmd_rsp_buf[i]);
  13351. if (data_len >= PIP1_RESP_COMMAND_ID_OFFSET) {
  13352. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  13353. "\n(%zd bytes)\n",
  13354. data_len - PIP1_RESP_COMMAND_ID_OFFSET);
  13355. } else {
  13356. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  13357. "\n(%zd bytes)\n", 0);
  13358. }
  13359. error:
  13360. mutex_unlock(&cd->sysfs_lock);
  13361. return index;
  13362. }
  13363. static DEVICE_ATTR(pip2_cmd_rsp, 0644, pt_pip2_cmd_rsp_show,
  13364. pt_pip2_cmd_rsp_store);
  13365. /*******************************************************************************
  13366. * FUNCTION: pt_command_store
  13367. *
  13368. * SUMMARY: This is the store method for the raw PIP command sysfs node. Any
  13369. * raw PIP command echo'd to this node will be sent directly to the DUT.
  13370. * TTDL will not parse the command.
  13371. *
  13372. * RETURN: Size of passed in buffer
  13373. *
  13374. * PARAMETERS:
  13375. * *dev - pointer to device structure
  13376. * *attr - pointer to device attributes
  13377. * *buf - pointer to buffer that hold the command parameters
  13378. * size - size of buf
  13379. ******************************************************************************/
  13380. static ssize_t pt_command_store(struct device *dev,
  13381. struct device_attribute *attr, const char *buf, size_t size)
  13382. {
  13383. struct pt_core_data *cd = dev_get_drvdata(dev);
  13384. #ifdef TTDL_PTVIRTDUT_SUPPORT
  13385. struct pt_hid_output hid_output;
  13386. #endif
  13387. unsigned short crc;
  13388. u16 actual_read_len;
  13389. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  13390. int length;
  13391. int len_field;
  13392. int rc = 0;
  13393. mutex_lock(&cd->sysfs_lock);
  13394. cd->cmd_rsp_buf_len = 0;
  13395. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  13396. mutex_unlock(&cd->sysfs_lock);
  13397. length = _pt_ic_parse_input_hex(dev, buf, size,
  13398. input_data, PT_MAX_PIP2_MSG_SIZE);
  13399. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  13400. pt_debug(dev, DL_WARN, "%s: Invalid number of arguments\n",
  13401. __func__);
  13402. rc = -EINVAL;
  13403. goto pt_command_store_exit;
  13404. }
  13405. /* Get HID Desc */
  13406. if (length == 2 && input_data[0] == 0x01 && input_data[1] == 0x00) {
  13407. pm_runtime_get_sync(dev);
  13408. rc = pt_get_hid_descriptor(cd, &cd->hid_desc);
  13409. mutex_lock(&cd->sysfs_lock);
  13410. if (!rc) {
  13411. cd->cmd_rsp_buf_len =
  13412. get_unaligned_le16(&cd->response_buf[0]);
  13413. memcpy(cd->cmd_rsp_buf, cd->response_buf,
  13414. cd->cmd_rsp_buf_len);
  13415. }
  13416. cd->raw_cmd_status = rc;
  13417. mutex_unlock(&cd->sysfs_lock);
  13418. pm_runtime_put(dev);
  13419. goto pt_command_store_exit;
  13420. }
  13421. /* Get Report Desc */
  13422. if (length == 2 && input_data[0] == 0x02 && input_data[1] == 0x00) {
  13423. pm_runtime_get_sync(dev);
  13424. rc = pt_get_report_descriptor(cd);
  13425. mutex_lock(&cd->sysfs_lock);
  13426. if (!rc) {
  13427. cd->cmd_rsp_buf_len = cd->hid_core.hid_report_desc_len;
  13428. memcpy(cd->cmd_rsp_buf, cd->response_buf,
  13429. cd->cmd_rsp_buf_len);
  13430. }
  13431. cd->raw_cmd_status = rc;
  13432. mutex_unlock(&cd->sysfs_lock);
  13433. pm_runtime_put(dev);
  13434. goto pt_command_store_exit;
  13435. }
  13436. /* PIP2 messages begin with 01 01 */
  13437. if (length >= 2 && input_data[0] == 0x01 && input_data[1] == 0x01) {
  13438. cd->pip2_prot_active = 1;
  13439. /* Override next seq tag with what was sent */
  13440. cd->pip2_cmd_tag_seq = input_data[4] & 0x0F;
  13441. /* For PIP2 cmd if length does not include crc, add it */
  13442. len_field = (input_data[3] << 8) | input_data[2];
  13443. if (len_field == length && length <= 254) {
  13444. crc = crc_ccitt_calculate(&input_data[2],
  13445. length - 2);
  13446. pt_debug(dev, DL_WARN, "%s: len=%d crc=0x%02X\n",
  13447. __func__, length, crc);
  13448. input_data[length] = (crc & 0xFF00) >> 8;
  13449. input_data[length + 1] = crc & 0x00FF;
  13450. length = length + 2;
  13451. }
  13452. }
  13453. /* write PIP command to log */
  13454. pt_pr_buf(dev, DL_INFO, input_data, length, "command_buf");
  13455. pm_runtime_get_sync(dev);
  13456. #ifdef TTDL_PTVIRTDUT_SUPPORT
  13457. /* Special case for handling Virtual DUT exit */
  13458. if (length >= 3 &&
  13459. input_data[0] == 0xFF &&
  13460. input_data[1] == 0xFF &&
  13461. input_data[2] == 0x00) {
  13462. hid_output.length = length,
  13463. hid_output.write_buf = input_data,
  13464. rc = pt_hid_send_output_user_(cd, &hid_output);
  13465. pm_runtime_put(dev);
  13466. goto pt_command_store_exit;
  13467. }
  13468. #endif
  13469. rc = pt_hid_output_user_cmd(cd, PT_MAX_INPUT, cd->cmd_rsp_buf,
  13470. length, input_data, &actual_read_len);
  13471. pm_runtime_put(dev);
  13472. mutex_lock(&cd->sysfs_lock);
  13473. cd->raw_cmd_status = rc;
  13474. if (rc) {
  13475. cd->cmd_rsp_buf_len = 0;
  13476. pt_debug(dev, DL_ERROR, "%s: Failed to send command: %s\n",
  13477. __func__, buf);
  13478. } else {
  13479. cd->cmd_rsp_buf_len = actual_read_len;
  13480. }
  13481. cd->pip2_prot_active = 0;
  13482. mutex_unlock(&cd->sysfs_lock);
  13483. pt_command_store_exit:
  13484. if (rc)
  13485. return rc;
  13486. return size;
  13487. }
  13488. static DEVICE_ATTR(command, 0220, NULL, pt_command_store);
  13489. /*******************************************************************************
  13490. * FUNCTION: pt_response_show
  13491. *
  13492. * SUMMARY: The show method for the raw PIP response sysfs node. Any PIP
  13493. * response generated after using the pt_command_store sysfs node, are
  13494. * available to be read here.
  13495. *
  13496. * PARAMETERS:
  13497. * *filp - pointer to file structure
  13498. * *kobj - pointer to kobject structure
  13499. * *bin_attr - pointer to bin_attribute structure
  13500. * buf - pointer to cmd input buffer
  13501. * offset - offset index to store input buffer
  13502. * count - size of data in buffer
  13503. ******************************************************************************/
  13504. static ssize_t pt_response_show(struct file *filp,
  13505. struct kobject *kobj, struct bin_attribute *bin_attr,
  13506. char *buf, loff_t offset, size_t count)
  13507. {
  13508. struct device *dev = container_of(kobj, struct device, kobj);
  13509. struct pt_core_data *cd = dev_get_drvdata(dev);
  13510. static int pr_left;
  13511. static int pr_index;
  13512. static u8 *pr_buf;
  13513. int i = 0;
  13514. int index = 0;
  13515. int rc = 0;
  13516. int print_len = 0;
  13517. if (pr_left) {
  13518. if (count < pr_left) {
  13519. index = count;
  13520. memcpy(buf, pr_buf + pr_index, index);
  13521. pr_left -= count;
  13522. pr_index += count;
  13523. } else {
  13524. index = pr_left;
  13525. memcpy(buf, pr_buf + pr_index, index);
  13526. pr_left = 0;
  13527. pr_index = 0;
  13528. kfree(pr_buf);
  13529. }
  13530. return index;
  13531. }
  13532. if (offset == 0) {
  13533. mutex_lock(&cd->sysfs_lock);
  13534. if (cd->raw_cmd_status) {
  13535. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13536. "Status: %d\n", cd->raw_cmd_status);
  13537. mutex_unlock(&cd->sysfs_lock);
  13538. return index;
  13539. }
  13540. /*
  13541. * Allocate memory according to the cost, each byte need 5
  13542. * character, and extra 100 bytes for the header and tail.
  13543. */
  13544. if (cd->cmd_rsp_buf_len > PT_MAX_INPUT) {
  13545. rc = -EINVAL;
  13546. goto exit;
  13547. }
  13548. print_len = cd->cmd_rsp_buf_len * 5 + 100;
  13549. pr_buf = kzalloc(print_len, GFP_KERNEL);
  13550. if (!pr_buf) {
  13551. rc = -ENOMEM;
  13552. goto exit;
  13553. }
  13554. /* Format all data to pr_buf */
  13555. index = scnprintf(pr_buf, print_len - index, "Status: %d\n",
  13556. cd->raw_cmd_status);
  13557. for (i = 0; i < cd->cmd_rsp_buf_len; i++)
  13558. index += scnprintf(pr_buf + index,
  13559. print_len - index, "0x%02X\n",
  13560. cd->cmd_rsp_buf[i]);
  13561. index += scnprintf(pr_buf + index, print_len - index,
  13562. "(%zd bytes)\n", cd->cmd_rsp_buf_len);
  13563. mutex_unlock(&cd->sysfs_lock);
  13564. if (count < index) {
  13565. memcpy(buf, pr_buf, count);
  13566. pr_left = index - count;
  13567. pr_index = count;
  13568. index = count;
  13569. } else {
  13570. memcpy(buf, pr_buf, index);
  13571. pr_left = 0;
  13572. pr_index = 0;
  13573. kfree(pr_buf);
  13574. }
  13575. return index;
  13576. }
  13577. exit:
  13578. mutex_unlock(&cd->sysfs_lock);
  13579. return rc;
  13580. }
  13581. static struct bin_attribute bin_attr_pt_response = {
  13582. .attr = {
  13583. .name = "response",
  13584. .mode = (0444),
  13585. },
  13586. .read = pt_response_show,
  13587. };
  13588. /*******************************************************************************
  13589. * FUNCTION: pt_dut_debug_show
  13590. *
  13591. * SUMMARY: Show method for the dut_debug sysfs node. Shows what parameters
  13592. * are available for the store method.
  13593. *
  13594. * RETURN:
  13595. * 0 = success
  13596. * !0 = failure
  13597. *
  13598. * PARAMETERS:
  13599. * *dev - pointer to device structure
  13600. * *attr - pointer to device attributes
  13601. * *buf - pointer to output buffer
  13602. ******************************************************************************/
  13603. static ssize_t pt_dut_debug_show(struct device *dev,
  13604. struct device_attribute *attr, char *buf)
  13605. {
  13606. ssize_t ret;
  13607. ret = scnprintf(buf, strlen(buf),
  13608. "Status: 0\n"
  13609. "dut_debug sends the following commands to the DUT:\n"
  13610. "%d %s \t- %s\n"
  13611. "%d %s \t- %s\n"
  13612. "%d %s \t- %s\n"
  13613. "%d %s \t- %s\n"
  13614. "%d %s \t- %s\n"
  13615. "%d %s \t- %s\n"
  13616. "%d %s \t- %s\n"
  13617. "%d %s \t- %s\n"
  13618. "%d %s \t- %s\n"
  13619. "%d %s \t- %s\n"
  13620. "%d %s \t- %s\n"
  13621. "%d %s \t- %s\n"
  13622. "%d %s \t- %s\n"
  13623. "%d %s \t- %s\n"
  13624. "%d %s \t- %s\n"
  13625. "%d %s \t- %s\n"
  13626. "%d %s \t- %s\n"
  13627. "%d %s \t- %s\n"
  13628. ,
  13629. PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY, "", "BL Verify APP",
  13630. PT_DUT_DBG_HID_RESET, "", "HID Reset",
  13631. PT_DUT_DBG_HID_SET_POWER_ON, "", "HID SET_POWER ON",
  13632. PT_DUT_DBG_HID_SET_POWER_SLEEP, "", "HID SET_POWER SLEEP",
  13633. PT_DUT_DBG_HID_SET_POWER_STANDBY, "", "HID SET_POWER STANDBY",
  13634. PIP1_BL_CMD_ID_GET_INFO, "", "BL Get Info",
  13635. PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY, "", "BL Program & Verify",
  13636. PIP1_BL_CMD_ID_LAUNCH_APP, "", "BL Launch APP",
  13637. PIP1_BL_CMD_ID_INITIATE_BL, "", "BL Initiate BL",
  13638. PT_DUT_DBG_PIP_SOFT_RESET, "", "PIP Soft Reset",
  13639. PT_DUT_DBG_RESET, "", "Toggle the TP_XRES GPIO",
  13640. PT_DUT_DBG_PIP_NULL, "", "PIP NULL (PING)",
  13641. PT_DUT_DBG_PIP_ENTER_BL, "", "PIP enter BL",
  13642. PT_DUT_DBG_HID_SYSINFO, "", "HID system info",
  13643. PT_DUT_DBG_PIP_SUSPEND_SCAN, "", "Suspend Scan",
  13644. PT_DUT_DBG_PIP_RESUME_SCAN, "", "Resume Scan",
  13645. PT_DUT_DBG_HID_DESC, "", "Get HID Desc",
  13646. PT_DUT_DBG_REPORT_DESC, "", "Get HID Report Desc"
  13647. );
  13648. return ret;
  13649. }
  13650. /*******************************************************************************
  13651. * FUNCTION: pt_drv_debug_show
  13652. *
  13653. * SUMMARY: Show method for the drv_debug sysfs node. Shows what parameters
  13654. * are available for the store method.
  13655. *
  13656. * RETURN:
  13657. * 0 = success
  13658. * !0 = failure
  13659. *
  13660. * PARAMETERS:
  13661. * *dev - pointer to device structure
  13662. * *attr - pointer to device attributes
  13663. * *buf - pointer to output buffer
  13664. ******************************************************************************/
  13665. static ssize_t pt_drv_debug_show(struct device *dev,
  13666. struct device_attribute *attr, char *buf)
  13667. {
  13668. ssize_t ret;
  13669. ret = scnprintf(buf, strlen(buf),
  13670. "Status: 0\n"
  13671. "drv_debug supports the following values:\n"
  13672. "%d %s \t- %s\n"
  13673. "%d %s \t- %s\n"
  13674. "%d %s \t- %s\n"
  13675. "%d %s \t- %s\n"
  13676. "%d %s \t- %s\n"
  13677. "%d %s \t- %s\n"
  13678. "%d %s \t- %s\n"
  13679. "%d %s \t- %s\n"
  13680. "%d %s \t- %s\n"
  13681. "%d %s - %s\n"
  13682. "%d %s \t- %s\n"
  13683. "%d %s \t- %s\n"
  13684. #ifdef TTDL_DIAGNOSTICS
  13685. "%d %s \t- %s\n"
  13686. "%d %s \t- %s\n"
  13687. "%d %s \t- %s\n"
  13688. #endif /* TTDL_DIAGNOSTICS */
  13689. "%d %s \t- %s\n"
  13690. #ifdef TTDL_DIAGNOSTICS
  13691. "%d %s \t- %s\n"
  13692. "%d %s \t- %s\n"
  13693. "%d %s \t- %s\n"
  13694. "%d %s \t- %s\n"
  13695. "%d %s \t- %s\n"
  13696. "%d %s \t- %s\n"
  13697. "%d %s \t- %s\n"
  13698. "%d %s \t- %s\n"
  13699. #endif /* TTDL_DIAGNOSTICS */
  13700. ,
  13701. PT_DRV_DBG_SUSPEND, " ", "Suspend TTDL responding to INT",
  13702. PT_DRV_DBG_RESUME, " ", "Resume TTDL responding to INT",
  13703. PT_DRV_DBG_STOP_WD, " ", "Stop TTDL WD",
  13704. PT_DRV_DBG_START_WD, " ", "Start TTDL WD",
  13705. PT_DRV_DBG_TTHE_TUNER_EXIT, " ", "Exit TTHE Tuner Logging",
  13706. PT_DRV_DBG_TTHE_BUF_CLEAN, " ", "Clear TTHE Tuner buffer",
  13707. PT_DRV_DBG_CLEAR_PARM_LIST, " ", "Clear RAM Param list",
  13708. PT_DRV_DBG_FORCE_BUS_READ, " ", "Force bus read",
  13709. PT_DRV_DBG_CLEAR_CAL_DATA, " ", "Clear CAL Cache",
  13710. PT_DRV_DBG_REPORT_LEVEL, "[0|1|2|3|4]", "Set TTDL Debug Level",
  13711. PT_DRV_DBG_WATCHDOG_INTERVAL, "[n] ", "TTDL WD Interval in ms",
  13712. PT_DRV_DBG_SHOW_TIMESTAMP, "[0|1]", "Show Timestamps"
  13713. #ifdef TTDL_DIAGNOSTICS
  13714. , PT_DRV_DBG_SETUP_PWR, "[0|1]", "Power DUT up/down",
  13715. PT_DRV_DBG_GET_PUT_SYNC, "[0|1]", "Get/Put Linux Sleep",
  13716. PT_DRV_DBG_SET_TT_DATA, "[0|1]", "Display TT_DATA"
  13717. #endif /* TTDL_DIAGNOSTICS */
  13718. , PT_DRV_DBG_SET_GENERATION, "[0|1|2]", "Set DUT generation"
  13719. #ifdef TTDL_DIAGNOSTICS
  13720. , PT_DRV_DBG_SET_BRIDGE_MODE, "[0|1]", "On/Off Bridge Mode",
  13721. PT_DRV_DBG_SET_I2C_ADDRESS, "[0-127]", "I2C DUT Address",
  13722. PT_DRV_DBG_SET_FLASHLESS_DUT, "[0|1]", "Flashless DUT yes/no",
  13723. PT_DRV_DBG_SET_FORCE_SEQ, "[8-15]", "Force PIP2 Sequence #",
  13724. PT_DRV_DBG_BL_WITH_NO_INT, "[0|1]", "BL with no INT",
  13725. PT_DRV_DBG_CAL_CACHE_IN_HOST, "[0|1]", "CAL Cache in host",
  13726. PT_DRV_DBG_NUM_DEVICES, "[1-255]", "Number of Devices",
  13727. PT_DRV_DBG_PIP_TIMEOUT, "[100-7000]", "PIP Resp Timeout (ms)"
  13728. #endif /* TTDL_DIAGNOSTICS */
  13729. );
  13730. return ret;
  13731. }
  13732. /*******************************************************************************
  13733. * FUNCTION: pt_drv_debug_store
  13734. *
  13735. * SUMMARY: Currently the store method for both sysfs nodes: drv_debug and
  13736. * dut_debug. Drv_debug will contain all functionality that can be run
  13737. * without a DUT preset and is available anytime TTDL is running.
  13738. * Dut_debug requires a DUT to be available and will only be created after
  13739. * a DUT has been detected.
  13740. * This function will eventually be split into two but until the overlap
  13741. * has been depricated this function contains all commands that can be
  13742. * used for TTDL/DUT debugging status and control.
  13743. * All commands require at least one value to be passed in *buf with some
  13744. * requiring two.
  13745. *
  13746. * RETURN: Size of passed in buffer
  13747. *
  13748. * PARAMETERS:
  13749. * *dev - pointer to device structure
  13750. * *attr - pointer to device attributes
  13751. * *buf - pointer to buffer that hold the command parameters
  13752. * size - size of buf
  13753. ******************************************************************************/
  13754. static ssize_t pt_drv_debug_store(struct device *dev,
  13755. struct device_attribute *attr, const char *buf, size_t size)
  13756. {
  13757. struct pt_core_data *cd = dev_get_drvdata(dev);
  13758. unsigned long value;
  13759. int rc = 0;
  13760. u8 return_data[8];
  13761. static u8 wd_disabled;
  13762. u32 input_data[3];
  13763. int length;
  13764. #ifdef TTDL_DIAGNOSTICS
  13765. struct i2c_client *client = to_i2c_client(dev);
  13766. unsigned short crc = 0;
  13767. u16 cal_size;
  13768. #endif
  13769. input_data[0] = 0;
  13770. input_data[1] = 0;
  13771. /* Maximmum input is two elements */
  13772. length = _pt_ic_parse_input(dev, buf, size,
  13773. input_data, ARRAY_SIZE(input_data));
  13774. if (length < 1 || length > 2) {
  13775. pt_debug(dev, DL_WARN, "%s: Invalid number of arguments\n",
  13776. __func__);
  13777. rc = -EINVAL;
  13778. goto pt_drv_debug_store_exit;
  13779. }
  13780. value = input_data[0];
  13781. if (length == 1) {
  13782. pt_debug(dev, DL_DEBUG,
  13783. "%s: Debug Cmd Received (id=%d)\n",
  13784. __func__, input_data[0]);
  13785. } else if (length == 2) {
  13786. pt_debug(dev, DL_DEBUG,
  13787. "%s: Debug Cmd Received (id=%d, data=%d)\n",
  13788. __func__, input_data[0], input_data[1]);
  13789. } else {
  13790. pt_debug(dev, DL_DEBUG,
  13791. "%s: Invalid arguments received\n", __func__);
  13792. rc = -EINVAL;
  13793. goto pt_drv_debug_store_exit;
  13794. }
  13795. /* Start watchdog timer command */
  13796. if (value == PT_DRV_DBG_START_WD) {
  13797. pt_debug(dev, DL_INFO, "%s: Cmd: Start Watchdog\n", __func__);
  13798. wd_disabled = 0;
  13799. cd->watchdog_force_stop = false;
  13800. pt_start_wd_timer(cd);
  13801. goto pt_drv_debug_store_exit;
  13802. }
  13803. /* Stop watchdog timer temporarily */
  13804. pt_stop_wd_timer(cd);
  13805. if (value == PT_DRV_DBG_STOP_WD) {
  13806. pt_debug(dev, DL_INFO, "%s: Cmd: Stop Watchdog\n", __func__);
  13807. wd_disabled = 1;
  13808. cd->watchdog_force_stop = true;
  13809. goto pt_drv_debug_store_exit;
  13810. }
  13811. switch (value) {
  13812. case PT_DRV_DBG_SUSPEND: /* 4 */
  13813. pt_debug(dev, DL_INFO, "%s: TTDL: Core Sleep\n", __func__);
  13814. wd_disabled = 1;
  13815. rc = pt_core_sleep(cd);
  13816. if (rc)
  13817. pt_debug(dev, DL_ERROR, "%s: Suspend failed rc=%d\n",
  13818. __func__, rc);
  13819. else
  13820. pt_debug(dev, DL_INFO, "%s: Suspend succeeded\n",
  13821. __func__);
  13822. break;
  13823. case PT_DRV_DBG_RESUME: /* 5 */
  13824. pt_debug(dev, DL_INFO, "%s: TTDL: Wake\n", __func__);
  13825. rc = pt_core_wake(cd);
  13826. if (rc)
  13827. pt_debug(dev, DL_ERROR, "%s: Resume failed rc=%d\n",
  13828. __func__, rc);
  13829. else
  13830. pt_debug(dev, DL_INFO, "%s: Resume succeeded\n",
  13831. __func__);
  13832. break;
  13833. case PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY: /* BL - 49 */
  13834. pt_debug(dev, DL_INFO, "%s: Cmd: verify app integ\n", __func__);
  13835. pt_hid_output_bl_verify_app_integrity(cd, &return_data[0]);
  13836. break;
  13837. case PT_DUT_DBG_HID_RESET: /* 50 */
  13838. pt_debug(dev, DL_INFO, "%s: Cmd: hid_reset\n", __func__);
  13839. pt_hid_cmd_reset(cd);
  13840. break;
  13841. case PT_DUT_DBG_HID_SET_POWER_ON: /* 53 */
  13842. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_on\n", __func__);
  13843. pt_hid_cmd_set_power(cd, HID_POWER_ON);
  13844. wd_disabled = 0;
  13845. break;
  13846. case PT_DUT_DBG_HID_SET_POWER_SLEEP: /* 54 */
  13847. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_off\n",
  13848. __func__);
  13849. wd_disabled = 1;
  13850. pt_hid_cmd_set_power(cd, HID_POWER_SLEEP);
  13851. break;
  13852. case PT_DUT_DBG_HID_SET_POWER_STANDBY: /* 55 */
  13853. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_standby\n",
  13854. __func__);
  13855. wd_disabled = 1;
  13856. pt_hid_cmd_set_power(cd, HID_POWER_STANDBY);
  13857. break;
  13858. case PIP1_BL_CMD_ID_GET_INFO: /* BL - 56 */
  13859. pt_debug(dev, DL_INFO, "%s: Cmd: bl get info\n", __func__);
  13860. pt_hid_output_bl_get_information(cd, return_data);
  13861. break;
  13862. case PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY: /* BL - 57 */
  13863. pt_debug(dev, DL_INFO, "%s: Cmd: program and verify\n",
  13864. __func__);
  13865. pt_hid_output_bl_program_and_verify(cd, 0, NULL);
  13866. break;
  13867. case PIP1_BL_CMD_ID_LAUNCH_APP: /* BL - 59 */
  13868. pt_debug(dev, DL_INFO, "%s: Cmd: launch app\n", __func__);
  13869. pt_hid_output_bl_launch_app(cd);
  13870. break;
  13871. case PIP1_BL_CMD_ID_INITIATE_BL: /* BL - 72 */
  13872. pt_debug(dev, DL_INFO, "%s: Cmd: initiate bl\n", __func__);
  13873. pt_hid_output_bl_initiate_bl(cd, 0, NULL, 0, NULL);
  13874. break;
  13875. case PT_DUT_DBG_PIP_SOFT_RESET: /* 97 */
  13876. pt_debug(dev, DL_INFO, "%s: Cmd: Soft Reset\n", __func__);
  13877. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  13878. break;
  13879. case PT_DUT_DBG_RESET: /* 98 */
  13880. pt_debug(dev, DL_INFO, "%s: Cmd: Hard Reset\n", __func__);
  13881. rc = pt_hw_hard_reset(cd);
  13882. break;
  13883. case PT_DUT_DBG_PIP_NULL: /* 100 */
  13884. pt_debug(dev, DL_INFO, "%s: Cmd: Ping (null)\n", __func__);
  13885. pt_pip_null(cd);
  13886. break;
  13887. case PT_DUT_DBG_PIP_ENTER_BL: /* 101 */
  13888. pt_debug(dev, DL_INFO, "%s: Cmd: start_bootloader\n", __func__);
  13889. rc = pt_pip_start_bootloader(cd);
  13890. if (!rc) {
  13891. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  13892. cd->mode = PT_MODE_BOOTLOADER;
  13893. }
  13894. break;
  13895. case PT_DUT_DBG_HID_SYSINFO: /* 102 */
  13896. pt_debug(dev, DL_INFO, "%s: Cmd: get_sysinfo\n", __func__);
  13897. pt_hid_output_get_sysinfo(cd);
  13898. break;
  13899. case PT_DUT_DBG_PIP_SUSPEND_SCAN: /* 103 */
  13900. pt_debug(dev, DL_INFO, "%s: Cmd: suspend_scanning\n", __func__);
  13901. pt_pip_suspend_scanning(cd);
  13902. break;
  13903. case PT_DUT_DBG_PIP_RESUME_SCAN: /* 104 */
  13904. pt_debug(dev, DL_INFO, "%s: Cmd: resume_scanning\n", __func__);
  13905. pt_pip_resume_scanning(cd);
  13906. break;
  13907. #ifdef TTHE_TUNER_SUPPORT
  13908. case PT_DRV_DBG_TTHE_TUNER_EXIT: /* 107 */
  13909. cd->tthe_exit = 1;
  13910. wake_up(&cd->wait_q);
  13911. kfree(cd->tthe_buf);
  13912. cd->tthe_buf = NULL;
  13913. cd->tthe_exit = 0;
  13914. break;
  13915. case PT_DRV_DBG_TTHE_BUF_CLEAN: /* 108 */
  13916. if (cd->tthe_buf)
  13917. memset(cd->tthe_buf, 0, PT_MAX_PRBUF_SIZE);
  13918. else
  13919. pt_debug(dev, DL_INFO, "%s : tthe_buf not existed\n",
  13920. __func__);
  13921. break;
  13922. #endif
  13923. #ifdef TTDL_DIAGNOSTICS
  13924. case PT_DUT_DBG_HID_DESC: /* 109 */
  13925. pt_debug(dev, DL_INFO, "%s: Cmd: get_hid_desc\n", __func__);
  13926. pt_get_hid_descriptor(cd, &cd->hid_desc);
  13927. break;
  13928. case PT_DRV_DBG_CLEAR_PARM_LIST: /* 110 */
  13929. pt_debug(dev, DL_INFO, "%s: TTDL: Clear Parameter List\n",
  13930. __func__);
  13931. pt_erase_parameter_list(cd);
  13932. break;
  13933. case PT_DRV_DBG_FORCE_BUS_READ: /* 111 */
  13934. rc = pt_read_input(cd);
  13935. if (!rc)
  13936. pt_parse_input(cd);
  13937. break;
  13938. case PT_DRV_DBG_CLEAR_CAL_DATA: /* 112 */
  13939. rc = _pt_manage_local_cal_data(dev, PT_CAL_DATA_CLEAR,
  13940. &cal_size, &crc);
  13941. if (rc)
  13942. pt_debug(dev, DL_ERROR,
  13943. "%s: CAL Data clear failed rc=%d\n",
  13944. __func__, rc);
  13945. else
  13946. pt_debug(dev, DL_INFO,
  13947. "%s: CAL Cleared, Chip ID=0x%04X size=%d\n",
  13948. __func__, crc, size);
  13949. break;
  13950. case PT_DUT_DBG_REPORT_DESC: /* 113 */
  13951. rc = pt_get_report_descriptor(cd);
  13952. if (rc != 0) {
  13953. pt_debug(cd->dev, DL_ERROR,
  13954. "%s: Error on getting report descriptor r=%d\n",
  13955. __func__, rc);
  13956. }
  13957. break;
  13958. case PT_DRV_DBG_REPORT_LEVEL: /* 200 */
  13959. mutex_lock(&cd->system_lock);
  13960. if (input_data[1] >= 0 && input_data[1] < DL_MAX) {
  13961. cd->debug_level = input_data[1];
  13962. pt_debug(dev, DL_INFO, "%s: Set debug_level: %d\n",
  13963. __func__, cd->debug_level);
  13964. } else {
  13965. rc = -EINVAL;
  13966. pt_debug(dev, DL_ERROR, "%s: Invalid debug_level: %d\n",
  13967. __func__, input_data[1]);
  13968. }
  13969. mutex_unlock(&(cd->system_lock));
  13970. break;
  13971. #endif
  13972. case PT_DRV_DBG_WATCHDOG_INTERVAL: /* 201 */
  13973. mutex_lock(&cd->system_lock);
  13974. if (input_data[1] > 100) {
  13975. cd->watchdog_interval = input_data[1];
  13976. pt_debug(dev, DL_INFO,
  13977. "%s: Set watchdog_ interval to: %d\n",
  13978. __func__, cd->watchdog_interval);
  13979. pt_start_wd_timer(cd);
  13980. } else {
  13981. rc = -EINVAL;
  13982. pt_debug(dev, DL_ERROR,
  13983. "%s: Invalid watchdog interval: %d\n",
  13984. __func__, input_data[1]);
  13985. }
  13986. mutex_unlock(&(cd->system_lock));
  13987. break;
  13988. #ifdef TTDL_DIAGNOSTICS
  13989. case PT_DRV_DBG_SHOW_TIMESTAMP: /* 202 */
  13990. mutex_lock(&cd->system_lock);
  13991. if (input_data[1] == 0) {
  13992. cd->show_timestamp = 0;
  13993. pt_debug(dev, DL_INFO, "%s: Disable show_timestamp\n",
  13994. __func__);
  13995. } else if (input_data[1] == 1) {
  13996. cd->show_timestamp = 1;
  13997. pt_debug(dev, DL_INFO, "%s: Enable show_timestamp\n",
  13998. __func__);
  13999. } else {
  14000. rc = -EINVAL;
  14001. pt_debug(dev, DL_ERROR,
  14002. "%s: Invalid parameter: %d\n",
  14003. __func__, input_data[1]);
  14004. }
  14005. mutex_unlock(&(cd->system_lock));
  14006. break;
  14007. case PT_DRV_DBG_SETUP_PWR: /* 205 */
  14008. if (input_data[1] == 0) {
  14009. cd->cpdata->setup_power(cd->cpdata,
  14010. PT_MT_POWER_OFF, cd->dev);
  14011. pt_debug(dev, DL_INFO,
  14012. "%s: Initiate Power Off\n", __func__);
  14013. } else if (input_data[1] == 1) {
  14014. cd->cpdata->setup_power(cd->cpdata,
  14015. PT_MT_POWER_ON, cd->dev);
  14016. pt_debug(dev, DL_INFO,
  14017. "%s: Initiate Power On\n", __func__);
  14018. } else {
  14019. rc = -EINVAL;
  14020. pt_debug(dev, DL_ERROR,
  14021. "%s: Invalid parameter: %d\n",
  14022. __func__, input_data[1]);
  14023. }
  14024. break;
  14025. case PT_DRV_DBG_GET_PUT_SYNC: /* 206 */
  14026. if (input_data[1] == 0) {
  14027. pm_runtime_put(dev);
  14028. pt_debug(dev, DL_WARN,
  14029. "%s: Force call pm_runtime_put()\n", __func__);
  14030. } else if (input_data[1] == 1) {
  14031. pm_runtime_get_sync(dev);
  14032. pt_debug(dev, DL_WARN,
  14033. "%s: Force call pm_runtime_get_sync()\n",
  14034. __func__);
  14035. } else {
  14036. rc = -EINVAL;
  14037. pt_debug(dev, DL_ERROR,
  14038. "%s: WARNING: Invalid parameter: %d\n",
  14039. __func__, input_data[1]);
  14040. }
  14041. break;
  14042. case PT_DRV_DBG_SET_TT_DATA: /* 208 */
  14043. mutex_lock(&cd->system_lock);
  14044. if (input_data[1] == 0) {
  14045. cd->show_tt_data = false;
  14046. pt_debug(dev, DL_INFO,
  14047. "%s: Disable TT_DATA\n", __func__);
  14048. } else if (input_data[1] == 1) {
  14049. cd->show_tt_data = true;
  14050. pt_debug(dev, DL_INFO,
  14051. "%s: Enable TT_DATA\n", __func__);
  14052. } else {
  14053. rc = -EINVAL;
  14054. pt_debug(dev, DL_ERROR,
  14055. "%s: Invalid parameter: %d\n",
  14056. __func__, input_data[1]);
  14057. }
  14058. mutex_unlock(&(cd->system_lock));
  14059. break;
  14060. case PT_DRV_DBG_SET_GENERATION: /* 210 */
  14061. if (input_data[1] == cd->active_dut_generation) {
  14062. mutex_lock(&cd->system_lock);
  14063. cd->set_dut_generation = true;
  14064. mutex_unlock(&(cd->system_lock));
  14065. } else {
  14066. mutex_lock(&cd->system_lock);
  14067. if (input_data[1] == 0) {
  14068. cd->active_dut_generation = DUT_UNKNOWN;
  14069. cd->set_dut_generation = false;
  14070. } else if (input_data[1] == 1) {
  14071. cd->active_dut_generation = DUT_PIP1_ONLY;
  14072. cd->set_dut_generation = true;
  14073. } else if (input_data[1] == 2) {
  14074. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  14075. cd->set_dut_generation = true;
  14076. } else {
  14077. rc = -EINVAL;
  14078. pt_debug(dev, DL_ERROR,
  14079. "%s: Invalid parameter: %d\n",
  14080. __func__, input_data[1]);
  14081. break;
  14082. }
  14083. cd->startup_status = STARTUP_STATUS_START;
  14084. pt_debug(cd->dev, DL_DEBUG,
  14085. "%s: Startup Status Reset\n", __func__);
  14086. mutex_unlock(&(cd->system_lock));
  14087. pt_debug(dev, DL_INFO,
  14088. "%s: Active DUT Generation Set to: %d\n",
  14089. __func__, cd->active_dut_generation);
  14090. /* Changing DUT generations full restart needed */
  14091. pt_queue_restart(cd);
  14092. }
  14093. break;
  14094. case PT_DRV_DBG_SET_BRIDGE_MODE: /* 211 */
  14095. mutex_lock(&cd->system_lock);
  14096. if (input_data[1] == 0) {
  14097. cd->bridge_mode = false;
  14098. pt_debug(dev, DL_INFO,
  14099. "%s: Disable Bridge Mode\n", __func__);
  14100. } else if (input_data[1] == 1) {
  14101. cd->bridge_mode = true;
  14102. pt_debug(dev, DL_INFO,
  14103. "%s: Enable Bridge Mode\n", __func__);
  14104. } else {
  14105. rc = -EINVAL;
  14106. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  14107. __func__, input_data[1]);
  14108. }
  14109. mutex_unlock(&(cd->system_lock));
  14110. break;
  14111. case PT_DRV_DBG_SET_I2C_ADDRESS: /* 212 */
  14112. mutex_lock(&cd->system_lock);
  14113. /* Only a 7bit address is valid */
  14114. if (input_data[1] >= 0 && input_data[1] <= 0x7F) {
  14115. client->addr = input_data[1];
  14116. pt_debug(dev, DL_INFO,
  14117. "%s: Set I2C Address: 0x%2X\n",
  14118. __func__, client->addr);
  14119. } else {
  14120. rc = -EINVAL;
  14121. pt_debug(dev, DL_ERROR, "%s: Invalid I2C Address %d\n",
  14122. __func__, input_data[1]);
  14123. client->addr = 0x24;
  14124. }
  14125. mutex_unlock(&(cd->system_lock));
  14126. break;
  14127. case PT_DRV_DBG_SET_FLASHLESS_DUT: /* 213 */
  14128. mutex_lock(&cd->system_lock);
  14129. if (input_data[1] == 0) {
  14130. cd->flashless_dut = 0;
  14131. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  14132. pt_debug(dev, DL_INFO, "%s: Disable FLAHLESS DUT\n",
  14133. __func__);
  14134. } else if (input_data[1] == 1) {
  14135. cd->flashless_dut = 1;
  14136. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  14137. pt_debug(dev, DL_INFO, "%s: Enable FLAHLESS DUT\n",
  14138. __func__);
  14139. }
  14140. mutex_unlock(&(cd->system_lock));
  14141. break;
  14142. case PT_DRV_DBG_SET_FORCE_SEQ: /* 214 */
  14143. mutex_lock(&cd->system_lock);
  14144. if (input_data[1] >= 0x8 && input_data[1] <= 0xF) {
  14145. cd->force_pip2_seq = input_data[1];
  14146. cd->pip2_cmd_tag_seq = input_data[1];
  14147. pt_debug(dev, DL_INFO,
  14148. "%s: Force PIP2 Seq to: 0x%02X\n",
  14149. __func__, input_data[1]);
  14150. } else {
  14151. cd->force_pip2_seq = 0;
  14152. pt_debug(dev, DL_INFO,
  14153. "%s: Clear Forced PIP2 Seq\n", __func__);
  14154. }
  14155. mutex_unlock(&(cd->system_lock));
  14156. break;
  14157. case PT_DRV_DBG_BL_WITH_NO_INT: /* 215 */
  14158. mutex_lock(&cd->system_lock);
  14159. if (input_data[1] == 0) {
  14160. cd->bl_with_no_int = 0;
  14161. pt_debug(dev, DL_INFO, "%s: BL using IRQ\n", __func__);
  14162. } else if (input_data[1] == 1) {
  14163. cd->bl_with_no_int = 1;
  14164. pt_debug(dev, DL_INFO, "%s: BL using Polling\n",
  14165. __func__);
  14166. } else {
  14167. rc = -EINVAL;
  14168. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  14169. __func__, input_data[1]);
  14170. }
  14171. mutex_unlock(&(cd->system_lock));
  14172. break;
  14173. case PT_DRV_DBG_CAL_CACHE_IN_HOST: /* 216 */
  14174. mutex_lock(&cd->system_lock);
  14175. if (input_data[1] == 0) {
  14176. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  14177. pt_debug(dev, DL_INFO,
  14178. "%s: Disable Calibration cache in host\n",
  14179. __func__);
  14180. } else if (input_data[1] == 1) {
  14181. cd->cal_cache_in_host = PT_FEATURE_ENABLE;
  14182. pt_debug(dev, DL_INFO,
  14183. "%s: Enable Calibration cache in host\n",
  14184. __func__);
  14185. } else {
  14186. rc = -EINVAL;
  14187. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  14188. __func__, input_data[1]);
  14189. }
  14190. mutex_unlock(&(cd->system_lock));
  14191. break;
  14192. case PT_DRV_DBG_NUM_DEVICES: /* 217 */
  14193. mutex_lock(&cd->system_lock);
  14194. if (input_data[1] == 0) {
  14195. /* 0 is not supported */
  14196. rc = -EINVAL;
  14197. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  14198. __func__, input_data[1]);
  14199. } else if (input_data[1] == 1) {
  14200. cd->num_devices = input_data[1];
  14201. cd->ttdl_bist_select = 0x07;
  14202. pt_debug(dev, DL_INFO,
  14203. "%s: Multi-chip support Disabled\n", __func__);
  14204. } else if (input_data[1] > 1 &&
  14205. input_data[1] <= PT_MAX_DEVICES) {
  14206. cd->num_devices = input_data[1];
  14207. cd->ttdl_bist_select = 0x3F;
  14208. pt_debug(dev, DL_INFO,
  14209. "%s: Multi-chip support Enabled with %d DUTs\n",
  14210. __func__, cd->num_devices);
  14211. } else {
  14212. rc = -EINVAL;
  14213. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  14214. __func__, input_data[1]);
  14215. }
  14216. mutex_unlock(&(cd->system_lock));
  14217. break;
  14218. case PT_DRV_DBG_SET_PANEL_ID_TYPE: /* 218 */
  14219. mutex_lock(&cd->system_lock);
  14220. if (input_data[1] <= 0x07) {
  14221. cd->panel_id_support = input_data[1];
  14222. pt_debug(dev, DL_INFO,
  14223. "%s: ATM - Set panel_id_support to %d\n",
  14224. __func__, cd->panel_id_support);
  14225. } else {
  14226. rc = -EINVAL;
  14227. pt_debug(dev, DL_ERROR,
  14228. "%s: ATM - Invalid parameter: %d\n",
  14229. __func__, input_data[1]);
  14230. }
  14231. mutex_unlock(&(cd->system_lock));
  14232. break;
  14233. case PT_DRV_DBG_PIP_TIMEOUT: /* 219 */
  14234. mutex_lock(&cd->system_lock);
  14235. if (input_data[1] >= 100 && input_data[1] <= 7000) {
  14236. /*
  14237. * The timeout is changed for some cases so the
  14238. * pip_cmd_timeout_default is used to retore back to
  14239. * what the user requested as the new timeout.
  14240. */
  14241. cd->pip_cmd_timeout_default = input_data[1];
  14242. cd->pip_cmd_timeout = input_data[1];
  14243. pt_debug(dev, DL_INFO,
  14244. "%s: ATM - PIP Timeout = %d\n", __func__,
  14245. cd->pip_cmd_timeout_default);
  14246. } else {
  14247. rc = -EINVAL;
  14248. pt_debug(dev, DL_ERROR,
  14249. "%s: ATM - Invalid parameter: %d\n",
  14250. __func__, input_data[1]);
  14251. }
  14252. mutex_unlock(&(cd->system_lock));
  14253. break;
  14254. case PT_DRV_DBG_CORE_PLATFORM_FLAG: /* 220 */
  14255. mutex_lock(&cd->system_lock);
  14256. if (cd->cpdata) {
  14257. cd->cpdata->flags = input_data[1];
  14258. pt_debug(dev, DL_INFO,
  14259. "%s: ATM - Set core platform flag to 0x%02X\n",
  14260. __func__, input_data[1]);
  14261. } else {
  14262. rc = -EINVAL;
  14263. pt_debug(dev, DL_ERROR, "%s: No platform data\n",
  14264. __func__);
  14265. }
  14266. mutex_unlock(&cd->system_lock);
  14267. break;
  14268. #ifdef TTDL_PTVIRTDUT_SUPPORT
  14269. case PT_DRV_DBG_SET_HW_DETECT: /* 298 */
  14270. mutex_lock(&cd->system_lock);
  14271. if (input_data[1])
  14272. cd->hw_detect_enabled = true;
  14273. else
  14274. cd->hw_detect_enabled = false;
  14275. pt_debug(dev, DL_INFO,
  14276. "%s: Set hw_detect_enabled to %d\n",
  14277. __func__, cd->hw_detect_enabled);
  14278. mutex_unlock(&(cd->system_lock));
  14279. break;
  14280. case PT_DRV_DBG_VIRTUAL_I2C_DUT: /* 299 */
  14281. if (input_data[1] == 0) {
  14282. /* Cancel all work threads when disabling */
  14283. pt_debug(dev, DL_WARN, "%s: Canceling Work", __func__);
  14284. #ifdef PT_PTSBC_SUPPORT
  14285. cancel_work_sync(&cd->irq_work);
  14286. cancel_work_sync(&cd->probe_work);
  14287. #endif
  14288. cancel_work_sync(&cd->ttdl_restart_work);
  14289. cancel_work_sync(&cd->enum_work);
  14290. pt_stop_wd_timer(cd);
  14291. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  14292. mutex_lock(&cd->system_lock);
  14293. cd->route_bus_virt_dut = 0;
  14294. mutex_unlock(&(cd->system_lock));
  14295. pt_debug(dev, DL_WARN, "%s: Enable Virtual DUT mode\n",
  14296. __func__);
  14297. } else if (input_data[1] == 1) {
  14298. mutex_lock(&cd->system_lock);
  14299. cd->route_bus_virt_dut = 1;
  14300. mutex_unlock(&(cd->system_lock));
  14301. pt_debug(dev, DL_WARN, "%s: Enable Virtual DUT mode\n",
  14302. __func__);
  14303. } else {
  14304. rc = -EINVAL;
  14305. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  14306. __func__, input_data[1]);
  14307. }
  14308. break;
  14309. #endif /* TTDL_PTVIRTDUT_SUPPORT */
  14310. #endif /* TTDL_DIAGNOSTICS */
  14311. default:
  14312. rc = -EINVAL;
  14313. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  14314. }
  14315. /* Enable watchdog timer */
  14316. if (!wd_disabled)
  14317. pt_start_wd_timer(cd);
  14318. pt_drv_debug_store_exit:
  14319. if (rc)
  14320. return rc;
  14321. return size;
  14322. }
  14323. static DEVICE_ATTR(drv_debug, 0644, pt_drv_debug_show,
  14324. pt_drv_debug_store);
  14325. /*******************************************************************************
  14326. * FUNCTION: pt_sleep_status_show
  14327. *
  14328. * SUMMARY: Show method for the sleep_status sysfs node that will show the
  14329. * sleep status as either ON or OFF
  14330. *
  14331. * RETURN:
  14332. * 0 = success
  14333. * !0 = failure
  14334. *
  14335. * PARAMETERS:
  14336. * *dev - pointer to device structure
  14337. * *attr - pointer to device attributes
  14338. * *buf - pointer to output buffer
  14339. ******************************************************************************/
  14340. static ssize_t pt_sleep_status_show(struct device *dev,
  14341. struct device_attribute *attr, char *buf)
  14342. {
  14343. struct pt_core_data *cd = dev_get_drvdata(dev);
  14344. ssize_t ret;
  14345. mutex_lock(&cd->system_lock);
  14346. if (cd->sleep_state == SS_SLEEP_ON)
  14347. ret = snprintf(buf, PT_MAX_PRBUF_SIZE, "off\n");
  14348. else
  14349. ret = snprintf(buf, PT_MAX_PRBUF_SIZE, "on\n");
  14350. mutex_unlock(&cd->system_lock);
  14351. return ret;
  14352. }
  14353. /*******************************************************************************
  14354. * FUNCTION: pt_panel_id_show
  14355. *
  14356. * SUMMARY: Show method for the panel_id sysfs node that will show the
  14357. * detected panel ID from the DUT
  14358. *
  14359. * RETURN:
  14360. * 0 = success
  14361. * !0 = failure
  14362. *
  14363. * PARAMETERS:
  14364. * *dev - pointer to device structure
  14365. * *attr - pointer to device attributes
  14366. * *buf - pointer to output buffer
  14367. ******************************************************************************/
  14368. static ssize_t pt_panel_id_show(struct device *dev,
  14369. struct device_attribute *attr, char *buf)
  14370. {
  14371. struct pt_core_data *cd = dev_get_drvdata(dev);
  14372. ssize_t ret;
  14373. u8 pid = PANEL_ID_NOT_ENABLED;
  14374. int rc = 0;
  14375. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  14376. /*
  14377. * The DUT should report the same panel ID from both the BL and
  14378. * the FW unless the panel_id feature is set to only
  14379. * PT_PANEL_ID_BY_SYS_INFO, in which case the BL is not able
  14380. * to retrieve the panel_id.
  14381. */
  14382. if (cd->mode == PT_MODE_BOOTLOADER) {
  14383. /*
  14384. * Return the stored value if PT_PANEL_ID_BY_BL
  14385. * is not supported while other feature exits.
  14386. */
  14387. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  14388. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  14389. if (rc) {
  14390. pt_debug(dev, DL_WARN, "%s: %s %s\n",
  14391. "Failed to retrieve Panel ID. ",
  14392. "Using cached value\n",
  14393. __func__);
  14394. }
  14395. }
  14396. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  14397. if (cd->panel_id_support &
  14398. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  14399. /* For all systems sysinfo has the panel_id */
  14400. rc = pt_hid_output_get_sysinfo(cd);
  14401. if (!rc)
  14402. pid =
  14403. cd->sysinfo.sensing_conf_data.panel_id;
  14404. pt_debug(dev, DL_WARN,
  14405. "%s: Gen6 FW mode rc=%d PID=0x%02X\n",
  14406. __func__, rc, pid);
  14407. }
  14408. } else {
  14409. pt_debug(dev, DL_WARN, "%s: Active mode unknown\n",
  14410. __func__);
  14411. rc = -EPERM;
  14412. }
  14413. } else if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  14414. if (cd->mode == PT_MODE_BOOTLOADER) {
  14415. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  14416. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  14417. if (rc) {
  14418. pt_debug(dev, DL_ERROR,
  14419. "%s: BL get panel ID failed rc=%d\n",
  14420. __func__, rc);
  14421. }
  14422. }
  14423. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  14424. if (cd->panel_id_support &
  14425. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  14426. rc = pt_hid_output_get_sysinfo(cd);
  14427. if (!rc)
  14428. pid =
  14429. cd->sysinfo.sensing_conf_data.panel_id;
  14430. pt_debug(dev, DL_WARN,
  14431. "%s: TT/TC FW mode rc=%d PID=0x%02X\n",
  14432. __func__, rc, pid);
  14433. }
  14434. } else {
  14435. pt_debug(dev, DL_WARN, "%s: Active mode unknown\n",
  14436. __func__);
  14437. rc = -EPERM;
  14438. }
  14439. } else {
  14440. pt_debug(dev, DL_WARN, "%s: Dut generation is unknown\n",
  14441. __func__);
  14442. rc = -EPERM;
  14443. }
  14444. ret = snprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n0x%02X\n",
  14445. rc, pid);
  14446. return ret;
  14447. }
  14448. /*******************************************************************************
  14449. * FUNCTION: pt_get_param_store
  14450. *
  14451. * SUMMARY: Store method for the get_param sysfs node. Stores what parameter
  14452. * ID to retrieve with the show method.
  14453. *
  14454. * NOTE: This sysfs node is only available after a DUT has been enumerated
  14455. *
  14456. * RETURN: Size of passed in buffer if successful
  14457. *
  14458. * PARAMETERS:
  14459. * *dev - pointer to device structure
  14460. * *attr - pointer to device attributes
  14461. * *buf - pointer to output buffer
  14462. * size - size of buf
  14463. ******************************************************************************/
  14464. static ssize_t pt_get_param_store(struct device *dev,
  14465. struct device_attribute *attr, const char *buf, size_t size)
  14466. {
  14467. struct pt_core_data *cd = dev_get_drvdata(dev);
  14468. u32 input_data[2];
  14469. int length;
  14470. int rc = 0;
  14471. /* Maximum input of one value */
  14472. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14473. ARRAY_SIZE(input_data));
  14474. if (length != 1) {
  14475. pt_debug(dev, DL_WARN, "%s: Invalid number of arguments\n",
  14476. __func__);
  14477. rc = -EINVAL;
  14478. goto exit;
  14479. }
  14480. mutex_lock(&cd->system_lock);
  14481. cd->get_param_id = input_data[0];
  14482. mutex_unlock(&(cd->system_lock));
  14483. exit:
  14484. if (rc)
  14485. return rc;
  14486. return size;
  14487. }
  14488. /*******************************************************************************
  14489. * FUNCTION: pt_get_param_show
  14490. *
  14491. * SUMMARY: Show method for the get_param sysfs node. Retrieves the
  14492. * parameter data from the DUT based on the ID stored in the core
  14493. * data variable "get_param_id". If the ID is invalid, the DUT cannot
  14494. * communicate or some other error occures, an error status is returned
  14495. * with no value following.
  14496. * Output is in the form:
  14497. * Status: x
  14498. * 0xyyyyyyyy
  14499. * The 32bit data will only follow the status code if the status == 0
  14500. *
  14501. * NOTE: This sysfs node is only available after a DUT has been enumerated
  14502. *
  14503. * RETURN:
  14504. * 0 = success
  14505. * !0 = failure
  14506. *
  14507. * PARAMETERS:
  14508. * *dev - pointer to device structure
  14509. * *attr - pointer to device attributes
  14510. * *buf - pointer to output buffer
  14511. ******************************************************************************/
  14512. static ssize_t pt_get_param_show(struct device *dev,
  14513. struct device_attribute *attr, char *buf)
  14514. {
  14515. struct pt_core_data *cd = dev_get_drvdata(dev);
  14516. ssize_t ret = 0;
  14517. int status;
  14518. u32 value = 0;
  14519. status = pt_pip_get_param(cd, cd->get_param_id, &value);
  14520. if (status) {
  14521. pt_debug(dev, DL_ERROR, "%s: %s Failed, status = %d\n",
  14522. __func__, "pt_get_param", status);
  14523. ret = scnprintf(buf, strlen(buf),
  14524. "%s %d\n",
  14525. "Status:", status);
  14526. } else {
  14527. pt_debug(dev, DL_DEBUG, "%s: Param [%d] = 0x%04X\n",
  14528. __func__, cd->get_param_id, value);
  14529. ret = scnprintf(buf, strlen(buf),
  14530. "Status: %d\n"
  14531. "0x%04X\n",
  14532. status, value);
  14533. }
  14534. return ret;
  14535. }
  14536. /*******************************************************************************
  14537. * FUNCTION: pt_ttdl_restart_show
  14538. *
  14539. * SUMMARY: Show method for ttdl_restart sysfs node. This node releases all
  14540. * probed modules, calls startup() and then re-probes modules.
  14541. *
  14542. * RETURN: size of data written to sysfs node
  14543. *
  14544. * PARAMETERS:
  14545. * *dev - pointer to device structure
  14546. * *attr - pointer to device attributes structure
  14547. * *buf - pointer to print output buffer
  14548. ******************************************************************************/
  14549. static ssize_t pt_ttdl_restart_show(struct device *dev,
  14550. struct device_attribute *attr, char *buf)
  14551. {
  14552. struct pt_core_data *cd = dev_get_drvdata(dev);
  14553. int t;
  14554. int rc = 0;
  14555. mutex_lock(&cd->system_lock);
  14556. cd->startup_state = STARTUP_NONE;
  14557. mutex_unlock(&(cd->system_lock));
  14558. /* ensure no left over exclusive access is still locked */
  14559. release_exclusive(cd, cd->dev);
  14560. pt_queue_restart(cd);
  14561. t = wait_event_timeout(cd->wait_q,
  14562. (cd->startup_status >= STARTUP_STATUS_COMPLETE),
  14563. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  14564. if (IS_TMO(t)) {
  14565. pt_debug(cd->dev, DL_ERROR,
  14566. "%s: TMO waiting for FW sentinel\n", __func__);
  14567. rc = -ETIME;
  14568. }
  14569. return scnprintf(buf, strlen(buf),
  14570. "Status: %d\n"
  14571. "Enum Status: 0x%04X\n", rc, cd->startup_status);
  14572. }
  14573. static DEVICE_ATTR(ttdl_restart, 0444, pt_ttdl_restart_show, NULL);
  14574. /*******************************************************************************
  14575. * FUNCTION: pt_pip2_gpio_read_show
  14576. *
  14577. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and prints the
  14578. * contents of the response to the passed in output buffer.
  14579. *
  14580. * RETURN: size of data written to sysfs node
  14581. *
  14582. * PARAMETERS:
  14583. * *dev - pointer to device structure
  14584. * *attr - pointer to device attributes structure
  14585. * *buf - pointer to print output buffer
  14586. ******************************************************************************/
  14587. static ssize_t pt_pip2_gpio_read_show(struct device *dev,
  14588. struct device_attribute *attr, char *buf)
  14589. {
  14590. struct pt_core_data *cd = dev_get_drvdata(dev);
  14591. u8 status = 0;
  14592. u32 gpio_value = 0;
  14593. int rc = 0;
  14594. /* This functionality is only available in the BL */
  14595. if (cd->mode == PT_MODE_BOOTLOADER)
  14596. rc = pt_pip2_read_gpio(dev, &status, &gpio_value);
  14597. else
  14598. rc = -EPERM;
  14599. if (!rc) {
  14600. if (status == 0)
  14601. return snprintf(buf, PT_MAX_PRBUF_SIZE,
  14602. "Status: %d\n"
  14603. "DUT GPIO Reg: 0x%08X\n",
  14604. rc, gpio_value);
  14605. else
  14606. return snprintf(buf, PT_MAX_PRBUF_SIZE,
  14607. "Status: %d\n"
  14608. "DUT GPIO Reg: n/a\n",
  14609. status);
  14610. } else
  14611. return snprintf(buf, PT_MAX_PRBUF_SIZE,
  14612. "Status: %d\n"
  14613. "DUT GPIO Reg: n/a\n",
  14614. rc);
  14615. }
  14616. /*******************************************************************************
  14617. * FUNCTION: pt_pip2_version_show
  14618. *
  14619. * SUMMARY: Sends a PIP2 VERSION command to the DUT and prints the
  14620. * contents of the response to the passed in output buffer.
  14621. *
  14622. * RETURN: size of data written to sysfs node
  14623. *
  14624. * PARAMETERS:
  14625. * *dev - pointer to device structure
  14626. * *attr - pointer to device attributes structure
  14627. * *buf - pointer to print output buffer
  14628. ******************************************************************************/
  14629. static ssize_t pt_pip2_version_show(struct device *dev,
  14630. struct device_attribute *attr, char *buf)
  14631. {
  14632. int rc = 0;
  14633. struct pt_core_data *cd = dev_get_drvdata(dev);
  14634. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  14635. rc = pt_pip2_get_version(cd);
  14636. if (!rc) {
  14637. return snprintf(buf, PT_MAX_PRBUF_SIZE,
  14638. "PIP VERSION : %02X.%02X\n"
  14639. "BL VERSION : %02X.%02X\n"
  14640. "FW VERSION : %02X.%02X\n"
  14641. "SILICON ID : %04X.%04X\n"
  14642. "UID : 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
  14643. ttdata->pip_ver_major, ttdata->pip_ver_minor,
  14644. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  14645. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  14646. ttdata->chip_id, ttdata->chip_rev,
  14647. ttdata->uid[0], ttdata->uid[1],
  14648. ttdata->uid[2], ttdata->uid[3],
  14649. ttdata->uid[4], ttdata->uid[5],
  14650. ttdata->uid[6], ttdata->uid[7],
  14651. ttdata->uid[8], ttdata->uid[9],
  14652. ttdata->uid[10], ttdata->uid[11]);
  14653. } else {
  14654. pt_debug(dev, DL_ERROR,
  14655. "%s: Failed to retriev PIP2 VERSION data\n", __func__);
  14656. return snprintf(buf, PT_MAX_PRBUF_SIZE,
  14657. "PIP VERSION : -\n"
  14658. "BL VERSION : -\n"
  14659. "FW VERSION : -\n"
  14660. "SILICON ID : -\n"
  14661. "UID : -\n");
  14662. }
  14663. }
  14664. #ifdef TTDL_DIAGNOSTICS
  14665. /*******************************************************************************
  14666. * FUNCTION: pt_ttdl_status_show
  14667. *
  14668. * SUMMARY: Show method for the ttdl_status sysfs node. Displays TTDL internal
  14669. * variable states and GPIO levels. Additional information printed when
  14670. * TTDL_DIAGNOSTICS is enabled.
  14671. *
  14672. * NOTE: All counters will be reset to 0 when this function is called.
  14673. *
  14674. * RETURN:
  14675. * 0 = success
  14676. * !0 = failure
  14677. *
  14678. * PARAMETERS:
  14679. * *dev - pointer to device structure
  14680. * *attr - pointer to device attributes
  14681. * *buf - pointer to output buffer
  14682. ******************************************************************************/
  14683. static ssize_t pt_ttdl_status_show(struct device *dev,
  14684. struct device_attribute *attr, char *buf)
  14685. {
  14686. struct pt_core_data *cd = dev_get_drvdata(dev);
  14687. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14688. struct i2c_client *client = to_i2c_client(dev);
  14689. ssize_t ret;
  14690. u16 cal_size = 0;
  14691. unsigned short crc = 0;
  14692. if (cd->cal_cache_in_host)
  14693. _pt_manage_local_cal_data(dev,
  14694. PT_CAL_DATA_INFO, &cal_size, &crc);
  14695. ret = scnprintf(buf, strlen(buf),
  14696. "%s: 0x%04X\n"
  14697. "%s: %d\n"
  14698. "%s: %s\n"
  14699. "%s: 0x%02X\n"
  14700. "%s: %s\n"
  14701. "%s: %s\n"
  14702. "%s: %d\n"
  14703. "%s: %s %s\n"
  14704. "%s: %s\n"
  14705. "%s: %s\n"
  14706. "%s: %s\n"
  14707. "%s: %s\n"
  14708. "%s: %s\n"
  14709. "%s: %s\n"
  14710. "%s: %d\n"
  14711. "%s: %s\n"
  14712. "%s: %s\n"
  14713. "%s: %d\n"
  14714. #ifdef TTDL_DIAGNOSTICS
  14715. "%s: %d\n"
  14716. "%s: %d\n"
  14717. "%s: %d\n"
  14718. "%s: %d\n"
  14719. "%s: %d\n"
  14720. "%s: %d\n"
  14721. "%s: %d\n"
  14722. "%s: %d\n"
  14723. "%s: %d\n"
  14724. "%s: %d\n"
  14725. "%s: %d\n"
  14726. "%s: %s\n"
  14727. "%s: %d\n"
  14728. "%s: 0x%04X\n"
  14729. #endif /* TTDL_DIAGNOSTICS */
  14730. ,
  14731. "Startup Status ", cd->startup_status,
  14732. "TTDL Debug Level ", cd->debug_level,
  14733. "Active Bus Module ",
  14734. cd->bus_ops->bustype == BUS_I2C ? "I2C" : "SPI",
  14735. "I2C Address ",
  14736. cd->bus_ops->bustype == BUS_I2C ? client->addr : 0,
  14737. "Exclusive Access Lock ", cd->exclusive_dev ? "Set":"Free",
  14738. "HW Detected ",
  14739. cd->hw_detected ? "True" : "False",
  14740. "Number of Devices ", cd->num_devices,
  14741. "DUT Generation ",
  14742. cd->active_dut_generation ?
  14743. (cd->active_dut_generation == DUT_PIP2_CAPABLE ?
  14744. "PT TC/TT" : "Gen5/6") : "Unknown",
  14745. cd->active_dut_generation ?
  14746. (cd->set_dut_generation == true ?
  14747. "(Set)" : "(Detected)") : "",
  14748. "Protocol Mode ",
  14749. cd->dut_status.protocol_mode == PT_PROTOCOL_MODE_HID ?
  14750. "Hybrid HID" : "PIP",
  14751. "Mode ",
  14752. cd->mode ? (cd->mode == PT_MODE_OPERATIONAL ?
  14753. "Operational" : "BL") : "Unknown",
  14754. "Flashless Mode ",
  14755. cd->flashless_dut == 1 ? "Yes" : "No",
  14756. "Suppress No-Flash Auto BL ",
  14757. cd->flashless_auto_bl == PT_SUPPRESS_AUTO_BL ?
  14758. "Yes" : "No",
  14759. "GPIO state - IRQ ",
  14760. cd->cpdata->irq_stat ?
  14761. (cd->cpdata->irq_stat(cd->cpdata, dev) ?
  14762. "High" : "Low") : "not defined",
  14763. "GPIO state - TP_XRES ",
  14764. pdata->core_pdata->rst_gpio ?
  14765. (gpio_get_value(pdata->core_pdata->rst_gpio) ?
  14766. "High" : "Low") : "not defined",
  14767. "Error GPIO trigger type ", cd->err_gpio_type,
  14768. "WD - Manual Force Stop ",
  14769. cd->watchdog_force_stop ? "True" : "False",
  14770. "WD - Enabled ",
  14771. cd->watchdog_enabled ? "True" : "False",
  14772. "WD - Interval (ms) ", cd->watchdog_interval
  14773. #ifdef TTDL_DIAGNOSTICS
  14774. ,
  14775. "WD - Triggered Count ", cd->watchdog_count,
  14776. "WD - IRQ Stuck low count ", cd->watchdog_irq_stuck_count,
  14777. "WD - Device Access Errors ", cd->watchdog_failed_access_count,
  14778. "WD - XRES Count ", cd->wd_xres_count,
  14779. "Startup Retry Count ", cd->startup_retry_count,
  14780. "IRQ Triggered Count ", cd->irq_count,
  14781. "BL Packet Retry Count ", cd->bl_retry_packet_count,
  14782. "PIP2 CRC Error Count ", cd->pip2_crc_error_count,
  14783. "Bus Transmit Error Count ", cd->bus_transmit_error_count,
  14784. "File Erase Timeout Count ", cd->file_erase_timeout_count,
  14785. "RAM Parm Restore Count ", pt_count_parameter_list(cd),
  14786. "Calibration Cache on host ",
  14787. cd->cal_cache_in_host == PT_FEATURE_ENABLE ?
  14788. "Yes" : "No",
  14789. "Calibration Cache size ", cal_size,
  14790. "Calibration Cache chip ID ", crc
  14791. #endif /* TTDL_DIAGNOSTICS */
  14792. );
  14793. #ifdef TTDL_DIAGNOSTICS
  14794. /* Reset all diagnostic counters */
  14795. cd->watchdog_count = 0;
  14796. cd->watchdog_irq_stuck_count = 0;
  14797. cd->watchdog_failed_access_count = 0;
  14798. cd->wd_xres_count = 0;
  14799. cd->irq_count = 0;
  14800. cd->bl_retry_packet_count = 0;
  14801. cd->pip2_crc_error_count = 0;
  14802. cd->bus_transmit_error_count = 0;
  14803. #endif
  14804. return ret;
  14805. }
  14806. static DEVICE_ATTR(ttdl_status, 0444, pt_ttdl_status_show, NULL);
  14807. /*******************************************************************************
  14808. * FUNCTION: pt_pip2_enter_bl_show
  14809. *
  14810. * SUMMARY: Show method for the pip2_enter_bl sysfs node that will force
  14811. * the DUT into the BL and show the success or failure of entering the BL
  14812. *
  14813. * RETURN:
  14814. * 0 = success
  14815. * !0 = failure
  14816. *
  14817. * PARAMETERS:
  14818. * *dev - pointer to device structure
  14819. * *attr - pointer to device attributes
  14820. * *buf - pointer to output buffer
  14821. ******************************************************************************/
  14822. static ssize_t pt_pip2_enter_bl_show(struct device *dev,
  14823. struct device_attribute *attr, char *buf)
  14824. {
  14825. ssize_t ret = 0;
  14826. int rc = 0;
  14827. int result = 0;
  14828. u8 mode = PT_MODE_UNKNOWN;
  14829. struct pt_core_data *cd = dev_get_drvdata(dev);
  14830. bool current_bridge_mode = cd->bridge_mode;
  14831. /* Turn off the TTDL WD before enter bootloader */
  14832. pt_stop_wd_timer(cd);
  14833. /* Ensure NO enumeration work is queued or will be queued */
  14834. cancel_work_sync(&cd->enum_work);
  14835. mutex_lock(&cd->system_lock);
  14836. cd->bridge_mode = true;
  14837. mutex_unlock(&cd->system_lock);
  14838. /* set mode to operational to avoid any extra PIP traffic */
  14839. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14840. switch (result) {
  14841. case PT_ENTER_BL_PASS:
  14842. ret = snprintf(buf, PT_MAX_PRBUF_SIZE,
  14843. "Status: %d\nEntered BL\n", PT_ENTER_BL_PASS);
  14844. break;
  14845. case PT_ENTER_BL_ERROR:
  14846. ret = snprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  14847. rc,
  14848. " Unknown Error");
  14849. break;
  14850. case PT_ENTER_BL_RESET_FAIL:
  14851. ret = snprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  14852. rc,
  14853. " Soft Reset Failed");
  14854. break;
  14855. case PT_ENTER_BL_HID_START_BL_FAIL:
  14856. ret = snprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  14857. rc,
  14858. " PIP Start BL Cmd Failed");
  14859. break;
  14860. case PT_ENTER_BL_CONFIRM_FAIL:
  14861. ret = snprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  14862. rc,
  14863. " Error confirming DUT entered BL");
  14864. break;
  14865. default:
  14866. ret = snprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  14867. rc, " Unknown Error");
  14868. break;
  14869. };
  14870. /* Restore state of allowing enumeration work to be queued again */
  14871. mutex_lock(&cd->system_lock);
  14872. cd->bridge_mode = current_bridge_mode;
  14873. mutex_unlock(&cd->system_lock);
  14874. return ret;
  14875. }
  14876. static DEVICE_ATTR(pip2_enter_bl, 0444, pt_pip2_enter_bl_show, NULL);
  14877. #define PT_STATUS_STR_LEN (50)
  14878. /*******************************************************************************
  14879. * FUNCTION: pt_pip2_exit_bl_show
  14880. *
  14881. * SUMMARY: Show method for the pip2_exit_bl sysfs node that will attempt to
  14882. * launch the APP and put the DUT Application mode
  14883. *
  14884. * RETURN:
  14885. * 0 = success
  14886. * !0 = failure
  14887. *
  14888. * PARAMETERS:
  14889. * *dev - pointer to device structure
  14890. * *attr - pointer to device attributes
  14891. * *buf - pointer to output buffer
  14892. ******************************************************************************/
  14893. static ssize_t pt_pip2_exit_bl_show(struct device *dev,
  14894. struct device_attribute *attr, char *buf)
  14895. {
  14896. struct pt_core_data *cd = dev_get_drvdata(dev);
  14897. ssize_t ret = 0;
  14898. int rc = 0;
  14899. u8 status_str[PT_STATUS_STR_LEN];
  14900. rc = pt_pip2_exit_bl_(cd, status_str, PT_STATUS_STR_LEN);
  14901. /*
  14902. * Perform enum if startup_status doesn't reach to
  14903. * STARTUP_STATUS_FW_OUT_OF_BOOT.
  14904. */
  14905. if (!rc && (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT))) {
  14906. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  14907. if (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  14908. strlcpy(status_str,
  14909. "Already in APP mode - FW stuck in Boot mode", sizeof(status_str));
  14910. }
  14911. }
  14912. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  14913. rc, status_str);
  14914. return ret;
  14915. }
  14916. static DEVICE_ATTR(pip2_exit_bl, 0444, pt_pip2_exit_bl_show, NULL);
  14917. #endif
  14918. #ifdef EASYWAKE_TSG6
  14919. /*******************************************************************************
  14920. * FUNCTION: pt_easy_wakeup_gesture_show
  14921. *
  14922. * SUMMARY: Show method for the easy_wakeup_gesture sysfs node that will show
  14923. * current easy wakeup gesture
  14924. *
  14925. * RETURN:
  14926. * 0 = success
  14927. * !0 = failure
  14928. *
  14929. * PARAMETERS:
  14930. * *dev - pointer to device structure
  14931. * *attr - pointer to device attributes
  14932. * *buf - pointer to output buffer
  14933. ******************************************************************************/
  14934. static ssize_t pt_easy_wakeup_gesture_show(struct device *dev,
  14935. struct device_attribute *attr, char *buf)
  14936. {
  14937. struct pt_core_data *cd = dev_get_drvdata(dev);
  14938. ssize_t ret;
  14939. mutex_lock(&cd->system_lock);
  14940. ret = snprintf(buf, PT_MAX_PRBUF_SIZE, "0x%02X\n",
  14941. cd->easy_wakeup_gesture);
  14942. mutex_unlock(&cd->system_lock);
  14943. return ret;
  14944. }
  14945. /*******************************************************************************
  14946. * FUNCTION: pt_easy_wakeup_gesture_store
  14947. *
  14948. * SUMMARY: The store method for the easy_wakeup_gesture sysfs node that
  14949. * allows the wake gesture to be set to a custom value.
  14950. *
  14951. * RETURN: Size of passed in buffer is success
  14952. *
  14953. * PARAMETERS:
  14954. * *dev - pointer to device structure
  14955. * *attr - pointer to device attributes
  14956. * *buf - pointer to buffer that hold the command parameters
  14957. * size - size of buf
  14958. ******************************************************************************/
  14959. static ssize_t pt_easy_wakeup_gesture_store(struct device *dev,
  14960. struct device_attribute *attr, const char *buf, size_t size)
  14961. {
  14962. struct pt_core_data *cd = dev_get_drvdata(dev);
  14963. u32 input_data[2];
  14964. int length;
  14965. int rc = 0;
  14966. /* Maximum input of one value */
  14967. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14968. ARRAY_SIZE(input_data));
  14969. if (length != 1) {
  14970. pt_debug(dev, DL_WARN, "%s: Invalid number of arguments\n",
  14971. __func__);
  14972. rc = -EINVAL;
  14973. goto exit;
  14974. }
  14975. pt_debug(dev, DL_INFO, "%s: features.easywake = 0x%02X\n",
  14976. __func__, cd->features.easywake);
  14977. if (!cd->features.easywake || input_data[0] > 0xFF) {
  14978. rc = -EINVAL;
  14979. goto exit;
  14980. }
  14981. pm_runtime_get_sync(dev);
  14982. mutex_lock(&cd->system_lock);
  14983. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 2)) {
  14984. cd->easy_wakeup_gesture = (u8)input_data[0];
  14985. pt_debug(dev, DL_INFO,
  14986. "%s: Updated easy_wakeup_gesture = 0x%02X\n",
  14987. __func__, cd->easy_wakeup_gesture);
  14988. } else
  14989. rc = -ENODEV;
  14990. mutex_unlock(&cd->system_lock);
  14991. pm_runtime_put(dev);
  14992. exit:
  14993. if (rc)
  14994. return rc;
  14995. return size;
  14996. }
  14997. /*******************************************************************************
  14998. * FUNCTION: pt_easy_wakeup_gesture_id_show
  14999. *
  15000. * SUMMARY: Show method for the easy_wakeup_gesture_id sysfs node that will
  15001. * show the TSG6 easywake gesture ID
  15002. *
  15003. * RETURN:
  15004. * 0 = success
  15005. * !0 = failure
  15006. *
  15007. * PARAMETERS:
  15008. * *dev - pointer to device structure
  15009. * *attr - pointer to device attributes
  15010. * *buf - pointer to output buffer
  15011. ******************************************************************************/
  15012. static ssize_t pt_easy_wakeup_gesture_id_show(struct device *dev,
  15013. struct device_attribute *attr, char *buf)
  15014. {
  15015. struct pt_core_data *cd = dev_get_drvdata(dev);
  15016. ssize_t ret;
  15017. mutex_lock(&cd->system_lock);
  15018. ret = snprintf(buf, PT_MAX_PRBUF_SIZE, "Status: 0\n0x%02X\n",
  15019. cd->gesture_id);
  15020. mutex_unlock(&cd->system_lock);
  15021. return ret;
  15022. }
  15023. /*******************************************************************************
  15024. * FUNCTION: pt_easy_wakeup_gesture_data_show
  15025. *
  15026. * SUMMARY: Show method for the easy_wakeup_gesture_data sysfs node that will
  15027. * show the TSG6 easywake gesture data in the following format:
  15028. * x1(LSB),x1(MSB), y1(LSB),y1(MSB), x2(LSB),x2(MSB), y2(LSB),y2(MSB),...
  15029. *
  15030. * RETURN:
  15031. * 0 = success
  15032. * !0 = failure
  15033. *
  15034. * PARAMETERS:
  15035. * *dev - pointer to device structure
  15036. * *attr - pointer to device attributes
  15037. * *buf - pointer to output buffer
  15038. ******************************************************************************/
  15039. static ssize_t pt_easy_wakeup_gesture_data_show(struct device *dev,
  15040. struct device_attribute *attr, char *buf)
  15041. {
  15042. struct pt_core_data *cd = dev_get_drvdata(dev);
  15043. ssize_t ret = 0;
  15044. int i;
  15045. mutex_lock(&cd->system_lock);
  15046. ret += snprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret, "Status: %d\n", 0);
  15047. for (i = 0; i < cd->gesture_data_length; i++)
  15048. ret += snprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  15049. "0x%02X\n", cd->gesture_data[i]);
  15050. ret += snprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  15051. "(%d bytes)\n", cd->gesture_data_length);
  15052. mutex_unlock(&cd->system_lock);
  15053. return ret;
  15054. }
  15055. #endif /* EASYWAKE_TSG6 */
  15056. #ifdef TTDL_DIAGNOSTICS
  15057. /*******************************************************************************
  15058. * FUNCTION: pt_err_gpio_show
  15059. *
  15060. * SUMMARY: Show method for the err_gpio sysfs node that will show if
  15061. * setting up the gpio was successful
  15062. *
  15063. * RETURN: Char buffer with printed GPIO creation state
  15064. *
  15065. * PARAMETERS:
  15066. * *dev - pointer to device structure
  15067. * *attr - pointer to device attributes
  15068. * *buf - pointer to output buffer
  15069. ******************************************************************************/
  15070. static ssize_t pt_err_gpio_show(struct device *dev,
  15071. struct device_attribute *attr, char *buf)
  15072. {
  15073. struct pt_core_data *cd = dev_get_drvdata(dev);
  15074. return scnprintf(buf, strlen(buf), "Status: 0\n"
  15075. "Err GPIO (%d) : %s\n"
  15076. "Err GPIO trig type: %d\n",
  15077. cd->err_gpio,
  15078. (cd->err_gpio ? (gpio_get_value(cd->err_gpio) ?
  15079. "HIGH" : "low") : "not defined"),
  15080. cd->err_gpio_type);
  15081. }
  15082. /*******************************************************************************
  15083. * FUNCTION: pt_err_gpio_store
  15084. *
  15085. * SUMMARY: The store method for the err_gpio sysfs node that allows any
  15086. * available host GPIO to be used to trigger when TTDL detects a PIP
  15087. * command/response timeout.
  15088. *
  15089. * RETURN:
  15090. * 0 = success
  15091. * !0 = failure
  15092. *
  15093. * PARAMETERS:
  15094. * *dev - pointer to device structure
  15095. * *attr - pointer to device attributes
  15096. * *buf - pointer to buffer that hold the command parameters
  15097. * size - size of buf
  15098. ******************************************************************************/
  15099. static ssize_t pt_err_gpio_store(struct device *dev,
  15100. struct device_attribute *attr, const char *buf, size_t size)
  15101. {
  15102. struct pt_core_data *cd = dev_get_drvdata(dev);
  15103. unsigned long gpio;
  15104. int rc = 0;
  15105. u32 input_data[3];
  15106. int length;
  15107. u8 err_type;
  15108. input_data[0] = 0;
  15109. input_data[1] = 0;
  15110. /* Maximmum input is two elements */
  15111. length = _pt_ic_parse_input(dev, buf, size,
  15112. input_data, ARRAY_SIZE(input_data));
  15113. if (length < 1 || length > 2) {
  15114. pt_debug(dev, DL_WARN, "%s: Invalid number of arguments\n",
  15115. __func__);
  15116. rc = -EINVAL;
  15117. goto exit;
  15118. }
  15119. gpio = input_data[0];
  15120. err_type = (u8)input_data[1];
  15121. if (err_type < 0 || err_type > PT_ERR_GPIO_MAX_TYPE) {
  15122. rc = -EINVAL;
  15123. goto exit;
  15124. }
  15125. mutex_lock(&cd->system_lock);
  15126. gpio_free(gpio);
  15127. rc = gpio_request(gpio, NULL);
  15128. if (rc) {
  15129. pt_debug(dev, DL_ERROR, "error requesting gpio %lu\n", gpio);
  15130. rc = -ENODEV;
  15131. } else {
  15132. cd->err_gpio = gpio;
  15133. cd->err_gpio_type = err_type;
  15134. gpio_direction_output(gpio, 0);
  15135. }
  15136. mutex_unlock(&cd->system_lock);
  15137. exit:
  15138. if (rc)
  15139. return rc;
  15140. return size;
  15141. }
  15142. static DEVICE_ATTR(err_gpio, 0644, pt_err_gpio_show,
  15143. pt_err_gpio_store);
  15144. /*******************************************************************************
  15145. * FUNCTION: pt_drv_irq_show
  15146. *
  15147. * SUMMARY: Show method for the drv_irq sysfs node that will show if the
  15148. * TTDL interrupt is enabled/disabled
  15149. *
  15150. * RETURN:
  15151. * 0 = success
  15152. * !0 = failure
  15153. *
  15154. * PARAMETERS:
  15155. * *dev - pointer to device structure
  15156. * *attr - pointer to device attributes
  15157. * *buf - pointer to output buffer
  15158. ******************************************************************************/
  15159. static ssize_t pt_drv_irq_show(struct device *dev,
  15160. struct device_attribute *attr, char *buf)
  15161. {
  15162. struct pt_core_data *cd = dev_get_drvdata(dev);
  15163. ssize_t ret = 0;
  15164. mutex_lock(&cd->system_lock);
  15165. ret += snprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n", 0);
  15166. if (cd->irq_enabled)
  15167. ret += snprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  15168. "Driver interrupt: ENABLED\n");
  15169. else
  15170. ret += snprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  15171. "Driver interrupt: DISABLED\n");
  15172. mutex_unlock(&cd->system_lock);
  15173. return ret;
  15174. }
  15175. /*******************************************************************************
  15176. * FUNCTION: pt_drv_irq_store
  15177. *
  15178. * SUMMARY: The store method for the drv_irq sysfs node that allows the TTDL
  15179. * IRQ to be enabled/disabled.
  15180. *
  15181. * RETURN: Size of passed in buffer
  15182. *
  15183. * PARAMETERS:
  15184. * *dev - pointer to device structure
  15185. * *attr - pointer to device attributes
  15186. * *buf - pointer to buffer that hold the command parameters
  15187. * size - size of buf
  15188. ******************************************************************************/
  15189. static ssize_t pt_drv_irq_store(struct device *dev,
  15190. struct device_attribute *attr, const char *buf, size_t size)
  15191. {
  15192. struct pt_core_data *cd = dev_get_drvdata(dev);
  15193. u32 input_data[2];
  15194. int length;
  15195. int rc = 0;
  15196. /* Maximum input of one value */
  15197. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15198. ARRAY_SIZE(input_data));
  15199. if (length != 1) {
  15200. pt_debug(dev, DL_WARN, "%s: Invalid number of arguments\n",
  15201. __func__);
  15202. rc = -EINVAL;
  15203. goto exit;
  15204. }
  15205. mutex_lock(&cd->system_lock);
  15206. switch (input_data[0]) {
  15207. case 0:
  15208. if (cd->irq_enabled) {
  15209. cd->irq_enabled = false;
  15210. /* Disable IRQ has no return value to check */
  15211. disable_irq_nosync(cd->irq);
  15212. pt_debug(dev, DL_INFO,
  15213. "%s: Driver IRQ now disabled\n",
  15214. __func__);
  15215. } else
  15216. pt_debug(dev, DL_INFO,
  15217. "%s: Driver IRQ already disabled\n",
  15218. __func__);
  15219. break;
  15220. case 1:
  15221. if (cd->irq_enabled == false) {
  15222. cd->irq_enabled = true;
  15223. enable_irq(cd->irq);
  15224. pt_debug(dev, DL_INFO,
  15225. "%s: Driver IRQ now enabled\n",
  15226. __func__);
  15227. } else
  15228. pt_debug(dev, DL_INFO,
  15229. "%s: Driver IRQ already enabled\n",
  15230. __func__);
  15231. break;
  15232. default:
  15233. rc = -EINVAL;
  15234. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  15235. }
  15236. mutex_unlock(&(cd->system_lock));
  15237. exit:
  15238. if (rc)
  15239. return rc;
  15240. return size;
  15241. }
  15242. /*******************************************************************************
  15243. * FUNCTION: pt_pip2_bin_hdr_show
  15244. *
  15245. * SUMMARY: Show method for the pip2_bin_hdr sysfs node that will read
  15246. * the bin file header from flash and show each field
  15247. *
  15248. * RETURN:
  15249. * 0 = success
  15250. * !0 = failure
  15251. *
  15252. * PARAMETERS:
  15253. * *dev - pointer to device structure
  15254. * *attr - pointer to device attributes
  15255. * *buf - pointer to output buffer
  15256. ******************************************************************************/
  15257. static ssize_t pt_pip2_bin_hdr_show(struct device *dev,
  15258. struct device_attribute *attr, char *buf)
  15259. {
  15260. ssize_t ret = 0;
  15261. struct pt_bin_file_hdr hdr = {0};
  15262. int rc;
  15263. rc = _pt_request_pip2_bin_hdr(dev, &hdr);
  15264. ret = scnprintf(buf, strlen(buf),
  15265. "%s: %d\n"
  15266. "%s: %d\n"
  15267. "%s: 0x%04X\n"
  15268. "%s: %d\n"
  15269. "%s: %d\n"
  15270. "%s: %d\n"
  15271. "%s: 0x%08X\n"
  15272. "%s: 0x%04X\n"
  15273. "%s: 0x%04X\n"
  15274. "%s: %d\n"
  15275. "%s: %d\n",
  15276. "Status", rc,
  15277. "Header Length ", hdr.length,
  15278. "TTPID ", hdr.ttpid,
  15279. "FW Major Ver ", hdr.fw_major,
  15280. "FW Minor Ver ", hdr.fw_minor,
  15281. "FW Rev Control ", hdr.fw_rev_ctrl,
  15282. "FW CRC ", hdr.fw_crc,
  15283. "Silicon Rev ", hdr.si_rev,
  15284. "Silicon ID ", hdr.si_id,
  15285. "Config Ver ", hdr.config_ver,
  15286. "HEX File Size ", hdr.hex_file_size
  15287. );
  15288. return ret;
  15289. }
  15290. /*******************************************************************************
  15291. * FUNCTION: pt_platform_data_show
  15292. *
  15293. * SUMMARY: Show method for the platform_data sysfs node that will show the
  15294. * active platform data including: GPIOs, Vendor and Product IDs,
  15295. * Virtual Key coordinates, Core/MT/Loader flags, Level trigger delay,
  15296. * HID registers, and Easy wake gesture
  15297. *
  15298. * RETURN: Size of printed data
  15299. *
  15300. * PARAMETERS:
  15301. * *dev - pointer to device structure
  15302. * *attr - pointer to device attributes
  15303. * *buf - pointer to output buffer
  15304. ******************************************************************************/
  15305. static ssize_t pt_platform_data_show(struct device *dev,
  15306. struct device_attribute *attr, char *buf)
  15307. {
  15308. struct pt_platform_data *pdata = dev_get_platdata(dev);
  15309. struct pt_core_data *cd = dev_get_drvdata(dev);
  15310. ssize_t ret;
  15311. ret = scnprintf(buf, strlen(buf),
  15312. "%s: %d\n"
  15313. "%s: %d\n"
  15314. "%s: %d\n"
  15315. "%s: %d\n"
  15316. "%s: %d\n"
  15317. "%s: %d\n"
  15318. "%s: %d\n"
  15319. "%s: %d\n"
  15320. "%s: %d\n"
  15321. "%s: %d\n"
  15322. "%s: %d\n"
  15323. "%s: %d\n"
  15324. "%s: %d\n"
  15325. "%s: %d\n"
  15326. "%s: %d\n"
  15327. "%s: %d\n"
  15328. "%s: %d\n"
  15329. "%s: %d\n"
  15330. "%s: %d\n"
  15331. "%s: %d\n"
  15332. "%s: %d\n"
  15333. "%s: %d\n"
  15334. "%s: %d\n"
  15335. "%s: %d\n"
  15336. "%s: %d\n"
  15337. "%s: %d\n"
  15338. "%s: %d\n"
  15339. "%s: %s\n"
  15340. "%s: %s\n"
  15341. "%s: %d\n",
  15342. "Status", 0,
  15343. "Interrupt GPIO ", pdata->core_pdata->irq_gpio,
  15344. "Interrupt GPIO Value ",
  15345. pdata->core_pdata->irq_gpio ?
  15346. gpio_get_value(pdata->core_pdata->irq_gpio) : 0,
  15347. "Reset GPIO ", pdata->core_pdata->rst_gpio,
  15348. "Reset GPIO Value ",
  15349. pdata->core_pdata->rst_gpio ?
  15350. gpio_get_value(pdata->core_pdata->rst_gpio) : 0,
  15351. "DDI Reset GPIO ", pdata->core_pdata->ddi_rst_gpio,
  15352. "DDI Reset GPIO Value ",
  15353. pdata->core_pdata->ddi_rst_gpio ?
  15354. gpio_get_value(pdata->core_pdata->ddi_rst_gpio) : 0,
  15355. "VDDI GPIO ", pdata->core_pdata->vddi_gpio,
  15356. "VDDI GPIO Value ",
  15357. pdata->core_pdata->vddi_gpio ?
  15358. gpio_get_value(pdata->core_pdata->vddi_gpio) : 0,
  15359. "VCC GPIO ", pdata->core_pdata->vcc_gpio,
  15360. "VCC GPIO Value ",
  15361. pdata->core_pdata->vcc_gpio ?
  15362. gpio_get_value(pdata->core_pdata->vcc_gpio) : 0,
  15363. "AVDD GPIO ", pdata->core_pdata->avdd_gpio,
  15364. "AVDD GPIO Value ",
  15365. pdata->core_pdata->avdd_gpio ?
  15366. gpio_get_value(pdata->core_pdata->avdd_gpio) : 0,
  15367. "AVEE GPIO ", pdata->core_pdata->avee_gpio,
  15368. "AVEE GPIO Value ",
  15369. pdata->core_pdata->avee_gpio ?
  15370. gpio_get_value(pdata->core_pdata->avee_gpio) : 0,
  15371. "Vendor ID ", pdata->core_pdata->vendor_id,
  15372. "Product ID ", pdata->core_pdata->product_id,
  15373. "Vkeys x ", pdata->mt_pdata->vkeys_x,
  15374. "Vkeys y ", pdata->mt_pdata->vkeys_y,
  15375. "Core data flags ", pdata->core_pdata->flags,
  15376. "MT data flags ", pdata->mt_pdata->flags,
  15377. "Loader data flags ", pdata->loader_pdata->flags,
  15378. "Level trigger delay (us) ",
  15379. pdata->core_pdata->level_irq_udelay,
  15380. "HID Descriptor Register ",
  15381. pdata->core_pdata->hid_desc_register,
  15382. "HID Output Register ",
  15383. cd->hid_desc.output_register,
  15384. "HID Command Register ",
  15385. cd->hid_desc.command_register,
  15386. "Easy wakeup gesture ",
  15387. pdata->core_pdata->easy_wakeup_gesture,
  15388. "Config DUT generation ",
  15389. pdata->core_pdata->config_dut_generation ?
  15390. (pdata->core_pdata->config_dut_generation ==
  15391. CONFIG_DUT_PIP2_CAPABLE ?
  15392. "PT TC/TT" : "Gen5/6") : "Auto",
  15393. "Watchdog Force Stop ",
  15394. pdata->core_pdata->watchdog_force_stop ?
  15395. "True" : "False",
  15396. "Panel ID Support ",
  15397. pdata->core_pdata->panel_id_support);
  15398. return ret;
  15399. }
  15400. #define PT_ERR_STR_SIZE 64
  15401. /*******************************************************************************
  15402. * FUNCTION: pt_bist_bus_test
  15403. *
  15404. * SUMMARY: Tests the connectivity of the active bus pins:
  15405. * I2C - SDA and SCL
  15406. * SPI - MOSI, MISO, CLK
  15407. *
  15408. * Disable TTDL interrupts, send a PIP cmd and then manually read the
  15409. * bus. If any data is read we know the I2C/SPI pins are connected
  15410. *
  15411. * RETURN:
  15412. * 0 = Success
  15413. * !0 = Failure
  15414. *
  15415. * PARAMETERS:
  15416. * *dev - pointer to device structure
  15417. * *net_toggled - pointer to where to store if bus toggled
  15418. * *err_str - pointer to error string buffer
  15419. ******************************************************************************/
  15420. static int pt_bist_bus_test(struct device *dev, u8 *net_toggled, u8 *err_str)
  15421. {
  15422. struct pt_core_data *cd = dev_get_drvdata(dev);
  15423. u8 ver_cmd[8] = {0x01, 0x01, 0x06, 0x00, 0x0E, 0x07, 0xF0, 0xB1};
  15424. u8 *read_buf = NULL;
  15425. int bytes_read = 0;
  15426. int rc = 0;
  15427. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  15428. if (read_buf == NULL) {
  15429. rc = -ENOMEM;
  15430. goto exit;
  15431. }
  15432. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  15433. pt_debug(dev, DL_INFO, "%s: TTDL Core Suspend\n", __func__);
  15434. disable_irq(cd->irq);
  15435. mutex_lock(&cd->system_lock);
  15436. cd->irq_disabled = true;
  15437. mutex_unlock(&cd->system_lock);
  15438. /*
  15439. * Sleep >4ms to allow any pending TTDL IRQ to finish. Without this
  15440. * the disable_irq_nosync() could cause the IRQ to get stuck asserted
  15441. */
  15442. usleep_range(5000, 6000);
  15443. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer Size[%d] VERSION\n",
  15444. __func__, (int)sizeof(ver_cmd));
  15445. pt_pr_buf(cd->dev, DL_DEBUG, ver_cmd, (int)sizeof(ver_cmd),
  15446. ">>> User CMD");
  15447. rc = pt_adap_write_read_specific(cd, sizeof(ver_cmd), ver_cmd, NULL, 0);
  15448. if (rc) {
  15449. pt_debug(dev, DL_ERROR,
  15450. "%s: BUS Test - Failed to send VER cmd\n", __func__);
  15451. *net_toggled = 0;
  15452. strlcpy(err_str,
  15453. "- Write failed, bus open or shorted or DUT in reset", PT_ERR_STR_SIZE);
  15454. goto exit_enable_irq;
  15455. }
  15456. usleep_range(4000, 5000);
  15457. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, read_buf);
  15458. pt_debug(dev, DL_INFO, "%s: BUS Test - %d bytes forced read\n",
  15459. __func__, bytes_read);
  15460. if (bytes_read == 0) {
  15461. *net_toggled = 0;
  15462. pt_debug(dev, DL_INFO, "%s: BUS Read Failed, 0 bytes read\n",
  15463. __func__);
  15464. strlcpy(err_str,
  15465. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  15466. rc = -EIO;
  15467. goto exit_enable_irq;
  15468. } else {
  15469. if (cd->bus_ops->bustype == BUS_I2C)
  15470. *net_toggled = 1;
  15471. else {
  15472. if ((bytes_read > 3) &&
  15473. (read_buf[3] & PIP2_CMD_COMMAND_ID_MASK) ==
  15474. PIP2_CMD_ID_VERSION)
  15475. *net_toggled = 1;
  15476. else {
  15477. *net_toggled = 0;
  15478. pt_debug(dev, DL_INFO,
  15479. "%s: BUS Read Failed, %d bytes read\n",
  15480. __func__, bytes_read);
  15481. strlcpy(err_str,
  15482. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  15483. }
  15484. }
  15485. }
  15486. exit_enable_irq:
  15487. enable_irq(cd->irq);
  15488. usleep_range(5000, 6000);
  15489. mutex_lock(&cd->system_lock);
  15490. cd->irq_disabled = false;
  15491. mutex_unlock(&cd->system_lock);
  15492. pt_debug(dev, DL_INFO, "%s: TTDL Core Resumed\n", __func__);
  15493. exit:
  15494. kfree(read_buf);
  15495. return rc;
  15496. }
  15497. /*******************************************************************************
  15498. * FUNCTION: pt_bist_irq_test
  15499. *
  15500. * SUMMARY: Tests the connectivity of the IRQ net
  15501. *
  15502. * This test will ensure there is a good connection between the host
  15503. * and the DUT on the irq pin. First determine if the IRQ is stuck
  15504. * asserted and if so keep reading messages off of the bus until
  15505. * it de-asserts. Possible outcomes:
  15506. * - IRQ was already de-asserted: Send a PIP command and if an
  15507. * interrupt is generated the test passes.
  15508. * - IRQ was asserted: Reading off the bus de-assertes the IRQ,
  15509. * test passes.
  15510. * - IRQ stays asserted: After reading the bus multiple times
  15511. * the IRQ stays asserted. Likely open or shorted to GND
  15512. *
  15513. * RETURN:
  15514. * 0 = Success
  15515. * !0 = Failure
  15516. *
  15517. * PARAMETERS:
  15518. * *dev - pointer to device structure
  15519. * *bus_toggled - pointer to where to store if bus toggled
  15520. * *irq_toggled - pointer to where to store if IRQ toggled
  15521. * *xres_toggled - pointer to where to store if XRES toggled
  15522. * *err_str - pointer to error string buffer
  15523. ******************************************************************************/
  15524. static int pt_bist_irq_test(struct device *dev,
  15525. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  15526. {
  15527. #ifdef TTDL_PTVIRTDUT_SUPPORT
  15528. u8 release_irq[3] = {0xFF, 0xFF, 0x03};
  15529. #endif /* TTDL_PTVIRTDUT_SUPPORT */
  15530. struct pt_core_data *cd = dev_get_drvdata(dev);
  15531. u8 *read_buf = NULL;
  15532. u8 mode = PT_MODE_UNKNOWN;
  15533. u16 actual_read_len;
  15534. int bytes_read = 0;
  15535. int count = 0;
  15536. int rc = 0;
  15537. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  15538. if (read_buf == NULL) {
  15539. rc = -ENOMEM;
  15540. goto exit;
  15541. }
  15542. /* Clear IRQ triggered count, and re-evaluate at the end of test */
  15543. cd->irq_count = 0;
  15544. /*
  15545. * Check if IRQ is stuck asserted, if so try a non forced flush of
  15546. * the bus based on the 2 byte initial length read. Try up to 5x.
  15547. */
  15548. while (pt_check_irq_asserted(cd) && count < 5) {
  15549. count++;
  15550. bytes_read += pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  15551. }
  15552. #ifdef TTDL_PTVIRTDUT_SUPPORT
  15553. if (pt_check_irq_asserted(cd) && cd->route_bus_virt_dut) {
  15554. /* Force virtual DUT to release IRQ */
  15555. pt_pr_buf(cd->dev, DL_DEBUG, release_irq,
  15556. (int)sizeof(release_irq), ">>> User CMD");
  15557. pt_adap_write_read_specific(cd, 3, release_irq, NULL, 0);
  15558. }
  15559. #endif /* TTDL_PTVIRTDUT_SUPPORT */
  15560. if (count > 1 && count < 5 && bytes_read > 0) {
  15561. /*
  15562. * IRQ was stuck but data was successfully read from the
  15563. * bus releasing the IRQ line.
  15564. */
  15565. pt_debug(dev, DL_INFO, "%s: count=%d bytes_read=%d\n",
  15566. __func__, count, bytes_read);
  15567. *bus_toggled = 1;
  15568. *irq_toggled = 1;
  15569. goto exit;
  15570. }
  15571. if (count == 5 && bytes_read == 0) {
  15572. /*
  15573. * Looped 5x and read nothing off the bus yet the IRQ is still
  15574. * asserted. Possible conditions:
  15575. * - IRQ open circuit
  15576. * - IRQ shorted to GND
  15577. * - I2C/SPI bus is disconnected
  15578. * - FW holding the pin low
  15579. * Try entering the BL to see if communication works there.
  15580. */
  15581. mode = PT_MODE_IGNORE;
  15582. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  15583. if (rc) {
  15584. pt_debug(dev, DL_ERROR,
  15585. "%s Failed to enter BL\n", __func__);
  15586. strlcpy(err_str,
  15587. "- likely shorted to GND or FW holding it.", PT_ERR_STR_SIZE);
  15588. *irq_toggled = 0;
  15589. goto exit;
  15590. }
  15591. /*
  15592. * If original mode was operational and we successfully
  15593. * entered the BL, then the XRES net must have toggled
  15594. */
  15595. if (mode == PT_MODE_OPERATIONAL)
  15596. *xres_toggled = 1;
  15597. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  15598. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  15599. &actual_read_len);
  15600. if (rc) {
  15601. /*
  15602. * Could not communicate to DUT in BL mode. Save the
  15603. * error string, slim chance but the XRES test below may
  15604. * show the IRQ is actually working.
  15605. */
  15606. strlcpy(err_str, "- likely shorted to GND.", PT_ERR_STR_SIZE);
  15607. pt_debug(dev, DL_ERROR,
  15608. "%s: %s, count=%d bytes_read=%d\n",
  15609. __func__, err_str, count, bytes_read);
  15610. *irq_toggled = 0;
  15611. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  15612. goto exit;
  15613. } else {
  15614. *bus_toggled = 1;
  15615. *irq_toggled = 1;
  15616. goto exit;
  15617. }
  15618. }
  15619. if (pt_check_irq_asserted(cd)) {
  15620. strlcpy(err_str, "- likely shorted to GND", PT_ERR_STR_SIZE);
  15621. rc = -EIO;
  15622. *irq_toggled = 0;
  15623. goto exit;
  15624. }
  15625. /* Try sending a PIP command to see if we get a response */
  15626. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  15627. PIP2_CMD_ID_VERSION, NULL, 0, read_buf, &actual_read_len);
  15628. if (rc) {
  15629. /*
  15630. * Potential IRQ issue, no communication in App mode, attempt
  15631. * the same command in the BL
  15632. */
  15633. mode = PT_MODE_IGNORE;
  15634. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  15635. if (rc) {
  15636. pt_debug(dev, DL_ERROR,
  15637. "%s Failed to enter BL\n", __func__);
  15638. *irq_toggled = 0;
  15639. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  15640. goto exit;
  15641. }
  15642. /*
  15643. * If original mode was operational and we successfully
  15644. * entered the BL, this will be useful info for the tp_xres
  15645. * test below.
  15646. */
  15647. if (mode == PT_MODE_OPERATIONAL)
  15648. *xres_toggled = 1;
  15649. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  15650. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  15651. &actual_read_len);
  15652. if (rc) {
  15653. /*
  15654. * Could not communicate in FW mode or BL mode. Save the
  15655. * error string, slim chance but the XRES test below may
  15656. * show the IRQ is actually working.
  15657. */
  15658. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  15659. pt_debug(dev, DL_ERROR,
  15660. "%s: request_active_pip_prot failed\n",
  15661. __func__);
  15662. *irq_toggled = 0;
  15663. goto exit;
  15664. }
  15665. }
  15666. if (cd->irq_count > 0) {
  15667. pt_debug(dev, DL_INFO, "%s: irq_count=%d\n", __func__,
  15668. cd->irq_count);
  15669. *bus_toggled = 1;
  15670. *irq_toggled = 1;
  15671. goto exit;
  15672. }
  15673. exit:
  15674. kfree(read_buf);
  15675. return rc;
  15676. }
  15677. /*******************************************************************************
  15678. * FUNCTION: pt_bist_xres_test
  15679. *
  15680. * SUMMARY: Tests the connectivity of the TP_XRES net
  15681. *
  15682. * This test will ensure there is a good connection between the host
  15683. * and the DUT on the tp_xres pin. The pin will be toggled to
  15684. * generate a TP reset which will cause the DUT to output a reset
  15685. * sentinel. If the reset sentinel is seen the test passes. If it is
  15686. * not seen the test will attempt to send a soft reset to simply gain
  15687. * some additional information on the failure:
  15688. * - soft reset fails to send: XRES and IRQ likely open
  15689. * - soft reset passes: XRES likely open or stuck de-asserted
  15690. * - soft reset fails: XRES likely stuck asserted
  15691. *
  15692. * RETURN:
  15693. * 0 = Success
  15694. * !0 = Failure
  15695. *
  15696. * PARAMETERS:
  15697. * *dev - pointer to device structure
  15698. * *bus_toggled - pointer to where to store if bus toggled
  15699. * *irq_toggled - pointer to where to store if IRQ toggled
  15700. * *xres_toggled - pointer to where to store if XRES toggled
  15701. * *err_str - pointer to error string buffer
  15702. ******************************************************************************/
  15703. static int pt_bist_xres_test(struct device *dev,
  15704. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  15705. {
  15706. struct pt_core_data *cd = dev_get_drvdata(dev);
  15707. struct pt_platform_data *pdata = dev_get_platdata(dev);
  15708. #ifdef TTDL_PTVIRTDUT_SUPPORT
  15709. u8 release_irq[3] = {0xFF, 0xFF, 0x03};
  15710. #endif /* TTDL_PTVIRTDUT_SUPPORT */
  15711. u8 *read_buf = NULL;
  15712. u8 mode = PT_MODE_UNKNOWN;
  15713. int rc = 0;
  15714. int t = 0;
  15715. int timeout = 300;
  15716. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  15717. if (read_buf == NULL) {
  15718. rc = -ENOMEM;
  15719. goto exit;
  15720. }
  15721. /* Clear the startup bit mask, reset and enum will re-populate it */
  15722. cd->startup_status = STARTUP_STATUS_START;
  15723. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  15724. if ((!pdata->core_pdata->rst_gpio) || (!pdata->core_pdata->xres)) {
  15725. strlcpy(err_str, "- Net not configured or available", PT_ERR_STR_SIZE);
  15726. rc = -ENODEV;
  15727. goto exit;
  15728. }
  15729. /* Ensure we have nothing pending on active bus */
  15730. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  15731. #ifdef TTDL_PTVIRTDUT_SUPPORT
  15732. if (pt_check_irq_asserted(cd) && cd->route_bus_virt_dut) {
  15733. /* Force virtual DUT to release IRQ */
  15734. pt_pr_buf(cd->dev, DL_DEBUG, release_irq,
  15735. (int)sizeof(release_irq), ">>> User CMD");
  15736. pt_adap_write_read_specific(cd, 3, release_irq, NULL, 0);
  15737. }
  15738. #endif /* TTDL_PTVIRTDUT_SUPPORT */
  15739. /* Perform a hard XRES toggle and wait for reset sentinel */
  15740. mutex_lock(&cd->system_lock);
  15741. cd->hid_reset_cmd_state = 1;
  15742. mutex_unlock(&cd->system_lock);
  15743. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  15744. rc = pt_hw_hard_reset(cd);
  15745. /* Set timeout to 1s for the flashless case where a BL could be done */
  15746. if (cd->flashless_dut)
  15747. timeout = 1000;
  15748. /*
  15749. * To avoid the case that next PIP command can be confused by BL/FW
  15750. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  15751. * followed by "wakeup event" function can lower the failure rate.
  15752. */
  15753. t = wait_event_timeout(cd->wait_q,
  15754. ((cd->startup_status != STARTUP_STATUS_START)
  15755. && (cd->hid_reset_cmd_state == 0)),
  15756. msecs_to_jiffies(timeout));
  15757. if (IS_TMO(t)) {
  15758. pt_debug(cd->dev, DL_ERROR,
  15759. "%s: TMO waiting for sentinel\n", __func__);
  15760. *xres_toggled = 0;
  15761. strlcpy(err_str, "- likely open. (No Reset Sentinel)", PT_ERR_STR_SIZE);
  15762. /*
  15763. * Possibly bad FW, Try entering BL and wait for reset sentinel.
  15764. * To enter the BL we need to generate an XRES so first try to
  15765. * launch the applicaiton
  15766. */
  15767. if (cd->mode == PT_MODE_BOOTLOADER)
  15768. pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  15769. pt_debug(dev, DL_INFO, "%s: Enter BL with a hard reset\n",
  15770. __func__);
  15771. mode = PT_MODE_IGNORE;
  15772. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  15773. if (rc) {
  15774. pt_debug(dev, DL_ERROR, "%s Failed to enter BL\n",
  15775. __func__);
  15776. *xres_toggled = 0;
  15777. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  15778. goto exit;
  15779. } else {
  15780. /* Wait for the BL sentinel */
  15781. t = wait_event_timeout(cd->wait_q,
  15782. (cd->startup_status != STARTUP_STATUS_START),
  15783. msecs_to_jiffies(500));
  15784. if (IS_TMO(t)) {
  15785. pt_debug(cd->dev, DL_ERROR,
  15786. "%s: TMO waiting for BL sentinel\n",
  15787. __func__);
  15788. *xres_toggled = 0;
  15789. strlcpy(err_str,
  15790. "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  15791. rc = -ETIME;
  15792. goto exit;
  15793. }
  15794. }
  15795. }
  15796. mutex_lock(&cd->system_lock);
  15797. cd->hid_reset_cmd_state = 0;
  15798. mutex_unlock(&cd->system_lock);
  15799. /* Look for BL or FW reset sentinels */
  15800. if (!rc && ((cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL) ||
  15801. (cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL))) {
  15802. pt_debug(dev, DL_INFO, "%s: hard XRES pass\n", __func__);
  15803. /* If a sentinel was seen, all nets are working */
  15804. *xres_toggled = 1;
  15805. *irq_toggled = 1;
  15806. /*
  15807. * For SPI test, bus read result can be confused as FW sentinel
  15808. * if MISO(slave) is connected to MISO(host).
  15809. */
  15810. if (cd->bus_ops->bustype == BUS_I2C)
  15811. *bus_toggled = 1;
  15812. } else {
  15813. /*
  15814. * Hard reset failed, however some additional information
  15815. * could be determined. Try a soft reset to see if DUT resets
  15816. * with the possible outcomes:
  15817. * - if it resets the line is not stuck asserted
  15818. * - if it does not reset the line could be stuck asserted
  15819. */
  15820. *xres_toggled = 0;
  15821. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  15822. msleep(30);
  15823. pt_debug(dev, DL_INFO, "%s: TP_XRES BIST soft reset rc=%d",
  15824. __func__, rc);
  15825. if (rc) {
  15826. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  15827. pt_debug(dev, DL_ERROR,
  15828. "%s: Hard reset failed, soft reset failed %s\n",
  15829. __func__, err_str);
  15830. goto exit;
  15831. }
  15832. if (cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL ||
  15833. cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) {
  15834. strlcpy(err_str,
  15835. "- likely open or stuck high, soft reset OK", PT_ERR_STR_SIZE);
  15836. pt_debug(dev, DL_ERROR,
  15837. "%s: Hard reset failed, soft reset passed-%s\n",
  15838. __func__, err_str);
  15839. } else if (cd->startup_status == 0) {
  15840. strlcpy(err_str, "- likely stuck high.", PT_ERR_STR_SIZE);
  15841. pt_debug(dev, DL_ERROR,
  15842. "%s: Hard reset failed, soft reset failed-%s\n",
  15843. __func__, err_str);
  15844. } else {
  15845. strlcpy(err_str, "- open or stuck.", PT_ERR_STR_SIZE);
  15846. pt_debug(dev, DL_ERROR,
  15847. "%s: Hard and Soft reset failed - %s\n",
  15848. __func__, err_str);
  15849. }
  15850. }
  15851. exit:
  15852. kfree(read_buf);
  15853. return rc;
  15854. }
  15855. /*******************************************************************************
  15856. * FUNCTION: pt_bist_slave_irq_test
  15857. *
  15858. * SUMMARY: Tests the connectivity of the Master/Slave IRQ net
  15859. *
  15860. * This test will ensure there is a good IRQ connection between the master
  15861. * DUT and the slave DUT(s). After power up the STATUS command is sent
  15862. * and the 'Slave Detect' bits are verified to ensure the master DUT
  15863. * saw each slave trigger the IRQ with it's reset sentinel.
  15864. *
  15865. * RETURN:
  15866. * 0 = Success
  15867. * !0 = Failure
  15868. *
  15869. * PARAMETERS:
  15870. * *dev - pointer to device structure
  15871. * *slave - pointer to one entry in the slave info array
  15872. ******************************************************************************/
  15873. static int pt_bist_slave_irq_test(struct device *dev,
  15874. struct pt_bist_data *slave)
  15875. {
  15876. struct pt_core_data *cd = dev_get_drvdata(dev);
  15877. u8 mode = PT_MODE_UNKNOWN;
  15878. u8 status;
  15879. u8 boot;
  15880. u8 read_buf[12];
  15881. u8 detected = 0;
  15882. u8 last_err = -1;
  15883. u16 actual_read_len;
  15884. int result = 0;
  15885. int rc = 0;
  15886. /*
  15887. * Ensure DUT is in the BL where the STATUS cmd will report the slave
  15888. * detect bits. If the DUT was in FW, entering the BL will cause an
  15889. * XRES signal which will inadvertently test the XRES net as well
  15890. */
  15891. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  15892. if (rc) {
  15893. pt_debug(cd->dev, DL_WARN, "%s: Error entering BL rc=%d\n",
  15894. __func__, rc);
  15895. if (slave->irq_err_str)
  15896. strlcpy(slave->irq_err_str,
  15897. "- State could not be determined.", PT_ERR_STR_SIZE);
  15898. goto exit;
  15899. }
  15900. /* Use the STATUS command to retrieve the slave detect bit(s) */
  15901. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  15902. PIP2_CMD_ID_STATUS, NULL, 0, read_buf,
  15903. &actual_read_len);
  15904. if (!rc) {
  15905. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  15906. "PIP2 STATUS");
  15907. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  15908. boot = read_buf[PIP2_RESP_BODY_OFFSET] & slave->mask;
  15909. /* Slave detect is only valid if status ok and in boot exec */
  15910. if (status == PIP2_RSP_ERR_NONE &&
  15911. boot == PIP2_STATUS_BOOT_EXEC) {
  15912. detected = read_buf[PIP2_RESP_BODY_OFFSET + 2] &
  15913. SLAVE_DETECT_MASK;
  15914. } else {
  15915. if (slave->irq_err_str)
  15916. strlcpy(slave->irq_err_str,
  15917. "- State could not be determined", PT_ERR_STR_SIZE);
  15918. rc = -EPERM;
  15919. }
  15920. } else {
  15921. pt_debug(cd->dev, DL_WARN, "%s: STATUS cmd failure\n",
  15922. __func__);
  15923. if (slave->irq_err_str)
  15924. strlcpy(slave->irq_err_str,
  15925. "- State could not be determined.", PT_ERR_STR_SIZE);
  15926. goto exit;
  15927. }
  15928. /*
  15929. * Retrieve boot error regardless of the state of the slave detect
  15930. * bit because the IRQ could have been stuck high or low.
  15931. */
  15932. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  15933. PIP2_CMD_ID_GET_LAST_ERRNO, NULL, 0,
  15934. read_buf, &actual_read_len);
  15935. if (!rc) {
  15936. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  15937. "PIP2 GET_LAST_ERRNO");
  15938. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  15939. last_err = read_buf[PIP2_RESP_BODY_OFFSET];
  15940. if (last_err) {
  15941. pt_debug(cd->dev, DL_WARN,
  15942. "%s: Master Boot Last Err = 0x%02X\n",
  15943. __func__, last_err);
  15944. }
  15945. } else {
  15946. pt_debug(cd->dev, DL_WARN,
  15947. "%s: GET_LAST_ERRNO cmd failure\n", __func__);
  15948. if (slave->irq_err_str)
  15949. strlcpy(slave->irq_err_str,
  15950. "- stuck, likely shorted to GND.", PT_ERR_STR_SIZE);
  15951. }
  15952. exit:
  15953. pt_debug(cd->dev, DL_INFO,
  15954. "%s: rc=%d detected=0x%02X boot_err=0x%02X\n",
  15955. __func__, rc, detected, last_err);
  15956. /*
  15957. * Clear any possible false positives:
  15958. * - An invalid image error as BIST doesn't need valid FW
  15959. * - A Flash file too small as BIST doesn't need FLASH
  15960. * - FLASH access errors when in no-flash mode
  15961. */
  15962. if ((last_err == PIP2_RSP_ERR_INVALID_IMAGE) ||
  15963. (last_err == PIP2_RSP_ERR_BUF_TOO_SMALL) ||
  15964. (last_err == PIP2_RSP_ERR_BAD_ADDRESS && cd->flashless_dut) ||
  15965. (last_err == PIP2_RSP_ERR_BAD_FRAME && cd->flashless_dut)) {
  15966. pt_debug(cd->dev, DL_INFO, "%s: Cleared boot error: 0x%02X\n",
  15967. __func__, last_err);
  15968. last_err = PIP2_RSP_ERR_NONE;
  15969. }
  15970. /* Attempt to add a hint based on boot error and detection */
  15971. if (slave->irq_err_str) {
  15972. if (last_err && detected)
  15973. scnprintf(slave->irq_err_str,PT_ERR_STR_SIZE, "%s 0x%02X",
  15974. "- Likely stuck low. Boot Error:",
  15975. last_err);
  15976. else if (last_err && !detected)
  15977. scnprintf(slave->irq_err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  15978. "- Likely stuck high. Boot Error:",
  15979. last_err);
  15980. else if (detected)
  15981. strlcpy(slave->irq_err_str,
  15982. "- Likely stuck low. No Critical Boot Error", PT_ERR_STR_SIZE);
  15983. else if (!detected)
  15984. strlcpy(slave->irq_err_str,
  15985. "- Likely stuck high. No Critical Boot Error.", PT_ERR_STR_SIZE);
  15986. }
  15987. slave->irq_toggled = (detected && !last_err) ? true : false;
  15988. /* Leave as UNTEST if slave not detected */
  15989. if (detected)
  15990. slave->bus_toggled = !last_err ? true : false;
  15991. slave->detected = detected;
  15992. slave->boot_err = last_err;
  15993. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X, %s=0x%02X, %s=0x%02X\n",
  15994. __func__,
  15995. "Detected", detected,
  15996. "slave_irq_toggled", slave->irq_toggled,
  15997. "slave_bus_toggled", slave->bus_toggled);
  15998. return rc;
  15999. }
  16000. /*******************************************************************************
  16001. * FUNCTION: pt_bist_slave_xres_test
  16002. *
  16003. * SUMMARY: Tests the connectivity of the Master/Slave TP_XRES net
  16004. *
  16005. * This test will ensure there is a good TP_XRES connection between the
  16006. * master DUT and the slave DUT(s). After toggling the XRES pin to the
  16007. * master, the STATUS command is sent and the 'Slave Detect' bits are
  16008. * verified to ensure the master DUT saw each slave trigger the IRQ with
  16009. * it's reset sentinel.
  16010. *
  16011. * RETURN:
  16012. * 0 = Success
  16013. * !0 = Failure
  16014. *
  16015. * PARAMETERS:
  16016. * *dev - pointer to device structure
  16017. * *slave - pointer to one entry in the slave info array
  16018. ******************************************************************************/
  16019. static int pt_bist_slave_xres_test(struct device *dev,
  16020. struct pt_bist_data *slave)
  16021. {
  16022. struct pt_core_data *cd = dev_get_drvdata(dev);
  16023. int rc = 0;
  16024. /* Force a reset to force the 'slave detect' bits to be re-acquired */
  16025. mutex_lock(&cd->system_lock);
  16026. cd->hid_reset_cmd_state = 1;
  16027. mutex_unlock(&cd->system_lock);
  16028. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  16029. pt_hw_hard_reset(cd);
  16030. msleep(100);
  16031. rc = pt_bist_slave_irq_test(dev, slave);
  16032. pt_debug(dev, DL_INFO, "%s: IRQ test rc = %d\n", __func__, rc);
  16033. if (!rc && slave->irq_toggled == false) {
  16034. /*
  16035. * If the slave IRQ did not toggle, either the slave_detect
  16036. * bit was not set or we had a boot error. If the slave
  16037. * detect was not set the slave did not reset causing a boot
  16038. * error.
  16039. */
  16040. if (slave->xres_err_str && !slave->detected) {
  16041. strlcpy(slave->xres_err_str, slave->irq_err_str, PT_ERR_STR_SIZE);
  16042. pt_debug(dev, DL_INFO,
  16043. "%s: detected=0 irq_err_str = %s\n",
  16044. __func__, slave->irq_err_str);
  16045. } else if (slave->xres_err_str && slave->boot_err > 0) {
  16046. scnprintf(slave->xres_err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  16047. "- likely open or an IRQ issue. Boot Error:",
  16048. slave->boot_err);
  16049. pt_debug(dev, DL_INFO,
  16050. "%s: boot_err=%d xres_err_str = %s\n",
  16051. __func__,
  16052. slave->boot_err, slave->xres_err_str);
  16053. } else {
  16054. pt_debug(dev, DL_WARN, "%s: No xres_err_str buffer\n",
  16055. __func__);
  16056. }
  16057. }
  16058. if (!rc)
  16059. slave->xres_toggled = slave->irq_toggled ? true : false;
  16060. else
  16061. slave->xres_toggled = false;
  16062. return rc;
  16063. }
  16064. /*******************************************************************************
  16065. * FUNCTION: pt_bist_slave_bus_test
  16066. *
  16067. * SUMMARY: Tests the connectivity of the Master/Slave SPI bus net
  16068. *
  16069. * This test will ensure a good SPI bus connection between the
  16070. * master DUT and the slave DUT(s). This bus connection is ensured by
  16071. * opening file 0 (SRAM loader). If there is a slave and the open fails
  16072. * then there is a master/slave communication issue. Opening file 0 on
  16073. * the master will open it on the slave as well if the slave was detected.
  16074. *
  16075. * RETURN:
  16076. * 0 = Success
  16077. * !0 = Failure
  16078. *
  16079. * PARAMETERS:
  16080. * *dev - pointer to device structure
  16081. * *slave - pointer to one entry in the slave info array
  16082. ******************************************************************************/
  16083. static int pt_bist_slave_bus_test(struct device *dev,
  16084. struct pt_bist_data *slave)
  16085. {
  16086. struct pt_core_data *cd = dev_get_drvdata(dev);
  16087. u8 mode = PT_MODE_UNKNOWN;
  16088. u8 bus_toggled = false;
  16089. u8 file_handle;
  16090. int result = 0;
  16091. int rc = 0;
  16092. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  16093. if (rc) {
  16094. pt_debug(cd->dev, DL_WARN, "%s: Error entering BL rc=%d\n",
  16095. __func__, rc);
  16096. if (slave->bus_err_str)
  16097. strlcpy(slave->bus_err_str,
  16098. "- State could not be determined.", PT_ERR_STR_SIZE);
  16099. goto exit;
  16100. }
  16101. pt_debug(dev, DL_INFO, "%s Attempt open file 0\n", __func__);
  16102. file_handle = _pt_pip2_file_open(dev, PIP2_RAM_FILE);
  16103. if (file_handle != PIP2_RAM_FILE) {
  16104. rc = -ENOENT;
  16105. bus_toggled = false;
  16106. pt_debug(dev, DL_WARN,
  16107. "%s Failed to open bin file\n", __func__);
  16108. if (slave->bus_err_str)
  16109. strlcpy(slave->bus_err_str,
  16110. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  16111. goto exit;
  16112. } else {
  16113. bus_toggled = true;
  16114. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  16115. pt_debug(dev, DL_WARN,
  16116. "%s: File Close failed, file_handle=%d\n",
  16117. __func__, file_handle);
  16118. }
  16119. }
  16120. exit:
  16121. /* If the master was able to send/recv a PIP msg, the IRQ must be ok */
  16122. slave->irq_toggled = bus_toggled;
  16123. slave->bus_toggled = bus_toggled;
  16124. return rc;
  16125. }
  16126. /*******************************************************************************
  16127. * FUNCTION: pt_bist_slave_test
  16128. *
  16129. * SUMMARY: Tests XRES, SPI BUS and IRQ for the slave DUT
  16130. *
  16131. * RETURN:
  16132. * 0 = Success
  16133. * !0 = Failure
  16134. *
  16135. * PARAMETERS:
  16136. * *dev - pointer to device structure
  16137. * *slave - pointer to one entry in the bist info array
  16138. * slave_id - id of the slave chip
  16139. ******************************************************************************/
  16140. static int pt_bist_slave_test(struct device *dev,
  16141. struct pt_bist_data *slave, int slave_id)
  16142. {
  16143. struct pt_core_data *cd = dev_get_drvdata(dev);
  16144. char *pr_buf;
  16145. int rc = 0;
  16146. slave->mask = 0x01 << slave_id;
  16147. slave->bus_toggled = 0x0F; /* untested */
  16148. slave->irq_toggled = 0x0F; /* untested */
  16149. slave->xres_toggled = 0x0F; /* untested */
  16150. pr_buf = slave->print_buf;
  16151. if (!pr_buf) {
  16152. rc = -ENOMEM;
  16153. goto print_results;
  16154. }
  16155. slave->bus_err_str = kzalloc(PT_ERR_STR_SIZE,
  16156. GFP_KERNEL);
  16157. slave->irq_err_str = kzalloc(PT_ERR_STR_SIZE,
  16158. GFP_KERNEL);
  16159. slave->xres_err_str = kzalloc(PT_ERR_STR_SIZE,
  16160. GFP_KERNEL);
  16161. /* ensure no malloc failure */
  16162. if (!slave->bus_err_str ||
  16163. !slave->irq_err_str ||
  16164. !slave->xres_err_str)
  16165. goto print_results;
  16166. memset(slave->bus_err_str, 0, PT_ERR_STR_SIZE);
  16167. memset(slave->irq_err_str, 0, PT_ERR_STR_SIZE);
  16168. memset(slave->xres_err_str, 0, PT_ERR_STR_SIZE);
  16169. /* --------------- SLAVE XRES BIST TEST --------------- */
  16170. if ((cd->ttdl_bist_select & PT_BIST_SLAVE_XRES_TEST) != 0) {
  16171. pt_debug(dev, DL_INFO,
  16172. "%s: ----- Start Slave %d XRES BIST -----",
  16173. __func__, slave_id);
  16174. slave->xres_toggled = 0xFF;
  16175. rc = pt_bist_slave_xres_test(dev, slave);
  16176. if (slave->bus_toggled == 1 &&
  16177. slave->irq_toggled == 1 &&
  16178. slave->xres_toggled == 1)
  16179. goto print_results;
  16180. }
  16181. /* --------------- SLAVE IRQ BIST TEST --------------- */
  16182. if ((cd->ttdl_bist_select & PT_BIST_SLAVE_IRQ_TEST) != 0) {
  16183. pt_debug(dev, DL_INFO,
  16184. "%s: ----- Start Slave %d IRQ BIST -----",
  16185. __func__, slave_id);
  16186. slave->irq_toggled = 0xFF;
  16187. rc = pt_bist_slave_irq_test(dev, slave);
  16188. pt_debug(dev, DL_INFO,
  16189. "%s: slave_irq_toggled = 0x%02X\n",
  16190. __func__, slave->irq_toggled);
  16191. if (slave->irq_toggled == 1) {
  16192. slave->bus_toggled = 1;
  16193. goto print_results;
  16194. }
  16195. }
  16196. /* --------------- SLAVE BUS BIST TEST --------------- */
  16197. if ((cd->ttdl_bist_select & PT_BIST_SLAVE_BUS_TEST) != 0) {
  16198. pt_debug(dev, DL_INFO,
  16199. "%s: ----- Start Slave %d BUS BIST -----",
  16200. __func__, slave_id);
  16201. slave->bus_toggled = 0xFF;
  16202. rc = pt_bist_slave_bus_test(dev, slave);
  16203. }
  16204. print_results:
  16205. /* --------------- PRINT OUT BIST RESULTS ---------------*/
  16206. pt_debug(dev, DL_INFO, "%s: ----- BIST Print Results ----", __func__);
  16207. if (!slave->bus_err_str ||
  16208. !slave->irq_err_str ||
  16209. !slave->xres_err_str) {
  16210. slave->pr_index = scnprintf(pr_buf, PT_MAX_PR_BUF_SIZE,
  16211. "M/S%d SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  16212. "M/S%d IRQ connection: [UNTEST]\n"
  16213. "M/S%d TP_XRES connection: [UNTEST]\n",
  16214. slave_id, slave_id, slave_id);
  16215. } else {
  16216. if (slave->irq_toggled == 1)
  16217. memset(slave->irq_err_str, 0, PT_ERR_STR_SIZE);
  16218. if (slave->xres_toggled == 1)
  16219. memset(slave->xres_err_str, 0, PT_ERR_STR_SIZE);
  16220. if (slave->bus_toggled == 1)
  16221. memset(slave->bus_err_str, 0, PT_ERR_STR_SIZE);
  16222. slave->status = 0;
  16223. if (cd->ttdl_bist_select & PT_BIST_SLAVE_BUS_TEST)
  16224. slave->status += slave->bus_toggled;
  16225. if (cd->ttdl_bist_select & PT_BIST_SLAVE_IRQ_TEST)
  16226. slave->status += slave->irq_toggled;
  16227. if (cd->ttdl_bist_select & PT_BIST_SLAVE_XRES_TEST)
  16228. slave->status += slave->xres_toggled;
  16229. pt_debug(dev, DL_WARN,
  16230. "%s: status = %d (Slave %d: %d,%d,%d)\n",
  16231. __func__, slave->status, slave_id,
  16232. slave->bus_toggled,
  16233. slave->irq_toggled,
  16234. slave->xres_toggled);
  16235. slave->pr_index = scnprintf(pr_buf, PT_MAX_PR_BUF_SIZE,
  16236. "M/S%d SPI (MISO,MOSI,CS,CLK): %s %s\n"
  16237. "M/S%d IRQ connection: %s %s\n"
  16238. "M/S%d TP_XRES connection: %s %s\n",
  16239. slave_id,
  16240. slave->bus_toggled == 0x0F ?
  16241. "[UNTEST]" :
  16242. slave->bus_toggled == 1 ?
  16243. "[ OK ]" :
  16244. "[FAILED]",
  16245. slave->bus_err_str,
  16246. slave_id,
  16247. slave->irq_toggled == 0x0F ?
  16248. "[UNTEST]" :
  16249. slave->irq_toggled == 1 ?
  16250. "[ OK ]" :
  16251. "[FAILED]",
  16252. slave->irq_err_str,
  16253. slave_id,
  16254. slave->xres_toggled == 0x0F ?
  16255. "[UNTEST]" :
  16256. slave->xres_toggled == 1 ?
  16257. "[ OK ]" :
  16258. "[FAILED]",
  16259. slave->xres_err_str);
  16260. }
  16261. kfree(slave->bus_err_str);
  16262. kfree(slave->irq_err_str);
  16263. kfree(slave->xres_err_str);
  16264. return rc;
  16265. }
  16266. /*******************************************************************************
  16267. * FUNCTION: pt_bist_host_test
  16268. *
  16269. * SUMMARY: Tests XRES, SPI BUS and IRQ for the host DUT
  16270. *
  16271. * RETURN:
  16272. * 0 = Success
  16273. * !0 = Failure
  16274. *
  16275. * PARAMETERS:
  16276. * *dev - pointer to device structure
  16277. * *host - pointer to the entry in the bist info array
  16278. ******************************************************************************/
  16279. static int pt_bist_host_test(struct device *dev,
  16280. struct pt_bist_data *host)
  16281. {
  16282. struct pt_core_data *cd = dev_get_drvdata(dev);
  16283. char *pr_buf;
  16284. int rc = 0;
  16285. u8 bus_toggled = 0x0F; /* default to untested */
  16286. u8 i2c_toggled = 0x0F; /* default to untested */
  16287. u8 spi_toggled = 0x0F; /* default to untested */
  16288. u8 irq_toggled = 0x0F; /* default to untested */
  16289. u8 xres_toggled = 0x0F; /* default to untested */
  16290. pr_buf = host->print_buf;
  16291. if (!pr_buf) {
  16292. rc = -ENOMEM;
  16293. goto print_results;
  16294. }
  16295. host->bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  16296. host->irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  16297. host->xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  16298. if (!host->bus_err_str ||
  16299. !host->irq_err_str ||
  16300. !host->xres_err_str)
  16301. goto print_results;
  16302. memset(host->xres_err_str, 0, PT_ERR_STR_SIZE);
  16303. memset(host->irq_err_str, 0, PT_ERR_STR_SIZE);
  16304. memset(host->bus_err_str, 0, PT_ERR_STR_SIZE);
  16305. /* --------------- TP_XRES BIST TEST --------------- */
  16306. if ((cd->ttdl_bist_select & PT_BIST_TP_XRES_TEST) != 0) {
  16307. pt_debug(dev, DL_INFO,
  16308. "%s: ----- Start TP_XRES BIST -----", __func__);
  16309. rc = pt_bist_xres_test(dev, &bus_toggled, &irq_toggled,
  16310. &xres_toggled, host->xres_err_str);
  16311. /* Done if the rest of all nets toggled */
  16312. if (bus_toggled == 1 && irq_toggled == 1 && xres_toggled == 1)
  16313. goto print_results;
  16314. }
  16315. /* Flush bus in case a PIP response is waiting from previous test */
  16316. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  16317. /* --------------- IRQ BIST TEST --------------- */
  16318. if ((cd->ttdl_bist_select & PT_BIST_IRQ_TEST) != 0) {
  16319. pt_debug(dev, DL_INFO,
  16320. "%s: ----- Start IRQ BIST -----", __func__);
  16321. bus_toggled = 0xFF;
  16322. irq_toggled = 0xFF;
  16323. rc = pt_bist_irq_test(dev, &bus_toggled, &irq_toggled,
  16324. &xres_toggled, host->irq_err_str);
  16325. /* If this net failed clear results from previous net */
  16326. if (irq_toggled != 1) {
  16327. xres_toggled = 0x0F;
  16328. memset(host->xres_err_str, 0, PT_ERR_STR_SIZE);
  16329. }
  16330. if (bus_toggled == 1 && irq_toggled == 1)
  16331. goto print_results;
  16332. }
  16333. /* Flush bus in case a PIP response is waiting from previous test */
  16334. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  16335. /* --------------- BUS BIST TEST --------------- */
  16336. if ((cd->ttdl_bist_select & PT_BIST_BUS_TEST) != 0) {
  16337. pt_debug(dev, DL_INFO,
  16338. "%s: ----- Start BUS BIST -----", __func__);
  16339. bus_toggled = 0xFF;
  16340. rc = pt_bist_bus_test(dev, &bus_toggled, host->bus_err_str);
  16341. /* If this net failed clear results from previous net */
  16342. if (bus_toggled == 0) {
  16343. irq_toggled = 0x0F;
  16344. memset(host->irq_err_str, 0, PT_ERR_STR_SIZE);
  16345. }
  16346. }
  16347. print_results:
  16348. /* --------------- PRINT OUT BIST RESULTS ---------------*/
  16349. pt_debug(dev, DL_INFO, "%s: ----- BIST Print Results ----", __func__);
  16350. /* Cannot print if any memory allocation issues */
  16351. if (!host->bus_err_str || !host->irq_err_str || !host->xres_err_str) {
  16352. host->pr_index = scnprintf(pr_buf, PT_MAX_PR_BUF_SIZE,
  16353. "Status: %d\n"
  16354. "I2C (SDA,SCL): [UNTEST]\n"
  16355. "SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  16356. "IRQ connection: [UNTEST]\n"
  16357. "TP_XRES connection: [UNTEST]\n", -ENOMEM);
  16358. } else {
  16359. host->status = 0;
  16360. if (bus_toggled == 1)
  16361. memset(host->bus_err_str, 0, PT_ERR_STR_SIZE);
  16362. if (irq_toggled == 1)
  16363. memset(host->irq_err_str, 0, PT_ERR_STR_SIZE);
  16364. if (xres_toggled == 1)
  16365. memset(host->xres_err_str, 0, PT_ERR_STR_SIZE);
  16366. if (cd->ttdl_bist_select & PT_BIST_BUS_TEST)
  16367. host->status += bus_toggled;
  16368. if (cd->ttdl_bist_select & PT_BIST_IRQ_TEST)
  16369. host->status += irq_toggled;
  16370. if (cd->ttdl_bist_select & PT_BIST_TP_XRES_TEST)
  16371. host->status += xres_toggled;
  16372. pt_debug(dev, DL_WARN, "%s: status = %d (%d,%d,%d)\n",
  16373. __func__, host->status, bus_toggled, irq_toggled,
  16374. xres_toggled);
  16375. if (cd->bus_ops->bustype == BUS_I2C)
  16376. i2c_toggled = bus_toggled;
  16377. else
  16378. spi_toggled = bus_toggled;
  16379. host->pr_index = scnprintf(pr_buf, PT_MAX_PR_BUF_SIZE,
  16380. "I2C (SDA,SCL): %s %s\n"
  16381. "SPI (MISO,MOSI,CS,CLK): %s %s\n"
  16382. "IRQ connection: %s %s\n"
  16383. "TP_XRES connection: %s %s\n",
  16384. i2c_toggled == 0x0F ? "[UNTEST]" :
  16385. i2c_toggled == 1 ? "[ OK ]" : "[FAILED]",
  16386. i2c_toggled == 0x0F ? "" : host->bus_err_str,
  16387. spi_toggled == 0x0F ? "[UNTEST]" :
  16388. spi_toggled == 1 ? "[ OK ]" : "[FAILED]",
  16389. spi_toggled == 0x0F ? "" : host->bus_err_str,
  16390. irq_toggled == 0x0F ? "[UNTEST]" :
  16391. irq_toggled == 1 ? "[ OK ]" : "[FAILED]",
  16392. host->irq_err_str,
  16393. xres_toggled == 0x0F ? "[UNTEST]" :
  16394. xres_toggled == 1 ? "[ OK ]" : "[FAILED]",
  16395. host->xres_err_str);
  16396. }
  16397. kfree(host->bus_err_str);
  16398. kfree(host->irq_err_str);
  16399. kfree(host->xres_err_str);
  16400. return rc;
  16401. }
  16402. /*******************************************************************************
  16403. * FUNCTION: pt_ttdl_bist_show
  16404. *
  16405. * SUMMARY: Show method for the ttdl_bist sysfs node. This built in self test
  16406. * will test that I2C/SPI, IRQ and TP_XRES pins are operational.
  16407. *
  16408. * NOTE: This function will reset the DUT and the startup_status bit
  16409. * mask. A pt_enum will be queued after completion.
  16410. *
  16411. * NOTE: The order of the net tests is done to optimize the time it takes
  16412. * to run. The first test is capable of verifying all nets, each subsequent
  16413. * test is only run if the previous was not able to see all nets toggle.
  16414. *
  16415. * RETURN:
  16416. * 0 = success
  16417. * !0 = failure
  16418. *
  16419. * PARAMETERS:
  16420. * *dev - pointer to device structure
  16421. * *attr - pointer to device attributes
  16422. * *buf - pointer to output buffer
  16423. ******************************************************************************/
  16424. static ssize_t pt_ttdl_bist_show(struct device *dev,
  16425. struct device_attribute *attr, char *buf)
  16426. {
  16427. struct pt_core_data *cd = dev_get_drvdata(dev);
  16428. ssize_t ret = 0;
  16429. u8 tests;
  16430. int slave_id = 0;
  16431. int num_slaves = 0;
  16432. int rc = 0;
  16433. int num_tests = 0;
  16434. int status = 1; /* 0 = Pass, !0 = fail */
  16435. struct pt_bist_data host;
  16436. struct pt_bist_data slaves[PT_MAX_DEVICES];
  16437. int idx_status = 0;
  16438. host.print_buf = kzalloc(PT_MAX_PR_BUF_SIZE, GFP_KERNEL);
  16439. if (!host.print_buf) {
  16440. ret = scnprintf(buf, strlen(buf),
  16441. "Status: 1\n"
  16442. "Failed to alloc memory");
  16443. return ret;
  16444. }
  16445. /* Load up slave info array when slaves present */
  16446. num_slaves = cd->num_devices - 1;
  16447. /* Turn off the TTDL WD during the test */
  16448. pt_stop_wd_timer(cd);
  16449. /* Shorten default PIP cmd timeout while running BIST */
  16450. cd->pip_cmd_timeout = 200;
  16451. /* Count the number of tests to run */
  16452. tests = cd->ttdl_bist_select;
  16453. while (tests) {
  16454. num_tests += tests & 1;
  16455. tests >>= 1;
  16456. }
  16457. pt_debug(dev, DL_WARN, "%s: BIST select = 0x%02X, run %d tests\n",
  16458. __func__, cd->ttdl_bist_select, num_tests);
  16459. /* Suppress auto BL to avoid loader thread sending PIP during xres */
  16460. if (cd->flashless_dut) {
  16461. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - SUPPRESS\n",
  16462. __func__);
  16463. mutex_lock(&cd->system_lock);
  16464. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  16465. mutex_unlock(&cd->system_lock);
  16466. }
  16467. /* --------------- TP HOST BIST TEST --------------- */
  16468. host.status = 0;
  16469. host.pr_index = 0;
  16470. rc = pt_bist_host_test(dev, &host);
  16471. pt_debug(dev, DL_INFO, "%s print_idx = %d\n",
  16472. __func__, host.pr_index);
  16473. status = host.status;
  16474. /* --------------- TP SLAVE BIST TEST --------------- */
  16475. for (slave_id = 0; slave_id < num_slaves; slave_id++) {
  16476. slaves[slave_id].print_buf = kzalloc(PT_MAX_PR_BUF_SIZE,
  16477. GFP_KERNEL);
  16478. if (!slaves[slave_id].print_buf) {
  16479. ret = scnprintf(buf, strlen(buf),
  16480. "Status: 1\n"
  16481. "Failed to alloc memory");
  16482. goto exit;
  16483. }
  16484. slaves[slave_id].status = 0;
  16485. slaves[slave_id].pr_index = 0;
  16486. pt_bist_slave_test(dev, &slaves[slave_id], slave_id);
  16487. status += slaves[slave_id].status;
  16488. }
  16489. idx_status = scnprintf(buf, strlen(buf), "Status: %d\n",
  16490. status == num_tests ? 0 : 1);
  16491. memcpy(buf + idx_status, host.print_buf, host.pr_index);
  16492. ret = idx_status + host.pr_index;
  16493. for (slave_id = 0; slave_id < num_slaves; slave_id++) {
  16494. memcpy(buf + ret, slaves[slave_id].print_buf,
  16495. slaves[slave_id].pr_index);
  16496. ret += slaves[slave_id].pr_index;
  16497. }
  16498. exit:
  16499. /* Restore PIP command timeout */
  16500. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  16501. /*
  16502. * We're done! - Perform a hard XRES toggle, allowing BL
  16503. * to load FW if there is any in Flash
  16504. */
  16505. mutex_lock(&cd->system_lock);
  16506. cd->hid_reset_cmd_state = 0;
  16507. mutex_unlock(&cd->system_lock);
  16508. pt_debug(dev, DL_INFO,
  16509. "%s: TTDL BIST Complete - Final reset\n", __func__);
  16510. if (cd->flashless_dut) {
  16511. /*
  16512. * For Flashless solution, FW update is triggered after BL is
  16513. * seen that several miliseconds delay is needed.
  16514. */
  16515. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - ALLOW\n",
  16516. __func__);
  16517. mutex_lock(&cd->system_lock);
  16518. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  16519. mutex_unlock(&cd->system_lock);
  16520. /* Reset DUT and wait 100ms to see if loader started */
  16521. pt_hw_hard_reset(cd);
  16522. msleep(100);
  16523. if (cd->fw_updating) {
  16524. pt_debug(dev, DL_INFO,
  16525. "%s: ----- BIST Wait FW Loading ----",
  16526. __func__);
  16527. rc = _pt_request_wait_for_enum_state(
  16528. dev, 4000, STARTUP_STATUS_COMPLETE);
  16529. }
  16530. } else {
  16531. if (cd->mode == PT_MODE_BOOTLOADER) {
  16532. rc = pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  16533. if (rc) {
  16534. pt_debug(dev, DL_ERROR,
  16535. "%s Failed to launch app\n", __func__);
  16536. rc = pt_hw_hard_reset(cd);
  16537. }
  16538. }
  16539. /*
  16540. * If FW exists the BL may have just started or will start soon,
  16541. * so the FW sentinel may be on it's way but with no FW it will
  16542. * not arrive, wait for it before deciding if we need to queue
  16543. * an enum.
  16544. */
  16545. rc = _pt_request_wait_for_enum_state(
  16546. dev, 400, STARTUP_STATUS_FW_RESET_SENTINEL);
  16547. if ((cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) &&
  16548. (cd->startup_status & STARTUP_STATUS_COMPLETE) == 0) {
  16549. pt_debug(dev, DL_INFO, "%s: ----- BIST Enum ----",
  16550. __func__);
  16551. pt_queue_enum(cd);
  16552. rc = _pt_request_wait_for_enum_state(
  16553. dev, 2000, STARTUP_STATUS_COMPLETE);
  16554. }
  16555. }
  16556. msleep(20);
  16557. /* Put TTDL back into a known state, issue a ttdl enum if needed */
  16558. pt_debug(dev, DL_INFO, "%s: Startup_status = 0x%04X\n",
  16559. __func__, cd->startup_status);
  16560. kfree(host.print_buf);
  16561. for (slave_id = 0; slave_id < num_slaves; slave_id++)
  16562. kfree(slaves[slave_id].print_buf);
  16563. return ret;
  16564. }
  16565. /*******************************************************************************
  16566. * FUNCTION: pt_ttdl_bist_store
  16567. *
  16568. * SUMMARY: Store method for the ttdl_bist sysfs node.
  16569. *
  16570. * RETURN: Size of passed in buffer if successful
  16571. *
  16572. * PARAMETERS:
  16573. * *dev - pointer to device structure
  16574. * *attr - pointer to device attributes
  16575. * *buf - pointer to command buffer
  16576. * size - size of buf
  16577. ******************************************************************************/
  16578. static ssize_t pt_ttdl_bist_store(struct device *dev,
  16579. struct device_attribute *attr, const char *buf, size_t size)
  16580. {
  16581. struct pt_core_data *cd = dev_get_drvdata(dev);
  16582. u32 input_data[2] = {0};
  16583. int length;
  16584. int rc = 0;
  16585. /* Maximum input of one value */
  16586. length = _pt_ic_parse_input(dev, buf, size, input_data,
  16587. ARRAY_SIZE(input_data));
  16588. if (length != 1) {
  16589. pt_debug(dev, DL_WARN, "%s: Invalid number of arguments\n",
  16590. __func__);
  16591. rc = -EINVAL;
  16592. goto exit;
  16593. } else {
  16594. mutex_lock(&cd->system_lock);
  16595. cd->ttdl_bist_select = input_data[0];
  16596. mutex_unlock(&cd->system_lock);
  16597. }
  16598. exit:
  16599. if (rc)
  16600. return rc;
  16601. return size;
  16602. }
  16603. static DEVICE_ATTR(ttdl_bist, 0644, pt_ttdl_bist_show,
  16604. pt_ttdl_bist_store);
  16605. /*******************************************************************************
  16606. * FUNCTION: pt_flush_bus_store
  16607. *
  16608. * SUMMARY: Store method for the flush_bus sysfs node.
  16609. *
  16610. * RETURN: Size of passed in buffer if successful
  16611. *
  16612. * PARAMETERS:
  16613. * *dev - pointer to device structure
  16614. * *attr - pointer to device attributes
  16615. * *buf - pointer to command buffer
  16616. * size - size of buf
  16617. ******************************************************************************/
  16618. static ssize_t pt_flush_bus_store(struct device *dev,
  16619. struct device_attribute *attr, const char *buf, size_t size)
  16620. {
  16621. struct pt_core_data *cd = dev_get_drvdata(dev);
  16622. u32 input_data[2] = {0};
  16623. int length;
  16624. int rc = 0;
  16625. /* Maximum input of one value */
  16626. length = _pt_ic_parse_input(dev, buf, size, input_data,
  16627. ARRAY_SIZE(input_data));
  16628. if (length != 1) {
  16629. pt_debug(dev, DL_WARN, "%s: Invalid number of arguments\n",
  16630. __func__);
  16631. rc = -EINVAL;
  16632. goto exit;
  16633. }
  16634. mutex_lock(&cd->system_lock);
  16635. if (input_data[0] == 0)
  16636. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  16637. else if (input_data[0] == 1)
  16638. cd->flush_bus_type = PT_FLUSH_BUS_FULL_256_READ;
  16639. else
  16640. rc = -EINVAL;
  16641. mutex_unlock(&cd->system_lock);
  16642. exit:
  16643. if (rc)
  16644. return rc;
  16645. return size;
  16646. }
  16647. /*******************************************************************************
  16648. * FUNCTION: pt_flush_bus_show
  16649. *
  16650. * SUMMARY: Show method for the flush_bus sysfs node that flushes the active bus
  16651. * based on either the size of the first two bytes or a blind 256 bytes.
  16652. *
  16653. * RETURN:
  16654. * 0 = success
  16655. * !0 = failure
  16656. *
  16657. * PARAMETERS:
  16658. * *dev - pointer to device structure
  16659. * *attr - pointer to device attributes
  16660. * *buf - pointer to output buffer
  16661. ******************************************************************************/
  16662. static ssize_t pt_flush_bus_show(struct device *dev,
  16663. struct device_attribute *attr, char *buf)
  16664. {
  16665. ssize_t ret = 0;
  16666. ssize_t bytes = 0;
  16667. struct pt_core_data *cd = dev_get_drvdata(dev);
  16668. mutex_lock(&cd->system_lock);
  16669. bytes = pt_flush_bus(cd, cd->flush_bus_type, NULL);
  16670. ret = scnprintf(buf, strlen(buf),
  16671. "Status: 0\n"
  16672. "%s: %zd\n",
  16673. "Bytes flushed", bytes);
  16674. mutex_unlock(&cd->system_lock);
  16675. return ret;
  16676. }
  16677. static DEVICE_ATTR(flush_bus, 0644, pt_flush_bus_show,
  16678. pt_flush_bus_store);
  16679. /*******************************************************************************
  16680. * FUNCTION: pt_pip2_ping_test_store
  16681. *
  16682. * SUMMARY: Store method for the pip2_ping_test sysfs node.
  16683. *
  16684. * NOTE: The max PIP2 packet size is 255 (payload for PING 247) however
  16685. * someone may want to test sending invalid packet lengths so any values
  16686. * up to 255 are allowed.
  16687. *
  16688. * RETURN: Size of passed in buffer if successful
  16689. *
  16690. * PARAMETERS:
  16691. * *dev - pointer to device structure
  16692. * *attr - pointer to device attributes
  16693. * *buf - pointer to command buffer
  16694. * size - size of buf
  16695. ******************************************************************************/
  16696. static ssize_t pt_pip2_ping_test_store(struct device *dev,
  16697. struct device_attribute *attr, const char *buf, size_t size)
  16698. {
  16699. struct pt_core_data *cd = dev_get_drvdata(dev);
  16700. u32 input_data[2];
  16701. int length;
  16702. int rc = 0;
  16703. /* Maximum input of one value */
  16704. length = _pt_ic_parse_input(dev, buf, size, input_data,
  16705. ARRAY_SIZE(input_data));
  16706. if (length != 1) {
  16707. pt_debug(dev, DL_WARN, "%s: Invalid number of arguments\n",
  16708. __func__);
  16709. rc = -EINVAL;
  16710. goto exit;
  16711. }
  16712. mutex_lock(&cd->system_lock);
  16713. if (input_data[1] >= 0 && input_data[0] <= PT_MAX_PIP2_MSG_SIZE)
  16714. cd->ping_test_size = input_data[0];
  16715. else
  16716. rc = -EINVAL;
  16717. mutex_unlock(&cd->system_lock);
  16718. exit:
  16719. if (rc)
  16720. return rc;
  16721. return size;
  16722. }
  16723. /*******************************************************************************
  16724. * FUNCTION: pt_ping_test_show
  16725. *
  16726. * SUMMARY: Show method for the ping_test_show sysfs node that sends the PIP2
  16727. * PING command and ramps up the optional payload from 0 to
  16728. * ping_test_size.
  16729. * The max payload size is 247:
  16730. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  16731. *
  16732. * RETURN:
  16733. * 0 = success
  16734. * !0 = failure
  16735. *
  16736. * PARAMETERS:
  16737. * *dev - pointer to device structure
  16738. * *attr - pointer to device attributes
  16739. * *buf - pointer to output buffer
  16740. ******************************************************************************/
  16741. static ssize_t pt_pip2_ping_test_show(struct device *dev,
  16742. struct device_attribute *attr, char *buf)
  16743. {
  16744. struct pt_core_data *cd = dev_get_drvdata(dev);
  16745. ssize_t ret;
  16746. int last_packet_size;
  16747. int rc = 0;
  16748. rc = pt_pip2_ping_test(dev, cd->ping_test_size, &last_packet_size);
  16749. if (rc) {
  16750. ret = scnprintf(buf, strlen(buf), "Status: %d\n", rc);
  16751. return ret;
  16752. }
  16753. ret = scnprintf(buf, strlen(buf),
  16754. "Status: %d\n"
  16755. "PING payload test passed with packet sizes 0 - %d\n",
  16756. (last_packet_size == cd->ping_test_size ? 0 : 1),
  16757. last_packet_size);
  16758. return ret;
  16759. }
  16760. /*******************************************************************************
  16761. * FUNCTION: pt_t_refresh_store
  16762. *
  16763. * SUMMARY: Store method for the t-refresh sysfs node that will takes a passed
  16764. * in integer as the number of interrupts to count. A timer is started to
  16765. * calculate the total time it takes to see that number of interrupts.
  16766. *
  16767. * RETURN: Size of passed in buffer if successful
  16768. *
  16769. * PARAMETERS:
  16770. * *dev - pointer to device structure
  16771. * *attr - pointer to device attributes
  16772. * *buf - pointer to buffer that hold the command parameters
  16773. * size - size of buf
  16774. ******************************************************************************/
  16775. static ssize_t pt_t_refresh_store(struct device *dev,
  16776. struct device_attribute *attr, const char *buf, size_t size)
  16777. {
  16778. struct pt_core_data *cd = dev_get_drvdata(dev);
  16779. u32 input_data[2];
  16780. int length;
  16781. int rc = 0;
  16782. /* Maximum input of one value */
  16783. length = _pt_ic_parse_input(dev, buf, size, input_data,
  16784. ARRAY_SIZE(input_data));
  16785. if (length != 1) {
  16786. pt_debug(dev, DL_WARN, "%s: Invalid number of arguments\n",
  16787. __func__);
  16788. rc = -EINVAL;
  16789. goto exit;
  16790. }
  16791. mutex_lock(&cd->system_lock);
  16792. pt_debug(dev, DL_INFO, "%s: Input value: %d\n", __func__,
  16793. input_data[0]);
  16794. if (input_data[0] >= 0 && input_data[0] <= 1000) {
  16795. cd->t_refresh_total = input_data[0];
  16796. cd->t_refresh_count = 0;
  16797. cd->t_refresh_active = 1;
  16798. } else {
  16799. pt_debug(dev, DL_WARN, "%s: Invalid value\n", __func__);
  16800. rc = -EINVAL;
  16801. }
  16802. mutex_unlock(&cd->system_lock);
  16803. exit:
  16804. pt_debug(dev, DL_WARN, "%s: rc = %d\n", __func__, rc);
  16805. if (rc)
  16806. return rc;
  16807. return size;
  16808. }
  16809. /*******************************************************************************
  16810. * FUNCTION: pt_t_refresh_show
  16811. *
  16812. * SUMMARY: Show method for the t-refresh sysfs node that will show the results
  16813. * of the T-Refresh timer counting the time it takes to see a user defined
  16814. * number of interrupts.
  16815. *
  16816. * RETURN:
  16817. * 0 = success
  16818. * !0 = failure
  16819. *
  16820. * PARAMETERS:
  16821. * *dev - pointer to device structure
  16822. * *attr - pointer to device attributes
  16823. * *buf - pointer to output buffer
  16824. ******************************************************************************/
  16825. static ssize_t pt_t_refresh_show(struct device *dev,
  16826. struct device_attribute *attr, char *buf)
  16827. {
  16828. ssize_t ret = 0;
  16829. struct pt_core_data *cd = dev_get_drvdata(dev);
  16830. u32 whole;
  16831. u16 fraction;
  16832. mutex_lock(&cd->system_lock);
  16833. /* Check if we have counted the number requested */
  16834. if (cd->t_refresh_count != cd->t_refresh_total) {
  16835. ret = scnprintf(buf, strlen(buf),
  16836. "Status: 0\n"
  16837. "%s: %d\n",
  16838. "Still counting... current IRQ count",
  16839. cd->t_refresh_count);
  16840. } else {
  16841. /* Ensure T-Refresh is de-activated */
  16842. cd->t_refresh_active = 0;
  16843. whole = cd->t_refresh_time / cd->t_refresh_count;
  16844. fraction = cd->t_refresh_time % cd->t_refresh_count;
  16845. fraction = fraction * 1000 / cd->t_refresh_count;
  16846. ret = scnprintf(buf, strlen(buf),
  16847. "Status: 0\n"
  16848. "%s: %d\n"
  16849. "%s: %d\n"
  16850. "%s: %d\n"
  16851. "%s: %d.%02d\n",
  16852. "Requested IRQ Count ", cd->t_refresh_total,
  16853. "IRQ Counted ", cd->t_refresh_count,
  16854. "Total Time Elapsed (ms) ", (int)cd->t_refresh_time,
  16855. "Average T-Refresh (ms) ", whole, fraction);
  16856. }
  16857. mutex_unlock(&cd->system_lock);
  16858. return ret;
  16859. }
  16860. /*******************************************************************************
  16861. * FUNCTION: pt_dut_status_show
  16862. *
  16863. * SUMMARY: Show method for DUT status sysfs node. Display DUT's scan state, and
  16864. * more items such as operation mode,easywake state are added in the future.
  16865. *
  16866. * RETURN: Char buffer with printed scan status information
  16867. *
  16868. * PARAMETERS:
  16869. * *dev - pointer to device structure
  16870. * *attr - pointer to device attributes
  16871. * *buf - pointer to output buffer
  16872. ******************************************************************************/
  16873. static ssize_t pt_dut_status_show(struct device *dev,
  16874. struct device_attribute *attr, char *buf)
  16875. {
  16876. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  16877. u8 mode = PT_MODE_UNKNOWN;
  16878. char *outputstring[7] = {"BOOT", "SCANNING", "DEEP_SLEEP",
  16879. "TEST", "DEEP_STANDBY", "UNDEFINED", "n/a"};
  16880. struct pt_core_data *cd = dev_get_drvdata(dev);
  16881. ssize_t ret;
  16882. u16 calculated_crc = 0;
  16883. u16 stored_crc = 0;
  16884. u8 status;
  16885. int rc = 0;
  16886. /* In STANDBY the DUT will not repond to any PIP cmd */
  16887. if (cd->fw_sys_mode_in_standby_state) {
  16888. mode = PT_MODE_OPERATIONAL;
  16889. sys_mode = FW_SYS_MODE_DEEP_STANDBY;
  16890. goto print_limited_results;
  16891. }
  16892. /* Retrieve mode and FW system mode which can only be 0-4 */
  16893. rc = pt_get_fw_sys_mode(cd, &sys_mode, &mode);
  16894. if (rc || mode == PT_MODE_UNKNOWN) {
  16895. ret = snprintf(buf, PT_MAX_PRBUF_SIZE,
  16896. "%s: %d\n"
  16897. "%s: n/a\n"
  16898. "%s: n/a\n"
  16899. "%s: n/a\n"
  16900. "%s: n/a\n",
  16901. "Status", rc,
  16902. "Active Exec ",
  16903. "FW System Mode ",
  16904. "Stored CRC ",
  16905. "Calculated CRC ");
  16906. return ret;
  16907. } else {
  16908. if (mode == PT_MODE_OPERATIONAL) {
  16909. if (sys_mode > FW_SYS_MODE_MAX)
  16910. sys_mode = FW_SYS_MODE_UNDEFINED;
  16911. if (sys_mode != FW_SYS_MODE_TEST)
  16912. goto print_limited_results;
  16913. rc = pt_pip_verify_config_block_crc_(cd,
  16914. PT_TCH_PARM_EBID, &status,
  16915. &calculated_crc, &stored_crc);
  16916. if (rc)
  16917. goto print_limited_results;
  16918. ret = snprintf(buf, PT_MAX_PRBUF_SIZE,
  16919. "%s: %d\n"
  16920. "%s: %s\n"
  16921. "%s: %s\n"
  16922. "%s: 0x%04X\n"
  16923. "%s: 0x%04X\n",
  16924. "Status", rc,
  16925. "Active Exec ", "FW",
  16926. "FW System Mode ", outputstring[sys_mode],
  16927. "Stored CRC ", stored_crc,
  16928. "Calculated CRC ", calculated_crc);
  16929. return ret;
  16930. } else {
  16931. /* When in BL or unknon mode Active Exec is "n/a" */
  16932. sys_mode = FW_SYS_MODE_UNDEFINED + 1;
  16933. }
  16934. }
  16935. print_limited_results:
  16936. ret = snprintf(buf, PT_MAX_PRBUF_SIZE,
  16937. "%s: %d\n"
  16938. "%s: %s\n"
  16939. "%s: %s\n"
  16940. "%s: n/a\n"
  16941. "%s: n/a\n",
  16942. "Status", rc,
  16943. "Active Exec ",
  16944. mode == PT_MODE_OPERATIONAL ? "FW" : "BL",
  16945. "FW System Mode ", outputstring[sys_mode],
  16946. "Stored CRC ",
  16947. "Calculated CRC ");
  16948. return ret;
  16949. }
  16950. #endif /* TTDL_DIAGNOSTICS */
  16951. /*******************************************************************************
  16952. * Structures of sysfs attributes for all DUT dependent sysfs node
  16953. ******************************************************************************/
  16954. static struct attribute *early_attrs[] = {
  16955. &dev_attr_hw_version.attr,
  16956. &dev_attr_drv_version.attr,
  16957. &dev_attr_drv_ver.attr,
  16958. &dev_attr_fw_version.attr,
  16959. &dev_attr_sysinfo.attr,
  16960. #ifndef TTDL_KERNEL_SUBMISSION
  16961. &dev_attr_pip2_cmd_rsp.attr,
  16962. &dev_attr_command.attr,
  16963. &dev_attr_drv_debug.attr,
  16964. &dev_attr_hw_reset.attr,
  16965. &dev_attr_ttdl_restart.attr,
  16966. #ifdef TTDL_DIAGNOSTICS
  16967. &dev_attr_ttdl_status.attr,
  16968. &dev_attr_pip2_enter_bl.attr,
  16969. &dev_attr_pip2_exit_bl.attr,
  16970. &dev_attr_err_gpio.attr,
  16971. &dev_attr_flush_bus.attr,
  16972. &dev_attr_ttdl_bist.attr,
  16973. #endif /* TTDL_DIAGNOSTICS */
  16974. #endif /* !TTDL_KERNEL_SUBMISSION */
  16975. NULL,
  16976. };
  16977. static struct attribute_group early_attr_group = {
  16978. .attrs = early_attrs,
  16979. };
  16980. static struct device_attribute pip2_attributes[] = {
  16981. __ATTR(pip2_version, 0444, pt_pip2_version_show, NULL),
  16982. __ATTR(pip2_gpio_read, 0444, pt_pip2_gpio_read_show, NULL),
  16983. #ifdef TTDL_DIAGNOSTICS
  16984. __ATTR(pip2_bin_hdr, 0444, pt_pip2_bin_hdr_show, NULL),
  16985. __ATTR(pip2_ping_test, 0644, pt_pip2_ping_test_show,
  16986. pt_pip2_ping_test_store),
  16987. #endif
  16988. };
  16989. static struct device_attribute attributes[] = {
  16990. __ATTR(dut_debug, 0644,
  16991. pt_dut_debug_show, pt_drv_debug_store),
  16992. __ATTR(sleep_status, 0444, pt_sleep_status_show, NULL),
  16993. __ATTR(panel_id, 0444, pt_panel_id_show, NULL),
  16994. __ATTR(get_param, 0644,
  16995. pt_get_param_show, pt_get_param_store),
  16996. #ifdef EASYWAKE_TSG6
  16997. __ATTR(easy_wakeup_gesture, 0644, pt_easy_wakeup_gesture_show,
  16998. pt_easy_wakeup_gesture_store),
  16999. __ATTR(easy_wakeup_gesture_id, 0444,
  17000. pt_easy_wakeup_gesture_id_show, NULL),
  17001. __ATTR(easy_wakeup_gesture_data, 0444,
  17002. pt_easy_wakeup_gesture_data_show, NULL),
  17003. #endif
  17004. #ifdef TTDL_DIAGNOSTICS
  17005. __ATTR(platform_data, 0444, pt_platform_data_show, NULL),
  17006. __ATTR(drv_irq, 0644, pt_drv_irq_show, pt_drv_irq_store),
  17007. __ATTR(dut_status, 0444, pt_dut_status_show, NULL),
  17008. __ATTR(t_refresh, 0644, pt_t_refresh_show, pt_t_refresh_store),
  17009. #endif /* TTDL_DIAGNOSTICS */
  17010. };
  17011. /*******************************************************************************
  17012. * FUNCTION: add_sysfs_interfaces
  17013. *
  17014. * SUMMARY: Creates all DUT dependent sysfs nodes owned by the core
  17015. *
  17016. * RETURN:
  17017. * 0 = success
  17018. * !0 = failure
  17019. *
  17020. * PARAMETERS:
  17021. * *dev - pointer to device structure
  17022. ******************************************************************************/
  17023. static int add_sysfs_interfaces(struct device *dev)
  17024. {
  17025. int i;
  17026. int j = 0;
  17027. struct pt_core_data *cd = dev_get_drvdata(dev);
  17028. for (i = 0; i < ARRAY_SIZE(attributes); i++) {
  17029. if (device_create_file(dev, attributes + i))
  17030. goto undo;
  17031. }
  17032. pt_debug(dev, DL_INFO, "%s: Active DUT Generation: %d",
  17033. __func__, cd->active_dut_generation);
  17034. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  17035. for (j = 0; j < ARRAY_SIZE(pip2_attributes); j++) {
  17036. if (device_create_file(dev, pip2_attributes + j))
  17037. goto undo;
  17038. }
  17039. }
  17040. return 0;
  17041. undo:
  17042. for (i--; i >= 0; i--)
  17043. device_remove_file(dev, attributes + i);
  17044. for (j--; j >= 0; j--)
  17045. device_remove_file(dev, pip2_attributes + j);
  17046. pt_debug(dev, DL_ERROR, "%s: failed to create sysfs interface\n",
  17047. __func__);
  17048. return -ENODEV;
  17049. }
  17050. /*******************************************************************************
  17051. * FUNCTION: remove_sysfs_interfaces
  17052. *
  17053. * SUMMARY: Removes all DUT dependent sysfs nodes owned by the core
  17054. *
  17055. * RETURN: void
  17056. *
  17057. * PARAMETERS:
  17058. * *dev - pointer to device structure
  17059. ******************************************************************************/
  17060. static void remove_sysfs_interfaces(struct device *dev)
  17061. {
  17062. int i;
  17063. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  17064. device_remove_file(dev, attributes + i);
  17065. for (i = 0; i < ARRAY_SIZE(pip2_attributes); i++)
  17066. device_remove_file(dev, pip2_attributes + i);
  17067. }
  17068. /*******************************************************************************
  17069. * FUNCTION: remove_sysfs_and_modules
  17070. *
  17071. * SUMMARY: Removes all DUT dependent sysfs nodes and modules
  17072. *
  17073. * RETURN: void
  17074. *
  17075. * PARAMETERS:
  17076. * *dev - pointer to device structure
  17077. ******************************************************************************/
  17078. static void remove_sysfs_and_modules(struct device *dev)
  17079. {
  17080. struct pt_core_data *cd = dev_get_drvdata(dev);
  17081. /* Queued work should be removed before to release loader module */
  17082. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  17083. pt_release_modules(cd);
  17084. pt_btn_release(dev);
  17085. pt_mt_release(dev);
  17086. remove_sysfs_interfaces(dev);
  17087. }
  17088. #endif /* !TTDL_KERNEL_SUBMISSION */
  17089. /*******************************************************************************
  17090. *******************************************************************************
  17091. * FUNCTION: pt_probe
  17092. *
  17093. * SUMMARY: Probe of the core module.
  17094. *
  17095. * NOTE: For the Parade Technologies development platform (PtSBC) the
  17096. * probe functionality is split into two functions; pt_probe() and
  17097. * pt_probe_complete(). the initial setup is done in this function which
  17098. * then creates a WORK task which runs after the probe timer expires. This
  17099. * ensures the I2C/SPI is up on the PtSBC in time for TTDL.
  17100. *
  17101. * RETURN:
  17102. * 0 = success
  17103. * !0 = failure
  17104. *
  17105. * PARAMETERS:
  17106. * *ops - pointer to the bus
  17107. * *dev - pointer to the device structure
  17108. * irq - IRQ
  17109. * xfer_buf_size - size of the buffer
  17110. ******************************************************************************/
  17111. int pt_probe(const struct pt_bus_ops *ops, struct device *dev,
  17112. u16 irq, size_t xfer_buf_size)
  17113. {
  17114. struct pt_core_data *cd;
  17115. struct pt_platform_data *pdata = dev_get_platdata(dev);
  17116. enum pt_atten_type type;
  17117. int rc = 0;
  17118. #ifndef PT_PTSBC_SUPPORT
  17119. u8 pip_ver_major;
  17120. u8 pip_ver_minor;
  17121. u32 status = STARTUP_STATUS_START;
  17122. #endif
  17123. #ifdef TTDL_PTVIRTDUT_SUPPORT
  17124. int retry = 3;
  17125. #endif
  17126. if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
  17127. pt_debug(dev, DL_ERROR, "%s: Missing platform data\n",
  17128. __func__);
  17129. rc = -ENODEV;
  17130. goto error_no_pdata;
  17131. }
  17132. if (pdata->core_pdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  17133. if (!pdata->core_pdata->power) {
  17134. pt_debug(dev, DL_ERROR,
  17135. "%s: Missing platform data function\n",
  17136. __func__);
  17137. rc = -ENODEV;
  17138. goto error_no_pdata;
  17139. }
  17140. }
  17141. /* get context and debug print buffers */
  17142. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  17143. if (!cd) {
  17144. rc = -ENOMEM;
  17145. goto error_alloc_data;
  17146. }
  17147. /* Initialize device info */
  17148. cd->dev = dev;
  17149. cd->pdata = pdata;
  17150. cd->cpdata = pdata->core_pdata;
  17151. cd->bus_ops = ops;
  17152. cd->debug_level = PT_INITIAL_DEBUG_LEVEL;
  17153. cd->show_timestamp = PT_INITIAL_SHOW_TIME_STAMP;
  17154. scnprintf(cd->core_id, 20, "%s%d", PT_CORE_NAME, core_number++);
  17155. cd->hw_detected = false;
  17156. cd->pip2_prot_active = false;
  17157. cd->pip2_send_user_cmd = false;
  17158. cd->bl_pip_ver_ready = false;
  17159. cd->app_pip_ver_ready = false;
  17160. cd->pip2_cmd_tag_seq = 0x08; /* PIP2 TAG=1 and 3 bit SEQ=0 */
  17161. cd->get_param_id = 0;
  17162. cd->watchdog_enabled = 0;
  17163. cd->startup_retry_count = 0;
  17164. cd->core_probe_complete = 0;
  17165. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  17166. cd->pip_cmd_timeout = PT_PIP_CMD_DEFAULT_TIMEOUT;
  17167. cd->pip_cmd_timeout_default = PT_PIP_CMD_DEFAULT_TIMEOUT;
  17168. cd->flashless_dut = 0;
  17169. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  17170. cd->bl_with_no_int = 0;
  17171. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  17172. cd->num_devices = 1;
  17173. cd->tthe_hid_usb_format = PT_TTHE_TUNER_FORMAT_HID_I2C;
  17174. if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP2_CAPABLE) {
  17175. cd->set_dut_generation = true;
  17176. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  17177. } else if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP1_ONLY) {
  17178. cd->set_dut_generation = true;
  17179. cd->active_dut_generation = DUT_PIP1_ONLY;
  17180. } else {
  17181. cd->set_dut_generation = false;
  17182. cd->active_dut_generation = DUT_UNKNOWN;
  17183. }
  17184. /* Initialize with platform data */
  17185. cd->watchdog_force_stop = cd->cpdata->watchdog_force_stop;
  17186. #ifdef PT_PTSBC_SUPPORT
  17187. /* Extend first WD to allow DDI to complete configuration */
  17188. cd->watchdog_interval = PT_PTSBC_INIT_WATCHDOG_TIMEOUT;
  17189. #else
  17190. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  17191. #endif
  17192. cd->hid_cmd_state = 1;
  17193. cd->fw_updating = false;
  17194. #ifdef TTDL_DIAGNOSTICS
  17195. cd->t_refresh_active = 0;
  17196. cd->t_refresh_count = 0;
  17197. cd->pip2_crc_error_count = 0;
  17198. cd->wd_xres_count = 0;
  17199. cd->bl_retry_packet_count = 0;
  17200. cd->file_erase_timeout_count = 0;
  17201. cd->show_tt_data = false;
  17202. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  17203. cd->err_gpio = 0;
  17204. cd->err_gpio_type = PT_ERR_GPIO_NONE;
  17205. cd->ttdl_bist_select = 0x07;
  17206. cd->force_pip2_seq = 0;
  17207. #endif /* TTDL_DIAGNOSTICS */
  17208. #ifdef TTDL_PTVIRTDUT_SUPPORT
  17209. /*
  17210. * This variable is only used for BATS test. The
  17211. * default value "true" has no effect on whether
  17212. * the PT_DETECT_HW build flag is enabled or not.
  17213. */
  17214. cd->hw_detect_enabled = true;
  17215. cd->route_bus_virt_dut = 0;
  17216. #endif /* TTDL_PTVIRTDUT_SUPPORT */
  17217. memset(cd->pip2_us_file_path, 0, PT_MAX_PATH_SIZE);
  17218. memcpy(cd->pip2_us_file_path, PT_PIP2_BIN_FILE_PATH,
  17219. sizeof(PT_PIP2_BIN_FILE_PATH));
  17220. pt_init_hid_descriptor(&cd->hid_desc);
  17221. /* Read and store the descriptor lengths */
  17222. cd->hid_core.hid_report_desc_len =
  17223. le16_to_cpu(cd->hid_desc.report_desc_len);
  17224. cd->hid_core.hid_max_input_len =
  17225. le16_to_cpu(cd->hid_desc.max_input_len);
  17226. cd->hid_core.hid_max_output_len =
  17227. le16_to_cpu(cd->hid_desc.max_output_len);
  17228. /* Initialize mutexes and spinlocks */
  17229. mutex_init(&cd->module_list_lock);
  17230. mutex_init(&cd->system_lock);
  17231. mutex_init(&cd->sysfs_lock);
  17232. mutex_init(&cd->ttdl_restart_lock);
  17233. mutex_init(&cd->firmware_class_lock);
  17234. mutex_init(&cd->hid_report_lock);
  17235. spin_lock_init(&cd->spinlock);
  17236. /* Initialize module list */
  17237. INIT_LIST_HEAD(&cd->module_list);
  17238. /* Initialize attention lists */
  17239. for (type = 0; type < PT_ATTEN_NUM_ATTEN; type++)
  17240. INIT_LIST_HEAD(&cd->atten_list[type]);
  17241. /* Initialize parameter list */
  17242. INIT_LIST_HEAD(&cd->param_list);
  17243. /* Initialize wait queue */
  17244. init_waitqueue_head(&cd->wait_q);
  17245. /* Initialize works */
  17246. INIT_WORK(&cd->enum_work, pt_enum_work_function);
  17247. INIT_WORK(&cd->ttdl_restart_work, pt_restart_work_function);
  17248. INIT_WORK(&cd->watchdog_work, pt_watchdog_work);
  17249. /* Initialize HID specific data */
  17250. cd->hid_core.hid_vendor_id = (cd->cpdata->vendor_id) ?
  17251. cd->cpdata->vendor_id : HID_VENDOR_ID;
  17252. cd->hid_core.hid_product_id = (cd->cpdata->product_id) ?
  17253. cd->cpdata->product_id : HID_APP_PRODUCT_ID;
  17254. cd->hid_core.hid_desc_register =
  17255. cpu_to_le16(cd->cpdata->hid_desc_register);
  17256. /* Set platform easywake value */
  17257. cd->easy_wakeup_gesture = cd->cpdata->easy_wakeup_gesture;
  17258. /* Set platform panel_id value */
  17259. cd->panel_id_support = cd->cpdata->panel_id_support;
  17260. if (cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO)
  17261. /* Set Panel ID to default to 0 */
  17262. cd->pid_for_loader = PT_PANEL_ID_DEFAULT;
  17263. else
  17264. /* Set Panel ID to Not Enabled */
  17265. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  17266. /* Initialize hw_version default to FFFF.FFFF.FF */
  17267. snprintf(cd->hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  17268. dev_set_drvdata(dev, cd);
  17269. #ifndef PT_PTSBC_SUPPORT
  17270. /* PtSBC builds will call this function in pt_probe_complete() */
  17271. pt_add_core(dev);
  17272. #endif
  17273. rc = sysfs_create_group(&dev->kobj, &early_attr_group);
  17274. if (rc)
  17275. pt_debug(cd->dev, DL_WARN, "%s:create early attrs failed\n",
  17276. __func__);
  17277. #ifndef TTDL_KERNEL_SUBMISSION
  17278. rc = device_create_bin_file(dev, &bin_attr_pt_response);
  17279. if (rc) {
  17280. pt_debug(dev, DL_ERROR,
  17281. "%s: Error, could not create node response\n",
  17282. __func__);
  17283. }
  17284. #endif /* !TTDL_KERNEL_SUBMISSION */
  17285. /*
  17286. * Save the pointer to a global value, which will be used
  17287. * in ttdl_restart function
  17288. */
  17289. cd->bus_ops = ops;
  17290. /*
  17291. * When the IRQ GPIO is not direclty accessible and no function is
  17292. * defined to get the IRQ status, the IRQ passed in must be assigned
  17293. * directly as the gpio_to_irq will not work. e.g. CHROMEOS
  17294. */
  17295. if (!cd->cpdata->irq_stat) {
  17296. cd->irq = irq;
  17297. pt_debug(cd->dev, DL_WARN, "%s:No irq_stat, Set cd->irq = %d\n",
  17298. __func__, cd->irq);
  17299. }
  17300. #ifdef PT_PTSBC_SUPPORT
  17301. /*
  17302. * For the PtSBC, on the first bring up, I2C/SPI will not be ready
  17303. * in time so complete probe with pt_probe_complete() after work
  17304. * probe timer expires.
  17305. */
  17306. INIT_WORK(&cd->probe_work, pt_probe_work);
  17307. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  17308. setup_timer(&cd->probe_timer, pt_probe_timer, (unsigned long)cd);
  17309. #else
  17310. timer_setup(&cd->probe_timer, pt_probe_timer, 0);
  17311. #endif
  17312. /* Some host i2c/spi busses start late and then run too slow */
  17313. pt_debug(cd->dev, DL_INFO, "%s:start wait for probe timer\n",
  17314. __func__);
  17315. mod_timer(&cd->probe_timer, jiffies +
  17316. msecs_to_jiffies(PT_CORE_PROBE_STARTUP_DELAY_MS));
  17317. return rc;
  17318. error_alloc_data:
  17319. error_no_pdata:
  17320. pt_debug(dev, DL_ERROR, "%s failed.\n", __func__);
  17321. return rc;
  17322. }
  17323. /*******************************************************************************
  17324. * FUNCTION: pt_probe_complete
  17325. *
  17326. * SUMMARY: This function is only needed when PT_PTSBC_SUPPORT is enabled.
  17327. * For the PtSBC, the probe functionality is split into two functions;
  17328. * pt_probe() and pt_probe_complete(). The initial setup is done
  17329. * in pt_probe() and the rest is done here after I2C/SPI is up. This
  17330. * function also configures all voltage regulators for the PtSBC.
  17331. *
  17332. * RETURN:
  17333. * 0 = success
  17334. * !0 = failure
  17335. *
  17336. * PARAMETERS:
  17337. * *cd - pointer to the core data structure
  17338. ******************************************************************************/
  17339. static int pt_probe_complete(struct pt_core_data *cd)
  17340. {
  17341. int rc = -1;
  17342. u32 status = STARTUP_STATUS_START;
  17343. struct device *dev = cd->dev;
  17344. u8 pip_ver_major;
  17345. u8 pip_ver_minor;
  17346. #ifdef TTDL_PTVIRTDUT_SUPPORT
  17347. int retry = 3;
  17348. #endif
  17349. pt_debug(cd->dev, DL_DEBUG,
  17350. "%s: PARADE Entering Probe complete function\n", __func__);
  17351. pt_add_core(cd->dev);
  17352. #endif /* --- End PT_PTSBC_SUPPORT --- */
  17353. /* Call platform init function before setting up the GPIO's */
  17354. if (cd->cpdata->init) {
  17355. pt_debug(cd->dev, DL_INFO, "%s: Init HW\n", __func__);
  17356. rc = cd->cpdata->init(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  17357. } else {
  17358. pt_debug(cd->dev, DL_WARN, "%s: No HW INIT function\n",
  17359. __func__);
  17360. rc = 0;
  17361. }
  17362. if (rc < 0) {
  17363. pt_debug(cd->dev, DL_ERROR, "%s: HW Init fail r=%d\n",
  17364. __func__, rc);
  17365. }
  17366. /* Power on any needed regulator(s) */
  17367. if (cd->cpdata->setup_power) {
  17368. pt_debug(cd->dev, DL_INFO, "%s: Device power on!\n", __func__);
  17369. rc = cd->cpdata->setup_power(cd->cpdata,
  17370. PT_MT_POWER_ON, cd->dev);
  17371. } else {
  17372. pt_debug(cd->dev, DL_WARN, "%s: No setup power function\n",
  17373. __func__);
  17374. rc = 0;
  17375. }
  17376. if (rc < 0)
  17377. pt_debug(cd->dev, DL_ERROR, "%s: Setup power on fail r=%d\n",
  17378. __func__, rc);
  17379. #ifdef TTDL_DIAGNOSTICS
  17380. cd->watchdog_irq_stuck_count = 0;
  17381. cd->bus_transmit_error_count = 0;
  17382. #endif /* TTDL_DIAGNOSTICS */
  17383. #ifdef TTDL_PTVIRTDUT_SUPPORT
  17384. /*
  17385. * In the case that the variable hw_detect_enabled needs to be set to
  17386. * false as the default value, it needs to be set to true through
  17387. * drv_debug sysfs node when testing HW detect function. But the probe()
  17388. * function runs fast, and drv_debug may not set this variable to
  17389. * true in time. This "retry" is to avoid this issue.
  17390. */
  17391. retry_hw_detect:
  17392. if (cd->hw_detect_enabled) {
  17393. #endif
  17394. if (cd->cpdata->detect) {
  17395. pt_debug(cd->dev, DL_INFO, "%s: Detect HW\n", __func__);
  17396. rc = cd->cpdata->detect(cd->cpdata, cd->dev,
  17397. pt_platform_detect_read);
  17398. if (!rc) {
  17399. cd->hw_detected = true;
  17400. pt_debug(cd->dev, DL_INFO,
  17401. "%s: HW detected\n", __func__);
  17402. } else {
  17403. cd->hw_detected = false;
  17404. pt_debug(cd->dev, DL_INFO,
  17405. "%s: No HW detected\n", __func__);
  17406. rc = -ENODEV;
  17407. goto error_detect;
  17408. }
  17409. } else {
  17410. pt_debug(dev, DL_WARN,
  17411. "%s: PARADE No HW detect function pointer\n",
  17412. __func__);
  17413. /*
  17414. * "hw_reset" is not needed in the "if" statement,
  17415. * because "hw_reset" is already included in "hw_detect"
  17416. * function.
  17417. */
  17418. rc = pt_hw_hard_reset(cd);
  17419. if (rc)
  17420. pt_debug(cd->dev, DL_ERROR,
  17421. "%s: FAILED to execute HARD reset\n",
  17422. __func__);
  17423. }
  17424. #ifdef TTDL_PTVIRTDUT_SUPPORT
  17425. } else {
  17426. if (retry--) {
  17427. msleep(50);
  17428. goto retry_hw_detect;
  17429. }
  17430. }
  17431. #endif
  17432. if (cd->cpdata->setup_irq) {
  17433. pt_debug(cd->dev, DL_INFO, "%s: setup IRQ\n", __func__);
  17434. rc = cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_REG, cd->dev);
  17435. if (rc) {
  17436. pt_debug(dev, DL_ERROR,
  17437. "%s: Error, couldn't setup IRQ\n", __func__);
  17438. goto error_setup_irq;
  17439. }
  17440. } else {
  17441. pt_debug(dev, DL_ERROR,
  17442. "%s: IRQ function pointer not setup\n",
  17443. __func__);
  17444. goto error_setup_irq;
  17445. }
  17446. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  17447. setup_timer(&cd->watchdog_timer, pt_watchdog_timer,
  17448. (unsigned long)cd);
  17449. #else
  17450. timer_setup(&cd->watchdog_timer, pt_watchdog_timer, 0);
  17451. #endif
  17452. pt_stop_wd_timer(cd);
  17453. #ifdef TTHE_TUNER_SUPPORT
  17454. mutex_init(&cd->tthe_lock);
  17455. cd->tthe_debugfs = debugfs_create_file(PT_TTHE_TUNER_FILE_NAME,
  17456. 0644, NULL, cd, &tthe_debugfs_fops);
  17457. #endif
  17458. rc = device_init_wakeup(dev, 1);
  17459. if (rc < 0)
  17460. pt_debug(dev, DL_ERROR, "%s: Error, device_init_wakeup rc:%d\n",
  17461. __func__, rc);
  17462. pm_runtime_get_noresume(dev);
  17463. pm_runtime_set_active(dev);
  17464. pm_runtime_enable(dev);
  17465. /* If IRQ asserted, read out all from buffer to release INT pin */
  17466. if (cd->cpdata->irq_stat) {
  17467. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  17468. } else {
  17469. /* Force a read in case the reset sentinel already arrived */
  17470. rc = pt_read_input(cd);
  17471. if (!rc)
  17472. pt_parse_input(cd);
  17473. }
  17474. /* Without sleep DUT is not ready and will NAK the first write */
  17475. msleep(150);
  17476. /* Attempt to set the DUT generation if not yet set */
  17477. if (cd->active_dut_generation == DUT_UNKNOWN) {
  17478. if (cd->bl_pip_ver_ready ||
  17479. (cd->app_pip_ver_ready &&
  17480. IS_PIP_VER_GE(&cd->sysinfo, 1, 12))) {
  17481. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  17482. pt_debug(dev, DL_INFO, "%s: dut generation is %d\n",
  17483. __func__, cd->active_dut_generation);
  17484. } else {
  17485. rc = _pt_detect_dut_generation(cd->dev,
  17486. &status, &cd->active_dut_generation,
  17487. &cd->mode);
  17488. if ((cd->active_dut_generation == DUT_UNKNOWN)
  17489. || rc) {
  17490. pt_debug(cd->dev, DL_ERROR,
  17491. " === DUT Gen unknown, Skip Enum ===\n");
  17492. goto skip_enum;
  17493. }
  17494. }
  17495. }
  17496. _pt_request_active_pip_protocol(cd->dev, PT_CORE_CMD_PROTECTED,
  17497. &pip_ver_major, &pip_ver_minor);
  17498. if (pip_ver_major == 2) {
  17499. cd->bl_pip_ver_ready = true;
  17500. pt_debug(dev, DL_WARN,
  17501. " === PIP2.%d Detected, Attempt to launch APP ===\n",
  17502. pip_ver_minor);
  17503. cd->hw_detected = true;
  17504. } else if (pip_ver_major == 1) {
  17505. cd->app_pip_ver_ready = true;
  17506. pt_debug(dev, DL_WARN,
  17507. " === PIP1.%d Detected ===\n", pip_ver_minor);
  17508. cd->hw_detected = true;
  17509. } else {
  17510. cd->sysinfo.ttdata.pip_ver_major = 0;
  17511. cd->sysinfo.ttdata.pip_ver_minor = 0;
  17512. cd->app_pip_ver_ready = false;
  17513. cd->hw_detected = false;
  17514. pt_debug(dev, DL_ERROR,
  17515. " === PIP Version Not Detected, Skip Enum ===\n");
  17516. /* For legacy DUTS proceed, enum will attempt to launch app */
  17517. if (cd->active_dut_generation != DUT_PIP1_ONLY)
  17518. goto skip_enum;
  17519. }
  17520. rc = pt_enum_with_dut(cd, false, &status);
  17521. pt_debug(dev, DL_WARN, "%s: cd->startup_status=0x%04X status=0x%04X\n",
  17522. __func__, cd->startup_status, status);
  17523. if (rc == -ENODEV) {
  17524. pt_debug(cd->dev, DL_ERROR,
  17525. "%s: Enumeration Failed r=%d\n", __func__, rc);
  17526. #ifndef PT_PTSBC_SUPPORT
  17527. /* For PtSBC don't error out, allow TTDL to stay up */
  17528. goto error_after_startup;
  17529. #endif
  17530. }
  17531. /* Suspend scanning until probe is complete to avoid asyc touches */
  17532. pt_pip_suspend_scanning_(cd);
  17533. #ifndef TTDL_KERNEL_SUBMISSION
  17534. if (cd->hw_detected) {
  17535. pt_debug(dev, DL_INFO, "%s: Add sysfs interfaces\n",
  17536. __func__);
  17537. rc = add_sysfs_interfaces(dev);
  17538. if (rc < 0) {
  17539. pt_debug(dev, DL_ERROR,
  17540. "%s: Error, fail sysfs init\n", __func__);
  17541. goto error_after_startup;
  17542. }
  17543. } else {
  17544. pt_debug(dev, DL_WARN,
  17545. "%s: No HW detected, sysfs interfaces not added\n",
  17546. __func__);
  17547. }
  17548. #endif /* !TTDL_KERNEL_SUBMISSION */
  17549. skip_enum:
  17550. pm_runtime_put_sync(dev);
  17551. pt_debug(dev, DL_INFO, "%s: Probe: MT, BTN\n", __func__);
  17552. rc = pt_mt_probe(dev);
  17553. if (rc < 0) {
  17554. pt_debug(dev, DL_ERROR, "%s: Error, fail mt probe\n",
  17555. __func__);
  17556. goto error_after_sysfs_create;
  17557. }
  17558. rc = pt_btn_probe(dev);
  17559. if (rc < 0) {
  17560. pt_debug(dev, DL_ERROR, "%s: Error, fail btn probe\n",
  17561. __func__);
  17562. goto error_after_startup_mt;
  17563. }
  17564. pt_probe_modules(cd);
  17565. #ifdef CONFIG_HAS_EARLYSUSPEND
  17566. pt_setup_early_suspend(cd);
  17567. #elif defined(CONFIG_FB)
  17568. pt_setup_fb_notifier(cd);
  17569. #endif
  17570. #ifdef NEED_SUSPEND_NOTIFIER
  17571. cd->pm_notifier.notifier_call = pt_pm_notifier;
  17572. register_pm_notifier(&cd->pm_notifier);
  17573. #endif
  17574. pt_pip_resume_scanning_(cd);
  17575. mutex_lock(&cd->system_lock);
  17576. cd->startup_status |= status;
  17577. cd->core_probe_complete = 1;
  17578. mutex_unlock(&cd->system_lock);
  17579. pt_debug(dev, DL_WARN, "%s: TTDL Core Probe Completed Successfully\n",
  17580. __func__);
  17581. return 0;
  17582. error_after_startup_mt:
  17583. pr_err("%s PARADE error_after_startup_mt\n", __func__);
  17584. pt_mt_release(dev);
  17585. error_after_sysfs_create:
  17586. pr_err("%s PARADE error_after_sysfs_create\n", __func__);
  17587. pm_runtime_disable(dev);
  17588. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  17589. device_wakeup_disable(dev);
  17590. #endif
  17591. device_init_wakeup(dev, 0);
  17592. cancel_work_sync(&cd->enum_work);
  17593. pt_stop_wd_timer(cd);
  17594. pt_free_si_ptrs(cd);
  17595. #ifndef TTDL_KERNEL_SUBMISSION
  17596. remove_sysfs_interfaces(dev);
  17597. #endif /* !TTDL_KERNEL_SUBMISSION */
  17598. error_after_startup:
  17599. pr_err("%s PARADE error_after_startup\n", __func__);
  17600. del_timer(&cd->watchdog_timer);
  17601. if (cd->cpdata->setup_irq)
  17602. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  17603. error_setup_irq:
  17604. error_detect:
  17605. #ifdef PT_PTSBC_SUPPORT
  17606. del_timer(&cd->probe_timer);
  17607. #endif
  17608. if (cd->cpdata->init)
  17609. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  17610. if (cd->cpdata->setup_power)
  17611. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  17612. sysfs_remove_group(&dev->kobj, &early_attr_group);
  17613. pt_del_core(dev);
  17614. dev_set_drvdata(dev, NULL);
  17615. kfree(cd);
  17616. #ifndef PT_PTSBC_SUPPORT
  17617. error_alloc_data:
  17618. error_no_pdata:
  17619. #endif
  17620. pr_err("%s failed.\n", __func__);
  17621. return rc;
  17622. }
  17623. EXPORT_SYMBOL_GPL(pt_probe);
  17624. /*******************************************************************************
  17625. * FUNCTION: pt_release
  17626. *
  17627. * SUMMARY: This function does the following cleanup:
  17628. * - Releases all probed modules
  17629. * - Stops the watchdog
  17630. * - Cancels all pending work tasks
  17631. * - Removes all created sysfs nodes
  17632. * - Removes all created debugfs nodes
  17633. * - Frees the IRQ
  17634. * - Deletes the core
  17635. * - Frees all pointers and HID reports
  17636. *
  17637. * RETURN:
  17638. * 0 = success
  17639. * !0 = failure
  17640. *
  17641. * PARAMETERS:
  17642. * *cd - pointer to the core data structure
  17643. ******************************************************************************/
  17644. int pt_release(struct pt_core_data *cd)
  17645. {
  17646. struct device *dev = cd->dev;
  17647. /*
  17648. * Suspend the device before freeing the startup_work and stopping
  17649. * the watchdog since sleep function restarts watchdog on failure
  17650. */
  17651. pm_runtime_suspend(dev);
  17652. pm_runtime_disable(dev);
  17653. /*
  17654. * Any 'work' that can trigger a new thread should be canceled first.
  17655. * The watchdog is also stopped again because another thread could have
  17656. * restarted it. The irq_work is cancelled last because it is used for
  17657. * all I2C/SPI communication.
  17658. */
  17659. pt_stop_wd_timer(cd);
  17660. #ifdef PT_PTSBC_SUPPORT
  17661. cancel_work_sync(&cd->probe_work);
  17662. #endif
  17663. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  17664. cancel_work_sync(&cd->ttdl_restart_work);
  17665. cancel_work_sync(&cd->enum_work);
  17666. pt_stop_wd_timer(cd);
  17667. #ifdef PT_PTSBC_SUPPORT
  17668. cancel_work_sync(&cd->irq_work);
  17669. #endif
  17670. pt_release_modules(cd);
  17671. pt_proximity_release(dev);
  17672. pt_btn_release(dev);
  17673. pt_mt_release(dev);
  17674. #ifdef CONFIG_HAS_EARLYSUSPEND
  17675. unregister_early_suspend(&cd->es);
  17676. #elif defined(CONFIG_FB)
  17677. fb_unregister_client(&cd->fb_notifier);
  17678. #endif
  17679. #ifdef NEED_SUSPEND_NOTIFIER
  17680. unregister_pm_notifier(&cd->pm_notifier);
  17681. #endif
  17682. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  17683. device_wakeup_disable(dev);
  17684. #endif
  17685. device_init_wakeup(dev, 0);
  17686. #ifdef TTHE_TUNER_SUPPORT
  17687. mutex_lock(&cd->tthe_lock);
  17688. cd->tthe_exit = 1;
  17689. wake_up(&cd->wait_q);
  17690. mutex_unlock(&cd->tthe_lock);
  17691. debugfs_remove(cd->tthe_debugfs);
  17692. #endif
  17693. sysfs_remove_group(&dev->kobj, &early_attr_group);
  17694. #ifndef TTDL_KERNEL_SUBMISSION
  17695. remove_sysfs_interfaces(dev);
  17696. #endif /* !TTDL_KERNEL_SUBMISSION */
  17697. disable_irq_nosync(cd->irq);
  17698. if (cd->cpdata->setup_irq)
  17699. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  17700. if (cd->cpdata->init)
  17701. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  17702. if (cd->cpdata->setup_power)
  17703. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  17704. dev_set_drvdata(dev, NULL);
  17705. pt_del_core(dev);
  17706. pt_free_si_ptrs(cd);
  17707. pt_free_hid_reports(cd);
  17708. kfree(cd);
  17709. return 0;
  17710. }
  17711. EXPORT_SYMBOL_GPL(pt_release);
  17712. MODULE_LICENSE("GPL");
  17713. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product Core Driver");
  17714. MODULE_AUTHOR("Parade Technologies <[email protected]>");