pt_core.c 501 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456
  1. /*
  2. * pt_core.c
  3. * Parade TrueTouch(TM) Standard Product Core Module.
  4. * For use with Parade touchscreen controllers.
  5. * Supported parts include:
  6. * TMA5XX
  7. * TMA448
  8. * TMA445A
  9. * TT21XXX
  10. * TT31XXX
  11. * TT4XXXX
  12. * TT7XXX
  13. * TC3XXX
  14. *
  15. * Copyright (C) 2015-2020 Parade Technologies
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * version 2, and only version 2, as published by the
  20. * Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * Contact Parade Technologies at www.paradetech.com <[email protected]>
  28. */
  29. #include <linux/i2c.h>
  30. #include <linux/gpio.h>
  31. #include <linux/kthread.h>
  32. #include "pt_regs.h"
  33. #define PINCTRL_STATE_ACTIVE "pmx_ts_active"
  34. #define PINCTRL_STATE_SUSPEND "pmx_ts_suspend"
  35. #define PINCTRL_STATE_RELEASE "pmx_ts_release"
  36. #define FT_VTG_MIN_UV 2800000
  37. #define FT_VTG_MAX_UV 2800000
  38. #define FT_I2C_VTG_MIN_UV 1800000
  39. #define FT_I2C_VTG_MAX_UV 1800000
  40. #define PT_CORE_STARTUP_RETRY_COUNT 3
  41. #define PT_STATUS_STR_LEN (50)
  42. MODULE_FIRMWARE(PT_FW_FILE_NAME);
  43. static const char *pt_driver_core_name = PT_CORE_NAME;
  44. static const char *pt_driver_core_version = PT_DRIVER_VERSION;
  45. static const char *pt_driver_core_date = PT_DRIVER_DATE;
  46. struct pt_hid_field {
  47. int report_count;
  48. int report_size;
  49. int size; /* report_count * report_size */
  50. int offset;
  51. int data_type;
  52. int logical_min;
  53. int logical_max;
  54. /* Usage Page (Hi 16 bit) + Usage (Lo 16 bit) */
  55. u32 usage_page;
  56. u32 collection_usage_pages[PT_HID_MAX_COLLECTIONS];
  57. struct pt_hid_report *report;
  58. bool record_field;
  59. };
  60. struct pt_hid_report {
  61. u8 id;
  62. u8 type;
  63. int size;
  64. struct pt_hid_field *fields[PT_HID_MAX_FIELDS];
  65. int num_fields;
  66. int record_field_index;
  67. int header_size;
  68. int record_size;
  69. u32 usage_page;
  70. };
  71. struct atten_node {
  72. struct list_head node;
  73. char *id;
  74. struct device *dev;
  75. int (*func)(struct device *dev);
  76. int mode;
  77. };
  78. struct param_node {
  79. struct list_head node;
  80. u8 id;
  81. u32 value;
  82. u8 size;
  83. };
  84. struct module_node {
  85. struct list_head node;
  86. struct pt_module *module;
  87. void *data;
  88. };
  89. struct pt_hid_cmd {
  90. u8 opcode;
  91. u8 report_type;
  92. union {
  93. u8 report_id;
  94. u8 power_state;
  95. };
  96. u8 has_data_register;
  97. size_t write_length;
  98. u8 *write_buf;
  99. u8 *read_buf;
  100. u8 wait_interrupt;
  101. u8 reset_cmd;
  102. u16 timeout_ms;
  103. };
  104. struct pt_hid_output {
  105. u8 cmd_type;
  106. u16 length;
  107. u8 command_code;
  108. size_t write_length;
  109. u8 *write_buf;
  110. u8 novalidate;
  111. u8 reset_expected;
  112. u16 timeout_ms;
  113. };
  114. #define SET_CMD_OPCODE(byte, opcode) SET_CMD_LOW(byte, opcode)
  115. #define SET_CMD_REPORT_TYPE(byte, type) SET_CMD_HIGH(byte, ((type) << 4))
  116. #define SET_CMD_REPORT_ID(byte, id) SET_CMD_LOW(byte, id)
  117. #define CREATE_PIP1_FW_CMD(command) \
  118. .cmd_type = PIP1_CMD_TYPE_FW, \
  119. .command_code = command
  120. #define CREATE_PIP1_BL_CMD(command) \
  121. .cmd_type = PIP1_CMD_TYPE_BL, \
  122. .command_code = command
  123. #define PT_MAX_PR_BUF_SIZE 2048
  124. /*******************************************************************************
  125. * FUNCTION: pt_pr_buf
  126. *
  127. * SUMMARY: Print out the contents of a buffer to kmsg based on the debug level
  128. *
  129. * RETURN: Void
  130. *
  131. * PARAMETERS:
  132. * *dev - pointer to Device structure
  133. * debug_level - requested debug level to print at
  134. * *buf - pointer to buffer to print
  135. * buf_len - size of buf
  136. * *data_name - Descriptive name of data prefixed to data
  137. ******************************************************************************/
  138. void pt_pr_buf(struct device *dev, u8 debug_level, u8 *buf,
  139. u16 buf_len, const char *data_name)
  140. {
  141. struct pt_core_data *cd = dev_get_drvdata(dev);
  142. int i;
  143. ssize_t pr_buf_index = 0;
  144. int max_size;
  145. /* only proceed if valid debug level and there is data to print */
  146. if (debug_level <= cd->debug_level && buf_len > 0) {
  147. char *pr_buf = kzalloc(PT_MAX_PR_BUF_SIZE, GFP_KERNEL);
  148. if (!pr_buf)
  149. return;
  150. /*
  151. * With a space each printed char takes 3 bytes, subtract
  152. * the length of the data_name prefix as well as 11 bytes
  153. * for the " [0..xxx]: " printed before the data.
  154. */
  155. max_size = (PT_MAX_PR_BUF_SIZE - sizeof(data_name) - 11) / 3;
  156. /* Ensure pr_buf_index stays within the 1018 size */
  157. pr_buf_index += scnprintf(pr_buf, PT_MAX_PR_BUF_SIZE, "%s [0..%d]: ",
  158. data_name);
  159. for (i = 0; i < buf_len && i < max_size; i++)
  160. pr_buf_index += scnprintf(pr_buf + pr_buf_index,
  161. PT_MAX_PR_BUF_SIZE, "%02X ", buf[i]);
  162. pt_debug(dev, debug_level, "%s\n", pr_buf);
  163. kfree(pr_buf);
  164. }
  165. }
  166. EXPORT_SYMBOL_GPL(pt_pr_buf);
  167. #ifdef TTHE_TUNER_SUPPORT
  168. /*******************************************************************************
  169. * FUNCTION: tthe_print
  170. *
  171. * SUMMARY: Format data name and time stamp as the header and format the
  172. * content of input buffer with hex base to "tthe_buf". And then wake up event
  173. * semaphore for tthe debugfs node.
  174. *
  175. * RETURN:
  176. * 0 = success
  177. * !0 = failure
  178. *
  179. * PARAMETERS:
  180. * *cd - pointer to core data
  181. * *buf - pointer to input buffer
  182. * buf_len - size of input buffer
  183. * *data_name - pointer to data name
  184. ******************************************************************************/
  185. static int tthe_print(struct pt_core_data *cd, u8 *buf, int buf_len,
  186. const u8 *data_name)
  187. {
  188. int name_len = strlen(data_name);
  189. int i, n;
  190. u8 *p;
  191. int remain;
  192. u8 data_name_with_time_stamp[100];
  193. /* Prepend timestamp, if requested, to data_name */
  194. if (cd->show_timestamp) {
  195. scnprintf(data_name_with_time_stamp,
  196. sizeof(data_name_with_time_stamp),
  197. "[%u] %s", pt_get_time_stamp(), data_name);
  198. data_name = data_name_with_time_stamp;
  199. name_len = strlen(data_name);
  200. }
  201. mutex_lock(&cd->tthe_lock);
  202. if (!cd->tthe_buf)
  203. goto exit;
  204. /* Add 1 due to the '\n' that is appended at the end */
  205. if (cd->tthe_buf_len + name_len + buf_len + 1 > cd->tthe_buf_size)
  206. goto exit;
  207. if (name_len + buf_len == 0)
  208. goto exit;
  209. remain = cd->tthe_buf_size - cd->tthe_buf_len;
  210. if (remain < name_len)
  211. name_len = remain;
  212. p = cd->tthe_buf + cd->tthe_buf_len;
  213. memcpy(p, data_name, name_len);
  214. cd->tthe_buf_len += name_len;
  215. p += name_len;
  216. remain -= name_len;
  217. *p = 0;
  218. for (i = 0; i < buf_len; i++) {
  219. n = scnprintf(p, remain, "%02X ", buf[i]);
  220. if (n <= 0)
  221. break;
  222. p += n;
  223. remain -= n;
  224. cd->tthe_buf_len += n;
  225. }
  226. n = scnprintf(p, remain, "\n");
  227. cd->tthe_buf_len += n;
  228. exit:
  229. wake_up(&cd->wait_q);
  230. mutex_unlock(&cd->tthe_lock);
  231. return 0;
  232. }
  233. /*******************************************************************************
  234. * FUNCTION: _pt_request_tthe_print
  235. *
  236. * SUMMARY: Function pointer included in core_cmds to allow other modules
  237. * to request to print data to the "tthe_buffer".
  238. *
  239. * RETURN:
  240. * 0 = success
  241. * !0 = failure
  242. *
  243. * PARAMETERS:
  244. * *dev - pointer to device structure
  245. ******************************************************************************/
  246. static int _pt_request_tthe_print(struct device *dev, u8 *buf,
  247. int buf_len, const u8 *data_name)
  248. {
  249. struct pt_core_data *cd = dev_get_drvdata(dev);
  250. return tthe_print(cd, buf, buf_len, data_name);
  251. }
  252. #endif
  253. /*******************************************************************************
  254. * FUNCTION: pt_platform_detect_read
  255. *
  256. * SUMMARY: To be passed to platform dectect function to perform a read
  257. * operation.
  258. *
  259. * RETURN:
  260. * 0 = success
  261. * !0 = failure
  262. *
  263. * PARAMETERS:
  264. * *dev - pointer to Device structure
  265. * *buf - pointer to buffer where the data read will be stored
  266. * size - size to be read
  267. ******************************************************************************/
  268. static int pt_platform_detect_read(struct device *dev, void *buf, int size)
  269. {
  270. struct pt_core_data *cd = dev_get_drvdata(dev);
  271. return pt_adap_read_default(cd, buf, size);
  272. }
  273. /*******************************************************************************
  274. * FUNCTION: pt_add_parameter
  275. *
  276. * SUMMARY: Adds a parameter that has been altered to the parameter linked list.
  277. * On every reset of the DUT this linked list is traversed and all
  278. * parameters in it are restored to the DUT.
  279. *
  280. * RETURN:
  281. * 0 = success
  282. * !0 = failure
  283. *
  284. * PARAMETERS:
  285. * *cd - pointer to core data
  286. * param_id - parameter ID to add
  287. * param_value - Value corresponding to the ID
  288. * param_size - Size of param_value
  289. ******************************************************************************/
  290. static int pt_add_parameter(struct pt_core_data *cd,
  291. u8 param_id, u32 param_value, u8 param_size)
  292. {
  293. struct param_node *param, *param_new;
  294. /* Check if parameter already exists in the list */
  295. spin_lock(&cd->spinlock);
  296. list_for_each_entry(param, &cd->param_list, node) {
  297. if (param->id == param_id) {
  298. /* Update parameter */
  299. param->value = param_value;
  300. pt_debug(cd->dev, DL_INFO,
  301. "%s: Update parameter id:%d value:%d size:%d\n",
  302. __func__, param_id, param_value, param_size);
  303. goto exit_unlock;
  304. }
  305. }
  306. spin_unlock(&cd->spinlock);
  307. param_new = kzalloc(sizeof(*param_new), GFP_KERNEL);
  308. if (!param_new)
  309. return -ENOMEM;
  310. param_new->id = param_id;
  311. param_new->value = param_value;
  312. param_new->size = param_size;
  313. pt_debug(cd->dev, DL_INFO,
  314. "%s: Add parameter id:%d value:%d size:%d\n",
  315. __func__, param_id, param_value, param_size);
  316. spin_lock(&cd->spinlock);
  317. list_add(&param_new->node, &cd->param_list);
  318. exit_unlock:
  319. spin_unlock(&cd->spinlock);
  320. return 0;
  321. }
  322. #ifdef TTDL_DIAGNOSTICS
  323. /*******************************************************************************
  324. * FUNCTION: pt_erase_parameter_list
  325. *
  326. * SUMMARY: Empty out the entire parameter linked list of all parameter/value
  327. * pairs. In some test cases this functionality is needed to ensure DUT
  328. * returns to a virgin state after a reset and no parameters are restored.
  329. *
  330. * RETURN:
  331. * 0 = success
  332. * !0 = failure
  333. *
  334. * PARAMETERS:
  335. * *cd - pointer to core data
  336. ******************************************************************************/
  337. static int pt_erase_parameter_list(struct pt_core_data *cd)
  338. {
  339. struct param_node *pos, *temp;
  340. spin_lock(&cd->spinlock);
  341. list_for_each_entry_safe(pos, temp, &cd->param_list, node) {
  342. pt_debug(cd->dev, DL_INFO,
  343. "%s: Parameter Restore List - remove 0x%02x\n",
  344. __func__, pos->id);
  345. list_del(&pos->node);
  346. kfree(pos);
  347. }
  348. spin_unlock(&cd->spinlock);
  349. return 0;
  350. }
  351. /*******************************************************************************
  352. * FUNCTION: pt_count_parameter_list
  353. *
  354. * SUMMARY: Count the items in the RAM parameter restor list
  355. *
  356. * RETURN:
  357. * 0 = success
  358. * !0 = failure
  359. *
  360. * PARAMETERS:
  361. * *cd - pointer to core data
  362. ******************************************************************************/
  363. static int pt_count_parameter_list(struct pt_core_data *cd)
  364. {
  365. struct param_node *pos, *temp;
  366. int entries = 0;
  367. spin_lock(&cd->spinlock);
  368. list_for_each_entry_safe(pos, temp, &cd->param_list, node)
  369. entries++;
  370. spin_unlock(&cd->spinlock);
  371. return entries;
  372. }
  373. #endif /* TTDL_DIAGNOSTICS */
  374. /*******************************************************************************
  375. * FUNCTION: request_exclusive
  376. *
  377. * SUMMARY: Request exclusive access to the DUT
  378. *
  379. * RETURN:
  380. * 0 = success
  381. * !0 = failure
  382. *
  383. * PARAMETERS:
  384. * *cd - pointer to core data
  385. * *ownptr - pointer to device
  386. * timeout_ms - Timeout value
  387. ******************************************************************************/
  388. int request_exclusive(struct pt_core_data *cd, void *ownptr,
  389. int timeout_ms)
  390. {
  391. int t = msecs_to_jiffies(timeout_ms);
  392. bool with_timeout = (timeout_ms != 0);
  393. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Request EXCLUSIVE t=%d\n",
  394. __func__, timeout_ms);
  395. mutex_lock(&cd->system_lock);
  396. if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
  397. cd->exclusive_dev = ownptr;
  398. goto exit;
  399. }
  400. cd->exclusive_waits++;
  401. wait:
  402. mutex_unlock(&cd->system_lock);
  403. if (with_timeout) {
  404. t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
  405. if (IS_TMO(t)) {
  406. pt_debug(cd->dev, DL_ERROR,
  407. "%s: tmo waiting exclusive access\n", __func__);
  408. return -ETIME;
  409. }
  410. } else {
  411. wait_event(cd->wait_q, !cd->exclusive_dev);
  412. }
  413. mutex_lock(&cd->system_lock);
  414. if (cd->exclusive_dev)
  415. goto wait;
  416. cd->exclusive_dev = ownptr;
  417. cd->exclusive_waits--;
  418. exit:
  419. mutex_unlock(&cd->system_lock);
  420. pt_debug(cd->dev, DL_DEBUG, "%s: request exclusive ok=%p\n",
  421. __func__, ownptr);
  422. return 0;
  423. }
  424. /*******************************************************************************
  425. * FUNCTION: release_exclusive_
  426. *
  427. * SUMMARY: Release exclusive access to the DUT
  428. *
  429. * RETURN:
  430. * 0 = success
  431. *
  432. * PARAMETERS:
  433. * *cd - pointer to core data
  434. * *ownptr - pointer to device
  435. ******************************************************************************/
  436. static int release_exclusive_(struct pt_core_data *cd, void *ownptr)
  437. {
  438. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Release EXCLUSIVE\n",
  439. __func__);
  440. if (cd->exclusive_dev != ownptr)
  441. return -EINVAL;
  442. pt_debug(cd->dev, DL_DEBUG, "%s: exclusive_dev %p freed\n",
  443. __func__, cd->exclusive_dev);
  444. cd->exclusive_dev = NULL;
  445. wake_up(&cd->wait_q);
  446. return 0;
  447. }
  448. /*******************************************************************************
  449. * FUNCTION: release_exclusive
  450. *
  451. * SUMMARY: Protected wrapper to release_exclusive_()
  452. *
  453. * RETURN:
  454. * 0 = success
  455. * !0 = failure
  456. *
  457. * PARAMETERS:
  458. * *cd - pointer to core data
  459. * *ownptr - pointer to device
  460. ******************************************************************************/
  461. int release_exclusive(struct pt_core_data *cd, void *ownptr)
  462. {
  463. int rc;
  464. mutex_lock(&cd->system_lock);
  465. rc = release_exclusive_(cd, ownptr);
  466. mutex_unlock(&cd->system_lock);
  467. return rc;
  468. }
  469. /*******************************************************************************
  470. * FUNCTION: pt_hid_exec_cmd_
  471. *
  472. * SUMMARY: Send the HID command to the DUT
  473. *
  474. * RETURN:
  475. * 0 = success
  476. * !0 = failure
  477. *
  478. * PARAMETERS:
  479. * *cd - pointer to core data
  480. * *hid_cmd - pointer to the HID command to send
  481. ******************************************************************************/
  482. static int pt_hid_exec_cmd_(struct pt_core_data *cd,
  483. struct pt_hid_cmd *hid_cmd)
  484. {
  485. int rc = 0;
  486. u8 *cmd;
  487. u16 cmd_length;
  488. u8 cmd_offset = 0;
  489. cmd_length = 2 /* command register */
  490. + 2 /* command */
  491. + (hid_cmd->report_id >= 0XF ? 1 : 0) /* Report ID */
  492. + (hid_cmd->has_data_register ? 2 : 0) /* Data register */
  493. + hid_cmd->write_length; /* Data length */
  494. cmd = kzalloc(cmd_length, GFP_KERNEL);
  495. if (!cmd)
  496. return -ENOMEM;
  497. /* Set Command register */
  498. memcpy(&cmd[cmd_offset], &cd->hid_desc.command_register,
  499. sizeof(cd->hid_desc.command_register));
  500. cmd_offset += sizeof(cd->hid_desc.command_register);
  501. /* Set Command */
  502. SET_CMD_REPORT_TYPE(cmd[cmd_offset], hid_cmd->report_type);
  503. if (hid_cmd->report_id >= 0XF)
  504. SET_CMD_REPORT_ID(cmd[cmd_offset], 0xF);
  505. else
  506. SET_CMD_REPORT_ID(cmd[cmd_offset], hid_cmd->report_id);
  507. cmd_offset++;
  508. SET_CMD_OPCODE(cmd[cmd_offset], hid_cmd->opcode);
  509. cmd_offset++;
  510. if (hid_cmd->report_id >= 0XF) {
  511. cmd[cmd_offset] = hid_cmd->report_id;
  512. cmd_offset++;
  513. }
  514. /* Set Data register */
  515. if (hid_cmd->has_data_register) {
  516. memcpy(&cmd[cmd_offset], &cd->hid_desc.data_register,
  517. sizeof(cd->hid_desc.data_register));
  518. cmd_offset += sizeof(cd->hid_desc.data_register);
  519. }
  520. /* Set Data */
  521. if (hid_cmd->write_length && hid_cmd->write_buf) {
  522. memcpy(&cmd[cmd_offset], hid_cmd->write_buf,
  523. hid_cmd->write_length);
  524. cmd_offset += hid_cmd->write_length;
  525. }
  526. pt_debug(cd->dev, DL_INFO,
  527. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  528. __func__, cmd_length, hid_cmd->report_id);
  529. pt_pr_buf(cd->dev, DL_DEBUG, cmd, cmd_length, ">>> CMD");
  530. rc = pt_adap_write_read_specific(cd, cmd_length, cmd,
  531. hid_cmd->read_buf);
  532. if (rc)
  533. pt_debug(cd->dev, DL_ERROR,
  534. "%s: Fail pt_adap_transfer\n", __func__);
  535. kfree(cmd);
  536. return rc;
  537. }
  538. #ifdef TTDL_DIAGNOSTICS
  539. /*******************************************************************************
  540. * FUNCTION: pt_toggle_err_gpio
  541. *
  542. * SUMMARY: Toggles the pre-defined error GPIO
  543. *
  544. * RETURN: n/a
  545. *
  546. * PARAMETERS:
  547. * *cd - pointer to core data
  548. * type - type of err that occurred
  549. ******************************************************************************/
  550. void pt_toggle_err_gpio(struct pt_core_data *cd, u8 type)
  551. {
  552. pt_debug(cd->dev, DL_DEBUG, "%s called with type = %d\n",
  553. __func__, type);
  554. if (cd->err_gpio && type == cd->err_gpio_type) {
  555. pt_debug(cd->dev, DL_WARN, "%s: Toggle ERR GPIO\n", __func__);
  556. gpio_direction_output(cd->err_gpio,
  557. !gpio_get_value(cd->err_gpio));
  558. }
  559. }
  560. /*******************************************************************************
  561. * FUNCTION: _pt_request_toggle_err_gpio
  562. *
  563. * SUMMARY: Function pointer included in core_cmds to allow other modules
  564. * to request to toggle the err_gpio
  565. *
  566. * RETURN: n/a
  567. *
  568. * PARAMETERS:
  569. * *cd - pointer to core data
  570. * type - type of err that occurred
  571. ******************************************************************************/
  572. void _pt_request_toggle_err_gpio(struct device *dev, u8 type)
  573. {
  574. struct pt_core_data *cd = dev_get_drvdata(dev);
  575. pt_toggle_err_gpio(cd, type);
  576. }
  577. #endif /* TTDL_DIAGNOSTICS */
  578. /*******************************************************************************
  579. * FUNCTION: pt_hid_exec_cmd_and_wait_
  580. *
  581. * SUMMARY: Send the HID command to the DUT and wait for the response
  582. *
  583. * RETURN:
  584. * 0 = success
  585. * !0 = failure
  586. *
  587. * PARAMETERS:
  588. * *cd - pointer to core data
  589. * *hid_cmd - pointer to the HID command to send
  590. ******************************************************************************/
  591. static int pt_hid_exec_cmd_and_wait_(struct pt_core_data *cd,
  592. struct pt_hid_cmd *hid_cmd)
  593. {
  594. int rc = 0;
  595. int t;
  596. u16 timeout_ms;
  597. int *cmd_state;
  598. if (hid_cmd->reset_cmd)
  599. cmd_state = &cd->hid_reset_cmd_state;
  600. else
  601. cmd_state = &cd->hid_cmd_state;
  602. if (hid_cmd->wait_interrupt) {
  603. mutex_lock(&cd->system_lock);
  604. *cmd_state = 1;
  605. mutex_unlock(&cd->system_lock);
  606. }
  607. rc = pt_hid_exec_cmd_(cd, hid_cmd);
  608. if (rc) {
  609. if (hid_cmd->wait_interrupt)
  610. goto error;
  611. goto exit;
  612. }
  613. if (!hid_cmd->wait_interrupt)
  614. goto exit;
  615. if (hid_cmd->timeout_ms)
  616. timeout_ms = hid_cmd->timeout_ms;
  617. else
  618. timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT;
  619. t = wait_event_timeout(cd->wait_q, (*cmd_state == 0),
  620. msecs_to_jiffies(timeout_ms));
  621. if (IS_TMO(t)) {
  622. #ifdef TTDL_DIAGNOSTICS
  623. cd->bus_transmit_error_count++;
  624. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  625. #endif /* TTDL_DIAGNOSTICS */
  626. pt_debug(cd->dev, DL_ERROR,
  627. "%s: HID output cmd execution timed out\n",
  628. __func__);
  629. rc = -ETIME;
  630. goto error;
  631. }
  632. goto exit;
  633. error:
  634. mutex_lock(&cd->system_lock);
  635. *cmd_state = 0;
  636. mutex_unlock(&cd->system_lock);
  637. exit:
  638. return rc;
  639. }
  640. /*******************************************************************************
  641. * FUNCTION: pt_hid_cmd_reset_
  642. *
  643. * SUMMARY: Send the HID RESET command to the DUT
  644. *
  645. * RETURN:
  646. * 0 = success
  647. * !0 = failure
  648. *
  649. * PARAMETERS:
  650. * *cd - pointer to core data
  651. ******************************************************************************/
  652. static int pt_hid_cmd_reset_(struct pt_core_data *cd)
  653. {
  654. struct pt_hid_cmd hid_cmd = {
  655. .opcode = HID_CMD_RESET,
  656. .wait_interrupt = 1,
  657. .reset_cmd = 1,
  658. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  659. };
  660. return pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  661. }
  662. /*******************************************************************************
  663. * FUNCTION: pt_hid_cmd_reset
  664. *
  665. * SUMMARY: Wrapper function for pt_hid_cmd_reset_ that guarantees exclusive
  666. * access.
  667. *
  668. * RETURN:
  669. * 0 = success
  670. * !0 = failure
  671. *
  672. * PARAMETERS:
  673. * *cd - pointer to core data
  674. ******************************************************************************/
  675. static int pt_hid_cmd_reset(struct pt_core_data *cd)
  676. {
  677. int rc = 0;
  678. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  679. if (rc < 0) {
  680. pt_debug(cd->dev, DL_ERROR,
  681. "%s: fail get exclusive ex=%p own=%p\n",
  682. __func__, cd->exclusive_dev, cd->dev);
  683. return rc;
  684. }
  685. pt_debug(cd->dev, DL_INFO, "%s: Send HID Reset command\n", __func__);
  686. rc = pt_hid_cmd_reset_(cd);
  687. if (release_exclusive(cd, cd->dev) < 0)
  688. pt_debug(cd->dev, DL_ERROR,
  689. "%s: fail to release exclusive\n", __func__);
  690. return rc;
  691. }
  692. /*******************************************************************************
  693. * FUNCTION: pt_hid_cmd_set_power_
  694. *
  695. * SUMMARY: Send hid cmd to set power state for the DUT and wait for response
  696. *
  697. * RETURN:
  698. * 0 = success
  699. * !0 = failure
  700. *
  701. * PARAMETERS:
  702. * *cd - pointer to core data
  703. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  704. ******************************************************************************/
  705. static int pt_hid_cmd_set_power_(struct pt_core_data *cd,
  706. u8 power_state)
  707. {
  708. int rc = 0;
  709. struct pt_hid_cmd hid_cmd = {
  710. .opcode = HID_CMD_SET_POWER,
  711. .wait_interrupt = 1,
  712. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  713. };
  714. hid_cmd.power_state = power_state;
  715. /* The chip won't give response if goes to Deep Standby */
  716. if (power_state == HID_POWER_STANDBY) {
  717. rc = pt_hid_exec_cmd_(cd, &hid_cmd);
  718. if (rc)
  719. pt_debug(cd->dev, DL_ERROR,
  720. "%s: Failed to set power to state:%d\n",
  721. __func__, power_state);
  722. else
  723. cd->fw_sys_mode_in_standby_state = true;
  724. return rc;
  725. }
  726. cd->fw_sys_mode_in_standby_state = false;
  727. rc = pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  728. if (rc) {
  729. pt_debug(cd->dev, DL_ERROR,
  730. "%s: Failed to set power to state:%d\n",
  731. __func__, power_state);
  732. return rc;
  733. }
  734. /* validate */
  735. if ((cd->response_buf[2] != HID_RESPONSE_REPORT_ID)
  736. || ((cd->response_buf[3] & 0x3) != power_state)
  737. || ((cd->response_buf[4] & 0xF) != HID_CMD_SET_POWER))
  738. rc = -EINVAL;
  739. return rc;
  740. }
  741. /*******************************************************************************
  742. * FUNCTION: pt_hid_cmd_set_power
  743. *
  744. * SUMMARY: Wrapper function for pt_hid_cmd_set_power_ that guarantees
  745. * exclusive access.
  746. *
  747. * RETURN:
  748. * 0 = success
  749. * !0 = failure
  750. *
  751. * PARAMETERS:
  752. * *cd - pointer to core data
  753. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  754. ******************************************************************************/
  755. static int pt_hid_cmd_set_power(struct pt_core_data *cd,
  756. u8 power_state)
  757. {
  758. int rc;
  759. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  760. if (rc < 0) {
  761. pt_debug(cd->dev, DL_ERROR,
  762. "%s: fail get exclusive ex=%p own=%p\n",
  763. __func__, cd->exclusive_dev, cd->dev);
  764. return rc;
  765. }
  766. rc = pt_hid_cmd_set_power_(cd, power_state);
  767. if (release_exclusive(cd, cd->dev) < 0)
  768. pt_debug(cd->dev, DL_ERROR,
  769. "%s: fail to release exclusive\n", __func__);
  770. return rc;
  771. }
  772. static const u16 crc_table[16] = {
  773. 0x0000, 0x1021, 0x2042, 0x3063,
  774. 0x4084, 0x50a5, 0x60c6, 0x70e7,
  775. 0x8108, 0x9129, 0xa14a, 0xb16b,
  776. 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  777. };
  778. /*******************************************************************************
  779. * FUNCTION: _pt_compute_crc
  780. *
  781. * SUMMARY: Calculate CRC by CRC table.
  782. *
  783. * RETURN:
  784. * CRC calculation result
  785. *
  786. * PARAMETERS:
  787. * *buf - pointer to the data array to be calculated
  788. * size - size of data array
  789. ******************************************************************************/
  790. static u16 _pt_compute_crc(u8 *buf, u32 size)
  791. {
  792. u16 remainder = 0xFFFF;
  793. u16 xor_mask = 0x0000;
  794. u32 index;
  795. u32 byte_value;
  796. u32 table_index;
  797. u32 crc_bit_width = sizeof(u16) * 8;
  798. /* Divide the message by polynomial, via the table. */
  799. for (index = 0; index < size; index++) {
  800. byte_value = buf[index];
  801. table_index = ((byte_value >> 4) & 0x0F)
  802. ^ (remainder >> (crc_bit_width - 4));
  803. remainder = crc_table[table_index] ^ (remainder << 4);
  804. table_index = (byte_value & 0x0F)
  805. ^ (remainder >> (crc_bit_width - 4));
  806. remainder = crc_table[table_index] ^ (remainder << 4);
  807. }
  808. /* Perform the final remainder CRC. */
  809. return remainder ^ xor_mask;
  810. }
  811. u16 ccitt_Table[] = {
  812. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  813. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  814. 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  815. 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  816. 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  817. 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  818. 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  819. 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  820. 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  821. 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  822. 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  823. 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  824. 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  825. 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  826. 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  827. 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  828. 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  829. 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  830. 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  831. 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  832. 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  833. 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  834. 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  835. 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  836. 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  837. 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  838. 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  839. 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  840. 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  841. 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  842. 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  843. 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0,
  844. };
  845. /*******************************************************************************
  846. * FUNCTION: crc_ccitt_calculate
  847. *
  848. * SUMMARY: Calculate CRC with ccitt standard by CRC table.
  849. *
  850. * RETURN:
  851. * CRC calculation result
  852. *
  853. * PARAMETERS:
  854. * *q - pointer to the data array to be calculated
  855. * len - size of data array
  856. ******************************************************************************/
  857. static unsigned short crc_ccitt_calculate(unsigned char *q, int len)
  858. {
  859. unsigned short crc = 0xffff;
  860. while (len-- > 0)
  861. crc = ccitt_Table[(crc >> 8 ^ *q++) & 0xff] ^ (crc << 8);
  862. return crc;
  863. }
  864. /*******************************************************************************
  865. * FUNCTION: pt_pip2_cmd_calculate_crc
  866. *
  867. * SUMMARY: Calculate the CRC of a command packet
  868. *
  869. * RETURN: void
  870. *
  871. * PARAMETERS:
  872. * *cmd - pointer to command data
  873. * extra_bytes - Extra bytes included in command length
  874. ******************************************************************************/
  875. static void pt_pip2_cmd_calculate_crc(struct pip2_cmd_structure *cmd,
  876. u8 extra_bytes)
  877. {
  878. u8 buf[PT_MAX_PIP2_MSG_SIZE + 1] = {0};
  879. unsigned short crc;
  880. buf[0] = cmd->len & 0xff;
  881. buf[1] = (cmd->len & 0xff00) >> 8;
  882. buf[2] = cmd->seq;
  883. buf[3] = cmd->id;
  884. memcpy(&buf[4], cmd->data, cmd->len - extra_bytes);
  885. /* Calculate the CRC for the first 4 bytes above and the data payload */
  886. crc = crc_ccitt_calculate(buf, 4 + (cmd->len - extra_bytes));
  887. cmd->crc[0] = (crc & 0xff00) >> 8;
  888. cmd->crc[1] = (crc & 0xff);
  889. }
  890. /*******************************************************************************
  891. * FUNCTION: pt_pip2_get_next_cmd_seq
  892. *
  893. * SUMMARY: Gets the next sequence number for a PIP2 command. The sequence
  894. * number is a 3 bit value (bits [0-2]) but because TTDL will always have
  895. * the TAG bit set (bit 3), the counter starts at 0x08 and goes to 0x0F.
  896. * If the "force_pip2_seq" holds a valid seq value (0x08-0x0F) then do not
  897. * increment, just use the forced value.
  898. *
  899. * RETURN: Next command sequence number [0x08-0x0F]
  900. *
  901. * PARAMETERS:
  902. * *cd - pointer to core data
  903. ******************************************************************************/
  904. static u8 pt_pip2_get_next_cmd_seq(struct pt_core_data *cd)
  905. {
  906. #ifdef TTDL_DIAGNOSTICS
  907. if (cd->force_pip2_seq <= 0x07) {
  908. cd->pip2_cmd_tag_seq++;
  909. if (cd->pip2_cmd_tag_seq > 0x0F)
  910. cd->pip2_cmd_tag_seq = 0x08;
  911. } else {
  912. cd->pip2_cmd_tag_seq = cd->force_pip2_seq;
  913. }
  914. #else
  915. cd->pip2_cmd_tag_seq++;
  916. if (cd->pip2_cmd_tag_seq > 0x0F)
  917. cd->pip2_cmd_tag_seq = 0x08;
  918. #endif
  919. return cd->pip2_cmd_tag_seq;
  920. }
  921. /*
  922. * Following macros are to define the response time (the interval between PIP2
  923. * command finishes sending and INT pin falls). The unit is in microsecond.
  924. * It has different time settings between the solution GPIO polling and Bus
  925. * polling due to the considration for system load.
  926. */
  927. #ifdef PT_POLL_RESP_BY_BUS
  928. #define POLL_RETRY_DEFAULT_INTERVAL 50
  929. #define PIP2_RESP_DEFAULT_TIME_MIN 50
  930. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  931. #define PIP2_RESP_FILE_WRITE_TIME_MIN 220
  932. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  933. #else
  934. #define POLL_RETRY_DEFAULT_INTERVAL 20
  935. #define PIP2_RESP_DEFAULT_TIME_MIN 20
  936. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  937. #define PIP2_RESP_FILE_WRITE_TIME_MIN 20
  938. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  939. #endif
  940. /*
  941. * id: the command id defined in PIP2
  942. * response_len: the (maximum) length of response.
  943. * response_time_min: minimum response time in microsecond
  944. * response_time_max: maximum response time in microsecond
  945. */
  946. static const struct pip2_cmd_response_structure pip2_cmd_response[] = {
  947. {.id = PIP2_CMD_ID_PING,
  948. .response_len = 255,
  949. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  950. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  951. {.id = PIP2_CMD_ID_STATUS,
  952. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  953. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  954. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  955. {.id = PIP2_CMD_ID_CTRL,
  956. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  957. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  958. .response_time_max = PT_PIP2_CMD_FILE_ERASE_TIMEOUT},
  959. {.id = PIP2_CMD_ID_CONFIG,
  960. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  961. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  962. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  963. {.id = PIP2_CMD_ID_CLEAR,
  964. .response_len = PIP2_EXTRA_BYTES_NUM + 0,
  965. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  966. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  967. {.id = PIP2_CMD_ID_RESET,
  968. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  969. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  970. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  971. {.id = PIP2_CMD_ID_VERSION,
  972. .response_len = PIP2_EXTRA_BYTES_NUM + 23,
  973. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  974. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  975. {.id = PIP2_CMD_ID_FILE_OPEN,
  976. .response_len = PIP2_EXTRA_BYTES_NUM + 2,
  977. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  978. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  979. {.id = PIP2_CMD_ID_FILE_CLOSE,
  980. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  981. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  982. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  983. {.id = PIP2_CMD_ID_FILE_READ,
  984. .response_len = 255,
  985. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  986. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  987. {.id = PIP2_CMD_ID_FILE_WRITE,
  988. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  989. .response_time_min = PIP2_RESP_FILE_WRITE_TIME_MIN,
  990. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  991. {.id = PIP2_CMD_ID_FILE_IOCTL,
  992. .response_len = PIP2_EXTRA_BYTES_NUM + 10,
  993. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  994. .response_time_max = PIP2_RESP_FILE_IOCTL_TIME_MAX},
  995. {.id = PIP2_CMD_ID_FLASH_INFO,
  996. .response_len = PIP2_EXTRA_BYTES_NUM + 17,
  997. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  998. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  999. {.id = PIP2_CMD_ID_EXECUTE,
  1000. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1001. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1002. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1003. {.id = PIP2_CMD_ID_GET_LAST_ERRNO,
  1004. .response_len = PIP2_EXTRA_BYTES_NUM + 3,
  1005. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1006. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1007. {.id = PIP2_CMD_ID_EXIT_HOST_MODE,
  1008. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1009. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1010. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1011. {.id = PIP2_CMD_ID_READ_GPIO,
  1012. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  1013. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1014. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1015. {.id = PIP2_CMD_EXECUTE_SCAN,
  1016. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1017. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1018. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1019. {.id = PIP2_CMD_SET_PARAMETER,
  1020. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1021. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1022. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1023. {.id = PIP2_CMD_GET_PARAMETER,
  1024. .response_len = PIP2_EXTRA_BYTES_NUM + 7,
  1025. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1026. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1027. {.id = PIP2_CMD_SET_DDI_REG,
  1028. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1029. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1030. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1031. {.id = PIP2_CMD_GET_DDI_REG,
  1032. .response_len = PIP2_EXTRA_BYTES_NUM + 249,
  1033. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1034. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1035. {.id = PIP2_CMD_ID_END,
  1036. .response_len = 255,
  1037. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1038. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX}
  1039. };
  1040. /*******************************************************************************
  1041. * FUNCTION: pt_pip2_get_cmd_response_len
  1042. *
  1043. * SUMMARY: Gets the expected response length based on the command ID
  1044. *
  1045. * RETURN: Expected response length
  1046. *
  1047. * PARAMETERS:
  1048. * id - Command ID (-1 means input ID is not in list of PIP2 command)
  1049. ******************************************************************************/
  1050. static int pt_pip2_get_cmd_response_len(u8 id)
  1051. {
  1052. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1053. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1054. p++;
  1055. if (p->id != PIP2_CMD_ID_END)
  1056. return p->response_len;
  1057. else
  1058. return -EPERM;
  1059. }
  1060. /*******************************************************************************
  1061. * FUNCTION: pt_pip2_get_cmd_resp_time_min
  1062. *
  1063. * SUMMARY: Gets the minimum response time (the interval between PIP2 command
  1064. * finishes sending and INT pin falls) based on the command ID
  1065. *
  1066. * RETURN: Estimated minimum response time in microsecond
  1067. *
  1068. * PARAMETERS:
  1069. * id - Command ID
  1070. ******************************************************************************/
  1071. static u32 pt_pip2_get_cmd_resp_time_min(u8 id)
  1072. {
  1073. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1074. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1075. p++;
  1076. if (p->id != PIP2_CMD_ID_END)
  1077. return p->response_time_min;
  1078. else
  1079. return PIP2_RESP_DEFAULT_TIME_MIN;
  1080. }
  1081. /*******************************************************************************
  1082. * FUNCTION: pt_pip2_get_cmd_resp_time_max
  1083. *
  1084. * SUMMARY: Gets the maximum response time (the interval between PIP2 command
  1085. * finishes sending and INT pin falls) based on the command ID
  1086. *
  1087. * RETURN: Estimated maximum response time in microsecond
  1088. *
  1089. * PARAMETERS:
  1090. * id - Command ID
  1091. ******************************************************************************/
  1092. static u32 pt_pip2_get_cmd_resp_time_max(u8 id)
  1093. {
  1094. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1095. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1096. p++;
  1097. if (p->id != PIP2_CMD_ID_END)
  1098. return p->response_time_max;
  1099. else
  1100. return PIP2_RESP_DEFAULT_TIME_MAX;
  1101. }
  1102. /*******************************************************************************
  1103. * FUNCTION: pt_pip2_validate_response
  1104. *
  1105. * SUMMARY: Validate the response of PIP2 command.
  1106. *
  1107. * RETURN:
  1108. * 0 = success
  1109. * !0 = failure
  1110. *
  1111. * PARAMETERS:
  1112. * *cd - pointer to core data
  1113. * *pip2_cmd - pointer to PIP2 command to send
  1114. * *read_buf - pointer to response buffer
  1115. * actual_read_len - actual read length of the response
  1116. ******************************************************************************/
  1117. static int pt_pip2_validate_response(struct pt_core_data *cd,
  1118. struct pip2_cmd_structure *pip2_cmd, u8 *read_buf,
  1119. u16 actual_read_len)
  1120. {
  1121. int rc = 0;
  1122. u8 response_seq = 0;
  1123. u8 reserved_bits = 0;
  1124. u8 cmd_id = 0;
  1125. u8 response_bit = 0;
  1126. unsigned short calc_crc = 0;
  1127. unsigned short resp_crc = 0;
  1128. /* Verify the length of response buffer */
  1129. if (actual_read_len < PT_MIN_PIP2_PACKET_SIZE) {
  1130. pt_debug(cd->dev, DL_ERROR,
  1131. "%s cmd[0x%02X] read length ERR: read_len = %d\n",
  1132. __func__, pip2_cmd->id, actual_read_len);
  1133. rc = -EINVAL;
  1134. goto exit;
  1135. }
  1136. /* Verify the CRC */
  1137. calc_crc = crc_ccitt_calculate(read_buf, actual_read_len - 2);
  1138. resp_crc = read_buf[actual_read_len - 2] << 8;
  1139. resp_crc |= read_buf[actual_read_len - 1];
  1140. if (resp_crc != calc_crc) {
  1141. pt_debug(cd->dev, DL_ERROR,
  1142. "%s: cmd[0x%02X] CRC ERR: calc=0x%04X rsp=0x%04X\n",
  1143. __func__, pip2_cmd->id, calc_crc, resp_crc);
  1144. #ifdef TTDL_DIAGNOSTICS
  1145. cd->pip2_crc_error_count++;
  1146. #endif /* TTDL_DIAGNOSTICS */
  1147. rc = -EINVAL;
  1148. goto exit;
  1149. }
  1150. /* Verify the response bit is set */
  1151. response_bit = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x80;
  1152. if (!response_bit) {
  1153. pt_debug(cd->dev, DL_ERROR,
  1154. "%s cmd[0x%02X] response bit ERR: response_bit = %d\n",
  1155. __func__, pip2_cmd->id, response_bit);
  1156. rc = -EINVAL;
  1157. goto exit;
  1158. }
  1159. /* Verify the command ID matches from command to response */
  1160. cmd_id = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x7F;
  1161. if (cmd_id != pip2_cmd->id) {
  1162. pt_debug(cd->dev, DL_ERROR,
  1163. "%s cmd[0x%02X] command ID ERR: cmd_id = 0x%02X\n",
  1164. __func__, pip2_cmd->id, cmd_id);
  1165. rc = -EINVAL;
  1166. goto exit;
  1167. }
  1168. /* Verify the SEQ number matches from command to response */
  1169. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F;
  1170. if ((pip2_cmd->seq & 0x0F) != response_seq) {
  1171. pt_debug(cd->dev, DL_ERROR,
  1172. "%s cmd[0x%02X] send_seq = 0x%02X, resp_seq = 0x%02X\n",
  1173. __func__, pip2_cmd->id,
  1174. pip2_cmd->seq, response_seq);
  1175. rc = -EINVAL;
  1176. goto exit;
  1177. }
  1178. /* Verify the reserved bits are 0 */
  1179. reserved_bits = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0xF0;
  1180. if (reserved_bits)
  1181. pt_debug(cd->dev, DL_WARN,
  1182. "%s cmd[0x%02X] reserved_bits = 0x%02X\n",
  1183. __func__, pip2_cmd->id, reserved_bits);
  1184. exit:
  1185. if (rc)
  1186. pt_pr_buf(cd->dev, DL_WARN, cd->input_buf, actual_read_len,
  1187. "PIP RSP:");
  1188. return rc;
  1189. }
  1190. /*******************************************************************************
  1191. * FUNCTION: pt_hid_output_validate_bl_response
  1192. *
  1193. * SUMMARY: Validate the response of bootloader command.
  1194. *
  1195. * RETURN:
  1196. * 0 = success
  1197. * !0 = failure
  1198. *
  1199. * PARAMETERS:
  1200. * *cd - pointer to core data
  1201. * *hid_output - pointer to hid output data structure
  1202. ******************************************************************************/
  1203. static int pt_hid_output_validate_bl_response(
  1204. struct pt_core_data *cd,
  1205. struct pt_hid_output *hid_output)
  1206. {
  1207. u16 size;
  1208. u16 crc;
  1209. u8 status;
  1210. size = get_unaligned_le16(&cd->response_buf[0]);
  1211. if (hid_output->reset_expected && !size)
  1212. return 0;
  1213. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1214. != PT_PIP_BL_RESPONSE_REPORT_ID) {
  1215. pt_debug(cd->dev, DL_ERROR,
  1216. "%s: BL output response, wrong report_id\n", __func__);
  1217. return -EPROTO;
  1218. }
  1219. if (cd->response_buf[4] != PIP1_BL_SOP) {
  1220. pt_debug(cd->dev, DL_ERROR,
  1221. "%s: BL output response, wrong SOP\n", __func__);
  1222. return -EPROTO;
  1223. }
  1224. if (cd->response_buf[size - 1] != PIP1_BL_EOP) {
  1225. pt_debug(cd->dev, DL_ERROR,
  1226. "%s: BL output response, wrong EOP\n", __func__);
  1227. return -EPROTO;
  1228. }
  1229. crc = _pt_compute_crc(&cd->response_buf[4], size - 7);
  1230. if (cd->response_buf[size - 3] != LOW_BYTE(crc)
  1231. || cd->response_buf[size - 2] != HI_BYTE(crc)) {
  1232. pt_debug(cd->dev, DL_ERROR,
  1233. "%s: BL output response, wrong CRC 0x%X\n",
  1234. __func__, crc);
  1235. return -EPROTO;
  1236. }
  1237. status = cd->response_buf[5];
  1238. if (status) {
  1239. pt_debug(cd->dev, DL_ERROR,
  1240. "%s: BL output response, ERROR:%d\n",
  1241. __func__, status);
  1242. return -EPROTO;
  1243. }
  1244. return 0;
  1245. }
  1246. /*******************************************************************************
  1247. * FUNCTION: pt_hid_output_validate_app_response
  1248. *
  1249. * SUMMARY: Validate the response of application command.
  1250. *
  1251. * RETURN:
  1252. * 0 = success
  1253. * !0 = failure
  1254. *
  1255. * PARAMETERS:
  1256. * *cd - pointer to core data
  1257. * *hid_output - pointer to hid output data structure
  1258. ******************************************************************************/
  1259. static int pt_hid_output_validate_app_response(
  1260. struct pt_core_data *cd,
  1261. struct pt_hid_output *hid_output)
  1262. {
  1263. int command_code;
  1264. u16 size;
  1265. size = get_unaligned_le16(&cd->response_buf[0]);
  1266. if (hid_output->reset_expected && !size)
  1267. return 0;
  1268. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1269. != PT_PIP_NON_HID_RESPONSE_ID) {
  1270. pt_debug(cd->dev, DL_ERROR,
  1271. "%s: APP output response, wrong report_id\n", __func__);
  1272. return -EPROTO;
  1273. }
  1274. command_code = cd->response_buf[PIP1_RESP_COMMAND_ID_OFFSET]
  1275. & PIP1_RESP_COMMAND_ID_MASK;
  1276. if (command_code != hid_output->command_code) {
  1277. pt_debug(cd->dev, DL_ERROR,
  1278. "%s: APP output response, wrong command_code:%X\n",
  1279. __func__, command_code);
  1280. return -EPROTO;
  1281. }
  1282. return 0;
  1283. }
  1284. /*******************************************************************************
  1285. * FUNCTION: pt_check_set_parameter
  1286. *
  1287. * SUMMARY: Check command input and response for Set Parameter command.And
  1288. * store the parameter to the list for resume work if pass the check.
  1289. *
  1290. * PARAMETERS:
  1291. * *cd - pointer to core data
  1292. * *hid_output - pointer to hid output data structure
  1293. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1294. ******************************************************************************/
  1295. static void pt_check_set_parameter(struct pt_core_data *cd,
  1296. struct pt_hid_output *hid_output, bool raw)
  1297. {
  1298. u8 *param_buf;
  1299. u32 param_value = 0;
  1300. u8 param_size;
  1301. u8 param_id;
  1302. int i = 0;
  1303. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  1304. return;
  1305. /* Check command input for Set Parameter command */
  1306. if (raw && hid_output->length >= 10 && hid_output->length <= 13
  1307. && !memcmp(&hid_output->write_buf[0],
  1308. &cd->hid_desc.output_register,
  1309. sizeof(cd->hid_desc.output_register))
  1310. && hid_output->write_buf[4] ==
  1311. PT_PIP_NON_HID_COMMAND_ID
  1312. && hid_output->write_buf[6] ==
  1313. PIP1_CMD_ID_SET_PARAM)
  1314. param_buf = &hid_output->write_buf[7];
  1315. else if (!raw && hid_output->cmd_type == PIP1_CMD_TYPE_FW
  1316. && hid_output->command_code == PIP1_CMD_ID_SET_PARAM
  1317. && hid_output->write_length >= 3
  1318. && hid_output->write_length <= 6)
  1319. param_buf = &hid_output->write_buf[0];
  1320. else
  1321. return;
  1322. /* Get parameter ID, size and value */
  1323. param_id = param_buf[0];
  1324. param_size = param_buf[1];
  1325. if (param_size > 4) {
  1326. pt_debug(cd->dev, DL_ERROR,
  1327. "%s: Invalid parameter size\n", __func__);
  1328. return;
  1329. }
  1330. param_buf = &param_buf[2];
  1331. while (i < param_size)
  1332. param_value += *(param_buf++) << (8 * i++);
  1333. /* Check command response for Set Parameter command */
  1334. if (cd->response_buf[2] != PT_PIP_NON_HID_RESPONSE_ID
  1335. || (cd->response_buf[4] &
  1336. PIP1_RESP_COMMAND_ID_MASK) !=
  1337. PIP1_CMD_ID_SET_PARAM
  1338. || cd->response_buf[5] != param_id
  1339. || cd->response_buf[6] != param_size) {
  1340. pt_debug(cd->dev, DL_ERROR,
  1341. "%s: Set Parameter command not successful\n",
  1342. __func__);
  1343. return;
  1344. }
  1345. pt_add_parameter(cd, param_id, param_value, param_size);
  1346. }
  1347. /*******************************************************************************
  1348. * FUNCTION: pt_check_command
  1349. *
  1350. * SUMMARY: Check the output command. The function pt_check_set_parameter() is
  1351. * called here to check output command and store parameter to the list.
  1352. *
  1353. * PARAMETERS:
  1354. * *cd - pointer to core data
  1355. * *hid_output - pointer to hid output data structure
  1356. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1357. ******************************************************************************/
  1358. static void pt_check_command(struct pt_core_data *cd,
  1359. struct pt_hid_output *hid_output, bool raw)
  1360. {
  1361. pt_check_set_parameter(cd, hid_output, raw);
  1362. }
  1363. /*******************************************************************************
  1364. * FUNCTION: pt_hid_output_validate_response
  1365. *
  1366. * SUMMARY: Validate the response of application or bootloader command.
  1367. *
  1368. * RETURN:
  1369. * 0 = success
  1370. * !0 = failure
  1371. *
  1372. * PARAMETERS:
  1373. * *cd - pointer to core data
  1374. * *hid_output - pointer to hid output data structure
  1375. ******************************************************************************/
  1376. static int pt_hid_output_validate_response(struct pt_core_data *cd,
  1377. struct pt_hid_output *hid_output)
  1378. {
  1379. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1380. return pt_hid_output_validate_bl_response(cd, hid_output);
  1381. return pt_hid_output_validate_app_response(cd, hid_output);
  1382. }
  1383. /*******************************************************************************
  1384. * FUNCTION: pt_hid_send_output_user_
  1385. *
  1386. * SUMMARY: Blindly send user data to the DUT.
  1387. *
  1388. * RETURN:
  1389. * 0 = success
  1390. * !0 = failure
  1391. *
  1392. * PARAMETERS:
  1393. * *cd - pointer to core data
  1394. * *hid_output - pointer to the command to send
  1395. ******************************************************************************/
  1396. static int pt_hid_send_output_user_(struct pt_core_data *cd,
  1397. struct pt_hid_output *hid_output)
  1398. {
  1399. int rc = 0;
  1400. int cmd;
  1401. if (!hid_output->length || !hid_output->write_buf)
  1402. return -EINVAL;
  1403. if (cd->pip2_prot_active) {
  1404. cmd = hid_output->write_buf[PIP2_CMD_COMMAND_ID_OFFSET];
  1405. cmd &= PIP2_CMD_COMMAND_ID_MASK;
  1406. } else
  1407. cmd = hid_output->write_buf[PIP1_CMD_COMMAND_ID_OFFSET];
  1408. pt_debug(cd->dev, DL_INFO,
  1409. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1410. __func__, hid_output->length, cmd);
  1411. pt_pr_buf(cd->dev, DL_DEBUG, hid_output->write_buf,
  1412. hid_output->length, ">>> User CMD");
  1413. rc = pt_adap_write_read_specific(cd, hid_output->length,
  1414. hid_output->write_buf, NULL);
  1415. if (rc)
  1416. pt_debug(cd->dev, DL_ERROR,
  1417. "%s: Fail pt_adap_transfer\n", __func__);
  1418. return rc;
  1419. }
  1420. /*******************************************************************************
  1421. * FUNCTION: pt_hid_send_output_user_and_wait_
  1422. *
  1423. * SUMMARY: Blindly send user data to the DUT and wait for the response.
  1424. *
  1425. * RETURN:
  1426. * 0 = success
  1427. * !0 = failure
  1428. *
  1429. * PARAMETERS:
  1430. * *cd - pointer to core data
  1431. * *hid_output - pointer to the command to send
  1432. ******************************************************************************/
  1433. static int pt_hid_send_output_user_and_wait_(struct pt_core_data *cd,
  1434. struct pt_hid_output *hid_output)
  1435. {
  1436. int rc = 0;
  1437. int t;
  1438. mutex_lock(&cd->system_lock);
  1439. cd->hid_cmd_state = PIP1_CMD_ID_USER_CMD + 1;
  1440. mutex_unlock(&cd->system_lock);
  1441. rc = pt_hid_send_output_user_(cd, hid_output);
  1442. if (rc)
  1443. goto error;
  1444. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1445. msecs_to_jiffies(cd->pip_cmd_timeout));
  1446. if (IS_TMO(t)) {
  1447. #ifdef TTDL_DIAGNOSTICS
  1448. cd->bus_transmit_error_count++;
  1449. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1450. #endif /* TTDL_DIAGNOSTICS */
  1451. pt_debug(cd->dev, DL_ERROR,
  1452. "%s: HID output cmd execution timed out\n",
  1453. __func__);
  1454. rc = -ETIME;
  1455. goto error;
  1456. }
  1457. pt_check_command(cd, hid_output, true);
  1458. goto exit;
  1459. error:
  1460. mutex_lock(&cd->system_lock);
  1461. cd->hid_cmd_state = 0;
  1462. mutex_unlock(&cd->system_lock);
  1463. exit:
  1464. return rc;
  1465. }
  1466. /*******************************************************************************
  1467. * FUNCTION: pt_check_irq_asserted
  1468. *
  1469. * SUMMARY: Checks if the IRQ GPIO is asserted or not. There are times when
  1470. * the FW can hold the INT line low ~150us after the read is complete.
  1471. * NOTE: if irq_stat is not defined this function will return false
  1472. *
  1473. * RETURN:
  1474. * true = IRQ asserted
  1475. * false = IRQ not asserted
  1476. *
  1477. * PARAMETERS:
  1478. * *cd - pointer to core data
  1479. ******************************************************************************/
  1480. static bool pt_check_irq_asserted(struct pt_core_data *cd)
  1481. {
  1482. #ifdef ENABLE_WORKAROUND_FOR_GLITCH_AFTER_BL_LAUNCH_APP
  1483. /*
  1484. * Workaround for FW defect, CDT165308
  1485. * bl_launch app creates a glitch in IRQ line
  1486. */
  1487. if (cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1
  1488. && cd->cpdata->irq_stat) {
  1489. /*
  1490. * in X1S panel and GC1546 panel, the width for the INT
  1491. * glitch is about 4us,the normal INT width of response
  1492. * will last more than 200us, so use 10us delay
  1493. * for distinguish the glitch the normal INT is enough.
  1494. */
  1495. udelay(10);
  1496. }
  1497. #endif
  1498. if (cd->cpdata->irq_stat) {
  1499. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1500. == PT_IRQ_ASSERTED_VALUE) {
  1501. /* Debounce to allow FW to release INT */
  1502. usleep_range(100, 200);
  1503. }
  1504. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1505. == PT_IRQ_ASSERTED_VALUE)
  1506. return true;
  1507. else
  1508. return false;
  1509. }
  1510. return true;
  1511. }
  1512. /*******************************************************************************
  1513. * FUNCTION: pt_flush_bus
  1514. *
  1515. * SUMMARY: Force flushing the bus by reading len bytes or forced 255 bytes
  1516. * Used if IRQ is found to be stuck low
  1517. *
  1518. * RETURN: Length of bytes read from bus
  1519. *
  1520. * PARAMETERS:
  1521. * *cd - pointer to core data
  1522. * flush_type - type of flush
  1523. * - PT_FLUSH_BUS_BASED_ON_LEN (two reads)
  1524. * - PT_FLUSH_BUS_FULL_256_READ
  1525. * *read_buf - pointer to store read data
  1526. ******************************************************************************/
  1527. static ssize_t pt_flush_bus(struct pt_core_data *cd,
  1528. u8 flush_type, u8 *read_buf)
  1529. {
  1530. u8 buf[PT_MAX_PIP2_MSG_SIZE];
  1531. u16 pip_len;
  1532. int bytes_read;
  1533. int rc = 0;
  1534. if (flush_type == PT_FLUSH_BUS_BASED_ON_LEN) {
  1535. rc = pt_adap_read_default(cd, buf, 2);
  1536. if (rc) {
  1537. bytes_read = 0;
  1538. goto exit;
  1539. }
  1540. pip_len = get_unaligned_le16(&buf[0]);
  1541. if (pip_len == 2 || pip_len >= PT_PIP_1P7_EMPTY_BUF) {
  1542. #ifdef TTDL_DIAGNOSTICS
  1543. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EMPTY_PACKET);
  1544. #endif
  1545. bytes_read = 2;
  1546. pt_debug(cd->dev, DL_INFO,
  1547. "%s: Empty buf detected - len=0x%04X\n",
  1548. __func__, pip_len);
  1549. } else if (pip_len == 0) {
  1550. bytes_read = 0;
  1551. pt_debug(cd->dev, DL_INFO,
  1552. "%s: Sentinel detected\n", __func__);
  1553. } else if (pip_len > PT_MAX_PIP2_MSG_SIZE) {
  1554. pt_debug(cd->dev, DL_ERROR,
  1555. "%s: Illegal len=0x%04x, force %d byte read\n",
  1556. __func__, pip_len, PT_MAX_PIP2_MSG_SIZE);
  1557. rc = pt_adap_read_default(cd, buf,
  1558. PT_MAX_PIP2_MSG_SIZE);
  1559. if (!rc)
  1560. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1561. else
  1562. bytes_read = 0;
  1563. } else {
  1564. pt_debug(cd->dev, DL_INFO,
  1565. "%s: Flush read of %d bytes...\n",
  1566. __func__, pip_len);
  1567. rc = pt_adap_read_default(cd, buf, pip_len);
  1568. if (!rc)
  1569. bytes_read = pip_len;
  1570. else
  1571. bytes_read = 0;
  1572. }
  1573. } else {
  1574. pt_debug(cd->dev, DL_INFO,
  1575. "%s: Forced flush of max %d bytes...\n",
  1576. __func__, PT_MAX_PIP2_MSG_SIZE);
  1577. rc = pt_adap_read_default(cd, buf, PT_MAX_PIP2_MSG_SIZE);
  1578. if (!rc)
  1579. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1580. else
  1581. bytes_read = 0;
  1582. }
  1583. if (read_buf && (bytes_read > 3))
  1584. memcpy(read_buf, buf, bytes_read);
  1585. exit:
  1586. return bytes_read;
  1587. }
  1588. /*******************************************************************************
  1589. * FUNCTION: pt_flush_bus_if_irq_asserted
  1590. *
  1591. * SUMMARY: This function will flush the active bus if the INT is found to be
  1592. * asserted.
  1593. *
  1594. * RETURN: bytes cleared from bus
  1595. *
  1596. * PARAMETERS:
  1597. * *cd - pointer the core data structure
  1598. * flush_type - type of flush
  1599. * - PT_FLUSH_BUS_BASED_ON_LEN
  1600. * - PT_FLUSH_BUS_FULL_256_READ
  1601. ******************************************************************************/
  1602. static int pt_flush_bus_if_irq_asserted(struct pt_core_data *cd, u8 flush_type)
  1603. {
  1604. int count = 0;
  1605. int bytes_read = 0;
  1606. while (pt_check_irq_asserted(cd) && count < 5) {
  1607. count++;
  1608. bytes_read = pt_flush_bus(cd, flush_type, NULL);
  1609. if (bytes_read) {
  1610. pt_debug(cd->dev, DL_WARN,
  1611. "%s: Cleared %d bytes off bus\n",
  1612. __func__, bytes_read);
  1613. }
  1614. }
  1615. if (pt_check_irq_asserted(cd)) {
  1616. pt_debug(cd->dev, DL_ERROR,
  1617. "%s: IRQ still asserted, %d bytes read\n",
  1618. __func__, bytes_read);
  1619. } else {
  1620. pt_debug(cd->dev, DL_INFO,
  1621. "%s: IRQ cleared, %d bytes read\n",
  1622. __func__, bytes_read);
  1623. }
  1624. return bytes_read;
  1625. }
  1626. /*******************************************************************************
  1627. * FUNCTION: pt_hid_send_output_
  1628. *
  1629. * SUMMARY: Send a touch application command to the DUT
  1630. *
  1631. * RETURN:
  1632. * 0 = success
  1633. * !0 = failure
  1634. *
  1635. * PARAMETERS:
  1636. * *cd - pointer to core data
  1637. * *hid_output - pointer to the command to send
  1638. ******************************************************************************/
  1639. static int pt_hid_send_output_(struct pt_core_data *cd,
  1640. struct pt_hid_output *hid_output)
  1641. {
  1642. int rc = 0;
  1643. u8 *cmd;
  1644. u16 length;
  1645. u16 crc;
  1646. u8 report_id;
  1647. u8 cmd_offset = 0;
  1648. u8 cmd_allocated = 0;
  1649. switch (hid_output->cmd_type) {
  1650. case PIP1_CMD_TYPE_FW:
  1651. report_id = PT_PIP_NON_HID_COMMAND_ID;
  1652. length = 5;
  1653. break;
  1654. case PIP1_CMD_TYPE_BL:
  1655. report_id = PT_PIP_BL_COMMAND_REPORT_ID;
  1656. length = 11 /* 5 + SOP + LEN(2) + CRC(2) + EOP */;
  1657. break;
  1658. default:
  1659. return -EINVAL;
  1660. }
  1661. length += hid_output->write_length;
  1662. if (length + 2 > PT_PREALLOCATED_CMD_BUFFER) {
  1663. cmd = kzalloc(length + 2, GFP_KERNEL);
  1664. if (!cmd)
  1665. return -ENOMEM;
  1666. cmd_allocated = 1;
  1667. } else {
  1668. cmd = cd->cmd_buf;
  1669. }
  1670. /* Set Output register */
  1671. memcpy(&cmd[cmd_offset], &cd->hid_desc.output_register,
  1672. sizeof(cd->hid_desc.output_register));
  1673. cmd_offset += sizeof(cd->hid_desc.output_register);
  1674. cmd[cmd_offset++] = LOW_BYTE(length);
  1675. cmd[cmd_offset++] = HI_BYTE(length);
  1676. cmd[cmd_offset++] = report_id;
  1677. cmd[cmd_offset++] = 0x0; /* reserved */
  1678. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1679. cmd[cmd_offset++] = PIP1_BL_SOP;
  1680. cmd[cmd_offset++] = hid_output->command_code;
  1681. /* Set Data Length for bootloader */
  1682. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1683. cmd[cmd_offset++] = LOW_BYTE(hid_output->write_length);
  1684. cmd[cmd_offset++] = HI_BYTE(hid_output->write_length);
  1685. }
  1686. /* Set Data */
  1687. if (hid_output->write_length && hid_output->write_buf) {
  1688. memcpy(&cmd[cmd_offset], hid_output->write_buf,
  1689. hid_output->write_length);
  1690. cmd_offset += hid_output->write_length;
  1691. }
  1692. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1693. crc = _pt_compute_crc(&cmd[6],
  1694. hid_output->write_length + 4);
  1695. cmd[cmd_offset++] = LOW_BYTE(crc);
  1696. cmd[cmd_offset++] = HI_BYTE(crc);
  1697. cmd[cmd_offset++] = PIP1_BL_EOP;
  1698. }
  1699. pt_debug(cd->dev, DL_INFO,
  1700. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1701. __func__, length + 2, hid_output->command_code);
  1702. pt_pr_buf(cd->dev, DL_DEBUG, cmd, length + 2, ">>> CMD");
  1703. rc = pt_adap_write_read_specific(cd, length + 2, cmd, NULL);
  1704. if (rc)
  1705. pt_debug(cd->dev, DL_ERROR,
  1706. "%s: Fail pt_adap_transfer rc=%d\n", __func__, rc);
  1707. if (cmd_allocated)
  1708. kfree(cmd);
  1709. return rc;
  1710. }
  1711. /*******************************************************************************
  1712. * FUNCTION: pt_pip1_send_output_and_wait_
  1713. *
  1714. * SUMMARY: Send valid PIP1 command to the DUT and wait for the response.
  1715. *
  1716. * RETURN:
  1717. * 0 = success
  1718. * !0 = failure
  1719. *
  1720. * PARAMETERS:
  1721. * *cd - pointer to core data
  1722. * *hid_output - pointer to the command to send
  1723. ******************************************************************************/
  1724. static int pt_pip1_send_output_and_wait_(struct pt_core_data *cd,
  1725. struct pt_hid_output *hid_output)
  1726. {
  1727. int rc = 0;
  1728. int t;
  1729. u16 timeout_ms;
  1730. mutex_lock(&cd->system_lock);
  1731. cd->hid_cmd_state = hid_output->command_code + 1;
  1732. mutex_unlock(&cd->system_lock);
  1733. if (hid_output->timeout_ms)
  1734. timeout_ms = hid_output->timeout_ms;
  1735. else
  1736. timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT;
  1737. rc = pt_hid_send_output_(cd, hid_output);
  1738. if (rc)
  1739. goto error;
  1740. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1741. msecs_to_jiffies(timeout_ms));
  1742. if (IS_TMO(t)) {
  1743. #ifdef TTDL_DIAGNOSTICS
  1744. cd->bus_transmit_error_count++;
  1745. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1746. #endif /* TTDL_DIAGNOSTICS */
  1747. pt_debug(cd->dev, DL_ERROR,
  1748. "%s: HID output cmd execution timed out (%dms)\n",
  1749. __func__, timeout_ms);
  1750. rc = -ETIME;
  1751. goto error;
  1752. }
  1753. if (!hid_output->novalidate)
  1754. rc = pt_hid_output_validate_response(cd, hid_output);
  1755. pt_check_command(cd, hid_output, false);
  1756. goto exit;
  1757. error:
  1758. mutex_lock(&cd->system_lock);
  1759. cd->hid_cmd_state = 0;
  1760. mutex_unlock(&cd->system_lock);
  1761. exit:
  1762. return rc;
  1763. }
  1764. /*******************************************************************************
  1765. * FUNCTION: pt_hid_output_user_cmd_
  1766. *
  1767. * SUMMARY: Load the write buffer into a HID structure and send it as a HID cmd
  1768. * to the DUT waiting for the response and loading it into the read buffer
  1769. *
  1770. * RETURN:
  1771. * 0 = success
  1772. * !0 = failure
  1773. *
  1774. * PARAMETERS:
  1775. * *cd - pointer to core data
  1776. * read_len - expected read length of the response
  1777. * *read_buf - pointer to where the response will be loaded
  1778. * write_len - length of the write buffer
  1779. * *write_buf - pointer to the write buffer
  1780. * *actual_read_len - pointer to the actual amount of data read back
  1781. ******************************************************************************/
  1782. static int pt_hid_output_user_cmd_(struct pt_core_data *cd,
  1783. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1784. u16 *actual_read_len)
  1785. {
  1786. int rc = 0;
  1787. u16 size;
  1788. struct pt_hid_output hid_output = {
  1789. .length = write_len,
  1790. .write_buf = write_buf,
  1791. };
  1792. #ifdef TTHE_TUNER_SUPPORT
  1793. if (!cd->pip2_send_user_cmd) {
  1794. int command_code = 0;
  1795. int len;
  1796. /* Print up to cmd ID */
  1797. len = PIP1_CMD_COMMAND_ID_OFFSET + 1;
  1798. if (write_len < len)
  1799. len = write_len;
  1800. else
  1801. command_code = write_buf[PIP1_CMD_COMMAND_ID_OFFSET]
  1802. & PIP1_CMD_COMMAND_ID_MASK;
  1803. /* Don't print EXEC_PANEL_SCAN & RETRIEVE_PANEL_SCAN commands */
  1804. if (command_code != PIP1_CMD_ID_EXEC_PANEL_SCAN &&
  1805. command_code != PIP1_CMD_ID_RETRIEVE_PANEL_SCAN)
  1806. tthe_print(cd, write_buf, len, "CMD=");
  1807. }
  1808. #endif
  1809. rc = pt_hid_send_output_user_and_wait_(cd, &hid_output);
  1810. if (rc)
  1811. return rc;
  1812. /* Get the response size from the first 2 bytes in the response */
  1813. size = get_unaligned_le16(&cd->response_buf[0]);
  1814. /* Ensure size is not greater than max buffer size */
  1815. if (size > PT_MAX_PIP2_MSG_SIZE)
  1816. size = PT_MAX_PIP2_MSG_SIZE;
  1817. /* Minimum size to read is the 2 byte len field */
  1818. if (size == 0)
  1819. size = 2;
  1820. if (size > read_len) {
  1821. pt_debug(cd->dev, DL_ERROR,
  1822. "%s: PIP2 len field=%d, requested read_len=%d\n",
  1823. __func__, size, read_len);
  1824. *actual_read_len = 0;
  1825. return -EIO;
  1826. }
  1827. memcpy(read_buf, cd->response_buf, size);
  1828. *actual_read_len = size;
  1829. return 0;
  1830. }
  1831. /*******************************************************************************
  1832. * FUNCTION: pt_hid_output_user_cmd
  1833. *
  1834. * SUMMARY: Protected call to pt_hid_output_user_cmd_ by exclusive access to
  1835. * the DUT.
  1836. *
  1837. * RETURN:
  1838. * 0 = success
  1839. * !0 = failure
  1840. *
  1841. * PARAMETERS:
  1842. * *cd - pointer to core data
  1843. * read_len - length of data to read
  1844. * *read_buf - pointer to store read data
  1845. * write_len - length of data to write
  1846. * *write_buf - pointer to buffer to write
  1847. * *actual_read_len - pointer to store data length actually read
  1848. ******************************************************************************/
  1849. static int pt_hid_output_user_cmd(struct pt_core_data *cd,
  1850. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1851. u16 *actual_read_len)
  1852. {
  1853. int rc;
  1854. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  1855. if (rc < 0) {
  1856. pt_debug(cd->dev, DL_ERROR,
  1857. "%s: fail get exclusive ex=%p own=%p\n",
  1858. __func__, cd->exclusive_dev, cd->dev);
  1859. return rc;
  1860. }
  1861. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1862. write_len, write_buf, actual_read_len);
  1863. if (release_exclusive(cd, cd->dev) < 0)
  1864. pt_debug(cd->dev, DL_ERROR,
  1865. "%s: fail to release exclusive\n", __func__);
  1866. return rc;
  1867. }
  1868. /*******************************************************************************
  1869. * FUNCTION: _pt_request_pip2_send_cmd
  1870. *
  1871. * SUMMARY: Writes a PIP2 command packet to DUT, then waits for the
  1872. * interrupt and reads response data to read_buf
  1873. *
  1874. * RETURN:
  1875. * 0 = success
  1876. * !0 = failure
  1877. *
  1878. * PARAMETERS:
  1879. * *dev - pointer to device structure
  1880. * protect - flag to run in protected mode
  1881. * id - ID of PIP command
  1882. * *data - pointer to PIP data payload
  1883. * report_body_len - report length
  1884. * *read_buf - pointer to response buffer
  1885. * *actual_read_len - pointer to response buffer length
  1886. ******************************************************************************/
  1887. static int _pt_request_pip2_send_cmd(struct device *dev,
  1888. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  1889. u16 *actual_read_len)
  1890. {
  1891. struct pt_core_data *cd = dev_get_drvdata(dev);
  1892. struct pip2_cmd_structure pip2_cmd;
  1893. int rc = 0;
  1894. int i = 0;
  1895. int j = 0;
  1896. u16 write_len;
  1897. u8 *write_buf = NULL;
  1898. u16 read_len;
  1899. u8 extra_bytes;
  1900. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  1901. /* Hard coded register for PIP2.x */
  1902. pip2_cmd.reg[0] = 0x01;
  1903. pip2_cmd.reg[1] = 0x01;
  1904. /*
  1905. * For PIP2.1+ the length field value includes itself:
  1906. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  1907. *
  1908. * The overall write length must include only the register:
  1909. * ADD 2: 2 (Register)
  1910. */
  1911. extra_bytes = 6;
  1912. write_len = 2;
  1913. /* PIP2 the CMD ID is a 7bit field */
  1914. if (id > PIP2_CMD_ID_END) {
  1915. pt_debug(dev, DL_WARN, "%s: Invalid PIP2 CMD ID 0x%02X\n",
  1916. __func__, id);
  1917. rc = -EINVAL;
  1918. goto exit;
  1919. }
  1920. pip2_cmd.len = report_body_len + extra_bytes;
  1921. pip2_cmd.id = id & PIP2_CMD_COMMAND_ID_MASK;
  1922. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  1923. pip2_cmd.data = data;
  1924. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  1925. /* Add the command length to the extra bytes based on PIP version */
  1926. write_len += pip2_cmd.len;
  1927. pt_debug(dev, DL_INFO, "%s Length Field: %d, Write Len: %d",
  1928. __func__, pip2_cmd.len, write_len);
  1929. write_buf = kzalloc(write_len, GFP_KERNEL);
  1930. if (write_buf == NULL) {
  1931. rc = -ENOMEM;
  1932. goto exit;
  1933. }
  1934. write_buf[i++] = pip2_cmd.reg[0];
  1935. write_buf[i++] = pip2_cmd.reg[1];
  1936. write_buf[i++] = pip2_cmd.len & 0xff;
  1937. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  1938. write_buf[i++] = pip2_cmd.seq;
  1939. write_buf[i++] = pip2_cmd.id;
  1940. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  1941. write_buf[j] = pip2_cmd.data[j-i];
  1942. write_buf[j++] = pip2_cmd.crc[0];
  1943. write_buf[j++] = pip2_cmd.crc[1];
  1944. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  1945. if (read_len < 0)
  1946. read_len = 255;
  1947. pt_debug(dev, DL_INFO,
  1948. "%s cmd_id[0x%02X] expected response length:%d ",
  1949. __func__, pip2_cmd.id, read_len);
  1950. /*
  1951. * All PIP2 commands come through this function.
  1952. * Set flag for PIP2.x interface to allow response parsing to know
  1953. * how to decode the protocol header.
  1954. */
  1955. mutex_lock(&cd->system_lock);
  1956. cd->pip2_prot_active = true;
  1957. cd->pip2_send_user_cmd = true;
  1958. mutex_unlock(&cd->system_lock);
  1959. if (protect == PT_CORE_CMD_PROTECTED)
  1960. rc = pt_hid_output_user_cmd(cd, read_len, read_buf,
  1961. write_len, write_buf, actual_read_len);
  1962. else {
  1963. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1964. write_len, write_buf, actual_read_len);
  1965. }
  1966. if (rc) {
  1967. pt_debug(dev, DL_ERROR,
  1968. "%s: nonhid_cmd->user_cmd() Error = %d\n",
  1969. __func__, rc);
  1970. goto exit;
  1971. }
  1972. rc = pt_pip2_validate_response(cd, &pip2_cmd, read_buf,
  1973. *actual_read_len);
  1974. exit:
  1975. mutex_lock(&cd->system_lock);
  1976. cd->pip2_prot_active = false;
  1977. cd->pip2_send_user_cmd = false;
  1978. mutex_unlock(&cd->system_lock);
  1979. kfree(write_buf);
  1980. return rc;
  1981. }
  1982. /*******************************************************************************
  1983. * FUNCTION: _pt_pip2_send_cmd_no_int
  1984. *
  1985. * SUMMARY: Writes a PIP2 command packet to DUT, then poll the response and
  1986. * reads response data to read_buf if response is available.
  1987. *
  1988. * NOTE:
  1989. * Interrupt MUST be disabled before to call this function.
  1990. *
  1991. * RETURN:
  1992. * 0 = success
  1993. * !0 = failure
  1994. *
  1995. * PARAMETERS:
  1996. * *dev - pointer to device structure
  1997. * protect - flag to run in protected mode
  1998. * id - ID of PIP command
  1999. * *data - pointer to PIP data payload
  2000. * report_body_len - report length
  2001. * *read_buf - pointer to response buffer
  2002. * *actual_read_len - pointer to response buffer length
  2003. ******************************************************************************/
  2004. static int _pt_pip2_send_cmd_no_int(struct device *dev,
  2005. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  2006. u16 *actual_read_len)
  2007. {
  2008. int max_retry = 0;
  2009. int retry = 0;
  2010. int rc = 0;
  2011. int i = 0;
  2012. int j = 0;
  2013. u16 write_len;
  2014. u8 *write_buf = NULL;
  2015. u16 read_len;
  2016. u16 size = 0;
  2017. u8 response_seq = 0;
  2018. u8 extra_bytes;
  2019. u32 retry_interval = 0;
  2020. u32 retry_total_time = 0;
  2021. u32 resp_time_min = pt_pip2_get_cmd_resp_time_min(id);
  2022. u32 resp_time_max = pt_pip2_get_cmd_resp_time_max(id);
  2023. struct pt_core_data *cd = dev_get_drvdata(dev);
  2024. struct pip2_cmd_structure pip2_cmd;
  2025. if (protect == PT_CORE_CMD_PROTECTED) {
  2026. rc = request_exclusive(cd,
  2027. cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2028. if (rc < 0) {
  2029. pt_debug(cd->dev, DL_ERROR,
  2030. "%s: fail get exclusive ex=%p own=%p\n",
  2031. __func__, cd->exclusive_dev, cd->dev);
  2032. return rc;
  2033. }
  2034. }
  2035. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  2036. /* Hard coded register for PIP2.x */
  2037. pip2_cmd.reg[0] = 0x01;
  2038. pip2_cmd.reg[1] = 0x01;
  2039. /*
  2040. * For PIP2.1+ the length field value includes itself:
  2041. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  2042. *
  2043. * The overall write length must include only the register:
  2044. * ADD 2: 2 (Register)
  2045. */
  2046. extra_bytes = 6;
  2047. write_len = 2;
  2048. pip2_cmd.len = report_body_len + extra_bytes;
  2049. pip2_cmd.id = id;
  2050. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  2051. pip2_cmd.data = data;
  2052. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  2053. /* Add the command length to the extra bytes based on PIP version */
  2054. write_len += pip2_cmd.len;
  2055. write_buf = kzalloc(write_len, GFP_KERNEL);
  2056. if (write_buf == NULL) {
  2057. rc = -ENOMEM;
  2058. goto exit;
  2059. }
  2060. write_buf[i++] = pip2_cmd.reg[0];
  2061. write_buf[i++] = pip2_cmd.reg[1];
  2062. write_buf[i++] = pip2_cmd.len & 0xff;
  2063. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  2064. write_buf[i++] = pip2_cmd.seq;
  2065. write_buf[i++] = pip2_cmd.id;
  2066. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  2067. write_buf[j] = pip2_cmd.data[j-i];
  2068. write_buf[j++] = pip2_cmd.crc[0];
  2069. write_buf[j++] = pip2_cmd.crc[1];
  2070. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  2071. if (read_len < 0)
  2072. read_len = 255;
  2073. pt_debug(dev, DL_INFO,
  2074. "%s: ATM - cmd_id[0x%02X] expected response length:%d ",
  2075. __func__, pip2_cmd.id, read_len);
  2076. pt_pr_buf(cd->dev, DL_DEBUG, write_buf, write_len, ">>> NO_INT CMD");
  2077. rc = pt_adap_write_read_specific(cd, write_len, write_buf, NULL);
  2078. if (rc) {
  2079. pt_debug(dev, DL_ERROR,
  2080. "%s: SPI write Error = %d\n",
  2081. __func__, rc);
  2082. goto exit;
  2083. }
  2084. #ifdef PT_POLL_RESP_BY_BUS
  2085. /*
  2086. * Frequent bus read can increase system load obviously. The expected
  2087. * first bus read should be valid and timely. The tollerance for
  2088. * usleep_range should be limited. The minimum response delay (between
  2089. * command finishes sending and INT pin falls) is less than 50
  2090. * microseconds. So the 10 microseconds should be maximum tollerance
  2091. * with the consideration that the unit to calculate the response delay
  2092. * is 10 microseconds and more precise is not necessary. Every
  2093. * additional 10 microseconds only contribute less than 3 milliseconds
  2094. * for whole BL.
  2095. */
  2096. usleep_range(resp_time_min, resp_time_min+10);
  2097. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2098. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2099. rc = pt_adap_read_default(cd, read_buf, read_len);
  2100. if (rc) {
  2101. pt_debug(dev, DL_ERROR, "%s: SPI read Error = %d\n",
  2102. __func__, rc);
  2103. break;
  2104. }
  2105. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET];
  2106. size = get_unaligned_le16(&read_buf[0]);
  2107. if ((size <= read_len) &&
  2108. (size >= PIP2_EXTRA_BYTES_NUM) &&
  2109. (pip2_cmd.seq & 0x07) == (response_seq & 0x07)) {
  2110. break;
  2111. }
  2112. /*
  2113. * To reduce the bus and system load, increase the sleep
  2114. * step gradually:
  2115. * 1 ~ 19 : step=50 us, sleep_us=[50, 100, 150, 200, ..950]
  2116. * 20 ~ 39 : step=1000 us, sleep_us=[1950, 2950, ...20950]
  2117. * 40 ~ MAX: step=50 ms, sleep_ms=[71, 121, 191,..]
  2118. */
  2119. retry++;
  2120. if (retry < 20) {
  2121. retry_interval += POLL_RETRY_DEFAULT_INTERVAL;
  2122. usleep_range(retry_interval,
  2123. retry_interval + POLL_RETRY_DEFAULT_INTERVAL);
  2124. } else if (retry < 40) {
  2125. retry_interval += 1000;
  2126. usleep_range(retry_interval, retry_interval + 1000);
  2127. } else {
  2128. retry_interval += 50000;
  2129. msleep(retry_interval/1000);
  2130. }
  2131. retry_total_time += retry_interval;
  2132. }
  2133. #else
  2134. /*
  2135. * Frequent GPIO read will not increase CPU/system load heavily if the
  2136. * interval is longer than 10 us, so it is safe to poll GPIO with a
  2137. * fixed interval: 20 us.
  2138. */
  2139. usleep_range(resp_time_min, resp_time_min+10);
  2140. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2141. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2142. if (!gpio_get_value(cd->cpdata->irq_gpio)) {
  2143. rc = pt_adap_read_default(cd, read_buf, read_len);
  2144. size = get_unaligned_le16(&read_buf[0]);
  2145. if (rc)
  2146. pt_debug(dev, DL_ERROR,
  2147. "%s: SPI read Error = %d\n",
  2148. __func__, rc);
  2149. else if (size > read_len) {
  2150. pt_debug(cd->dev, DL_ERROR,
  2151. "%s: PIP2 len field=%d, requested read_len=%d\n",
  2152. __func__, size, read_len);
  2153. rc = -EIO;
  2154. }
  2155. break;
  2156. }
  2157. /*
  2158. * Poll GPIO with fixed interval 20 us, and tollerance is
  2159. * limited to 10 us to speed up the process.
  2160. */
  2161. retry_interval = POLL_RETRY_DEFAULT_INTERVAL;
  2162. usleep_range(retry_interval, retry_interval+10);
  2163. retry_total_time += retry_interval;
  2164. }
  2165. #endif
  2166. *actual_read_len = size;
  2167. if (rc || (retry >= max_retry) || (retry_total_time >= resp_time_max)) {
  2168. pt_debug(dev, DL_ERROR,
  2169. "%s cmd[0x%02X] timed out, send_seq=0x%02X, resp_seq=0x%02X\n",
  2170. __func__, pip2_cmd.id, pip2_cmd.seq, response_seq);
  2171. *actual_read_len = 0;
  2172. rc = -EINVAL;
  2173. }
  2174. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, *actual_read_len,
  2175. "<<< NO_INT Read");
  2176. exit:
  2177. kfree(write_buf);
  2178. if (protect == PT_CORE_CMD_PROTECTED) {
  2179. if (release_exclusive(cd, cd->dev) < 0)
  2180. pt_debug(cd->dev, DL_ERROR,
  2181. "%s: fail to release exclusive\n", __func__);
  2182. }
  2183. return rc;
  2184. }
  2185. /*******************************************************************************
  2186. * FUNCTION: pt_pip_null_
  2187. *
  2188. * SUMMARY: Send the PIP "ping"(0x00) command to the DUT and wait for response.
  2189. * This function is used by watchdog to check if the fw corrupts.
  2190. *
  2191. * RETURN:
  2192. * 0 = success
  2193. * !0 = failure
  2194. *
  2195. * PARAMETERS:
  2196. * *cd - pointer to core data
  2197. ******************************************************************************/
  2198. static int pt_pip_null_(struct pt_core_data *cd)
  2199. {
  2200. struct pt_hid_output hid_output = {
  2201. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_NULL),
  2202. };
  2203. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  2204. }
  2205. /*******************************************************************************
  2206. * FUNCTION: pt_pip_null
  2207. *
  2208. * SUMMARY: Wrapper function for pt_pip_null_ that guarantees exclusive access.
  2209. *
  2210. * RETURN:
  2211. * 0 = success
  2212. * !0 = failure
  2213. *
  2214. * PARAMETERS:
  2215. * *cd - pointer to core data
  2216. ******************************************************************************/
  2217. static int pt_pip_null(struct pt_core_data *cd)
  2218. {
  2219. int rc;
  2220. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2221. if (rc < 0) {
  2222. pt_debug(cd->dev, DL_ERROR,
  2223. "%s: fail get exclusive ex=%p own=%p\n",
  2224. __func__, cd->exclusive_dev, cd->dev);
  2225. return rc;
  2226. }
  2227. rc = pt_pip_null_(cd);
  2228. if (release_exclusive(cd, cd->dev) < 0)
  2229. pt_debug(cd->dev, DL_ERROR,
  2230. "%s: fail to release exclusive\n", __func__);
  2231. return rc;
  2232. }
  2233. static void pt_stop_wd_timer(struct pt_core_data *cd);
  2234. /*******************************************************************************
  2235. * FUNCTION: pt_pip_start_bootloader_
  2236. *
  2237. * SUMMARY: Sends the HID command start_bootloader [PIP cmd 0x01] to the DUT
  2238. *
  2239. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  2240. * the WD active could cause this function to fail!
  2241. *
  2242. * RETURN:
  2243. * 0 = success
  2244. * !0 = failure
  2245. *
  2246. * PARAMETERS:
  2247. * *cd - pointer to core data
  2248. ******************************************************************************/
  2249. static int pt_pip_start_bootloader_(struct pt_core_data *cd)
  2250. {
  2251. int rc = 0;
  2252. struct pt_hid_output hid_output = {
  2253. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_START_BOOTLOADER),
  2254. .timeout_ms = PT_PIP1_START_BOOTLOADER_TIMEOUT,
  2255. .reset_expected = 1,
  2256. };
  2257. if (cd->watchdog_enabled) {
  2258. pt_debug(cd->dev, DL_WARN,
  2259. "%s: watchdog isn't stopped before enter bl\n",
  2260. __func__);
  2261. goto exit;
  2262. }
  2263. /* Reset startup status after entering BL, new DUT enum required */
  2264. cd->startup_status = STARTUP_STATUS_START;
  2265. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  2266. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2267. if (rc) {
  2268. pt_debug(cd->dev, DL_ERROR,
  2269. "%s: Start BL PIP cmd failed. rc = %d\n",
  2270. __func__, rc);
  2271. }
  2272. exit:
  2273. return rc;
  2274. }
  2275. /*******************************************************************************
  2276. * FUNCTION: pt_pip_start_bootloader
  2277. *
  2278. * SUMMARY: Protected function to force DUT to enter the BL
  2279. *
  2280. * RETURN:
  2281. * 0 = success
  2282. * !0 = failure
  2283. *
  2284. * PARAMETERS:
  2285. * *cd - pointer to core data structure
  2286. ******************************************************************************/
  2287. static int pt_pip_start_bootloader(struct pt_core_data *cd)
  2288. {
  2289. int rc;
  2290. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2291. if (rc < 0) {
  2292. pt_debug(cd->dev, DL_ERROR,
  2293. "%s: fail get exclusive ex=%p own=%p\n",
  2294. __func__, cd->exclusive_dev, cd->dev);
  2295. return rc;
  2296. }
  2297. rc = pt_pip_start_bootloader_(cd);
  2298. if (release_exclusive(cd, cd->dev) < 0)
  2299. pt_debug(cd->dev, DL_ERROR,
  2300. "%s: fail to release exclusive\n", __func__);
  2301. return rc;
  2302. }
  2303. /*******************************************************************************
  2304. * FUNCTION: _pt_request_pip_start_bl
  2305. *
  2306. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other
  2307. * modules to request the DUT to enter the BL
  2308. *
  2309. * RETURN:
  2310. * 0 = success
  2311. * !0 = failure
  2312. *
  2313. * PARAMETERS:
  2314. * *dev - pointer to device structure
  2315. * protect - flag to run in protected mode
  2316. ******************************************************************************/
  2317. static int _pt_request_pip_start_bl(struct device *dev, int protect)
  2318. {
  2319. struct pt_core_data *cd = dev_get_drvdata(dev);
  2320. if (protect)
  2321. return pt_pip_start_bootloader(cd);
  2322. return pt_pip_start_bootloader_(cd);
  2323. }
  2324. /*******************************************************************************
  2325. * FUNCTION: pt_pip2_ver_load_ttdata
  2326. *
  2327. * SUMMARY: Function to load the Version information from the PIP2 VERSION
  2328. * command into the core data struct.
  2329. *
  2330. * RETURN: n/a
  2331. *
  2332. * PARAMETERS:
  2333. * *cd - pointer to core data structure
  2334. * len - Length of data in response_buf
  2335. ******************************************************************************/
  2336. static void pt_pip2_ver_load_ttdata(struct pt_core_data *cd, u16 len)
  2337. {
  2338. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2339. struct pt_pip2_version_full *full_ver;
  2340. struct pt_pip2_version *ver;
  2341. /*
  2342. * The PIP2 VERSION command can return different lengths of data.
  2343. * The additional LOT fields are included when the packet
  2344. * size is >= 29 bytes. Older FW sends a reduced packet size.
  2345. * NOTE:
  2346. * - The FW would swap the BL and FW versions when reporting
  2347. * the small packet.
  2348. * - Sub Lot bytes 16 and 17 are reserved.
  2349. */
  2350. if (len >= 0x1D) {
  2351. full_ver = (struct pt_pip2_version_full *)
  2352. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2353. ttdata->pip_ver_major = full_ver->pip2_version_msb;
  2354. ttdata->pip_ver_minor = full_ver->pip2_version_lsb;
  2355. ttdata->bl_ver_major = full_ver->bl_version_msb;
  2356. ttdata->bl_ver_minor = full_ver->bl_version_lsb;
  2357. ttdata->fw_ver_major = full_ver->fw_version_msb;
  2358. ttdata->fw_ver_minor = full_ver->fw_version_lsb;
  2359. /*
  2360. * BL PIP 2.02 and greater the version fields are
  2361. * swapped
  2362. */
  2363. if (ttdata->pip_ver_major >= 2 && ttdata->pip_ver_minor >= 2) {
  2364. ttdata->chip_rev =
  2365. get_unaligned_le16(&full_ver->chip_rev);
  2366. ttdata->chip_id =
  2367. get_unaligned_le16(&full_ver->chip_id);
  2368. } else {
  2369. ttdata->chip_rev =
  2370. get_unaligned_le16(&full_ver->chip_id);
  2371. ttdata->chip_id =
  2372. get_unaligned_le16(&full_ver->chip_rev);
  2373. }
  2374. memcpy(ttdata->uid, full_ver->uid, PT_UID_SIZE);
  2375. pt_pr_buf(cd->dev, DL_INFO, (u8 *)full_ver,
  2376. sizeof(struct pt_pip2_version_full),
  2377. "PIP2 VERSION FULL");
  2378. } else {
  2379. ver = (struct pt_pip2_version *)
  2380. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2381. ttdata->pip_ver_major = ver->pip2_version_msb;
  2382. ttdata->pip_ver_minor = ver->pip2_version_lsb;
  2383. ttdata->bl_ver_major = ver->bl_version_msb;
  2384. ttdata->bl_ver_minor = ver->bl_version_lsb;
  2385. ttdata->fw_ver_major = ver->fw_version_msb;
  2386. ttdata->fw_ver_minor = ver->fw_version_lsb;
  2387. ttdata->chip_rev = get_unaligned_le16(&ver->chip_rev);
  2388. ttdata->chip_id = get_unaligned_le16(&ver->chip_id);
  2389. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ver,
  2390. sizeof(struct pt_pip2_version), "PIP2 VERSION");
  2391. }
  2392. }
  2393. /*******************************************************************************
  2394. * FUNCTION: pt_si_get_ttdata
  2395. *
  2396. * SUMMARY: Function to load the version information from the system information
  2397. * PIP command into the core data struct.
  2398. *
  2399. * RETURN: n/a
  2400. *
  2401. * PARAMETERS:
  2402. * *cd - pointer to core data structure
  2403. ******************************************************************************/
  2404. static void pt_si_get_ttdata(struct pt_core_data *cd)
  2405. {
  2406. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2407. struct pt_ttdata_dev *ttdata_dev =
  2408. (struct pt_ttdata_dev *)
  2409. &cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  2410. ttdata->pip_ver_major = ttdata_dev->pip_ver_major;
  2411. ttdata->pip_ver_minor = ttdata_dev->pip_ver_minor;
  2412. ttdata->bl_ver_major = ttdata_dev->bl_ver_major;
  2413. ttdata->bl_ver_minor = ttdata_dev->bl_ver_minor;
  2414. ttdata->fw_ver_major = ttdata_dev->fw_ver_major;
  2415. ttdata->fw_ver_minor = ttdata_dev->fw_ver_minor;
  2416. ttdata->fw_pid = get_unaligned_le16(&ttdata_dev->fw_pid);
  2417. ttdata->fw_ver_conf = get_unaligned_le16(&ttdata_dev->fw_ver_conf);
  2418. ttdata->post_code = get_unaligned_le16(&ttdata_dev->post_code);
  2419. ttdata->revctrl = get_unaligned_le32(&ttdata_dev->revctrl);
  2420. ttdata->jtag_id_l = get_unaligned_le16(&ttdata_dev->jtag_si_id_l);
  2421. ttdata->jtag_id_h = get_unaligned_le16(&ttdata_dev->jtag_si_id_h);
  2422. memcpy(ttdata->mfg_id, ttdata_dev->mfg_id, PT_NUM_MFGID);
  2423. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ttdata_dev,
  2424. sizeof(struct pt_ttdata_dev), "sysinfo_ttdata");
  2425. }
  2426. /*******************************************************************************
  2427. * FUNCTION: pt_si_get_sensing_conf_data
  2428. *
  2429. * SUMMARY: Function to load the sensing information from the system information
  2430. * PIP command into the core data struct.
  2431. *
  2432. * RETURN: n/a
  2433. *
  2434. * PARAMETERS:
  2435. * *cd - pointer to core data structure
  2436. ******************************************************************************/
  2437. static void pt_si_get_sensing_conf_data(struct pt_core_data *cd)
  2438. {
  2439. struct pt_sensing_conf_data *scd = &cd->sysinfo.sensing_conf_data;
  2440. struct pt_sensing_conf_data_dev *scd_dev =
  2441. (struct pt_sensing_conf_data_dev *)
  2442. &cd->response_buf[PIP1_SYSINFO_SENSING_OFFSET];
  2443. scd->electrodes_x = scd_dev->electrodes_x;
  2444. scd->electrodes_y = scd_dev->electrodes_y;
  2445. scd->origin_x = scd_dev->origin_x;
  2446. scd->origin_y = scd_dev->origin_y;
  2447. /* PIP 1.4 (001-82649 *Q) add X_IS_TX bit in X_ORG */
  2448. if (scd->origin_x & 0x02) {
  2449. scd->tx_num = scd->electrodes_x;
  2450. scd->rx_num = scd->electrodes_y;
  2451. } else {
  2452. scd->tx_num = scd->electrodes_y;
  2453. scd->rx_num = scd->electrodes_x;
  2454. }
  2455. /*
  2456. * When the Panel ID is coming from an XY pin and not a dedicated
  2457. * GPIO, store the PID in pid_for_loader. This cannot be done for all
  2458. * other DUTs as the loader will use cd->pid_for_loader to generate
  2459. * the bin file name but will ignore it if pid_for_loader is still
  2460. * set to PANEL_ID_NOT_ENABLED
  2461. */
  2462. if (cd->panel_id_support &
  2463. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  2464. mutex_lock(&cd->system_lock);
  2465. cd->pid_for_loader = scd_dev->panel_id;
  2466. mutex_unlock(&cd->system_lock);
  2467. }
  2468. scd->panel_id = scd_dev->panel_id;
  2469. scd->btn = scd_dev->btn;
  2470. scd->scan_mode = scd_dev->scan_mode;
  2471. scd->max_tch = scd_dev->max_num_of_tch_per_refresh_cycle;
  2472. scd->res_x = get_unaligned_le16(&scd_dev->res_x);
  2473. scd->res_y = get_unaligned_le16(&scd_dev->res_y);
  2474. scd->max_z = get_unaligned_le16(&scd_dev->max_z);
  2475. scd->len_x = get_unaligned_le16(&scd_dev->len_x);
  2476. scd->len_y = get_unaligned_le16(&scd_dev->len_y);
  2477. pt_pr_buf(cd->dev, DL_INFO, (u8 *)scd_dev,
  2478. sizeof(struct pt_sensing_conf_data_dev),
  2479. "sensing_conf_data");
  2480. }
  2481. /*******************************************************************************
  2482. * FUNCTION: pt_si_setup
  2483. *
  2484. * SUMMARY: Setup the xy_data and xy_mode by allocating the needed memory
  2485. *
  2486. * RETURN:
  2487. * 0 = success
  2488. * !0 = failure
  2489. *
  2490. * PARAMETERS:
  2491. * *cd - pointer to core data structure
  2492. ******************************************************************************/
  2493. static int pt_si_setup(struct pt_core_data *cd)
  2494. {
  2495. struct pt_sysinfo *si = &cd->sysinfo;
  2496. int max_tch = si->sensing_conf_data.max_tch;
  2497. if (!si->xy_data)
  2498. si->xy_data = kzalloc(max_tch * si->desc.tch_record_size,
  2499. GFP_KERNEL);
  2500. if (!si->xy_data)
  2501. return -ENOMEM;
  2502. if (!si->xy_mode)
  2503. si->xy_mode = kzalloc(si->desc.tch_header_size, GFP_KERNEL);
  2504. if (!si->xy_mode) {
  2505. kfree(si->xy_data);
  2506. return -ENOMEM;
  2507. }
  2508. return 0;
  2509. }
  2510. /*******************************************************************************
  2511. * FUNCTION: pt_si_get_btn_data
  2512. *
  2513. * SUMMARY: Setup the core data button information based on the response of the
  2514. * System Information PIP command.
  2515. *
  2516. * RETURN:
  2517. * 0 = success
  2518. * !0 = failure
  2519. *
  2520. * PARAMETERS:
  2521. * *cd - pointer to core data structure
  2522. ******************************************************************************/
  2523. static int pt_si_get_btn_data(struct pt_core_data *cd)
  2524. {
  2525. struct pt_sysinfo *si = &cd->sysinfo;
  2526. int num_btns = 0;
  2527. int num_defined_keys;
  2528. u16 *key_table;
  2529. int btn;
  2530. int i;
  2531. int rc = 0;
  2532. unsigned int btns = cd->response_buf[PIP1_SYSINFO_BTN_OFFSET]
  2533. & PIP1_SYSINFO_BTN_MASK;
  2534. size_t btn_keys_size;
  2535. pt_debug(cd->dev, DL_INFO, "%s: get btn data\n", __func__);
  2536. for (i = 0; i < PIP1_SYSINFO_MAX_BTN; i++) {
  2537. if (btns & (1 << i))
  2538. num_btns++;
  2539. }
  2540. si->num_btns = num_btns;
  2541. if (num_btns) {
  2542. btn_keys_size = num_btns * sizeof(struct pt_btn);
  2543. if (!si->btn)
  2544. si->btn = kzalloc(btn_keys_size, GFP_KERNEL);
  2545. if (!si->btn)
  2546. return -ENOMEM;
  2547. if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS] == NULL)
  2548. num_defined_keys = 0;
  2549. else if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS]->data == NULL)
  2550. num_defined_keys = 0;
  2551. else
  2552. num_defined_keys = cd->cpdata->sett
  2553. [PT_IC_GRPNUM_BTN_KEYS]->size;
  2554. for (btn = 0; btn < num_btns && btn < num_defined_keys; btn++) {
  2555. key_table = (u16 *)cd->cpdata->sett
  2556. [PT_IC_GRPNUM_BTN_KEYS]->data;
  2557. si->btn[btn].key_code = key_table[btn];
  2558. si->btn[btn].enabled = true;
  2559. }
  2560. for (; btn < num_btns; btn++) {
  2561. si->btn[btn].key_code = KEY_RESERVED;
  2562. si->btn[btn].enabled = true;
  2563. }
  2564. return rc;
  2565. }
  2566. kfree(si->btn);
  2567. si->btn = NULL;
  2568. return rc;
  2569. }
  2570. /*******************************************************************************
  2571. * FUNCTION: pt_si_put_log_data
  2572. *
  2573. * SUMMARY: Prints all sys info data to kmsg log
  2574. *
  2575. * RETURN: n/a
  2576. *
  2577. * PARAMETERS:
  2578. * *cd - pointer to core data structure
  2579. ******************************************************************************/
  2580. static void pt_si_put_log_data(struct pt_core_data *cd)
  2581. {
  2582. struct pt_sysinfo *si = &cd->sysinfo;
  2583. struct pt_ttdata *ttdata = &si->ttdata;
  2584. struct pt_sensing_conf_data *scd = &si->sensing_conf_data;
  2585. int i;
  2586. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_major = 0x%02X (%d)\n",
  2587. __func__, ttdata->pip_ver_major, ttdata->pip_ver_major);
  2588. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_minor = 0x%02X (%d)\n",
  2589. __func__, ttdata->pip_ver_minor, ttdata->pip_ver_minor);
  2590. pt_debug(cd->dev, DL_DEBUG, "%s: fw_pid = 0x%04X (%d)\n",
  2591. __func__, ttdata->fw_pid, ttdata->fw_pid);
  2592. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_major = 0x%02X (%d)\n",
  2593. __func__, ttdata->fw_ver_major, ttdata->fw_ver_major);
  2594. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_minor = 0x%02X (%d)\n",
  2595. __func__, ttdata->fw_ver_minor, ttdata->fw_ver_minor);
  2596. pt_debug(cd->dev, DL_DEBUG, "%s: revctrl = 0x%08X (%d)\n",
  2597. __func__, ttdata->revctrl, ttdata->revctrl);
  2598. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_conf = 0x%04X (%d)\n",
  2599. __func__, ttdata->fw_ver_conf, ttdata->fw_ver_conf);
  2600. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_major = 0x%02X (%d)\n",
  2601. __func__, ttdata->bl_ver_major, ttdata->bl_ver_major);
  2602. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_minor = 0x%02X (%d)\n",
  2603. __func__, ttdata->bl_ver_minor, ttdata->bl_ver_minor);
  2604. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_h = 0x%04X (%d)\n",
  2605. __func__, ttdata->jtag_id_h, ttdata->jtag_id_h);
  2606. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_l = 0x%04X (%d)\n",
  2607. __func__, ttdata->jtag_id_l, ttdata->jtag_id_l);
  2608. for (i = 0; i < PT_NUM_MFGID; i++)
  2609. pt_debug(cd->dev, DL_DEBUG,
  2610. "%s: mfg_id[%d] = 0x%02X (%d)\n",
  2611. __func__, i, ttdata->mfg_id[i],
  2612. ttdata->mfg_id[i]);
  2613. pt_debug(cd->dev, DL_DEBUG, "%s: post_code = 0x%04X (%d)\n",
  2614. __func__, ttdata->post_code, ttdata->post_code);
  2615. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_x = 0x%02X (%d)\n",
  2616. __func__, scd->electrodes_x, scd->electrodes_x);
  2617. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_y = 0x%02X (%d)\n",
  2618. __func__, scd->electrodes_y, scd->electrodes_y);
  2619. pt_debug(cd->dev, DL_DEBUG, "%s: len_x = 0x%04X (%d)\n",
  2620. __func__, scd->len_x, scd->len_x);
  2621. pt_debug(cd->dev, DL_DEBUG, "%s: len_y = 0x%04X (%d)\n",
  2622. __func__, scd->len_y, scd->len_y);
  2623. pt_debug(cd->dev, DL_DEBUG, "%s: res_x = 0x%04X (%d)\n",
  2624. __func__, scd->res_x, scd->res_x);
  2625. pt_debug(cd->dev, DL_DEBUG, "%s: res_y = 0x%04X (%d)\n",
  2626. __func__, scd->res_y, scd->res_y);
  2627. pt_debug(cd->dev, DL_DEBUG, "%s: max_z = 0x%04X (%d)\n",
  2628. __func__, scd->max_z, scd->max_z);
  2629. pt_debug(cd->dev, DL_DEBUG, "%s: origin_x = 0x%02X (%d)\n",
  2630. __func__, scd->origin_x, scd->origin_x);
  2631. pt_debug(cd->dev, DL_DEBUG, "%s: origin_y = 0x%02X (%d)\n",
  2632. __func__, scd->origin_y, scd->origin_y);
  2633. pt_debug(cd->dev, DL_DEBUG, "%s: panel_id = 0x%02X (%d)\n",
  2634. __func__, scd->panel_id, scd->panel_id);
  2635. pt_debug(cd->dev, DL_DEBUG, "%s: btn =0x%02X (%d)\n",
  2636. __func__, scd->btn, scd->btn);
  2637. pt_debug(cd->dev, DL_DEBUG, "%s: scan_mode = 0x%02X (%d)\n",
  2638. __func__, scd->scan_mode, scd->scan_mode);
  2639. pt_debug(cd->dev, DL_DEBUG,
  2640. "%s: max_num_of_tch_per_refresh_cycle = 0x%02X (%d)\n",
  2641. __func__, scd->max_tch, scd->max_tch);
  2642. pt_debug(cd->dev, DL_DEBUG, "%s: xy_mode = %p\n",
  2643. __func__, si->xy_mode);
  2644. pt_debug(cd->dev, DL_DEBUG, "%s: xy_data = %p\n",
  2645. __func__, si->xy_data);
  2646. }
  2647. /*******************************************************************************
  2648. * FUNCTION: pt_get_sysinfo_regs
  2649. *
  2650. * SUMMARY: Setup all the core data System information based on the response
  2651. * of the System Information PIP command.
  2652. *
  2653. * RETURN:
  2654. * 0 = success
  2655. * !0 = failure
  2656. *
  2657. * PARAMETERS:
  2658. * *cd - pointer to core data structure
  2659. ******************************************************************************/
  2660. static int pt_get_sysinfo_regs(struct pt_core_data *cd)
  2661. {
  2662. struct pt_sysinfo *si = &cd->sysinfo;
  2663. int rc;
  2664. rc = pt_si_get_btn_data(cd);
  2665. if (rc < 0)
  2666. return rc;
  2667. pt_si_get_ttdata(cd);
  2668. pt_si_get_sensing_conf_data(cd);
  2669. pt_si_setup(cd);
  2670. pt_si_put_log_data(cd);
  2671. si->ready = true;
  2672. return rc;
  2673. }
  2674. /*******************************************************************************
  2675. * FUNCTION: pt_free_si_ptrs
  2676. *
  2677. * SUMMARY: Frees all memory associated with the System Information within
  2678. * core data
  2679. *
  2680. * RETURN: n/a
  2681. *
  2682. * PARAMETERS:
  2683. * *cd - pointer to core data structure
  2684. ******************************************************************************/
  2685. static void pt_free_si_ptrs(struct pt_core_data *cd)
  2686. {
  2687. struct pt_sysinfo *si = &cd->sysinfo;
  2688. kfree(si->btn);
  2689. kfree(si->xy_mode);
  2690. kfree(si->xy_data);
  2691. }
  2692. /*******************************************************************************
  2693. * FUNCTION: pt_hid_output_get_sysinfo_
  2694. *
  2695. * SUMMARY: Sends the PIP Get SYS INFO command to the DUT and waits for the
  2696. * response.
  2697. *
  2698. * RETURN::
  2699. * 0 = success
  2700. * !0 = failure
  2701. *
  2702. * PARAMETERS:
  2703. * *cd - pointer to core data structure
  2704. ******************************************************************************/
  2705. static int pt_hid_output_get_sysinfo_(struct pt_core_data *cd)
  2706. {
  2707. int rc = 0;
  2708. struct pt_hid_output hid_output = {
  2709. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  2710. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  2711. };
  2712. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2713. if (rc)
  2714. return rc;
  2715. /* Parse the sysinfo data */
  2716. rc = pt_get_sysinfo_regs(cd);
  2717. if (rc)
  2718. pt_free_si_ptrs(cd);
  2719. return rc;
  2720. }
  2721. /*******************************************************************************
  2722. * FUNCTION: pt_hid_output_get_sysinfo
  2723. *
  2724. * SUMMARY: Protected call to pt_hid_output_get_sysinfo_
  2725. *
  2726. * RETURN::
  2727. * 0 = success
  2728. * !0 = failure
  2729. *
  2730. * PARAMETERS:
  2731. * *cd - pointer to core data structure
  2732. ******************************************************************************/
  2733. static int pt_hid_output_get_sysinfo(struct pt_core_data *cd)
  2734. {
  2735. int rc;
  2736. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2737. if (rc < 0) {
  2738. pt_debug(cd->dev, DL_ERROR,
  2739. "%s: fail get exclusive ex=%p own=%p\n",
  2740. __func__, cd->exclusive_dev, cd->dev);
  2741. return rc;
  2742. }
  2743. rc = pt_hid_output_get_sysinfo_(cd);
  2744. if (release_exclusive(cd, cd->dev) < 0)
  2745. pt_debug(cd->dev, DL_ERROR,
  2746. "%s: fail to release exclusive\n", __func__);
  2747. return rc;
  2748. }
  2749. /*******************************************************************************
  2750. * FUNCTION: pt_pip_suspend_scanning_
  2751. *
  2752. * SUMMARY: Sends the PIP Suspend Scanning command to the DUT
  2753. *
  2754. * RETURN::
  2755. * 0 = success
  2756. * !0 = failure
  2757. *
  2758. * PARAMETERS:
  2759. * *cd - pointer to core data structure
  2760. ******************************************************************************/
  2761. static int pt_pip_suspend_scanning_(struct pt_core_data *cd)
  2762. {
  2763. int rc = 0;
  2764. struct pt_hid_output hid_output = {
  2765. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SUSPEND_SCANNING),
  2766. };
  2767. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2768. if (rc) {
  2769. pt_debug(cd->dev, DL_ERROR,
  2770. "%s: Suspend Scan PIP cmd failed. rc = %d\n",
  2771. __func__, rc);
  2772. }
  2773. return rc;
  2774. }
  2775. /*******************************************************************************
  2776. * FUNCTION: pt_pip_suspend_scanning
  2777. *
  2778. * SUMMARY: Protected wrapper for calling pt_hid_output_suspend_scanning_
  2779. *
  2780. * RETURN::
  2781. * 0 = success
  2782. * !0 = failure
  2783. *
  2784. * PARAMETERS:
  2785. * *cd - pointer to core data structure
  2786. ******************************************************************************/
  2787. static int pt_pip_suspend_scanning(struct pt_core_data *cd)
  2788. {
  2789. int rc;
  2790. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2791. if (rc < 0) {
  2792. pt_debug(cd->dev, DL_ERROR,
  2793. "%s: fail get exclusive ex=%p own=%p\n",
  2794. __func__, cd->exclusive_dev, cd->dev);
  2795. return rc;
  2796. }
  2797. rc = pt_pip_suspend_scanning_(cd);
  2798. if (release_exclusive(cd, cd->dev) < 0)
  2799. pt_debug(cd->dev, DL_ERROR,
  2800. "%s: fail to release exclusive\n", __func__);
  2801. return rc;
  2802. }
  2803. /*******************************************************************************
  2804. * FUNCTION: _pt_request_pip_suspend_scanning
  2805. *
  2806. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2807. * calls to the protected or unprotected call to pt_pip_suspend_scanning
  2808. *
  2809. * RETURN:
  2810. * 0 = success
  2811. * !0 = failure
  2812. *
  2813. * PARAMETERS:
  2814. * *dev - pointer to device structure
  2815. * protect - 0 = call non-protected function
  2816. * 1 = call protected function
  2817. ******************************************************************************/
  2818. static int _pt_request_pip_suspend_scanning(struct device *dev,
  2819. int protect)
  2820. {
  2821. struct pt_core_data *cd = dev_get_drvdata(dev);
  2822. if (protect)
  2823. return pt_pip_suspend_scanning(cd);
  2824. return pt_pip_suspend_scanning_(cd);
  2825. }
  2826. /*******************************************************************************
  2827. * FUNCTION: pt_pip_resume_scanning_
  2828. *
  2829. * SUMMARY: Sends the PIP Resume Scanning command to the DUT
  2830. *
  2831. * RETURN::
  2832. * 0 = success
  2833. * !0 = failure
  2834. *
  2835. * PARAMETERS:
  2836. * *cd - pointer to core data structure
  2837. ******************************************************************************/
  2838. static int pt_pip_resume_scanning_(struct pt_core_data *cd)
  2839. {
  2840. int rc = 0;
  2841. struct pt_hid_output hid_output = {
  2842. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RESUME_SCANNING),
  2843. };
  2844. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2845. if (rc) {
  2846. pt_debug(cd->dev, DL_ERROR,
  2847. "%s: Resume Scan PIP cmd failed. rc = %d\n",
  2848. __func__, rc);
  2849. }
  2850. return rc;
  2851. }
  2852. /*******************************************************************************
  2853. * FUNCTION: pt_pip_resume_scanning
  2854. *
  2855. * SUMMARY: Protected wrapper for calling pt_pip_resume_scanning_
  2856. *
  2857. * RETURN::
  2858. * 0 = success
  2859. * !0 = failure
  2860. *
  2861. * PARAMETERS:
  2862. * *cd - pointer to core data structure
  2863. ******************************************************************************/
  2864. static int pt_pip_resume_scanning(struct pt_core_data *cd)
  2865. {
  2866. int rc;
  2867. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2868. if (rc < 0) {
  2869. pt_debug(cd->dev, DL_ERROR,
  2870. "%s: fail get exclusive ex=%p own=%p\n",
  2871. __func__, cd->exclusive_dev, cd->dev);
  2872. return rc;
  2873. }
  2874. rc = pt_pip_resume_scanning_(cd);
  2875. if (release_exclusive(cd, cd->dev) < 0)
  2876. pt_debug(cd->dev, DL_ERROR,
  2877. "%s: fail to release exclusive\n", __func__);
  2878. return rc;
  2879. }
  2880. /*******************************************************************************
  2881. * FUNCTION: _pt_request_pip_resume_scanning
  2882. *
  2883. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2884. * calls to the protected or unprotected call to pt_pip_resume_scanning
  2885. *
  2886. * RETURN::
  2887. * 0 = success
  2888. * !0 = failure
  2889. *
  2890. * PARAMETERS:
  2891. * *dev - pointer to device structure
  2892. * protect - 0 = call non-protected function
  2893. * 1 = call protected function
  2894. ******************************************************************************/
  2895. static int _pt_request_pip_resume_scanning(struct device *dev,
  2896. int protect)
  2897. {
  2898. struct pt_core_data *cd = dev_get_drvdata(dev);
  2899. if (protect)
  2900. return pt_pip_resume_scanning(cd);
  2901. return pt_pip_resume_scanning_(cd);
  2902. }
  2903. /*******************************************************************************
  2904. * FUNCTION: pt_pip_get_param_
  2905. *
  2906. * SUMMARY: Sends a PIP command 0x05 Get Parameter to the DUT and returns
  2907. * the 32bit parameter value
  2908. *
  2909. * RETURN:
  2910. * 0 = success
  2911. * !0 = failure
  2912. *
  2913. * PARAMETERS:
  2914. * *cd - pointer to core data
  2915. * param_id - parameter ID to retrieve
  2916. * *value - value of DUT parameter
  2917. ******************************************************************************/
  2918. static int pt_pip_get_param_(struct pt_core_data *cd,
  2919. u8 param_id, u32 *value)
  2920. {
  2921. int write_length = 1;
  2922. u8 param[1] = { param_id };
  2923. u8 read_param_id;
  2924. int param_size;
  2925. u8 *ptr;
  2926. int rc = 0;
  2927. int i;
  2928. struct pt_hid_output hid_output = {
  2929. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_PARAM),
  2930. .write_length = write_length,
  2931. .write_buf = param,
  2932. };
  2933. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2934. if (rc)
  2935. return rc;
  2936. read_param_id = cd->response_buf[5];
  2937. if (read_param_id != param_id)
  2938. return -EPROTO;
  2939. param_size = cd->response_buf[6];
  2940. ptr = &cd->response_buf[7];
  2941. *value = 0;
  2942. for (i = 0; i < param_size; i++)
  2943. *value += ptr[i] << (i * 8);
  2944. return 0;
  2945. }
  2946. /*******************************************************************************
  2947. * FUNCTION: pt_pip_get_param
  2948. *
  2949. * SUMMARY: Protected call to pt_hid_output_get_param_ by a request exclusive
  2950. *
  2951. * RETURN:
  2952. * 0 = success
  2953. * !0 = failure
  2954. *
  2955. * PARAMETERS:
  2956. * *cd - pointer to core data
  2957. * param_id - parameter ID to retrieve
  2958. * *value - value of DUT parameter
  2959. ******************************************************************************/
  2960. static int pt_pip_get_param(struct pt_core_data *cd,
  2961. u8 param_id, u32 *value)
  2962. {
  2963. int rc;
  2964. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2965. if (rc < 0) {
  2966. pt_debug(cd->dev, DL_ERROR,
  2967. "%s: fail get exclusive ex=%p own=%p\n",
  2968. __func__, cd->exclusive_dev, cd->dev);
  2969. return rc;
  2970. }
  2971. rc = pt_pip_get_param_(cd, param_id, value);
  2972. if (release_exclusive(cd, cd->dev) < 0)
  2973. pt_debug(cd->dev, DL_ERROR,
  2974. "%s: fail to release exclusive\n", __func__);
  2975. return rc;
  2976. }
  2977. /*******************************************************************************
  2978. * FUNCTION: _pt_request_pip_get_param
  2979. *
  2980. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2981. * calls to the protected or unprotected call to pt_pip_get_param
  2982. *
  2983. * RETURN:
  2984. * 0 = success
  2985. * !0 = failure
  2986. *
  2987. * PARAMETERS:
  2988. * *dev - pointer to device structure
  2989. * protect - flag to call protected or non protected function
  2990. * param_id - parameter ID to retrieve
  2991. * *value - value of DUT parameter
  2992. ******************************************************************************/
  2993. int _pt_request_pip_get_param(struct device *dev,
  2994. int protect, u8 param_id, u32 *value)
  2995. {
  2996. struct pt_core_data *cd = dev_get_drvdata(dev);
  2997. if (protect)
  2998. return pt_pip_get_param(cd, param_id, value);
  2999. return pt_pip_get_param_(cd, param_id, value);
  3000. }
  3001. /*******************************************************************************
  3002. * FUNCTION: pt_pip_set_param_
  3003. *
  3004. * SUMMARY: Sends a PIP command 0x06 Set Parameter to the DUT writing the
  3005. * passed in value to flash
  3006. *
  3007. * RETURN:
  3008. * 0 = success
  3009. * !0 = failure
  3010. *
  3011. * PARAMETERS:
  3012. * *cd - pointer to core data
  3013. * param_id - parameter ID to set
  3014. * value - value to write
  3015. * size - size to write
  3016. ******************************************************************************/
  3017. static int pt_pip_set_param_(struct pt_core_data *cd,
  3018. u8 param_id, u32 value, u8 size)
  3019. {
  3020. u8 write_buf[6];
  3021. u8 *ptr = &write_buf[2];
  3022. int rc = 0;
  3023. int i;
  3024. struct pt_hid_output hid_output = {
  3025. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SET_PARAM),
  3026. .write_buf = write_buf,
  3027. };
  3028. write_buf[0] = param_id;
  3029. write_buf[1] = size;
  3030. for (i = 0; i < size; i++) {
  3031. ptr[i] = value & 0xFF;
  3032. value = value >> 8;
  3033. }
  3034. hid_output.write_length = 2 + size;
  3035. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3036. if (rc)
  3037. return rc;
  3038. if (param_id != cd->response_buf[5] || size != cd->response_buf[6])
  3039. return -EPROTO;
  3040. return 0;
  3041. }
  3042. /*******************************************************************************
  3043. * FUNCTION: pt_pip_set_param
  3044. *
  3045. * SUMMARY: Protected call to pt_hid_output_set_param_ by a request exclusive
  3046. *
  3047. * RETURN:
  3048. * 0 = success
  3049. * !0 = failure
  3050. *
  3051. * PARAMETERS:
  3052. * *cd - pointer to core data
  3053. * param_id - parameter ID to set
  3054. * value - value to write
  3055. * size - size to write
  3056. ******************************************************************************/
  3057. static int pt_pip_set_param(struct pt_core_data *cd,
  3058. u8 param_id, u32 value, u8 size)
  3059. {
  3060. int rc;
  3061. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3062. if (rc < 0) {
  3063. pt_debug(cd->dev, DL_ERROR,
  3064. "%s: fail get exclusive ex=%p own=%p\n",
  3065. __func__, cd->exclusive_dev, cd->dev);
  3066. return rc;
  3067. }
  3068. rc = pt_pip_set_param_(cd, param_id, value, size);
  3069. if (release_exclusive(cd, cd->dev) < 0)
  3070. pt_debug(cd->dev, DL_ERROR,
  3071. "%s: fail to release exclusive\n", __func__);
  3072. return rc;
  3073. }
  3074. /*******************************************************************************
  3075. * FUNCTION: _pt_request_pip_set_param
  3076. *
  3077. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3078. * calls to the protected or unprotected call to pt_pip_set_param
  3079. *
  3080. * RETURN:
  3081. * 0 = success
  3082. * !0 = failure
  3083. *
  3084. * PARAMETERS:
  3085. * *dev - pointer to device structure
  3086. * protect - flag to call protected or non-protected
  3087. * param_id - parameter ID to set
  3088. * value - value to write
  3089. * size - size to write
  3090. ******************************************************************************/
  3091. int _pt_request_pip_set_param(struct device *dev, int protect,
  3092. u8 param_id, u32 value, u8 size)
  3093. {
  3094. struct pt_core_data *cd = dev_get_drvdata(dev);
  3095. if (protect)
  3096. return pt_pip_set_param(cd, param_id, value, size);
  3097. return pt_pip_set_param_(cd, param_id, value, size);
  3098. }
  3099. /*******************************************************************************
  3100. * FUNCTION: _pt_pip_enter_easywake_state_
  3101. *
  3102. * SUMMARY: Sends a PIP command 0x09 Enter EasyWake State to the DUT
  3103. *
  3104. * RETURN:
  3105. * 0 = success
  3106. * !0 = failure
  3107. *
  3108. * PARAMETERS:
  3109. * *dev - pointer to device structure
  3110. * data - easywake guesture (Only used for PIP1.6 and earlier)
  3111. * *return_data - return status if easywake was entered
  3112. ******************************************************************************/
  3113. static int pt_hid_output_enter_easywake_state_(
  3114. struct pt_core_data *cd, u8 data, u8 *return_data)
  3115. {
  3116. int write_length = 1;
  3117. u8 param[1] = { data };
  3118. int rc = 0;
  3119. struct pt_hid_output hid_output = {
  3120. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_ENTER_EASYWAKE_STATE),
  3121. .write_length = write_length,
  3122. .write_buf = param,
  3123. };
  3124. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3125. if (rc)
  3126. return rc;
  3127. *return_data = cd->response_buf[5];
  3128. return rc;
  3129. }
  3130. /*******************************************************************************
  3131. * FUNCTION: pt_pip_verify_config_block_crc_
  3132. *
  3133. * SUMMARY: Sends the PIP "Verify Data Block CRC" (0x20) command to the DUT
  3134. *
  3135. * RETURN:
  3136. * 0 = success
  3137. * !0 = failure
  3138. *
  3139. * PARAMETERS:
  3140. * *cd - pointer the core data structure
  3141. * ebid - enumerated block ID
  3142. * *status - PIP command status
  3143. * calculated_crc - calculated CRC
  3144. * stored_crc - stored CRC in config area
  3145. ******************************************************************************/
  3146. static int pt_pip_verify_config_block_crc_(
  3147. struct pt_core_data *cd, u8 ebid, u8 *status,
  3148. u16 *calculated_crc, u16 *stored_crc)
  3149. {
  3150. int write_length = 1;
  3151. u8 param[1] = { ebid };
  3152. u8 *ptr;
  3153. int rc = 0;
  3154. struct pt_hid_output hid_output = {
  3155. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  3156. .write_length = write_length,
  3157. .write_buf = param,
  3158. };
  3159. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3160. if (rc)
  3161. return rc;
  3162. ptr = &cd->response_buf[5];
  3163. *status = ptr[0];
  3164. *calculated_crc = get_unaligned_le16(&ptr[1]);
  3165. *stored_crc = get_unaligned_le16(&ptr[3]);
  3166. return 0;
  3167. }
  3168. /*******************************************************************************
  3169. * FUNCTION: pt_pip_verify_config_block_crc
  3170. *
  3171. * SUMMARY: Protected call to pt_hid_output_verify_config_block_crc_() within
  3172. * an exclusive access to the DUT.
  3173. *
  3174. * RETURN:
  3175. * 0 = success
  3176. * !0 = failure
  3177. *
  3178. * PARAMETERS:
  3179. * *cd - pointer the core data structure
  3180. * ebid - enumerated block ID
  3181. * *status - PIP command status
  3182. * calculated_crc - calculated CRC
  3183. * stored_crc - stored CRC in config area
  3184. ******************************************************************************/
  3185. static int pt_pip_verify_config_block_crc(
  3186. struct pt_core_data *cd, u8 ebid, u8 *status,
  3187. u16 *calculated_crc, u16 *stored_crc)
  3188. {
  3189. int rc;
  3190. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3191. if (rc < 0) {
  3192. pt_debug(cd->dev, DL_ERROR,
  3193. "%s: fail get exclusive ex=%p own=%p\n",
  3194. __func__, cd->exclusive_dev, cd->dev);
  3195. return rc;
  3196. }
  3197. rc = pt_pip_verify_config_block_crc_(cd, ebid, status,
  3198. calculated_crc, stored_crc);
  3199. if (release_exclusive(cd, cd->dev) < 0)
  3200. pt_debug(cd->dev, DL_ERROR,
  3201. "%s: fail to release exclusive\n", __func__);
  3202. return rc;
  3203. }
  3204. /*******************************************************************************
  3205. * FUNCTION: _pt_request_pip_verify_config_block_crc
  3206. *
  3207. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3208. * calls to the protected or unprotected call to
  3209. * pt_pip_verify_config_block_crc_
  3210. *
  3211. * RETURN:
  3212. * 0 = success
  3213. * !0 = failure
  3214. *
  3215. * PARAMETERS:
  3216. * *dev - pointer to device structure
  3217. * protect - flag to call protected or non-protected
  3218. * ebid - enumerated block ID
  3219. * *status - PIP command status
  3220. * calculated_crc - calculated CRC
  3221. * stored_crc - stored CRC in config area
  3222. ******************************************************************************/
  3223. static int _pt_request_pip_verify_config_block_crc(
  3224. struct device *dev, int protect, u8 ebid, u8 *status,
  3225. u16 *calculated_crc, u16 *stored_crc)
  3226. {
  3227. struct pt_core_data *cd = dev_get_drvdata(dev);
  3228. if (protect)
  3229. return pt_pip_verify_config_block_crc(cd, ebid,
  3230. status, calculated_crc, stored_crc);
  3231. return pt_pip_verify_config_block_crc_(cd, ebid,
  3232. status, calculated_crc, stored_crc);
  3233. }
  3234. /*******************************************************************************
  3235. * FUNCTION: pt_pip_get_config_row_size_
  3236. *
  3237. * SUMMARY: Sends the PIP "Get Data Row Size" (0x21) command to the DUT
  3238. *
  3239. * RETURN:
  3240. * 0 = success
  3241. * !0 = failure
  3242. *
  3243. * PARAMETERS:
  3244. * *cd - pointer to core data
  3245. * protect - flag to call protected or non-protected
  3246. * *row_size - pointer to store the retrieved row size
  3247. ******************************************************************************/
  3248. static int pt_pip_get_config_row_size_(struct pt_core_data *cd,
  3249. u16 *row_size)
  3250. {
  3251. int rc = 0;
  3252. struct pt_hid_output hid_output = {
  3253. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_CONFIG_ROW_SIZE),
  3254. };
  3255. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3256. if (rc)
  3257. return rc;
  3258. *row_size = get_unaligned_le16(&cd->response_buf[5]);
  3259. return 0;
  3260. }
  3261. /*******************************************************************************
  3262. * FUNCTION: pt_pip_get_config_row_size
  3263. *
  3264. * SUMMARY: Protected call to pt_hid_output_get_config_row_size_ within
  3265. * an exclusive access to the DUT.
  3266. *
  3267. * RETURN:
  3268. * 0 = success
  3269. * !0 = failure
  3270. *
  3271. * PARAMETERS:
  3272. * *cd - pointer to core data
  3273. * protect - flag to call protected or non-protected
  3274. * *row_size - pointer to store the retrieved row size
  3275. ******************************************************************************/
  3276. static int pt_pip_get_config_row_size(struct pt_core_data *cd,
  3277. u16 *row_size)
  3278. {
  3279. int rc;
  3280. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3281. if (rc < 0) {
  3282. pt_debug(cd->dev, DL_ERROR,
  3283. "%s: fail get exclusive ex=%p own=%p\n",
  3284. __func__, cd->exclusive_dev, cd->dev);
  3285. return rc;
  3286. }
  3287. rc = pt_pip_get_config_row_size_(cd, row_size);
  3288. if (release_exclusive(cd, cd->dev) < 0)
  3289. pt_debug(cd->dev, DL_ERROR,
  3290. "%s: fail to release exclusive\n", __func__);
  3291. return rc;
  3292. }
  3293. /*******************************************************************************
  3294. * FUNCTION: _pt_request_pip_get_config_row_size
  3295. *
  3296. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3297. * calls to the protected or unprotected call to
  3298. * pt_pip_get_config_row_size_
  3299. *
  3300. * RETURN:
  3301. * 0 = success
  3302. * !0 = failure
  3303. *
  3304. * PARAMETERS:
  3305. * *dev - pointer to device structure
  3306. * protect - flag to call protected or non-protected
  3307. * *row_size - pointer to store the retrieved row size
  3308. ******************************************************************************/
  3309. static int _pt_request_pip_get_config_row_size(struct device *dev,
  3310. int protect, u16 *row_size)
  3311. {
  3312. struct pt_core_data *cd = dev_get_drvdata(dev);
  3313. if (protect)
  3314. return pt_pip_get_config_row_size(cd, row_size);
  3315. return pt_pip_get_config_row_size_(cd, row_size);
  3316. }
  3317. /*******************************************************************************
  3318. * FUNCTION: pt_pip1_read_data_block_
  3319. *
  3320. * SUMMARY: Sends the PIP "Read Data Block" (0x22) command to the DUT and print
  3321. * output data to the "read_buf" and update "crc".
  3322. *
  3323. * RETURN:
  3324. * 0 = success
  3325. * !0 = failure
  3326. *
  3327. * PARAMETERS:
  3328. * *cd - pointer to core data
  3329. * row_number - row number
  3330. * length - length of data to read
  3331. * ebid - block id
  3332. * *actual_read_len - Actual data length read
  3333. * *read_buf - pointer to the buffer to store read data
  3334. * read_buf_size - size of read_buf
  3335. * *crc - pointer to store CRC of row data
  3336. ******************************************************************************/
  3337. static int pt_pip1_read_data_block_(struct pt_core_data *cd,
  3338. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3339. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3340. {
  3341. int read_ebid;
  3342. int status;
  3343. int rc = 0;
  3344. int write_length = 5;
  3345. u8 write_buf[5];
  3346. u8 cmd_offset = 0;
  3347. u16 calc_crc;
  3348. struct pt_hid_output hid_output = {
  3349. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_READ_DATA_BLOCK),
  3350. .write_length = write_length,
  3351. .write_buf = write_buf,
  3352. };
  3353. write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3354. write_buf[cmd_offset++] = HI_BYTE(row_number);
  3355. write_buf[cmd_offset++] = LOW_BYTE(length);
  3356. write_buf[cmd_offset++] = HI_BYTE(length);
  3357. write_buf[cmd_offset++] = ebid;
  3358. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3359. if (rc)
  3360. return rc;
  3361. status = cd->response_buf[5];
  3362. if (status)
  3363. return status;
  3364. read_ebid = cd->response_buf[6];
  3365. if ((read_ebid != ebid) || (cd->response_buf[9] != 0))
  3366. return -EPROTO;
  3367. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  3368. if (length == 0 || *actual_read_len == 0)
  3369. return 0;
  3370. if (read_buf_size >= *actual_read_len)
  3371. memcpy(read_buf, &cd->response_buf[10], *actual_read_len);
  3372. else
  3373. return -EPROTO;
  3374. *crc = get_unaligned_le16(&cd->response_buf[*actual_read_len + 10]);
  3375. /* Validate Row Data CRC */
  3376. calc_crc = _pt_compute_crc(read_buf, *actual_read_len);
  3377. if (*crc == calc_crc) {
  3378. return 0;
  3379. } else {
  3380. pt_debug(cd->dev, DL_ERROR,
  3381. "%s: CRC Mismatch packet=0x%04X calc=0x%04X\n",
  3382. __func__, *crc, calc_crc);
  3383. return -EPROTO;
  3384. }
  3385. }
  3386. /*******************************************************************************
  3387. * FUNCTION: _pt_request_pip_read_data_block
  3388. *
  3389. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3390. * calls to pt_pip1_read_data_block_
  3391. *
  3392. * RETURN:
  3393. * 0 = success
  3394. * !0 = failure
  3395. *
  3396. * PARAMETERS:
  3397. * *dev - pointer to device structure
  3398. * row_number - row number
  3399. * length - length of data to read
  3400. * ebid - block id
  3401. * *actual_read_len - Actual data length read
  3402. * *read_buf - pointer to the buffer to store read data
  3403. * *crc - pointer to store CRC of row data
  3404. ******************************************************************************/
  3405. static int _pt_request_pip_read_data_block(struct device *dev,
  3406. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3407. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3408. {
  3409. struct pt_core_data *cd = dev_get_drvdata(dev);
  3410. return pt_pip1_read_data_block_(cd, row_number, length,
  3411. ebid, actual_read_len, read_buf, read_buf_size, crc);
  3412. }
  3413. /*******************************************************************************
  3414. * FUNCTION: pt_pip1_write_data_block_
  3415. *
  3416. * SUMMARY: Sends the PIP "Write Data Block" (0x23) command to the DUT and
  3417. * write data to the data block.
  3418. *
  3419. * RETURN:
  3420. * 0 = success
  3421. * !0 = failure
  3422. *
  3423. * PARAMETERS:
  3424. * *cd - pointer to core data
  3425. * row_number - row in config block to write to
  3426. * write_length - length of data to write
  3427. * ebid - enumerated block ID
  3428. * *write_buf - pointer to buffer to write
  3429. * *security_key - pointer to security key to allow write
  3430. * *actual_write_len - pointer to store data length actually written
  3431. ******************************************************************************/
  3432. static int pt_pip1_write_data_block_(struct pt_core_data *cd,
  3433. u16 row_number, u16 write_length, u8 ebid, u8 *write_buf,
  3434. u8 *security_key, u16 *actual_write_len)
  3435. {
  3436. /* row_number + write_len + ebid + security_key + crc */
  3437. int full_write_length = 2 + 2 + 1 + write_length + 8 + 2;
  3438. u8 *full_write_buf;
  3439. u8 cmd_offset = 0;
  3440. u16 crc;
  3441. int status;
  3442. int rc = 0;
  3443. int read_ebid;
  3444. u8 *data;
  3445. struct pt_hid_output hid_output = {
  3446. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_WRITE_DATA_BLOCK),
  3447. .write_length = full_write_length,
  3448. .timeout_ms = PT_PIP1_CMD_WRITE_CONF_BLOCK_TIMEOUT,
  3449. };
  3450. full_write_buf = kzalloc(full_write_length, GFP_KERNEL);
  3451. if (!full_write_buf)
  3452. return -ENOMEM;
  3453. hid_output.write_buf = full_write_buf;
  3454. full_write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3455. full_write_buf[cmd_offset++] = HI_BYTE(row_number);
  3456. full_write_buf[cmd_offset++] = LOW_BYTE(write_length);
  3457. full_write_buf[cmd_offset++] = HI_BYTE(write_length);
  3458. full_write_buf[cmd_offset++] = ebid;
  3459. data = &full_write_buf[cmd_offset];
  3460. memcpy(data, write_buf, write_length);
  3461. cmd_offset += write_length;
  3462. memcpy(&full_write_buf[cmd_offset], security_key, 8);
  3463. cmd_offset += 8;
  3464. crc = _pt_compute_crc(data, write_length);
  3465. full_write_buf[cmd_offset++] = LOW_BYTE(crc);
  3466. full_write_buf[cmd_offset++] = HI_BYTE(crc);
  3467. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3468. if (rc)
  3469. goto exit;
  3470. status = cd->response_buf[5];
  3471. if (status) {
  3472. rc = -EINVAL;
  3473. goto exit;
  3474. }
  3475. read_ebid = cd->response_buf[6];
  3476. if (read_ebid != ebid) {
  3477. rc = -EPROTO;
  3478. goto exit;
  3479. }
  3480. *actual_write_len = get_unaligned_le16(&cd->response_buf[7]);
  3481. exit:
  3482. kfree(full_write_buf);
  3483. return rc;
  3484. }
  3485. /*******************************************************************************
  3486. * FUNCTION: _pt_request_pip_write_data_block
  3487. *
  3488. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3489. * calls to the protected or unprotected call to pt_pip1_write_data_block_
  3490. *
  3491. * RETURN:
  3492. * 0 = success
  3493. * !0 = failure
  3494. *
  3495. * PARAMETERS:
  3496. * *dev - pointer to device structure
  3497. * protect - flag to call protected or non-protected
  3498. * row_number - row in config block to write to
  3499. * write_length - length of data to write
  3500. * ebid - enumerated block ID
  3501. * *write_buf - pointer to buffer to write
  3502. * *security_key - pointer to security key to allow write
  3503. * *actual_write_len - pointer to store data length actually written
  3504. ******************************************************************************/
  3505. static int _pt_request_pip_write_data_block(struct device *dev,
  3506. u16 row_number, u16 write_length, u8 ebid,
  3507. u8 *write_buf, u8 *security_key, u16 *actual_write_len)
  3508. {
  3509. struct pt_core_data *cd = dev_get_drvdata(dev);
  3510. return pt_pip1_write_data_block_(cd, row_number,
  3511. write_length, ebid, write_buf, security_key,
  3512. actual_write_len);
  3513. }
  3514. /*******************************************************************************
  3515. * FUNCTION: pt_pip_get_data_structure_
  3516. *
  3517. * SUMMARY: Sends the PIP "Retrieve Data Structure" (0x24) command to the DUT
  3518. * returning a structure of data defined by data_id
  3519. *
  3520. * RETURN:
  3521. * 0 = success
  3522. * !0 = failure
  3523. *
  3524. * PARAMETERS:
  3525. * *cd - pointer to core data
  3526. * read_offset - read pointer offset
  3527. * read_length - length of data to read
  3528. * data_id - data ID to read
  3529. * *status - pointer to store the read response status
  3530. * *data_format - pointer to store format of data read
  3531. * *actual_read_len - pointer to store data length actually read
  3532. * *data - pointer to store data read
  3533. ******************************************************************************/
  3534. static int pt_pip_get_data_structure_(
  3535. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3536. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3537. u8 *data)
  3538. {
  3539. int rc = 0;
  3540. u16 total_read_len = 0;
  3541. u16 read_len;
  3542. u16 off_buf = 0;
  3543. u8 write_buf[5];
  3544. u8 read_data_id;
  3545. struct pt_hid_output hid_output = {
  3546. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_DATA_STRUCTURE),
  3547. .write_length = 5,
  3548. .write_buf = write_buf,
  3549. };
  3550. again:
  3551. write_buf[0] = LOW_BYTE(read_offset);
  3552. write_buf[1] = HI_BYTE(read_offset);
  3553. write_buf[2] = LOW_BYTE(read_length);
  3554. write_buf[3] = HI_BYTE(read_length);
  3555. write_buf[4] = data_id;
  3556. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3557. if (rc)
  3558. return rc;
  3559. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  3560. goto set_status;
  3561. read_data_id = cd->response_buf[6];
  3562. if (read_data_id != data_id)
  3563. return -EPROTO;
  3564. read_len = get_unaligned_le16(&cd->response_buf[7]);
  3565. if (read_len && data) {
  3566. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  3567. total_read_len += read_len;
  3568. if (read_len < read_length) {
  3569. read_offset += read_len;
  3570. off_buf += read_len;
  3571. read_length -= read_len;
  3572. goto again;
  3573. }
  3574. }
  3575. if (data_format)
  3576. *data_format = cd->response_buf[9];
  3577. if (actual_read_len)
  3578. *actual_read_len = total_read_len;
  3579. set_status:
  3580. if (status)
  3581. *status = cd->response_buf[5];
  3582. return rc;
  3583. }
  3584. /*******************************************************************************
  3585. * FUNCTION: pt_pip_get_data_structure
  3586. *
  3587. * SUMMARY: Protected call to pt_hid_output_get_data_structure within
  3588. * an exclusive access to the DUT.
  3589. *
  3590. * RETURN:
  3591. * 0 = success
  3592. * !0 = failure
  3593. *
  3594. * PARAMETERS:
  3595. * *cd - pointer to core data
  3596. * read_offset - read pointer offset
  3597. * read_length - length of data to read
  3598. * data_id - data ID to read
  3599. * *status - pointer to store the read response status
  3600. * *data_format - pointer to store format of data read
  3601. * *actual_read_len - pointer to store data length actually read
  3602. * *data - pointer to store data read
  3603. ******************************************************************************/
  3604. static int pt_pip_get_data_structure(
  3605. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3606. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3607. u8 *data)
  3608. {
  3609. int rc;
  3610. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3611. if (rc < 0) {
  3612. pt_debug(cd->dev, DL_ERROR,
  3613. "%s: fail get exclusive ex=%p own=%p\n",
  3614. __func__, cd->exclusive_dev, cd->dev);
  3615. return rc;
  3616. }
  3617. rc = pt_pip_get_data_structure_(cd, read_offset,
  3618. read_length, data_id, status, data_format,
  3619. actual_read_len, data);
  3620. if (release_exclusive(cd, cd->dev) < 0)
  3621. pt_debug(cd->dev, DL_ERROR,
  3622. "%s: fail to release exclusive\n", __func__);
  3623. return rc;
  3624. }
  3625. /*******************************************************************************
  3626. * FUNCTION: _pt_request_pip_get_data_structure
  3627. *
  3628. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3629. * calls to the protected or unprotected call to
  3630. * pt_pip_get_data_structure
  3631. *
  3632. * RETURN:
  3633. * 0 = success
  3634. * !0 = failure
  3635. *
  3636. * PARAMETERS:
  3637. * *dev - pointer to device structure
  3638. * protect - flag to call protected or non-protected
  3639. * read_offset - read pointer offset
  3640. * read_length - length of data to read
  3641. * data_id - data ID to read
  3642. * *status - pointer to store the read response status
  3643. * *data_format - pointer to store format of data read
  3644. * *actual_read_len - pointer to store data length actually read
  3645. * *data - pointer to store data read
  3646. ******************************************************************************/
  3647. static int _pt_request_pip_get_data_structure(struct device *dev,
  3648. int protect, u16 read_offset, u16 read_length, u8 data_id,
  3649. u8 *status, u8 *data_format, u16 *actual_read_len, u8 *data)
  3650. {
  3651. struct pt_core_data *cd = dev_get_drvdata(dev);
  3652. if (protect)
  3653. return pt_pip_get_data_structure(cd,
  3654. read_offset, read_length, data_id, status,
  3655. data_format, actual_read_len, data);
  3656. return pt_pip_get_data_structure_(cd,
  3657. read_offset, read_length, data_id, status,
  3658. data_format, actual_read_len, data);
  3659. }
  3660. /*******************************************************************************
  3661. * FUNCTION: _pt_manage_local_cal_data
  3662. *
  3663. * SUMMARY: This function manages storing or restoring a copy of the Firmware
  3664. * CALIBRATION data. It stores it in a local static array and can be
  3665. * cleared, loaded or used to restore the CAL data back to the running FW.
  3666. * The CAL data is read or restored by use of the PIP1 commands:
  3667. * - READ_DATA_BLOCK (0x22)
  3668. * - WRITE_DATA_BLOCK (0x23)
  3669. *
  3670. * RETURN:
  3671. * 0 = success
  3672. * !0 = failure
  3673. *
  3674. * PARAMETERS:
  3675. * *dev - pointer to device structure
  3676. * action - One of the following actions:
  3677. * - PT_CAL_DATA_SAVE
  3678. * - PT_CAL_DATA_RESTORE
  3679. * - PT_CAL_DATA_CLEAR
  3680. * - PT_CAL_DATA_SIZE
  3681. * *size - pointer to the number of bytes transferred
  3682. * *crc - pointer to Chip ID CRC that the CAL data was retrieved from
  3683. ******************************************************************************/
  3684. static int _pt_manage_local_cal_data(struct device *dev, u8 action, u16 *size,
  3685. unsigned short *crc)
  3686. {
  3687. struct pt_core_data *cd = dev_get_drvdata(dev);
  3688. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  3689. unsigned short calc_id_crc = 0;
  3690. static u8 *cal_cache_data;
  3691. static u16 cal_cache_len;
  3692. static unsigned short cal_cache_chip_id;
  3693. int rc = 0;
  3694. u8 *tmp_data = NULL;
  3695. u8 row_number = 0;
  3696. u8 prefix[20];
  3697. u16 cal_size = 0;
  3698. u16 transfer_size;
  3699. u16 act_trans_len = 0;
  3700. u16 byte_offset = 0;
  3701. u16 cal_blk_size;
  3702. u16 total_rows;
  3703. u16 remain_bytes;
  3704. u16 data_block_crc;
  3705. u16 buf_size = 12;
  3706. pt_debug(dev, DL_INFO, "%s: ATM - CAL Cache action=%d\n",
  3707. __func__, action);
  3708. switch (action) {
  3709. case PT_CAL_DATA_SAVE:
  3710. /* Read the size of the CAL block and calculate # rows */
  3711. tmp_data = kzalloc(buf_size, GFP_KERNEL);
  3712. if (!tmp_data) {
  3713. rc = -ENOMEM;
  3714. goto exit;
  3715. }
  3716. /*
  3717. * Don't check rc as doing a read size will give a false
  3718. * error on the CRC check.
  3719. */
  3720. rc = pt_pip1_read_data_block_(cd, row_number, 0, PT_CAL_EBID,
  3721. &act_trans_len, tmp_data, buf_size, &data_block_crc);
  3722. cal_blk_size = act_trans_len;
  3723. kfree(tmp_data);
  3724. pt_debug(dev, DL_INFO,
  3725. "%s: CAL Cache size=%d FW CAL Size=%d\n",
  3726. __func__, cal_cache_len, cal_blk_size);
  3727. /* Safety net to ensure we didn't read incorrect size */
  3728. if (cal_blk_size > PT_CAL_DATA_MAX_SIZE) {
  3729. pt_debug(dev, DL_ERROR, "%s: Alloc struct Failed\n",
  3730. __func__);
  3731. rc = 1;
  3732. goto exit;
  3733. }
  3734. /* Panels could have diff CAL sizes, Re-allocate the cache */
  3735. if (cal_blk_size != cal_cache_len) {
  3736. kfree(cal_cache_data);
  3737. cal_cache_data = kzalloc(cal_blk_size + 2,
  3738. GFP_KERNEL);
  3739. if (!cal_cache_data) {
  3740. rc = -ENOMEM;
  3741. goto exit;
  3742. }
  3743. pt_debug(dev, DL_INFO, "%s: CAL Cache Allocated\n",
  3744. __func__);
  3745. }
  3746. memset(&cal_cache_data[0], 0, cal_blk_size + 2);
  3747. /* Calculate how many rows [0-n] (PIP Transactions) */
  3748. total_rows = (cal_blk_size / PT_CAL_DATA_ROW_SIZE) - 1;
  3749. remain_bytes = cal_blk_size % PT_CAL_DATA_ROW_SIZE;
  3750. /* Add row if we have a last partial row */
  3751. if (remain_bytes > 0)
  3752. total_rows++;
  3753. pt_debug(dev, DL_INFO,
  3754. "%s: CAL size=%d rows=[0-%d] partial row bytes=%d\n",
  3755. __func__, cal_blk_size, total_rows, remain_bytes);
  3756. /* Read all rows unless an error occurs */
  3757. rc = 0;
  3758. while (rc == 0 && row_number <= total_rows) {
  3759. act_trans_len = 0;
  3760. if (remain_bytes > 0 && row_number == total_rows)
  3761. transfer_size = remain_bytes;
  3762. else
  3763. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3764. rc = pt_pip1_read_data_block_(cd, row_number,
  3765. transfer_size, PT_CAL_EBID,
  3766. &act_trans_len,
  3767. &cal_cache_data[byte_offset], cal_blk_size + 2,
  3768. &data_block_crc);
  3769. if (rc) {
  3770. /* Error occurred, exit loop */
  3771. cal_size = 0;
  3772. break;
  3773. }
  3774. pt_debug(dev, DL_INFO,
  3775. "%s: CAL read rc=%d actual read len=%d\n",
  3776. __func__, rc, act_trans_len);
  3777. byte_offset += act_trans_len;
  3778. cal_size = byte_offset;
  3779. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3780. pt_pr_buf(dev, DL_INFO,
  3781. &cal_cache_data[byte_offset - act_trans_len],
  3782. act_trans_len, prefix);
  3783. row_number++;
  3784. }
  3785. if (cal_size > 0) {
  3786. /* Save a CRC of the chip info the CAL was saved from */
  3787. calc_id_crc = crc_ccitt_calculate(
  3788. (u8 *)&ttdata->chip_rev, 4 + PT_UID_SIZE);
  3789. cal_cache_chip_id = calc_id_crc;
  3790. cal_cache_len = cal_size;
  3791. pt_debug(dev, DL_INFO,
  3792. "%s: CAL Cache: CRC=0x%04X Total Size=%d\n",
  3793. __func__, calc_id_crc, cal_size);
  3794. }
  3795. *size = cal_size;
  3796. *crc = calc_id_crc;
  3797. break;
  3798. case PT_CAL_DATA_RESTORE:
  3799. cal_size = cal_cache_len;
  3800. while ((rc == 0) && (byte_offset < cal_size)) {
  3801. if (cal_size - byte_offset > PT_CAL_DATA_ROW_SIZE)
  3802. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3803. else
  3804. transfer_size = cal_size - byte_offset;
  3805. rc = pt_pip1_write_data_block_(cd, row_number,
  3806. transfer_size, PT_CAL_EBID,
  3807. &cal_cache_data[byte_offset],
  3808. (u8 *)pt_data_block_security_key,
  3809. &act_trans_len);
  3810. byte_offset += act_trans_len;
  3811. pt_debug(dev, DL_INFO, "%s: CAL write byte offset=%d\n",
  3812. __func__, byte_offset);
  3813. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3814. pt_pr_buf(dev, DL_INFO,
  3815. &cal_cache_data[byte_offset - act_trans_len],
  3816. act_trans_len, prefix);
  3817. if ((byte_offset > cal_size) ||
  3818. (act_trans_len != transfer_size))
  3819. rc = -EIO;
  3820. row_number++;
  3821. }
  3822. *size = byte_offset;
  3823. *crc = cal_cache_chip_id;
  3824. break;
  3825. case PT_CAL_DATA_CLEAR:
  3826. if (cal_cache_data)
  3827. memset(&cal_cache_data[0], 0, cal_cache_len);
  3828. cal_cache_len = 0;
  3829. cal_cache_chip_id = 0;
  3830. *size = 0;
  3831. *crc = 0;
  3832. break;
  3833. case PT_CAL_DATA_INFO:
  3834. default:
  3835. *size = cal_cache_len;
  3836. *crc = cal_cache_chip_id;
  3837. pt_debug(dev, DL_INFO,
  3838. "%s: CAL Cache: CRC=%04X Total Size=%d\n",
  3839. __func__, cal_cache_chip_id,
  3840. cal_cache_len);
  3841. break;
  3842. }
  3843. exit:
  3844. pt_debug(dev, DL_INFO,
  3845. "%s: CAL Cache exit: rc=%d CRC=0x%04X Total Size=%d\n",
  3846. __func__, rc, *crc, *size);
  3847. return rc;
  3848. }
  3849. /*******************************************************************************
  3850. * FUNCTION: pt_pip_run_selftest_
  3851. *
  3852. * SUMMARY: Sends the PIP "Run Self Test" (0x26) command to the DUT
  3853. * to execute a FW built in self test
  3854. *
  3855. * RETURN:
  3856. * 0 = success
  3857. * !0 = failure
  3858. *
  3859. * PARAMETERS:
  3860. * *cd - pointer to core data
  3861. * test_id - enumerated test ID to run
  3862. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3863. * *status - pointer to store the read response status
  3864. * *summary_results - pointer to store the results summary
  3865. * *results_available - pointer to store if results are available
  3866. *****************************************************************************/
  3867. static int pt_pip_run_selftest_(
  3868. struct pt_core_data *cd, u8 test_id,
  3869. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3870. u8 *results_available)
  3871. {
  3872. int rc = 0;
  3873. u8 write_buf[2];
  3874. struct pt_hid_output hid_output = {
  3875. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RUN_SELF_TEST),
  3876. .write_length = 2,
  3877. .write_buf = write_buf,
  3878. .timeout_ms = PT_PIP1_CMD_RUN_SELF_TEST_TIMEOUT,
  3879. };
  3880. write_buf[0] = test_id;
  3881. write_buf[1] = write_idacs_to_flash;
  3882. if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  3883. hid_output.write_length = 1;
  3884. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3885. if (rc)
  3886. return rc;
  3887. if (status)
  3888. *status = cd->response_buf[5];
  3889. if (summary_result)
  3890. *summary_result = cd->response_buf[6];
  3891. /* results_available only available before PIP 1.03 */
  3892. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 3)) {
  3893. if (results_available)
  3894. *results_available = cd->response_buf[7];
  3895. }
  3896. return rc;
  3897. }
  3898. /*******************************************************************************
  3899. * FUNCTION: pt_pip_run_selftest
  3900. *
  3901. * SUMMARY: Protected call to pt_hid_output_run_selftest within
  3902. * an exclusive access to the DUT.
  3903. *
  3904. * RETURN:
  3905. * 0 = success
  3906. * !0 = failure
  3907. *
  3908. * PARAMETERS:
  3909. * *cd - pointer to core data
  3910. * test_id - enumerated test ID to run
  3911. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3912. * *status - pointer to store the read response status
  3913. * *summary_results - pointer to store the results summary
  3914. * *results_available - pointer to store if results are available
  3915. ******************************************************************************/
  3916. static int pt_pip_run_selftest(
  3917. struct pt_core_data *cd, u8 test_id,
  3918. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3919. u8 *results_available)
  3920. {
  3921. int rc;
  3922. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3923. if (rc < 0) {
  3924. pt_debug(cd->dev, DL_ERROR,
  3925. "%s: fail get exclusive ex=%p own=%p\n",
  3926. __func__, cd->exclusive_dev, cd->dev);
  3927. return rc;
  3928. }
  3929. rc = pt_pip_run_selftest_(cd, test_id,
  3930. write_idacs_to_flash, status, summary_result,
  3931. results_available);
  3932. if (release_exclusive(cd, cd->dev) < 0)
  3933. pt_debug(cd->dev, DL_ERROR,
  3934. "%s: fail to release exclusive\n", __func__);
  3935. return rc;
  3936. }
  3937. /*******************************************************************************
  3938. * FUNCTION: _pt_request_pip_run_selftest
  3939. *
  3940. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3941. * calls to the protected or unprotected call to pt_pip_run_selftest
  3942. *
  3943. * RETURN:
  3944. * 0 = success
  3945. * !0 = failure
  3946. *
  3947. * PARAMETERS:
  3948. * *dev - pointer to device structure
  3949. * protect - flag to call protected or non-protected
  3950. * test_id - enumerated test ID to run
  3951. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3952. * *status - pointer to store the read response status
  3953. * *summary_results - pointer to store the results summary
  3954. * *results_available - pointer to store if results are available
  3955. ******************************************************************************/
  3956. static int _pt_request_pip_run_selftest(struct device *dev,
  3957. int protect, u8 test_id, u8 write_idacs_to_flash, u8 *status,
  3958. u8 *summary_result, u8 *results_available)
  3959. {
  3960. struct pt_core_data *cd = dev_get_drvdata(dev);
  3961. if (protect)
  3962. return pt_pip_run_selftest(cd, test_id,
  3963. write_idacs_to_flash, status, summary_result,
  3964. results_available);
  3965. return pt_pip_run_selftest_(cd, test_id,
  3966. write_idacs_to_flash, status, summary_result,
  3967. results_available);
  3968. }
  3969. /*******************************************************************************
  3970. * FUNCTION: _pt_pip_get_selftest_result_
  3971. *
  3972. * SUMMARY: Sends the PIP "Get Self Test Results" (0x27) command to the DUT
  3973. * to retrieve the self test results from the self test already executed
  3974. *
  3975. * RETURN:
  3976. * 0 = success
  3977. * !0 = failure
  3978. *
  3979. * PARAMETERS:
  3980. * *cd - pointer to core data
  3981. * read_offset - read pointer offset
  3982. * read_length - length of data to read
  3983. * test_id - enumerated test ID to read selftest results from
  3984. * *status - pointer to store the read response status
  3985. * *actual_read_len - pointer to store data length actually read
  3986. * *status - pointer to where the cmd response statas is stored
  3987. ******************************************************************************/
  3988. static int pt_pip_get_selftest_result_(
  3989. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3990. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  3991. {
  3992. int rc = 0;
  3993. u16 total_read_len = 0;
  3994. u16 read_len;
  3995. u16 off_buf = 0;
  3996. u8 write_buf[5];
  3997. u8 read_test_id;
  3998. bool repeat;
  3999. struct pt_hid_output hid_output = {
  4000. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SELF_TEST_RESULT),
  4001. .write_length = 5,
  4002. .write_buf = write_buf,
  4003. };
  4004. /*
  4005. * Do not repeat reading for Auto Shorts test
  4006. * when PIP version < 1.3
  4007. */
  4008. repeat = IS_PIP_VER_GE(&cd->sysinfo, 1, 3)
  4009. || test_id != PT_ST_ID_AUTOSHORTS;
  4010. again:
  4011. write_buf[0] = LOW_BYTE(read_offset);
  4012. write_buf[1] = HI_BYTE(read_offset);
  4013. write_buf[2] = LOW_BYTE(read_length);
  4014. write_buf[3] = HI_BYTE(read_length);
  4015. write_buf[4] = test_id;
  4016. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4017. if (rc)
  4018. return rc;
  4019. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  4020. goto set_status;
  4021. read_test_id = cd->response_buf[6];
  4022. if (read_test_id != test_id)
  4023. return -EPROTO;
  4024. read_len = get_unaligned_le16(&cd->response_buf[7]);
  4025. if (read_len && data) {
  4026. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  4027. total_read_len += read_len;
  4028. if (repeat && read_len < read_length) {
  4029. read_offset += read_len;
  4030. off_buf += read_len;
  4031. read_length -= read_len;
  4032. goto again;
  4033. }
  4034. }
  4035. if (actual_read_len)
  4036. *actual_read_len = total_read_len;
  4037. set_status:
  4038. if (status)
  4039. *status = cd->response_buf[5];
  4040. return rc;
  4041. }
  4042. /*******************************************************************************
  4043. * FUNCTION: _pt_pip_get_selftest_result
  4044. *
  4045. * SUMMARY: Protected call to pt_hid_output_get_selftest_result by exclusive
  4046. * access to the DUT
  4047. *
  4048. * RETURN:
  4049. * 0 = success
  4050. * !0 = failure
  4051. *
  4052. * PARAMETERS:
  4053. * *cd - pointer to core data
  4054. * read_offset - read pointer offset
  4055. * read_length - length of data to read
  4056. * test_id - enumerated test ID to read selftest results from
  4057. * *status - pointer to store the read response status
  4058. * *actual_read_len - pointer to store data length actually read
  4059. * *status - pointer to where the cmd response statas is stored
  4060. ******************************************************************************/
  4061. static int pt_pip_get_selftest_result(
  4062. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4063. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4064. {
  4065. int rc;
  4066. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4067. if (rc < 0) {
  4068. pt_debug(cd->dev, DL_ERROR,
  4069. "%s: fail get exclusive ex=%p own=%p\n",
  4070. __func__, cd->exclusive_dev, cd->dev);
  4071. return rc;
  4072. }
  4073. rc = pt_pip_get_selftest_result_(cd, read_offset,
  4074. read_length, test_id, status, actual_read_len, data);
  4075. if (release_exclusive(cd, cd->dev) < 0)
  4076. pt_debug(cd->dev, DL_ERROR,
  4077. "%s: fail to release exclusive\n", __func__);
  4078. return rc;
  4079. }
  4080. /*******************************************************************************
  4081. * FUNCTION: _pt_request_pip_get_selftest_result
  4082. *
  4083. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4084. * calls to the protected or unprotected call to pt_pip_get_selftest_result
  4085. *
  4086. * RETURN:
  4087. * 0 = success
  4088. * !0 = failure
  4089. *
  4090. * PARAMETERS:
  4091. * *dev - pointer to device structure
  4092. * protect - flag to call protected or non-protected
  4093. * read_offset - read pointer offset
  4094. * read_length - length of data to read
  4095. * test_id - enumerated test ID to read selftest results from
  4096. * *status - pointer to store the read response status
  4097. * *actual_read_len - pointer to store data length actually read
  4098. * *data - pointer to where the data read is stored
  4099. ******************************************************************************/
  4100. static int _pt_request_pip_get_selftest_result(struct device *dev,
  4101. int protect, u16 read_offset, u16 read_length, u8 test_id,
  4102. u8 *status, u16 *actual_read_len, u8 *data)
  4103. {
  4104. struct pt_core_data *cd = dev_get_drvdata(dev);
  4105. if (protect)
  4106. return pt_pip_get_selftest_result(cd, read_offset,
  4107. read_length, test_id, status, actual_read_len,
  4108. data);
  4109. return pt_pip_get_selftest_result_(cd, read_offset,
  4110. read_length, test_id, status, actual_read_len,
  4111. data);
  4112. }
  4113. /*******************************************************************************
  4114. * FUNCTION: _pt_pip_load_self_test_param
  4115. *
  4116. * SUMMARY: Sends the PIP "Load Self Test Parameters" (0x25) command to the DUT
  4117. * to load parameters needed by a self test
  4118. *
  4119. * RETURN:
  4120. * 0 = success
  4121. * !0 = failure
  4122. *
  4123. * PARAMETERS:
  4124. * *cd - pointer to core data
  4125. * self_test_id - enumerated test ID for which the parmeters belong
  4126. * load_offset - mem offset to where to load parameters
  4127. * load_length - length of parameter data to load
  4128. * *parameters - pointer to list of parameter data
  4129. * *status - pointer to store the response status
  4130. * *ret_test_id - pointer to returned test id the parameters were stored
  4131. * *act_load_len - pointer to store the actual load length that was writen
  4132. ******************************************************************************/
  4133. static int pt_pip_load_self_test_param_(struct pt_core_data *cd,
  4134. u8 self_test_id, u16 load_offset, u16 load_length,
  4135. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4136. {
  4137. int rc = 0;
  4138. int i;
  4139. u8 write_buf[PT_MAX_PIP1_MSG_SIZE];
  4140. struct pt_hid_output hid_output = {
  4141. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_LOAD_SELF_TEST_PARAM),
  4142. .write_length = 5 + load_length,
  4143. .write_buf = write_buf,
  4144. .timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT,
  4145. };
  4146. write_buf[0] = LOW_BYTE(load_offset);
  4147. write_buf[1] = HI_BYTE(load_offset);
  4148. write_buf[2] = LOW_BYTE(load_length);
  4149. write_buf[3] = HI_BYTE(load_length);
  4150. write_buf[4] = self_test_id;
  4151. for (i = 0; i < load_length; i++)
  4152. write_buf[i + 5] = parameters[i];
  4153. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4154. if (rc)
  4155. return rc;
  4156. if (status)
  4157. *status = cd->response_buf[5];
  4158. if (ret_test_id)
  4159. *ret_test_id = cd->response_buf[6];
  4160. if (act_load_len)
  4161. *act_load_len = get_unaligned_le16(&cd->response_buf[7]);
  4162. return rc;
  4163. }
  4164. /*******************************************************************************
  4165. * FUNCTION: pt_pip_load_self_test_param
  4166. *
  4167. * SUMMARY: Protected call to pt_pip_load_self_test_param_ within an exclusive
  4168. * access to the DUT.
  4169. *
  4170. * RETURN:
  4171. * 0 = success
  4172. * !0 = failure
  4173. *
  4174. * PARAMETERS:
  4175. * *cd - pointer to core data
  4176. * self_test_id - enumerated test ID for which the parmeters belong
  4177. * load_offset - mem offset to where to load parameters
  4178. * load_length - length of parameter data to load
  4179. * *parameters - pointer to list of parameter data
  4180. * *status - pointer to store the response status
  4181. * *ret_test_id - pointer to returned test id the parameters were stored
  4182. * *act_load_len - pointer to store the actual load length that was writen
  4183. ******************************************************************************/
  4184. static int pt_pip_load_self_test_param(struct pt_core_data *cd,
  4185. u8 self_test_id, u16 load_offset, u16 load_length,
  4186. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4187. {
  4188. int rc;
  4189. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4190. if (rc < 0) {
  4191. pt_debug(cd->dev, DL_ERROR,
  4192. "%s: fail get exclusive ex=%p own=%p\n",
  4193. __func__, cd->exclusive_dev, cd->dev);
  4194. return rc;
  4195. }
  4196. rc = pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4197. load_length, parameters, status, ret_test_id, act_load_len);
  4198. if (release_exclusive(cd, cd->dev) < 0)
  4199. pt_debug(cd->dev, DL_ERROR,
  4200. "%s: fail to release exclusive\n", __func__);
  4201. return rc;
  4202. }
  4203. /*******************************************************************************
  4204. * FUNCTION: _pt_request_pip_load_self_test_param
  4205. *
  4206. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4207. * calls to the protected or unprotected call to
  4208. * pt_pip_load_self_test_param
  4209. *
  4210. * RETURN:
  4211. * 0 = success
  4212. * !0 = failure
  4213. *
  4214. * PARAMETERS:
  4215. * *dev - pointer to device structure
  4216. * protect - flag to call protected or non-protected
  4217. * self_test_id - enumerated test ID for which the parmeters belong
  4218. * load_offset - mem offset to where to load parameters
  4219. * load_length - length of parameter data to load
  4220. * *parameters - pointer to list of parameter data
  4221. * *status - pointer to store the response status
  4222. * *ret_test_id - pointer to returned test id the parameters were stored
  4223. * *act_load_len - pointer to store the actual load length that was writen
  4224. ******************************************************************************/
  4225. static int _pt_request_pip_load_self_test_param(struct device *dev,
  4226. int protect, u8 self_test_id, u16 load_offset, u16 load_length,
  4227. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4228. {
  4229. struct pt_core_data *cd = dev_get_drvdata(dev);
  4230. if (protect)
  4231. return pt_pip_load_self_test_param(cd, self_test_id,
  4232. load_offset, load_length, parameters, status, ret_test_id,
  4233. act_load_len);
  4234. return pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4235. load_length, parameters, status, ret_test_id, act_load_len);
  4236. }
  4237. /*******************************************************************************
  4238. * FUNCTION: pt_pip_calibrate_ext_
  4239. *
  4240. * SUMMARY: Send the PIP1 Extended Calibrate command (0x30) to the DUT waiting
  4241. * for the response
  4242. *
  4243. * NOTE: This calibrate command requires the DUT to support PIP version >= 1.10
  4244. *
  4245. * RETURN:
  4246. * 0 = success
  4247. * !0 = failure
  4248. *
  4249. * PARAMETERS:
  4250. * *cd - pointer to core data
  4251. * *cal_data - pointer to extended calibration data structure
  4252. * *status - pointer to where the command response status is stored
  4253. ******************************************************************************/
  4254. static int pt_pip_calibrate_ext_(struct pt_core_data *cd,
  4255. struct pt_cal_ext_data *cal_data, u8 *status)
  4256. {
  4257. int rc = 0;
  4258. int write_length = 4;
  4259. u8 write_buf[4];
  4260. u16 size = 0;
  4261. unsigned short crc = 0;
  4262. struct pt_hid_output hid_output = {
  4263. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_DEVICE_EXTENDED),
  4264. .write_length = write_length,
  4265. .write_buf = write_buf,
  4266. .timeout_ms = PT_PIP1_CMD_CALIBRATE_EXT_TIMEOUT,
  4267. };
  4268. if (cal_data == NULL)
  4269. return -EINVAL;
  4270. memcpy(write_buf, cal_data, sizeof(struct pt_cal_ext_data));
  4271. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4272. if (rc)
  4273. return rc;
  4274. if (status)
  4275. *status = cd->response_buf[5];
  4276. /*
  4277. * When doing a calibration on a flashless DUT, save CAL data in
  4278. * the TTDL cache on any successful calibration
  4279. */
  4280. if (*status == 0 && cd->cal_cache_in_host) {
  4281. pt_debug(cd->dev, DL_INFO, "%s: Retrieve and Save CAL\n",
  4282. __func__);
  4283. rc = _pt_manage_local_cal_data(cd->dev, PT_CAL_DATA_SAVE,
  4284. &size, &crc);
  4285. if (rc)
  4286. pt_debug(cd->dev, DL_ERROR,
  4287. "%s: Error Saving CAL rc=%d\n", __func__, rc);
  4288. else
  4289. pt_debug(cd->dev, DL_INFO,
  4290. "%s: Saved CAL: chip ID=0x%04X size=%d\n",
  4291. __func__, crc, size);
  4292. }
  4293. return 0;
  4294. }
  4295. /*******************************************************************************
  4296. * FUNCTION: pt_pip_calibrate_ext
  4297. *
  4298. * SUMMARY: Protected call to pt_pip_calibrate_ext_ by exclusive access to the
  4299. * DUT
  4300. *
  4301. * RETURN:
  4302. * 0 = success
  4303. * !0 = failure
  4304. *
  4305. * PARAMETERS:
  4306. * *cd - pointer to core data
  4307. * *cal_data - pointer to extended calibration data structure
  4308. * *status - pointer to where the command response status is stored
  4309. ******************************************************************************/
  4310. static int pt_pip_calibrate_ext(struct pt_core_data *cd,
  4311. struct pt_cal_ext_data *cal_data, u8 *status)
  4312. {
  4313. int rc;
  4314. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4315. if (rc < 0) {
  4316. pt_debug(cd->dev, DL_ERROR,
  4317. "%s: fail get exclusive ex=%p own=%p\n",
  4318. __func__, cd->exclusive_dev, cd->dev);
  4319. return rc;
  4320. }
  4321. rc = pt_pip_calibrate_ext_(cd, cal_data, status);
  4322. if (release_exclusive(cd, cd->dev) < 0)
  4323. pt_debug(cd->dev, DL_ERROR,
  4324. "%s: fail to release exclusive\n", __func__);
  4325. return rc;
  4326. }
  4327. /*******************************************************************************
  4328. * FUNCTION: _pt_request_pip_calibrate_ext
  4329. *
  4330. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4331. * calls to the protected or unprotected call to pt_pip_calibrate_ext
  4332. *
  4333. * RETURN:
  4334. * 0 = success
  4335. * !0 = failure
  4336. *
  4337. * PARAMETERS:
  4338. * *dev - pointer to device structure
  4339. * protect - flag to call protected or non-protected
  4340. * *cal_data - pointer to extended calibration data structure
  4341. * *status - pointer to where the command response status is stored
  4342. ******************************************************************************/
  4343. static int _pt_request_pip_calibrate_ext(struct device *dev,
  4344. int protect, struct pt_cal_ext_data *cal_data, u8 *status)
  4345. {
  4346. struct pt_core_data *cd = dev_get_drvdata(dev);
  4347. if (protect)
  4348. return pt_pip_calibrate_ext(cd, cal_data, status);
  4349. return pt_pip_calibrate_ext_(cd, cal_data, status);
  4350. }
  4351. /*******************************************************************************
  4352. * FUNCTION: pt_pip_calibrate_idacs_
  4353. *
  4354. * SUMMARY: Send the PIP Calibrate IDACs command (0x28) to the DUT waiting
  4355. * for the response
  4356. *
  4357. * RETURN:
  4358. * 0 = success
  4359. * !0 = failure
  4360. *
  4361. * PARAMETERS:
  4362. * *cd - pointer to core data
  4363. * mode - sense mode to calibrate (0-5)
  4364. * *status - pointer to where the command response status is stored
  4365. ******************************************************************************/
  4366. static int pt_pip_calibrate_idacs_(struct pt_core_data *cd,
  4367. u8 mode, u8 *status)
  4368. {
  4369. int rc = 0;
  4370. int write_length = 1;
  4371. u8 write_buf[1];
  4372. u8 cmd_offset = 0;
  4373. struct pt_hid_output hid_output = {
  4374. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_IDACS),
  4375. .write_length = write_length,
  4376. .write_buf = write_buf,
  4377. .timeout_ms = PT_PIP1_CMD_CALIBRATE_IDAC_TIMEOUT,
  4378. };
  4379. write_buf[cmd_offset++] = mode;
  4380. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4381. if (rc)
  4382. return rc;
  4383. *status = cd->response_buf[5];
  4384. if (*status)
  4385. return -EINVAL;
  4386. return 0;
  4387. }
  4388. /*******************************************************************************
  4389. * FUNCTION: pt_pip_calibrate_idacs
  4390. *
  4391. * SUMMARY: Protected call to pt_hid_output_calibrate_idacs_ by exclusive
  4392. * access to the DUT
  4393. *
  4394. * RETURN:
  4395. * 0 = success
  4396. * !0 = failure
  4397. *
  4398. * PARAMETERS:
  4399. * *cd - pointer to core data
  4400. * mode - sense mode to calibrate (0-5)
  4401. * *status - pointer to where the command response status is stored
  4402. ******************************************************************************/
  4403. static int pt_pip_calibrate_idacs(struct pt_core_data *cd,
  4404. u8 mode, u8 *status)
  4405. {
  4406. int rc;
  4407. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4408. if (rc < 0) {
  4409. pt_debug(cd->dev, DL_ERROR,
  4410. "%s: fail get exclusive ex=%p own=%p\n",
  4411. __func__, cd->exclusive_dev, cd->dev);
  4412. return rc;
  4413. }
  4414. rc = pt_pip_calibrate_idacs_(cd, mode, status);
  4415. if (release_exclusive(cd, cd->dev) < 0)
  4416. pt_debug(cd->dev, DL_ERROR,
  4417. "%s: fail to release exclusive\n", __func__);
  4418. return rc;
  4419. }
  4420. /*******************************************************************************
  4421. * FUNCTION: _pt_request_pip_calibrate_idacs
  4422. *
  4423. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4424. * calls to the protected or unprotected call to pt_pip_calibrate_idacs
  4425. *
  4426. * RETURN:
  4427. * 0 = success
  4428. * !0 = failure
  4429. *
  4430. * PARAMETERS:
  4431. * *dev - pointer to device structure
  4432. * protect - flag to call protected or non-protected
  4433. * mode - sense mode to calibrate (0-5)
  4434. * *status - pointer to where the command response status is stored
  4435. ******************************************************************************/
  4436. static int _pt_request_pip_calibrate_idacs(struct device *dev,
  4437. int protect, u8 mode, u8 *status)
  4438. {
  4439. struct pt_core_data *cd = dev_get_drvdata(dev);
  4440. if (protect)
  4441. return pt_pip_calibrate_idacs(cd, mode, status);
  4442. return pt_pip_calibrate_idacs_(cd, mode, status);
  4443. }
  4444. /*******************************************************************************
  4445. * FUNCTION: pt_hid_output_initialize_baselines_
  4446. *
  4447. * SUMMARY: Send the PIP "Initialize Baselines" command (0x29) to the DUT
  4448. * waiting for the response.
  4449. *
  4450. * RETURN:
  4451. * 0 = success
  4452. * !0 = failure
  4453. *
  4454. * PARAMETERS:
  4455. * *cd - pointer to core data
  4456. * test_id - bit type flag to allow initialize baseline MUT,BTN,SELG
  4457. * each or together with a single command.
  4458. * *status - pointer to where the command response status is stored
  4459. ******************************************************************************/
  4460. static int pt_hid_output_initialize_baselines_(
  4461. struct pt_core_data *cd, u8 test_id, u8 *status)
  4462. {
  4463. int rc = 0;
  4464. int write_length = 1;
  4465. u8 write_buf[1];
  4466. u8 cmd_offset = 0;
  4467. struct pt_hid_output hid_output = {
  4468. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_INITIALIZE_BASELINES),
  4469. .write_length = write_length,
  4470. .write_buf = write_buf,
  4471. };
  4472. write_buf[cmd_offset++] = test_id;
  4473. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4474. if (rc)
  4475. return rc;
  4476. *status = cd->response_buf[5];
  4477. if (*status)
  4478. return -EINVAL;
  4479. return rc;
  4480. }
  4481. /*******************************************************************************
  4482. * FUNCTION: pt_hid_output_initialize_baselines
  4483. *
  4484. * SUMMARY: Protected call to pt_hid_output_initialize_baselines_ by exclusive
  4485. * access to the DUT
  4486. *
  4487. * RETURN:
  4488. * 0 = success
  4489. * !0 = failure
  4490. *
  4491. * PARAMETERS:
  4492. * *cd - pointer to core data
  4493. * test_id - enumerated ID against which to initialize the baseline
  4494. * *status - pointer to where the command response status is stored
  4495. ******************************************************************************/
  4496. static int pt_hid_output_initialize_baselines(struct pt_core_data *cd,
  4497. u8 test_id, u8 *status)
  4498. {
  4499. int rc;
  4500. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4501. if (rc < 0) {
  4502. pt_debug(cd->dev, DL_ERROR,
  4503. "%s: fail get exclusive ex=%p own=%p\n",
  4504. __func__, cd->exclusive_dev, cd->dev);
  4505. return rc;
  4506. }
  4507. rc = pt_hid_output_initialize_baselines_(cd, test_id, status);
  4508. if (release_exclusive(cd, cd->dev) < 0)
  4509. pt_debug(cd->dev, DL_ERROR,
  4510. "%s: fail to release exclusive\n", __func__);
  4511. return rc;
  4512. }
  4513. /*******************************************************************************
  4514. * FUNCTION: _pt_request_pip_initialize_baselines
  4515. *
  4516. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4517. * calls to the protected or unprotected call to
  4518. * pt_pip_initialize_baselines
  4519. *
  4520. * RETURN:
  4521. * 0 = success
  4522. * !0 = failure
  4523. *
  4524. * PARAMETERS:
  4525. * *dev - pointer to device structure
  4526. * protect - flag to call protected or non-protected
  4527. * test_id - enumerated ID against which to initialize the baseline
  4528. * *status - pointer to where the command response status is stored
  4529. ******************************************************************************/
  4530. static int _pt_request_pip_initialize_baselines(struct device *dev,
  4531. int protect, u8 test_id, u8 *status)
  4532. {
  4533. struct pt_core_data *cd = dev_get_drvdata(dev);
  4534. if (protect)
  4535. return pt_hid_output_initialize_baselines(cd, test_id,
  4536. status);
  4537. return pt_hid_output_initialize_baselines_(cd, test_id, status);
  4538. }
  4539. /*******************************************************************************
  4540. * FUNCTION: pt_hid_output_exec_panel_scan_
  4541. *
  4542. * SUMMARY: Sends the PIP "Execute Panel Scan" (0x2A) to the DUT and waits for
  4543. * the response
  4544. *
  4545. * RETURN:
  4546. * 0 = success
  4547. * !0 = failure
  4548. *
  4549. * PARAMETERS:
  4550. * *cd - pointer to core data
  4551. ******************************************************************************/
  4552. static int pt_hid_output_exec_panel_scan_(struct pt_core_data *cd)
  4553. {
  4554. struct pt_hid_output hid_output = {
  4555. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_EXEC_PANEL_SCAN),
  4556. };
  4557. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  4558. }
  4559. /*******************************************************************************
  4560. * FUNCTION: pt_hid_output_exec_panel_scan
  4561. *
  4562. * SUMMARY: Protected call to pt_hid_output_exec_panel_scan_ by exclusive
  4563. * access to the DUT.
  4564. *
  4565. * RETURN:
  4566. * 0 = success
  4567. * !0 = failure
  4568. *
  4569. * PARAMETERS:
  4570. * *cd - pointer to core data
  4571. ******************************************************************************/
  4572. static int pt_hid_output_exec_panel_scan(struct pt_core_data *cd)
  4573. {
  4574. int rc;
  4575. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4576. if (rc < 0) {
  4577. pt_debug(cd->dev, DL_ERROR,
  4578. "%s: fail get exclusive ex=%p own=%p\n",
  4579. __func__, cd->exclusive_dev, cd->dev);
  4580. return rc;
  4581. }
  4582. rc = pt_hid_output_exec_panel_scan_(cd);
  4583. if (release_exclusive(cd, cd->dev) < 0)
  4584. pt_debug(cd->dev, DL_ERROR,
  4585. "%s: fail to release exclusive\n", __func__);
  4586. return rc;
  4587. }
  4588. /*******************************************************************************
  4589. * FUNCTION: pt_pip2_exec_panel_scan_
  4590. *
  4591. * SUMMARY: Send the PIP2 "Execute Panel Scan" (0x21) to the DUT and waits for
  4592. * the response
  4593. *
  4594. * RETURN:
  4595. * 0 = success
  4596. * !0 = failure
  4597. *
  4598. * PARAMETERS:
  4599. * *cd - pointer to core data
  4600. * scan_type - type of panel scan to perform (PIP2 only)
  4601. ******************************************************************************/
  4602. static int pt_pip2_exec_panel_scan_(struct pt_core_data *cd, u8 scan_type)
  4603. {
  4604. int rc = 0;
  4605. u8 data[2];
  4606. u8 read_buf[10];
  4607. u16 actual_read_len;
  4608. pt_debug(cd->dev, DL_DEBUG, "%s: PIP2 Execute Scan %d\n",
  4609. __func__, scan_type);
  4610. data[0] = scan_type;
  4611. rc = _pt_request_pip2_send_cmd(cd->dev,
  4612. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_EXECUTE_SCAN,
  4613. data, 1, read_buf, &actual_read_len);
  4614. if (rc) {
  4615. pt_debug(cd->dev, DL_ERROR,
  4616. "%s EXECUTE_SCAN command for type %d failed. rc=%d\n",
  4617. __func__, scan_type, rc);
  4618. }
  4619. return rc;
  4620. }
  4621. /*******************************************************************************
  4622. * FUNCTION: pt_pip2_exec_panel_scan
  4623. *
  4624. * SUMMARY: Protected call to pt_pip2_exec_panel_scan_ by exclusive
  4625. * access to the DUT.
  4626. *
  4627. * RETURN:
  4628. * 0 = success
  4629. * !0 = failure
  4630. *
  4631. * PARAMETERS:
  4632. * *cd - pointer to core data
  4633. * scan_type - type of panel scan to perform (PIP2 only)
  4634. ******************************************************************************/
  4635. static int pt_pip2_exec_panel_scan(struct pt_core_data *cd, u8 scan_type)
  4636. {
  4637. int rc;
  4638. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4639. if (rc < 0) {
  4640. pt_debug(cd->dev, DL_ERROR,
  4641. "%s: fail get exclusive ex=%p own=%p\n",
  4642. __func__, cd->exclusive_dev, cd->dev);
  4643. return rc;
  4644. }
  4645. rc = pt_pip2_exec_panel_scan_(cd, scan_type);
  4646. if (release_exclusive(cd, cd->dev) < 0)
  4647. pt_debug(cd->dev, DL_ERROR,
  4648. "%s: fail to release exclusive\n", __func__);
  4649. return rc;
  4650. }
  4651. /*******************************************************************************
  4652. * FUNCTION: _pt_request_pip_exec_panel_scan
  4653. *
  4654. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4655. * calls to the protected or unprotected call to
  4656. * pt_pip2_exec_panel_scan or pt_hid_output_exec_panel_scan
  4657. *
  4658. * RETURN:
  4659. * 0 = success
  4660. * !0 = failure
  4661. *
  4662. * PARAMETERS:
  4663. * *dev - pointer to device structure
  4664. * protect - flag to call protected or non-protected
  4665. * scan_type - type of panel scan to perform (PIP2 only)
  4666. ******************************************************************************/
  4667. static int _pt_request_pip_exec_panel_scan(struct device *dev,
  4668. int protect, u8 scan_type)
  4669. {
  4670. struct pt_core_data *cd = dev_get_drvdata(dev);
  4671. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 12)) {
  4672. if (protect)
  4673. return pt_pip2_exec_panel_scan(cd, scan_type);
  4674. return pt_pip2_exec_panel_scan_(cd, scan_type);
  4675. }
  4676. if (protect)
  4677. return pt_hid_output_exec_panel_scan(cd);
  4678. return pt_hid_output_exec_panel_scan_(cd);
  4679. }
  4680. /*******************************************************************************
  4681. * FUNCTION: pt_hid_output_retrieve_panel_scan_
  4682. *
  4683. * SUMMARY: Sends the PIP "Retrieve Panel Scan" (0x2B) command to the DUT
  4684. * to retrieve the specified data type for a the last successful Execute
  4685. * Panel Scan command.
  4686. *
  4687. * RETURN:
  4688. * 0 = success
  4689. * !0 = failure
  4690. *
  4691. * PARAMETERS:
  4692. * *dev - pointer to device structure
  4693. * protect - flag to call protected or non-protected
  4694. * read_offset - read pointer offset
  4695. * read_count - length of data to read
  4696. * data_id - enumerated test ID to read selftest results from
  4697. * *response - pointer to store the read response status
  4698. * *config - pointer to store config data
  4699. * *actual_read_len - pointer to store data length actually read
  4700. * *read_buf - pointer to the read buffer
  4701. ******************************************************************************/
  4702. static int pt_hid_output_retrieve_panel_scan_(
  4703. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4704. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4705. u8 *read_buf)
  4706. {
  4707. int status;
  4708. u8 read_data_id;
  4709. int rc = 0;
  4710. int write_length = 5;
  4711. u8 write_buf[5];
  4712. u8 cmd_offset = 0;
  4713. u8 data_elem_size;
  4714. int size;
  4715. int data_size;
  4716. struct pt_hid_output hid_output = {
  4717. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RETRIEVE_PANEL_SCAN),
  4718. .write_length = write_length,
  4719. .write_buf = write_buf,
  4720. };
  4721. write_buf[cmd_offset++] = LOW_BYTE(read_offset);
  4722. write_buf[cmd_offset++] = HI_BYTE(read_offset);
  4723. write_buf[cmd_offset++] = LOW_BYTE(read_count);
  4724. write_buf[cmd_offset++] = HI_BYTE(read_count);
  4725. write_buf[cmd_offset++] = data_id;
  4726. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4727. if (rc)
  4728. return rc;
  4729. status = cd->response_buf[5];
  4730. if (status)
  4731. return -EINVAL;
  4732. read_data_id = cd->response_buf[6];
  4733. if (read_data_id != data_id)
  4734. return -EPROTO;
  4735. size = get_unaligned_le16(&cd->response_buf[0]);
  4736. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  4737. *config = cd->response_buf[9];
  4738. data_elem_size = *config & 0x07;
  4739. data_size = *actual_read_len * data_elem_size;
  4740. if (read_buf)
  4741. memcpy(read_buf, &cd->response_buf[10], data_size);
  4742. if (response)
  4743. memcpy(response, cd->response_buf, size);
  4744. return rc;
  4745. }
  4746. /*******************************************************************************
  4747. * FUNCTION: pt_hid_output_retrieve_panel_scan
  4748. *
  4749. * SUMMARY: Protected call to pt_hid_output_retrieve_panel_scan_ by exclusive
  4750. * access to the DUT.
  4751. *
  4752. * RETURN:
  4753. * 0 = success
  4754. * !0 = failure
  4755. *
  4756. * PARAMETERS:
  4757. * *dev - pointer to device structure
  4758. * protect - flag to call protected or non-protected
  4759. * read_offset - read pointer offset
  4760. * read_count - length of data to read
  4761. * data_id - enumerated test ID to read selftest results from
  4762. * *response - pointer to store the read response status
  4763. * *config - pointer to store config data
  4764. * *actual_read_len - pointer to store data length actually read
  4765. * *read_buf - pointer to the read buffer
  4766. ******************************************************************************/
  4767. static int pt_hid_output_retrieve_panel_scan(
  4768. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4769. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4770. u8 *read_buf)
  4771. {
  4772. int rc;
  4773. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4774. if (rc < 0) {
  4775. pt_debug(cd->dev, DL_ERROR,
  4776. "%s: fail get exclusive ex=%p own=%p\n",
  4777. __func__, cd->exclusive_dev, cd->dev);
  4778. return rc;
  4779. }
  4780. rc = pt_hid_output_retrieve_panel_scan_(cd, read_offset,
  4781. read_count, data_id, response, config,
  4782. actual_read_len, read_buf);
  4783. if (release_exclusive(cd, cd->dev) < 0)
  4784. pt_debug(cd->dev, DL_ERROR,
  4785. "%s: fail to release exclusive\n", __func__);
  4786. return rc;
  4787. }
  4788. /*******************************************************************************
  4789. * FUNCTION: _pt_request_pip_retrieve_panel_scan
  4790. *
  4791. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4792. * calls to the protected or unprotected call to
  4793. * pt_hid_output_retrieve_panel_scan
  4794. *
  4795. * RETURN:
  4796. * 0 = success
  4797. * !0 = failure
  4798. *
  4799. * PARAMETERS:
  4800. * *dev - pointer to device structure
  4801. * protect - flag to call protected or non-protected
  4802. * read_offset - read pointer offset
  4803. * read_count - length of data to read
  4804. * data_id - enumerated test ID to read selftest results from
  4805. * *response - pointer to store the read response status
  4806. * *config - pointer to store config data
  4807. * *actual_read_len - pointer to store data length actually read
  4808. * *read_buf - pointer to the read buffer
  4809. ******************************************************************************/
  4810. static int _pt_request_pip_retrieve_panel_scan(struct device *dev,
  4811. int protect, u16 read_offset, u16 read_count, u8 data_id,
  4812. u8 *response, u8 *config, u16 *actual_read_len, u8 *read_buf)
  4813. {
  4814. struct pt_core_data *cd = dev_get_drvdata(dev);
  4815. if (protect)
  4816. return pt_hid_output_retrieve_panel_scan(cd,
  4817. read_offset, read_count, data_id, response,
  4818. config, actual_read_len, read_buf);
  4819. return pt_hid_output_retrieve_panel_scan_(cd,
  4820. read_offset, read_count, data_id, response,
  4821. config, actual_read_len, read_buf);
  4822. }
  4823. /*******************************************************************************
  4824. * FUNCTION: _pt_request_pip_user_cmd
  4825. *
  4826. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4827. * calls to the protected or unprotected call to
  4828. * pt_hid_output_user_cmd
  4829. *
  4830. * RETURN:
  4831. * 0 = success
  4832. * !0 = failure
  4833. *
  4834. * PARAMETERS:
  4835. * *dev - pointer to device structure
  4836. * protect - flag to call protected or non-protected
  4837. * read_len - length of data to read
  4838. * *read_buf - pointer to store read data
  4839. * write_len - length of data to write
  4840. * *write_buf - pointer to buffer to write
  4841. * *actual_read_len - pointer to store data length actually read
  4842. ******************************************************************************/
  4843. static int _pt_request_pip_user_cmd(struct device *dev,
  4844. int protect, u16 read_len, u8 *read_buf, u16 write_len,
  4845. u8 *write_buf, u16 *actual_read_len)
  4846. {
  4847. struct pt_core_data *cd = dev_get_drvdata(dev);
  4848. if (protect)
  4849. return pt_hid_output_user_cmd(cd, read_len, read_buf,
  4850. write_len, write_buf, actual_read_len);
  4851. return pt_hid_output_user_cmd_(cd, read_len, read_buf,
  4852. write_len, write_buf, actual_read_len);
  4853. }
  4854. /*******************************************************************************
  4855. * FUNCTION: pt_hid_output_bl_get_information_
  4856. *
  4857. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x38) command to the
  4858. * DUT to retrieve bootloader version and chip identification information.
  4859. *
  4860. * RETURN:
  4861. * 0 = success
  4862. * !0 = failure
  4863. *
  4864. * PARAMETERS:
  4865. * *cd - pointer to core data
  4866. * *return_data - pointer to store the return data
  4867. *****************************************************************************/
  4868. static int pt_hid_output_bl_get_information_(struct pt_core_data *cd,
  4869. u8 *return_data)
  4870. {
  4871. int rc;
  4872. int data_len;
  4873. struct pt_hid_output hid_output = {
  4874. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_INFO),
  4875. };
  4876. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4877. if (rc)
  4878. return rc;
  4879. data_len = get_unaligned_le16(&cd->input_buf[6]);
  4880. if (!data_len)
  4881. return -EPROTO;
  4882. memcpy(return_data, &cd->response_buf[8], data_len);
  4883. return 0;
  4884. }
  4885. /*******************************************************************************
  4886. * FUNCTION: pt_hid_output_bl_get_information
  4887. *
  4888. * SUMMARY: Protected call to pt_hid_output_bl_get_information_ by exclusive
  4889. * access to the DUT.
  4890. *
  4891. * RETURN:
  4892. * 0 = success
  4893. * !0 = failure
  4894. *
  4895. * PARAMETERS:
  4896. * *cd - pointer to core data
  4897. * *return_data - pointer to store the return data
  4898. ******************************************************************************/
  4899. static int pt_hid_output_bl_get_information(struct pt_core_data *cd,
  4900. u8 *return_data)
  4901. {
  4902. int rc;
  4903. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4904. if (rc < 0) {
  4905. pt_debug(cd->dev, DL_ERROR,
  4906. "%s: fail get exclusive ex=%p own=%p\n",
  4907. __func__, cd->exclusive_dev, cd->dev);
  4908. return rc;
  4909. }
  4910. rc = pt_hid_output_bl_get_information_(cd, return_data);
  4911. if (release_exclusive(cd, cd->dev) < 0)
  4912. pt_debug(cd->dev, DL_ERROR,
  4913. "%s: fail to release exclusive\n", __func__);
  4914. return rc;
  4915. }
  4916. /*******************************************************************************
  4917. * FUNCTION: _pt_request_pip_bl_get_information
  4918. *
  4919. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4920. * calls to the protected or unprotected call to
  4921. * pt_hid_output_bl_get_information
  4922. *
  4923. * RETURN:
  4924. * 0 = success
  4925. * !0 = failure
  4926. *
  4927. * PARAMETERS:
  4928. * *dev - pointer to device structure
  4929. * protect - flag to call protected or non-protected
  4930. * *return_data - pointer to store bl data
  4931. ******************************************************************************/
  4932. static int _pt_request_pip_bl_get_information(struct device *dev,
  4933. int protect, u8 *return_data)
  4934. {
  4935. struct pt_core_data *cd = dev_get_drvdata(dev);
  4936. if (protect)
  4937. return pt_hid_output_bl_get_information(cd, return_data);
  4938. return pt_hid_output_bl_get_information_(cd, return_data);
  4939. }
  4940. /*******************************************************************************
  4941. * FUNCTION: pt_hid_output_bl_initiate_bl_
  4942. *
  4943. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x48) command to the
  4944. * DUT to erases the entire TrueTouch application, Configuration Data block,
  4945. * and Design Data block in flash and enables the host to execute the Program
  4946. * and Verify Row command to bootload the application image and data.
  4947. *
  4948. * RETURN:
  4949. * 0 = success
  4950. * !0 = failure
  4951. *
  4952. * PARAMETERS:
  4953. * *cd - pointer to core data
  4954. * protect - flag to call protected or non-protected
  4955. * key_size - size of key
  4956. * *key_buf - pointer to key data to allow operation
  4957. * row_size - size of the meta data row
  4958. * *metadata_row_buf - pointer to meta data to write
  4959. ******************************************************************************/
  4960. static int pt_hid_output_bl_initiate_bl_(struct pt_core_data *cd,
  4961. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  4962. {
  4963. u16 write_length = key_size + row_size;
  4964. u8 *write_buf;
  4965. int rc = 0;
  4966. struct pt_hid_output hid_output = {
  4967. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_INITIATE_BL),
  4968. .write_length = write_length,
  4969. .timeout_ms = PT_PIP1_CMD_INITIATE_BL_TIMEOUT,
  4970. };
  4971. write_buf = kzalloc(write_length, GFP_KERNEL);
  4972. if (!write_buf)
  4973. return -ENOMEM;
  4974. hid_output.write_buf = write_buf;
  4975. if (key_size)
  4976. memcpy(write_buf, key_buf, key_size);
  4977. if (row_size)
  4978. memcpy(&write_buf[key_size], metadata_row_buf, row_size);
  4979. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4980. kfree(write_buf);
  4981. return rc;
  4982. }
  4983. /*******************************************************************************
  4984. * FUNCTION: pt_hid_output_bl_initiate_bl
  4985. *
  4986. * SUMMARY: Protected call to pt_hid_output_bl_initiate_bl_ by exclusive
  4987. * access to the DUT.
  4988. *
  4989. * RETURN:
  4990. * 0 = success
  4991. * !0 = failure
  4992. *
  4993. * PARAMETERS:
  4994. * *cd - pointer to core data
  4995. * key_size - size of key
  4996. * *key_buf - pointer to key data to allow operation
  4997. * row_size - size of the meta data row
  4998. * *metadata_row_buf - pointer to meta data to write
  4999. ******************************************************************************/
  5000. static int pt_hid_output_bl_initiate_bl(struct pt_core_data *cd,
  5001. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  5002. {
  5003. int rc;
  5004. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5005. if (rc < 0) {
  5006. pt_debug(cd->dev, DL_ERROR,
  5007. "%s: fail get exclusive ex=%p own=%p\n",
  5008. __func__, cd->exclusive_dev, cd->dev);
  5009. return rc;
  5010. }
  5011. rc = pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5012. row_size, metadata_row_buf);
  5013. if (release_exclusive(cd, cd->dev) < 0)
  5014. pt_debug(cd->dev, DL_ERROR,
  5015. "%s: fail to release exclusive\n", __func__);
  5016. return rc;
  5017. }
  5018. /*******************************************************************************
  5019. * FUNCTION: _pt_request_pip_bl_initiate_bl
  5020. *
  5021. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  5022. * calls to the protected or unprotected call to
  5023. * pt_hid_output_bl_initiate_bl
  5024. *
  5025. * RETURN:
  5026. * 0 = success
  5027. * !0 = failure
  5028. *
  5029. * PARAMETERS:
  5030. * *dev - pointer to device structure
  5031. * protect - flag to call protected or non-protected
  5032. * key_size - size of key
  5033. * *key_buf - pointer to key data to allow operation
  5034. * row_size - size of the meta data row
  5035. * *metadata_row_buf - pointer to meta data to write
  5036. ******************************************************************************/
  5037. static int _pt_request_pip_bl_initiate_bl(struct device *dev,
  5038. int protect, u16 key_size, u8 *key_buf, u16 row_size,
  5039. u8 *metadata_row_buf)
  5040. {
  5041. struct pt_core_data *cd = dev_get_drvdata(dev);
  5042. if (protect)
  5043. return pt_hid_output_bl_initiate_bl(cd, key_size, key_buf,
  5044. row_size, metadata_row_buf);
  5045. return pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5046. row_size, metadata_row_buf);
  5047. }
  5048. /*******************************************************************************
  5049. * FUNCTION: pt_hid_output_bl_program_and_verify_
  5050. *
  5051. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x39) command to upload
  5052. * and program a 128-byte row into the flash, and then verifies written data.
  5053. *
  5054. * RETURN:
  5055. * 0 = success
  5056. * !0 = failure
  5057. *
  5058. * PARAMETERS:
  5059. * *cd - pointer to core data
  5060. * data_len - length of data_buf
  5061. * *data_buf - firmware image to program
  5062. ******************************************************************************/
  5063. static int pt_hid_output_bl_program_and_verify_(
  5064. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5065. {
  5066. struct pt_hid_output hid_output = {
  5067. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY),
  5068. .write_length = data_len,
  5069. .write_buf = data_buf,
  5070. .timeout_ms = PT_PIP1_CMD_PROGRAM_AND_VERIFY_TIMEOUT,
  5071. };
  5072. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5073. }
  5074. /*******************************************************************************
  5075. * FUNCTION: pt_hid_output_bl_program_and_verify
  5076. *
  5077. * SUMMARY: Protected call to pt_hid_output_bl_program_and_verify_ by exclusive
  5078. * access to the DUT.
  5079. *
  5080. * RETURN:
  5081. * 0 = success
  5082. * !0 = failure
  5083. *
  5084. * PARAMETERS:
  5085. * *cd - pointer to core data
  5086. * data_len - length of data_buf
  5087. * *data_buf - firmware image to program
  5088. ******************************************************************************/
  5089. static int pt_hid_output_bl_program_and_verify(
  5090. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5091. {
  5092. int rc;
  5093. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5094. if (rc < 0) {
  5095. pt_debug(cd->dev, DL_ERROR,
  5096. "%s: fail get exclusive ex=%p own=%p\n",
  5097. __func__, cd->exclusive_dev, cd->dev);
  5098. return rc;
  5099. }
  5100. rc = pt_hid_output_bl_program_and_verify_(cd, data_len, data_buf);
  5101. if (release_exclusive(cd, cd->dev) < 0)
  5102. pt_debug(cd->dev, DL_ERROR,
  5103. "%s: fail to release exclusive\n", __func__);
  5104. return rc;
  5105. }
  5106. /*******************************************************************************
  5107. * FUNCTION: _pt_request_pip_bl_program_and_verify
  5108. *
  5109. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5110. * to request to have the BL program and verify a FW image
  5111. *
  5112. * RETURN:
  5113. * 0 = success
  5114. * !0 = failure
  5115. *
  5116. * PARAMETERS:
  5117. * *dev - pointer to device structure
  5118. * protect - boolean to determine to call the protected function
  5119. * data_len - length of data_buf
  5120. * *data_buf - firmware image to program
  5121. ******************************************************************************/
  5122. static int _pt_request_pip_bl_program_and_verify(
  5123. struct device *dev, int protect, u16 data_len, u8 *data_buf)
  5124. {
  5125. struct pt_core_data *cd = dev_get_drvdata(dev);
  5126. if (protect)
  5127. return pt_hid_output_bl_program_and_verify(cd, data_len,
  5128. data_buf);
  5129. return pt_hid_output_bl_program_and_verify_(cd, data_len,
  5130. data_buf);
  5131. }
  5132. /*******************************************************************************
  5133. * FUNCTION: pt_hid_output_bl_verify_app_integrity_
  5134. *
  5135. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x31) command to
  5136. * perform a full verification of the application integrity by calculating the
  5137. * CRC of the image in flash and compare it to the expected CRC stored in the
  5138. * Metadata row.
  5139. *
  5140. * RETURN:
  5141. * 0 = success
  5142. * !0 = failure
  5143. *
  5144. * PARAMETERS:
  5145. * *cd - pointer to core data
  5146. * *result - pointer to store result
  5147. ******************************************************************************/
  5148. static int pt_hid_output_bl_verify_app_integrity_(
  5149. struct pt_core_data *cd, u8 *result)
  5150. {
  5151. int rc;
  5152. struct pt_hid_output hid_output = {
  5153. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY),
  5154. };
  5155. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5156. if (rc) {
  5157. *result = 0;
  5158. return rc;
  5159. }
  5160. *result = cd->response_buf[8];
  5161. return 0;
  5162. }
  5163. /*******************************************************************************
  5164. * FUNCTION: pt_hid_output_bl_verify_app_integrity
  5165. *
  5166. * SUMMARY: Protected call to pt_hid_output_bl_verify_app_integrity_ by
  5167. * exclusive access to the DUT.
  5168. *
  5169. * RETURN:
  5170. * 0 = success
  5171. * !0 = failure
  5172. *
  5173. * PARAMETERS:
  5174. * *cd - pointer to core data
  5175. * *result - pointer to store result
  5176. ******************************************************************************/
  5177. static int pt_hid_output_bl_verify_app_integrity(
  5178. struct pt_core_data *cd, u8 *result)
  5179. {
  5180. int rc;
  5181. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5182. if (rc < 0) {
  5183. pt_debug(cd->dev, DL_ERROR,
  5184. "%s: fail get exclusive ex=%p own=%p\n",
  5185. __func__, cd->exclusive_dev, cd->dev);
  5186. return rc;
  5187. }
  5188. rc = pt_hid_output_bl_verify_app_integrity_(cd, result);
  5189. if (release_exclusive(cd, cd->dev) < 0)
  5190. pt_debug(cd->dev, DL_ERROR,
  5191. "%s: fail to release exclusive\n", __func__);
  5192. return rc;
  5193. }
  5194. /*******************************************************************************
  5195. * FUNCTION: _pt_request_pip_bl_verify_app_integrity
  5196. *
  5197. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5198. * to request to have the BL verify the application integrity (PIP1.x only)
  5199. *
  5200. * RETURN:
  5201. * 0 = success
  5202. * !0 = failure
  5203. *
  5204. * PARAMETERS:
  5205. * *dev - pointer to device structure
  5206. * protect - boolean to determine to call the protected function
  5207. * *result - pointer to store result
  5208. ******************************************************************************/
  5209. static int _pt_request_pip_bl_verify_app_integrity(
  5210. struct device *dev, int protect, u8 *result)
  5211. {
  5212. struct pt_core_data *cd = dev_get_drvdata(dev);
  5213. if (protect)
  5214. return pt_hid_output_bl_verify_app_integrity(cd, result);
  5215. return pt_hid_output_bl_verify_app_integrity_(cd, result);
  5216. }
  5217. /*******************************************************************************
  5218. * FUNCTION: pt_hid_output_bl_launch_app_
  5219. *
  5220. * SUMMARY: Sends the PIP "Launch Application" (0x3B) command to launch the
  5221. * application from bootloader (PIP1.x only).
  5222. *
  5223. * RETURN:
  5224. * 0 = success
  5225. * !0 = failure
  5226. *
  5227. * PARAMETERS:
  5228. * *cd - pointer to core data
  5229. ******************************************************************************/
  5230. static int pt_hid_output_bl_launch_app_(struct pt_core_data *cd)
  5231. {
  5232. struct pt_hid_output hid_output = {
  5233. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_LAUNCH_APP),
  5234. .reset_expected = 1,
  5235. };
  5236. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5237. }
  5238. /*******************************************************************************
  5239. * FUNCTION: pt_hid_output_bl_launch_app
  5240. *
  5241. * SUMMARY: Protected call to pt_hid_output_bl_launch_app_ by exclusive access
  5242. * to the DUT.
  5243. *
  5244. * RETURN:
  5245. * 0 = success
  5246. * !0 = failure
  5247. *
  5248. * PARAMETERS:
  5249. * *cd - pointer to core data
  5250. ******************************************************************************/
  5251. static int pt_hid_output_bl_launch_app(struct pt_core_data *cd)
  5252. {
  5253. int rc;
  5254. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5255. if (rc < 0) {
  5256. pt_debug(cd->dev, DL_ERROR,
  5257. "%s: fail get exclusive ex=%p own=%p\n",
  5258. __func__, cd->exclusive_dev, cd->dev);
  5259. return rc;
  5260. }
  5261. rc = pt_hid_output_bl_launch_app_(cd);
  5262. if (release_exclusive(cd, cd->dev) < 0)
  5263. pt_debug(cd->dev, DL_ERROR,
  5264. "%s: fail to release exclusive\n", __func__);
  5265. return rc;
  5266. }
  5267. /*******************************************************************************
  5268. * FUNCTION: _pt_request_pip_launch_app
  5269. *
  5270. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5271. * to request to have the BL launch the application. (PIP1.x only)
  5272. *
  5273. * RETURN:
  5274. * 0 = success
  5275. * !0 = failure
  5276. *
  5277. * PARAMETERS:
  5278. * *dev - pointer to device structure
  5279. * protect - boolean to determine to call the protected function
  5280. ******************************************************************************/
  5281. static int _pt_request_pip_launch_app(struct device *dev,
  5282. int protect)
  5283. {
  5284. struct pt_core_data *cd = dev_get_drvdata(dev);
  5285. if (protect)
  5286. return pt_hid_output_bl_launch_app(cd);
  5287. return pt_hid_output_bl_launch_app_(cd);
  5288. }
  5289. /*******************************************************************************
  5290. * FUNCTION: pt_hid_output_bl_get_panel_id_
  5291. *
  5292. * SUMMARY: Sends the PIP "Get Panel ID" (0x3E) command to return the Panel ID
  5293. * value store in the System Information.
  5294. *
  5295. * RETURN:
  5296. * 0 = success
  5297. * !0 = failure
  5298. *
  5299. * PARAMETERS:
  5300. * *cd - pointer to core data
  5301. * *panel_id - pointer to where the panel ID will be stored
  5302. ******************************************************************************/
  5303. static int pt_hid_output_bl_get_panel_id_(
  5304. struct pt_core_data *cd, u8 *panel_id)
  5305. {
  5306. int rc;
  5307. struct pt_hid_output hid_output = {
  5308. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_PANEL_ID),
  5309. };
  5310. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5311. if (rc == -EPROTO && cd->response_buf[5] == ERROR_COMMAND) {
  5312. pt_debug(cd->dev, DL_ERROR,
  5313. "%s: Get Panel ID command not supported\n",
  5314. __func__);
  5315. *panel_id = PANEL_ID_NOT_ENABLED;
  5316. return 0;
  5317. } else if (rc < 0) {
  5318. pt_debug(cd->dev, DL_ERROR,
  5319. "%s: Error on Get Panel ID command\n", __func__);
  5320. return rc;
  5321. }
  5322. *panel_id = cd->response_buf[8];
  5323. return 0;
  5324. }
  5325. /*******************************************************************************
  5326. * FUNCTION: pt_hid_output_bl_get_panel_id
  5327. *
  5328. * SUMMARY: Protected call to pt_hid_output_bl_get_panel_id_ by exclusive access
  5329. * to the DUT.
  5330. *
  5331. * RETURN:
  5332. * 0 = success
  5333. * !0 = failure
  5334. *
  5335. * PARAMETERS:
  5336. * *cd - pointer to core data
  5337. * *panel_id - pointer to where the panel ID will be stored
  5338. ******************************************************************************/
  5339. static int pt_hid_output_bl_get_panel_id(
  5340. struct pt_core_data *cd, u8 *panel_id)
  5341. {
  5342. int rc;
  5343. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5344. if (rc < 0) {
  5345. pt_debug(cd->dev, DL_ERROR,
  5346. "%s: fail get exclusive ex=%p own=%p\n",
  5347. __func__, cd->exclusive_dev, cd->dev);
  5348. return rc;
  5349. }
  5350. rc = pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5351. if (release_exclusive(cd, cd->dev) < 0)
  5352. pt_debug(cd->dev, DL_ERROR,
  5353. "%s: fail to release exclusive\n", __func__);
  5354. return rc;
  5355. }
  5356. /*******************************************************************************
  5357. * FUNCTION: _pt_request_pip_bl_get_panel_id
  5358. *
  5359. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5360. * to have the BL retrieve the panel ID
  5361. *
  5362. * RETURN:
  5363. * 0 = success
  5364. * !0 = failure
  5365. *
  5366. * PARAMETERS:
  5367. * *dev - pointer to device structure
  5368. * protect - flag to run in protected mode
  5369. * *panel_id - pointer to where the panel ID will be stored
  5370. ******************************************************************************/
  5371. static int _pt_request_pip_bl_get_panel_id(
  5372. struct device *dev, int protect, u8 *panel_id)
  5373. {
  5374. struct pt_core_data *cd = dev_get_drvdata(dev);
  5375. if (protect)
  5376. return pt_hid_output_bl_get_panel_id(cd, panel_id);
  5377. return pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5378. }
  5379. /*******************************************************************************
  5380. * FUNCTION: pt_pip2_get_mode_sysmode_
  5381. *
  5382. * SUMMARY: Determine the current mode and system mode of the DUT by use of the
  5383. * PIP2 STATUS command.
  5384. *
  5385. * RETURN:
  5386. * 0 = success
  5387. * !0 = failure
  5388. *
  5389. * PARAMETERS:
  5390. * *cd - pointer to core data structure
  5391. * *mode - pointer to store the retrieved mode
  5392. * *sys_mode - pointer to store the FW system mode
  5393. ******************************************************************************/
  5394. static int pt_pip2_get_mode_sysmode_(struct pt_core_data *cd,
  5395. u8 *mode, u8 *sys_mode)
  5396. {
  5397. u16 actual_read_len;
  5398. u8 read_buf[12];
  5399. u8 status, boot;
  5400. int rc = 0;
  5401. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  5402. PIP2_CMD_ID_STATUS, NULL, 0, read_buf, &actual_read_len);
  5403. pt_debug(cd->dev, DL_INFO, "%s: PIP2 STATUS command rc = %d\n",
  5404. __func__, rc);
  5405. if (!rc) {
  5406. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, actual_read_len,
  5407. "PIP2 STATUS");
  5408. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5409. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  5410. if (sys_mode) {
  5411. if (status == PIP2_RSP_ERR_NONE &&
  5412. boot == PIP2_STATUS_APP_EXEC)
  5413. *sys_mode = read_buf[PIP2_RESP_BODY_OFFSET + 1];
  5414. else
  5415. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5416. }
  5417. if (mode) {
  5418. if (status == PIP2_RSP_ERR_NONE &&
  5419. boot == PIP2_STATUS_BOOT_EXEC)
  5420. *mode = PT_MODE_BOOTLOADER;
  5421. else if (status == PIP2_RSP_ERR_NONE &&
  5422. boot == PIP2_STATUS_APP_EXEC)
  5423. *mode = PT_MODE_OPERATIONAL;
  5424. else
  5425. *mode = PT_MODE_UNKNOWN;
  5426. }
  5427. } else {
  5428. if (mode)
  5429. *mode = PT_MODE_UNKNOWN;
  5430. if (sys_mode)
  5431. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5432. pt_debug(cd->dev, DL_WARN,
  5433. "%s: Mode and sys_mode could not be determined\n",
  5434. __func__);
  5435. }
  5436. return rc;
  5437. }
  5438. /*******************************************************************************
  5439. * FUNCTION: pt_pip2_get_mode_sysmode
  5440. *
  5441. * SUMMARY: Protected call to pt_pip2_get_mode_sysmode_ by exclusive
  5442. * access to the DUT.
  5443. *
  5444. * RETURN:
  5445. * 0 = success
  5446. * !0 = failure
  5447. *
  5448. * PARAMETERS:
  5449. * *cd - pointer to core data structure
  5450. * *mode - pointer to store the retrieved mode
  5451. * *sys_mode - pointer to store the FW system mode
  5452. ******************************************************************************/
  5453. static int pt_pip2_get_mode_sysmode(struct pt_core_data *cd,
  5454. u8 *mode, u8 *sys_mode)
  5455. {
  5456. int rc;
  5457. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5458. if (rc < 0) {
  5459. pt_debug(cd->dev, DL_ERROR,
  5460. "%s: fail get exclusive ex=%p own=%p\n",
  5461. __func__, cd->exclusive_dev, cd->dev);
  5462. return rc;
  5463. }
  5464. rc = pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5465. if (release_exclusive(cd, cd->dev) < 0)
  5466. pt_debug(cd->dev, DL_ERROR,
  5467. "%s: fail to release exclusive\n", __func__);
  5468. return rc;
  5469. }
  5470. /*******************************************************************************
  5471. * FUNCTION: _pt_request_pip2_get_mode_sysmode
  5472. *
  5473. * SUMMARY: Function pointer included in core_commands struct for external
  5474. * calls to the protected or unprotected call to
  5475. * pt_pip2_get_mode_sysmode
  5476. *
  5477. * RETURN:
  5478. * 0 = success
  5479. * !0 = failure
  5480. *
  5481. * PARAMETERS:
  5482. * *dev - pointer to device structure
  5483. * *mode - pointer to store the retrieved mode
  5484. * *sys_mode - pointer to store the FW system mode
  5485. ******************************************************************************/
  5486. static int _pt_request_pip2_get_mode_sysmode(struct device *dev,
  5487. int protect, u8 *mode, u8 *sys_mode)
  5488. {
  5489. struct pt_core_data *cd = dev_get_drvdata(dev);
  5490. if (protect)
  5491. return pt_pip2_get_mode_sysmode(cd, mode, sys_mode);
  5492. return pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5493. }
  5494. /*******************************************************************************
  5495. * FUNCTION: _pt_poll_for_fw_exit_boot_mode
  5496. *
  5497. * SUMMARY: Verify and or poll for the FW to exit BOOT mode. During the FW BOOT
  5498. * mode only the following PIP commands will be serviced, any other PIP
  5499. * command the FW will respond with an "Invalid PIP Command" response.
  5500. * - Get HID Descriptor (Register 0x0001, no command ID)
  5501. * - Reset (Register 0x0005, RESET HID request)
  5502. * - Ping (Register 0x0004, Command ID 0x00
  5503. * - Get System Information (Register 0x0004, Command ID 0x02)
  5504. * - PIP2 Status (Register 0x0101, Command ID 0x01)
  5505. * - PIP2 Version (Register 0x0101, Command ID 0x07)
  5506. * This function will loop on the results of the STATUS command until
  5507. * the FW reports it is out of BOOT mode.
  5508. *
  5509. * NOTE:
  5510. * - This function will update cd->fw_system_mode
  5511. * - The STATUS cmd only supports this functionality for PIP 1.11+
  5512. *
  5513. * RETURN:
  5514. * 0 = success
  5515. * !0 = failure
  5516. *
  5517. * PARAMETERS:
  5518. * *cd - pointer to core data
  5519. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5520. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5521. ******************************************************************************/
  5522. static int _pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5523. int *actual_wait)
  5524. {
  5525. int loop = 0;
  5526. u8 sys_mode = cd->fw_system_mode;
  5527. u8 pause = 10; /* in ms */
  5528. int rc = 0;
  5529. int max_loop = (timeout / pause) + 1; /* Add 1 due to int math */
  5530. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5531. /*
  5532. * For PIP <1.11, no support for polling wait so do a hard
  5533. * coded wait and assume the FW is out of BOOT. Added 1 to
  5534. * timeout to make it clear in kmsg if non polling was done.
  5535. */
  5536. *actual_wait = PT_FW_EXIT_BOOT_MODE_TIMEOUT + 1;
  5537. pt_debug(cd->dev, DL_ERROR,
  5538. "%s: PIP %d.%d no support for ext STATUS, sleep %d\n",
  5539. __func__,
  5540. cd->sysinfo.ttdata.pip_ver_major,
  5541. cd->sysinfo.ttdata.pip_ver_minor, *actual_wait);
  5542. msleep(*actual_wait);
  5543. sys_mode = FW_SYS_MODE_SCANNING;
  5544. }
  5545. if (sys_mode == FW_SYS_MODE_BOOT) {
  5546. while (!rc && loop <= max_loop &&
  5547. (sys_mode == FW_SYS_MODE_BOOT)) {
  5548. loop++;
  5549. usleep_range(9000, pause * 1000);
  5550. rc = pt_pip2_get_mode_sysmode_(cd, NULL, &sys_mode);
  5551. pt_debug(cd->dev, DL_DEBUG,
  5552. "%s: FW in BOOT mode-sleep %dms, sys_mode=%d\n",
  5553. __func__, loop * pause, sys_mode);
  5554. }
  5555. *actual_wait = (int)(loop * pause);
  5556. pt_debug(cd->dev, DL_WARN,
  5557. "%s: FW exited BOOT mode in %dms, sys_mode=%d\n",
  5558. __func__, *actual_wait, sys_mode);
  5559. if (rc)
  5560. sys_mode = FW_SYS_MODE_UNDEFINED;
  5561. else if (sys_mode == FW_SYS_MODE_BOOT ||
  5562. sys_mode == FW_SYS_MODE_UNDEFINED)
  5563. rc = -EBUSY;
  5564. }
  5565. mutex_lock(&cd->system_lock);
  5566. cd->fw_system_mode = sys_mode;
  5567. mutex_unlock(&cd->system_lock);
  5568. return rc;
  5569. }
  5570. /*******************************************************************************
  5571. * FUNCTION: pt_poll_for_fw_exit_boot_mode
  5572. *
  5573. * SUMMARY: Protected call to _pt_poll_for_fw_exit_boot_mode by exclusive
  5574. * access to the DUT.
  5575. *
  5576. * RETURN:
  5577. * 0 = success
  5578. * !0 = failure
  5579. *
  5580. * PARAMETERS:
  5581. * *cd - pointer to core data
  5582. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5583. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5584. ******************************************************************************/
  5585. static int pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5586. int *actual_wait)
  5587. {
  5588. int rc;
  5589. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5590. if (rc < 0) {
  5591. pt_debug(cd->dev, DL_ERROR,
  5592. "%s: fail get exclusive ex=%p own=%p\n",
  5593. __func__, cd->exclusive_dev, cd->dev);
  5594. return rc;
  5595. }
  5596. rc = _pt_poll_for_fw_exit_boot_mode(cd, timeout, actual_wait);
  5597. if (release_exclusive(cd, cd->dev) < 0)
  5598. pt_debug(cd->dev, DL_ERROR,
  5599. "%s: fail to release exclusive\n", __func__);
  5600. return rc;
  5601. }
  5602. /*******************************************************************************
  5603. * FUNCTION: _pt_get_fw_sys_mode
  5604. *
  5605. * SUMMARY: Determine the FW system mode. For PIP 1.11+ the
  5606. * PIP2 STATUS command is used to directly query the FW system mode. For older
  5607. * PIP versions, there is no direct PIP commamnd that will directly provide this
  5608. * information but any PIP command above 0x1F requires scanning to be disabled
  5609. * before it will be operational. If scanning was not disabled before sending
  5610. * these PIP commands the FW will respond with a 6 byte error response. So to
  5611. * safely determine the scanning state, a PIP message that will not affect the
  5612. * operation of the FW was chosen:
  5613. * "Verify Data Block CRC (ID 0x20)" is sent and if a 6 byte error code is
  5614. * received scanning is active.
  5615. *
  5616. * RETURN:
  5617. * 0 = success
  5618. * !0 = failure
  5619. *
  5620. * PARAMETERS:
  5621. * *cd - pointer to core data
  5622. * *sys_mode - pointer to FW System mode
  5623. * *mode - pointer to mode (BL/FW)
  5624. ******************************************************************************/
  5625. static int _pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5626. {
  5627. int write_length = 1;
  5628. int report_length;
  5629. int rc = 0;
  5630. u8 tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5631. u8 tmp_mode = PT_MODE_UNKNOWN;
  5632. u8 param[1] = { PT_TCH_PARM_EBID };
  5633. struct pt_hid_output hid_output = {
  5634. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  5635. .write_length = write_length,
  5636. .write_buf = param,
  5637. .novalidate = true,
  5638. };
  5639. /* AFter PIP1.11 the preferred method is using STATUS cmd */
  5640. if (IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5641. rc = pt_pip2_get_mode_sysmode_(cd, &tmp_mode, &tmp_sys_mode);
  5642. pt_debug(cd->dev, DL_DEBUG, "%s: tmp_sys_mode=%d tmp_mode=%d\n",
  5643. __func__, tmp_sys_mode, tmp_mode);
  5644. if (!rc) {
  5645. if (tmp_mode != PT_MODE_OPERATIONAL)
  5646. tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5647. }
  5648. goto exit;
  5649. }
  5650. /* Older systems use PIP1 CONFIG_BLOCK_CRC to best determine sys_mode */
  5651. if (cd->mode != PT_MODE_OPERATIONAL) {
  5652. tmp_mode = cd->mode;
  5653. goto exit;
  5654. }
  5655. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5656. if (rc)
  5657. goto exit;
  5658. report_length = (cd->response_buf[1] << 8) | (cd->response_buf[0]);
  5659. if ((report_length == 0x06) &&
  5660. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) == 0x00) &&
  5661. (cd->response_buf[5] == PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5662. tmp_mode = PIP2_STATUS_APP_EXEC;
  5663. tmp_sys_mode = FW_SYS_MODE_SCANNING;
  5664. } else if ((report_length == 0x0A) &&
  5665. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) ==
  5666. PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5667. tmp_mode = PIP2_STATUS_APP_EXEC;
  5668. tmp_sys_mode = FW_SYS_MODE_TEST;
  5669. }
  5670. exit:
  5671. if (mode)
  5672. *mode = tmp_mode;
  5673. if (sys_mode)
  5674. *sys_mode = tmp_sys_mode;
  5675. pt_debug(cd->dev, DL_INFO, "%s: Return Mode=%d sys_mode=%d\n",
  5676. __func__, tmp_mode, tmp_sys_mode);
  5677. return rc;
  5678. }
  5679. /*******************************************************************************
  5680. * FUNCTION: pt_get_fw_sys_mode
  5681. *
  5682. * SUMMARY: Protected call to _pt_get_fw_sys_mode() to determine if FW scanning
  5683. * is active or not.
  5684. *
  5685. * RETURN:
  5686. * 0 = success
  5687. * !0 = failure
  5688. *
  5689. * PARAMETERS:
  5690. * *cd - pointer to core data
  5691. * *sys_mode - pointer to fw system mode
  5692. * *mode - pointer to mode
  5693. ******************************************************************************/
  5694. static int pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5695. {
  5696. int rc;
  5697. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5698. if (rc < 0) {
  5699. pt_debug(cd->dev, DL_ERROR,
  5700. "%s: fail get exclusive ex=%p own=%p\n",
  5701. __func__, cd->exclusive_dev, cd->dev);
  5702. return rc;
  5703. }
  5704. rc = _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5705. if (release_exclusive(cd, cd->dev) < 0)
  5706. pt_debug(cd->dev, DL_ERROR,
  5707. "%s: fail to release exclusive\n", __func__);
  5708. return rc;
  5709. }
  5710. /*******************************************************************************
  5711. * FUNCTION: _pt_request_get_fw_sys_mode
  5712. *
  5713. * SUMMARY: Function pointer included in core_cmds to allow other modules
  5714. * to request to get scan state
  5715. *
  5716. * RETURN:
  5717. * 0 = success
  5718. * !0 = failure
  5719. *
  5720. * PARAMETERS:
  5721. * *dev - pointer to device structure
  5722. * protect - flag to call protected or non-protected
  5723. * *sys_mode - pointer to FW system mode
  5724. * *mode - pointer to mode
  5725. ******************************************************************************/
  5726. static int _pt_request_get_fw_sys_mode(struct device *dev, int protect,
  5727. u8 *sys_mode, u8 *mode)
  5728. {
  5729. struct pt_core_data *cd = dev_get_drvdata(dev);
  5730. if (protect)
  5731. return pt_get_fw_sys_mode(cd, sys_mode, mode);
  5732. return _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5733. }
  5734. /* Default hid descriptor to provide basic register map */
  5735. const struct pt_hid_desc hid_desc_default = {
  5736. 230, /* hid_desc_len */
  5737. HID_APP_REPORT_ID, /* packet_id */
  5738. 0x00, /* reserved_byte */
  5739. 0x0100, /* bcd_version */
  5740. 0x00EC, /* report_desc_len */
  5741. 0x0002, /* report_desc_register */
  5742. 0x0003, /* input_register */
  5743. 0x00FE, /* max_input_len */
  5744. 0x0004, /* output_register */
  5745. 0x00FE, /* max_output_len */
  5746. 0x0005, /* command_register */
  5747. 0x0006, /* data_register */
  5748. 0x04B4, /* vendor_id */
  5749. 0xC101, /* product_id */
  5750. 0x0100, /* version_id */
  5751. {0x00, 0x00, 0x00, 0x00} /* reserved[4] */
  5752. };
  5753. /*******************************************************************************
  5754. * FUNCTION: pt_init_hid_descriptor
  5755. *
  5756. * SUMMARY: Setup default values for HID descriptor structure
  5757. *
  5758. *
  5759. * PARAMETERS:
  5760. * *desc - pointer to the HID descriptor data read back from DUT
  5761. ******************************************************************************/
  5762. static inline void pt_init_hid_descriptor(struct pt_hid_desc *desc)
  5763. {
  5764. memcpy(desc, &hid_desc_default, sizeof(hid_desc_default));
  5765. }
  5766. /*******************************************************************************
  5767. * FUNCTION: pt_get_hid_descriptor_
  5768. *
  5769. * SUMMARY: Send the get HID descriptor command to the DUT and load the response
  5770. * into the HID descriptor structure
  5771. *
  5772. * RETURN:
  5773. * 0 = success
  5774. * !0 = failure
  5775. *
  5776. * PARAMETERS:
  5777. * *cd - pointer to core data
  5778. * *desc - pointer to the HID descriptor data read back from DUT
  5779. ******************************************************************************/
  5780. static int pt_get_hid_descriptor_(struct pt_core_data *cd,
  5781. struct pt_hid_desc *desc)
  5782. {
  5783. struct device *dev = cd->dev;
  5784. int rc = 0;
  5785. int t;
  5786. u8 cmd[2];
  5787. /*
  5788. * During startup the HID descriptor is required for all future
  5789. * processing. If IRQ is already asserted due to an early touch report
  5790. * the report must be cleared before sending command.
  5791. */
  5792. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5793. /* Read HID descriptor length and version */
  5794. mutex_lock(&cd->system_lock);
  5795. cd->hid_cmd_state = 1;
  5796. mutex_unlock(&cd->system_lock);
  5797. /* Set HID descriptor register */
  5798. memcpy(cmd, &cd->hid_core.hid_desc_register,
  5799. sizeof(cd->hid_core.hid_desc_register));
  5800. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer [%zu]",
  5801. __func__, sizeof(cmd));
  5802. pt_pr_buf(cd->dev, DL_DEBUG, cmd, sizeof(cmd), ">>> Get HID Desc");
  5803. rc = pt_adap_write_read_specific(cd, 2, cmd, NULL);
  5804. if (rc) {
  5805. pt_debug(dev, DL_ERROR,
  5806. "%s: failed to get HID descriptor, rc=%d\n",
  5807. __func__, rc);
  5808. goto error;
  5809. }
  5810. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  5811. msecs_to_jiffies(PT_GET_HID_DESCRIPTOR_TIMEOUT));
  5812. if (IS_TMO(t)) {
  5813. #ifdef TTDL_DIAGNOSTICS
  5814. cd->bus_transmit_error_count++;
  5815. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  5816. #endif /* TTDL_DIAGNOSTICS */
  5817. pt_debug(cd->dev, DL_ERROR,
  5818. "%s: HID get descriptor timed out\n", __func__);
  5819. rc = -ETIME;
  5820. goto error;
  5821. } else {
  5822. cd->hw_detected = true;
  5823. }
  5824. /* Load the HID descriptor including all registers */
  5825. memcpy((u8 *)desc, cd->response_buf, sizeof(struct pt_hid_desc));
  5826. /* Check HID descriptor length and version */
  5827. pt_debug(dev, DL_INFO, "%s: HID len:%X HID ver:%X\n", __func__,
  5828. le16_to_cpu(desc->hid_desc_len),
  5829. le16_to_cpu(desc->bcd_version));
  5830. if (le16_to_cpu(desc->hid_desc_len) != sizeof(*desc) ||
  5831. le16_to_cpu(desc->bcd_version) != HID_VERSION) {
  5832. pt_debug(dev, DL_ERROR, "%s: Unsupported HID version\n",
  5833. __func__);
  5834. return -ENODEV;
  5835. }
  5836. goto exit;
  5837. error:
  5838. mutex_lock(&cd->system_lock);
  5839. cd->hid_cmd_state = 0;
  5840. mutex_unlock(&cd->system_lock);
  5841. exit:
  5842. return rc;
  5843. }
  5844. /*******************************************************************************
  5845. * FUNCTION: pt_get_hid_descriptor
  5846. *
  5847. * SUMMARY: Protected call to pt_get_hid_descriptor_()
  5848. *
  5849. * RETURN:
  5850. * 0 = success
  5851. * !0 = failure
  5852. *
  5853. * PARAMETERS:
  5854. * *cd - pointer to core data
  5855. * *desc - pointer to the HID descriptor data read back from DUT
  5856. ******************************************************************************/
  5857. static int pt_get_hid_descriptor(struct pt_core_data *cd,
  5858. struct pt_hid_desc *desc)
  5859. {
  5860. int rc;
  5861. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5862. if (rc < 0) {
  5863. pt_debug(cd->dev, DL_ERROR,
  5864. "%s: fail get exclusive ex=%p own=%p\n",
  5865. __func__, cd->exclusive_dev, cd->dev);
  5866. return rc;
  5867. }
  5868. rc = pt_get_hid_descriptor_(cd, desc);
  5869. if (release_exclusive(cd, cd->dev) < 0)
  5870. pt_debug(cd->dev, DL_ERROR,
  5871. "%s: fail to release exclusive\n", __func__);
  5872. return rc;
  5873. }
  5874. /*******************************************************************************
  5875. * FUNCTION: pt_pip2_get_version_
  5876. *
  5877. * SUMMARY: Sends a PIP2 VERSION command to the DUT and stores the data in
  5878. * cd-ttdata
  5879. *
  5880. * RETURN:
  5881. * 0 = success
  5882. * !0 = failure
  5883. *
  5884. * PARAMETERS:
  5885. * *cd - pointer to core data
  5886. ******************************************************************************/
  5887. static int pt_pip2_get_version_(struct pt_core_data *cd)
  5888. {
  5889. int rc = 0;
  5890. int status;
  5891. u8 read_buf[64];
  5892. u16 actual_read_len;
  5893. rc = _pt_request_pip2_send_cmd(cd->dev,
  5894. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  5895. NULL, 0, read_buf, &actual_read_len);
  5896. if (rc) {
  5897. pt_debug(cd->dev, DL_ERROR,
  5898. "%s: Error Sending PIP2 VERSION Cmd rc=%d\n",
  5899. __func__, rc);
  5900. return rc;
  5901. }
  5902. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5903. if (status == 0) {
  5904. /* Parse the PIP2 VERSION response into ttdata */
  5905. pt_pip2_ver_load_ttdata(cd, actual_read_len);
  5906. } else {
  5907. pt_debug(cd->dev, DL_ERROR,
  5908. "%s: Error in PIP2 VERSION Cmd status=%d\n",
  5909. __func__, status);
  5910. return status;
  5911. }
  5912. return rc;
  5913. }
  5914. /*******************************************************************************
  5915. * FUNCTION: pt_pip2_get_version
  5916. *
  5917. * SUMMARY: Protected call to pt_pip2_get_version_ by exclusive
  5918. * access to the DUT.
  5919. *
  5920. * RETURN:
  5921. * 0 = success
  5922. * !0 = failure
  5923. *
  5924. * PARAMETERS:
  5925. * *cd - pointer to core data structure
  5926. ******************************************************************************/
  5927. static int pt_pip2_get_version(struct pt_core_data *cd)
  5928. {
  5929. int rc;
  5930. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5931. if (rc < 0) {
  5932. pt_debug(cd->dev, DL_ERROR,
  5933. "%s: fail get exclusive ex=%p own=%p\n",
  5934. __func__, cd->exclusive_dev, cd->dev);
  5935. return rc;
  5936. }
  5937. rc = pt_pip2_get_version_(cd);
  5938. if (release_exclusive(cd, cd->dev) < 0)
  5939. pt_debug(cd->dev, DL_ERROR,
  5940. "%s: fail to release exclusive\n", __func__);
  5941. return rc;
  5942. }
  5943. /*******************************************************************************
  5944. * FUNCTION: _pt_request_active_pip_protocol
  5945. *
  5946. * SUMMARY: Get active PIP protocol version using the PIP2 version command.
  5947. * Function will return PIP version of BL or application based on
  5948. * when it's called.
  5949. *
  5950. * RETURN:
  5951. * 0 = success
  5952. * !0 = failure
  5953. *
  5954. * PARAMETERS:
  5955. * *dev - pointer to device structure
  5956. * protect - flag to run in protected mode
  5957. * *pip_version_major - pointer to store PIP major version
  5958. * *pip_version_minor - pointer to store PIP minor version
  5959. ******************************************************************************/
  5960. int _pt_request_active_pip_protocol(struct device *dev, int protect,
  5961. u8 *pip_version_major, u8 *pip_version_minor)
  5962. {
  5963. struct pt_core_data *cd = dev_get_drvdata(dev);
  5964. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  5965. int rc = 0;
  5966. struct pt_hid_output sys_info = {
  5967. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  5968. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  5969. };
  5970. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5971. /* Skip PIP2 command if DUT generation is confirmed */
  5972. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  5973. goto skip_pip2_command;
  5974. rc = pt_pip2_get_version_(cd);
  5975. if (!rc) {
  5976. *pip_version_major = ttdata->pip_ver_major;
  5977. *pip_version_minor = ttdata->pip_ver_minor;
  5978. pt_debug(dev, DL_INFO,
  5979. "%s: pip_version = %d.%d\n", __func__,
  5980. *pip_version_major, *pip_version_minor);
  5981. } else {
  5982. /*
  5983. * Legacy products do not support the pip2 protocol to get
  5984. * pip version. However, they do support the "get sysinfo"
  5985. * command to get pip version from FW, but the bootloader
  5986. * does not support it. This function will try "get sysinfo"
  5987. * command if the pip2 command failed but this cmd could also
  5988. * fail if DUT is stuck in bootloader mode.
  5989. */
  5990. pt_debug(dev, DL_INFO,
  5991. "%s: PIP2 no response rc = %d, try legacy cmd\n",
  5992. __func__, rc);
  5993. skip_pip2_command:
  5994. rc = pt_pip1_send_output_and_wait_(cd, &sys_info);
  5995. if (!rc) {
  5996. *pip_version_minor =
  5997. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET + 1];
  5998. *pip_version_major =
  5999. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  6000. pt_debug(dev, DL_INFO,
  6001. "%s: pip_version = %d.%d\n", __func__,
  6002. *pip_version_major, *pip_version_minor);
  6003. } else {
  6004. *pip_version_major = 0;
  6005. *pip_version_minor = 0;
  6006. pt_debug(dev, DL_ERROR,
  6007. "%s: pip_version Not Detected\n", __func__);
  6008. }
  6009. }
  6010. return rc;
  6011. }
  6012. EXPORT_SYMBOL_GPL(_pt_request_active_pip_protocol);
  6013. /*******************************************************************************
  6014. * FUNCTION: _pt_detect_dut_generation
  6015. *
  6016. * SUMMARY: Determine the generation of device that we are communicating with:
  6017. * DUT_PIP1_ONLY (Gen5 or Gen6)
  6018. * DUT_PIP2_CAPABLE (TC33xx or TT7xxx)
  6019. * The HID_DESC command is supported in Gen5/6 BL and FW as well as
  6020. * TT/TC FW. The packet ID in the descriptor, however, is unique when
  6021. * coming form the BL or the FW:
  6022. * Packet_ID in BL = HID_BL_REPORT_ID (0xFF)
  6023. * Packet_ID in FW = HID_APP_REPORT_ID (0xF7)
  6024. * This function will return a modified status if it detects the DUT
  6025. * is in the BL. In the case of a Gen5/6 BL, which also sends out a FW
  6026. * reset sentinel, the status is "corrected" from a FW to BL sentinel.
  6027. *
  6028. * RETURN:
  6029. * 0 = success
  6030. * !0 = failure
  6031. *
  6032. * PARAMETERS:
  6033. * *dev - pointer to device structure
  6034. * *status - pointer to status bitmask
  6035. * *dut_gen - pointer to store the dut_generation
  6036. * *mode - pointer to store the PT_MODE
  6037. ******************************************************************************/
  6038. static int _pt_detect_dut_generation(struct device *dev,
  6039. u32 *status, u8 *dut_gen, enum pt_mode *mode)
  6040. {
  6041. int rc = 0;
  6042. u8 dut_gen_tmp = DUT_UNKNOWN;
  6043. u8 mode_tmp = PT_MODE_UNKNOWN;
  6044. u8 attempt = 1;
  6045. u32 status_tmp = STARTUP_STATUS_START;
  6046. struct pt_core_data *cd = dev_get_drvdata(dev);
  6047. struct pt_hid_desc hid_desc;
  6048. memset(&hid_desc, 0, sizeof(hid_desc));
  6049. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6050. while (rc && attempt < 3) {
  6051. attempt++;
  6052. usleep_range(2000, 5000);
  6053. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6054. }
  6055. if (!rc && hid_desc.packet_id == HID_BL_REPORT_ID) {
  6056. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 BL */
  6057. mode_tmp = PT_MODE_BOOTLOADER;
  6058. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6059. } else if (!rc && hid_desc.packet_id == HID_APP_REPORT_ID) {
  6060. rc = pt_pip2_get_version_(cd);
  6061. if (!rc)
  6062. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC FW */
  6063. else
  6064. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 FW */
  6065. mode_tmp = PT_MODE_OPERATIONAL;
  6066. status_tmp = STARTUP_STATUS_FW_RESET_SENTINEL;
  6067. rc = 0; /* To return success instead of error code */
  6068. } else if (rc) {
  6069. rc = pt_pip2_get_version_(cd);
  6070. if (!rc) {
  6071. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC BL */
  6072. mode_tmp = PT_MODE_BOOTLOADER;
  6073. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6074. }
  6075. }
  6076. mutex_lock(&cd->system_lock);
  6077. if (dut_gen)
  6078. *dut_gen = dut_gen_tmp;
  6079. if (mode)
  6080. *mode = mode_tmp;
  6081. if (status)
  6082. *status = status_tmp;
  6083. mutex_unlock(&cd->system_lock);
  6084. #ifdef TTDL_DIAGNOSTICS
  6085. pt_debug(cd->dev, DL_INFO, "%s: Generation=%d Mode=%d\n",
  6086. __func__, dut_gen_tmp, mode_tmp);
  6087. #endif /* TTDL_DIAGNOSTICS */
  6088. return rc;
  6089. }
  6090. /*******************************************************************************
  6091. * FUNCTION: _pt_request_dut_generation
  6092. *
  6093. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6094. * to get current dut generation.
  6095. *
  6096. * NOTE: This function WILL NOT try to determine dut generation.
  6097. *
  6098. * RETURN:
  6099. * The current dut generation.
  6100. *
  6101. * PARAMETERS:
  6102. * *dev - pointer to device structure
  6103. ******************************************************************************/
  6104. static int _pt_request_dut_generation(struct device *dev)
  6105. {
  6106. struct pt_core_data *cd = dev_get_drvdata(dev);
  6107. return cd->active_dut_generation;
  6108. }
  6109. #define HW_VERSION_LEN_MAX 13
  6110. /*******************************************************************************
  6111. * FUNCTION: _legacy_generate_hw_version
  6112. *
  6113. * SUMMARY: Format chip information from struct ttdata (maintained by PIP1
  6114. * SYSINFO command) or struct bl_info (maintained by PIP1 BL INFORMATION
  6115. * command) to the hw_version.
  6116. *
  6117. * RETURN:
  6118. * 0 = success
  6119. * !0 = failure
  6120. *
  6121. * PARAMETERS:
  6122. * *cd - pointer to core data
  6123. * *hw_version - pointer to store the hardware version
  6124. ******************************************************************************/
  6125. static int _legacy_generate_hw_version(struct pt_core_data *cd,
  6126. char *hw_version)
  6127. {
  6128. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6129. if (cd->sysinfo.ready) {
  6130. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6131. ttdata->jtag_id_h, cd->pid_for_loader);
  6132. return 0;
  6133. } else if (cd->bl_info.ready) {
  6134. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6135. cd->bl_info.chip_id, cd->pid_for_loader);
  6136. return 0;
  6137. } else {
  6138. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6139. pt_debug(cd->dev, DL_ERROR,
  6140. "%s: SYSINFO and BL_INFO are not ready\n", __func__);
  6141. return -ENODATA;
  6142. }
  6143. }
  6144. /*******************************************************************************
  6145. * FUNCTION: _pip2_generate_hw_version
  6146. *
  6147. * SUMMARY: Format chip information from struct ttdata (maintained by PIP2
  6148. * VERSION command) to the hw_version.
  6149. *
  6150. * RETURN:
  6151. * 0 = success
  6152. * !0 = failure
  6153. *
  6154. * PARAMETERS:
  6155. * *cd - pointer to core data
  6156. * *hw_version - pointer to store the hardware version
  6157. ******************************************************************************/
  6158. static int _pip2_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6159. {
  6160. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6161. if (cd->app_pip_ver_ready | cd->bl_pip_ver_ready) {
  6162. snprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.%04X.%02X",
  6163. ttdata->chip_id, ttdata->chip_rev, cd->pid_for_loader);
  6164. return 0;
  6165. } else {
  6166. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6167. pt_debug(cd->dev, DL_ERROR,
  6168. "%s: PIP Version are not ready\n", __func__);
  6169. return -ENODATA;
  6170. }
  6171. }
  6172. /*******************************************************************************
  6173. * FUNCTION: pt_generate_hw_version
  6174. *
  6175. * SUMMARY: Wraaper function for both legacy and TT/TC products generate the
  6176. * hw_version from static data.
  6177. *
  6178. * RETURN:
  6179. * 0 = success
  6180. * !0 = failure
  6181. *
  6182. * PARAMETERS:
  6183. * *cd - pointer to core data
  6184. * *hw_version - pointer to store the hardware version
  6185. ******************************************************************************/
  6186. static int pt_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6187. {
  6188. int rc = 0;
  6189. if (!hw_version)
  6190. return -ENOMEM;
  6191. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  6192. rc = _legacy_generate_hw_version(cd, hw_version);
  6193. else if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  6194. rc = _pip2_generate_hw_version(cd, hw_version);
  6195. else {
  6196. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6197. rc = -ENODATA;
  6198. }
  6199. return rc;
  6200. }
  6201. /*******************************************************************************
  6202. * SUMMARY: Attempt to retrieve the HW version of the connected DUT
  6203. *
  6204. * NOTE: The calling function must ensure to free *hw_version
  6205. *
  6206. * RETURN:
  6207. * 0 = success
  6208. * !0 = Failure
  6209. *
  6210. * PARAMETERS:
  6211. * *dev - pointer to device structure
  6212. * *hw_version - pointer to where the hw_version string will be stored
  6213. ******************************************************************************/
  6214. static int _pt_request_hw_version(struct device *dev, char *hw_version)
  6215. {
  6216. int rc = 0;
  6217. u16 actual_read_len;
  6218. u16 pip_ver;
  6219. u8 rd_buf[256];
  6220. u8 status;
  6221. u8 index = PIP2_RESP_STATUS_OFFSET;
  6222. u8 return_data[8];
  6223. u8 panel_id;
  6224. struct pt_core_data *cd = dev_get_drvdata(dev);
  6225. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6226. if (!hw_version)
  6227. return -ENOMEM;
  6228. if (!cd->hw_detected) {
  6229. /* No HW detected */
  6230. rc = -ENODEV;
  6231. pt_debug(dev, DL_ERROR, "%s: no hardware is detected!\n",
  6232. __func__);
  6233. goto exit_error;
  6234. }
  6235. /* For Parade TC or TT parts */
  6236. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  6237. rc = _pt_request_pip2_send_cmd(dev,
  6238. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  6239. NULL, 0, rd_buf, &actual_read_len);
  6240. if (rc) {
  6241. pt_debug(dev, DL_ERROR,
  6242. "%s: Failed to send PIP2 VERSION cmd\n",
  6243. __func__);
  6244. goto exit_error;
  6245. }
  6246. status = rd_buf[index];
  6247. if (status == 0) {
  6248. pip_ver = 256 * rd_buf[index + 2] + rd_buf[index + 1];
  6249. /*
  6250. * BL PIP 2.02 and greater the version fields are
  6251. * swapped
  6252. */
  6253. if (pip_ver >= 0x0202) {
  6254. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6255. "%02X%02X.%02X%02X.FF",
  6256. rd_buf[index + 10], rd_buf[index + 9],
  6257. rd_buf[index + 8], rd_buf[index + 7]);
  6258. } else {
  6259. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6260. "%02X%02X.%02X%02X.FF",
  6261. rd_buf[index + 8], rd_buf[index + 7],
  6262. rd_buf[index + 10], rd_buf[index + 9]);
  6263. }
  6264. return STATUS_SUCCESS;
  6265. } else {
  6266. rc = status;
  6267. pt_debug(dev, DL_WARN,
  6268. "%s: PIP2 VERSION cmd response error\n",
  6269. __func__);
  6270. }
  6271. } else if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  6272. /*
  6273. * For Parade/Cypress legacy parts the RevID and FamilyID are
  6274. * hard coded to FFFF
  6275. */
  6276. if (cd->mode == PT_MODE_OPERATIONAL) {
  6277. rc = pt_hid_output_get_sysinfo(cd);
  6278. if (!rc) {
  6279. panel_id =
  6280. cd->sysinfo.sensing_conf_data.panel_id;
  6281. } else {
  6282. panel_id = PANEL_ID_NOT_ENABLED;
  6283. }
  6284. /* In FW - simply retrieve from ttdata struct */
  6285. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6286. "%04X.FFFF.%02X",
  6287. ttdata->jtag_id_h,
  6288. panel_id);
  6289. return STATUS_SUCCESS;
  6290. } else {
  6291. /*
  6292. * Return the stored value if PT_PANEL_ID_BY_BL
  6293. * is not supported while other feature is.
  6294. */
  6295. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  6296. rc = pt_hid_output_bl_get_information(
  6297. cd, return_data);
  6298. if (!rc) {
  6299. rc = pt_hid_output_bl_get_panel_id(
  6300. cd, &panel_id);
  6301. }
  6302. } else
  6303. panel_id = cd->pid_for_loader;
  6304. if (!rc) {
  6305. snprintf(hw_version,
  6306. HW_VERSION_LEN_MAX,
  6307. "%02X%02X.FFFF.%02X",
  6308. return_data[3], return_data[2],
  6309. panel_id);
  6310. return STATUS_SUCCESS;
  6311. }
  6312. }
  6313. } else {
  6314. /* Unknown generation */
  6315. rc = -ENODEV;
  6316. pt_debug(dev, DL_ERROR, "%s: generation is unknown!\n",
  6317. __func__);
  6318. }
  6319. exit_error:
  6320. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6321. return rc;
  6322. }
  6323. /*******************************************************************************
  6324. * FUNCTION: pt_start_wd_timer
  6325. *
  6326. * SUMMARY: Starts the TTDL watchdog timer if the timer interval is > 0
  6327. *
  6328. * RETURN: void
  6329. *
  6330. * PARAMETERS:
  6331. * *cd - pointer to core data
  6332. ******************************************************************************/
  6333. static void pt_start_wd_timer(struct pt_core_data *cd)
  6334. {
  6335. if (cd->watchdog_interval < 100) {
  6336. pt_debug(cd->dev, DL_ERROR,
  6337. "%s: WARNING: Invalid watchdog interval: %d\n",
  6338. __func__, cd->watchdog_interval);
  6339. return;
  6340. }
  6341. if (cd->watchdog_force_stop) {
  6342. pt_debug(cd->dev, DL_INFO,
  6343. "%s: TTDL WD Forced stop\n", __func__);
  6344. return;
  6345. }
  6346. mod_timer(&cd->watchdog_timer, jiffies +
  6347. msecs_to_jiffies(cd->watchdog_interval));
  6348. cd->watchdog_enabled = 1;
  6349. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Started\n", __func__);
  6350. }
  6351. /*******************************************************************************
  6352. * FUNCTION: pt_stop_wd_timer
  6353. *
  6354. * SUMMARY: Stops the TTDL watchdog timer if the timer interval is > 0
  6355. *
  6356. * RETURN: void
  6357. *
  6358. * PARAMETERS:
  6359. * *cd - pointer to core data
  6360. ******************************************************************************/
  6361. static void pt_stop_wd_timer(struct pt_core_data *cd)
  6362. {
  6363. if (!cd->watchdog_interval)
  6364. return;
  6365. /*
  6366. * Ensure we wait until the watchdog timer
  6367. * running on a different CPU finishes
  6368. */
  6369. del_timer_sync(&cd->watchdog_timer);
  6370. cancel_work_sync(&cd->watchdog_work);
  6371. del_timer_sync(&cd->watchdog_timer);
  6372. cd->watchdog_enabled = 0;
  6373. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Stopped\n", __func__);
  6374. }
  6375. /*******************************************************************************
  6376. * FUNCTION: pt_hw_soft_reset
  6377. *
  6378. * SUMMARY: Sends a PIP reset command to the DUT. Disable/re-enable the
  6379. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6380. * schedule an enum if it fires when the DUT is being reset.
  6381. * This can cause a double reset.
  6382. *
  6383. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6384. * the WD active could cause this function to fail!
  6385. *
  6386. * RETURN:
  6387. * 0 = success
  6388. * !0 = failure
  6389. *
  6390. * PARAMETERS:
  6391. * *cd - pointer to core data struct
  6392. * protect - flag to call protected or non-protected
  6393. ******************************************************************************/
  6394. static int pt_hw_soft_reset(struct pt_core_data *cd, int protect)
  6395. {
  6396. int rc = 0;
  6397. mutex_lock(&cd->system_lock);
  6398. cd->startup_status = STARTUP_STATUS_START;
  6399. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  6400. mutex_unlock(&cd->system_lock);
  6401. if (protect)
  6402. rc = pt_hid_cmd_reset(cd);
  6403. else
  6404. rc = pt_hid_cmd_reset_(cd);
  6405. if (rc < 0) {
  6406. pt_debug(cd->dev, DL_ERROR,
  6407. "%s: FAILED to execute SOFT reset\n", __func__);
  6408. return rc;
  6409. }
  6410. pt_debug(cd->dev, DL_INFO, "%s: SOFT reset successful\n",
  6411. __func__);
  6412. return 0;
  6413. }
  6414. /*******************************************************************************
  6415. * FUNCTION: pt_hw_hard_reset
  6416. *
  6417. * SUMMARY: Calls the platform xres function if it exists to perform a hard
  6418. * reset on the DUT by toggling the XRES gpio. Disable/re-enable the
  6419. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6420. * schedule an enum if it fires when the DUT is being reset.
  6421. * This can cause a double reset.
  6422. *
  6423. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6424. * the WD active could cause this function to fail!
  6425. *
  6426. * RETURN:
  6427. * 0 = success
  6428. * !0 = failure
  6429. *
  6430. * PARAMETERS:
  6431. * *cd - pointer to core data struct
  6432. ******************************************************************************/
  6433. static int pt_hw_hard_reset(struct pt_core_data *cd)
  6434. {
  6435. if (cd->cpdata->xres) {
  6436. cd->startup_status = STARTUP_STATUS_START;
  6437. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n",
  6438. __func__);
  6439. cd->cpdata->xres(cd->cpdata, cd->dev);
  6440. pt_debug(cd->dev, DL_WARN, "%s: executed HARD reset\n",
  6441. __func__);
  6442. return 0;
  6443. }
  6444. pt_debug(cd->dev, DL_ERROR,
  6445. "%s: FAILED to execute HARD reset\n", __func__);
  6446. return -ENODEV;
  6447. }
  6448. /*******************************************************************************
  6449. * FUNCTION: pt_dut_reset
  6450. *
  6451. * SUMMARY: Attempts to reset the DUT by a hard reset and if that fails a
  6452. * soft reset.
  6453. *
  6454. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6455. * the WD active could cause this function to fail!
  6456. * NOTE: "protect" flag is only used for soft reset.
  6457. *
  6458. * RETURN:
  6459. * 0 = success
  6460. * !0 = failure
  6461. *
  6462. * PARAMETERS:
  6463. * *cd - pointer to core data structure
  6464. * protect - flag to call protected or non-protected
  6465. ******************************************************************************/
  6466. static int pt_dut_reset(struct pt_core_data *cd, int protect)
  6467. {
  6468. int rc = 0;
  6469. pt_debug(cd->dev, DL_INFO, "%s: reset hw...\n", __func__);
  6470. mutex_lock(&cd->system_lock);
  6471. cd->hid_reset_cmd_state = 1;
  6472. rc = pt_hw_hard_reset(cd);
  6473. mutex_unlock(&cd->system_lock);
  6474. if (rc == -ENODEV) {
  6475. mutex_lock(&cd->system_lock);
  6476. cd->hid_reset_cmd_state = 0;
  6477. mutex_unlock(&cd->system_lock);
  6478. pt_debug(cd->dev, DL_ERROR,
  6479. "%s: Hard reset failed, try soft reset\n", __func__);
  6480. rc = pt_hw_soft_reset(cd, protect);
  6481. }
  6482. if (rc)
  6483. pt_debug(cd->dev, DL_ERROR, "%s: %s dev='%s' r=%d\n",
  6484. __func__, "Fail hw reset", dev_name(cd->dev), rc);
  6485. return rc;
  6486. }
  6487. /*******************************************************************************
  6488. * FUNCTION: pt_dut_reset_and_wait
  6489. *
  6490. * SUMMARY: Wrapper function for pt_dut_reset that waits for the reset to
  6491. * complete
  6492. *
  6493. * RETURN:
  6494. * 0 = success
  6495. * !0 = failure
  6496. *
  6497. * PARAMETERS:
  6498. * *cd - pointer to core data structure
  6499. ******************************************************************************/
  6500. static int pt_dut_reset_and_wait(struct pt_core_data *cd)
  6501. {
  6502. int rc = 0;
  6503. int t;
  6504. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  6505. if (rc < 0)
  6506. goto exit;
  6507. t = wait_event_timeout(cd->wait_q,
  6508. (cd->hid_reset_cmd_state == 0),
  6509. msecs_to_jiffies(PT_HID_CMD_DEFAULT_TIMEOUT));
  6510. if (IS_TMO(t)) {
  6511. #ifdef TTDL_DIAGNOSTICS
  6512. cd->bus_transmit_error_count++;
  6513. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  6514. #endif /* TTDL_DIAGNOSTICS */
  6515. pt_debug(cd->dev, DL_ERROR, "%s: reset timed out\n",
  6516. __func__);
  6517. rc = -ETIME;
  6518. goto exit;
  6519. }
  6520. exit:
  6521. return rc;
  6522. }
  6523. /*
  6524. * touch default parameters (from report descriptor) to resolve protocol for
  6525. * touch report
  6526. */
  6527. const struct pt_tch_abs_params tch_hdr_default[PT_TCH_NUM_HDR] = {
  6528. /* byte offset, size, min, max, bit offset, report */
  6529. {0x00, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* SCAN TIME */
  6530. {0x02, 0x01, 0x00, 0x20, 0x00, 0x01}, /* NUMBER OF RECORDS */
  6531. {0x02, 0x01, 0x00, 0x02, 0x05, 0x01}, /* LARGE OBJECT */
  6532. {0x03, 0x01, 0x00, 0x08, 0x00, 0x01}, /* NOISE EFFECT */
  6533. {0x03, 0x01, 0x00, 0x04, 0x06, 0x01}, /* REPORT_COUNTER */
  6534. };
  6535. /*
  6536. * button default parameters (from report descriptor) to resolve protocol for
  6537. * button report
  6538. */
  6539. const struct pt_tch_abs_params tch_abs_default[PT_TCH_NUM_ABS] = {
  6540. /* byte offset, size, min, max, bit offset, report */
  6541. {0x02, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* X */
  6542. {0x04, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* Y */
  6543. {0x06, 0x01, 0x00, 0x100, 0x00, 0x01}, /* P (Z) */
  6544. {0x01, 0x01, 0x00, 0x20, 0x00, 0x01}, /* TOUCH ID */
  6545. {0x01, 0x01, 0x00, 0x04, 0x05, 0x01}, /* EVENT ID */
  6546. {0x00, 0x01, 0x00, 0x08, 0x00, 0x01}, /* OBJECT ID */
  6547. {0x01, 0x01, 0x00, 0x02, 0x07, 0x01}, /* LIFTOFF */
  6548. {0x07, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MAJOR */
  6549. {0x08, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MINOR */
  6550. {0x09, 0x01, 0x00, 0x100, 0x00, 0x01}, /* ORIENTATION */
  6551. };
  6552. /*******************************************************************************
  6553. * FUNCTION: pt_init_pip_report_fields
  6554. *
  6555. * SUMMARY: Setup default values for touch/button report parsing.
  6556. *
  6557. * PARAMETERS:
  6558. * *cd - pointer to core data structure
  6559. ******************************************************************************/
  6560. static void pt_init_pip_report_fields(struct pt_core_data *cd)
  6561. {
  6562. struct pt_sysinfo *si = &cd->sysinfo;
  6563. memcpy(si->tch_hdr, tch_hdr_default, sizeof(tch_hdr_default));
  6564. memcpy(si->tch_abs, tch_abs_default, sizeof(tch_abs_default));
  6565. si->desc.tch_report_id = PT_PIP_TOUCH_REPORT_ID;
  6566. si->desc.tch_record_size = TOUCH_REPORT_SIZE;
  6567. si->desc.tch_header_size = TOUCH_INPUT_HEADER_SIZE;
  6568. si->desc.btn_report_id = PT_PIP_CAPSENSE_BTN_REPORT_ID;
  6569. cd->features.easywake = 1;
  6570. cd->features.noise_metric = 1;
  6571. cd->features.tracking_heatmap = 1;
  6572. cd->features.sensor_data = 1;
  6573. }
  6574. /*******************************************************************************
  6575. * FUNCTION: pt_get_mode
  6576. *
  6577. * SUMMARY: Determine the current mode from the contents of a HID descriptor
  6578. * message
  6579. *
  6580. * RETURN: Enum of the current mode
  6581. *
  6582. * PARAMETERS:
  6583. * *cd - pointer to the Core Data structure
  6584. * protect - run command in protected mode
  6585. * *mode - pointer to store the retrieved mode
  6586. ******************************************************************************/
  6587. static int pt_get_mode(struct pt_core_data *cd, struct pt_hid_desc *desc)
  6588. {
  6589. if (desc->packet_id == HID_APP_REPORT_ID)
  6590. return PT_MODE_OPERATIONAL;
  6591. else if (desc->packet_id == HID_BL_REPORT_ID)
  6592. return PT_MODE_BOOTLOADER;
  6593. return PT_MODE_UNKNOWN;
  6594. }
  6595. /*******************************************************************************
  6596. * FUNCTION: _pt_request_get_mode
  6597. *
  6598. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6599. * to determine the current mode of the DUT by use of the Get HID
  6600. * Descriptor command.
  6601. *
  6602. * RETURN:
  6603. * 0 = success
  6604. * !0 = failure
  6605. *
  6606. * PARAMETERS:
  6607. * *dev - pointer to device structure
  6608. * protect - run command in protected mode
  6609. * *mode - pointer to store the retrieved mode
  6610. ******************************************************************************/
  6611. static int _pt_request_get_mode(struct device *dev, int protect, u8 *mode)
  6612. {
  6613. struct pt_core_data *cd = dev_get_drvdata(dev);
  6614. struct pt_hid_desc hid_desc;
  6615. int rc = 0;
  6616. memset(&hid_desc, 0, sizeof(hid_desc));
  6617. if (protect)
  6618. rc = pt_get_hid_descriptor(cd, &hid_desc);
  6619. else
  6620. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6621. if (rc)
  6622. *mode = PT_MODE_UNKNOWN;
  6623. else
  6624. *mode = pt_get_mode(cd, &hid_desc);
  6625. return rc;
  6626. }
  6627. /*******************************************************************************
  6628. * FUNCTION: pt_queue_enum_
  6629. *
  6630. * SUMMARY: Queues a TTDL enum by scheduling work with the pt_enum_with_dut()
  6631. * function. It won't try to add/delete sysfs node or modules.
  6632. *
  6633. * RETURN: void
  6634. *
  6635. * PARAMETERS:
  6636. * *cd - pointer to core data
  6637. ******************************************************************************/
  6638. static void pt_queue_enum_(struct pt_core_data *cd)
  6639. {
  6640. if (cd->startup_state == STARTUP_NONE) {
  6641. cd->startup_state = STARTUP_QUEUED;
  6642. #ifdef TTDL_DIAGNOSTICS
  6643. if (!cd->bridge_mode)
  6644. schedule_work(&cd->enum_work);
  6645. else
  6646. cd->startup_state = STARTUP_NONE;
  6647. #else
  6648. schedule_work(&cd->enum_work);
  6649. #endif
  6650. pt_debug(cd->dev, DL_INFO,
  6651. "%s: enum_work queued\n", __func__);
  6652. } else {
  6653. pt_debug(cd->dev, DL_WARN,
  6654. "%s: Enum not queued - startup_state = %d\n",
  6655. __func__, cd->startup_state);
  6656. }
  6657. }
  6658. /*******************************************************************************
  6659. * FUNCTION: pt_queue_enum
  6660. *
  6661. * SUMMARY: Queues a TTDL enum within a mutex lock
  6662. *
  6663. * RETURN: void
  6664. *
  6665. * PARAMETERS:
  6666. * *cd - pointer to core data
  6667. ******************************************************************************/
  6668. static void pt_queue_enum(struct pt_core_data *cd)
  6669. {
  6670. mutex_lock(&cd->system_lock);
  6671. pt_queue_enum_(cd);
  6672. mutex_unlock(&cd->system_lock);
  6673. }
  6674. static void remove_sysfs_and_modules(struct device *dev);
  6675. /*******************************************************************************
  6676. * FUNCTION: pt_queue_restart
  6677. *
  6678. * SUMMARY: Queues a TTDL restart within a mutex lock
  6679. *
  6680. * RETURN: void
  6681. *
  6682. * PARAMETERS:
  6683. * *cd - pointer to core data
  6684. * remove_sysfs_module - True: remove all DUT relative sysfs nodes and modules
  6685. * False: will not perform remove action
  6686. ******************************************************************************/
  6687. static void pt_queue_restart(struct pt_core_data *cd)
  6688. {
  6689. mutex_lock(&cd->system_lock);
  6690. if (cd->startup_state == STARTUP_NONE) {
  6691. cd->startup_state = STARTUP_QUEUED;
  6692. schedule_work(&cd->ttdl_restart_work);
  6693. pt_debug(cd->dev, DL_INFO,
  6694. "%s: pt_ttdl_restart queued\n", __func__);
  6695. } else {
  6696. pt_debug(cd->dev, DL_INFO, "%s: startup_state = %d\n",
  6697. __func__, cd->startup_state);
  6698. }
  6699. mutex_unlock(&cd->system_lock);
  6700. }
  6701. /*******************************************************************************
  6702. * FUNCTION: call_atten_cb
  6703. *
  6704. * SUMMARY: Iterate over attention list call the function that registered.
  6705. *
  6706. * RETURN: void
  6707. *
  6708. * PARAMETERS:
  6709. * *cd - pointer to core data
  6710. * type - type of attention list
  6711. * mode - condition for execution
  6712. ******************************************************************************/
  6713. static void call_atten_cb(struct pt_core_data *cd,
  6714. enum pt_atten_type type, int mode)
  6715. {
  6716. struct atten_node *atten, *atten_n;
  6717. pt_debug(cd->dev, DL_DEBUG, "%s: check list type=%d mode=%d\n",
  6718. __func__, type, mode);
  6719. spin_lock(&cd->spinlock);
  6720. list_for_each_entry_safe(atten, atten_n,
  6721. &cd->atten_list[type], node) {
  6722. if (!mode || atten->mode & mode) {
  6723. spin_unlock(&cd->spinlock);
  6724. pt_debug(cd->dev, DL_DEBUG,
  6725. "%s: attention for '%s'",
  6726. __func__, dev_name(atten->dev));
  6727. atten->func(atten->dev);
  6728. spin_lock(&cd->spinlock);
  6729. }
  6730. }
  6731. spin_unlock(&cd->spinlock);
  6732. }
  6733. /*******************************************************************************
  6734. * FUNCTION: start_fw_upgrade
  6735. *
  6736. * SUMMARY: Calling "PT_ATTEN_LOADER" attention list that loader registered to
  6737. * start firmware upgrade.
  6738. *
  6739. * RETURN:
  6740. * 0 = success
  6741. *
  6742. * PARAMETERS:
  6743. * *data - pointer to core data
  6744. ******************************************************************************/
  6745. static int start_fw_upgrade(void *data)
  6746. {
  6747. struct pt_core_data *cd = (struct pt_core_data *)data;
  6748. call_atten_cb(cd, PT_ATTEN_LOADER, 0);
  6749. return 0;
  6750. }
  6751. /*******************************************************************************
  6752. * FUNCTION: pt_put_device_into_easy_wakeup_
  6753. *
  6754. * SUMMARY: Call the enter_easywake_state function and set the device into easy
  6755. * wake up state.
  6756. *
  6757. * RETURN:
  6758. * 0 = success
  6759. * !0 = failure
  6760. *
  6761. * PARAMETERS:
  6762. * *cd - pointer to core data
  6763. ******************************************************************************/
  6764. static int pt_put_device_into_easy_wakeup_(struct pt_core_data *cd)
  6765. {
  6766. int rc = 0;
  6767. u8 status = 0;
  6768. mutex_lock(&cd->system_lock);
  6769. cd->wait_until_wake = 0;
  6770. mutex_unlock(&cd->system_lock);
  6771. rc = pt_hid_output_enter_easywake_state_(cd,
  6772. cd->easy_wakeup_gesture, &status);
  6773. if (rc || status == 0)
  6774. return -EBUSY;
  6775. return rc;
  6776. }
  6777. /*******************************************************************************
  6778. * FUNCTION: pt_put_device_into_deep_sleep_
  6779. *
  6780. * SUMMARY: Call the set_power function and set the DUT to deep sleep
  6781. *
  6782. * RETURN:
  6783. * 0 = success
  6784. * !0 = error
  6785. *
  6786. * PARAMETERS:
  6787. * *cd - pointer to core data
  6788. ******************************************************************************/
  6789. static int pt_put_device_into_deep_sleep_(struct pt_core_data *cd)
  6790. {
  6791. int rc = 0;
  6792. rc = pt_hid_cmd_set_power_(cd, HID_POWER_SLEEP);
  6793. if (rc)
  6794. rc = -EBUSY;
  6795. return rc;
  6796. }
  6797. /*******************************************************************************
  6798. * FUNCTION: pt_put_device_into_deep_standby_
  6799. *
  6800. * SUMMARY: Call the set_power function and set the DUT to Deep Standby
  6801. *
  6802. * RETURN:
  6803. * 0 = success
  6804. * !0 = error
  6805. *
  6806. * PARAMETERS:
  6807. * *cd - pointer to core data
  6808. ******************************************************************************/
  6809. static int pt_put_device_into_deep_standby_(struct pt_core_data *cd)
  6810. {
  6811. int rc = 0;
  6812. rc = pt_hid_cmd_set_power_(cd, HID_POWER_STANDBY);
  6813. if (rc)
  6814. rc = -EBUSY;
  6815. return rc;
  6816. }
  6817. /*******************************************************************************
  6818. * FUNCTION: pt_core_poweroff_device_
  6819. *
  6820. * SUMMARY: Disable IRQ and HW power down the device.
  6821. *
  6822. * RETURN:
  6823. * 0 = success
  6824. * !0 = failure
  6825. *
  6826. * PARAMETERS:
  6827. * *cd - pointer to core data
  6828. ******************************************************************************/
  6829. static int pt_core_poweroff_device_(struct pt_core_data *cd)
  6830. {
  6831. int rc;
  6832. if (cd->irq_enabled) {
  6833. cd->irq_enabled = false;
  6834. disable_irq_nosync(cd->irq);
  6835. }
  6836. rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, 0);
  6837. if (rc < 0)
  6838. pt_debug(cd->dev, DL_ERROR, "%s: HW Power down fails r=%d\n",
  6839. __func__, rc);
  6840. return rc;
  6841. }
  6842. /*******************************************************************************
  6843. * FUNCTION: pt_core_sleep_
  6844. *
  6845. * SUMMARY: Suspend the device with power off or deep sleep based on the
  6846. * configuration in the core platform data structure.
  6847. *
  6848. * RETURN:
  6849. * 0 = success
  6850. * !0 = failure
  6851. *
  6852. * PARAMETERS:
  6853. * *cd - pointer to core data
  6854. ******************************************************************************/
  6855. static int pt_core_sleep_(struct pt_core_data *cd)
  6856. {
  6857. int rc = 0;
  6858. mutex_lock(&cd->system_lock);
  6859. if (cd->sleep_state == SS_SLEEP_OFF) {
  6860. cd->sleep_state = SS_SLEEPING;
  6861. } else {
  6862. mutex_unlock(&cd->system_lock);
  6863. return 1;
  6864. }
  6865. mutex_unlock(&cd->system_lock);
  6866. /* Ensure watchdog and startup works stopped */
  6867. pt_stop_wd_timer(cd);
  6868. cancel_work_sync(&cd->enum_work);
  6869. pt_stop_wd_timer(cd);
  6870. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  6871. rc = pt_put_device_into_easy_wakeup_(cd);
  6872. else if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  6873. pt_debug(cd->dev, DL_INFO,
  6874. "%s: Entering into poweroff mode:\n", __func__);
  6875. rc = pt_core_poweroff_device_(cd);
  6876. if (rc < 0)
  6877. pr_err("%s: Poweroff error detected :rc=%d\n",
  6878. __func__, rc);
  6879. }
  6880. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  6881. rc = pt_put_device_into_deep_standby_(cd);
  6882. else
  6883. rc = pt_put_device_into_deep_sleep_(cd);
  6884. mutex_lock(&cd->system_lock);
  6885. cd->sleep_state = SS_SLEEP_ON;
  6886. mutex_unlock(&cd->system_lock);
  6887. return rc;
  6888. }
  6889. /*******************************************************************************
  6890. * FUNCTION: pt_core_sleep
  6891. *
  6892. * SUMMARY: Protected call to pt_core_sleep_ by exclusive access to the DUT.
  6893. *
  6894. * RETURN:
  6895. * 0 = success
  6896. * !0 = failure
  6897. *
  6898. * PARAMETERS:
  6899. * *cd - pointer to core data
  6900. ******************************************************************************/
  6901. static int pt_core_sleep(struct pt_core_data *cd)
  6902. {
  6903. int rc = 0;
  6904. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6905. if (rc < 0) {
  6906. pt_debug(cd->dev, DL_ERROR,
  6907. "%s: fail get exclusive ex=%p own=%p\n",
  6908. __func__, cd->exclusive_dev, cd->dev);
  6909. return rc;
  6910. }
  6911. rc = pt_core_sleep_(cd);
  6912. if (release_exclusive(cd, cd->dev) < 0)
  6913. pt_debug(cd->dev, DL_ERROR,
  6914. "%s: fail to release exclusive\n", __func__);
  6915. else
  6916. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  6917. __func__);
  6918. return rc;
  6919. }
  6920. /*******************************************************************************
  6921. * FUNCTION: pt_wakeup_host
  6922. *
  6923. * SUMMARY: Check wake up report and call the PT_ATTEN_WAKE attention list.
  6924. *
  6925. * NOTE: TSG5 EasyWake and TSG6 EasyWake use different protocol.
  6926. *
  6927. * RETURN:
  6928. * 0 = success
  6929. * !0 = failure
  6930. *
  6931. * PARAMETERS:
  6932. * *cd - pointer to core data
  6933. ******************************************************************************/
  6934. static int pt_wakeup_host(struct pt_core_data *cd)
  6935. {
  6936. #ifndef EASYWAKE_TSG6
  6937. /* TSG5 EasyWake */
  6938. int rc = 0;
  6939. int event_id;
  6940. int size = get_unaligned_le16(&cd->input_buf[0]);
  6941. /* Validate report */
  6942. if (size != 4 || cd->input_buf[2] != 4)
  6943. rc = -EINVAL;
  6944. event_id = cd->input_buf[3];
  6945. pt_debug(cd->dev, DL_INFO, "%s: e=%d, rc=%d\n",
  6946. __func__, event_id, rc);
  6947. if (rc) {
  6948. pt_core_sleep_(cd);
  6949. goto exit;
  6950. }
  6951. /* attention WAKE */
  6952. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  6953. exit:
  6954. return rc;
  6955. #else
  6956. /* TSG6 FW1.3 EasyWake */
  6957. int rc = 0;
  6958. int i = 0;
  6959. int report_length;
  6960. /* Validate report */
  6961. if (cd->input_buf[2] != PT_PIP_WAKEUP_REPORT_ID) {
  6962. rc = -EINVAL;
  6963. pt_core_sleep_(cd);
  6964. goto exit;
  6965. }
  6966. /* Get gesture id and gesture data length */
  6967. cd->gesture_id = cd->input_buf[3];
  6968. report_length = (cd->input_buf[1] << 8) | (cd->input_buf[0]);
  6969. cd->gesture_data_length = report_length - 4;
  6970. pt_debug(cd->dev, DL_INFO,
  6971. "%s: gesture_id = %d, gesture_data_length = %d\n",
  6972. __func__, cd->gesture_id, cd->gesture_data_length);
  6973. for (i = 0; i < cd->gesture_data_length; i++)
  6974. cd->gesture_data[i] = cd->input_buf[4 + i];
  6975. /* attention WAKE */
  6976. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  6977. exit:
  6978. return rc;
  6979. #endif
  6980. }
  6981. /*******************************************************************************
  6982. * FUNCTION: pt_get_touch_axis
  6983. *
  6984. * SUMMARY: Function to calculate touch axis
  6985. *
  6986. * PARAMETERS:
  6987. * *cd - pointer to core data structure
  6988. * *axis - pointer to axis calculation result
  6989. * size - size in bytes
  6990. * max - max value of result
  6991. * *xy_data - pointer to input data to be parsed
  6992. * bofs - bit offset
  6993. ******************************************************************************/
  6994. static void pt_get_touch_axis(struct pt_core_data *cd,
  6995. int *axis, int size, int max, u8 *data, int bofs)
  6996. {
  6997. int nbyte;
  6998. int next;
  6999. for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
  7000. *axis = *axis + ((data[next] >> bofs) << (nbyte * 8));
  7001. next++;
  7002. }
  7003. *axis &= max - 1;
  7004. }
  7005. /*******************************************************************************
  7006. * FUNCTION: move_tracking_heatmap_data
  7007. *
  7008. * SUMMARY: Move the valid tracking heatmap data from the input buffer into the
  7009. * system information structure, xy_mode and xy_data.
  7010. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7011. * the tthe_tuner sysfs node under the label "THM"
  7012. *
  7013. * RETURN:
  7014. * 0 = success
  7015. * !0 = failure
  7016. *
  7017. * PARAMETERS:
  7018. * *cd - pointer to core data
  7019. * *si - pointer to the system information structure
  7020. ******************************************************************************/
  7021. static int move_tracking_heatmap_data(struct pt_core_data *cd,
  7022. struct pt_sysinfo *si)
  7023. {
  7024. #ifdef TTHE_TUNER_SUPPORT
  7025. int size = get_unaligned_le16(&cd->input_buf[0]);
  7026. if (size)
  7027. tthe_print(cd, cd->input_buf, size, "THM=");
  7028. #endif
  7029. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7030. return 0;
  7031. }
  7032. /*******************************************************************************
  7033. * FUNCTION: move_sensor_data
  7034. *
  7035. * SUMMARY: Move the valid sensor data from the input buffer into the system
  7036. * information structure, xy_mode and xy_data.
  7037. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7038. * the tthe_tuner sysfs node under the label "sensor_monitor"
  7039. *
  7040. * RETURN:
  7041. * 0 = success
  7042. * !0 = failure
  7043. *
  7044. * PARAMETERS:
  7045. * *cd - pointer to core data
  7046. * *si - pointer to the system information structure
  7047. ******************************************************************************/
  7048. static int move_sensor_data(struct pt_core_data *cd,
  7049. struct pt_sysinfo *si)
  7050. {
  7051. #ifdef TTHE_TUNER_SUPPORT
  7052. int size = get_unaligned_le16(&cd->input_buf[0]);
  7053. if (size)
  7054. tthe_print(cd, cd->input_buf, size, "sensor_monitor=");
  7055. #endif
  7056. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7057. return 0;
  7058. }
  7059. /*******************************************************************************
  7060. * FUNCTION: move_button_data
  7061. *
  7062. * SUMMARY: Move the valid button data from the input buffer into the system
  7063. * information structure, xy_mode and xy_data.
  7064. * - If TTHE_TUNER_SUPPORT is defined print the raw button data into
  7065. * the tthe_tuner sysfs node under the label "OpModeData"
  7066. *
  7067. * RETURN:
  7068. * 0 = success
  7069. * !0 = failure
  7070. *
  7071. * PARAMETERS:
  7072. * *cd - pointer to core data
  7073. * *si - pointer to the system information structure
  7074. ******************************************************************************/
  7075. static int move_button_data(struct pt_core_data *cd,
  7076. struct pt_sysinfo *si)
  7077. {
  7078. #ifdef TTHE_TUNER_SUPPORT
  7079. int size = get_unaligned_le16(&cd->input_buf[0]);
  7080. if (size)
  7081. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7082. #endif
  7083. memcpy(si->xy_mode, cd->input_buf, BTN_INPUT_HEADER_SIZE);
  7084. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode, BTN_INPUT_HEADER_SIZE,
  7085. "xy_mode");
  7086. memcpy(si->xy_data, &cd->input_buf[BTN_INPUT_HEADER_SIZE],
  7087. BTN_REPORT_SIZE);
  7088. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, BTN_REPORT_SIZE,
  7089. "xy_data");
  7090. return 0;
  7091. }
  7092. /*******************************************************************************
  7093. * FUNCTION: move_touch_data
  7094. *
  7095. * SUMMARY: Move the valid touch data from the input buffer into the system
  7096. * information structure, xy_mode and xy_data.
  7097. * - If TTHE_TUNER_SUPPORT is defined print the raw touch data into
  7098. * the tthe_tuner sysfs node under the label "OpModeData"
  7099. *
  7100. * RETURN:
  7101. * 0 = success
  7102. * !0 = failure
  7103. *
  7104. * PARAMETERS:
  7105. * *cd - pointer to core data
  7106. * *si - pointer to the system information structure
  7107. ******************************************************************************/
  7108. static int move_touch_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7109. {
  7110. int max_tch = si->sensing_conf_data.max_tch;
  7111. int num_cur_tch;
  7112. int length;
  7113. struct pt_tch_abs_params *tch = &si->tch_hdr[PT_TCH_NUM];
  7114. #ifdef TTHE_TUNER_SUPPORT
  7115. int size = get_unaligned_le16(&cd->input_buf[0]);
  7116. if (size)
  7117. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7118. #endif
  7119. memcpy(si->xy_mode, cd->input_buf, si->desc.tch_header_size);
  7120. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode,
  7121. si->desc.tch_header_size, "xy_mode");
  7122. pt_get_touch_axis(cd, &num_cur_tch, tch->size,
  7123. tch->max, si->xy_mode + 3 + tch->ofs, tch->bofs);
  7124. if (unlikely(num_cur_tch > max_tch))
  7125. num_cur_tch = max_tch;
  7126. length = num_cur_tch * si->desc.tch_record_size;
  7127. memcpy(si->xy_data, &cd->input_buf[si->desc.tch_header_size], length);
  7128. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, length, "xy_data");
  7129. return 0;
  7130. }
  7131. /*******************************************************************************
  7132. * FUNCTION: move_hid_pen_data
  7133. *
  7134. * SUMMARY: TODO Move the valid pen data from the input buffer into the system
  7135. * information structure, xy_mode and xy_data.
  7136. * - If TTHE_TUNER_SUPPORT is defined print the raw pen data into
  7137. * the tthe_tuner sysfs node under the label "HID" starting with the
  7138. * report ID.
  7139. *
  7140. * RETURN:
  7141. * 0 = success
  7142. * !0 = failure
  7143. *
  7144. * PARAMETERS:
  7145. * *cd - pointer to core data
  7146. * *si - pointer to the system information structure
  7147. ******************************************************************************/
  7148. static int move_hid_pen_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7149. {
  7150. #ifdef TTHE_TUNER_SUPPORT
  7151. int size = get_unaligned_le16(&cd->input_buf[0]);
  7152. if (size) {
  7153. /*
  7154. * HID over USB does not require the two byte length field, so
  7155. * this should print from input_buf[2] but to keep both finger
  7156. * and pen reports the same the length is included
  7157. */
  7158. if (cd->tthe_hid_usb_format == PT_FEATURE_ENABLE)
  7159. tthe_print(cd, &(cd->input_buf[2]), size - 2,
  7160. "HID-USB=");
  7161. else
  7162. tthe_print(cd, &(cd->input_buf[0]), size,
  7163. "HID-I2C=");
  7164. }
  7165. #endif
  7166. pt_pr_buf(cd->dev, DL_INFO, (u8 *)&(cd->input_buf[0]), size, "HID Pen");
  7167. return 0;
  7168. }
  7169. /*******************************************************************************
  7170. * FUNCTION: parse_touch_input
  7171. *
  7172. * SUMMARY: Parse the touch report and take action based on the touch
  7173. * report_id.
  7174. *
  7175. * RETURN:
  7176. * 0 = success
  7177. * !0 = failure
  7178. *
  7179. * PARAMETERS:
  7180. * *cd - pointer to core data
  7181. * size - size of touch record
  7182. ******************************************************************************/
  7183. static int parse_touch_input(struct pt_core_data *cd, int size)
  7184. {
  7185. struct pt_sysinfo *si = &cd->sysinfo;
  7186. int report_id = cd->input_buf[2];
  7187. int rc = -EINVAL;
  7188. pt_debug(cd->dev, DL_DEBUG, "%s: Received touch report\n",
  7189. __func__);
  7190. if (!si->ready) {
  7191. pt_debug(cd->dev, DL_ERROR,
  7192. "%s: Need system information to parse touches\n",
  7193. __func__);
  7194. return 0;
  7195. }
  7196. if (!si->xy_mode || !si->xy_data)
  7197. return rc;
  7198. if (report_id == PT_PIP_TOUCH_REPORT_ID)
  7199. rc = move_touch_data(cd, si);
  7200. else if (report_id == PT_HID_PEN_REPORT_ID)
  7201. rc = move_hid_pen_data(cd, si);
  7202. else if (report_id == PT_PIP_CAPSENSE_BTN_REPORT_ID)
  7203. rc = move_button_data(cd, si);
  7204. else if (report_id == PT_PIP_SENSOR_DATA_REPORT_ID)
  7205. rc = move_sensor_data(cd, si);
  7206. else if (report_id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7207. rc = move_tracking_heatmap_data(cd, si);
  7208. if (rc)
  7209. return rc;
  7210. /* attention IRQ */
  7211. call_atten_cb(cd, PT_ATTEN_IRQ, cd->mode);
  7212. return 0;
  7213. }
  7214. /*******************************************************************************
  7215. * FUNCTION: parse_command_input
  7216. *
  7217. * SUMMARY: Move the response data from the input buffer to the response buffer
  7218. *
  7219. * RETURN:
  7220. * 0 = success
  7221. * !0 = failure
  7222. *
  7223. * PARAMETERS:
  7224. * *cd - pointer to core data
  7225. * size - size of response data
  7226. ******************************************************************************/
  7227. static int parse_command_input(struct pt_core_data *cd, int size)
  7228. {
  7229. pt_debug(cd->dev, DL_DEBUG, "%s: Received cmd interrupt\n",
  7230. __func__);
  7231. memcpy(cd->response_buf, cd->input_buf, size);
  7232. #if defined(TTHE_TUNER_SUPPORT) && defined(TTDL_DIAGNOSTICS)
  7233. if (size && cd->show_tt_data) {
  7234. if (cd->pip2_prot_active)
  7235. tthe_print(cd, cd->input_buf, size, "TT_DATA_PIP2=");
  7236. else
  7237. tthe_print(cd, cd->input_buf, size, "TT_DATA=");
  7238. }
  7239. #endif
  7240. mutex_lock(&cd->system_lock);
  7241. cd->hid_cmd_state = 0;
  7242. mutex_unlock(&cd->system_lock);
  7243. wake_up(&cd->wait_q);
  7244. return 0;
  7245. }
  7246. /*******************************************************************************
  7247. * FUNCTION: pt_allow_enumeration
  7248. *
  7249. * SUMMARY: Determine if an enumeration or fully re-probe should perform when
  7250. * FW sentinel is seen.
  7251. *
  7252. * RETURN:
  7253. * true = allow enumeration or fully re-probe
  7254. * false = skip enumeration and fully re-probe
  7255. *
  7256. * PARAMETERS:
  7257. * *cd - pointer to core data
  7258. ******************************************************************************/
  7259. static inline bool pt_allow_enumeration(struct pt_core_data *cd)
  7260. {
  7261. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7262. (!cd->hid_reset_cmd_state) &&
  7263. (cd->core_probe_complete) &&
  7264. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7265. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7266. (cd->mode == PT_MODE_OPERATIONAL)) {
  7267. return true;
  7268. }
  7269. if ((!cd->hid_reset_cmd_state) &&
  7270. (cd->core_probe_complete) &&
  7271. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7272. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7273. (cd->active_dut_generation != DUT_PIP1_ONLY)) {
  7274. return true;
  7275. }
  7276. pt_debug(cd->dev, DL_INFO,
  7277. "%s: Dissallow - %s=%d %s=%d %s=0x%02X %s=%d\n",
  7278. __func__,
  7279. "hid_reset_cmd_state(0)", cd->hid_reset_cmd_state,
  7280. "core_probe_complete(1)", cd->core_probe_complete,
  7281. "hid_cmd_state(Not 0x02 or 0x39)", cd->hid_cmd_state,
  7282. "active_dut_gen(0,2)", cd->active_dut_generation);
  7283. return false;
  7284. }
  7285. /*******************************************************************************
  7286. * FUNCTION: pt_is_touch_report
  7287. *
  7288. * SUMMARY: Determine if a report ID should be treated as a touch report
  7289. *
  7290. * RETURN:
  7291. * true = report ID is a touch report
  7292. * false = report ID is not a touch report
  7293. *
  7294. * PARAMETERS:
  7295. * id - Report ID
  7296. ******************************************************************************/
  7297. static bool pt_is_touch_report(int id)
  7298. {
  7299. if (id == PT_PIP_TOUCH_REPORT_ID ||
  7300. id == PT_HID_PEN_REPORT_ID ||
  7301. id == PT_PIP_CAPSENSE_BTN_REPORT_ID ||
  7302. id == PT_PIP_SENSOR_DATA_REPORT_ID ||
  7303. id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7304. return true;
  7305. else
  7306. return false;
  7307. }
  7308. /*******************************************************************************
  7309. * FUNCTION: pt_parse_input
  7310. *
  7311. * SUMMARY: Parse the input data read from DUT due to IRQ. Handle data based
  7312. * on if its a response to a command or asynchronous touch data / reset
  7313. * sentinel. PIP2.x messages have additional error checking that is
  7314. * parsed (SEQ match from cmd to rsp, CRC valid).
  7315. * Look for special packets based on unique lengths:
  7316. * 0 bytes - APP(FW) reset sentinel or Gen5/6 BL sentinel
  7317. * 2 bytes - Empty buffer (PIP 1.7 and earlier)
  7318. * 11 bytes - possible PIP2.x reset sentinel (TAG and SEQ must = 0)
  7319. * 0xFFXX - Empty buffer (PIP 1.7+)
  7320. * Queue a startup after any asynchronous FW reset sentinel is seen, unless
  7321. * the initial probe has not yet been done.
  7322. *
  7323. * RETURN:
  7324. * 0 = success
  7325. * !0 = failure
  7326. *
  7327. * PARAMETERS:
  7328. * *cd - pointer to core data
  7329. ******************************************************************************/
  7330. static int pt_parse_input(struct pt_core_data *cd)
  7331. {
  7332. int report_id;
  7333. int cmd_id;
  7334. int is_command = 0;
  7335. int size;
  7336. int print_size;
  7337. bool touch_report = true;
  7338. unsigned short calc_crc;
  7339. unsigned short resp_crc;
  7340. cd->fw_sys_mode_in_standby_state = false;
  7341. size = get_unaligned_le16(&cd->input_buf[0]);
  7342. print_size = size;
  7343. pt_debug(cd->dev, DL_DEBUG, "<<< %s: IRQ Triggered, read len [%d]\n",
  7344. __func__, print_size);
  7345. if (print_size <= PT_MAX_INPUT)
  7346. pt_pr_buf(cd->dev, DL_DEBUG, cd->input_buf, print_size,
  7347. "<<< Read buf");
  7348. if (size == 0 ||
  7349. (size == 11 &&
  7350. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] &
  7351. PIP2_RESP_SEQUENCE_MASK) == 0 &&
  7352. (cd->input_buf[PIP2_RESP_REPORT_ID_OFFSET] &
  7353. PIP2_CMD_COMMAND_ID_MASK) ==
  7354. PIP2_CMD_ID_STATUS)) {
  7355. touch_report = false;
  7356. cd->hw_detected = true;
  7357. cd->bl_pip_ver_ready = false;
  7358. cd->app_pip_ver_ready = false;
  7359. if (size == 0) {
  7360. mutex_lock(&cd->system_lock);
  7361. cd->pip2_prot_active = false;
  7362. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  7363. /*
  7364. * For Gen5/6 this sentinel could be from
  7365. * the BL or FW. Attempt to set the correct
  7366. * mode based on the previous PIP command.
  7367. */
  7368. if (cd->hid_cmd_state ==
  7369. PIP1_BL_CMD_ID_LAUNCH_APP + 1) {
  7370. cd->mode = PT_MODE_OPERATIONAL;
  7371. cd->startup_status =
  7372. STARTUP_STATUS_FW_RESET_SENTINEL;
  7373. } else if (cd->hid_cmd_state ==
  7374. PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7375. cd->hid_reset_cmd_state) {
  7376. cd->mode = PT_MODE_BOOTLOADER;
  7377. cd->startup_status =
  7378. STARTUP_STATUS_BL_RESET_SENTINEL;
  7379. } else {
  7380. cd->mode = PT_MODE_UNKNOWN;
  7381. cd->startup_status =
  7382. STARTUP_STATUS_START;
  7383. }
  7384. cd->fw_system_mode = FW_SYS_MODE_UNDEFINED;
  7385. pt_debug(cd->dev, DL_INFO,
  7386. "%s: ATM Gen5/6 %s sentinel received\n",
  7387. __func__,
  7388. (cd->mode == PT_MODE_OPERATIONAL ?
  7389. "FW" :
  7390. (cd->mode == PT_MODE_BOOTLOADER ?
  7391. "BL" : "Unknown")));
  7392. } else {
  7393. cd->mode = PT_MODE_OPERATIONAL;
  7394. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  7395. cd->startup_status =
  7396. STARTUP_STATUS_FW_RESET_SENTINEL;
  7397. pt_debug(cd->dev, DL_INFO,
  7398. "%s: ATM PT/TT FW sentinel received\n",
  7399. __func__);
  7400. }
  7401. mutex_unlock(&cd->system_lock);
  7402. if (pt_allow_enumeration(cd)) {
  7403. if (cd->active_dut_generation == DUT_UNKNOWN) {
  7404. pt_debug(cd->dev, DL_INFO,
  7405. "%s: Queue Restart\n", __func__);
  7406. pt_queue_restart(cd);
  7407. } else {
  7408. pt_debug(cd->dev, DL_INFO,
  7409. "%s: Queue Enum\n", __func__);
  7410. pt_queue_enum(cd);
  7411. }
  7412. } else {
  7413. pt_debug(cd->dev, DL_INFO,
  7414. "%s: Sentinel - No Queued Action\n",
  7415. __func__);
  7416. }
  7417. } else {
  7418. /* Sentinel must be from TT/TC BL */
  7419. mutex_lock(&cd->system_lock);
  7420. cd->pip2_prot_active = true;
  7421. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  7422. cd->mode = PT_MODE_BOOTLOADER;
  7423. cd->sysinfo.ready = false;
  7424. mutex_unlock(&cd->system_lock);
  7425. pt_debug(cd->dev, DL_INFO,
  7426. "%s: BL Reset sentinel received\n", __func__);
  7427. if (cd->flashless_dut &&
  7428. cd->flashless_auto_bl == PT_ALLOW_AUTO_BL) {
  7429. pt_debug(cd->dev, DL_INFO,
  7430. "%s: BL to RAM for flashless DUT\n",
  7431. __func__);
  7432. kthread_run(start_fw_upgrade, cd, "pt_loader");
  7433. }
  7434. }
  7435. mutex_lock(&cd->system_lock);
  7436. memcpy(cd->response_buf, cd->input_buf, 2);
  7437. if (!cd->hid_reset_cmd_state && !cd->hid_cmd_state) {
  7438. mutex_unlock(&cd->system_lock);
  7439. pt_debug(cd->dev, DL_WARN,
  7440. "%s: Device Initiated Reset\n", __func__);
  7441. wake_up(&cd->wait_q);
  7442. return 0;
  7443. }
  7444. cd->hid_reset_cmd_state = 0;
  7445. if (cd->hid_cmd_state == PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7446. cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1 ||
  7447. cd->hid_cmd_state == PIP1_CMD_ID_USER_CMD + 1)
  7448. cd->hid_cmd_state = 0;
  7449. wake_up(&cd->wait_q);
  7450. mutex_unlock(&cd->system_lock);
  7451. return 0;
  7452. } else if (size == 2 || size >= PT_PIP_1P7_EMPTY_BUF) {
  7453. /*
  7454. * This debug message below is used by PBATS to calculate the
  7455. * time from the last lift off IRQ to when FW exits LFT mode.
  7456. */
  7457. touch_report = false;
  7458. pt_debug(cd->dev, DL_WARN,
  7459. "%s: DUT - Empty buffer detected\n", __func__);
  7460. return 0;
  7461. } else if (size > PT_MAX_INPUT) {
  7462. pt_debug(cd->dev, DL_ERROR,
  7463. "%s: DUT - Unexpected len field in active bus data!\n",
  7464. __func__);
  7465. return -EINVAL;
  7466. }
  7467. if (cd->pip2_prot_active) {
  7468. pt_debug(cd->dev, DL_DEBUG,
  7469. "%s: Decode PIP2.x Response\n", __func__);
  7470. /* PIP2 does not have a report id, hard code it */
  7471. report_id = 0x00;
  7472. cmd_id = cd->input_buf[PIP2_RESP_COMMAND_ID_OFFSET];
  7473. calc_crc = crc_ccitt_calculate(cd->input_buf, size - 2);
  7474. resp_crc = cd->input_buf[size - 2] << 8;
  7475. resp_crc |= cd->input_buf[size - 1];
  7476. if ((cd->pip2_cmd_tag_seq !=
  7477. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F)) &&
  7478. (resp_crc != calc_crc) &&
  7479. ((cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7480. == PT_PIP_TOUCH_REPORT_ID) ||
  7481. (cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7482. == PT_PIP_CAPSENSE_BTN_REPORT_ID))) {
  7483. pt_debug(cd->dev, DL_WARN,
  7484. "%s: %s %d %s\n",
  7485. __func__,
  7486. "Received PIP1 report id =",
  7487. cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET],
  7488. "when expecting a PIP2 report - IGNORE report");
  7489. return 0;
  7490. }
  7491. is_command = 1;
  7492. touch_report = false;
  7493. } else {
  7494. report_id = cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET];
  7495. cmd_id = cd->input_buf[PIP1_RESP_COMMAND_ID_OFFSET];
  7496. }
  7497. #ifdef TTDL_DIAGNOSTICS
  7498. pt_debug(cd->dev, DL_DEBUG,
  7499. "%s: pip2 = %d report_id: 0x%02X, cmd_code: 0x%02X\n",
  7500. __func__, cd->pip2_prot_active, report_id,
  7501. (cmd_id & PIP2_CMD_COMMAND_ID_MASK));
  7502. #endif /* TTDL_DIAGNOSTICS */
  7503. if (report_id == PT_PIP_WAKEUP_REPORT_ID) {
  7504. pt_wakeup_host(cd);
  7505. #ifdef TTHE_TUNER_SUPPORT
  7506. tthe_print(cd, cd->input_buf, size, "TT_WAKEUP=");
  7507. #endif
  7508. return 0;
  7509. }
  7510. mod_timer_pending(&cd->watchdog_timer, jiffies +
  7511. msecs_to_jiffies(cd->watchdog_interval));
  7512. /*
  7513. * If it is PIP2 response, the report_id has been set to 0,
  7514. * so it will not be parsed as a touch packet.
  7515. */
  7516. if (!pt_is_touch_report(report_id)) {
  7517. is_command = 1;
  7518. touch_report = false;
  7519. }
  7520. if (unlikely(is_command)) {
  7521. parse_command_input(cd, size);
  7522. return 0;
  7523. }
  7524. if (touch_report)
  7525. parse_touch_input(cd, size);
  7526. return 0;
  7527. }
  7528. /*******************************************************************************
  7529. * FUNCTION: pt_read_input
  7530. *
  7531. * SUMMARY: Reads incoming data off of the active bus
  7532. *
  7533. * RETURN:
  7534. * 0 = success
  7535. * !0 = failure
  7536. *
  7537. * PARAMETERS:
  7538. * *cd - pointer to core data
  7539. ******************************************************************************/
  7540. static int pt_read_input(struct pt_core_data *cd)
  7541. {
  7542. struct device *dev = cd->dev;
  7543. int rc = 0;
  7544. int t;
  7545. int retry = PT_BUS_READ_INPUT_RETRY_COUNT;
  7546. /*
  7547. * Workaround for easywake failure
  7548. * Interrupt for easywake, wait for bus controller to wake
  7549. */
  7550. mutex_lock(&cd->system_lock);
  7551. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  7552. if (cd->sleep_state == SS_SLEEP_ON) {
  7553. mutex_unlock(&cd->system_lock);
  7554. if (!dev->power.is_suspended)
  7555. goto read;
  7556. t = wait_event_timeout(cd->wait_q,
  7557. (cd->wait_until_wake == 1),
  7558. msecs_to_jiffies(2000));
  7559. #ifdef TTDL_DIAGNOSTICS
  7560. if (IS_TMO(t)) {
  7561. cd->bus_transmit_error_count++;
  7562. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  7563. pt_debug(dev, DL_ERROR,
  7564. "%s: !!!I2C Transmission Error %d\n",
  7565. __func__,
  7566. cd->bus_transmit_error_count);
  7567. }
  7568. #else
  7569. if (IS_TMO(t))
  7570. pt_queue_enum(cd);
  7571. #endif /* TTDL_DIAGNOSTICS */
  7572. goto read;
  7573. }
  7574. }
  7575. mutex_unlock(&cd->system_lock);
  7576. read:
  7577. /* Try reading up to 'retry' times */
  7578. while (retry-- != 0) {
  7579. rc = pt_adap_read_default_nosize(cd, cd->input_buf,
  7580. PT_MAX_INPUT);
  7581. if (!rc) {
  7582. pt_debug(dev, DL_DEBUG,
  7583. "%s: Read input successfully\n", __func__);
  7584. goto read_exit;
  7585. }
  7586. usleep_range(1000, 2000);
  7587. }
  7588. pt_debug(dev, DL_ERROR,
  7589. "%s: Error getting report, rc=%d\n", __func__, rc);
  7590. read_exit:
  7591. return rc;
  7592. }
  7593. /*******************************************************************************
  7594. * FUNCTION: pt_irq
  7595. *
  7596. * SUMMARY: Process all detected interrupts
  7597. *
  7598. * RETURN:
  7599. * IRQ_HANDLED - Finished processing the interrupt
  7600. *
  7601. * PARAMETERS:
  7602. * irq - IRQ number
  7603. * *handle - pointer to core data struct
  7604. ******************************************************************************/
  7605. irqreturn_t pt_irq(int irq, void *handle)
  7606. {
  7607. struct pt_core_data *cd = handle;
  7608. int rc = 0;
  7609. if (!pt_check_irq_asserted(cd))
  7610. return IRQ_HANDLED;
  7611. rc = pt_read_input(cd);
  7612. #ifdef TTDL_DIAGNOSTICS
  7613. cd->irq_count++;
  7614. /* Used to calculate T-Refresh */
  7615. if (cd->t_refresh_active) {
  7616. if (cd->t_refresh_count == 0) {
  7617. cd->t_refresh_time = jiffies;
  7618. cd->t_refresh_count++;
  7619. } else if (cd->t_refresh_count < cd->t_refresh_total) {
  7620. cd->t_refresh_count++;
  7621. } else {
  7622. cd->t_refresh_active = 0;
  7623. cd->t_refresh_time =
  7624. jiffies_to_msecs(jiffies - cd->t_refresh_time);
  7625. }
  7626. }
  7627. #endif /* TTDL_DIAGNOSTICS */
  7628. if (!rc)
  7629. pt_parse_input(cd);
  7630. return IRQ_HANDLED;
  7631. }
  7632. /*******************************************************************************
  7633. * FUNCTION: _pt_subscribe_attention
  7634. *
  7635. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7636. * to subscribe themselves into the TTDL attention list
  7637. *
  7638. * RETURN:
  7639. * 0 = success
  7640. * !0 = failure
  7641. *
  7642. * PARAMETERS:
  7643. * *dev - pointer to device structure
  7644. * type - attention type enum
  7645. * *id - ID of the module calling this function
  7646. * *func - callback function pointer to be called when notified
  7647. * mode - attention mode
  7648. ******************************************************************************/
  7649. int _pt_subscribe_attention(struct device *dev,
  7650. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7651. int mode)
  7652. {
  7653. struct pt_core_data *cd = dev_get_drvdata(dev);
  7654. struct atten_node *atten, *atten_new;
  7655. atten_new = kzalloc(sizeof(*atten_new), GFP_KERNEL);
  7656. if (!atten_new)
  7657. return -ENOMEM;
  7658. pt_debug(cd->dev, DL_INFO, "%s from '%s'\n", __func__,
  7659. dev_name(cd->dev));
  7660. spin_lock(&cd->spinlock);
  7661. list_for_each_entry(atten, &cd->atten_list[type], node) {
  7662. if (atten->id == id && atten->mode == mode) {
  7663. spin_unlock(&cd->spinlock);
  7664. kfree(atten_new);
  7665. pt_debug(cd->dev, DL_INFO, "%s: %s=%p %s=%d\n",
  7666. __func__,
  7667. "already subscribed attention",
  7668. dev, "mode", mode);
  7669. return 0;
  7670. }
  7671. }
  7672. atten_new->id = id;
  7673. atten_new->dev = dev;
  7674. atten_new->mode = mode;
  7675. atten_new->func = func;
  7676. list_add(&atten_new->node, &cd->atten_list[type]);
  7677. spin_unlock(&cd->spinlock);
  7678. return 0;
  7679. }
  7680. /*******************************************************************************
  7681. * FUNCTION: _pt_unsubscribe_attention
  7682. *
  7683. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7684. * to unsubscribe themselves from the TTDL attention list
  7685. *
  7686. * RETURN:
  7687. * 0 = success
  7688. * !0 = failure
  7689. *
  7690. * PARAMETERS:
  7691. * *dev - pointer to device structure
  7692. * type - attention type enum
  7693. * *id - ID of the module calling this function
  7694. * *func - function pointer
  7695. * mode - attention mode
  7696. ******************************************************************************/
  7697. int _pt_unsubscribe_attention(struct device *dev,
  7698. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7699. int mode)
  7700. {
  7701. struct pt_core_data *cd = dev_get_drvdata(dev);
  7702. struct atten_node *atten, *atten_n;
  7703. spin_lock(&cd->spinlock);
  7704. list_for_each_entry_safe(atten, atten_n, &cd->atten_list[type], node) {
  7705. if (atten->id == id && atten->mode == mode) {
  7706. list_del(&atten->node);
  7707. spin_unlock(&cd->spinlock);
  7708. pt_debug(cd->dev, DL_DEBUG, "%s: %s=%p %s=%d\n",
  7709. __func__,
  7710. "unsub for atten->dev", atten->dev,
  7711. "atten->mode", atten->mode);
  7712. kfree(atten);
  7713. return 0;
  7714. }
  7715. }
  7716. spin_unlock(&cd->spinlock);
  7717. return -ENODEV;
  7718. }
  7719. /*******************************************************************************
  7720. * FUNCTION: _pt_request_exclusive
  7721. *
  7722. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7723. * to request exclusive access
  7724. *
  7725. * RETURN:
  7726. * 0 = success
  7727. * !0 = failure
  7728. *
  7729. * PARAMETERS:
  7730. * *dev - pointer to device structure
  7731. * timeout_ms - timeout to wait for exclusive access
  7732. ******************************************************************************/
  7733. static int _pt_request_exclusive(struct device *dev,
  7734. int timeout_ms)
  7735. {
  7736. struct pt_core_data *cd = dev_get_drvdata(dev);
  7737. return request_exclusive(cd, (void *)dev, timeout_ms);
  7738. }
  7739. /*******************************************************************************
  7740. * FUNCTION: _pt_release_exclusive
  7741. *
  7742. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7743. * to release exclusive access
  7744. *
  7745. * RETURN:
  7746. * 0 = success
  7747. * !0 = failure
  7748. *
  7749. * PARAMETERS:
  7750. * *dev - pointer to device structure
  7751. ******************************************************************************/
  7752. static int _pt_release_exclusive(struct device *dev)
  7753. {
  7754. struct pt_core_data *cd = dev_get_drvdata(dev);
  7755. return release_exclusive(cd, (void *)dev);
  7756. }
  7757. /*******************************************************************************
  7758. * FUNCTION: _pt_request_reset
  7759. *
  7760. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7761. * to request the DUT to be reset. Function returns err if refused or
  7762. * timeout occurs (Note: core uses fixed timeout period).
  7763. *
  7764. * NOTE: Function blocks until ISR occurs.
  7765. *
  7766. * RETURN:
  7767. * 0 = success
  7768. * !0 = failure
  7769. *
  7770. * PARAMETERS:
  7771. * *dev - pointer to device structure
  7772. * protect - flag to call protected or non-protected
  7773. ******************************************************************************/
  7774. static int _pt_request_reset(struct device *dev, int protect)
  7775. {
  7776. struct pt_core_data *cd = dev_get_drvdata(dev);
  7777. int rc;
  7778. rc = pt_dut_reset(cd, protect);
  7779. if (rc < 0) {
  7780. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  7781. __func__, rc);
  7782. }
  7783. return rc;
  7784. }
  7785. /*******************************************************************************
  7786. * FUNCTION: _pt_request_enum
  7787. *
  7788. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7789. * to request TTDL to queue an enum. This function will return err
  7790. * if refused; if no error then enum has completed and system is in
  7791. * normal operation mode.
  7792. *
  7793. * RETURN:
  7794. * 0 = success
  7795. * !0 = failure
  7796. *
  7797. * PARAMETERS:
  7798. * *dev - pointer to device structure
  7799. * wait - boolean to determine if to wait for startup event
  7800. ******************************************************************************/
  7801. static int _pt_request_enum(struct device *dev, bool wait)
  7802. {
  7803. struct pt_core_data *cd = dev_get_drvdata(dev);
  7804. pt_queue_enum(cd);
  7805. if (wait)
  7806. wait_event_timeout(cd->wait_q,
  7807. cd->startup_state == STARTUP_NONE,
  7808. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  7809. return 0;
  7810. }
  7811. /*******************************************************************************
  7812. * FUNCTION: _pt_request_sysinfo
  7813. *
  7814. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7815. * to request the pointer to the system information structure. This
  7816. * function will return NULL if sysinfo has not been acquired from the
  7817. * DUT yet.
  7818. *
  7819. * RETURN: Pointer to the system information struct
  7820. *
  7821. * PARAMETERS:
  7822. * *dev - pointer to device structure
  7823. ******************************************************************************/
  7824. struct pt_sysinfo *_pt_request_sysinfo(struct device *dev)
  7825. {
  7826. struct pt_core_data *cd = dev_get_drvdata(dev);
  7827. int rc = 0;
  7828. /* Attempt to get sysinfo if not ready and panel_id is from XY pin */
  7829. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7830. !cd->sysinfo.ready) {
  7831. rc = pt_hid_output_get_sysinfo_(cd);
  7832. if (rc) {
  7833. pt_debug(cd->dev, DL_ERROR,
  7834. "%s: Error getting sysinfo rc=%d\n",
  7835. __func__, rc);
  7836. }
  7837. }
  7838. if (cd->sysinfo.ready)
  7839. return &cd->sysinfo;
  7840. return NULL;
  7841. }
  7842. /*******************************************************************************
  7843. * FUNCTION: _pt_request_loader_pdata
  7844. *
  7845. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7846. * to request the pointer to the loader platform data
  7847. *
  7848. * RETURN: Pointer to the loader platform data struct
  7849. *
  7850. * PARAMETERS:
  7851. * *dev - pointer to device structure
  7852. ******************************************************************************/
  7853. static struct pt_loader_platform_data *_pt_request_loader_pdata(
  7854. struct device *dev)
  7855. {
  7856. struct pt_core_data *cd = dev_get_drvdata(dev);
  7857. return cd->pdata->loader_pdata;
  7858. }
  7859. /*******************************************************************************
  7860. * FUNCTION: _pt_request_start_wd
  7861. *
  7862. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7863. * to request to start the TTDL watchdog
  7864. *
  7865. * RETURN:
  7866. * 0 = success
  7867. * !0 = failure
  7868. *
  7869. * PARAMETERS:
  7870. * *dev - pointer to device structure
  7871. ******************************************************************************/
  7872. static int _pt_request_start_wd(struct device *dev)
  7873. {
  7874. struct pt_core_data *cd = dev_get_drvdata(dev);
  7875. pt_start_wd_timer(cd);
  7876. return 0;
  7877. }
  7878. /*******************************************************************************
  7879. * FUNCTION: _pt_request_stop_wd
  7880. *
  7881. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7882. * to request to stop the TTDL watchdog
  7883. *
  7884. * RETURN:
  7885. * 0 = success
  7886. * !0 = failure
  7887. *
  7888. * PARAMETERS:
  7889. * *dev - pointer to device structure
  7890. ******************************************************************************/
  7891. static int _pt_request_stop_wd(struct device *dev)
  7892. {
  7893. struct pt_core_data *cd = dev_get_drvdata(dev);
  7894. pt_stop_wd_timer(cd);
  7895. return 0;
  7896. }
  7897. /*******************************************************************************
  7898. * FUNCTION: pt_pip2_launch_app
  7899. *
  7900. * SUMMARY: Sends the PIP2 EXECUTE command to launch the APP and then wait for
  7901. * the FW reset sentinel to indicate the function succeeded.
  7902. *
  7903. * NOTE: Calling this function when the DUT is in Application mode WILL result
  7904. * in a timeout delay and with the DUT being reset with an XRES.
  7905. *
  7906. * RETURN:
  7907. * 0 = success
  7908. * !0 = failure
  7909. *
  7910. * PARAMETERS:
  7911. * *cd - pointer to core data structure
  7912. * protect - flag to call protected or non-protected
  7913. ******************************************************************************/
  7914. static int pt_pip2_launch_app(struct device *dev, int protect)
  7915. {
  7916. struct pt_core_data *cd = dev_get_drvdata(dev);
  7917. u16 actual_read_len;
  7918. u16 tmp_startup_status = cd->startup_status;
  7919. u8 read_buf[12];
  7920. u8 status;
  7921. int time = 0;
  7922. int rc = 0;
  7923. mutex_lock(&cd->system_lock);
  7924. cd->startup_status = STARTUP_STATUS_START;
  7925. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  7926. mutex_unlock(&cd->system_lock);
  7927. rc = _pt_request_pip2_send_cmd(dev, protect,
  7928. PIP2_CMD_ID_EXECUTE, NULL, 0, read_buf,
  7929. &actual_read_len);
  7930. if (rc) {
  7931. pt_debug(dev, DL_ERROR,
  7932. "%s: PIP2 EXECUTE cmd failed rc = %d\n",
  7933. __func__, rc);
  7934. } else {
  7935. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  7936. /* Test for no or invalid image in FLASH, no point to reset */
  7937. if (status == PIP2_RSP_ERR_INVALID_IMAGE) {
  7938. rc = status;
  7939. goto exit;
  7940. }
  7941. /* Any other boot failure */
  7942. if (status != 0) {
  7943. pt_debug(dev, DL_ERROR,
  7944. "%s: FW did not EXECUTE, status = %d\n",
  7945. __func__, status);
  7946. rc = status;
  7947. }
  7948. }
  7949. if (rc) {
  7950. pt_debug(dev, DL_ERROR,
  7951. "%s: Failed to launch APP, XRES DUT rc = %d\n",
  7952. __func__, rc);
  7953. goto exit;
  7954. }
  7955. while ((cd->startup_status == STARTUP_STATUS_START) && time < 240) {
  7956. msleep(20);
  7957. pt_debug(cd->dev, DL_INFO, "%s: wait for %d for enum=0x%04X\n",
  7958. __func__, time, cd->startup_status);
  7959. time += 20;
  7960. }
  7961. if (cd->startup_status == STARTUP_STATUS_START) {
  7962. pt_debug(cd->dev, DL_WARN,
  7963. "%s: TMO waiting for FW reset sentinel\n", __func__);
  7964. rc = -ETIME;
  7965. }
  7966. exit:
  7967. if (cd->startup_status == STARTUP_STATUS_START) {
  7968. /* Reset to original state because we could be stuck in BL */
  7969. mutex_lock(&cd->system_lock);
  7970. cd->startup_status = tmp_startup_status;
  7971. mutex_unlock(&cd->system_lock);
  7972. }
  7973. return rc;
  7974. }
  7975. /*******************************************************************************
  7976. * FUNCTION: _pt_request_pip2_launch_app
  7977. *
  7978. * SUMMARY: Calls pt_pip2_launch_app() when configured to. A small delay is
  7979. * inserted to ensure the reset has allowed the BL reset sentinel to be
  7980. * consumed.
  7981. *
  7982. * RETURN:
  7983. * 0 = success
  7984. * !0 = failure
  7985. *
  7986. * PARAMETERS:
  7987. * *cd - pointer to core data structure
  7988. ******************************************************************************/
  7989. static int _pt_request_pip2_launch_app(struct device *dev, int protect)
  7990. {
  7991. return pt_pip2_launch_app(dev, protect);
  7992. }
  7993. /*******************************************************************************
  7994. * FUNCTION: _pt_request_wait_for_enum_state
  7995. *
  7996. * SUMMARY: Loops for up to timeout waiting for the startup_status to reach
  7997. * the state passed in or STARTUP_STATUS_COMPLETE whichever comes first
  7998. *
  7999. * RETURN:
  8000. * 0 = success
  8001. * !0 = failure
  8002. *
  8003. * PARAMETERS:
  8004. * *cd - pointer to core data structure
  8005. * timeout - timeout for how long to wait
  8006. * state - enum state to wait for
  8007. ******************************************************************************/
  8008. static int _pt_request_wait_for_enum_state(struct device *dev, int timeout,
  8009. int state)
  8010. {
  8011. int rc = 0;
  8012. int t;
  8013. struct pt_core_data *cd = dev_get_drvdata(dev);
  8014. t = wait_event_timeout(cd->wait_q,
  8015. (cd->startup_status & state) || (cd->startup_status & 0x0100),
  8016. msecs_to_jiffies(timeout));
  8017. if (IS_TMO(t)) {
  8018. pt_debug(cd->dev, DL_ERROR,
  8019. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8020. __func__, state, timeout);
  8021. pt_debug(cd->dev, DL_WARN,
  8022. "%s: enum state reached 0x%04X\n",
  8023. __func__, cd->startup_status);
  8024. rc = -ETIME;
  8025. } else if (cd->startup_status & state) {
  8026. pt_debug(cd->dev, DL_INFO,
  8027. "%s: Enum state reached: enum=0x%04X in %dms\n",
  8028. __func__, cd->startup_status,
  8029. (t == 1) ? timeout : (timeout - jiffies_to_msecs(t)));
  8030. } else {
  8031. if (t == 1) {
  8032. pt_debug(
  8033. cd->dev, DL_ERROR,
  8034. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8035. __func__, state, timeout);
  8036. rc = -ETIME;
  8037. } else {
  8038. pt_debug(
  8039. cd->dev, DL_ERROR,
  8040. "%s: Enum state 0x%04X not reached in %dms\n",
  8041. __func__, state, timeout - jiffies_to_msecs(t));
  8042. rc = -EINVAL;
  8043. }
  8044. pt_debug(cd->dev, DL_INFO, "%s: enum state reached 0x%04X\n",
  8045. __func__, cd->startup_status);
  8046. }
  8047. return rc;
  8048. }
  8049. /*******************************************************************************
  8050. * FUNCTION: pt_core_wake_device_from_deep_sleep_
  8051. *
  8052. * SUMMARY: Call the set_power function and set the DUT to wake up from
  8053. * deep sleep.
  8054. *
  8055. * RETURN:
  8056. * 0 = success
  8057. * !0 = error
  8058. *
  8059. * PARAMETERS:
  8060. * *cd - pointer to core data
  8061. ******************************************************************************/
  8062. static int pt_core_wake_device_from_deep_sleep_(
  8063. struct pt_core_data *cd)
  8064. {
  8065. int rc;
  8066. rc = pt_hid_cmd_set_power_(cd, HID_POWER_ON);
  8067. if (rc)
  8068. rc = -EAGAIN;
  8069. /* Prevent failure on sequential wake/sleep requests from OS */
  8070. msleep(20);
  8071. return rc;
  8072. }
  8073. /*******************************************************************************
  8074. * FUNCTION: pt_core_wake_device_from_easy_wake_
  8075. *
  8076. * SUMMARY: Wake up device from Easy-Wake state.
  8077. *
  8078. * RETURN:
  8079. * 0 = success
  8080. * !0 = failure
  8081. *
  8082. * PARAMETERS:
  8083. * *cd - pointer to core data
  8084. ******************************************************************************/
  8085. static int pt_core_wake_device_from_easy_wake_(struct pt_core_data *cd)
  8086. {
  8087. mutex_lock(&cd->system_lock);
  8088. cd->wait_until_wake = 1;
  8089. mutex_unlock(&cd->system_lock);
  8090. wake_up(&cd->wait_q);
  8091. msleep(20);
  8092. return pt_core_wake_device_from_deep_sleep_(cd);
  8093. }
  8094. /*******************************************************************************
  8095. * FUNCTION: pt_restore_parameters_
  8096. *
  8097. * SUMMARY: This function sends all RAM parameters stored in the linked list
  8098. * back to the DUT
  8099. *
  8100. * RETURN:
  8101. * 0 = success
  8102. * !0 = failure
  8103. *
  8104. * PARAMETERS:
  8105. * *cd - pointer the core data structure
  8106. ******************************************************************************/
  8107. static int pt_restore_parameters_(struct pt_core_data *cd)
  8108. {
  8109. struct param_node *param;
  8110. int rc = 0;
  8111. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  8112. goto exit;
  8113. spin_lock(&cd->spinlock);
  8114. list_for_each_entry(param, &cd->param_list, node) {
  8115. spin_unlock(&cd->spinlock);
  8116. pt_debug(cd->dev, DL_INFO, "%s: Parameter id:%d value:%d\n",
  8117. __func__, param->id, param->value);
  8118. rc = pt_pip_set_param_(cd, param->id,
  8119. param->value, param->size);
  8120. if (rc)
  8121. goto exit;
  8122. spin_lock(&cd->spinlock);
  8123. }
  8124. spin_unlock(&cd->spinlock);
  8125. exit:
  8126. return rc;
  8127. }
  8128. /*******************************************************************************
  8129. * FUNCTION: pt_pip2_exit_bl_
  8130. *
  8131. * SUMMARY: Attempt to exit the BL and run the application, taking into account
  8132. * a DUT that may not have flash and will need FW to be loaded into RAM
  8133. *
  8134. * RETURN:
  8135. * 0 = success
  8136. * !0 = failure
  8137. *
  8138. * PARAMETERS:
  8139. * *cd - pointer the core data structure
  8140. * *status_str - pointer to optional status string buffer
  8141. * buf_size - size of status_str buffer
  8142. ******************************************************************************/
  8143. int pt_pip2_exit_bl_(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8144. {
  8145. int rc;
  8146. int wait_time = 0;
  8147. u8 mode = PT_MODE_UNKNOWN;
  8148. bool load_status_str = false;
  8149. /*
  8150. * Below function has protective call to ensure no enum is still on
  8151. * going, while this kind of protection should be applied widely in
  8152. * future (TODO).
  8153. */
  8154. rc = pt_pip2_get_mode_sysmode(cd, &mode, NULL);
  8155. if (status_str && buf_size <= 50)
  8156. load_status_str = true;
  8157. if (mode == PT_MODE_BOOTLOADER) {
  8158. if (cd->flashless_dut == 1) {
  8159. rc = pt_hw_hard_reset(cd);
  8160. } else {
  8161. rc = pt_pip2_launch_app(cd->dev,
  8162. PT_CORE_CMD_UNPROTECTED);
  8163. if (rc == PIP2_RSP_ERR_INVALID_IMAGE) {
  8164. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8165. __func__, "Invalid image in FLASH rc", rc);
  8166. } else if (rc) {
  8167. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8168. __func__, "Failed to launch app rc", rc);
  8169. }
  8170. }
  8171. if (!rc) {
  8172. if (cd->flashless_dut == 1) {
  8173. /* Wait for BL to complete before enum */
  8174. rc = _pt_request_wait_for_enum_state(cd->dev,
  8175. 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  8176. if (rc && load_status_str) {
  8177. strlcpy(status_str, "No FW sentinel after BL",
  8178. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8179. goto exit;
  8180. }
  8181. }
  8182. /*
  8183. * If the host wants to interact with the FW or do a
  8184. * forced calibration, the FW must be out of BOOT mode
  8185. * and the system information must have been retrieved.
  8186. * Reaching the FW_OUT_OF_BOOT state guarantees both.
  8187. * If, however, the enumeration does not reach this
  8188. * point, the DUT may still be in APP mode so test
  8189. * for all conditions.
  8190. */
  8191. rc = _pt_request_wait_for_enum_state(cd->dev, 4500,
  8192. STARTUP_STATUS_FW_OUT_OF_BOOT);
  8193. if (!rc || cd->startup_status >=
  8194. STARTUP_STATUS_FW_RESET_SENTINEL) {
  8195. mutex_lock(&cd->system_lock);
  8196. cd->mode = PT_MODE_OPERATIONAL;
  8197. mutex_unlock(&cd->system_lock);
  8198. }
  8199. if (rc) {
  8200. pt_debug(cd->dev, DL_WARN, "%s: %s: 0x%04X\n",
  8201. __func__, "Failed to enum with DUT",
  8202. cd->startup_status);
  8203. if (load_status_str && !(cd->startup_status &
  8204. STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  8205. strlcpy(status_str, "FW Stuck in Boot mode",
  8206. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8207. goto exit;
  8208. }
  8209. }
  8210. /*
  8211. * The coming FW sentinel could wake up the event
  8212. * queue, which has chance to be taken by next command
  8213. * wrongly. Following delay is a workaround to avoid
  8214. * this issue for most situations.
  8215. */
  8216. msleep(20);
  8217. pt_start_wd_timer(cd);
  8218. }
  8219. if (load_status_str) {
  8220. if (rc == PIP2_RSP_ERR_INVALID_IMAGE)
  8221. strlcpy(status_str, "Failed - Invalid image in FLASH",
  8222. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8223. else if (!rc)
  8224. strlcpy(status_str, "Entered APP from BL mode",
  8225. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8226. else
  8227. strlcpy(status_str, "Failed to enter APP from BL mode",
  8228. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8229. }
  8230. } else if (mode == PT_MODE_OPERATIONAL) {
  8231. mutex_lock(&cd->system_lock);
  8232. cd->mode = mode;
  8233. mutex_unlock(&cd->system_lock);
  8234. rc = pt_poll_for_fw_exit_boot_mode(cd, 1500, &wait_time);
  8235. if (load_status_str) {
  8236. if (!rc)
  8237. strlcpy(status_str, "Already in APP mode",
  8238. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8239. else
  8240. strlcpy(status_str, "Already in APP mode - FW stuck in Boot mode",
  8241. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8242. }
  8243. } else if (rc || mode == PT_MODE_UNKNOWN) {
  8244. mutex_lock(&cd->system_lock);
  8245. cd->mode = mode;
  8246. mutex_unlock(&cd->system_lock);
  8247. if (load_status_str)
  8248. strlcpy(status_str, "Failed to determine active mode",
  8249. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8250. }
  8251. exit:
  8252. if (!rc)
  8253. pt_start_wd_timer(cd);
  8254. return rc;
  8255. }
  8256. /*******************************************************************************
  8257. * FUNCTION: pt_pip2_exit_bl
  8258. *
  8259. * SUMMARY: Wrapper function for _pt_pip2_exit_bl that guarantees exclusive
  8260. * access.
  8261. *
  8262. * RETURN:
  8263. * 0 = success
  8264. * !0 = failure
  8265. *
  8266. * PARAMETERS:
  8267. * *cd - pointer the core data structure
  8268. * *status_str - pointer to optional status string buffer
  8269. * buf_size - size of status_str buffer
  8270. ******************************************************************************/
  8271. int pt_pip2_exit_bl(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8272. {
  8273. int rc;
  8274. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8275. if (rc < 0) {
  8276. pt_debug(cd->dev, DL_ERROR,
  8277. "%s: fail get exclusive ex=%p own=%p\n", __func__,
  8278. cd->exclusive_dev, cd->dev);
  8279. return rc;
  8280. }
  8281. rc = pt_pip2_exit_bl_(cd, status_str, buf_size);
  8282. if (release_exclusive(cd, cd->dev) < 0)
  8283. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8284. __func__);
  8285. return rc;
  8286. }
  8287. /*******************************************************************************
  8288. * FUNCTION: _fast_startup
  8289. *
  8290. * SUMMARY: Perform fast startup after resume device by power on/off stratergy.
  8291. *
  8292. * RETURN:
  8293. * 0 = success
  8294. * !0 = failure
  8295. *
  8296. * PARAMETERS:
  8297. * *cd - pointer the core data structure
  8298. ******************************************************************************/
  8299. static int _fast_startup(struct pt_core_data *cd)
  8300. {
  8301. int retry = PT_CORE_STARTUP_RETRY_COUNT;
  8302. int rc = 0;
  8303. u8 mode = PT_MODE_UNKNOWN;
  8304. struct pt_hid_desc hid_desc;
  8305. int wait_time = 0;
  8306. memset(&hid_desc, 0, sizeof(hid_desc));
  8307. reset:
  8308. if (retry != PT_CORE_STARTUP_RETRY_COUNT)
  8309. pt_debug(cd->dev, DL_INFO, "%s: Retry %d\n", __func__,
  8310. PT_CORE_STARTUP_RETRY_COUNT - retry);
  8311. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8312. pt_debug(cd->dev, DL_INFO, "%s: PIP1 Enumeration start\n",
  8313. __func__);
  8314. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8315. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8316. if (rc < 0) {
  8317. pt_debug(cd->dev, DL_ERROR,
  8318. "%s: Error on getting HID descriptor r=%d\n",
  8319. __func__, rc);
  8320. if (retry--)
  8321. goto reset;
  8322. goto exit;
  8323. }
  8324. cd->mode = pt_get_mode(cd, &hid_desc);
  8325. if (cd->mode == PT_MODE_BOOTLOADER) {
  8326. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8327. __func__);
  8328. rc = pt_hid_output_bl_launch_app_(cd);
  8329. if (rc < 0) {
  8330. pt_debug(cd->dev, DL_ERROR,
  8331. "%s: Error on launch app r=%d\n",
  8332. __func__, rc);
  8333. if (retry--)
  8334. goto reset;
  8335. goto exit;
  8336. }
  8337. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8338. if (rc < 0) {
  8339. pt_debug(cd->dev, DL_ERROR,
  8340. "%s: Error on getting HID descriptor r=%d\n",
  8341. __func__, rc);
  8342. if (retry--)
  8343. goto reset;
  8344. goto exit;
  8345. }
  8346. cd->mode = pt_get_mode(cd, &hid_desc);
  8347. if (cd->mode == PT_MODE_BOOTLOADER) {
  8348. if (retry--)
  8349. goto reset;
  8350. goto exit;
  8351. }
  8352. }
  8353. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8354. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8355. } else {
  8356. pt_debug(cd->dev, DL_INFO, "%s: PIP2 Enumeration start\n",
  8357. __func__);
  8358. if (retry == PT_CORE_STARTUP_RETRY_COUNT) {
  8359. /* Wait for any sentinel before first try */
  8360. rc = _pt_request_wait_for_enum_state(
  8361. cd->dev, 150,
  8362. STARTUP_STATUS_BL_RESET_SENTINEL |
  8363. STARTUP_STATUS_FW_RESET_SENTINEL);
  8364. if (rc)
  8365. pt_debug(cd->dev, DL_ERROR,
  8366. "%s: No Sentinel detected rc = %d\n",
  8367. __func__, rc);
  8368. } else
  8369. pt_flush_bus_if_irq_asserted(cd,
  8370. PT_FLUSH_BUS_BASED_ON_LEN);
  8371. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8372. if (rc) {
  8373. pt_debug(cd->dev, DL_ERROR,
  8374. "%s: Get mode failed, mode unknown\n",
  8375. __func__);
  8376. mode = PT_MODE_UNKNOWN;
  8377. }
  8378. cd->mode = mode;
  8379. if (cd->mode == PT_MODE_BOOTLOADER) {
  8380. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8381. __func__);
  8382. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  8383. if (rc) {
  8384. pt_debug(cd->dev, DL_ERROR,
  8385. "%s Failed to exit bootloader\n",
  8386. __func__);
  8387. msleep(50);
  8388. rc = -ENODEV;
  8389. if (retry--)
  8390. goto reset;
  8391. goto exit;
  8392. } else {
  8393. pt_debug(cd->dev, DL_INFO,
  8394. "%s: Exit bootloader successfully\n",
  8395. __func__);
  8396. }
  8397. if (cd->mode != PT_MODE_OPERATIONAL) {
  8398. pt_debug(cd->dev, DL_WARN,
  8399. "%s: restore mode failure mode = %d\n",
  8400. __func__, cd->mode);
  8401. if (retry--)
  8402. goto reset;
  8403. goto exit;
  8404. }
  8405. }
  8406. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8407. }
  8408. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  8409. rc = _pt_poll_for_fw_exit_boot_mode(cd, 500, &wait_time);
  8410. if (!rc) {
  8411. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8412. pt_debug(cd->dev, DL_WARN,
  8413. "%s: Exit FW BOOT Mode after %dms\n",
  8414. __func__, wait_time);
  8415. } else {
  8416. pt_debug(cd->dev, DL_WARN,
  8417. "%s: FW stuck in BOOT Mode after %dms\n",
  8418. __func__, wait_time);
  8419. goto exit;
  8420. }
  8421. if (!cd->sysinfo.ready) {
  8422. rc = pt_hid_output_get_sysinfo_(cd);
  8423. if (rc) {
  8424. pt_debug(cd->dev, DL_ERROR,
  8425. "%s: Error on getting sysinfo r=%d\n",
  8426. __func__, rc);
  8427. if (retry--)
  8428. goto reset;
  8429. goto exit;
  8430. }
  8431. }
  8432. cd->startup_status |= STARTUP_STATUS_GET_SYS_INFO;
  8433. rc = pt_restore_parameters_(cd);
  8434. if (rc)
  8435. pt_debug(cd->dev, DL_ERROR,
  8436. "%s: failed to restore parameters rc=%d\n",
  8437. __func__, rc);
  8438. else
  8439. cd->startup_status |= STARTUP_STATUS_RESTORE_PARM;
  8440. exit:
  8441. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  8442. return rc;
  8443. }
  8444. /*******************************************************************************
  8445. * FUNCTION: pt_core_poweron_device_
  8446. *
  8447. * SUMMARY: Power on device, enable IRQ, and then perform a fast startup.
  8448. *
  8449. * RETURN:
  8450. * 0 = success
  8451. * !0 = failure
  8452. *
  8453. * PARAMETERS:
  8454. * *cd - pointer to core data
  8455. ******************************************************************************/
  8456. static int pt_core_poweron_device_(struct pt_core_data *cd)
  8457. {
  8458. struct device *dev = cd->dev;
  8459. int rc = 0;
  8460. /*
  8461. * After power on action, the chip can general FW sentinel. It can
  8462. * trigger an enumeration without hid_reset_cmd_state flag. Since the
  8463. * _fast_startup() can perform a quick enumeration too, here doesn't
  8464. * need another enumeration.
  8465. */
  8466. mutex_lock(&cd->system_lock);
  8467. cd->startup_status = STARTUP_STATUS_START;
  8468. cd->hid_reset_cmd_state = 1;
  8469. mutex_unlock(&cd->system_lock);
  8470. rc = cd->cpdata->power(cd->cpdata, 1, dev, 0);
  8471. if (rc < 0) {
  8472. pt_debug(dev, DL_ERROR, "%s: HW Power up fails r=%d\n",
  8473. __func__, rc);
  8474. goto exit;
  8475. }
  8476. if (!cd->irq_enabled) {
  8477. cd->irq_enabled = true;
  8478. enable_irq(cd->irq);
  8479. }
  8480. /* TBD: following function doesn't update startup_status */
  8481. rc = _fast_startup(cd);
  8482. exit:
  8483. return rc;
  8484. }
  8485. /*******************************************************************************
  8486. * FUNCTION: pt_core_wake_device_from_deep_standby_
  8487. *
  8488. * SUMMARY: Reset device, and then trigger a full enumeration.
  8489. *
  8490. * RETURN:
  8491. * 0 = success
  8492. * !0 = failure
  8493. *
  8494. * PARAMETERS:
  8495. * *cd - pointer to core data
  8496. ******************************************************************************/
  8497. static int pt_core_wake_device_from_deep_standby_(struct pt_core_data *cd)
  8498. {
  8499. int rc;
  8500. rc = pt_dut_reset_and_wait(cd);
  8501. if (rc < 0) {
  8502. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  8503. __func__, rc);
  8504. goto exit;
  8505. }
  8506. rc = _fast_startup(cd);
  8507. exit:
  8508. return rc;
  8509. }
  8510. /*******************************************************************************
  8511. * FUNCTION: pt_core_wake_
  8512. *
  8513. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8514. * the configuration in the core platform data structure.
  8515. *
  8516. * RETURN:
  8517. * 0 = success
  8518. * !0 = failure
  8519. *
  8520. * PARAMETERS:
  8521. * *cd - pointer to core data
  8522. ******************************************************************************/
  8523. static int pt_core_wake_(struct pt_core_data *cd)
  8524. {
  8525. int rc = 0;
  8526. mutex_lock(&cd->system_lock);
  8527. if (cd->sleep_state == SS_SLEEP_ON) {
  8528. cd->sleep_state = SS_WAKING;
  8529. } else {
  8530. mutex_unlock(&cd->system_lock);
  8531. return 1;
  8532. }
  8533. mutex_unlock(&cd->system_lock);
  8534. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8535. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  8536. rc = pt_core_wake_device_from_easy_wake_(cd);
  8537. else if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  8538. pt_debug(cd->dev, DL_INFO,
  8539. "%s: Entering into poweron mode:\n", __func__);
  8540. rc = pt_core_poweron_device_(cd);
  8541. if (rc < 0)
  8542. pr_err("%s: Poweron error detected: rc=%d\n",
  8543. __func__, rc);
  8544. }
  8545. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  8546. rc = pt_core_wake_device_from_deep_standby_(cd);
  8547. else /* Default action to exit DeepSleep */
  8548. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8549. }
  8550. mutex_lock(&cd->system_lock);
  8551. cd->sleep_state = SS_SLEEP_OFF;
  8552. mutex_unlock(&cd->system_lock);
  8553. pt_start_wd_timer(cd);
  8554. return rc;
  8555. }
  8556. /*******************************************************************************
  8557. * FUNCTION: pt_core_wake_
  8558. *
  8559. * SUMMARY: Protected call to pt_core_wake_ by exclusive access to the DUT.
  8560. *
  8561. * RETURN:
  8562. * 0 = success
  8563. * !0 = failure
  8564. *
  8565. * PARAMETERS:
  8566. * *cd - pointer to core data
  8567. ******************************************************************************/
  8568. static int pt_core_wake(struct pt_core_data *cd)
  8569. {
  8570. int rc;
  8571. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8572. if (rc < 0) {
  8573. pt_debug(cd->dev, DL_ERROR,
  8574. "%s: fail get exclusive ex=%p own=%p\n",
  8575. __func__, cd->exclusive_dev, cd->dev);
  8576. return rc;
  8577. }
  8578. rc = pt_core_wake_(cd);
  8579. if (release_exclusive(cd, cd->dev) < 0)
  8580. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8581. __func__);
  8582. else
  8583. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8584. __func__);
  8585. return rc;
  8586. }
  8587. /*******************************************************************************
  8588. * FUNCTION: pt_get_ic_crc_
  8589. *
  8590. * SUMMARY: This function retrieves the config block CRC
  8591. *
  8592. * NOTE: The post condition of calling this function will be that the DUT will
  8593. * be in SCANNINING mode if no failures occur
  8594. *
  8595. * RETURN:
  8596. * 0 = success
  8597. * !0 = failure
  8598. *
  8599. * PARAMETERS:
  8600. * *cd - pointer the core data structure
  8601. * ebid - enumerated block ID
  8602. ******************************************************************************/
  8603. static int pt_get_ic_crc_(struct pt_core_data *cd, u8 ebid)
  8604. {
  8605. struct pt_sysinfo *si = &cd->sysinfo;
  8606. int rc = 0;
  8607. u8 status;
  8608. u16 calculated_crc = 0;
  8609. u16 stored_crc = 0;
  8610. rc = pt_pip_suspend_scanning_(cd);
  8611. if (rc)
  8612. goto error;
  8613. rc = pt_pip_verify_config_block_crc_(cd, ebid, &status,
  8614. &calculated_crc, &stored_crc);
  8615. if (rc)
  8616. goto exit;
  8617. if (status) {
  8618. rc = -EINVAL;
  8619. goto exit;
  8620. }
  8621. si->ttconfig.crc = stored_crc;
  8622. exit:
  8623. pt_pip_resume_scanning_(cd);
  8624. error:
  8625. pt_debug(cd->dev, DL_INFO,
  8626. "%s: CRC: ebid:%d, calc:0x%04X, stored:0x%04X, rc=%d\n",
  8627. __func__, ebid, calculated_crc, stored_crc, rc);
  8628. return rc;
  8629. }
  8630. /*******************************************************************************
  8631. * FUNCTION: pt_pip2_read_gpio
  8632. *
  8633. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and stores the 32 gpio
  8634. * bits into the passed in variable
  8635. *
  8636. * NOTE: PIP2 READ_GPIO command is only supported in bootloader
  8637. *
  8638. * RETURN:
  8639. * 0 = success
  8640. * !0 = failure
  8641. *
  8642. * PARAMETERS:
  8643. * *dev - pointer to device structure
  8644. * *status - pointer to where the command response status is stored
  8645. * *gpio - pointer to device attributes structure
  8646. ******************************************************************************/
  8647. static int pt_pip2_read_gpio(struct device *dev, u8 *status, u32 *gpio)
  8648. {
  8649. int rc = 0;
  8650. u16 actual_read_len;
  8651. u8 read_buf[12];
  8652. u8 tmp_status = 0;
  8653. u8 index = PIP2_RESP_STATUS_OFFSET;
  8654. memset(read_buf, 0, ARRAY_SIZE(read_buf));
  8655. rc = _pt_request_pip2_send_cmd(dev,
  8656. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_READ_GPIO,
  8657. NULL, 0, read_buf, &actual_read_len);
  8658. if (rc) {
  8659. pt_debug(dev, DL_ERROR,
  8660. "%s: Failed to send PIP2 READ_GPIO cmd\n", __func__);
  8661. rc = -ECOMM;
  8662. } else {
  8663. tmp_status = read_buf[index];
  8664. }
  8665. if (status)
  8666. *status = tmp_status;
  8667. if (!rc && gpio && (tmp_status == 0)) {
  8668. *gpio = ((read_buf[index + 4] << 24) |
  8669. (read_buf[index + 3] << 16) |
  8670. (read_buf[index + 2] << 8) |
  8671. (read_buf[index + 1]));
  8672. }
  8673. return rc;
  8674. }
  8675. /*******************************************************************************
  8676. * FUNCTION: _pt_pip2_get_panel_id_by_gpio
  8677. *
  8678. * SUMMARY: Wrapper function to call pt_pip2_read_gpio() to get panel ID
  8679. *
  8680. * RETURN:
  8681. * 0 = success
  8682. * !0 = failure
  8683. *
  8684. * PARAMETERS:
  8685. * *cd - pointer the core data structure
  8686. * *pid - pointer to store panel ID
  8687. ******************************************************************************/
  8688. static int _pt_pip2_get_panel_id_by_gpio(struct pt_core_data *cd, u8 *pid)
  8689. {
  8690. u32 gpio_value = 0;
  8691. u8 status = 0;
  8692. u8 panel_id = PANEL_ID_NOT_ENABLED;
  8693. int rc = 0;
  8694. if (!pid)
  8695. return -ENOMEM;
  8696. rc = pt_pip2_read_gpio(cd->dev, &status, &gpio_value);
  8697. if (!rc) {
  8698. if (status == 0) {
  8699. panel_id = (gpio_value & PT_PANEL_ID_BITMASK) >>
  8700. PT_PANEL_ID_SHIFT;
  8701. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X %s=0x%08X\n",
  8702. __func__,
  8703. "BL mode PID", panel_id, "gpio", gpio_value);
  8704. *pid = panel_id;
  8705. } else {
  8706. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8707. __func__,
  8708. "BL read gpio failed status", status);
  8709. }
  8710. } else {
  8711. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8712. __func__,
  8713. "BL read gpio failed status", status);
  8714. }
  8715. return rc;
  8716. }
  8717. /*******************************************************************************
  8718. * FUNCTION: pt_enum_with_dut_
  8719. *
  8720. * SUMMARY: This function does the full enumeration of the DUT with TTDL.
  8721. * The core data (cd) startup_status will store, as a bitmask, each
  8722. * state of the enumeration process. The startup will be attempted
  8723. * PT_CORE_STARTUP_RETRY_COUNT times before giving up.
  8724. *
  8725. * RETURN:
  8726. * 0 = success
  8727. * !0 = failure
  8728. *
  8729. * PARAMETERS:
  8730. * *cd - pointer the core data structure
  8731. * reset - Flag to reset the DUT before attempting to enumerate
  8732. * *status - poionter to store the enum status bitmask flags
  8733. ******************************************************************************/
  8734. static int pt_enum_with_dut_(struct pt_core_data *cd, bool reset,
  8735. u32 *enum_status)
  8736. {
  8737. int try = 1;
  8738. int rc = 0;
  8739. int wait_time = 0;
  8740. bool detected = false;
  8741. u8 return_data[8];
  8742. u8 mode = PT_MODE_UNKNOWN;
  8743. u8 pid = PANEL_ID_NOT_ENABLED;
  8744. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  8745. struct pt_hid_desc hid_desc;
  8746. memset(&hid_desc, 0, sizeof(hid_desc));
  8747. #ifdef TTHE_TUNER_SUPPORT
  8748. tthe_print(cd, NULL, 0, "enter startup");
  8749. #endif
  8750. pt_debug(cd->dev, DL_INFO, "%s: Start enum... 0x%04X, reset=%d\n",
  8751. __func__, cd->startup_status, reset);
  8752. pt_stop_wd_timer(cd);
  8753. reset:
  8754. if (try > 1)
  8755. pt_debug(cd->dev, DL_WARN, "%s: DUT Enum Attempt %d\n",
  8756. __func__, try);
  8757. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8758. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8759. pt_debug(cd->dev, DL_INFO,
  8760. "%s: PIP1 Enumeration start\n", __func__);
  8761. /* Only reset the DUT after the first try */
  8762. if (reset || (try > 1)) {
  8763. /*
  8764. * Reset hardware only for Legacy parts. Skip for TT/TC
  8765. * parts because if the FW image was loaded directly
  8766. * to SRAM issueing a reset ill wipe out what was just
  8767. * loaded.
  8768. */
  8769. rc = pt_dut_reset_and_wait(cd);
  8770. if (rc < 0) {
  8771. pt_debug(cd->dev, DL_ERROR,
  8772. "%s: Error on h/w reset r=%d\n",
  8773. __func__, rc);
  8774. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8775. goto reset;
  8776. goto exit;
  8777. }
  8778. /* sleep to allow FW to be launched if available */
  8779. msleep(120);
  8780. }
  8781. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8782. if (rc < 0) {
  8783. pt_debug(cd->dev, DL_ERROR,
  8784. "%s: Error getting HID Descriptor r=%d\n",
  8785. __func__, rc);
  8786. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8787. goto reset;
  8788. rc = -EIO;
  8789. goto exit;
  8790. }
  8791. detected = true;
  8792. cd->mode = pt_get_mode(cd, &hid_desc);
  8793. /*
  8794. * Most systems do not use an XY pin as the panel_id and so
  8795. * the BL is used to retrieve the panel_id, however for
  8796. * systems that do use an XY pin, the panel_id MUST be
  8797. * retrieved from the system information when running FW
  8798. * (done below) and so this section of code is skipped.
  8799. * Entering the BL here is only needed on XY_PIN systems.
  8800. */
  8801. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  8802. if (cd->mode == PT_MODE_OPERATIONAL) {
  8803. rc = pt_pip_start_bootloader_(cd);
  8804. if (rc < 0) {
  8805. pt_debug(cd->dev, DL_ERROR,
  8806. "%s: Error on start bootloader r=%d\n",
  8807. __func__, rc);
  8808. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8809. goto reset;
  8810. goto exit;
  8811. }
  8812. cd->mode = PT_MODE_BOOTLOADER;
  8813. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8814. __func__);
  8815. }
  8816. rc = pt_hid_output_bl_get_information_(cd, return_data);
  8817. if (!rc) {
  8818. cd->bl_info.ready = true;
  8819. cd->bl_info.chip_id =
  8820. get_unaligned_le16(&return_data[2]);
  8821. pt_debug(cd->dev, DL_INFO, "%s: chip ID %04X\n",
  8822. __func__, cd->bl_info.chip_id);
  8823. } else {
  8824. pt_debug(cd->dev, DL_ERROR,
  8825. "%s: failed to get chip ID, r=%d\n",
  8826. __func__, rc);
  8827. }
  8828. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  8829. mutex_lock(&cd->system_lock);
  8830. if (!rc) {
  8831. cd->pid_for_loader = pid;
  8832. pt_debug(cd->dev, DL_INFO, "%s: Panel ID: 0x%02X\n",
  8833. __func__, cd->pid_for_loader);
  8834. } else {
  8835. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  8836. pt_debug(cd->dev, DL_WARN,
  8837. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  8838. __func__, cd->pid_for_loader);
  8839. }
  8840. mutex_unlock(&cd->system_lock);
  8841. }
  8842. /* Exit BL due to XY_PIN case or any other cause to be in BL */
  8843. if (cd->mode == PT_MODE_BOOTLOADER) {
  8844. rc = pt_hid_output_bl_launch_app_(cd);
  8845. if (rc < 0) {
  8846. pt_debug(cd->dev, DL_ERROR,
  8847. "%s: Error on launch app r=%d\n",
  8848. __func__, rc);
  8849. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8850. goto reset;
  8851. rc = -ENODEV;
  8852. goto exit;
  8853. }
  8854. /* sleep to allow FW to be launched if available */
  8855. msleep(120);
  8856. /* Ensure the DUT is now in Application mode */
  8857. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8858. if (rc < 0) {
  8859. pt_debug(cd->dev, DL_ERROR,
  8860. "%s: Error getting HID Desc r=%d\n",
  8861. __func__, rc);
  8862. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8863. goto reset;
  8864. rc = -EIO;
  8865. goto exit;
  8866. }
  8867. cd->mode = pt_get_mode(cd, &hid_desc);
  8868. if (cd->mode == PT_MODE_BOOTLOADER) {
  8869. pt_debug(cd->dev, DL_WARN,
  8870. "%s: Error confiming exit BL\n",
  8871. __func__);
  8872. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8873. goto reset;
  8874. rc = -ENODEV;
  8875. goto exit;
  8876. }
  8877. }
  8878. pt_debug(cd->dev, DL_INFO, "%s: Operational mode\n", __func__);
  8879. cd->mode = PT_MODE_OPERATIONAL;
  8880. *enum_status |= STARTUP_STATUS_GET_DESC;
  8881. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8882. } else {
  8883. /* Generation is PIP2 Capable */
  8884. pt_debug(cd->dev, DL_INFO,
  8885. "%s: PIP2 Enumeration start\n", __func__);
  8886. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8887. if (rc) {
  8888. pt_debug(cd->dev, DL_ERROR,
  8889. "%s: Get mode failed, mode unknown\n",
  8890. __func__);
  8891. mode = PT_MODE_UNKNOWN;
  8892. } else
  8893. detected = true;
  8894. cd->mode = mode;
  8895. switch (cd->mode) {
  8896. case PT_MODE_OPERATIONAL:
  8897. pt_debug(cd->dev, DL_INFO,
  8898. "%s: Operational mode\n", __func__);
  8899. if (cd->app_pip_ver_ready == false) {
  8900. rc = pt_pip2_get_version_(cd);
  8901. if (!rc)
  8902. cd->app_pip_ver_ready = true;
  8903. else {
  8904. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8905. goto reset;
  8906. goto exit;
  8907. }
  8908. }
  8909. break;
  8910. case PT_MODE_BOOTLOADER:
  8911. pt_debug(cd->dev, DL_INFO,
  8912. "%s: Bootloader mode\n", __func__);
  8913. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  8914. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  8915. mutex_lock(&cd->system_lock);
  8916. if (!rc) {
  8917. cd->pid_for_loader = pid;
  8918. pt_debug(cd->dev, DL_INFO,
  8919. "%s: Panel ID: 0x%02X\n",
  8920. __func__, cd->pid_for_loader);
  8921. } else {
  8922. cd->pid_for_loader =
  8923. PANEL_ID_NOT_ENABLED;
  8924. pt_debug(cd->dev, DL_WARN,
  8925. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  8926. __func__, cd->pid_for_loader);
  8927. }
  8928. mutex_unlock(&cd->system_lock);
  8929. }
  8930. if (cd->bl_pip_ver_ready == false) {
  8931. rc = pt_pip2_get_version_(cd);
  8932. if (!rc)
  8933. cd->bl_pip_ver_ready = true;
  8934. else {
  8935. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8936. goto reset;
  8937. goto exit;
  8938. }
  8939. }
  8940. /*
  8941. * Launch app command will fail in flashless mode.
  8942. * Skip launch app command here to save time for
  8943. * enumeration flow.
  8944. */
  8945. if (cd->flashless_dut)
  8946. goto exit;
  8947. /*
  8948. * pt_pip2_launch_app() is needed here instead of
  8949. * pt_pip2_exit_bl() because exit_bl will cause several
  8950. * hw_resets to occur and the auto BL on a flashless
  8951. * DUT will fail.
  8952. */
  8953. rc = pt_pip2_launch_app(cd->dev,
  8954. PT_CORE_CMD_UNPROTECTED);
  8955. if (rc) {
  8956. pt_debug(cd->dev, DL_ERROR,
  8957. "%s: Error on launch app r=%d\n",
  8958. __func__, rc);
  8959. msleep(50);
  8960. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8961. goto reset;
  8962. goto exit;
  8963. }
  8964. /*
  8965. * IRQ thread can be delayed if the serial log print is
  8966. * enabled. It causes next command to get wrong response
  8967. * Here the delay is to ensure pt_parse_input() to be
  8968. * finished.
  8969. */
  8970. msleep(60);
  8971. /* Check and update the mode */
  8972. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8973. if (rc) {
  8974. pt_debug(cd->dev, DL_ERROR,
  8975. "%s: Get mode failed, mode unknown\n",
  8976. __func__);
  8977. mode = PT_MODE_UNKNOWN;
  8978. }
  8979. cd->mode = mode;
  8980. if (cd->mode == PT_MODE_OPERATIONAL) {
  8981. pt_debug(cd->dev, DL_INFO,
  8982. "%s: Launched to Operational mode\n",
  8983. __func__);
  8984. } else if (cd->mode == PT_MODE_BOOTLOADER) {
  8985. pt_debug(cd->dev, DL_ERROR,
  8986. "%s: Launch failed, Bootloader mode\n",
  8987. __func__);
  8988. goto exit;
  8989. } else if (cd->mode == PT_MODE_UNKNOWN) {
  8990. pt_debug(cd->dev, DL_ERROR,
  8991. "%s: Launch failed, Unknown mode\n",
  8992. __func__);
  8993. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8994. goto reset;
  8995. goto exit;
  8996. }
  8997. if (cd->app_pip_ver_ready == false) {
  8998. rc = pt_pip2_get_version_(cd);
  8999. if (!rc)
  9000. cd->app_pip_ver_ready = true;
  9001. else {
  9002. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9003. goto reset;
  9004. goto exit;
  9005. }
  9006. }
  9007. break;
  9008. default:
  9009. pt_debug(cd->dev, DL_ERROR,
  9010. "%s: Unknown mode\n", __func__);
  9011. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9012. goto reset;
  9013. goto exit;
  9014. }
  9015. *enum_status |= STARTUP_STATUS_GET_DESC;
  9016. }
  9017. pt_init_pip_report_fields(cd);
  9018. *enum_status |= STARTUP_STATUS_GET_RPT_DESC;
  9019. if (!cd->features.easywake)
  9020. cd->easy_wakeup_gesture = PT_CORE_EWG_NONE;
  9021. pt_debug(cd->dev, DL_INFO, "%s: Reading sysinfo\n", __func__);
  9022. rc = pt_hid_output_get_sysinfo_(cd);
  9023. if (rc) {
  9024. pt_debug(cd->dev, DL_ERROR,
  9025. "%s: Error on getting sysinfo r=%d\n", __func__, rc);
  9026. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9027. goto reset;
  9028. goto exit;
  9029. }
  9030. *enum_status |= STARTUP_STATUS_GET_SYS_INFO;
  9031. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  9032. rc = _pt_poll_for_fw_exit_boot_mode(cd, 10000, &wait_time);
  9033. if (!rc) {
  9034. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9035. pt_debug(cd->dev, DL_WARN,
  9036. "%s: Exit FW BOOT Mode after %dms\n",
  9037. __func__, wait_time);
  9038. } else {
  9039. pt_debug(cd->dev, DL_WARN,
  9040. "%s: FW stuck in BOOT Mode after %dms\n",
  9041. __func__, wait_time);
  9042. goto exit;
  9043. }
  9044. pt_debug(cd->dev, DL_INFO, "%s pt Prot Version: %d.%d\n",
  9045. __func__,
  9046. cd->sysinfo.ttdata.pip_ver_major,
  9047. cd->sysinfo.ttdata.pip_ver_minor);
  9048. rc = pt_get_ic_crc_(cd, PT_TCH_PARM_EBID);
  9049. if (rc) {
  9050. pt_debug(cd->dev, DL_ERROR,
  9051. "%s: DUT Config block CRC failure rc=%d\n",
  9052. __func__, rc);
  9053. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9054. goto reset;
  9055. } else {
  9056. _pt_get_fw_sys_mode(cd, &sys_mode, NULL);
  9057. if (sys_mode != FW_SYS_MODE_SCANNING) {
  9058. pt_debug(cd->dev, DL_ERROR,
  9059. "%s: scan state: %d, retry: %d\n",
  9060. __func__, sys_mode, try);
  9061. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9062. goto reset;
  9063. } else
  9064. *enum_status |= STARTUP_STATUS_GET_CFG_CRC;
  9065. }
  9066. rc = pt_restore_parameters_(cd);
  9067. if (rc) {
  9068. pt_debug(cd->dev, DL_ERROR,
  9069. "%s: Failed to restore parameters rc=%d\n",
  9070. __func__, rc);
  9071. } else
  9072. *enum_status |= STARTUP_STATUS_RESTORE_PARM;
  9073. call_atten_cb(cd, PT_ATTEN_STARTUP, 0);
  9074. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  9075. cd->startup_retry_count = 0;
  9076. exit:
  9077. /* Generate the HW Version of the connected DUT and store in cd */
  9078. pt_generate_hw_version(cd, cd->hw_version);
  9079. pt_debug(cd->dev, DL_WARN, "%s: HW Version: %s\n", __func__,
  9080. cd->hw_version);
  9081. pt_start_wd_timer(cd);
  9082. if (!detected)
  9083. rc = -ENODEV;
  9084. #ifdef TTHE_TUNER_SUPPORT
  9085. tthe_print(cd, NULL, 0, "exit startup");
  9086. #endif
  9087. pt_debug(cd->dev, DL_WARN,
  9088. "%s: Completed Enumeration rc=%d On Attempt %d\n",
  9089. __func__, rc, try);
  9090. return rc;
  9091. }
  9092. /*******************************************************************************
  9093. * FUNCTION: pt_enum_with_dut
  9094. *
  9095. * SUMMARY: This is the safe function wrapper for pt_enum_with_dut_() by
  9096. * requesting exclusive access around the call.
  9097. *
  9098. * RETURN:
  9099. * 0 = success
  9100. * !0 = failure
  9101. *
  9102. * PARAMETERS:
  9103. * *cd - pointer the core data structure
  9104. * reset - Flag to reset the DUT before attempting to enumerate
  9105. * *status - pointer to store the enum status bitmask flags
  9106. ******************************************************************************/
  9107. static int pt_enum_with_dut(struct pt_core_data *cd, bool reset, u32 *status)
  9108. {
  9109. int rc = 0;
  9110. mutex_lock(&cd->system_lock);
  9111. cd->startup_state = STARTUP_RUNNING;
  9112. mutex_unlock(&cd->system_lock);
  9113. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  9114. if (rc) {
  9115. pt_debug(cd->dev, DL_ERROR,
  9116. "%s: fail get exclusive ex=%p own=%p\n",
  9117. __func__, cd->exclusive_dev, cd->dev);
  9118. goto exit;
  9119. }
  9120. rc = pt_enum_with_dut_(cd, reset, status);
  9121. if (release_exclusive(cd, cd->dev) < 0)
  9122. /* Don't return fail code, mode is already changed. */
  9123. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  9124. __func__);
  9125. else
  9126. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  9127. __func__);
  9128. exit:
  9129. mutex_lock(&cd->system_lock);
  9130. /* Clear startup state for any tasks waiting for startup completion */
  9131. cd->startup_state = STARTUP_NONE;
  9132. mutex_unlock(&cd->system_lock);
  9133. /* Set STATUS_COMPLETE bit to indicate the status is ready to be read */
  9134. *status |= STARTUP_STATUS_COMPLETE;
  9135. /* Wake the waiters for end of startup */
  9136. wake_up(&cd->wait_q);
  9137. return rc;
  9138. }
  9139. static int add_sysfs_interfaces(struct device *dev);
  9140. static void remove_sysfs_interfaces(struct device *dev);
  9141. static void remove_sysfs_and_modules(struct device *dev);
  9142. static void pt_release_modules(struct pt_core_data *cd);
  9143. static void pt_probe_modules(struct pt_core_data *cd);
  9144. /*******************************************************************************
  9145. * FUNCTION: _pt_ttdl_restart
  9146. *
  9147. * SUMMARY: Restarts TTDL enumeration with the DUT and re-probes all modules
  9148. *
  9149. * NOTE: The function DOSE NOT remove sysfs and modules. Trying to create
  9150. * existing sysfs nodes will produce an error.
  9151. *
  9152. * RETURN:
  9153. * 0 = success
  9154. * !0 = failure
  9155. *
  9156. * PARAMETERS:
  9157. * *dev - pointer to core device
  9158. ******************************************************************************/
  9159. static int _pt_ttdl_restart(struct device *dev)
  9160. {
  9161. int rc = 0;
  9162. struct pt_core_data *cd = dev_get_drvdata(dev);
  9163. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9164. struct i2c_client *client =
  9165. (struct i2c_client *)container_of(dev, struct i2c_client, dev);
  9166. #endif
  9167. /*
  9168. * Make sure the device is awake, pt_mt_release function will
  9169. * cause pm sleep function and lead to deadlock.
  9170. */
  9171. pm_runtime_get_sync(dev);
  9172. /* Use ttdl_restart_lock to avoid reentry */
  9173. mutex_lock(&cd->ttdl_restart_lock);
  9174. remove_sysfs_and_modules(cd->dev);
  9175. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9176. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  9177. pt_debug(dev, DL_ERROR,
  9178. "%s I2C functionality not Supported\n", __func__);
  9179. rc = -EIO;
  9180. goto ttdl_no_error;
  9181. }
  9182. #endif
  9183. if (cd->active_dut_generation == DUT_UNKNOWN) {
  9184. rc = _pt_detect_dut_generation(cd->dev,
  9185. &cd->startup_status, &cd->active_dut_generation,
  9186. &cd->mode);
  9187. if ((cd->active_dut_generation == DUT_UNKNOWN) || (rc)) {
  9188. pt_debug(dev, DL_ERROR,
  9189. "%s: Error, Unknown DUT Generation rc=%d\n",
  9190. __func__, rc);
  9191. }
  9192. }
  9193. rc = add_sysfs_interfaces(cd->dev);
  9194. if (rc < 0)
  9195. pt_debug(cd->dev, DL_ERROR,
  9196. "%s: Error, failed sysfs nodes rc=%d\n",
  9197. __func__, rc);
  9198. if (!(cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL)) {
  9199. pt_debug(dev, DL_INFO, "%s: Call pt_enum_with_dut\n", __func__);
  9200. rc = pt_enum_with_dut(cd, true, &cd->startup_status);
  9201. if (rc < 0)
  9202. pt_debug(dev, DL_ERROR,
  9203. "%s: Error, Failed to Enumerate\n", __func__);
  9204. }
  9205. rc = pt_mt_probe(dev);
  9206. if (rc < 0) {
  9207. pt_debug(dev, DL_ERROR,
  9208. "%s: Error, fail mt probe\n", __func__);
  9209. }
  9210. rc = pt_btn_probe(dev);
  9211. if (rc < 0) {
  9212. pt_debug(dev, DL_ERROR,
  9213. "%s: Error, fail btn probe\n", __func__);
  9214. }
  9215. pt_probe_modules(cd);
  9216. pt_debug(cd->dev, DL_WARN,
  9217. "%s: Well Done! TTDL Restart Completed\n", __func__);
  9218. rc = 0;
  9219. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9220. ttdl_no_error:
  9221. #endif
  9222. mutex_unlock(&cd->ttdl_restart_lock);
  9223. mutex_lock(&cd->system_lock);
  9224. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  9225. cd->startup_state = STARTUP_NONE;
  9226. mutex_unlock(&cd->system_lock);
  9227. pm_runtime_put(dev);
  9228. return rc;
  9229. }
  9230. /*******************************************************************************
  9231. * FUNCTION: pt_restart_work_function
  9232. *
  9233. * SUMMARY: This is the wrapper function placed in a work queue to call
  9234. * _pt_ttdl_restart()
  9235. *
  9236. * RETURN: none
  9237. *
  9238. * PARAMETERS:
  9239. * *work - pointer to the work_struct
  9240. ******************************************************************************/
  9241. static void pt_restart_work_function(struct work_struct *work)
  9242. {
  9243. struct pt_core_data *cd = container_of(work,
  9244. struct pt_core_data, ttdl_restart_work);
  9245. int rc = 0;
  9246. rc = _pt_ttdl_restart(cd->dev);
  9247. if (rc < 0)
  9248. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9249. __func__, rc);
  9250. }
  9251. /*******************************************************************************
  9252. * FUNCTION: pt_enum_work_function
  9253. *
  9254. * SUMMARY: This is the wrapper function placed in a work queue to call
  9255. * pt_enum_with_dut()
  9256. *
  9257. * RETURN: none
  9258. *
  9259. * PARAMETERS:
  9260. * *work - pointer to the work_struct
  9261. ******************************************************************************/
  9262. static void pt_enum_work_function(struct work_struct *work)
  9263. {
  9264. struct pt_core_data *cd = container_of(work,
  9265. struct pt_core_data, enum_work);
  9266. int rc;
  9267. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  9268. if (rc < 0)
  9269. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9270. __func__, rc);
  9271. }
  9272. static int pt_get_regulator(struct pt_core_data *cd, bool get)
  9273. {
  9274. int rc;
  9275. if (!get) {
  9276. rc = 0;
  9277. goto regulator_put;
  9278. }
  9279. cd->vdd = regulator_get(cd->dev, "vdd");
  9280. if (IS_ERR(cd->vdd)) {
  9281. rc = PTR_ERR(cd->vdd);
  9282. dev_err(cd->dev,
  9283. "Regulator get failed vdd rc=%d\n", rc);
  9284. goto regulator_put;
  9285. }
  9286. cd->vcc_i2c = regulator_get(cd->dev, "vcc_i2c");
  9287. if (IS_ERR(cd->vcc_i2c)) {
  9288. rc = PTR_ERR(cd->vcc_i2c);
  9289. dev_err(cd->dev,
  9290. "Regulator get failed vcc_i2c rc=%d\n", rc);
  9291. goto regulator_put;
  9292. }
  9293. return 0;
  9294. regulator_put:
  9295. if (cd->vdd) {
  9296. regulator_put(cd->vdd);
  9297. cd->vdd = NULL;
  9298. }
  9299. if (cd->vcc_i2c) {
  9300. regulator_put(cd->vcc_i2c);
  9301. cd->vcc_i2c = NULL;
  9302. }
  9303. return rc;
  9304. }
  9305. static int pt_enable_regulator(struct pt_core_data *cd, bool en)
  9306. {
  9307. int rc;
  9308. if (!en) {
  9309. rc = 0;
  9310. goto disable_vcc_i2c_reg;
  9311. }
  9312. if (cd->vdd) {
  9313. if (regulator_count_voltages(cd->vdd) > 0) {
  9314. rc = regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9315. FT_VTG_MAX_UV);
  9316. if (rc) {
  9317. dev_err(cd->dev,
  9318. "Regulator set_vtg failed vdd rc=%d\n", rc);
  9319. goto exit;
  9320. }
  9321. }
  9322. rc = regulator_enable(cd->vdd);
  9323. if (rc) {
  9324. dev_err(cd->dev,
  9325. "Regulator vdd enable failed rc=%d\n", rc);
  9326. goto exit;
  9327. }
  9328. }
  9329. if (cd->vcc_i2c) {
  9330. if (regulator_count_voltages(cd->vcc_i2c) > 0) {
  9331. rc = regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9332. FT_I2C_VTG_MAX_UV);
  9333. if (rc) {
  9334. dev_err(cd->dev,
  9335. "Regulator set_vtg failed vcc_i2c rc=%d\n", rc);
  9336. goto disable_vdd_reg;
  9337. }
  9338. }
  9339. rc = regulator_enable(cd->vcc_i2c);
  9340. if (rc) {
  9341. dev_err(cd->dev,
  9342. "Regulator vcc_i2c enable failed rc=%d\n", rc);
  9343. goto disable_vdd_reg;
  9344. }
  9345. }
  9346. return 0;
  9347. disable_vcc_i2c_reg:
  9348. if (cd->vcc_i2c) {
  9349. if (regulator_count_voltages(cd->vcc_i2c) > 0)
  9350. regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9351. FT_I2C_VTG_MAX_UV);
  9352. regulator_disable(cd->vcc_i2c);
  9353. }
  9354. disable_vdd_reg:
  9355. if (cd->vdd) {
  9356. if (regulator_count_voltages(cd->vdd) > 0)
  9357. regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9358. FT_VTG_MAX_UV);
  9359. regulator_disable(cd->vdd);
  9360. }
  9361. exit:
  9362. return rc;
  9363. }
  9364. #if (KERNEL_VERSION(3, 19, 0) <= LINUX_VERSION_CODE)
  9365. #define KERNEL_VER_GT_3_19
  9366. #endif
  9367. #if defined(CONFIG_PM_RUNTIME) || defined(KERNEL_VER_GT_3_19)
  9368. /* CONFIG_PM_RUNTIME option is removed in 3.19.0 */
  9369. #if defined(CONFIG_PM_SLEEP)
  9370. /*******************************************************************************
  9371. * FUNCTION: pt_core_rt_suspend
  9372. *
  9373. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_sleep.
  9374. *
  9375. * RETURN:
  9376. * 0 = success
  9377. * !0 = failure
  9378. *
  9379. * PARAMETERS:
  9380. * *dev - pointer to core device
  9381. ******************************************************************************/
  9382. static int pt_core_rt_suspend(struct device *dev)
  9383. {
  9384. struct pt_core_data *cd = dev_get_drvdata(dev);
  9385. int rc = 0;
  9386. dev_info(dev, "%s: Entering into runtime suspend mode:\n",
  9387. __func__);
  9388. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  9389. return 0;
  9390. rc = pt_core_sleep(cd);
  9391. if (rc < 0) {
  9392. pt_debug(dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  9393. return -EAGAIN;
  9394. }
  9395. return 0;
  9396. }
  9397. /*******************************************************************************
  9398. * FUNCTION: pt_core_rt_resume
  9399. *
  9400. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_wake.
  9401. *
  9402. * RETURN:
  9403. * 0 = success
  9404. * !0 = failure
  9405. *
  9406. * PARAMETERS:
  9407. * *dev - pointer to core device
  9408. ******************************************************************************/
  9409. static int pt_core_rt_resume(struct device *dev)
  9410. {
  9411. struct pt_core_data *cd = dev_get_drvdata(dev);
  9412. int rc = 0;
  9413. dev_info(dev, "%s: Entering into runtime resume mode:\n",
  9414. __func__);
  9415. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  9416. return 0;
  9417. rc = pt_core_wake(cd);
  9418. if (rc < 0) {
  9419. pt_debug(dev, DL_ERROR, "%s: Error on wake\n", __func__);
  9420. return -EAGAIN;
  9421. }
  9422. return 0;
  9423. }
  9424. #endif /* CONFIG_PM_SLEEP */
  9425. #endif /* CONFIG_PM_RUNTIME || LINUX_VERSION_CODE */
  9426. #if defined(CONFIG_PM_SLEEP)
  9427. /*******************************************************************************
  9428. * FUNCTION: pt_core_suspend_
  9429. *
  9430. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9431. * pt_core_sleep. This function may disable IRQ during sleep state.
  9432. *
  9433. * RETURN:
  9434. * 0 = success
  9435. * !0 = failure
  9436. *
  9437. * PARAMETERS:
  9438. * *dev - pointer to core device
  9439. ******************************************************************************/
  9440. static int pt_core_suspend_(struct device *dev)
  9441. {
  9442. int rc;
  9443. struct pt_core_data *cd = dev_get_drvdata(dev);
  9444. pt_debug(dev, DL_INFO, "%s: Entering into suspend mode:\n",
  9445. __func__);
  9446. rc = pt_core_sleep(cd);
  9447. if (rc < 0) {
  9448. pt_debug(dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  9449. return -EAGAIN;
  9450. }
  9451. rc = pt_enable_regulator(cd, false);
  9452. if (rc) {
  9453. dev_err(dev, "%s: Failed to disable regulators: rc=%d\n",
  9454. __func__, rc);
  9455. }
  9456. dev_info(dev, "%s: Sayantan1: Voltage regulators disabled: rc=%d\n",
  9457. __func__, rc);
  9458. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9459. return 0;
  9460. /* Required to prevent interrupts before bus awake */
  9461. disable_irq(cd->irq);
  9462. cd->irq_disabled = 1;
  9463. if (device_may_wakeup(dev)) {
  9464. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9465. __func__);
  9466. if (!enable_irq_wake(cd->irq))
  9467. cd->irq_wake = 1;
  9468. } else {
  9469. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9470. __func__);
  9471. }
  9472. return rc;
  9473. }
  9474. /*******************************************************************************
  9475. * FUNCTION: pt_core_suspend
  9476. *
  9477. * SUMMARY: Wrapper function of pt_core_suspend_() to help avoid TP from being
  9478. * woke up or put to sleep based on Kernel power state even when the display
  9479. * is off based on the check of TTDL core platform flag.
  9480. *
  9481. * RETURN:
  9482. * 0 = success
  9483. * !0 = failure
  9484. *
  9485. * PARAMETERS:
  9486. * *dev - pointer to core device
  9487. ******************************************************************************/
  9488. static int pt_core_suspend(struct device *dev)
  9489. {
  9490. struct pt_core_data *cd = dev_get_drvdata(dev);
  9491. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9492. return 0;
  9493. return pt_core_suspend_(dev);
  9494. }
  9495. /*******************************************************************************
  9496. * FUNCTION: pt_core_resume_
  9497. *
  9498. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9499. * pt_core_wake. This function may enable IRQ before wake up.
  9500. *
  9501. * RETURN:
  9502. * 0 = success
  9503. * !0 = failure
  9504. *
  9505. * PARAMETERS:
  9506. * *dev - pointer to core device
  9507. ******************************************************************************/
  9508. static int pt_core_resume_(struct device *dev)
  9509. {
  9510. int rc = 0;
  9511. struct pt_core_data *cd = dev_get_drvdata(dev);
  9512. dev_info(dev, "%s: Entering into resume mode:\n",
  9513. __func__);
  9514. rc = pt_enable_regulator(cd, true);
  9515. if (rc < 0) {
  9516. dev_err(dev, "%s: Failed to enable regulators: rc=%d\n",
  9517. __func__, rc);
  9518. }
  9519. dev_info(dev, "%s: Voltage regulator enabled: rc=%d\n",
  9520. __func__, rc);
  9521. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9522. goto exit;
  9523. /*
  9524. * Bus pm does not call suspend if device runtime suspended
  9525. * This flag is covers that case
  9526. */
  9527. if (cd->irq_disabled) {
  9528. enable_irq(cd->irq);
  9529. cd->irq_disabled = 0;
  9530. }
  9531. if (device_may_wakeup(dev)) {
  9532. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9533. __func__);
  9534. if (cd->irq_wake) {
  9535. disable_irq_wake(cd->irq);
  9536. cd->irq_wake = 0;
  9537. }
  9538. } else {
  9539. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9540. __func__);
  9541. }
  9542. exit:
  9543. pt_core_wake(cd);
  9544. return 0;
  9545. }
  9546. /*******************************************************************************
  9547. * FUNCTION: pt_core_resume
  9548. *
  9549. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9550. * along with kernel power state even the display is off based on the check of
  9551. * TTDL core platform flag.
  9552. *
  9553. * RETURN:
  9554. * 0 = success
  9555. * !0 = failure
  9556. *
  9557. * PARAMETERS:
  9558. * *dev - pointer to core device
  9559. ******************************************************************************/
  9560. static int pt_core_resume(struct device *dev)
  9561. {
  9562. struct pt_core_data *cd = dev_get_drvdata(dev);
  9563. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9564. return 0;
  9565. return pt_core_resume_(dev);
  9566. }
  9567. #endif
  9568. #ifdef NEED_SUSPEND_NOTIFIER
  9569. /*******************************************************************************
  9570. * FUNCTION: pt_pm_notifier
  9571. *
  9572. * SUMMARY: This function is registered to notifier chain and will perform
  9573. * suspend operation if match event PM_SUSPEND_PREPARE.
  9574. *
  9575. * RETURN:
  9576. * 0 = success
  9577. * !0 = failure
  9578. *
  9579. * PARAMETERS:
  9580. * *nb - pointer to notifier_block structure
  9581. * action - notifier event type
  9582. * *data - void pointer
  9583. ******************************************************************************/
  9584. static int pt_pm_notifier(struct notifier_block *nb,
  9585. unsigned long action, void *data)
  9586. {
  9587. struct pt_core_data *cd = container_of(nb,
  9588. struct pt_core_data, pm_notifier);
  9589. if (action == PM_SUSPEND_PREPARE) {
  9590. pt_debug(cd->dev, DL_INFO, "%s: Suspend prepare\n",
  9591. __func__);
  9592. /*
  9593. * If PM runtime is not suspended, either call runtime
  9594. * PM suspend callback or wait until it finishes
  9595. */
  9596. if (!pm_runtime_suspended(cd->dev))
  9597. pm_runtime_suspend(cd->dev);
  9598. (void) pt_core_suspend(cd->dev);
  9599. }
  9600. return NOTIFY_DONE;
  9601. }
  9602. #endif
  9603. const struct dev_pm_ops pt_pm_ops = {
  9604. SET_SYSTEM_SLEEP_PM_OPS(pt_core_suspend, pt_core_resume)
  9605. SET_RUNTIME_PM_OPS(pt_core_rt_suspend, pt_core_rt_resume,
  9606. NULL)
  9607. };
  9608. EXPORT_SYMBOL_GPL(pt_pm_ops);
  9609. /*******************************************************************************
  9610. * FUNCTION: _pt_request_pip2_enter_bl
  9611. *
  9612. * SUMMARY: Force the DUT to enter the BL by resetting the DUT by use of the
  9613. * XRES pin or a soft reset.
  9614. *
  9615. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  9616. * the WD active could cause this function to fail!
  9617. * NOTE: If start_mode is passed in as PT_MODE_IGNORE, this function
  9618. * will not try to determine the current mode but will proceed with
  9619. * resetting the DUT and entering the BL.
  9620. *
  9621. * NOTE: The definition of result code:
  9622. * PT_ENTER_BL_PASS (0)
  9623. * PT_ENTER_BL_ERROR (1)
  9624. * PT_ENTER_BL_RESET_FAIL (2)
  9625. * PT_ENTER_BL_HID_START_BL_FAIL (3)
  9626. * PT_ENTER_BL_CONFIRM_FAIL (4)
  9627. * PT_ENTER_BL_GET_FLASH_INFO_FAIL (5)
  9628. *
  9629. * RETURNS:
  9630. * 0 = success
  9631. * !0 = failure
  9632. *
  9633. *
  9634. * PARAMETERS:
  9635. * *dev - pointer to device structure
  9636. * *start_mode - pointer to the mode the DUT was in when this function
  9637. * starts
  9638. * *result - pointer to store the result when to enter BL
  9639. ******************************************************************************/
  9640. int _pt_request_pip2_enter_bl(struct device *dev, u8 *start_mode, int *result)
  9641. {
  9642. int rc = 0;
  9643. int t;
  9644. int tmp_result = PT_ENTER_BL_ERROR;
  9645. int flash_info_retry = 2;
  9646. u8 mode = PT_MODE_UNKNOWN;
  9647. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  9648. u8 read_buf[32];
  9649. u16 actual_read_len;
  9650. struct pt_core_data *cd = dev_get_drvdata(dev);
  9651. u8 host_mode_cmd[4] = {0xA5, 0xA5, 0xA5, 0xA5};
  9652. u8 time = 0;
  9653. u8 saved_flashless_auto_bl_mode = cd->flashless_auto_bl;
  9654. if (cd->watchdog_enabled) {
  9655. pt_debug(dev, DL_WARN,
  9656. "%s: Watchdog must be stopped before entering BL\n",
  9657. __func__);
  9658. goto exit;
  9659. }
  9660. cancel_work_sync(&cd->enum_work);
  9661. cancel_work_sync(&cd->watchdog_work);
  9662. /* if undefined assume operational/test to bypass all checks */
  9663. if (*start_mode == PT_MODE_IGNORE) {
  9664. mode = PT_MODE_OPERATIONAL;
  9665. sys_mode = FW_SYS_MODE_TEST;
  9666. pt_debug(dev, DL_INFO, "%s: Assume Mode = %d", __func__, mode);
  9667. } else if (*start_mode == PT_MODE_UNKNOWN) {
  9668. rc = pt_pip2_get_mode_sysmode_(cd, &mode, &sys_mode);
  9669. if (rc) {
  9670. pt_debug(dev, DL_ERROR,
  9671. "%s: Get mode failed, mode unknown\n",
  9672. __func__);
  9673. }
  9674. *start_mode = mode;
  9675. pt_debug(dev, DL_INFO, "%s: Get Mode = %d", __func__, mode);
  9676. } else if (*start_mode == PT_MODE_OPERATIONAL) {
  9677. /* Assume SCANNIING mode to avoid doing an extra get_mode */
  9678. sys_mode = FW_SYS_MODE_SCANNING;
  9679. }
  9680. _retry:
  9681. /* For Flashless DUTs - Suppress auto BL on next BL sentinel */
  9682. pt_debug(dev, DL_INFO, "%s: Flashless Auto_BL - SUPPRESS\n", __func__);
  9683. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  9684. switch (mode) {
  9685. case PT_MODE_UNKNOWN:
  9686. /*
  9687. * When the mode could not be determined the DUT could be
  9688. * in App mode running corrupted FW or FW that is not
  9689. * responding to the mode request, assume no communication
  9690. * and do a hard reset
  9691. */
  9692. mutex_lock(&cd->system_lock);
  9693. cd->startup_status = STARTUP_STATUS_START;
  9694. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9695. mutex_unlock(&cd->system_lock);
  9696. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  9697. if (rc) {
  9698. tmp_result = PT_ENTER_BL_RESET_FAIL;
  9699. goto exit;
  9700. }
  9701. break;
  9702. case PT_MODE_OPERATIONAL:
  9703. if (sys_mode == FW_SYS_MODE_SCANNING) {
  9704. pt_debug(dev, DL_INFO, "%s: Suspend Scanning\n",
  9705. __func__);
  9706. rc = pt_pip_suspend_scanning_(cd);
  9707. if (rc) {
  9708. /*
  9709. * Print to log but don't exit, the FW could be
  9710. * running but be hung or fail to respond to
  9711. * this request
  9712. */
  9713. pt_debug(dev, DL_ERROR,
  9714. "%s Suspend Scan Failed\n", __func__);
  9715. }
  9716. /* sleep to allow the suspend scan to be processed */
  9717. usleep_range(1000, 2000);
  9718. }
  9719. mutex_lock(&cd->system_lock);
  9720. cd->startup_status = STARTUP_STATUS_START;
  9721. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9722. mutex_unlock(&cd->system_lock);
  9723. /* Reset device to enter the BL */
  9724. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  9725. if (rc) {
  9726. tmp_result = PT_ENTER_BL_RESET_FAIL;
  9727. goto exit;
  9728. }
  9729. break;
  9730. case PT_MODE_BOOTLOADER:
  9731. /* Do nothing as we are already in the BL */
  9732. tmp_result = PT_ENTER_BL_PASS;
  9733. goto exit;
  9734. default:
  9735. /* Should NEVER get here */
  9736. tmp_result = PT_ENTER_BL_ERROR;
  9737. pt_debug(dev, DL_ERROR, "%s: Unknown mode code\n", __func__);
  9738. goto exit;
  9739. }
  9740. if (!cd->flashless_dut &&
  9741. (mode == PT_MODE_UNKNOWN || mode == PT_MODE_OPERATIONAL)) {
  9742. /*
  9743. * Sending the special "Host Mode" command will instruct the
  9744. * BL to not execute the FW it has loaded into RAM.
  9745. * The command must be sent within a 40ms window from releasing
  9746. * the XRES pin. If the messages is sent too early it will NAK,
  9747. * so keep sending it every 2ms until it is accepted by the BL.
  9748. * A no-flash DUT does not require this command as there is no
  9749. * FW for the BL to load and execute.
  9750. */
  9751. usleep_range(4000, 6000);
  9752. pt_debug(cd->dev, DL_INFO,
  9753. ">>> %s: Write Buffer Size[%d] Stay in BL\n",
  9754. __func__, (int)sizeof(host_mode_cmd));
  9755. pt_pr_buf(cd->dev, DL_DEBUG, host_mode_cmd,
  9756. (int)sizeof(host_mode_cmd), ">>> User CMD");
  9757. rc = 1;
  9758. while (rc && time < 34) {
  9759. rc = pt_adap_write_read_specific(cd, 4,
  9760. host_mode_cmd, NULL);
  9761. usleep_range(1800, 2000);
  9762. time += 2;
  9763. }
  9764. /* Sleep to allow the BL to come up */
  9765. usleep_range(1000, 2000);
  9766. }
  9767. /*
  9768. * To avoid the case that next PIP command can be confused by BL/FW
  9769. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  9770. * followed by "wakeup event" function can lower the failure rate.
  9771. */
  9772. t = wait_event_timeout(cd->wait_q,
  9773. ((cd->startup_status != STARTUP_STATUS_START)
  9774. && (cd->hid_reset_cmd_state == 0)),
  9775. msecs_to_jiffies(300));
  9776. if (IS_TMO(t)) {
  9777. pt_debug(cd->dev, DL_ERROR,
  9778. "%s: TMO waiting for BL sentinel\n", __func__);
  9779. }
  9780. /* Check if device is now in BL mode */
  9781. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9782. pt_debug(dev, DL_INFO, "%s: Mode=%d, Status=0x%04X\n", __func__, mode,
  9783. cd->startup_status);
  9784. if (!rc && mode == PT_MODE_BOOTLOADER) {
  9785. pt_debug(dev, DL_INFO, "%s In bootloader mode now\n", __func__);
  9786. mutex_lock(&cd->system_lock);
  9787. cd->pip2_prot_active = true;
  9788. cd->mode = PT_MODE_BOOTLOADER;
  9789. mutex_unlock(&cd->system_lock);
  9790. tmp_result = PT_ENTER_BL_PASS;
  9791. } else {
  9792. /*
  9793. * If the device doesn't enter BL mode as expected and rc is
  9794. * tested pass by above function pt_pip2_get_mode_sysmode_(),
  9795. * the function should return an error code to indicate this
  9796. * failure PT_ENTER_BL_CONFIRM_FAIL.
  9797. */
  9798. if (!rc)
  9799. rc = -EINVAL;
  9800. tmp_result = PT_ENTER_BL_CONFIRM_FAIL;
  9801. mutex_lock(&cd->system_lock);
  9802. cd->mode = mode;
  9803. mutex_unlock(&cd->system_lock);
  9804. pt_debug(dev, DL_ERROR,
  9805. "%s ERROR: Not in BL as expected", __func__);
  9806. }
  9807. exit:
  9808. if (!cd->flashless_dut && (tmp_result == PT_ENTER_BL_PASS)) {
  9809. /* Check to get (buffered) flash information */
  9810. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  9811. PIP2_CMD_ID_FLASH_INFO, NULL, 0,
  9812. read_buf, &actual_read_len);
  9813. if (!rc) {
  9814. if (read_buf[PIP2_RESP_BODY_OFFSET] == 0) {
  9815. pt_debug(
  9816. dev, DL_WARN,
  9817. "%s Unavailable Manufacturer ID: 0x%02x\n",
  9818. __func__,
  9819. read_buf[PIP2_RESP_BODY_OFFSET]);
  9820. /*
  9821. * If the BL was unable to cache the correct
  9822. * values when entering the first time due to
  9823. * the Flash part not having been powered up
  9824. * long enough, re-enter the BL to trigger the
  9825. * BL to re-attempt to cache the values.
  9826. */
  9827. if (flash_info_retry-- > 0) {
  9828. mode = PT_MODE_UNKNOWN;
  9829. pt_debug(dev, DL_WARN,
  9830. "%s Assume mode to UNKNOWN to enter BL again, retry=%d\n",
  9831. __func__, flash_info_retry);
  9832. goto _retry;
  9833. } else {
  9834. pt_debug(dev, DL_WARN,
  9835. "%s Manufacturer ID Unknown\n",
  9836. __func__);
  9837. tmp_result = PT_ENTER_BL_PASS;
  9838. }
  9839. }
  9840. } else {
  9841. tmp_result = PT_ENTER_BL_GET_FLASH_INFO_FAIL;
  9842. pt_debug(
  9843. dev, DL_ERROR,
  9844. "%s: Failed to send PIP2 READ_FLASH_INFO cmd\n",
  9845. __func__);
  9846. }
  9847. }
  9848. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - %s\n", __func__,
  9849. saved_flashless_auto_bl_mode == PT_ALLOW_AUTO_BL ? "ALLOW" :
  9850. "SUPPRESS");
  9851. cd->flashless_auto_bl = saved_flashless_auto_bl_mode;
  9852. if (result)
  9853. *result = tmp_result;
  9854. return rc;
  9855. }
  9856. /*******************************************************************************
  9857. * FUNCTION: _pt_pip2_file_open
  9858. *
  9859. * SUMMARY: Using the BL PIP2 commands open a file and return the file handle
  9860. *
  9861. * NOTE: The DUT must be in BL mode for this command to work
  9862. *
  9863. * RETURNS:
  9864. * <0 = Error
  9865. * >0 = file handle opened
  9866. *
  9867. * PARAMETERS:
  9868. * *dev - pointer to device structure
  9869. * file_no - PIP2 file number to open
  9870. ******************************************************************************/
  9871. int _pt_pip2_file_open(struct device *dev, u8 file_no)
  9872. {
  9873. int ret = 0;
  9874. u16 status;
  9875. u16 actual_read_len;
  9876. u8 file_handle;
  9877. u8 data[2];
  9878. u8 read_buf[10];
  9879. pt_debug(dev, DL_DEBUG, "%s: OPEN file %d\n", __func__, file_no);
  9880. data[0] = file_no;
  9881. ret = _pt_request_pip2_send_cmd(dev,
  9882. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_OPEN,
  9883. data, 1, read_buf, &actual_read_len);
  9884. if (ret) {
  9885. pt_debug(dev, DL_ERROR,
  9886. "%s ROM BL FILE_OPEN timeout for file = %d\n",
  9887. __func__, file_no);
  9888. return -PIP2_RSP_ERR_NOT_OPEN;
  9889. }
  9890. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  9891. file_handle = read_buf[PIP2_RESP_BODY_OFFSET];
  9892. if (ret || ((status != 0x00) && (status != 0x03)) ||
  9893. (file_handle != file_no)) {
  9894. pt_debug(dev, DL_ERROR,
  9895. "%s ROM BL FILE_OPEN failure: 0x%02X for file = %d\n",
  9896. __func__, status, file_handle);
  9897. return -status;
  9898. }
  9899. return file_handle;
  9900. }
  9901. /*******************************************************************************
  9902. * FUNCTION: _pt_pip2_file_close
  9903. *
  9904. * SUMMARY: Using the BL PIP2 commands close a file
  9905. *
  9906. * NOTE: The DUT must be in BL mode for this command to work
  9907. *
  9908. * RETURNS:
  9909. * <0 = Error
  9910. * >0 = file handle closed
  9911. *
  9912. * PARAMETERS:
  9913. * *dev - pointer to device structure
  9914. * file_handle - handle to the file to be closed
  9915. ******************************************************************************/
  9916. int _pt_pip2_file_close(struct device *dev, u8 file_handle)
  9917. {
  9918. int ret = 0;
  9919. u16 status;
  9920. u16 actual_read_len;
  9921. u8 data[2];
  9922. u8 read_buf[32];
  9923. pt_debug(dev, DL_DEBUG, "%s: CLOSE file %d\n", __func__, file_handle);
  9924. data[0] = file_handle;
  9925. ret = _pt_request_pip2_send_cmd(dev,
  9926. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_CLOSE,
  9927. data, 1, read_buf, &actual_read_len);
  9928. if (ret) {
  9929. pt_debug(dev, DL_ERROR,
  9930. "%s ROM BL FILE_CLOSE timeout for file = %d\n",
  9931. __func__, file_handle);
  9932. return -ETIME;
  9933. }
  9934. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  9935. if (status != 0x00) {
  9936. pt_debug(dev, DL_ERROR,
  9937. "%s ROM BL FILE_CLOSE failure: 0x%02X for file = %d\n",
  9938. __func__, status, file_handle);
  9939. return -status;
  9940. }
  9941. return file_handle;
  9942. }
  9943. /*******************************************************************************
  9944. * FUNCTION: _pt_pip2_file_erase
  9945. *
  9946. * SUMMARY: Using the BL PIP2 commands erase a file
  9947. *
  9948. * NOTE: The DUT must be in BL mode for this command to work
  9949. * NOTE: Some FLASH parts can time out while erasing one or more sectors,
  9950. * one retry is attempted for each sector in a file.
  9951. *
  9952. * RETURNS:
  9953. * <0 = Error
  9954. * >0 = file handle closed
  9955. *
  9956. * PARAMETERS:
  9957. * *dev - pointer to device structure
  9958. * file_handle - handle to the file to be erased
  9959. * *status - PIP2 erase status code
  9960. ******************************************************************************/
  9961. static int _pt_pip2_file_erase(struct device *dev, u8 file_handle, int *status)
  9962. {
  9963. int ret = 0;
  9964. int max_retry = PT_PIP2_MAX_FILE_SIZE/PT_PIP2_FILE_SECTOR_SIZE;
  9965. int retry = 1;
  9966. u16 actual_read_len;
  9967. u8 data[2];
  9968. u8 read_buf[10];
  9969. struct pt_core_data *cd = dev_get_drvdata(dev);
  9970. pt_debug(dev, DL_DEBUG, "%s: ERASE file %d\n", __func__, file_handle);
  9971. data[0] = file_handle;
  9972. data[1] = PIP2_FILE_IOCTL_CODE_ERASE_FILE;
  9973. *status = PIP2_RSP_ERR_TIMEOUT;
  9974. /* Increase waiting time for large file erase */
  9975. mutex_lock(&cd->system_lock);
  9976. cd->pip_cmd_timeout = PT_PIP2_CMD_FILE_ERASE_TIMEOUT;
  9977. mutex_unlock(&cd->system_lock);
  9978. while (*status == PIP2_RSP_ERR_TIMEOUT && retry < max_retry) {
  9979. ret = _pt_request_pip2_send_cmd(dev,
  9980. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  9981. data, 2, read_buf, &actual_read_len);
  9982. if (ret)
  9983. break;
  9984. *status = read_buf[PIP2_RESP_STATUS_OFFSET];
  9985. if (*status == PIP2_RSP_ERR_TIMEOUT) {
  9986. #ifdef TTDL_DIAGNOSTICS
  9987. cd->file_erase_timeout_count++;
  9988. #endif
  9989. pt_debug(dev, DL_WARN,
  9990. "%s: ERASE timeout %d for file = %d\n",
  9991. __func__, retry, file_handle);
  9992. }
  9993. retry++;
  9994. }
  9995. mutex_lock(&cd->system_lock);
  9996. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  9997. mutex_unlock(&cd->system_lock);
  9998. if (ret) {
  9999. pt_debug(dev, DL_ERROR,
  10000. "%s ROM FILE_ERASE cmd failure: %d for file = %d\n",
  10001. __func__, ret, file_handle);
  10002. return -EIO;
  10003. }
  10004. if (*status != 0x00) {
  10005. pt_debug(dev, DL_ERROR,
  10006. "%s ROM FILE_ERASE failure: 0x%02X for file = %d\n",
  10007. __func__, *status, file_handle);
  10008. return -EIO;
  10009. }
  10010. return file_handle;
  10011. }
  10012. /*******************************************************************************
  10013. * FUNCTION: _pt_pip2_file_read
  10014. *
  10015. * SUMMARY: Using the BL PIP2 commands read n bytes from a already opened file
  10016. *
  10017. * NOTE: The DUT must be in BL mode for this command to work
  10018. *
  10019. * RETURNS:
  10020. * <0 = Error
  10021. * >0 = number of bytes read
  10022. *
  10023. * PARAMETERS:
  10024. * *dev - pointer to device structure
  10025. * file_handle - File handle to read from
  10026. * num_bytes - number of bytes to read
  10027. ******************************************************************************/
  10028. int _pt_pip2_file_read(struct device *dev, u8 file_handle, u16 num_bytes,
  10029. u8 *read_buf)
  10030. {
  10031. int ret = 0;
  10032. u16 status;
  10033. u16 actual_read_len;
  10034. u8 data[3];
  10035. data[0] = file_handle;
  10036. data[1] = (num_bytes & 0x00FF);
  10037. data[2] = (num_bytes & 0xFF00) >> 8;
  10038. ret = _pt_request_pip2_send_cmd(dev,
  10039. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_READ,
  10040. data, 3, read_buf, &actual_read_len);
  10041. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10042. if (ret || ((status != 0x00) && (status != 0x03))) {
  10043. pt_debug(dev, DL_ERROR,
  10044. "%s File open failure with error code = %d\n",
  10045. __func__, status);
  10046. return -EPERM;
  10047. }
  10048. ret = num_bytes;
  10049. return ret;
  10050. }
  10051. /*******************************************************************************
  10052. * FUNCTION: _pt_read_us_file
  10053. *
  10054. * SUMMARY: Open a user space file and read 'size' bytes into buf. If size = 0
  10055. * then read the entire file.
  10056. * NOTE: The file size must be less than PT_PIP2_MAX_FILE_SIZE
  10057. *
  10058. * RETURN:
  10059. * 0 = success
  10060. * !0 = failure
  10061. *
  10062. * PARAMETERS:
  10063. * *file_path - pointer to the file path
  10064. * *buf - pointer to the buffer to store the file contents
  10065. * *size - pointer to the size of the file
  10066. ******************************************************************************/
  10067. int _pt_read_us_file(struct device *dev, u8 *file_path, u8 *buf, int *size)
  10068. {
  10069. struct file *filp = NULL;
  10070. struct inode *inode = NULL;
  10071. unsigned int file_len = 0;
  10072. unsigned int read_len = 0;
  10073. mm_segment_t oldfs;
  10074. int rc = 0;
  10075. if (file_path == NULL || buf == NULL) {
  10076. pt_debug(dev, DL_ERROR, "%s: path || buf is NULL.\n", __func__);
  10077. return -EINVAL;
  10078. }
  10079. pt_debug(dev, DL_WARN, "%s: path = %s\n", __func__, file_path);
  10080. oldfs = get_fs();
  10081. set_fs(KERNEL_DS);
  10082. filp = filp_open(file_path, O_RDONLY, 0400);
  10083. if (IS_ERR(filp)) {
  10084. pt_debug(dev, DL_ERROR, "%s: Failed to open %s\n", __func__,
  10085. file_path);
  10086. rc = -ENOENT;
  10087. goto err;
  10088. }
  10089. if (filp->f_op == NULL) {
  10090. pt_debug(dev, DL_ERROR, "%s: File Operation Method Error\n",
  10091. __func__);
  10092. rc = -EINVAL;
  10093. goto exit;
  10094. }
  10095. inode = filp->f_path.dentry->d_inode;
  10096. if (inode == NULL) {
  10097. pt_debug(dev, DL_ERROR, "%s: Get inode from filp failed\n",
  10098. __func__);
  10099. rc = -EINVAL;
  10100. goto exit;
  10101. }
  10102. file_len = i_size_read(inode->i_mapping->host);
  10103. if (file_len == 0) {
  10104. pt_debug(dev, DL_ERROR, "%s: file size error,file_len = %d\n",
  10105. __func__, file_len);
  10106. rc = -EINVAL;
  10107. goto exit;
  10108. }
  10109. if (*size == 0)
  10110. read_len = file_len;
  10111. else
  10112. read_len = *size;
  10113. if (read_len > PT_PIP2_MAX_FILE_SIZE) {
  10114. pt_debug(dev, DL_ERROR, "%s: file size ( %d ) exception.\n",
  10115. __func__, read_len);
  10116. rc = -EINVAL;
  10117. goto exit;
  10118. }
  10119. filp->private_data = inode->i_private;
  10120. if (vfs_read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  10121. pt_debug(dev, DL_ERROR, "%s: file read error.\n", __func__);
  10122. rc = -EINVAL;
  10123. goto exit;
  10124. }
  10125. *size = read_len;
  10126. exit:
  10127. if (filp_close(filp, NULL) != 0)
  10128. pt_debug(dev, DL_ERROR, "%s: file close error.\n", __func__);
  10129. err:
  10130. set_fs(oldfs);
  10131. return rc;
  10132. }
  10133. /*******************************************************************************
  10134. * FUNCTION: _pt_request_pip2_bin_hdr
  10135. *
  10136. * SUMMARY: Read the stored bin file header from Flash or the User Space file
  10137. * in the case of a flashless DUT, and parse the contents
  10138. *
  10139. * RETURNS:
  10140. * 0 = Success
  10141. * !0 = Error condition
  10142. *
  10143. * PARAMETERS:
  10144. * *dev - pointer to device structure
  10145. ******************************************************************************/
  10146. int _pt_request_pip2_bin_hdr(struct device *dev, struct pt_bin_file_hdr *hdr)
  10147. {
  10148. struct pt_core_data *cd = dev_get_drvdata(dev);
  10149. u8 file_handle;
  10150. u8 read_buf[255];
  10151. u8 hdr_len = 0;
  10152. u8 i;
  10153. int bytes_read;
  10154. int read_size;
  10155. int ret = 0;
  10156. int rc = 0;
  10157. bool load_hdr_struct = false;
  10158. if (cd->flashless_dut) {
  10159. read_size = sizeof(read_buf);
  10160. rc = _pt_read_us_file(dev, cd->pip2_us_file_path,
  10161. read_buf, &read_size);
  10162. if (rc) {
  10163. ret = rc;
  10164. pt_debug(dev, DL_ERROR,
  10165. "%s Failed to read fw image from US, rc=%d\n",
  10166. __func__, rc);
  10167. goto exit;
  10168. }
  10169. load_hdr_struct = true;
  10170. hdr_len = read_buf[0];
  10171. i = 0;
  10172. } else {
  10173. if (cd->mode != PT_MODE_BOOTLOADER) {
  10174. ret = -EPERM;
  10175. goto exit;
  10176. }
  10177. /* Open the bin file in Flash */
  10178. pt_debug(dev, DL_INFO, "%s Open File 1\n", __func__);
  10179. file_handle = _pt_pip2_file_open(dev, PIP2_FW_FILE);
  10180. if (file_handle != PIP2_FW_FILE) {
  10181. ret = -ENOENT;
  10182. pt_debug(dev, DL_ERROR,
  10183. "%s Failed to open bin file\n", __func__);
  10184. goto exit;
  10185. }
  10186. /* Read the header length from the file */
  10187. pt_debug(dev, DL_INFO, "%s Read length of header\n", __func__);
  10188. read_size = 1;
  10189. bytes_read = _pt_pip2_file_read(dev, file_handle, read_size,
  10190. read_buf);
  10191. if (bytes_read != read_size) {
  10192. ret = -EX_ERR_FREAD;
  10193. pt_debug(dev, DL_ERROR,
  10194. "%s Failed to bin file header len\n", __func__);
  10195. goto exit_close_file;
  10196. }
  10197. hdr_len = read_buf[PIP2_RESP_BODY_OFFSET];
  10198. if (hdr_len == 0xFF) {
  10199. ret = -EX_ERR_FLEN;
  10200. pt_debug(dev, DL_ERROR,
  10201. "%s Bin header len is invalid\n", __func__);
  10202. goto exit_close_file;
  10203. }
  10204. /* Read the rest of the header from the bin file */
  10205. pt_debug(dev, DL_INFO, "%s Read bin file header\n", __func__);
  10206. memset(read_buf, 0, sizeof(read_buf));
  10207. bytes_read = _pt_pip2_file_read(dev, file_handle, hdr_len,
  10208. read_buf);
  10209. if (bytes_read != hdr_len) {
  10210. ret = -EX_ERR_FREAD;
  10211. pt_debug(dev, DL_ERROR,
  10212. "%s Failed to read bin file\n", __func__);
  10213. goto exit_close_file;
  10214. }
  10215. load_hdr_struct = true;
  10216. exit_close_file:
  10217. /* Close the file */
  10218. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  10219. ret = -EX_ERR_FCLOSE;
  10220. pt_debug(dev, DL_ERROR,
  10221. "%s Failed to close bin file\n", __func__);
  10222. }
  10223. /*
  10224. * The length was already read so subtract 1 to make the rest of
  10225. * the offsets match the spec
  10226. */
  10227. i = PIP2_RESP_BODY_OFFSET - 1;
  10228. }
  10229. if (load_hdr_struct) {
  10230. hdr->length = hdr_len;
  10231. hdr->ttpid = (read_buf[i+1] << 8) | read_buf[i+2];
  10232. hdr->fw_major = (read_buf[i+3]);
  10233. hdr->fw_minor = (read_buf[i+4]);
  10234. hdr->fw_crc = (read_buf[i+5] << 24) |
  10235. (read_buf[i+6] << 16) |
  10236. (read_buf[i+7] << 8) |
  10237. (read_buf[i+8]);
  10238. hdr->fw_rev_ctrl = (read_buf[i+9] << 24) |
  10239. (read_buf[i+10] << 16) |
  10240. (read_buf[i+11] << 8) |
  10241. (read_buf[i+12]);
  10242. hdr->si_rev = (read_buf[i+14] << 8) | (read_buf[i+13]);
  10243. hdr->si_id = (read_buf[i+16] << 8) | (read_buf[i+15]);
  10244. hdr->config_ver = (read_buf[i+17] << 8) | (read_buf[i+18]);
  10245. if (hdr_len >= 22) {
  10246. hdr->hex_file_size = (read_buf[i+19] << 24) |
  10247. (read_buf[i+20] << 16) |
  10248. (read_buf[i+21] << 8) |
  10249. (read_buf[i+22]);
  10250. } else {
  10251. hdr->hex_file_size = 0;
  10252. }
  10253. }
  10254. exit:
  10255. return ret;
  10256. }
  10257. /*******************************************************************************
  10258. * FUNCTION: _pt_pip2_file_get_stats
  10259. *
  10260. * SUMMARY: Using the BL PIP2 commands get file information from an already
  10261. * opened file
  10262. *
  10263. * NOTE: The DUT must be in BL mode for this command to work
  10264. *
  10265. * RETURNS:
  10266. * !0 = Error
  10267. * 0 = Success
  10268. *
  10269. * PARAMETERS:
  10270. * *dev - pointer to device structure
  10271. * file_handle - File handle to read from
  10272. * *address - pointer to store address of file
  10273. * *file_size _ pointer to store size of file
  10274. ******************************************************************************/
  10275. int _pt_pip2_file_get_stats(struct device *dev, u8 file_handle, u32 *address,
  10276. u32 *file_size)
  10277. {
  10278. int ret = 1;
  10279. u16 status;
  10280. u16 actual_read_len;
  10281. u8 data[2];
  10282. u8 read_buf[16];
  10283. data[0] = file_handle;
  10284. data[1] = PIP2_FILE_IOCTL_CODE_FILE_STATS;
  10285. ret = _pt_request_pip2_send_cmd(dev,
  10286. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10287. data, 2, read_buf, &actual_read_len);
  10288. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10289. if (ret || (status != 0x00)) {
  10290. pt_debug(dev, DL_ERROR,
  10291. "%s ROM FILE_STATS failure: 0x%02X for file = %d, ret = %d\n",
  10292. __func__, status, file_handle, ret);
  10293. ret = -EIO;
  10294. goto exit;
  10295. }
  10296. pt_debug(dev, DL_DEBUG,
  10297. "%s --- FILE %d Information ---\n", __func__, file_handle);
  10298. if (address) {
  10299. *address = read_buf[PIP2_RESP_BODY_OFFSET] +
  10300. (read_buf[PIP2_RESP_BODY_OFFSET + 1] << 8) +
  10301. (read_buf[PIP2_RESP_BODY_OFFSET + 2] << 16) +
  10302. (read_buf[PIP2_RESP_BODY_OFFSET + 3] << 24);
  10303. pt_debug(dev, DL_DEBUG, "%s Address: 0x%08x\n",
  10304. __func__, *address);
  10305. }
  10306. if (file_size) {
  10307. *file_size = read_buf[PIP2_RESP_BODY_OFFSET + 4] +
  10308. (read_buf[PIP2_RESP_BODY_OFFSET + 5] << 8) +
  10309. (read_buf[PIP2_RESP_BODY_OFFSET + 6] << 16) +
  10310. (read_buf[PIP2_RESP_BODY_OFFSET + 7] << 24);
  10311. pt_debug(dev, DL_DEBUG, "%s Size : 0x%08x\n",
  10312. __func__, *file_size);
  10313. }
  10314. exit:
  10315. return ret;
  10316. }
  10317. /*******************************************************************************
  10318. * FUNCTION: _pt_pip2_file_seek_offset
  10319. *
  10320. * SUMMARY: Using the BL PIP2 commands seek read/write offset for an already
  10321. * opened file
  10322. *
  10323. * NOTE: The DUT must be in BL mode for this command to work
  10324. * NOTE: File open/erase command can reset the offset
  10325. *
  10326. * RETURNS:
  10327. * !0 = Error
  10328. * 0 = Success
  10329. *
  10330. * PARAMETERS:
  10331. * *dev - pointer to device structure
  10332. * file_handle - File handle to read from
  10333. * read_offset - read offset of file
  10334. * write_offset - write offset of file
  10335. ******************************************************************************/
  10336. int _pt_pip2_file_seek_offset(struct device *dev, u8 file_handle,
  10337. u32 read_offset, u32 write_offset)
  10338. {
  10339. int ret = 1;
  10340. u16 status;
  10341. u16 actual_read_len;
  10342. u8 data[10];
  10343. u8 read_buf[16];
  10344. data[0] = file_handle;
  10345. data[1] = PIP2_FILE_IOCTL_CODE_SEEK_POINTER;
  10346. data[2] = 0xff & read_offset;
  10347. data[3] = 0xff & (read_offset >> 8);
  10348. data[4] = 0xff & (read_offset >> 16);
  10349. data[5] = 0xff & (read_offset >> 24);
  10350. data[6] = 0xff & write_offset;
  10351. data[7] = 0xff & (write_offset >> 8);
  10352. data[8] = 0xff & (write_offset >> 16);
  10353. data[9] = 0xff & (write_offset >> 24);
  10354. ret = _pt_request_pip2_send_cmd(dev,
  10355. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10356. data, 10, read_buf, &actual_read_len);
  10357. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10358. if (ret || (status != 0x00)) {
  10359. pt_debug(dev, DL_ERROR,
  10360. "%s ROM FILE_SEEK failure: 0x%02X for file = %d, ret = %d\n",
  10361. __func__, status, ret, file_handle);
  10362. ret = -EIO;
  10363. }
  10364. return ret;
  10365. }
  10366. /*******************************************************************************
  10367. * FUNCTION: _pt_pip2_file_crc
  10368. *
  10369. * SUMMARY: Using the BL PIP2 commands to calculate CRC for a file or portion of
  10370. * the file.
  10371. *
  10372. * NOTE: The DUT must be in BL mode for this command to work
  10373. * NOTE: This command only can be used for BL version 1.8 or greater.
  10374. * BL version 1.8 added this change according to PGV-173.
  10375. *
  10376. * RETURNS:
  10377. * !0 = Error
  10378. * 0 = Success
  10379. *
  10380. * PARAMETERS:
  10381. * *dev - pointer to device structure
  10382. * file_handle - File handle to read from
  10383. * offset - start offset for CRC calculation
  10384. * length - number of bytes to calculate CRC over
  10385. * read_buf - pointer to the read buffer
  10386. ******************************************************************************/
  10387. int _pt_pip2_file_crc(struct device *dev, u8 file_handle,
  10388. u32 offset, u32 length, u8 *read_buf)
  10389. {
  10390. int rc = 1;
  10391. u16 actual_read_len;
  10392. u8 data[10];
  10393. data[0] = file_handle;
  10394. data[1] = PIP2_FILE_IOCTL_CODE_FILE_CRC;
  10395. data[2] = 0xff & offset;
  10396. data[3] = 0xff & (offset >> 8);
  10397. data[4] = 0xff & (offset >> 16);
  10398. data[5] = 0xff & (offset >> 24);
  10399. data[6] = 0xff & length;
  10400. data[7] = 0xff & (length >> 8);
  10401. data[8] = 0xff & (length >> 16);
  10402. data[9] = 0xff & (length >> 24);
  10403. rc = _pt_request_pip2_send_cmd(dev,
  10404. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10405. data, 10, read_buf, &actual_read_len);
  10406. if (rc)
  10407. pt_debug(dev, DL_ERROR,
  10408. "%s Return FILE_CRC failure, rc = %d\n",
  10409. __func__, rc);
  10410. return rc;
  10411. }
  10412. /*******************************************************************************
  10413. * FUNCTION: pt_pip2_ping_test
  10414. *
  10415. * SUMMARY: BIST type test that uses the PIP2 PING command and ramps up the
  10416. * optional payload from 0 bytes to max_bytes and ensures the PIP2
  10417. * response payload matches what was sent.
  10418. * The max payload size is 247:
  10419. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  10420. *
  10421. * RETURN:
  10422. * 0 = success
  10423. * !0 = failure
  10424. *
  10425. * PARAMETERS:
  10426. * *dev - pointer to device structure
  10427. * *attr - pointer to device attributes
  10428. * *buf - pointer to output buffer
  10429. ******************************************************************************/
  10430. int pt_pip2_ping_test(struct device *dev, int max_bytes, int *last_packet_size)
  10431. {
  10432. u16 actual_read_len;
  10433. u8 *read_buf = NULL;
  10434. u8 *data = NULL;
  10435. int data_offset = PIP2_RESP_STATUS_OFFSET;
  10436. int i = 1;
  10437. int j = 0;
  10438. int rc = 0;
  10439. read_buf = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10440. if (!read_buf)
  10441. goto ping_test_exit;
  10442. data = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10443. if (!data)
  10444. goto ping_test_exit;
  10445. /* Load data payload with an array of walking 1's */
  10446. for (i = 0; i < 255; i++)
  10447. data[i] = 0x01 << (i % 8);
  10448. /* Send 'max_bytes' PING cmds using 'i' bytes as payload for each */
  10449. for (i = 0; i <= max_bytes; i++) {
  10450. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10451. PIP2_CMD_ID_PING, data, i, read_buf,
  10452. &actual_read_len);
  10453. if (rc) {
  10454. pt_debug(dev, DL_ERROR,
  10455. "%s: PING failed with %d byte payload\n",
  10456. __func__, i);
  10457. break;
  10458. }
  10459. /* Verify data returned matches data sent */
  10460. for (j = 0; j < i; j++) {
  10461. if (read_buf[data_offset + j] != data[j]) {
  10462. pt_debug(dev, DL_DEBUG,
  10463. "%s: PING packet size %d: sent[%d]=0x%02X recv[%d]=0x%02X\n",
  10464. __func__, i, j, data[j], j,
  10465. read_buf[data_offset + j]);
  10466. goto ping_test_exit;
  10467. }
  10468. }
  10469. }
  10470. ping_test_exit:
  10471. *last_packet_size = i - 1;
  10472. kfree(read_buf);
  10473. kfree(data);
  10474. return rc;
  10475. }
  10476. /*******************************************************************************
  10477. * FUNCTION: _pt_ic_parse_input_hex
  10478. *
  10479. * SUMMARY: Parse a char data array as space delimited hex values into
  10480. * an int array.
  10481. *
  10482. * NOTE: _pt_ic_parse_input() function may have similar work while the type of
  10483. * buffer to store data is "u32". This function is still needed by the
  10484. * "command" sysfs node because the buffer type to store data is "u8".
  10485. *
  10486. * RETURN: Length of parsed data
  10487. *
  10488. * PARAMETERS:
  10489. * *dev - pointer to device structure
  10490. * *buf - pointer to buffer that holds the input array to parse
  10491. * buf_size - size of buf
  10492. * *ic_buf - pointer to array to store parsed data
  10493. * ic_buf_size - max size of ic_buf
  10494. ******************************************************************************/
  10495. static int _pt_ic_parse_input_hex(struct device *dev, const char *buf,
  10496. size_t buf_size, u8 *ic_buf, size_t ic_buf_size)
  10497. {
  10498. const char *pbuf = buf;
  10499. unsigned long value;
  10500. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10501. u32 i = 0;
  10502. u32 j;
  10503. int last = 0;
  10504. int ret;
  10505. pt_debug(dev, DL_DEBUG,
  10506. "%s: pbuf=%p buf=%p size=%zu %s=%d buf=%s\n",
  10507. __func__, pbuf, buf, buf_size, "scan buf size",
  10508. PT_MAX_PIP2_MSG_SIZE, buf);
  10509. while (pbuf <= (buf + buf_size)) {
  10510. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10511. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10512. __func__, "Max cmd size exceeded", i,
  10513. PT_MAX_PIP2_MSG_SIZE);
  10514. return -EINVAL;
  10515. }
  10516. if (i >= ic_buf_size) {
  10517. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10518. __func__, "Buffer size exceeded", i,
  10519. ic_buf_size);
  10520. return -EINVAL;
  10521. }
  10522. while (((*pbuf == ' ') || (*pbuf == ','))
  10523. && (pbuf < (buf + buf_size))) {
  10524. last = *pbuf;
  10525. pbuf++;
  10526. }
  10527. if (pbuf >= (buf + buf_size))
  10528. break;
  10529. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10530. if ((last == ',') && (*pbuf == ',')) {
  10531. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10532. __func__, "Invalid data format.");
  10533. return -EINVAL;
  10534. }
  10535. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10536. && (pbuf < (buf + buf_size))
  10537. && (*pbuf != ' ')
  10538. && (*pbuf != ','); j++) {
  10539. last = *pbuf;
  10540. scan_buf[j] = *pbuf++;
  10541. }
  10542. ret = kstrtoul(scan_buf, 16, &value);
  10543. if (ret < 0) {
  10544. pt_debug(dev, DL_ERROR,
  10545. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10546. "Invalid data format. ", scan_buf,
  10547. "Use \"0xHH,...,0xHH\"", " instead.",
  10548. i, ret);
  10549. return ret;
  10550. }
  10551. ic_buf[i] = value;
  10552. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx",
  10553. __func__, i, value);
  10554. i++;
  10555. }
  10556. return i;
  10557. }
  10558. /*******************************************************************************
  10559. * FUNCTION: _pt_ic_parse_input
  10560. *
  10561. * SUMMARY: Parse user sysfs input data as a space or comma delimited list of
  10562. * hex values or dec values into an int array with the following rules:
  10563. * 1) Hex values must have a "0x" prefix for each element or the first element
  10564. * only
  10565. * 2) Dec values do not have any prefix
  10566. * 3) It is not allowed to have a mix of dec and hex values in the user input
  10567. * string
  10568. *
  10569. * RETURN: Number of values parsed
  10570. *
  10571. * PARAMETERS:
  10572. * *dev - pointer to device structure
  10573. * *buf - pointer to buffer that holds the input array to parse
  10574. * buf_size - size of buf
  10575. * *out_buf - pointer to array to store parsed data
  10576. * out_buf_size - max size of buffer to be stored
  10577. ******************************************************************************/
  10578. static int _pt_ic_parse_input(struct device *dev,
  10579. const char *buf, size_t buf_size,
  10580. u32 *out_buf, size_t out_buf_size)
  10581. {
  10582. const char *pbuf = buf;
  10583. unsigned long value;
  10584. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10585. u32 i = 0;
  10586. u32 j;
  10587. int last = 0;
  10588. int ret = 0;
  10589. u8 str_base = 0;
  10590. pt_debug(dev, DL_DEBUG,
  10591. "%s: in_buf_size=%zu out_buf_size=%zu %s=%d buf=%s\n",
  10592. __func__, buf_size, out_buf_size, "scan buf size",
  10593. PT_MAX_PIP2_MSG_SIZE, buf);
  10594. while (pbuf <= (buf + buf_size)) {
  10595. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10596. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10597. __func__, "Max cmd size exceeded", i,
  10598. PT_MAX_PIP2_MSG_SIZE);
  10599. ret = -EINVAL;
  10600. goto error;
  10601. }
  10602. if (i >= out_buf_size) {
  10603. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10604. __func__, "Buffer size exceeded", i,
  10605. out_buf_size);
  10606. ret = -EINVAL;
  10607. goto error;
  10608. }
  10609. while (((*pbuf == ' ') || (*pbuf == ','))
  10610. && (pbuf < (buf + buf_size))) {
  10611. last = *pbuf;
  10612. pbuf++;
  10613. }
  10614. if (pbuf >= (buf + buf_size))
  10615. break;
  10616. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10617. if ((last == ',') && (*pbuf == ',')) {
  10618. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10619. __func__, "Invalid data format.");
  10620. ret = -EINVAL;
  10621. goto error;
  10622. }
  10623. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10624. && (pbuf < (buf + buf_size))
  10625. && (*pbuf != ' ')
  10626. && (*pbuf != ','); j++) {
  10627. last = *pbuf;
  10628. scan_buf[j] = *pbuf++;
  10629. }
  10630. if (i == 0) {
  10631. if ((strncmp(scan_buf, "0x", 2) == 0) ||
  10632. (strncmp(scan_buf, "0X", 2) == 0))
  10633. str_base = 16;
  10634. else
  10635. str_base = 10;
  10636. } else {
  10637. if (((strncmp(scan_buf, "0x", 2) == 0) ||
  10638. (strncmp(scan_buf, "0X", 2) == 0)) &&
  10639. (str_base == 10)) {
  10640. pt_debug(dev, DL_ERROR,
  10641. "%s: Decimal and Heximal data mixed\n",
  10642. __func__);
  10643. ret = -EINVAL;
  10644. goto error;
  10645. }
  10646. }
  10647. ret = kstrtoul(scan_buf, str_base, &value);
  10648. if (ret < 0) {
  10649. pt_debug(dev, DL_ERROR,
  10650. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10651. "Invalid data format. ", scan_buf,
  10652. "Use \"0xHH,...,0xHH\" or \"DD DD DD ... DD\"",
  10653. " instead.", i, ret);
  10654. goto error;
  10655. }
  10656. out_buf[i] = value;
  10657. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx(%lu)",
  10658. __func__, i, value, value);
  10659. i++;
  10660. }
  10661. ret = i;
  10662. error:
  10663. return ret;
  10664. }
  10665. #ifdef TTHE_TUNER_SUPPORT
  10666. /*******************************************************************************
  10667. * FUNCTION: tthe_debugfs_open
  10668. *
  10669. * SUMMARY: Open method for tthe_tuner debugfs node. On some hosts the size of
  10670. * PT_MAX_PRBUF_SIZE (equal to PAGE_SIZE) is not large enough to handle
  10671. * printing a large number of fingers and sensor data without overflowing
  10672. * the buffer. tthe_tuner needs ~4K and so the buffer is sized to some
  10673. * even multiple of PAGE_SIZE
  10674. *
  10675. * RETURN:
  10676. * 0 = success
  10677. * !0 = failure
  10678. *
  10679. * PARAMETERS:
  10680. * *inode - file inode number
  10681. * *filp - file pointer to debugfs file
  10682. ******************************************************************************/
  10683. static int tthe_debugfs_open(struct inode *inode, struct file *filp)
  10684. {
  10685. struct pt_core_data *cd = inode->i_private;
  10686. u32 buf_size = PT_MAX_PRBUF_SIZE;
  10687. filp->private_data = inode->i_private;
  10688. if (cd->tthe_buf)
  10689. return -EBUSY;
  10690. while (buf_size < 4096)
  10691. buf_size = buf_size << 1;
  10692. pt_debug(cd->dev, DL_INFO, "%s:PT_MAX_BRBUF_SIZE=%d buf_size=%d\n",
  10693. __func__, (int)PT_MAX_PRBUF_SIZE, (int)buf_size);
  10694. cd->tthe_buf_size = buf_size;
  10695. cd->tthe_buf = kzalloc(cd->tthe_buf_size, GFP_KERNEL);
  10696. if (!cd->tthe_buf)
  10697. return -ENOMEM;
  10698. return 0;
  10699. }
  10700. /*******************************************************************************
  10701. * FUNCTION: tthe_debugfs_close
  10702. *
  10703. * SUMMARY: Close method for tthe_tuner debugfs node.
  10704. *
  10705. * RETURN:
  10706. * 0 = success
  10707. * !0 = failure
  10708. *
  10709. * PARAMETERS:
  10710. * *inode - file inode number
  10711. * *filp - file pointer to debugfs file
  10712. ******************************************************************************/
  10713. static int tthe_debugfs_close(struct inode *inode, struct file *filp)
  10714. {
  10715. struct pt_core_data *cd = filp->private_data;
  10716. filp->private_data = NULL;
  10717. kfree(cd->tthe_buf);
  10718. cd->tthe_buf = NULL;
  10719. return 0;
  10720. }
  10721. /*******************************************************************************
  10722. * FUNCTION: tthe_debugfs_read
  10723. *
  10724. * SUMMARY: Read method for tthe_tuner debugfs node. This function prints
  10725. * tthe_buf to user buffer.
  10726. *
  10727. * RETURN: Size of debugfs data print
  10728. *
  10729. * PARAMETERS:
  10730. * *filp - file pointer to debugfs file
  10731. * *buf - the user space buffer to read to
  10732. * count - the maximum number of bytes to read
  10733. * *ppos - the current position in the buffer
  10734. ******************************************************************************/
  10735. static ssize_t tthe_debugfs_read(struct file *filp, char __user *buf,
  10736. size_t count, loff_t *ppos)
  10737. {
  10738. struct pt_core_data *cd = filp->private_data;
  10739. int size;
  10740. int ret;
  10741. static int partial_read;
  10742. wait_event_interruptible(cd->wait_q,
  10743. cd->tthe_buf_len != 0 || cd->tthe_exit);
  10744. mutex_lock(&cd->tthe_lock);
  10745. if (cd->tthe_exit) {
  10746. mutex_unlock(&cd->tthe_lock);
  10747. return 0;
  10748. }
  10749. if (count > cd->tthe_buf_len)
  10750. size = cd->tthe_buf_len;
  10751. else
  10752. size = count;
  10753. if (!size) {
  10754. mutex_unlock(&cd->tthe_lock);
  10755. return 0;
  10756. }
  10757. if (partial_read) {
  10758. ret = copy_to_user(buf, cd->tthe_buf + partial_read, size);
  10759. partial_read = 0;
  10760. } else {
  10761. ret = copy_to_user(buf, cd->tthe_buf, size);
  10762. }
  10763. if (size == count)
  10764. partial_read = count;
  10765. if (ret == size)
  10766. return -EFAULT;
  10767. size -= ret;
  10768. cd->tthe_buf_len -= size;
  10769. mutex_unlock(&cd->tthe_lock);
  10770. *ppos += size;
  10771. return size;
  10772. }
  10773. static const struct file_operations tthe_debugfs_fops = {
  10774. .open = tthe_debugfs_open,
  10775. .release = tthe_debugfs_close,
  10776. .read = tthe_debugfs_read,
  10777. };
  10778. #endif
  10779. static struct pt_core_nonhid_cmd _pt_core_nonhid_cmd = {
  10780. .start_bl = _pt_request_pip_start_bl,
  10781. .suspend_scanning = _pt_request_pip_suspend_scanning,
  10782. .resume_scanning = _pt_request_pip_resume_scanning,
  10783. .get_param = _pt_request_pip_get_param,
  10784. .set_param = _pt_request_pip_set_param,
  10785. .verify_cfg_block_crc = _pt_request_pip_verify_config_block_crc,
  10786. .get_config_row_size = _pt_request_pip_get_config_row_size,
  10787. .get_data_structure = _pt_request_pip_get_data_structure,
  10788. .run_selftest = _pt_request_pip_run_selftest,
  10789. .get_selftest_result = _pt_request_pip_get_selftest_result,
  10790. .load_self_test_param = _pt_request_pip_load_self_test_param,
  10791. .calibrate_idacs = _pt_request_pip_calibrate_idacs,
  10792. .calibrate_ext = _pt_request_pip_calibrate_ext,
  10793. .initialize_baselines = _pt_request_pip_initialize_baselines,
  10794. .exec_panel_scan = _pt_request_pip_exec_panel_scan,
  10795. .retrieve_panel_scan = _pt_request_pip_retrieve_panel_scan,
  10796. .read_data_block = _pt_request_pip_read_data_block,
  10797. .write_data_block = _pt_request_pip_write_data_block,
  10798. .user_cmd = _pt_request_pip_user_cmd,
  10799. .get_bl_info = _pt_request_pip_bl_get_information,
  10800. .initiate_bl = _pt_request_pip_bl_initiate_bl,
  10801. .launch_app = _pt_request_pip_launch_app,
  10802. .prog_and_verify = _pt_request_pip_bl_program_and_verify,
  10803. .verify_app_integrity = _pt_request_pip_bl_verify_app_integrity,
  10804. .get_panel_id = _pt_request_pip_bl_get_panel_id,
  10805. .pip2_send_cmd = _pt_request_pip2_send_cmd,
  10806. .pip2_send_cmd_no_int = _pt_pip2_send_cmd_no_int,
  10807. .pip2_file_open = _pt_pip2_file_open,
  10808. .pip2_file_close = _pt_pip2_file_close,
  10809. .pip2_file_erase = _pt_pip2_file_erase,
  10810. .read_us_file = _pt_read_us_file,
  10811. .manage_cal_data = _pt_manage_local_cal_data,
  10812. .calc_crc = crc_ccitt_calculate,
  10813. #ifdef TTDL_DIAGNOSTICS
  10814. .pip2_file_read = _pt_pip2_file_read,
  10815. .pip2_file_seek_offset = _pt_pip2_file_seek_offset,
  10816. .pip2_file_get_stats = _pt_pip2_file_get_stats,
  10817. .pip2_file_crc = _pt_pip2_file_crc,
  10818. #endif
  10819. };
  10820. static struct pt_core_commands _pt_core_commands = {
  10821. .subscribe_attention = _pt_subscribe_attention,
  10822. .unsubscribe_attention = _pt_unsubscribe_attention,
  10823. .request_exclusive = _pt_request_exclusive,
  10824. .release_exclusive = _pt_release_exclusive,
  10825. .request_reset = _pt_request_reset,
  10826. .request_pip2_launch_app = _pt_request_pip2_launch_app,
  10827. .request_enum = _pt_request_enum,
  10828. .request_sysinfo = _pt_request_sysinfo,
  10829. .request_loader_pdata = _pt_request_loader_pdata,
  10830. .request_stop_wd = _pt_request_stop_wd,
  10831. .request_start_wd = _pt_request_start_wd,
  10832. .request_get_mode = _pt_request_get_mode,
  10833. .request_active_pip_prot = _pt_request_active_pip_protocol,
  10834. .request_pip2_get_mode_sysmode = _pt_request_pip2_get_mode_sysmode,
  10835. .request_pip2_enter_bl = _pt_request_pip2_enter_bl,
  10836. .request_pip2_bin_hdr = _pt_request_pip2_bin_hdr,
  10837. .request_dut_generation = _pt_request_dut_generation,
  10838. .request_hw_version = _pt_request_hw_version,
  10839. .parse_sysfs_input = _pt_ic_parse_input,
  10840. #ifdef TTHE_TUNER_SUPPORT
  10841. .request_tthe_print = _pt_request_tthe_print,
  10842. #endif
  10843. #ifdef TTDL_DIAGNOSTICS
  10844. .request_toggle_err_gpio = _pt_request_toggle_err_gpio,
  10845. #endif
  10846. .nonhid_cmd = &_pt_core_nonhid_cmd,
  10847. .request_get_fw_mode = _pt_request_get_fw_sys_mode,
  10848. };
  10849. struct pt_core_commands *pt_get_commands(void)
  10850. {
  10851. return &_pt_core_commands;
  10852. }
  10853. EXPORT_SYMBOL_GPL(pt_get_commands);
  10854. static DEFINE_MUTEX(core_list_lock);
  10855. static LIST_HEAD(core_list);
  10856. static DEFINE_MUTEX(module_list_lock);
  10857. static LIST_HEAD(module_list);
  10858. static int core_number;
  10859. /*******************************************************************************
  10860. * FUNCTION: pt_probe_module
  10861. *
  10862. * SUMMARY: Add the module pointer to module_node and call the probe pointer.
  10863. *
  10864. * RETURN:
  10865. * 0 = success
  10866. * !0 = failure
  10867. *
  10868. * PARAMETERS:
  10869. * *cd - pointer to core data
  10870. * *module - pointer to module structure
  10871. ******************************************************************************/
  10872. static int pt_probe_module(struct pt_core_data *cd,
  10873. struct pt_module *module)
  10874. {
  10875. struct module_node *module_node;
  10876. int rc = 0;
  10877. module_node = kzalloc(sizeof(*module_node), GFP_KERNEL);
  10878. if (!module_node)
  10879. return -ENOMEM;
  10880. module_node->module = module;
  10881. mutex_lock(&cd->module_list_lock);
  10882. list_add(&module_node->node, &cd->module_list);
  10883. mutex_unlock(&cd->module_list_lock);
  10884. rc = module->probe(cd->dev, &module_node->data);
  10885. if (rc) {
  10886. /*
  10887. * Remove from the list when probe fails
  10888. * in order not to call release
  10889. */
  10890. mutex_lock(&cd->module_list_lock);
  10891. list_del(&module_node->node);
  10892. mutex_unlock(&cd->module_list_lock);
  10893. kfree(module_node);
  10894. goto exit;
  10895. }
  10896. exit:
  10897. return rc;
  10898. }
  10899. /*******************************************************************************
  10900. * FUNCTION: pt_release_module
  10901. *
  10902. * SUMMARY: Call the release pointer and remove the module pointer from
  10903. * module_list.
  10904. *
  10905. * PARAMETERS:
  10906. * *cd - pointer to core data
  10907. * *module - pointer to module structure
  10908. ******************************************************************************/
  10909. static void pt_release_module(struct pt_core_data *cd,
  10910. struct pt_module *module)
  10911. {
  10912. struct module_node *m, *m_n;
  10913. mutex_lock(&cd->module_list_lock);
  10914. list_for_each_entry_safe(m, m_n, &cd->module_list, node)
  10915. if (m->module == module) {
  10916. module->release(cd->dev, m->data);
  10917. list_del(&m->node);
  10918. kfree(m);
  10919. break;
  10920. }
  10921. mutex_unlock(&cd->module_list_lock);
  10922. }
  10923. /*******************************************************************************
  10924. * FUNCTION: pt_probe_modules
  10925. *
  10926. * SUMMARY: Iterate module_list and probe each module.
  10927. *
  10928. * PARAMETERS:
  10929. * *cd - pointer to core data
  10930. ******************************************************************************/
  10931. static void pt_probe_modules(struct pt_core_data *cd)
  10932. {
  10933. struct pt_module *m;
  10934. int rc = 0;
  10935. mutex_lock(&module_list_lock);
  10936. list_for_each_entry(m, &module_list, node) {
  10937. pt_debug(cd->dev, DL_ERROR, "%s: Probe module %s\n",
  10938. __func__, m->name);
  10939. rc = pt_probe_module(cd, m);
  10940. if (rc)
  10941. pt_debug(cd->dev, DL_ERROR,
  10942. "%s: Probe fails for module %s\n",
  10943. __func__, m->name);
  10944. }
  10945. mutex_unlock(&module_list_lock);
  10946. }
  10947. /*******************************************************************************
  10948. * FUNCTION: pt_release_modules
  10949. *
  10950. * SUMMARY: Iterate module_list and remove each module.
  10951. *
  10952. * PARAMETERS:
  10953. * *cd - pointer to core data
  10954. ******************************************************************************/
  10955. static void pt_release_modules(struct pt_core_data *cd)
  10956. {
  10957. struct pt_module *m;
  10958. mutex_lock(&module_list_lock);
  10959. list_for_each_entry(m, &module_list, node)
  10960. pt_release_module(cd, m);
  10961. mutex_unlock(&module_list_lock);
  10962. }
  10963. /*******************************************************************************
  10964. * FUNCTION: pt_get_core_data
  10965. *
  10966. * SUMMARY: Iterate core_list and get core data.
  10967. *
  10968. * RETURN:
  10969. * pointer to core data or null pointer if fail
  10970. *
  10971. * PARAMETERS:
  10972. * *id - pointer to core id
  10973. ******************************************************************************/
  10974. struct pt_core_data *pt_get_core_data(char *id)
  10975. {
  10976. struct pt_core_data *d;
  10977. list_for_each_entry(d, &core_list, node)
  10978. if (!strncmp(d->core_id, id, 20))
  10979. return d;
  10980. return NULL;
  10981. }
  10982. EXPORT_SYMBOL_GPL(pt_get_core_data);
  10983. /*******************************************************************************
  10984. * FUNCTION: pt_add_core
  10985. *
  10986. * SUMMARY: Add core data to the core_list.
  10987. *
  10988. * PARAMETERS:
  10989. * *dev - pointer to device structure
  10990. ******************************************************************************/
  10991. static void pt_add_core(struct device *dev)
  10992. {
  10993. struct pt_core_data *d;
  10994. struct pt_core_data *cd = dev_get_drvdata(dev);
  10995. mutex_lock(&core_list_lock);
  10996. list_for_each_entry(d, &core_list, node)
  10997. if (d->dev == dev)
  10998. goto unlock;
  10999. list_add(&cd->node, &core_list);
  11000. unlock:
  11001. mutex_unlock(&core_list_lock);
  11002. }
  11003. /*******************************************************************************
  11004. * FUNCTION: pt_del_core
  11005. *
  11006. * SUMMARY: Remove core data from the core_list.
  11007. *
  11008. * PARAMETERS:
  11009. * *dev - pointer to device structure
  11010. ******************************************************************************/
  11011. static void pt_del_core(struct device *dev)
  11012. {
  11013. struct pt_core_data *d, *d_n;
  11014. mutex_lock(&core_list_lock);
  11015. list_for_each_entry_safe(d, d_n, &core_list, node)
  11016. if (d->dev == dev) {
  11017. list_del(&d->node);
  11018. goto unlock;
  11019. }
  11020. unlock:
  11021. mutex_unlock(&core_list_lock);
  11022. }
  11023. /*******************************************************************************
  11024. * FUNCTION: pt_register_module
  11025. *
  11026. * SUMMARY: Register the module to module_list and probe the module for each
  11027. * core.
  11028. *
  11029. * RETURN:
  11030. * 0 = success
  11031. * !0 = failure
  11032. *
  11033. * PARAMETERS:
  11034. * *module - pointer to module structure
  11035. ******************************************************************************/
  11036. int pt_register_module(struct pt_module *module)
  11037. {
  11038. struct pt_module *m;
  11039. struct pt_core_data *cd;
  11040. int rc = 0;
  11041. if (!module || !module->probe || !module->release)
  11042. return -EINVAL;
  11043. mutex_lock(&module_list_lock);
  11044. list_for_each_entry(m, &module_list, node)
  11045. if (m == module) {
  11046. rc = -EEXIST;
  11047. goto unlock;
  11048. }
  11049. list_add(&module->node, &module_list);
  11050. /* Probe the module for each core */
  11051. mutex_lock(&core_list_lock);
  11052. list_for_each_entry(cd, &core_list, node)
  11053. pt_probe_module(cd, module);
  11054. mutex_unlock(&core_list_lock);
  11055. unlock:
  11056. mutex_unlock(&module_list_lock);
  11057. return rc;
  11058. }
  11059. EXPORT_SYMBOL_GPL(pt_register_module);
  11060. /*******************************************************************************
  11061. * FUNCTION: pt_unregister_module
  11062. *
  11063. * SUMMARY: Release the module for each core and remove the module from
  11064. * module_list.
  11065. *
  11066. * RETURN:
  11067. * 0 = success
  11068. * !0 = failure
  11069. *
  11070. * PARAMETERS:
  11071. * *module - pointer to module structure
  11072. ******************************************************************************/
  11073. void pt_unregister_module(struct pt_module *module)
  11074. {
  11075. struct pt_module *m, *m_n;
  11076. struct pt_core_data *cd;
  11077. if (!module)
  11078. return;
  11079. mutex_lock(&module_list_lock);
  11080. /* Release the module for each core */
  11081. mutex_lock(&core_list_lock);
  11082. list_for_each_entry(cd, &core_list, node)
  11083. pt_release_module(cd, module);
  11084. mutex_unlock(&core_list_lock);
  11085. list_for_each_entry_safe(m, m_n, &module_list, node)
  11086. if (m == module) {
  11087. list_del(&m->node);
  11088. break;
  11089. }
  11090. mutex_unlock(&module_list_lock);
  11091. }
  11092. EXPORT_SYMBOL_GPL(pt_unregister_module);
  11093. /*******************************************************************************
  11094. * FUNCTION: pt_get_module_data
  11095. *
  11096. * SUMMARY: Get module data from module_node by module_list.
  11097. *
  11098. * RETURN:
  11099. * pointer to module data
  11100. *
  11101. * PARAMETERS:
  11102. * *dev - pointer to device structure
  11103. * *module - pointer to module structure
  11104. ******************************************************************************/
  11105. void *pt_get_module_data(struct device *dev, struct pt_module *module)
  11106. {
  11107. struct pt_core_data *cd = dev_get_drvdata(dev);
  11108. struct module_node *m;
  11109. void *data = NULL;
  11110. mutex_lock(&cd->module_list_lock);
  11111. list_for_each_entry(m, &cd->module_list, node)
  11112. if (m->module == module) {
  11113. data = m->data;
  11114. break;
  11115. }
  11116. mutex_unlock(&cd->module_list_lock);
  11117. return data;
  11118. }
  11119. EXPORT_SYMBOL(pt_get_module_data);
  11120. #ifdef CONFIG_HAS_EARLYSUSPEND
  11121. /*******************************************************************************
  11122. * FUNCTION: pt_early_suspend
  11123. *
  11124. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_SUSPEND"
  11125. * attention list.
  11126. *
  11127. * PARAMETERS:
  11128. * *h - pointer to early_suspend structure
  11129. ******************************************************************************/
  11130. static void pt_early_suspend(struct early_suspend *h)
  11131. {
  11132. struct pt_core_data *cd =
  11133. container_of(h, struct pt_core_data, es);
  11134. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11135. }
  11136. /*******************************************************************************
  11137. * FUNCTION: pt_late_resume
  11138. *
  11139. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_RESUME"
  11140. * attention list.
  11141. *
  11142. * PARAMETERS:
  11143. * *h - pointer to early_suspend structure
  11144. ******************************************************************************/
  11145. static void pt_late_resume(struct early_suspend *h)
  11146. {
  11147. struct pt_core_data *cd =
  11148. container_of(h, struct pt_core_data, es);
  11149. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11150. }
  11151. /*******************************************************************************
  11152. * FUNCTION: pt_setup_early_suspend
  11153. *
  11154. * SUMMARY: Register early/suspend function to the system.
  11155. *
  11156. * PARAMETERS:
  11157. * *cd - pointer to core data
  11158. ******************************************************************************/
  11159. static void pt_setup_early_suspend(struct pt_core_data *cd)
  11160. {
  11161. cd->es.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  11162. cd->es.suspend = pt_early_suspend;
  11163. cd->es.resume = pt_late_resume;
  11164. register_early_suspend(&cd->es);
  11165. }
  11166. #elif defined(CONFIG_FB)
  11167. /*******************************************************************************
  11168. * FUNCTION: fb_notifier_callback
  11169. *
  11170. * SUMMARY: Call back function for FrameBuffer notifier to allow to call
  11171. * resume/suspend attention list.
  11172. *
  11173. * RETURN:
  11174. * 0 = success
  11175. *
  11176. * PARAMETERS:
  11177. * *self - pointer to notifier_block structure
  11178. * event - event type of fb notifier
  11179. * *data - pointer to fb_event structure
  11180. ******************************************************************************/
  11181. static int fb_notifier_callback(struct notifier_block *self,
  11182. unsigned long event, void *data)
  11183. {
  11184. struct pt_core_data *cd =
  11185. container_of(self, struct pt_core_data, fb_notifier);
  11186. struct fb_event *evdata = data;
  11187. int *blank;
  11188. if (event != FB_EVENT_BLANK || !evdata)
  11189. goto exit;
  11190. blank = evdata->data;
  11191. if (*blank == FB_BLANK_UNBLANK) {
  11192. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11193. if (cd->fb_state != FB_ON) {
  11194. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11195. #if defined(CONFIG_PM_SLEEP)
  11196. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11197. pt_core_resume_(cd->dev);
  11198. #endif
  11199. cd->fb_state = FB_ON;
  11200. }
  11201. } else if (*blank == FB_BLANK_POWERDOWN) {
  11202. pt_debug(cd->dev, DL_INFO, "%s: POWERDOWN!\n", __func__);
  11203. if (cd->fb_state != FB_OFF) {
  11204. #if defined(CONFIG_PM_SLEEP)
  11205. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11206. pt_core_suspend_(cd->dev);
  11207. #endif
  11208. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11209. cd->fb_state = FB_OFF;
  11210. }
  11211. }
  11212. exit:
  11213. return 0;
  11214. }
  11215. /*******************************************************************************
  11216. * FUNCTION: pt_setup_fb_notifier
  11217. *
  11218. * SUMMARY: Set up call back function into fb notifier.
  11219. *
  11220. * PARAMETERS:
  11221. * *cd - pointer to core data
  11222. ******************************************************************************/
  11223. static void pt_setup_fb_notifier(struct pt_core_data *cd)
  11224. {
  11225. int rc = 0;
  11226. cd->fb_state = FB_ON;
  11227. cd->fb_notifier.notifier_call = fb_notifier_callback;
  11228. rc = fb_register_client(&cd->fb_notifier);
  11229. if (rc)
  11230. pt_debug(cd->dev, DL_ERROR,
  11231. "Unable to register fb_notifier: %d\n", rc);
  11232. }
  11233. #endif
  11234. /*******************************************************************************
  11235. * FUNCTION: pt_watchdog_work
  11236. *
  11237. * SUMMARY: This is where the watchdog work is done except if the DUT is
  11238. * sleeping then this function simply returns. If the DUT is awake the
  11239. * first thing is to ensure the IRQ is not stuck asserted meaning that
  11240. * somehow a response is waiting on the DUT that has not been read. If
  11241. * this occurs the message is simply consumed. If or once the IRQ is
  11242. * cleared, a PIP PING message is sent to the DUT and if the response
  11243. * is received the watchdog succeeds and exits, if no response is seen
  11244. * a startup is queued unless the maximum number of startups have already
  11245. * been attempted, in that case a BL is attempted.
  11246. *
  11247. * NOTE: pt_stop_wd_timer() cannot be called within the context of this
  11248. * work thread
  11249. *
  11250. * RETURN: void
  11251. *
  11252. * PARAMETERS:
  11253. * *work - pointer to a work structure for the watchdog work queue
  11254. ******************************************************************************/
  11255. static void pt_watchdog_work(struct work_struct *work)
  11256. {
  11257. int rc = 0;
  11258. struct pt_core_data *cd = container_of(work,
  11259. struct pt_core_data, watchdog_work);
  11260. /*
  11261. * if found the current sleep_state is SS_SLEEPING
  11262. * then no need to request_exclusive, directly return
  11263. */
  11264. if (cd->sleep_state == SS_SLEEPING)
  11265. return;
  11266. #ifdef TTDL_DIAGNOSTICS
  11267. cd->watchdog_count++;
  11268. #endif /* TTDL_DIAGNOSTICS */
  11269. /*
  11270. * The first WD interval was extended to allow DDI to come up.
  11271. * If the WD interval is not the default then adjust timer to the
  11272. * current setting. The user can override value with drv_debug sysfs.
  11273. */
  11274. if (cd->watchdog_interval != PT_WATCHDOG_TIMEOUT) {
  11275. mod_timer_pending(&cd->watchdog_timer, jiffies +
  11276. msecs_to_jiffies(cd->watchdog_interval));
  11277. }
  11278. if (pt_check_irq_asserted(cd)) {
  11279. #ifdef TTDL_DIAGNOSTICS
  11280. cd->watchdog_irq_stuck_count++;
  11281. pt_toggle_err_gpio(cd, PT_ERR_GPIO_IRQ_STUCK);
  11282. #endif /* TTDL_DIAGNOSTICS */
  11283. pt_debug(cd->dev, DL_ERROR,
  11284. "%s: TTDL WD found IRQ asserted, attempt to clear\n",
  11285. __func__);
  11286. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  11287. }
  11288. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11289. if (rc < 0) {
  11290. pt_debug(cd->dev, DL_ERROR,
  11291. "%s: fail get exclusive ex=%p own=%p\n",
  11292. __func__, cd->exclusive_dev, cd->dev);
  11293. goto queue_startup;
  11294. }
  11295. rc = pt_pip_null_(cd);
  11296. if (release_exclusive(cd, cd->dev) < 0)
  11297. pt_debug(cd->dev, DL_ERROR,
  11298. "%s: fail to release exclusive\n", __func__);
  11299. queue_startup:
  11300. if (rc) {
  11301. #ifdef TTDL_DIAGNOSTICS
  11302. cd->watchdog_failed_access_count++;
  11303. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EXCLUSIVE_ACCESS);
  11304. #endif /* TTDL_DIAGNOSTICS */
  11305. pt_debug(cd->dev, DL_ERROR,
  11306. "%s: failed to access device in WD, retry count=%d\n",
  11307. __func__, cd->startup_retry_count);
  11308. /* Already tried FW upgrade because of watchdog but failed */
  11309. if (cd->startup_retry_count > PT_WATCHDOG_RETRY_COUNT)
  11310. return;
  11311. if (cd->startup_retry_count++ < PT_WATCHDOG_RETRY_COUNT) {
  11312. /*
  11313. * Any wrapper function that trys to disable the
  11314. * WD killing this worker cannot be called here.
  11315. */
  11316. rc = request_exclusive(cd, cd->dev,
  11317. PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11318. if (rc < 0) {
  11319. pt_debug(cd->dev, DL_ERROR,
  11320. "%s: fail get exclusive ex=%p own=%p\n",
  11321. __func__, cd->exclusive_dev, cd->dev);
  11322. goto exit;
  11323. }
  11324. cd->hw_detected = false;
  11325. cd->startup_status = STARTUP_STATUS_START;
  11326. pt_debug(cd->dev, DL_DEBUG,
  11327. "%s: Startup Status Reset\n", __func__);
  11328. rc = pt_dut_reset_and_wait(cd);
  11329. if (release_exclusive(cd, cd->dev) < 0)
  11330. pt_debug(cd->dev, DL_ERROR,
  11331. "%s: fail to release exclusive\n",
  11332. __func__);
  11333. if (!rc) {
  11334. cd->hw_detected = true;
  11335. if (!cd->flashless_dut)
  11336. pt_queue_enum(cd);
  11337. }
  11338. #ifdef TTDL_DIAGNOSTICS
  11339. cd->wd_xres_count++;
  11340. pt_debug(cd->dev, DL_ERROR,
  11341. "%s: Comm Failed - DUT reset [#%d]\n",
  11342. __func__, cd->wd_xres_count);
  11343. #endif /* TTDL_DIAGNOSTICS */
  11344. } else {
  11345. /*
  11346. * After trying PT_WATCHDOG_RETRY_COUNT times to
  11347. * reset the part to regain communications, try to BL
  11348. */
  11349. pt_debug(cd->dev, DL_ERROR,
  11350. "%s: WD DUT access failure, Start FW Upgrade\n",
  11351. __func__);
  11352. #ifdef TTDL_DIAGNOSTICS
  11353. /*
  11354. * When diagnostics is enabled allow TTDL to keep
  11355. * trying to find the DUT. This allows the DUT to be
  11356. * hot swap-able while the host stays running. In
  11357. * production this may not be wanted as a customer
  11358. * may have several touch drivers and any driver
  11359. * that doesn't match the current DUT should give
  11360. * up trying and give up using the bus.
  11361. */
  11362. pt_debug(cd->dev, DL_INFO,
  11363. "%s: Resetting startup_retry_count\n",
  11364. __func__);
  11365. cd->startup_retry_count = 0;
  11366. #endif /* TTDL_DIAGNOSTICS */
  11367. /*
  11368. * Since fw may be broken,reset sysinfo ready flag
  11369. * to let upgrade function work.
  11370. */
  11371. mutex_lock(&cd->system_lock);
  11372. cd->sysinfo.ready = false;
  11373. mutex_unlock(&cd->system_lock);
  11374. if (cd->active_dut_generation == DUT_UNKNOWN) {
  11375. pt_debug(cd->dev, DL_ERROR,
  11376. "%s: Queue Restart\n", __func__);
  11377. pt_queue_restart(cd);
  11378. } else
  11379. kthread_run(start_fw_upgrade, cd, "pt_loader");
  11380. }
  11381. } else {
  11382. cd->hw_detected = true;
  11383. if (cd->startup_status <= (STARTUP_STATUS_FW_RESET_SENTINEL |
  11384. STARTUP_STATUS_BL_RESET_SENTINEL)) {
  11385. pt_debug(cd->dev, DL_ERROR,
  11386. "%s: HW detected but not enumerated\n",
  11387. __func__);
  11388. pt_queue_enum(cd);
  11389. }
  11390. }
  11391. exit:
  11392. pt_start_wd_timer(cd);
  11393. }
  11394. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  11395. /*******************************************************************************
  11396. * FUNCTION: pt_watchdog_timer
  11397. *
  11398. * SUMMARY: The function that is called when the WD timer expires. If the
  11399. * watchdog work is not already busy schedule the watchdog work queue.
  11400. *
  11401. * RETURN: void
  11402. *
  11403. * PARAMETERS:
  11404. * handle - Handle to the watchdog timer
  11405. ******************************************************************************/
  11406. static void pt_watchdog_timer(unsigned long handle)
  11407. {
  11408. struct pt_core_data *cd = (struct pt_core_data *)handle;
  11409. if (!cd)
  11410. return;
  11411. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11412. __func__);
  11413. if (!work_pending(&cd->watchdog_work))
  11414. schedule_work(&cd->watchdog_work);
  11415. }
  11416. #else
  11417. /*******************************************************************************
  11418. * FUNCTION: pt_watchdog_timer
  11419. *
  11420. * SUMMARY: The function that is called when the WD timer expires. If the
  11421. * watchdog work is not already busy schedule the watchdog work queue.
  11422. *
  11423. * RETURN: void
  11424. *
  11425. * PARAMETERS:
  11426. * *t - Pointer to timer list
  11427. ******************************************************************************/
  11428. static void pt_watchdog_timer(struct timer_list *t)
  11429. {
  11430. struct pt_core_data *cd = from_timer(cd, t, watchdog_timer);
  11431. if (!cd)
  11432. return;
  11433. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11434. __func__);
  11435. if (!work_pending(&cd->watchdog_work))
  11436. schedule_work(&cd->watchdog_work);
  11437. }
  11438. #endif
  11439. /*******************************************************************************
  11440. * Core sysfs show and store functions
  11441. ******************************************************************************/
  11442. /*******************************************************************************
  11443. * FUNCTION: pt_hw_version_show
  11444. *
  11445. * SUMMARY: Gets the HW version for either PIP1.x or PIP2.x DUTS
  11446. * Output data format: [SiliconID].[RevID FamilyID].[PanelID]
  11447. *
  11448. * RETURN: size of data written to sysfs node
  11449. *
  11450. * PARAMETERS:
  11451. * *dev - pointer to device structure
  11452. * *attr - pointer to device attributes structure
  11453. * *buf - pointer to print output buffer
  11454. ******************************************************************************/
  11455. static ssize_t pt_hw_version_show(struct device *dev,
  11456. struct device_attribute *attr, char *buf)
  11457. {
  11458. struct pt_core_data *cd = dev_get_drvdata(dev);
  11459. _pt_request_hw_version(dev, cd->hw_version);
  11460. return scnprintf(buf, PT_MAX_PRBUF_SIZE, "%s\n", cd->hw_version);
  11461. }
  11462. static DEVICE_ATTR(hw_version, 0444, pt_hw_version_show, NULL);
  11463. /*******************************************************************************
  11464. * FUNCTION: pt_drv_version_show
  11465. *
  11466. * SUMMARY: Show method for the drv_version sysfs node that will show the
  11467. * TTDL version information
  11468. *
  11469. * RETURN: Char buffer with printed TTDL version information
  11470. *
  11471. * PARAMETERS:
  11472. * *dev - pointer to device structure
  11473. * *attr - pointer to device attributes
  11474. * *buf - pointer to output buffer
  11475. ******************************************************************************/
  11476. static ssize_t pt_drv_version_show(struct device *dev,
  11477. struct device_attribute *attr, char *buf)
  11478. {
  11479. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  11480. "Driver: %s\nVersion: %s\nDate: %s\n",
  11481. pt_driver_core_name, pt_driver_core_version,
  11482. pt_driver_core_date);
  11483. }
  11484. static DEVICE_ATTR(drv_version, 0444, pt_drv_version_show, NULL);
  11485. static DEVICE_ATTR(drv_ver, 0444, pt_drv_version_show, NULL);
  11486. /*******************************************************************************
  11487. * FUNCTION: pt_fw_version_show
  11488. *
  11489. * SUMMARY: Show method for the fw_version sysfs node that will
  11490. * show the firmware, bootloader and PIP version information
  11491. *
  11492. * RETURN: Size of printed buffer
  11493. *
  11494. * PARAMETERS:
  11495. * *dev - pointer to device structure
  11496. * *attr - pointer to device attributes
  11497. * *buf - pointer to output buffer
  11498. ******************************************************************************/
  11499. static ssize_t pt_fw_version_show(struct device *dev,
  11500. struct device_attribute *attr, char *buf)
  11501. {
  11502. struct pt_core_data *cd = dev_get_drvdata(dev);
  11503. struct pt_ttdata *ttdata;
  11504. int rc = 0;
  11505. if (cd->mode == PT_MODE_OPERATIONAL)
  11506. rc = pt_hid_output_get_sysinfo_(cd);
  11507. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11508. __func__, cd->mode, cd->sysinfo.ready);
  11509. if (cd->sysinfo.ready)
  11510. ttdata = &cd->sysinfo.ttdata;
  11511. else
  11512. rc = -ENODATA;
  11513. if (!rc) {
  11514. return scnprintf(buf, strlen(buf),
  11515. "Status: %d\n"
  11516. "FW : %d.%d.%d\n"
  11517. "Config: %d\n"
  11518. "BL : %d.%d\n"
  11519. "PIP : %d.%d\n",
  11520. rc,
  11521. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  11522. ttdata->revctrl,
  11523. ttdata->fw_ver_conf,
  11524. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  11525. ttdata->pip_ver_major, ttdata->pip_ver_minor);
  11526. } else {
  11527. return scnprintf(buf, strlen(buf),
  11528. "Status: %d\n"
  11529. "FW : n/a\n"
  11530. "Config: n/a\n"
  11531. "BL : n/a\n"
  11532. "PIP : n/a\n",
  11533. rc);
  11534. }
  11535. }
  11536. static DEVICE_ATTR(fw_version, 0444, pt_fw_version_show, NULL);
  11537. /*******************************************************************************
  11538. * FUNCTION: pt_sysinfo_show
  11539. *
  11540. * SUMMARY: Show method for the sysinfo sysfs node that will
  11541. * show all the information from get system information command.
  11542. *
  11543. * RETURN: Size of printed buffer
  11544. *
  11545. * PARAMETERS:
  11546. * *dev - pointer to device structure
  11547. * *attr - pointer to device attributes
  11548. * *buf - pointer to output buffer
  11549. ******************************************************************************/
  11550. static ssize_t pt_sysinfo_show(struct device *dev,
  11551. struct device_attribute *attr, char *buf)
  11552. {
  11553. struct pt_core_data *cd = dev_get_drvdata(dev);
  11554. struct pt_sysinfo *si;
  11555. struct pt_ttdata *ttdata = NULL;
  11556. struct pt_sensing_conf_data *scd = NULL;
  11557. int rc = 0;
  11558. if (cd->mode == PT_MODE_OPERATIONAL) {
  11559. rc = pt_hid_output_get_sysinfo_(cd);
  11560. if (cd->sysinfo.ready) {
  11561. si = &cd->sysinfo;
  11562. ttdata = &si->ttdata;
  11563. scd = &si->sensing_conf_data;
  11564. } else
  11565. rc = -ENODATA;
  11566. } else
  11567. rc = -EPERM;
  11568. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11569. __func__, cd->mode, cd->sysinfo.ready);
  11570. if (!rc && ttdata && scd) {
  11571. return scnprintf(buf, strlen(buf),
  11572. "Status: %d\n"
  11573. "pip_ver_major: 0x%02X\n"
  11574. "pip_ver_minor: 0x%02X\n"
  11575. "fw_pid : 0x%04X\n"
  11576. "fw_ver_major : 0x%02X\n"
  11577. "fw_ver_minor : 0x%02X\n"
  11578. "revctrl : 0x%08X\n"
  11579. "fw_ver_conf : 0x%04X\n"
  11580. "bl_ver_major : 0x%02X\n"
  11581. "bl_ver_minor : 0x%02X\n"
  11582. "jtag_id_h : 0x%04X\n"
  11583. "jtag_id_l : 0x%04X\n"
  11584. "mfg_id[0] : 0x%02X\n"
  11585. "mfg_id[1] : 0x%02X\n"
  11586. "mfg_id[2] : 0x%02X\n"
  11587. "mfg_id[3] : 0x%02X\n"
  11588. "mfg_id[4] : 0x%02X\n"
  11589. "mfg_id[5] : 0x%02X\n"
  11590. "mfg_id[6] : 0x%02X\n"
  11591. "mfg_id[7] : 0x%02X\n"
  11592. "post_code : 0x%04X\n"
  11593. "electrodes_x : 0x%02X\n"
  11594. "electrodes_y : 0x%02X\n"
  11595. "len_x : 0x%04X\n"
  11596. "len_y : 0x%04X\n"
  11597. "res_x : 0x%04X\n"
  11598. "res_y : 0x%04X\n"
  11599. "max_z : 0x%04X\n"
  11600. "origin_x : 0x%02X\n"
  11601. "origin_y : 0x%02X\n"
  11602. "panel_id : 0x%02X\n"
  11603. "btn : 0x%02X\n"
  11604. "scan_mode : 0x%02X\n"
  11605. "max_num_of_tch_per_refresh_cycle: 0x%02X\n",
  11606. rc,
  11607. ttdata->pip_ver_major,
  11608. ttdata->pip_ver_minor,
  11609. ttdata->fw_pid,
  11610. ttdata->fw_ver_major,
  11611. ttdata->fw_ver_minor,
  11612. ttdata->revctrl,
  11613. ttdata->fw_ver_conf,
  11614. ttdata->bl_ver_major,
  11615. ttdata->bl_ver_minor,
  11616. ttdata->jtag_id_h,
  11617. ttdata->jtag_id_l,
  11618. ttdata->mfg_id[0],
  11619. ttdata->mfg_id[1],
  11620. ttdata->mfg_id[2],
  11621. ttdata->mfg_id[3],
  11622. ttdata->mfg_id[4],
  11623. ttdata->mfg_id[5],
  11624. ttdata->mfg_id[6],
  11625. ttdata->mfg_id[7],
  11626. ttdata->post_code,
  11627. scd->electrodes_x,
  11628. scd->electrodes_y,
  11629. scd->len_x,
  11630. scd->len_y,
  11631. scd->res_x,
  11632. scd->res_y,
  11633. scd->max_z,
  11634. scd->origin_x,
  11635. scd->origin_y,
  11636. scd->panel_id,
  11637. scd->btn,
  11638. scd->scan_mode,
  11639. scd->max_tch);
  11640. } else {
  11641. return scnprintf(buf, strlen(buf),
  11642. "Status: %d\n",
  11643. rc);
  11644. }
  11645. }
  11646. static DEVICE_ATTR(sysinfo, 0444, pt_sysinfo_show, NULL);
  11647. /*******************************************************************************
  11648. * FUNCTION: pt_hw_reset_show
  11649. *
  11650. * SUMMARY: The show method for the hw_reset sysfs node that does a hw reset
  11651. * by toggling the XRES line and then calls the startup function to
  11652. * allow TTDL to re-enumerate the DUT.
  11653. * The printed value reflects the status of the full reset/enum.
  11654. *
  11655. * PARAMETERS:
  11656. * *dev - pointer to Device structure
  11657. * *attr - pointer to the device attribute structure
  11658. * *buf - pointer to buffer to print
  11659. ******************************************************************************/
  11660. static ssize_t pt_hw_reset_show(struct device *dev,
  11661. struct device_attribute *attr, char *buf)
  11662. {
  11663. struct pt_core_data *cd = dev_get_drvdata(dev);
  11664. int rc = 0;
  11665. int time = 0;
  11666. u8 reset_status = 0;
  11667. int t;
  11668. struct pt_hid_desc hid_desc;
  11669. memset(&hid_desc, 0, sizeof(hid_desc));
  11670. /* Only allow DUT reset if no active BL in progress */
  11671. mutex_lock(&cd->firmware_class_lock);
  11672. mutex_lock(&cd->system_lock);
  11673. cd->startup_state = STARTUP_NONE;
  11674. mutex_unlock(&(cd->system_lock));
  11675. pt_stop_wd_timer(cd);
  11676. /* ensure no left over exclusive access is still locked */
  11677. release_exclusive(cd, cd->dev);
  11678. rc = pt_dut_reset(cd, PT_CORE_CMD_PROTECTED);
  11679. if (rc) {
  11680. mutex_unlock(&cd->firmware_class_lock);
  11681. pt_debug(cd->dev, DL_ERROR,
  11682. "%s: HW reset failed rc = %d\n", __func__, rc);
  11683. goto exit_hw_reset;
  11684. }
  11685. reset_status |= 0x01 << 0;
  11686. if (cd->flashless_dut) {
  11687. mutex_unlock(&cd->firmware_class_lock);
  11688. t = wait_event_timeout(cd->wait_q, (cd->fw_updating == true),
  11689. msecs_to_jiffies(200));
  11690. if (IS_TMO(t)) {
  11691. pt_debug(dev, DL_ERROR,
  11692. "%s: Timeout waiting for FW update",
  11693. __func__);
  11694. rc = -ETIME;
  11695. goto exit_hw_reset;
  11696. } else {
  11697. pt_debug(dev, DL_INFO,
  11698. "%s: ----- Wait FW Loading ----",
  11699. __func__);
  11700. rc = _pt_request_wait_for_enum_state(
  11701. dev, 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  11702. if (rc) {
  11703. pt_debug(cd->dev, DL_ERROR,
  11704. "%s: No FW Sentinel detected rc = %d\n",
  11705. __func__, rc);
  11706. goto exit_hw_reset;
  11707. }
  11708. reset_status |= 0x01 << 1;
  11709. }
  11710. } else {
  11711. /* Wait for any sentinel */
  11712. rc = _pt_request_wait_for_enum_state(dev, 150,
  11713. STARTUP_STATUS_BL_RESET_SENTINEL |
  11714. STARTUP_STATUS_FW_RESET_SENTINEL);
  11715. if (rc) {
  11716. mutex_unlock(&cd->firmware_class_lock);
  11717. pt_debug(cd->dev, DL_ERROR,
  11718. "%s: No Sentinel detected rc = %d\n",
  11719. __func__, rc);
  11720. goto exit_hw_reset;
  11721. }
  11722. /* sleep needed to ensure no cmd is sent while DUT will NAK */
  11723. msleep(30);
  11724. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  11725. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  11726. if (rc < 0) {
  11727. pt_debug(cd->dev, DL_ERROR,
  11728. "%s: Error on getting HID descriptor r=%d\n",
  11729. __func__, rc);
  11730. goto exit_hw_reset;
  11731. }
  11732. cd->mode = pt_get_mode(cd, &hid_desc);
  11733. if (cd->mode == PT_MODE_BOOTLOADER)
  11734. rc = pt_hid_output_bl_launch_app_(cd);
  11735. } else {
  11736. if (cd->mode == PT_MODE_BOOTLOADER)
  11737. rc = pt_pip2_launch_app(dev,
  11738. PT_CORE_CMD_UNPROTECTED);
  11739. }
  11740. if (rc) {
  11741. mutex_unlock(&cd->firmware_class_lock);
  11742. pt_debug(cd->dev, DL_ERROR,
  11743. "%s: PIP launch app failed rc = %d\n",
  11744. __func__, rc);
  11745. goto exit_hw_reset;
  11746. }
  11747. mutex_unlock(&cd->firmware_class_lock);
  11748. reset_status |= 0x01 << 1;
  11749. msleep(20);
  11750. if ((cd->active_dut_generation == DUT_UNKNOWN) ||
  11751. (cd->mode != PT_MODE_OPERATIONAL))
  11752. pt_queue_restart(cd);
  11753. else
  11754. pt_queue_enum(cd);
  11755. }
  11756. while (!(cd->startup_status & STARTUP_STATUS_COMPLETE) && time < 2000) {
  11757. msleep(50);
  11758. pt_debug(cd->dev, DL_INFO,
  11759. "%s: wait %dms for 0x%04X, current enum=0x%04X\n",
  11760. __func__, time, STARTUP_STATUS_COMPLETE,
  11761. cd->startup_status);
  11762. time += 50;
  11763. }
  11764. if (!(cd->startup_status & STARTUP_STATUS_COMPLETE)) {
  11765. rc = -ETIME;
  11766. goto exit_hw_reset;
  11767. }
  11768. pt_debug(cd->dev, DL_INFO, "%s: HW Reset complete. enum=0x%04X\n",
  11769. __func__, cd->startup_status);
  11770. reset_status |= 0x01 << 2;
  11771. pt_start_wd_timer(cd);
  11772. exit_hw_reset:
  11773. return scnprintf(buf, strlen(buf),
  11774. "Status: %d\n"
  11775. "Reset Status: 0x%02X\n", rc, reset_status);
  11776. }
  11777. static DEVICE_ATTR(hw_reset, 0444, pt_hw_reset_show, NULL);
  11778. /*******************************************************************************
  11779. * FUNCTION: pt_pip2_cmd_rsp_store
  11780. *
  11781. * SUMMARY: This is the store method for the raw PIP2 cmd/rsp sysfs node. Any
  11782. * raw PIP2 command echo'd to this node will be sent directly to the DUT.
  11783. * Command byte order:
  11784. * Byte [0] - PIP2 command ID
  11785. * Byte [1-n] - PIP2 command payload
  11786. *
  11787. * RETURN: Size of passed in buffer
  11788. *
  11789. * PARAMETERS:
  11790. * *dev - pointer to device structure
  11791. * *attr - pointer to device attributes
  11792. * *buf - pointer to buffer that hold the command parameters
  11793. * size - size of buf
  11794. ******************************************************************************/
  11795. static ssize_t pt_pip2_cmd_rsp_store(struct device *dev,
  11796. struct device_attribute *attr, const char *buf, size_t size)
  11797. {
  11798. struct pt_core_data *cd = dev_get_drvdata(dev);
  11799. u16 actual_read_len;
  11800. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  11801. u8 read_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  11802. u8 pip2_cmd_id = 0x00;
  11803. u8 *pip2_cmd_data = NULL;
  11804. int data_len = 0;
  11805. int length;
  11806. int rc = 0;
  11807. /* clear shared data */
  11808. mutex_lock(&cd->sysfs_lock);
  11809. cd->raw_cmd_status = 0;
  11810. cd->cmd_rsp_buf_len = 0;
  11811. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  11812. mutex_unlock(&cd->sysfs_lock);
  11813. length = _pt_ic_parse_input_hex(dev, buf, size,
  11814. input_data, PT_MAX_PIP2_MSG_SIZE);
  11815. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  11816. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  11817. __func__);
  11818. rc = -EINVAL;
  11819. goto exit;
  11820. }
  11821. /* Send PIP2 command if enough data was provided */
  11822. if (length >= 1) {
  11823. pip2_cmd_id = input_data[0];
  11824. pip2_cmd_data = &input_data[1];
  11825. data_len = length - 1;
  11826. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  11827. pip2_cmd_id, pip2_cmd_data, data_len,
  11828. read_buf, &actual_read_len);
  11829. cd->raw_cmd_status = rc;
  11830. if (rc) {
  11831. pt_debug(dev, DL_ERROR,
  11832. "%s: PIP2 cmd 0x%02x failed rc = %d\n",
  11833. __func__, pip2_cmd_id, rc);
  11834. goto exit;
  11835. } else {
  11836. cd->cmd_rsp_buf_len = actual_read_len;
  11837. memcpy(cd->cmd_rsp_buf, read_buf, actual_read_len);
  11838. pt_debug(dev, DL_ERROR,
  11839. "%s: PIP2 actual_read_len = %d\n",
  11840. __func__, actual_read_len);
  11841. }
  11842. } else {
  11843. rc = -EINVAL;
  11844. pt_debug(dev, DL_ERROR,
  11845. "%s: Insufficient data provided for PIP2 cmd\n",
  11846. __func__);
  11847. }
  11848. exit:
  11849. if (rc)
  11850. return rc;
  11851. return size;
  11852. }
  11853. /*******************************************************************************
  11854. * FUNCTION: pt_pip2_cmd_rsp_show
  11855. *
  11856. * SUMMARY: The show method for the raw pip2_cmd_rsp sysfs node. Any PIP2
  11857. * response generated after using the store method of the pip2_cmd_rsp
  11858. * sysfs node, are available to be read here.
  11859. *
  11860. * PARAMETERS:
  11861. * *dev - pointer to Device structure
  11862. * *attr - pointer to the device attribute structure
  11863. * *buf - pointer to buffer to print
  11864. ******************************************************************************/
  11865. static ssize_t pt_pip2_cmd_rsp_show(struct device *dev,
  11866. struct device_attribute *attr, char *buf)
  11867. {
  11868. struct pt_core_data *cd = dev_get_drvdata(dev);
  11869. int i;
  11870. ssize_t data_len;
  11871. int index;
  11872. mutex_lock(&cd->sysfs_lock);
  11873. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  11874. "Status: %d\n", cd->raw_cmd_status);
  11875. if (cd->raw_cmd_status)
  11876. goto error;
  11877. /* Remove the CRC from the length of the response */
  11878. data_len = cd->cmd_rsp_buf_len - 2;
  11879. /* Start printing from the data payload */
  11880. for (i = PIP1_RESP_COMMAND_ID_OFFSET; i < data_len; i++)
  11881. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  11882. "%02X ", cd->cmd_rsp_buf[i]);
  11883. if (data_len >= PIP1_RESP_COMMAND_ID_OFFSET) {
  11884. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  11885. "\n(%zd bytes)\n",
  11886. data_len - PIP1_RESP_COMMAND_ID_OFFSET);
  11887. } else {
  11888. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  11889. "\n(%zd bytes)\n", 0);
  11890. }
  11891. error:
  11892. mutex_unlock(&cd->sysfs_lock);
  11893. return index;
  11894. }
  11895. static DEVICE_ATTR(pip2_cmd_rsp, 0644, pt_pip2_cmd_rsp_show,
  11896. pt_pip2_cmd_rsp_store);
  11897. /*******************************************************************************
  11898. * FUNCTION: pt_command_store
  11899. *
  11900. * SUMMARY: This is the store method for the raw PIP command sysfs node. Any
  11901. * raw PIP command echo'd to this node will be sent directly to the DUT.
  11902. * TTDL will not parse the command.
  11903. *
  11904. * RETURN: Size of passed in buffer
  11905. *
  11906. * PARAMETERS:
  11907. * *dev - pointer to device structure
  11908. * *attr - pointer to device attributes
  11909. * *buf - pointer to buffer that hold the command parameters
  11910. * size - size of buf
  11911. ******************************************************************************/
  11912. static ssize_t pt_command_store(struct device *dev,
  11913. struct device_attribute *attr, const char *buf, size_t size)
  11914. {
  11915. struct pt_core_data *cd = dev_get_drvdata(dev);
  11916. unsigned short crc;
  11917. u16 actual_read_len;
  11918. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  11919. int length;
  11920. int len_field;
  11921. int rc = 0;
  11922. mutex_lock(&cd->sysfs_lock);
  11923. cd->cmd_rsp_buf_len = 0;
  11924. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  11925. mutex_unlock(&cd->sysfs_lock);
  11926. length = _pt_ic_parse_input_hex(dev, buf, size,
  11927. input_data, PT_MAX_PIP2_MSG_SIZE);
  11928. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  11929. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  11930. __func__);
  11931. rc = -EINVAL;
  11932. goto pt_command_store_exit;
  11933. }
  11934. /* PIP2 messages begin with 01 01 */
  11935. if (length >= 2 && input_data[0] == 0x01 && input_data[1] == 0x01) {
  11936. cd->pip2_prot_active = 1;
  11937. /* Override next seq tag with what was sent */
  11938. cd->pip2_cmd_tag_seq = input_data[4] & 0x0F;
  11939. /* For PIP2 cmd if length does not include crc, add it */
  11940. len_field = (input_data[3] << 8) | input_data[2];
  11941. if (len_field == length && length <= 254) {
  11942. crc = crc_ccitt_calculate(&input_data[2],
  11943. length - 2);
  11944. pt_debug(dev, DL_ERROR, "%s: len=%d crc=0x%02X\n",
  11945. __func__, length, crc);
  11946. input_data[length] = (crc & 0xFF00) >> 8;
  11947. input_data[length + 1] = crc & 0x00FF;
  11948. length = length + 2;
  11949. }
  11950. }
  11951. /* write PIP command to log */
  11952. pt_pr_buf(dev, DL_INFO, input_data, length, "command_buf");
  11953. pm_runtime_get_sync(dev);
  11954. rc = pt_hid_output_user_cmd(cd, PT_MAX_INPUT, cd->cmd_rsp_buf,
  11955. length, input_data, &actual_read_len);
  11956. pm_runtime_put(dev);
  11957. mutex_lock(&cd->sysfs_lock);
  11958. cd->raw_cmd_status = rc;
  11959. if (rc) {
  11960. cd->cmd_rsp_buf_len = 0;
  11961. pt_debug(dev, DL_ERROR, "%s: Failed to send command: %s\n",
  11962. __func__, buf);
  11963. } else {
  11964. cd->cmd_rsp_buf_len = actual_read_len;
  11965. }
  11966. cd->pip2_prot_active = 0;
  11967. mutex_unlock(&cd->sysfs_lock);
  11968. pt_command_store_exit:
  11969. if (rc)
  11970. return rc;
  11971. return size;
  11972. }
  11973. static DEVICE_ATTR(command, 0220, NULL, pt_command_store);
  11974. /*******************************************************************************
  11975. * FUNCTION: pt_response_show
  11976. *
  11977. * SUMMARY: The show method for the raw PIP response sysfs node. Any PIP
  11978. * response generated after using the pt_command_store sysfs node, are
  11979. * available to be read here.
  11980. *
  11981. * PARAMETERS:
  11982. * *dev - pointer to Device structure
  11983. * *attr - pointer to the device attribute structure
  11984. * *buf - pointer to buffer to print
  11985. ******************************************************************************/
  11986. static ssize_t pt_response_show(struct device *dev,
  11987. struct device_attribute *attr, char *buf)
  11988. {
  11989. struct pt_core_data *cd = dev_get_drvdata(dev);
  11990. int i;
  11991. ssize_t num_read;
  11992. int index;
  11993. mutex_lock(&cd->sysfs_lock);
  11994. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  11995. "Status: %d\n", cd->raw_cmd_status);
  11996. if (cd->raw_cmd_status)
  11997. goto error;
  11998. num_read = cd->cmd_rsp_buf_len;
  11999. for (i = 0; i < num_read; i++)
  12000. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12001. "0x%02X\n", cd->cmd_rsp_buf[i]);
  12002. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12003. "(%zd bytes)\n", num_read);
  12004. error:
  12005. mutex_unlock(&cd->sysfs_lock);
  12006. return index;
  12007. }
  12008. static DEVICE_ATTR(response, 0444, pt_response_show, NULL);
  12009. /*******************************************************************************
  12010. * FUNCTION: pt_dut_debug_show
  12011. *
  12012. * SUMMARY: Show method for the dut_debug sysfs node. Shows what parameters
  12013. * are available for the store method.
  12014. *
  12015. * RETURN:
  12016. * 0 = success
  12017. * !0 = failure
  12018. *
  12019. * PARAMETERS:
  12020. * *dev - pointer to device structure
  12021. * *attr - pointer to device attributes
  12022. * *buf - pointer to output buffer
  12023. ******************************************************************************/
  12024. static ssize_t pt_dut_debug_show(struct device *dev,
  12025. struct device_attribute *attr, char *buf)
  12026. {
  12027. ssize_t ret;
  12028. ret = scnprintf(buf, strlen(buf),
  12029. "Status: 0\n"
  12030. "dut_debug sends the following commands to the DUT:\n"
  12031. "%d %s \t- %s\n"
  12032. "%d %s \t- %s\n"
  12033. "%d %s \t- %s\n"
  12034. "%d %s \t- %s\n"
  12035. "%d %s \t- %s\n"
  12036. "%d %s \t- %s\n"
  12037. "%d %s \t- %s\n"
  12038. "%d %s \t- %s\n"
  12039. "%d %s \t- %s\n"
  12040. "%d %s \t- %s\n"
  12041. "%d %s \t- %s\n"
  12042. "%d %s \t- %s\n"
  12043. "%d %s \t- %s\n"
  12044. "%d %s \t- %s\n"
  12045. "%d %s \t- %s\n"
  12046. "%d %s \t- %s\n"
  12047. "%d %s \t- %s\n"
  12048. ,
  12049. PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY, "", "BL Verify APP",
  12050. PT_DUT_DBG_HID_RESET, "", "HID Reset",
  12051. PT_DUT_DBG_HID_SET_POWER_ON, "", "HID SET_POWER ON",
  12052. PT_DUT_DBG_HID_SET_POWER_SLEEP, "", "HID SET_POWER SLEEP",
  12053. PT_DUT_DBG_HID_SET_POWER_STANDBY, "", "HID SET_POWER STANDBY",
  12054. PIP1_BL_CMD_ID_GET_INFO, "", "BL Get Info",
  12055. PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY, "", "BL Program & Verify",
  12056. PIP1_BL_CMD_ID_LAUNCH_APP, "", "BL Launch APP",
  12057. PIP1_BL_CMD_ID_INITIATE_BL, "", "BL Initiate BL",
  12058. PT_DUT_DBG_PIP_SOFT_RESET, "", "PIP Soft Reset",
  12059. PT_DUT_DBG_RESET, "", "Toggle the TP_XRES GPIO",
  12060. PT_DUT_DBG_PIP_NULL, "", "PIP NULL (PING)",
  12061. PT_DUT_DBG_PIP_ENTER_BL, "", "PIP enter BL",
  12062. PT_DUT_DBG_HID_SYSINFO, "", "HID system info",
  12063. PT_DUT_DBG_PIP_SUSPEND_SCAN, "", "Suspend Scan",
  12064. PT_DUT_DBG_PIP_RESUME_SCAN, "", "Resume Scan",
  12065. PT_DUT_DBG_HID_DESC, "", "Get HID Desc"
  12066. );
  12067. return ret;
  12068. }
  12069. /*******************************************************************************
  12070. * FUNCTION: pt_drv_debug_show
  12071. *
  12072. * SUMMARY: Show method for the drv_debug sysfs node. Shows what parameters
  12073. * are available for the store method.
  12074. *
  12075. * RETURN:
  12076. * 0 = success
  12077. * !0 = failure
  12078. *
  12079. * PARAMETERS:
  12080. * *dev - pointer to device structure
  12081. * *attr - pointer to device attributes
  12082. * *buf - pointer to output buffer
  12083. ******************************************************************************/
  12084. static ssize_t pt_drv_debug_show(struct device *dev,
  12085. struct device_attribute *attr, char *buf)
  12086. {
  12087. ssize_t ret;
  12088. ret = scnprintf(buf, strlen(buf),
  12089. "Status: 0\n"
  12090. "drv_debug supports the following values:\n"
  12091. "%d %s \t- %s\n"
  12092. "%d %s \t- %s\n"
  12093. "%d %s \t- %s\n"
  12094. "%d %s \t- %s\n"
  12095. "%d %s \t- %s\n"
  12096. "%d %s \t- %s\n"
  12097. "%d %s \t- %s\n"
  12098. "%d %s \t- %s\n"
  12099. "%d %s \t- %s\n"
  12100. "%d %s - %s\n"
  12101. "%d %s \t- %s\n"
  12102. "%d %s \t- %s\n"
  12103. #ifdef TTDL_DIAGNOSTICS
  12104. "%d %s \t- %s\n"
  12105. "%d %s \t- %s\n"
  12106. "%d %s \t- %s\n"
  12107. #endif /* TTDL_DIAGNOSTICS */
  12108. "%d %s \t- %s\n"
  12109. #ifdef TTDL_DIAGNOSTICS
  12110. "%d %s \t- %s\n"
  12111. "%d %s \t- %s\n"
  12112. "%d %s \t- %s\n"
  12113. "%d %s \t- %s\n"
  12114. "%d %s \t- %s\n"
  12115. "%d %s \t- %s\n"
  12116. "%d %s \t- %s\n"
  12117. "%d %s \t- %s\n"
  12118. "%d %s \t- %s\n"
  12119. #endif /* TTDL_DIAGNOSTICS */
  12120. ,
  12121. PT_DRV_DBG_SUSPEND, " ", "Suspend TTDL responding to INT",
  12122. PT_DRV_DBG_RESUME, " ", "Resume TTDL responding to INT",
  12123. PT_DRV_DBG_STOP_WD, " ", "Stop TTDL WD",
  12124. PT_DRV_DBG_START_WD, " ", "Start TTDL WD",
  12125. PT_DRV_DBG_TTHE_TUNER_EXIT, " ", "Exit TTHE Tuner Logging",
  12126. PT_DRV_DBG_TTHE_BUF_CLEAN, " ", "Clear TTHE Tuner buffer",
  12127. PT_DRV_DBG_CLEAR_PARM_LIST, " ", "Clear RAM Param list",
  12128. PT_DRV_DBG_FORCE_BUS_READ, " ", "Force bus read",
  12129. PT_DRV_DBG_CLEAR_CAL_DATA, " ", "Clear CAL Cache",
  12130. PT_DRV_DBG_REPORT_LEVEL, "[0|1|2|3|4]", "Set TTDL Debug Level",
  12131. PT_DRV_DBG_WATCHDOG_INTERVAL, "[n] ", "TTDL WD Interval in ms",
  12132. PT_DRV_DBG_SHOW_TIMESTAMP, "[0|1]", "Show Timestamps"
  12133. #ifdef TTDL_DIAGNOSTICS
  12134. , PT_DRV_DBG_SETUP_PWR, "[0|1]", "Power DUT up/down",
  12135. PT_DRV_DBG_GET_PUT_SYNC, "[0|1]", "Get/Put Linux Sleep",
  12136. PT_DRV_DBG_SET_TT_DATA, "[0|1]", "Display TT_DATA"
  12137. #endif /* TTDL_DIAGNOSTICS */
  12138. , PT_DRV_DBG_SET_GENERATION, "[0|1|2]", "Set DUT generation"
  12139. #ifdef TTDL_DIAGNOSTICS
  12140. , PT_DRV_DBG_SET_BRIDGE_MODE, "[0|1]", "On/Off Bridge Mode",
  12141. PT_DRV_DBG_SET_I2C_ADDRESS, "[0-127]", "I2C DUT Address",
  12142. PT_DRV_DBG_SET_FLASHLESS_DUT, "[0|1]", "Flashless DUT yes/no",
  12143. PT_DRV_DBG_SET_FORCE_SEQ, "[8-15]", "Force PIP2 Sequence #",
  12144. PT_DRV_DBG_BL_WITH_NO_INT, "[0|1]", "BL with no INT",
  12145. PT_DRV_DBG_CAL_CACHE_IN_HOST, "[0|1]", "CAL Cache in host",
  12146. PT_DRV_DBG_MULTI_CHIP, "[0|1]", "Multi Chip Support",
  12147. PT_DRV_DBG_PIP_TIMEOUT, "[100-7000]", "PIP Resp Timeout (ms)",
  12148. PT_DRV_DBG_TTHE_HID_USB_FORMAT, "[0|1]",
  12149. "TTHE_TUNER HID USB Format"
  12150. #endif /* TTDL_DIAGNOSTICS */
  12151. );
  12152. return ret;
  12153. }
  12154. /*******************************************************************************
  12155. * FUNCTION: pt_drv_debug_store
  12156. *
  12157. * SUMMARY: Currently the store method for both sysfs nodes: drv_debug and
  12158. * dut_debug. Drv_debug will contain all functionality that can be run
  12159. * without a DUT preset and is available anytime TTDL is running.
  12160. * Dut_debug requires a DUT to be available and will only be created after
  12161. * a DUT has been detected.
  12162. * This function will eventually be split into two but until the overlap
  12163. * has been depricated this function contains all commands that can be
  12164. * used for TTDL/DUT debugging status and control.
  12165. * All commands require at least one value to be passed in *buf with some
  12166. * requiring two.
  12167. *
  12168. * RETURN: Size of passed in buffer
  12169. *
  12170. * PARAMETERS:
  12171. * *dev - pointer to device structure
  12172. * *attr - pointer to device attributes
  12173. * *buf - pointer to buffer that hold the command parameters
  12174. * size - size of buf
  12175. ******************************************************************************/
  12176. static ssize_t pt_drv_debug_store(struct device *dev,
  12177. struct device_attribute *attr, const char *buf, size_t size)
  12178. {
  12179. struct pt_core_data *cd = dev_get_drvdata(dev);
  12180. unsigned long value;
  12181. int rc = 0;
  12182. u8 return_data[8];
  12183. static u8 wd_disabled;
  12184. u32 input_data[3];
  12185. int length;
  12186. #ifdef TTDL_DIAGNOSTICS
  12187. struct i2c_client *client = to_i2c_client(dev);
  12188. unsigned short crc = 0;
  12189. u16 cal_size;
  12190. #endif
  12191. input_data[0] = 0;
  12192. input_data[1] = 0;
  12193. /* Maximmum input is two elements */
  12194. length = _pt_ic_parse_input(dev, buf, size,
  12195. input_data, ARRAY_SIZE(input_data));
  12196. if (length < 1 || length > 2) {
  12197. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12198. __func__);
  12199. rc = -EINVAL;
  12200. goto pt_drv_debug_store_exit;
  12201. }
  12202. value = input_data[0];
  12203. if (length == 1) {
  12204. pt_debug(dev, DL_DEBUG,
  12205. "%s: Debug Cmd Received (id=%d)\n",
  12206. __func__, input_data[0]);
  12207. } else if (length == 2) {
  12208. pt_debug(dev, DL_DEBUG,
  12209. "%s: Debug Cmd Received (id=%d, data=%d)\n",
  12210. __func__, input_data[0], input_data[1]);
  12211. } else {
  12212. pt_debug(dev, DL_DEBUG,
  12213. "%s: Invalid arguments received\n", __func__);
  12214. rc = -EINVAL;
  12215. goto pt_drv_debug_store_exit;
  12216. }
  12217. /* Start watchdog timer command */
  12218. if (value == PT_DRV_DBG_START_WD) {
  12219. pt_debug(dev, DL_INFO, "%s: Cmd: Start Watchdog\n", __func__);
  12220. wd_disabled = 0;
  12221. cd->watchdog_force_stop = false;
  12222. pt_start_wd_timer(cd);
  12223. goto pt_drv_debug_store_exit;
  12224. }
  12225. /* Stop watchdog timer temporarily */
  12226. pt_stop_wd_timer(cd);
  12227. if (value == PT_DRV_DBG_STOP_WD) {
  12228. pt_debug(dev, DL_INFO, "%s: Cmd: Stop Watchdog\n", __func__);
  12229. wd_disabled = 1;
  12230. cd->watchdog_force_stop = true;
  12231. goto pt_drv_debug_store_exit;
  12232. }
  12233. switch (value) {
  12234. case PT_DRV_DBG_SUSPEND: /* 4 */
  12235. pt_debug(dev, DL_INFO, "%s: TTDL: Core Sleep\n", __func__);
  12236. wd_disabled = 1;
  12237. rc = pt_core_sleep(cd);
  12238. if (rc)
  12239. pt_debug(dev, DL_ERROR, "%s: Suspend failed rc=%d\n",
  12240. __func__, rc);
  12241. else
  12242. pt_debug(dev, DL_INFO, "%s: Suspend succeeded\n",
  12243. __func__);
  12244. break;
  12245. case PT_DRV_DBG_RESUME: /* 5 */
  12246. pt_debug(dev, DL_INFO, "%s: TTDL: Wake\n", __func__);
  12247. rc = pt_core_wake(cd);
  12248. if (rc)
  12249. pt_debug(dev, DL_ERROR, "%s: Resume failed rc=%d\n",
  12250. __func__, rc);
  12251. else
  12252. pt_debug(dev, DL_INFO, "%s: Resume succeeded\n",
  12253. __func__);
  12254. break;
  12255. case PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY: /* BL - 49 */
  12256. pt_debug(dev, DL_INFO, "%s: Cmd: verify app integ\n", __func__);
  12257. pt_hid_output_bl_verify_app_integrity(cd, &return_data[0]);
  12258. break;
  12259. case PT_DUT_DBG_HID_RESET: /* 50 */
  12260. pt_debug(dev, DL_INFO, "%s: Cmd: hid_reset\n", __func__);
  12261. pt_hid_cmd_reset(cd);
  12262. break;
  12263. case PT_DUT_DBG_HID_SET_POWER_ON: /* 53 */
  12264. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_on\n", __func__);
  12265. pt_hid_cmd_set_power(cd, HID_POWER_ON);
  12266. wd_disabled = 0;
  12267. break;
  12268. case PT_DUT_DBG_HID_SET_POWER_SLEEP: /* 54 */
  12269. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_off\n",
  12270. __func__);
  12271. wd_disabled = 1;
  12272. pt_hid_cmd_set_power(cd, HID_POWER_SLEEP);
  12273. break;
  12274. case PT_DUT_DBG_HID_SET_POWER_STANDBY: /* 55 */
  12275. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_standby\n",
  12276. __func__);
  12277. wd_disabled = 1;
  12278. pt_hid_cmd_set_power(cd, HID_POWER_STANDBY);
  12279. break;
  12280. case PIP1_BL_CMD_ID_GET_INFO: /* BL - 56 */
  12281. pt_debug(dev, DL_INFO, "%s: Cmd: bl get info\n", __func__);
  12282. pt_hid_output_bl_get_information(cd, return_data);
  12283. break;
  12284. case PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY: /* BL - 57 */
  12285. pt_debug(dev, DL_INFO, "%s: Cmd: program and verify\n",
  12286. __func__);
  12287. pt_hid_output_bl_program_and_verify(cd, 0, NULL);
  12288. break;
  12289. case PIP1_BL_CMD_ID_LAUNCH_APP: /* BL - 59 */
  12290. pt_debug(dev, DL_INFO, "%s: Cmd: launch app\n", __func__);
  12291. pt_hid_output_bl_launch_app(cd);
  12292. break;
  12293. case PIP1_BL_CMD_ID_INITIATE_BL: /* BL - 72 */
  12294. pt_debug(dev, DL_INFO, "%s: Cmd: initiate bl\n", __func__);
  12295. pt_hid_output_bl_initiate_bl(cd, 0, NULL, 0, NULL);
  12296. break;
  12297. case PT_DUT_DBG_PIP_SOFT_RESET: /* 97 */
  12298. pt_debug(dev, DL_INFO, "%s: Cmd: Soft Reset\n", __func__);
  12299. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  12300. break;
  12301. case PT_DUT_DBG_RESET: /* 98 */
  12302. pt_debug(dev, DL_INFO, "%s: Cmd: Hard Reset\n", __func__);
  12303. rc = pt_hw_hard_reset(cd);
  12304. break;
  12305. case PT_DUT_DBG_PIP_NULL: /* 100 */
  12306. pt_debug(dev, DL_INFO, "%s: Cmd: Ping (null)\n", __func__);
  12307. pt_pip_null(cd);
  12308. break;
  12309. case PT_DUT_DBG_PIP_ENTER_BL: /* 101 */
  12310. pt_debug(dev, DL_INFO, "%s: Cmd: start_bootloader\n", __func__);
  12311. rc = pt_pip_start_bootloader(cd);
  12312. if (!rc) {
  12313. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  12314. cd->mode = PT_MODE_BOOTLOADER;
  12315. }
  12316. break;
  12317. case PT_DUT_DBG_HID_SYSINFO: /* 102 */
  12318. pt_debug(dev, DL_INFO, "%s: Cmd: get_sysinfo\n", __func__);
  12319. pt_hid_output_get_sysinfo(cd);
  12320. break;
  12321. case PT_DUT_DBG_PIP_SUSPEND_SCAN: /* 103 */
  12322. pt_debug(dev, DL_INFO, "%s: Cmd: suspend_scanning\n", __func__);
  12323. pt_pip_suspend_scanning(cd);
  12324. break;
  12325. case PT_DUT_DBG_PIP_RESUME_SCAN: /* 104 */
  12326. pt_debug(dev, DL_INFO, "%s: Cmd: resume_scanning\n", __func__);
  12327. pt_pip_resume_scanning(cd);
  12328. break;
  12329. #ifdef TTHE_TUNER_SUPPORT
  12330. case PT_DRV_DBG_TTHE_TUNER_EXIT: /* 107 */
  12331. cd->tthe_exit = 1;
  12332. wake_up(&cd->wait_q);
  12333. kfree(cd->tthe_buf);
  12334. cd->tthe_buf = NULL;
  12335. cd->tthe_exit = 0;
  12336. break;
  12337. case PT_DRV_DBG_TTHE_BUF_CLEAN: /* 108 */
  12338. if (cd->tthe_buf)
  12339. memset(cd->tthe_buf, 0, PT_MAX_PRBUF_SIZE);
  12340. else
  12341. pt_debug(dev, DL_INFO, "%s : tthe_buf not existed\n",
  12342. __func__);
  12343. break;
  12344. #endif
  12345. #ifdef TTDL_DIAGNOSTICS
  12346. case PT_DUT_DBG_HID_DESC: /* 109 */
  12347. pt_debug(dev, DL_INFO, "%s: Cmd: get_hid_desc\n", __func__);
  12348. pt_get_hid_descriptor(cd, &cd->hid_desc);
  12349. break;
  12350. case PT_DRV_DBG_CLEAR_PARM_LIST: /* 110 */
  12351. pt_debug(dev, DL_INFO, "%s: TTDL: Clear Parameter List\n",
  12352. __func__);
  12353. pt_erase_parameter_list(cd);
  12354. break;
  12355. case PT_DRV_DBG_FORCE_BUS_READ: /* 111 */
  12356. rc = pt_read_input(cd);
  12357. if (!rc)
  12358. pt_parse_input(cd);
  12359. break;
  12360. case PT_DRV_DBG_CLEAR_CAL_DATA: /* 112 */
  12361. rc = _pt_manage_local_cal_data(dev, PT_CAL_DATA_CLEAR,
  12362. &cal_size, &crc);
  12363. if (rc)
  12364. pt_debug(dev, DL_ERROR,
  12365. "%s: CAL Data clear failed rc=%d\n",
  12366. __func__, rc);
  12367. else
  12368. pt_debug(dev, DL_INFO,
  12369. "%s: CAL Cleared, Chip ID=0x%04X size=%d\n",
  12370. __func__, crc, size);
  12371. break;
  12372. case PT_DRV_DBG_REPORT_LEVEL: /* 200 */
  12373. mutex_lock(&cd->system_lock);
  12374. if (input_data[1] >= 0 && input_data[1] < DL_MAX) {
  12375. cd->debug_level = input_data[1];
  12376. pt_debug(dev, DL_INFO, "%s: Set debug_level: %d\n",
  12377. __func__, cd->debug_level);
  12378. } else {
  12379. rc = -EINVAL;
  12380. pt_debug(dev, DL_ERROR, "%s: Invalid debug_level: %d\n",
  12381. __func__, input_data[1]);
  12382. }
  12383. mutex_unlock(&(cd->system_lock));
  12384. break;
  12385. #endif
  12386. case PT_DRV_DBG_WATCHDOG_INTERVAL: /* 201 */
  12387. mutex_lock(&cd->system_lock);
  12388. if (input_data[1] > 100) {
  12389. cd->watchdog_interval = input_data[1];
  12390. pt_debug(dev, DL_INFO,
  12391. "%s: Set watchdog_ interval to: %d\n",
  12392. __func__, cd->watchdog_interval);
  12393. pt_start_wd_timer(cd);
  12394. } else {
  12395. rc = -EINVAL;
  12396. pt_debug(dev, DL_ERROR,
  12397. "%s: Invalid watchdog interval: %d\n",
  12398. __func__, input_data[1]);
  12399. }
  12400. mutex_unlock(&(cd->system_lock));
  12401. break;
  12402. #ifdef TTDL_DIAGNOSTICS
  12403. case PT_DRV_DBG_SHOW_TIMESTAMP: /* 202 */
  12404. mutex_lock(&cd->system_lock);
  12405. if (input_data[1] == 0) {
  12406. cd->show_timestamp = 0;
  12407. pt_debug(dev, DL_INFO, "%s: Disable show_timestamp\n",
  12408. __func__);
  12409. } else if (input_data[1] == 1) {
  12410. cd->show_timestamp = 1;
  12411. pt_debug(dev, DL_INFO, "%s: Enable show_timestamp\n",
  12412. __func__);
  12413. } else {
  12414. rc = -EINVAL;
  12415. pt_debug(dev, DL_ERROR,
  12416. "%s: Invalid parameter: %d\n",
  12417. __func__, input_data[1]);
  12418. }
  12419. mutex_unlock(&(cd->system_lock));
  12420. break;
  12421. case PT_DRV_DBG_SETUP_PWR: /* 205 */
  12422. if (input_data[1] == 0) {
  12423. cd->cpdata->setup_power(cd->cpdata,
  12424. PT_MT_POWER_OFF, cd->dev);
  12425. pt_debug(dev, DL_INFO,
  12426. "%s: Initiate Power Off\n", __func__);
  12427. } else if (input_data[1] == 1) {
  12428. cd->cpdata->setup_power(cd->cpdata,
  12429. PT_MT_POWER_ON, cd->dev);
  12430. pt_debug(dev, DL_INFO,
  12431. "%s: Initiate Power On\n", __func__);
  12432. } else {
  12433. rc = -EINVAL;
  12434. pt_debug(dev, DL_ERROR,
  12435. "%s: Invalid parameter: %d\n",
  12436. __func__, input_data[1]);
  12437. }
  12438. break;
  12439. case PT_DRV_DBG_GET_PUT_SYNC: /* 206 */
  12440. if (input_data[1] == 0) {
  12441. pm_runtime_put(dev);
  12442. pt_debug(dev, DL_ERROR,
  12443. "%s: Force call pm_runtime_put()\n", __func__);
  12444. } else if (input_data[1] == 1) {
  12445. pm_runtime_get_sync(dev);
  12446. pt_debug(dev, DL_ERROR,
  12447. "%s: Force call pm_runtime_get_sync()\n",
  12448. __func__);
  12449. } else {
  12450. rc = -EINVAL;
  12451. pt_debug(dev, DL_ERROR,
  12452. "%s: WARNING: Invalid parameter: %d\n",
  12453. __func__, input_data[1]);
  12454. }
  12455. break;
  12456. case PT_DRV_DBG_SET_TT_DATA: /* 208 */
  12457. mutex_lock(&cd->system_lock);
  12458. if (input_data[1] == 0) {
  12459. cd->show_tt_data = false;
  12460. pt_debug(dev, DL_INFO,
  12461. "%s: Disable TT_DATA\n", __func__);
  12462. } else if (input_data[1] == 1) {
  12463. cd->show_tt_data = true;
  12464. pt_debug(dev, DL_INFO,
  12465. "%s: Enable TT_DATA\n", __func__);
  12466. } else {
  12467. rc = -EINVAL;
  12468. pt_debug(dev, DL_ERROR,
  12469. "%s: Invalid parameter: %d\n",
  12470. __func__, input_data[1]);
  12471. }
  12472. mutex_unlock(&(cd->system_lock));
  12473. break;
  12474. case PT_DRV_DBG_SET_GENERATION: /* 210 */
  12475. if (input_data[1] == cd->active_dut_generation) {
  12476. mutex_lock(&cd->system_lock);
  12477. cd->set_dut_generation = true;
  12478. mutex_unlock(&(cd->system_lock));
  12479. } else {
  12480. mutex_lock(&cd->system_lock);
  12481. if (input_data[1] == 0) {
  12482. cd->active_dut_generation = DUT_UNKNOWN;
  12483. cd->set_dut_generation = false;
  12484. } else if (input_data[1] == 1) {
  12485. cd->active_dut_generation = DUT_PIP1_ONLY;
  12486. cd->set_dut_generation = true;
  12487. } else if (input_data[1] == 2) {
  12488. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  12489. cd->set_dut_generation = true;
  12490. } else {
  12491. rc = -EINVAL;
  12492. pt_debug(dev, DL_ERROR,
  12493. "%s: Invalid parameter: %d\n",
  12494. __func__, input_data[1]);
  12495. break;
  12496. }
  12497. cd->startup_status = STARTUP_STATUS_START;
  12498. pt_debug(cd->dev, DL_DEBUG,
  12499. "%s: Startup Status Reset\n", __func__);
  12500. mutex_unlock(&(cd->system_lock));
  12501. pt_debug(dev, DL_INFO,
  12502. "%s: Active DUT Generation Set to: %d\n",
  12503. __func__, cd->active_dut_generation);
  12504. /* Changing DUT generations full restart needed */
  12505. pt_queue_restart(cd);
  12506. }
  12507. break;
  12508. case PT_DRV_DBG_SET_BRIDGE_MODE: /* 211 */
  12509. mutex_lock(&cd->system_lock);
  12510. if (input_data[1] == 0) {
  12511. cd->bridge_mode = false;
  12512. pt_debug(dev, DL_INFO,
  12513. "%s: Disable Bridge Mode\n", __func__);
  12514. } else if (input_data[1] == 1) {
  12515. cd->bridge_mode = true;
  12516. pt_debug(dev, DL_INFO,
  12517. "%s: Enable Bridge Mode\n", __func__);
  12518. } else {
  12519. rc = -EINVAL;
  12520. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12521. __func__, input_data[1]);
  12522. }
  12523. mutex_unlock(&(cd->system_lock));
  12524. break;
  12525. case PT_DRV_DBG_SET_I2C_ADDRESS: /* 212 */
  12526. mutex_lock(&cd->system_lock);
  12527. /* Only a 7bit address is valid */
  12528. if (input_data[1] >= 0 && input_data[1] <= 0x7F) {
  12529. client->addr = input_data[1];
  12530. pt_debug(dev, DL_INFO,
  12531. "%s: Set I2C Address: 0x%2X\n",
  12532. __func__, client->addr);
  12533. } else {
  12534. rc = -EINVAL;
  12535. pt_debug(dev, DL_ERROR, "%s: Invalid I2C Address %d\n",
  12536. __func__, input_data[1]);
  12537. client->addr = 0x24;
  12538. }
  12539. mutex_unlock(&(cd->system_lock));
  12540. break;
  12541. case PT_DRV_DBG_SET_FLASHLESS_DUT: /* 213 */
  12542. mutex_lock(&cd->system_lock);
  12543. if (input_data[1] == 0) {
  12544. cd->flashless_dut = 0;
  12545. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  12546. pt_debug(dev, DL_INFO, "%s: Disable FLAHLESS DUT\n",
  12547. __func__);
  12548. } else if (input_data[1] == 1) {
  12549. cd->flashless_dut = 1;
  12550. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  12551. pt_debug(dev, DL_INFO, "%s: Enable FLAHLESS DUT\n",
  12552. __func__);
  12553. }
  12554. mutex_unlock(&(cd->system_lock));
  12555. break;
  12556. case PT_DRV_DBG_SET_FORCE_SEQ: /* 214 */
  12557. mutex_lock(&cd->system_lock);
  12558. if (input_data[1] >= 0x8 && input_data[1] <= 0xF) {
  12559. cd->force_pip2_seq = input_data[1];
  12560. cd->pip2_cmd_tag_seq = input_data[1];
  12561. pt_debug(dev, DL_INFO,
  12562. "%s: Force PIP2 Seq to: 0x%02X\n",
  12563. __func__, input_data[1]);
  12564. } else {
  12565. cd->force_pip2_seq = 0;
  12566. pt_debug(dev, DL_INFO,
  12567. "%s: Clear Forced PIP2 Seq\n", __func__);
  12568. }
  12569. mutex_unlock(&(cd->system_lock));
  12570. break;
  12571. case PT_DRV_DBG_BL_WITH_NO_INT: /* 215 */
  12572. mutex_lock(&cd->system_lock);
  12573. if (input_data[1] == 0) {
  12574. cd->bl_with_no_int = 0;
  12575. pt_debug(dev, DL_INFO, "%s: BL using IRQ\n", __func__);
  12576. } else if (input_data[1] == 1) {
  12577. cd->bl_with_no_int = 1;
  12578. pt_debug(dev, DL_INFO, "%s: BL using Polling\n",
  12579. __func__);
  12580. } else {
  12581. rc = -EINVAL;
  12582. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12583. __func__, input_data[1]);
  12584. }
  12585. mutex_unlock(&(cd->system_lock));
  12586. break;
  12587. case PT_DRV_DBG_CAL_CACHE_IN_HOST: /* 216 */
  12588. mutex_lock(&cd->system_lock);
  12589. if (input_data[1] == 0) {
  12590. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  12591. pt_debug(dev, DL_INFO,
  12592. "%s: Disable Calibration cache in host\n",
  12593. __func__);
  12594. } else if (input_data[1] == 1) {
  12595. cd->cal_cache_in_host = PT_FEATURE_ENABLE;
  12596. pt_debug(dev, DL_INFO,
  12597. "%s: Enable Calibration cache in host\n",
  12598. __func__);
  12599. } else {
  12600. rc = -EINVAL;
  12601. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12602. __func__, input_data[1]);
  12603. }
  12604. mutex_unlock(&(cd->system_lock));
  12605. break;
  12606. case PT_DRV_DBG_MULTI_CHIP: /* 217 */
  12607. mutex_lock(&cd->system_lock);
  12608. if (input_data[1] == 0) {
  12609. cd->multi_chip = PT_FEATURE_DISABLE;
  12610. cd->ttdl_bist_select = 0x07;
  12611. pt_debug(dev, DL_INFO,
  12612. "%s: Disable Multi-chip support\n", __func__);
  12613. } else if (input_data[1] == 1) {
  12614. cd->multi_chip = PT_FEATURE_ENABLE;
  12615. cd->ttdl_bist_select = 0x3F;
  12616. pt_debug(dev, DL_INFO,
  12617. "%s: Enable Multi-chip support\n", __func__);
  12618. } else {
  12619. rc = -EINVAL;
  12620. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12621. __func__, input_data[1]);
  12622. }
  12623. mutex_unlock(&(cd->system_lock));
  12624. break;
  12625. case PT_DRV_DBG_SET_PANEL_ID_TYPE: /* 218 */
  12626. mutex_lock(&cd->system_lock);
  12627. if (input_data[1] <= 0x07) {
  12628. cd->panel_id_support = input_data[1];
  12629. pt_debug(dev, DL_INFO,
  12630. "%s: Set panel_id_support to %d\n",
  12631. __func__, cd->panel_id_support);
  12632. } else {
  12633. rc = -EINVAL;
  12634. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12635. __func__, input_data[1]);
  12636. }
  12637. mutex_unlock(&(cd->system_lock));
  12638. break;
  12639. case PT_DRV_DBG_PIP_TIMEOUT: /* 219 */
  12640. mutex_lock(&cd->system_lock);
  12641. if (input_data[1] >= 100 && input_data[1] <= 7000) {
  12642. /*
  12643. * The timeout is changed for some cases so the
  12644. * pip_cmd_timeout_default is used to retore back to
  12645. * what the user requested as the new timeout.
  12646. */
  12647. cd->pip_cmd_timeout_default = input_data[1];
  12648. cd->pip_cmd_timeout = input_data[1];
  12649. pt_debug(dev, DL_INFO,
  12650. "%s: PIP Timeout = %d\n", __func__,
  12651. cd->pip_cmd_timeout_default);
  12652. } else {
  12653. rc = -EINVAL;
  12654. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12655. __func__, input_data[1]);
  12656. }
  12657. mutex_unlock(&(cd->system_lock));
  12658. break;
  12659. case PT_DRV_DBG_TTHE_HID_USB_FORMAT: /* 220 */
  12660. mutex_lock(&cd->system_lock);
  12661. if (input_data[1] == 0) {
  12662. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  12663. pt_debug(dev, DL_INFO,
  12664. "%s: Disable tthe_tuner HID-USB format\n",
  12665. __func__);
  12666. } else if (input_data[1] == 1) {
  12667. cd->tthe_hid_usb_format = PT_FEATURE_ENABLE;
  12668. pt_debug(dev, DL_INFO,
  12669. "%s: Enable tthe_tuner HID-USB format\n",
  12670. __func__);
  12671. } else {
  12672. rc = -EINVAL;
  12673. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12674. __func__, input_data[1]);
  12675. }
  12676. mutex_unlock(&(cd->system_lock));
  12677. break;
  12678. #endif /* TTDL_DIAGNOSTICS */
  12679. default:
  12680. rc = -EINVAL;
  12681. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  12682. }
  12683. /* Enable watchdog timer */
  12684. if (!wd_disabled)
  12685. pt_start_wd_timer(cd);
  12686. pt_drv_debug_store_exit:
  12687. if (rc)
  12688. return rc;
  12689. return size;
  12690. }
  12691. static DEVICE_ATTR(drv_debug, 0644, pt_drv_debug_show,
  12692. pt_drv_debug_store);
  12693. /*******************************************************************************
  12694. * FUNCTION: pt_sleep_status_show
  12695. *
  12696. * SUMMARY: Show method for the sleep_status sysfs node that will show the
  12697. * sleep status as either ON or OFF
  12698. *
  12699. * RETURN:
  12700. * 0 = success
  12701. * !0 = failure
  12702. *
  12703. * PARAMETERS:
  12704. * *dev - pointer to device structure
  12705. * *attr - pointer to device attributes
  12706. * *buf - pointer to output buffer
  12707. ******************************************************************************/
  12708. static ssize_t pt_sleep_status_show(struct device *dev,
  12709. struct device_attribute *attr, char *buf)
  12710. {
  12711. struct pt_core_data *cd = dev_get_drvdata(dev);
  12712. ssize_t ret;
  12713. mutex_lock(&cd->system_lock);
  12714. if (cd->sleep_state == SS_SLEEP_ON)
  12715. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "off\n");
  12716. else
  12717. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "on\n");
  12718. mutex_unlock(&cd->system_lock);
  12719. return ret;
  12720. }
  12721. /*******************************************************************************
  12722. * FUNCTION: pt_panel_id_show
  12723. *
  12724. * SUMMARY: Show method for the panel_id sysfs node that will show the
  12725. * detected panel ID from the DUT
  12726. *
  12727. * RETURN:
  12728. * 0 = success
  12729. * !0 = failure
  12730. *
  12731. * PARAMETERS:
  12732. * *dev - pointer to device structure
  12733. * *attr - pointer to device attributes
  12734. * *buf - pointer to output buffer
  12735. ******************************************************************************/
  12736. static ssize_t pt_panel_id_show(struct device *dev,
  12737. struct device_attribute *attr, char *buf)
  12738. {
  12739. struct pt_core_data *cd = dev_get_drvdata(dev);
  12740. ssize_t ret;
  12741. u8 pid = PANEL_ID_NOT_ENABLED;
  12742. int rc = 0;
  12743. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  12744. /*
  12745. * The DUT should report the same panel ID from both the BL and
  12746. * the FW unless the panel_id feature is set to only
  12747. * PT_PANEL_ID_BY_SYS_INFO, in which case the BL is not able
  12748. * to retrieve the panel_id.
  12749. */
  12750. if (cd->mode == PT_MODE_BOOTLOADER) {
  12751. /*
  12752. * Return the stored value if PT_PANEL_ID_BY_BL
  12753. * is not supported while other feature exits.
  12754. */
  12755. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  12756. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  12757. if (rc) {
  12758. pt_debug(dev, DL_ERROR, "%s: %s %s\n",
  12759. "Failed to retrieve Panel ID. ",
  12760. "Using cached value\n",
  12761. __func__);
  12762. }
  12763. }
  12764. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  12765. if (cd->panel_id_support &
  12766. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  12767. /* For all systems sysinfo has the panel_id */
  12768. rc = pt_hid_output_get_sysinfo(cd);
  12769. if (!rc)
  12770. pid =
  12771. cd->sysinfo.sensing_conf_data.panel_id;
  12772. pt_debug(dev, DL_ERROR,
  12773. "%s: Gen6 FW mode rc=%d PID=0x%02X\n",
  12774. __func__, rc, pid);
  12775. }
  12776. } else {
  12777. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  12778. __func__);
  12779. rc = -EPERM;
  12780. }
  12781. } else if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  12782. if (cd->mode == PT_MODE_BOOTLOADER) {
  12783. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  12784. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  12785. if (rc) {
  12786. pt_debug(dev, DL_ERROR,
  12787. "%s: BL get panel ID failed rc=%d\n",
  12788. __func__, rc);
  12789. }
  12790. }
  12791. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  12792. if (cd->panel_id_support &
  12793. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  12794. rc = pt_hid_output_get_sysinfo(cd);
  12795. if (!rc)
  12796. pid =
  12797. cd->sysinfo.sensing_conf_data.panel_id;
  12798. pt_debug(dev, DL_ERROR,
  12799. "%s: TT/TC FW mode rc=%d PID=0x%02X\n",
  12800. __func__, rc, pid);
  12801. }
  12802. } else {
  12803. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  12804. __func__);
  12805. rc = -EPERM;
  12806. }
  12807. } else {
  12808. pt_debug(dev, DL_ERROR, "%s: Dut generation is unknown\n",
  12809. __func__);
  12810. rc = -EPERM;
  12811. }
  12812. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n0x%02X\n",
  12813. rc, pid);
  12814. return ret;
  12815. }
  12816. /*******************************************************************************
  12817. * FUNCTION: pt_get_param_store
  12818. *
  12819. * SUMMARY: Store method for the get_param sysfs node. Stores what parameter
  12820. * ID to retrieve with the show method.
  12821. *
  12822. * NOTE: This sysfs node is only available after a DUT has been enumerated
  12823. *
  12824. * RETURN: Size of passed in buffer if successful
  12825. *
  12826. * PARAMETERS:
  12827. * *dev - pointer to device structure
  12828. * *attr - pointer to device attributes
  12829. * *buf - pointer to output buffer
  12830. * size - size of buf
  12831. ******************************************************************************/
  12832. static ssize_t pt_get_param_store(struct device *dev,
  12833. struct device_attribute *attr, const char *buf, size_t size)
  12834. {
  12835. struct pt_core_data *cd = dev_get_drvdata(dev);
  12836. u32 input_data[2];
  12837. int length;
  12838. int rc = 0;
  12839. /* Maximum input of one value */
  12840. length = _pt_ic_parse_input(dev, buf, size, input_data,
  12841. ARRAY_SIZE(input_data));
  12842. if (length != 1) {
  12843. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12844. __func__);
  12845. rc = -EINVAL;
  12846. goto exit;
  12847. }
  12848. mutex_lock(&cd->system_lock);
  12849. cd->get_param_id = input_data[0];
  12850. mutex_unlock(&(cd->system_lock));
  12851. exit:
  12852. if (rc)
  12853. return rc;
  12854. return size;
  12855. }
  12856. /*******************************************************************************
  12857. * FUNCTION: pt_get_param_show
  12858. *
  12859. * SUMMARY: Show method for the get_param sysfs node. Retrieves the
  12860. * parameter data from the DUT based on the ID stored in the core
  12861. * data variable "get_param_id". If the ID is invalid, the DUT cannot
  12862. * communicate or some other error occures, an error status is returned
  12863. * with no value following.
  12864. * Output is in the form:
  12865. * Status: x
  12866. * 0xyyyyyyyy
  12867. * The 32bit data will only follow the status code if the status == 0
  12868. *
  12869. * NOTE: This sysfs node is only available after a DUT has been enumerated
  12870. *
  12871. * RETURN:
  12872. * 0 = success
  12873. * !0 = failure
  12874. *
  12875. * PARAMETERS:
  12876. * *dev - pointer to device structure
  12877. * *attr - pointer to device attributes
  12878. * *buf - pointer to output buffer
  12879. ******************************************************************************/
  12880. static ssize_t pt_get_param_show(struct device *dev,
  12881. struct device_attribute *attr, char *buf)
  12882. {
  12883. struct pt_core_data *cd = dev_get_drvdata(dev);
  12884. ssize_t ret = 0;
  12885. int status;
  12886. u32 value = 0;
  12887. status = pt_pip_get_param(cd, cd->get_param_id, &value);
  12888. if (status) {
  12889. pt_debug(dev, DL_ERROR, "%s: %s Failed, status = %d\n",
  12890. __func__, "pt_get_param", status);
  12891. ret = scnprintf(buf, strlen(buf),
  12892. "%s %d\n",
  12893. "Status:", status);
  12894. } else {
  12895. pt_debug(dev, DL_DEBUG, "%s: Param [%d] = 0x%04X\n",
  12896. __func__, cd->get_param_id, value);
  12897. ret = scnprintf(buf, strlen(buf),
  12898. "Status: %d\n"
  12899. "0x%04X\n",
  12900. status, value);
  12901. }
  12902. return ret;
  12903. }
  12904. /*******************************************************************************
  12905. * FUNCTION: pt_ttdl_restart_show
  12906. *
  12907. * SUMMARY: Show method for ttdl_restart sysfs node. This node releases all
  12908. * probed modules, calls startup() and then re-probes modules.
  12909. *
  12910. * RETURN: size of data written to sysfs node
  12911. *
  12912. * PARAMETERS:
  12913. * *dev - pointer to device structure
  12914. * *attr - pointer to device attributes structure
  12915. * *buf - pointer to print output buffer
  12916. ******************************************************************************/
  12917. static ssize_t pt_ttdl_restart_show(struct device *dev,
  12918. struct device_attribute *attr, char *buf)
  12919. {
  12920. struct pt_core_data *cd = dev_get_drvdata(dev);
  12921. int t;
  12922. int rc = 0;
  12923. mutex_lock(&cd->system_lock);
  12924. cd->startup_state = STARTUP_NONE;
  12925. mutex_unlock(&(cd->system_lock));
  12926. /* ensure no left over exclusive access is still locked */
  12927. release_exclusive(cd, cd->dev);
  12928. pt_queue_restart(cd);
  12929. t = wait_event_timeout(cd->wait_q,
  12930. (cd->startup_status >= STARTUP_STATUS_COMPLETE),
  12931. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  12932. if (IS_TMO(t)) {
  12933. pt_debug(cd->dev, DL_ERROR,
  12934. "%s: TMO waiting for FW sentinel\n", __func__);
  12935. rc = -ETIME;
  12936. }
  12937. return scnprintf(buf, strlen(buf),
  12938. "Status: %d\n"
  12939. "Enum Status: 0x%04X\n", rc, cd->startup_status);
  12940. }
  12941. static DEVICE_ATTR(ttdl_restart, 0444, pt_ttdl_restart_show, NULL);
  12942. /*******************************************************************************
  12943. * FUNCTION: pt_pip2_gpio_read_show
  12944. *
  12945. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and prints the
  12946. * contents of the response to the passed in output buffer.
  12947. *
  12948. * RETURN: size of data written to sysfs node
  12949. *
  12950. * PARAMETERS:
  12951. * *dev - pointer to device structure
  12952. * *attr - pointer to device attributes structure
  12953. * *buf - pointer to print output buffer
  12954. ******************************************************************************/
  12955. static ssize_t pt_pip2_gpio_read_show(struct device *dev,
  12956. struct device_attribute *attr, char *buf)
  12957. {
  12958. struct pt_core_data *cd = dev_get_drvdata(dev);
  12959. u8 status = 0;
  12960. u32 gpio_value = 0;
  12961. int rc = 0;
  12962. /* This functionality is only available in the BL */
  12963. if (cd->mode == PT_MODE_BOOTLOADER)
  12964. rc = pt_pip2_read_gpio(dev, &status, &gpio_value);
  12965. else
  12966. rc = -EPERM;
  12967. if (!rc) {
  12968. if (status == 0)
  12969. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12970. "Status: %d\n"
  12971. "DUT GPIO Reg: 0x%08X\n",
  12972. rc, gpio_value);
  12973. else
  12974. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12975. "Status: %d\n"
  12976. "DUT GPIO Reg: n/a\n",
  12977. status);
  12978. } else
  12979. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12980. "Status: %d\n"
  12981. "DUT GPIO Reg: n/a\n",
  12982. rc);
  12983. }
  12984. /*******************************************************************************
  12985. * FUNCTION: pt_pip2_version_show
  12986. *
  12987. * SUMMARY: Sends a PIP2 VERSION command to the DUT and prints the
  12988. * contents of the response to the passed in output buffer.
  12989. *
  12990. * RETURN: size of data written to sysfs node
  12991. *
  12992. * PARAMETERS:
  12993. * *dev - pointer to device structure
  12994. * *attr - pointer to device attributes structure
  12995. * *buf - pointer to print output buffer
  12996. ******************************************************************************/
  12997. static ssize_t pt_pip2_version_show(struct device *dev,
  12998. struct device_attribute *attr, char *buf)
  12999. {
  13000. int rc = 0;
  13001. struct pt_core_data *cd = dev_get_drvdata(dev);
  13002. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  13003. rc = pt_pip2_get_version(cd);
  13004. if (!rc) {
  13005. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13006. "PIP VERSION : %02X.%02X\n"
  13007. "BL VERSION : %02X.%02X\n"
  13008. "FW VERSION : %02X.%02X\n"
  13009. "SILICON ID : %04X.%04X\n"
  13010. "UID : 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
  13011. ttdata->pip_ver_major, ttdata->pip_ver_minor,
  13012. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  13013. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  13014. ttdata->chip_id, ttdata->chip_rev,
  13015. ttdata->uid[0], ttdata->uid[1],
  13016. ttdata->uid[2], ttdata->uid[3],
  13017. ttdata->uid[4], ttdata->uid[5],
  13018. ttdata->uid[6], ttdata->uid[7],
  13019. ttdata->uid[8], ttdata->uid[9],
  13020. ttdata->uid[10], ttdata->uid[11]);
  13021. } else {
  13022. pt_debug(dev, DL_ERROR,
  13023. "%s: Failed to retriev PIP2 VERSION data\n", __func__);
  13024. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13025. "PIP VERSION : -\n"
  13026. "BL VERSION : -\n"
  13027. "FW VERSION : -\n"
  13028. "SILICON ID : -\n"
  13029. "UID : -\n");
  13030. }
  13031. }
  13032. #ifdef TTDL_DIAGNOSTICS
  13033. /*******************************************************************************
  13034. * FUNCTION: pt_ttdl_status_show
  13035. *
  13036. * SUMMARY: Show method for the ttdl_status sysfs node. Displays TTDL internal
  13037. * variable states and GPIO levels. Additional information printed when
  13038. * TTDL_DIAGNOSTICS is enabled.
  13039. *
  13040. * NOTE: All counters will be reset to 0 when this function is called.
  13041. *
  13042. * RETURN:
  13043. * 0 = success
  13044. * !0 = failure
  13045. *
  13046. * PARAMETERS:
  13047. * *dev - pointer to device structure
  13048. * *attr - pointer to device attributes
  13049. * *buf - pointer to output buffer
  13050. ******************************************************************************/
  13051. static ssize_t pt_ttdl_status_show(struct device *dev,
  13052. struct device_attribute *attr, char *buf)
  13053. {
  13054. struct pt_core_data *cd = dev_get_drvdata(dev);
  13055. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13056. struct i2c_client *client = to_i2c_client(dev);
  13057. ssize_t ret;
  13058. u16 cal_size = 0;
  13059. unsigned short crc = 0;
  13060. if (cd->cal_cache_in_host)
  13061. _pt_manage_local_cal_data(dev,
  13062. PT_CAL_DATA_INFO, &cal_size, &crc);
  13063. ret = scnprintf(buf, strlen(buf),
  13064. "%s: 0x%04X\n"
  13065. "%s: %d\n"
  13066. "%s: %s\n"
  13067. "%s: %s %s\n"
  13068. "%s: %s\n"
  13069. "%s: 0x%02X\n"
  13070. "%s: %s\n"
  13071. "%s: %s\n"
  13072. "%s: %s\n"
  13073. "%s: %s\n"
  13074. "%s: %d\n"
  13075. "%s: %d\n"
  13076. "%s: %s\n"
  13077. "%s: %s\n"
  13078. "%s: %d\n"
  13079. #ifdef TTDL_DIAGNOSTICS
  13080. "%s: %d\n"
  13081. "%s: %d\n"
  13082. "%s: %d\n"
  13083. "%s: %d\n"
  13084. "%s: %d\n"
  13085. "%s: %d\n"
  13086. "%s: %d\n"
  13087. "%s: %d\n"
  13088. "%s: %d\n"
  13089. "%s: %d\n"
  13090. "%s: %s\n"
  13091. "%s: %s\n"
  13092. "%s: %s\n"
  13093. "%s: %d\n"
  13094. "%s: 0x%04X\n"
  13095. "%s: %s\n"
  13096. #endif /* TTDL_DIAGNOSTICS */
  13097. ,
  13098. "Startup Status ", cd->startup_status,
  13099. "TTDL Debug Level ", cd->debug_level,
  13100. "Mode ",
  13101. cd->mode ? (cd->mode == PT_MODE_OPERATIONAL ?
  13102. "Operational" : "BL") : "Unknown",
  13103. "DUT Generation ",
  13104. cd->active_dut_generation ?
  13105. (cd->active_dut_generation == DUT_PIP2_CAPABLE ?
  13106. "PT TC/TT" : "Gen5/6") : "Unknown",
  13107. cd->active_dut_generation ?
  13108. (cd->set_dut_generation == true ?
  13109. "(Set)" : "(Detected)") : "",
  13110. "HW Detected ",
  13111. cd->hw_detected ? "True" : "False",
  13112. "I2C Address ",
  13113. cd->bus_ops->bustype == BUS_I2C ? client->addr : 0,
  13114. "Active Bus Module ",
  13115. cd->bus_ops->bustype == BUS_I2C ? "I2C" : "SPI",
  13116. "Flashless Mode ",
  13117. cd->flashless_dut == 1 ? "Yes" : "No",
  13118. "GPIO state - IRQ ",
  13119. cd->cpdata->irq_stat ?
  13120. (cd->cpdata->irq_stat(cd->cpdata, dev) ?
  13121. "High" : "Low") : "not defined",
  13122. "GPIO state - TP_XRES ",
  13123. pdata->core_pdata->rst_gpio ?
  13124. (gpio_get_value(pdata->core_pdata->rst_gpio) ?
  13125. "High" : "Low") : "not defined",
  13126. "RAM Parm restore list ", pt_count_parameter_list(cd),
  13127. "Startup Retry Count ", cd->startup_retry_count,
  13128. "WD - Manual Force Stop ",
  13129. cd->watchdog_force_stop ? "True" : "False",
  13130. "WD - Enabled ",
  13131. cd->watchdog_enabled ? "True" : "False",
  13132. "WD - Interval (ms) ", cd->watchdog_interval
  13133. #ifdef TTDL_DIAGNOSTICS
  13134. , "WD - Triggered Count ", cd->watchdog_count,
  13135. "WD - IRQ Stuck low count ", cd->watchdog_irq_stuck_count,
  13136. "WD - Device Access Errors ", cd->watchdog_failed_access_count,
  13137. "WD - XRES Count ", cd->wd_xres_count,
  13138. "IRQ Triggered Count ", cd->irq_count,
  13139. "BL Packet Retry Count ", cd->bl_retry_packet_count,
  13140. "PIP2 CRC Error Count ", cd->pip2_crc_error_count,
  13141. "Bus Transmit Error Count ", cd->bus_transmit_error_count,
  13142. "File Erase Timeout Count ", cd->file_erase_timeout_count,
  13143. "Error GPIO trigger type ", cd->err_gpio_type,
  13144. "Exclusive Access Lock ", cd->exclusive_dev ? "Set":"Free",
  13145. "Suppress No-Flash Auto BL ",
  13146. cd->flashless_auto_bl == PT_SUPPRESS_AUTO_BL ?
  13147. "Yes" : "No",
  13148. "Calibration Cache on host ",
  13149. cd->cal_cache_in_host == PT_FEATURE_ENABLE ?
  13150. "Yes" : "No",
  13151. "Calibration Cache size ", cal_size,
  13152. "Calibration Cache chip ID ", crc,
  13153. "Multi-Chip Support ",
  13154. cd->multi_chip == PT_FEATURE_ENABLE ? "Yes" : "No"
  13155. #endif /* TTDL_DIAGNOSTICS */
  13156. );
  13157. #ifdef TTDL_DIAGNOSTICS
  13158. /* Reset all diagnostic counters */
  13159. cd->watchdog_count = 0;
  13160. cd->watchdog_irq_stuck_count = 0;
  13161. cd->watchdog_failed_access_count = 0;
  13162. cd->wd_xres_count = 0;
  13163. cd->irq_count = 0;
  13164. cd->bl_retry_packet_count = 0;
  13165. cd->pip2_crc_error_count = 0;
  13166. cd->bus_transmit_error_count = 0;
  13167. #endif
  13168. return ret;
  13169. }
  13170. static DEVICE_ATTR(ttdl_status, 0444, pt_ttdl_status_show, NULL);
  13171. /*******************************************************************************
  13172. * FUNCTION: pt_pip2_enter_bl_show
  13173. *
  13174. * SUMMARY: Show method for the pip2_enter_bl sysfs node that will force
  13175. * the DUT into the BL and show the success or failure of entering the BL
  13176. *
  13177. * RETURN:
  13178. * 0 = success
  13179. * !0 = failure
  13180. *
  13181. * PARAMETERS:
  13182. * *dev - pointer to device structure
  13183. * *attr - pointer to device attributes
  13184. * *buf - pointer to output buffer
  13185. ******************************************************************************/
  13186. static ssize_t pt_pip2_enter_bl_show(struct device *dev,
  13187. struct device_attribute *attr, char *buf)
  13188. {
  13189. ssize_t ret = 0;
  13190. int rc = 0;
  13191. int result = 0;
  13192. u8 mode = PT_MODE_UNKNOWN;
  13193. struct pt_core_data *cd = dev_get_drvdata(dev);
  13194. /* Turn off the TTDL WD before enter bootloader */
  13195. pt_stop_wd_timer(cd);
  13196. /* Ensure NO enumeration work is queued or will be queued */
  13197. cancel_work_sync(&cd->enum_work);
  13198. mutex_lock(&cd->system_lock);
  13199. cd->bridge_mode = true;
  13200. mutex_unlock(&cd->system_lock);
  13201. /* set mode to operational to avoid any extra PIP traffic */
  13202. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  13203. switch (result) {
  13204. case PT_ENTER_BL_PASS:
  13205. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13206. "Status: %d\nEntered BL\n", PT_ENTER_BL_PASS);
  13207. break;
  13208. case PT_ENTER_BL_ERROR:
  13209. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13210. rc,
  13211. " Unknown Error");
  13212. break;
  13213. case PT_ENTER_BL_RESET_FAIL:
  13214. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13215. rc,
  13216. " Soft Reset Failed");
  13217. break;
  13218. case PT_ENTER_BL_HID_START_BL_FAIL:
  13219. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13220. rc,
  13221. " PIP Start BL Cmd Failed");
  13222. break;
  13223. case PT_ENTER_BL_CONFIRM_FAIL:
  13224. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13225. rc,
  13226. " Error confirming DUT entered BL");
  13227. break;
  13228. default:
  13229. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13230. rc, " Unknown Error");
  13231. break;
  13232. };
  13233. /* Allow enumeration work to be queued again */
  13234. cd->bridge_mode = false;
  13235. return ret;
  13236. }
  13237. static DEVICE_ATTR(pip2_enter_bl, 0444, pt_pip2_enter_bl_show, NULL);
  13238. /*******************************************************************************
  13239. * FUNCTION: pt_pip2_exit_bl_show
  13240. *
  13241. * SUMMARY: Show method for the pip2_exit_bl sysfs node that will attempt to
  13242. * launch the APP and put the DUT Application mode
  13243. *
  13244. * RETURN:
  13245. * 0 = success
  13246. * !0 = failure
  13247. *
  13248. * PARAMETERS:
  13249. * *dev - pointer to device structure
  13250. * *attr - pointer to device attributes
  13251. * *buf - pointer to output buffer
  13252. ******************************************************************************/
  13253. static ssize_t pt_pip2_exit_bl_show(struct device *dev,
  13254. struct device_attribute *attr, char *buf)
  13255. {
  13256. struct pt_core_data *cd = dev_get_drvdata(dev);
  13257. ssize_t ret = 0;
  13258. int rc = 0;
  13259. u8 status_str[PT_STATUS_STR_LEN];
  13260. rc = pt_pip2_exit_bl_(cd, status_str, PT_STATUS_STR_LEN);
  13261. /*
  13262. * Perform enum if startup_status doesn't reach to
  13263. * STARTUP_STATUS_FW_OUT_OF_BOOT.
  13264. */
  13265. if (!rc && (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT))) {
  13266. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  13267. if (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  13268. strlcpy(status_str,
  13269. "Already in APP mode - FW stuck in Boot mode", sizeof(status_str));
  13270. }
  13271. }
  13272. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13273. rc, status_str);
  13274. return ret;
  13275. }
  13276. static DEVICE_ATTR(pip2_exit_bl, 0444, pt_pip2_exit_bl_show, NULL);
  13277. #endif
  13278. #ifdef EASYWAKE_TSG6
  13279. /*******************************************************************************
  13280. * FUNCTION: pt_easy_wakeup_gesture_show
  13281. *
  13282. * SUMMARY: Show method for the easy_wakeup_gesture sysfs node that will show
  13283. * current easy wakeup gesture
  13284. *
  13285. * RETURN:
  13286. * 0 = success
  13287. * !0 = failure
  13288. *
  13289. * PARAMETERS:
  13290. * *dev - pointer to device structure
  13291. * *attr - pointer to device attributes
  13292. * *buf - pointer to output buffer
  13293. ******************************************************************************/
  13294. static ssize_t pt_easy_wakeup_gesture_show(struct device *dev,
  13295. struct device_attribute *attr, char *buf)
  13296. {
  13297. struct pt_core_data *cd = dev_get_drvdata(dev);
  13298. ssize_t ret;
  13299. mutex_lock(&cd->system_lock);
  13300. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "0x%02X\n",
  13301. cd->easy_wakeup_gesture);
  13302. mutex_unlock(&cd->system_lock);
  13303. return ret;
  13304. }
  13305. /*******************************************************************************
  13306. * FUNCTION: pt_easy_wakeup_gesture_store
  13307. *
  13308. * SUMMARY: The store method for the easy_wakeup_gesture sysfs node that
  13309. * allows the wake gesture to be set to a custom value.
  13310. *
  13311. * RETURN: Size of passed in buffer is success
  13312. *
  13313. * PARAMETERS:
  13314. * *dev - pointer to device structure
  13315. * *attr - pointer to device attributes
  13316. * *buf - pointer to buffer that hold the command parameters
  13317. * size - size of buf
  13318. ******************************************************************************/
  13319. static ssize_t pt_easy_wakeup_gesture_store(struct device *dev,
  13320. struct device_attribute *attr, const char *buf, size_t size)
  13321. {
  13322. struct pt_core_data *cd = dev_get_drvdata(dev);
  13323. u32 input_data[2];
  13324. int length;
  13325. int rc = 0;
  13326. /* Maximum input of one value */
  13327. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13328. ARRAY_SIZE(input_data));
  13329. if (length != 1) {
  13330. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13331. __func__);
  13332. rc = -EINVAL;
  13333. goto exit;
  13334. }
  13335. pt_debug(dev, DL_INFO, "%s: features.easywake = 0x%02X\n",
  13336. __func__, cd->features.easywake);
  13337. if (!cd->features.easywake || input_data[0] > 0xFF) {
  13338. rc = -EINVAL;
  13339. goto exit;
  13340. }
  13341. pm_runtime_get_sync(dev);
  13342. mutex_lock(&cd->system_lock);
  13343. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 2)) {
  13344. cd->easy_wakeup_gesture = (u8)input_data[0];
  13345. pt_debug(dev, DL_INFO,
  13346. "%s: Updated easy_wakeup_gesture = 0x%02X\n",
  13347. __func__, cd->easy_wakeup_gesture);
  13348. } else
  13349. rc = -ENODEV;
  13350. mutex_unlock(&cd->system_lock);
  13351. pm_runtime_put(dev);
  13352. exit:
  13353. if (rc)
  13354. return rc;
  13355. return size;
  13356. }
  13357. /*******************************************************************************
  13358. * FUNCTION: pt_easy_wakeup_gesture_id_show
  13359. *
  13360. * SUMMARY: Show method for the easy_wakeup_gesture_id sysfs node that will
  13361. * show the TSG6 easywake gesture ID
  13362. *
  13363. * RETURN:
  13364. * 0 = success
  13365. * !0 = failure
  13366. *
  13367. * PARAMETERS:
  13368. * *dev - pointer to device structure
  13369. * *attr - pointer to device attributes
  13370. * *buf - pointer to output buffer
  13371. ******************************************************************************/
  13372. static ssize_t pt_easy_wakeup_gesture_id_show(struct device *dev,
  13373. struct device_attribute *attr, char *buf)
  13374. {
  13375. struct pt_core_data *cd = dev_get_drvdata(dev);
  13376. ssize_t ret;
  13377. mutex_lock(&cd->system_lock);
  13378. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: 0\n0x%02X\n",
  13379. cd->gesture_id);
  13380. mutex_unlock(&cd->system_lock);
  13381. return ret;
  13382. }
  13383. /*******************************************************************************
  13384. * FUNCTION: pt_easy_wakeup_gesture_data_show
  13385. *
  13386. * SUMMARY: Show method for the easy_wakeup_gesture_data sysfs node that will
  13387. * show the TSG6 easywake gesture data in the following format:
  13388. * x1(LSB),x1(MSB), y1(LSB),y1(MSB), x2(LSB),x2(MSB), y2(LSB),y2(MSB),...
  13389. *
  13390. * RETURN:
  13391. * 0 = success
  13392. * !0 = failure
  13393. *
  13394. * PARAMETERS:
  13395. * *dev - pointer to device structure
  13396. * *attr - pointer to device attributes
  13397. * *buf - pointer to output buffer
  13398. ******************************************************************************/
  13399. static ssize_t pt_easy_wakeup_gesture_data_show(struct device *dev,
  13400. struct device_attribute *attr, char *buf)
  13401. {
  13402. struct pt_core_data *cd = dev_get_drvdata(dev);
  13403. ssize_t ret = 0;
  13404. int i;
  13405. mutex_lock(&cd->system_lock);
  13406. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret, "Status: %d\n", 0);
  13407. for (i = 0; i < cd->gesture_data_length; i++)
  13408. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13409. "0x%02X\n", cd->gesture_data[i]);
  13410. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13411. "(%d bytes)\n", cd->gesture_data_length);
  13412. mutex_unlock(&cd->system_lock);
  13413. return ret;
  13414. }
  13415. #endif /* EASYWAKE_TSG6 */
  13416. #ifdef TTDL_DIAGNOSTICS
  13417. /*******************************************************************************
  13418. * FUNCTION: pt_err_gpio_show
  13419. *
  13420. * SUMMARY: Show method for the err_gpio sysfs node that will show if
  13421. * setting up the gpio was successful
  13422. *
  13423. * RETURN: Char buffer with printed GPIO creation state
  13424. *
  13425. * PARAMETERS:
  13426. * *dev - pointer to device structure
  13427. * *attr - pointer to device attributes
  13428. * *buf - pointer to output buffer
  13429. ******************************************************************************/
  13430. static ssize_t pt_err_gpio_show(struct device *dev,
  13431. struct device_attribute *attr, char *buf)
  13432. {
  13433. struct pt_core_data *cd = dev_get_drvdata(dev);
  13434. return scnprintf(buf, strlen(buf), "Status: 0\n"
  13435. "Err GPIO (%d) : %s\n"
  13436. "Err GPIO trig type: %d\n",
  13437. cd->err_gpio,
  13438. (cd->err_gpio ? (gpio_get_value(cd->err_gpio) ?
  13439. "HIGH" : "low") : "not defined"),
  13440. cd->err_gpio_type);
  13441. }
  13442. /*******************************************************************************
  13443. * FUNCTION: pt_err_gpio_store
  13444. *
  13445. * SUMMARY: The store method for the err_gpio sysfs node that allows any
  13446. * available host GPIO to be used to trigger when TTDL detects a PIP
  13447. * command/response timeout.
  13448. *
  13449. * RETURN:
  13450. * 0 = success
  13451. * !0 = failure
  13452. *
  13453. * PARAMETERS:
  13454. * *dev - pointer to device structure
  13455. * *attr - pointer to device attributes
  13456. * *buf - pointer to buffer that hold the command parameters
  13457. * size - size of buf
  13458. ******************************************************************************/
  13459. static ssize_t pt_err_gpio_store(struct device *dev,
  13460. struct device_attribute *attr, const char *buf, size_t size)
  13461. {
  13462. struct pt_core_data *cd = dev_get_drvdata(dev);
  13463. unsigned long gpio;
  13464. int rc = 0;
  13465. u32 input_data[3];
  13466. int length;
  13467. u8 err_type;
  13468. input_data[0] = 0;
  13469. input_data[1] = 0;
  13470. /* Maximmum input is two elements */
  13471. length = _pt_ic_parse_input(dev, buf, size,
  13472. input_data, ARRAY_SIZE(input_data));
  13473. if (length < 1 || length > 2) {
  13474. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13475. __func__);
  13476. rc = -EINVAL;
  13477. goto exit;
  13478. }
  13479. gpio = input_data[0];
  13480. err_type = (u8)input_data[1];
  13481. if (err_type < 0 || err_type > PT_ERR_GPIO_MAX_TYPE) {
  13482. rc = -EINVAL;
  13483. goto exit;
  13484. }
  13485. mutex_lock(&cd->system_lock);
  13486. gpio_free(gpio);
  13487. rc = gpio_request(gpio, NULL);
  13488. if (rc) {
  13489. pt_debug(dev, DL_ERROR, "error requesting gpio %lu\n", gpio);
  13490. rc = -ENODEV;
  13491. } else {
  13492. cd->err_gpio = gpio;
  13493. cd->err_gpio_type = err_type;
  13494. gpio_direction_output(gpio, 0);
  13495. }
  13496. mutex_unlock(&cd->system_lock);
  13497. exit:
  13498. if (rc)
  13499. return rc;
  13500. return size;
  13501. }
  13502. static DEVICE_ATTR(err_gpio, 0644, pt_err_gpio_show,
  13503. pt_err_gpio_store);
  13504. /*******************************************************************************
  13505. * FUNCTION: pt_drv_irq_show
  13506. *
  13507. * SUMMARY: Show method for the drv_irq sysfs node that will show if the
  13508. * TTDL interrupt is enabled/disabled
  13509. *
  13510. * RETURN:
  13511. * 0 = success
  13512. * !0 = failure
  13513. *
  13514. * PARAMETERS:
  13515. * *dev - pointer to device structure
  13516. * *attr - pointer to device attributes
  13517. * *buf - pointer to output buffer
  13518. ******************************************************************************/
  13519. static ssize_t pt_drv_irq_show(struct device *dev,
  13520. struct device_attribute *attr, char *buf)
  13521. {
  13522. struct pt_core_data *cd = dev_get_drvdata(dev);
  13523. ssize_t ret = 0;
  13524. mutex_lock(&cd->system_lock);
  13525. ret += scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n", 0);
  13526. if (cd->irq_enabled)
  13527. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13528. "Driver interrupt: ENABLED\n");
  13529. else
  13530. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13531. "Driver interrupt: DISABLED\n");
  13532. mutex_unlock(&cd->system_lock);
  13533. return ret;
  13534. }
  13535. /*******************************************************************************
  13536. * FUNCTION: pt_drv_irq_store
  13537. *
  13538. * SUMMARY: The store method for the drv_irq sysfs node that allows the TTDL
  13539. * IRQ to be enabled/disabled.
  13540. *
  13541. * RETURN: Size of passed in buffer
  13542. *
  13543. * PARAMETERS:
  13544. * *dev - pointer to device structure
  13545. * *attr - pointer to device attributes
  13546. * *buf - pointer to buffer that hold the command parameters
  13547. * size - size of buf
  13548. ******************************************************************************/
  13549. static ssize_t pt_drv_irq_store(struct device *dev,
  13550. struct device_attribute *attr, const char *buf, size_t size)
  13551. {
  13552. struct pt_core_data *cd = dev_get_drvdata(dev);
  13553. u32 input_data[2];
  13554. int length;
  13555. int rc = 0;
  13556. /* Maximum input of one value */
  13557. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13558. ARRAY_SIZE(input_data));
  13559. if (length != 1) {
  13560. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13561. __func__);
  13562. rc = -EINVAL;
  13563. goto exit;
  13564. }
  13565. mutex_lock(&cd->system_lock);
  13566. switch (input_data[0]) {
  13567. case 0:
  13568. if (cd->irq_enabled) {
  13569. cd->irq_enabled = false;
  13570. /* Disable IRQ has no return value to check */
  13571. disable_irq_nosync(cd->irq);
  13572. pt_debug(dev, DL_INFO,
  13573. "%s: Driver IRQ now disabled\n",
  13574. __func__);
  13575. } else
  13576. pt_debug(dev, DL_INFO,
  13577. "%s: Driver IRQ already disabled\n",
  13578. __func__);
  13579. break;
  13580. case 1:
  13581. if (cd->irq_enabled == false) {
  13582. cd->irq_enabled = true;
  13583. enable_irq(cd->irq);
  13584. pt_debug(dev, DL_INFO,
  13585. "%s: Driver IRQ now enabled\n",
  13586. __func__);
  13587. } else
  13588. pt_debug(dev, DL_INFO,
  13589. "%s: Driver IRQ already enabled\n",
  13590. __func__);
  13591. break;
  13592. default:
  13593. rc = -EINVAL;
  13594. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  13595. }
  13596. mutex_unlock(&(cd->system_lock));
  13597. exit:
  13598. if (rc)
  13599. return rc;
  13600. return size;
  13601. }
  13602. /*******************************************************************************
  13603. * FUNCTION: pt_pip2_bin_hdr_show
  13604. *
  13605. * SUMMARY: Show method for the pip2_bin_hdr sysfs node that will read
  13606. * the bin file header from flash and show each field
  13607. *
  13608. * RETURN:
  13609. * 0 = success
  13610. * !0 = failure
  13611. *
  13612. * PARAMETERS:
  13613. * *dev - pointer to device structure
  13614. * *attr - pointer to device attributes
  13615. * *buf - pointer to output buffer
  13616. ******************************************************************************/
  13617. static ssize_t pt_pip2_bin_hdr_show(struct device *dev,
  13618. struct device_attribute *attr, char *buf)
  13619. {
  13620. ssize_t ret = 0;
  13621. struct pt_bin_file_hdr hdr = {0};
  13622. int rc;
  13623. rc = _pt_request_pip2_bin_hdr(dev, &hdr);
  13624. ret = scnprintf(buf, strlen(buf),
  13625. "%s: %d\n"
  13626. "%s: %d\n"
  13627. "%s: 0x%04X\n"
  13628. "%s: %d\n"
  13629. "%s: %d\n"
  13630. "%s: %d\n"
  13631. "%s: 0x%08X\n"
  13632. "%s: 0x%04X\n"
  13633. "%s: 0x%04X\n"
  13634. "%s: %d\n"
  13635. "%s: %d\n",
  13636. "Status", rc,
  13637. "Header Length ", hdr.length,
  13638. "TTPID ", hdr.ttpid,
  13639. "FW Major Ver ", hdr.fw_major,
  13640. "FW Minor Ver ", hdr.fw_minor,
  13641. "FW Rev Control ", hdr.fw_rev_ctrl,
  13642. "FW CRC ", hdr.fw_crc,
  13643. "Silicon Rev ", hdr.si_rev,
  13644. "Silicon ID ", hdr.si_id,
  13645. "Config Ver ", hdr.config_ver,
  13646. "HEX File Size ", hdr.hex_file_size
  13647. );
  13648. return ret;
  13649. }
  13650. /*******************************************************************************
  13651. * FUNCTION: pt_platform_data_show
  13652. *
  13653. * SUMMARY: Show method for the platform_data sysfs node that will show the
  13654. * active platform data including: GPIOs, Vendor and Product IDs,
  13655. * Virtual Key coordinates, Core/MT/Loader flags, Level trigger delay,
  13656. * HID registers, and Easy wake gesture
  13657. *
  13658. * RETURN: Size of printed data
  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_platform_data_show(struct device *dev,
  13666. struct device_attribute *attr, char *buf)
  13667. {
  13668. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13669. struct pt_core_data *cd = dev_get_drvdata(dev);
  13670. ssize_t ret;
  13671. ret = scnprintf(buf, strlen(buf),
  13672. "%s: %d\n"
  13673. "%s: %d\n"
  13674. "%s: %d\n"
  13675. "%s: %d\n"
  13676. "%s: %d\n"
  13677. "%s: %d\n"
  13678. "%s: %d\n"
  13679. "%s: %d\n"
  13680. "%s: %d\n"
  13681. "%s: %d\n"
  13682. "%s: %d\n"
  13683. "%s: %d\n"
  13684. "%s: %d\n"
  13685. "%s: %d\n"
  13686. "%s: %d\n"
  13687. "%s: %d\n"
  13688. "%s: %d\n"
  13689. "%s: %d\n"
  13690. "%s: %d\n"
  13691. "%s: %d\n"
  13692. "%s: %d\n"
  13693. "%s: %d\n"
  13694. "%s: %d\n"
  13695. "%s: %d\n"
  13696. "%s: %d\n"
  13697. "%s: %d\n"
  13698. "%s: %d\n"
  13699. "%s: %s\n"
  13700. "%s: %s\n"
  13701. "%s: %d\n",
  13702. "Status", 0,
  13703. "Interrupt GPIO ", pdata->core_pdata->irq_gpio,
  13704. "Interrupt GPIO Value ",
  13705. pdata->core_pdata->irq_gpio ?
  13706. gpio_get_value(pdata->core_pdata->irq_gpio) : 0,
  13707. "Reset GPIO ", pdata->core_pdata->rst_gpio,
  13708. "Reset GPIO Value ",
  13709. pdata->core_pdata->rst_gpio ?
  13710. gpio_get_value(pdata->core_pdata->rst_gpio) : 0,
  13711. "DDI Reset GPIO ", pdata->core_pdata->ddi_rst_gpio,
  13712. "DDI Reset GPIO Value ",
  13713. pdata->core_pdata->ddi_rst_gpio ?
  13714. gpio_get_value(pdata->core_pdata->ddi_rst_gpio) : 0,
  13715. "VDDI GPIO ", pdata->core_pdata->vddi_gpio,
  13716. "VDDI GPIO Value ",
  13717. pdata->core_pdata->vddi_gpio ?
  13718. gpio_get_value(pdata->core_pdata->vddi_gpio) : 0,
  13719. "VCC GPIO ", pdata->core_pdata->vcc_gpio,
  13720. "VCC GPIO Value ",
  13721. pdata->core_pdata->vcc_gpio ?
  13722. gpio_get_value(pdata->core_pdata->vcc_gpio) : 0,
  13723. "AVDD GPIO ", pdata->core_pdata->avdd_gpio,
  13724. "AVDD GPIO Value ",
  13725. pdata->core_pdata->avdd_gpio ?
  13726. gpio_get_value(pdata->core_pdata->avdd_gpio) : 0,
  13727. "AVEE GPIO ", pdata->core_pdata->avee_gpio,
  13728. "AVEE GPIO Value ",
  13729. pdata->core_pdata->avee_gpio ?
  13730. gpio_get_value(pdata->core_pdata->avee_gpio) : 0,
  13731. "Vendor ID ", pdata->core_pdata->vendor_id,
  13732. "Product ID ", pdata->core_pdata->product_id,
  13733. "Vkeys x ", pdata->mt_pdata->vkeys_x,
  13734. "Vkeys y ", pdata->mt_pdata->vkeys_y,
  13735. "Core data flags ", pdata->core_pdata->flags,
  13736. "MT data flags ", pdata->mt_pdata->flags,
  13737. "Loader data flags ", pdata->loader_pdata->flags,
  13738. "Level trigger delay (us) ",
  13739. pdata->core_pdata->level_irq_udelay,
  13740. "HID Descriptor Register ",
  13741. pdata->core_pdata->hid_desc_register,
  13742. "HID Output Register ",
  13743. cd->hid_desc.output_register,
  13744. "HID Command Register ",
  13745. cd->hid_desc.command_register,
  13746. "Easy wakeup gesture ",
  13747. pdata->core_pdata->easy_wakeup_gesture,
  13748. "Config DUT generation ",
  13749. pdata->core_pdata->config_dut_generation ?
  13750. (pdata->core_pdata->config_dut_generation ==
  13751. CONFIG_DUT_PIP2_CAPABLE ?
  13752. "PT TC/TT" : "Gen5/6") : "Auto",
  13753. "Watchdog Force Stop ",
  13754. pdata->core_pdata->watchdog_force_stop ?
  13755. "True" : "False",
  13756. "Panel ID Support ",
  13757. pdata->core_pdata->panel_id_support);
  13758. return ret;
  13759. }
  13760. #define PT_ERR_STR_SIZE 64
  13761. /*******************************************************************************
  13762. * FUNCTION: pt_bist_bus_test
  13763. *
  13764. * SUMMARY: Tests the connectivity of the active bus pins:
  13765. * I2C - SDA and SCL
  13766. * SPI - MOSI, MISO, CLK
  13767. *
  13768. * Disable TTDL interrupts, send a PIP cmd and then manually read the
  13769. * bus. If any data is read we know the I2C/SPI pins are connected
  13770. *
  13771. * RETURN:
  13772. * 0 = Success
  13773. * !0 = Failure
  13774. *
  13775. * PARAMETERS:
  13776. * *dev - pointer to device structure
  13777. * *net_toggled - pointer to where to store if bus toggled
  13778. * *err_str - pointer to error string buffer
  13779. ******************************************************************************/
  13780. static int pt_bist_bus_test(struct device *dev, u8 *net_toggled, u8 *err_str)
  13781. {
  13782. struct pt_core_data *cd = dev_get_drvdata(dev);
  13783. u8 ver_cmd[8] = {0x01, 0x01, 0x06, 0x00, 0x0E, 0x07, 0xF0, 0xB1};
  13784. u8 *read_buf = NULL;
  13785. int bytes_read = 0;
  13786. int rc = 0;
  13787. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  13788. if (read_buf == NULL) {
  13789. rc = -ENOMEM;
  13790. goto exit;
  13791. }
  13792. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  13793. pt_debug(dev, DL_INFO, "%s: TTDL Core Suspend\n", __func__);
  13794. disable_irq(cd->irq);
  13795. mutex_lock(&cd->system_lock);
  13796. cd->irq_disabled = true;
  13797. mutex_unlock(&cd->system_lock);
  13798. /*
  13799. * Sleep >4ms to allow any pending TTDL IRQ to finish. Without this
  13800. * the disable_irq_nosync() could cause the IRQ to get stuck asserted
  13801. */
  13802. usleep_range(5000, 6000);
  13803. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer Size[%d] VERSION\n",
  13804. __func__, (int)sizeof(ver_cmd));
  13805. pt_pr_buf(cd->dev, DL_DEBUG, ver_cmd, (int)sizeof(ver_cmd),
  13806. ">>> User CMD");
  13807. rc = pt_adap_write_read_specific(cd, sizeof(ver_cmd), ver_cmd, NULL);
  13808. if (rc) {
  13809. pt_debug(dev, DL_ERROR,
  13810. "%s: BUS Test - Failed to send VER cmd\n", __func__);
  13811. *net_toggled = 0;
  13812. strlcpy(err_str,
  13813. "- Write failed, bus open or shorted or DUT in reset", PT_ERR_STR_SIZE);
  13814. goto exit_enable_irq;
  13815. }
  13816. usleep_range(4000, 5000);
  13817. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, read_buf);
  13818. pt_debug(dev, DL_INFO, "%s: BUS Test - %d bytes forced read\n",
  13819. __func__, bytes_read);
  13820. if (bytes_read == 0) {
  13821. *net_toggled = 0;
  13822. pt_debug(dev, DL_INFO, "%s: BUS Read Failed, 0 bytes read\n",
  13823. __func__);
  13824. strlcpy(err_str,
  13825. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  13826. rc = -EIO;
  13827. goto exit_enable_irq;
  13828. } else {
  13829. if (cd->bus_ops->bustype == BUS_I2C)
  13830. *net_toggled = 1;
  13831. else {
  13832. if ((bytes_read > 3) &&
  13833. (read_buf[3] & PIP2_CMD_COMMAND_ID_MASK) ==
  13834. PIP2_CMD_ID_VERSION)
  13835. *net_toggled = 1;
  13836. else {
  13837. *net_toggled = 0;
  13838. pt_debug(dev, DL_INFO,
  13839. "%s: BUS Read Failed, %d bytes read\n",
  13840. __func__, bytes_read);
  13841. strlcpy(err_str,
  13842. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  13843. }
  13844. }
  13845. }
  13846. exit_enable_irq:
  13847. enable_irq(cd->irq);
  13848. usleep_range(5000, 6000);
  13849. mutex_lock(&cd->system_lock);
  13850. cd->irq_disabled = false;
  13851. mutex_unlock(&cd->system_lock);
  13852. pt_debug(dev, DL_INFO, "%s: TTDL Core Resumed\n", __func__);
  13853. exit:
  13854. kfree(read_buf);
  13855. return rc;
  13856. }
  13857. /*******************************************************************************
  13858. * FUNCTION: pt_bist_irq_test
  13859. *
  13860. * SUMMARY: Tests the connectivity of the IRQ net
  13861. *
  13862. * This test will ensure there is a good connection between the host
  13863. * and the DUT on the irq pin. First determine if the IRQ is stuck
  13864. * asserted and if so keep reading messages off of the bus until
  13865. * it de-asserts. Possible outcomes:
  13866. * - IRQ was already de-asserted: Send a PIP command and if an
  13867. * interrupt is generated the test passes.
  13868. * - IRQ was asserted: Reading off the bus de-assertes the IRQ,
  13869. * test passes.
  13870. * - IRQ stays asserted: After reading the bus multiple times
  13871. * the IRQ stays asserted. Likely open or shorted to GND
  13872. *
  13873. * RETURN:
  13874. * 0 = Success
  13875. * !0 = Failure
  13876. *
  13877. * PARAMETERS:
  13878. * *dev - pointer to device structure
  13879. * *bus_toggled - pointer to where to store if bus toggled
  13880. * *irq_toggled - pointer to where to store if IRQ toggled
  13881. * *xres_toggled - pointer to where to store if XRES toggled
  13882. * *err_str - pointer to error string buffer
  13883. ******************************************************************************/
  13884. static int pt_bist_irq_test(struct device *dev,
  13885. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  13886. {
  13887. struct pt_core_data *cd = dev_get_drvdata(dev);
  13888. u8 *read_buf = NULL;
  13889. u8 mode = PT_MODE_UNKNOWN;
  13890. u16 actual_read_len;
  13891. int bytes_read = 0;
  13892. int count = 0;
  13893. int rc = 0;
  13894. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  13895. if (read_buf == NULL) {
  13896. rc = -ENOMEM;
  13897. goto exit;
  13898. }
  13899. /* Clear IRQ triggered count, and re-evaluate at the end of test */
  13900. cd->irq_count = 0;
  13901. /*
  13902. * Check if IRQ is stuck asserted, if so try a non forced flush of
  13903. * the bus based on the 2 byte initial length read. Try up to 5x.
  13904. */
  13905. while (pt_check_irq_asserted(cd) && count < 5) {
  13906. count++;
  13907. bytes_read += pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  13908. }
  13909. if (count > 1 && count < 5 && bytes_read > 0) {
  13910. /*
  13911. * IRQ was stuck but data was successfully read from the
  13912. * bus releasing the IRQ line.
  13913. */
  13914. pt_debug(dev, DL_INFO, "%s: count=%d bytes_read=%d\n",
  13915. __func__, count, bytes_read);
  13916. *bus_toggled = 1;
  13917. *irq_toggled = 1;
  13918. goto exit;
  13919. }
  13920. if (count == 5 && bytes_read == 0) {
  13921. /*
  13922. * Looped 5x and read nothing off the bus yet the IRQ is still
  13923. * asserted. Possible conditions:
  13924. * - IRQ open circuit
  13925. * - IRQ shorted to GND
  13926. * - I2C/SPI bus is disconnected
  13927. * - FW holding the pin low
  13928. * Try entering the BL to see if communication works there.
  13929. */
  13930. mode = PT_MODE_IGNORE;
  13931. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  13932. if (rc) {
  13933. pt_debug(dev, DL_ERROR,
  13934. "%s Failed to enter BL\n", __func__);
  13935. strlcpy(err_str,
  13936. "- likely shorted to GND or FW holding it.", PT_ERR_STR_SIZE);
  13937. *irq_toggled = 0;
  13938. goto exit;
  13939. }
  13940. /*
  13941. * If original mode was operational and we successfully
  13942. * entered the BL, then the XRES net must have toggled
  13943. */
  13944. if (mode == PT_MODE_OPERATIONAL)
  13945. *xres_toggled = 1;
  13946. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  13947. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  13948. &actual_read_len);
  13949. if (rc) {
  13950. /*
  13951. * Could not communicate to DUT in BL mode. Save the
  13952. * error string, slim chance but the XRES test below may
  13953. * show the IRQ is actually working.
  13954. */
  13955. strlcpy(err_str, "- likely shorted to GND.", PT_ERR_STR_SIZE);
  13956. pt_debug(dev, DL_ERROR,
  13957. "%s: %s, count=%d bytes_read=%d\n",
  13958. __func__, err_str, count, bytes_read);
  13959. *irq_toggled = 0;
  13960. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  13961. goto exit;
  13962. } else {
  13963. *bus_toggled = 1;
  13964. *irq_toggled = 1;
  13965. goto exit;
  13966. }
  13967. }
  13968. if (pt_check_irq_asserted(cd)) {
  13969. strlcpy(err_str, "- likely shorted to GND", PT_ERR_STR_SIZE);
  13970. rc = -EIO;
  13971. *irq_toggled = 0;
  13972. goto exit;
  13973. }
  13974. /* Try sending a PIP command to see if we get a response */
  13975. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  13976. PIP2_CMD_ID_VERSION, NULL, 0, read_buf, &actual_read_len);
  13977. if (rc) {
  13978. /*
  13979. * Potential IRQ issue, no communication in App mode, attempt
  13980. * the same command in the BL
  13981. */
  13982. mode = PT_MODE_IGNORE;
  13983. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  13984. if (rc) {
  13985. pt_debug(dev, DL_ERROR,
  13986. "%s Failed to enter BL\n", __func__);
  13987. *irq_toggled = 0;
  13988. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  13989. goto exit;
  13990. }
  13991. /*
  13992. * If original mode was operational and we successfully
  13993. * entered the BL, this will be useful info for the tp_xres
  13994. * test below.
  13995. */
  13996. if (mode == PT_MODE_OPERATIONAL)
  13997. *xres_toggled = 1;
  13998. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  13999. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14000. &actual_read_len);
  14001. if (rc) {
  14002. /*
  14003. * Could not communicate in FW mode or BL mode. Save the
  14004. * error string, slim chance but the XRES test below may
  14005. * show the IRQ is actually working.
  14006. */
  14007. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14008. pt_debug(dev, DL_ERROR,
  14009. "%s: request_active_pip_prot failed\n",
  14010. __func__);
  14011. *irq_toggled = 0;
  14012. goto exit;
  14013. }
  14014. }
  14015. if (cd->irq_count > 0) {
  14016. pt_debug(dev, DL_INFO, "%s: irq_count=%d\n", __func__,
  14017. cd->irq_count);
  14018. *bus_toggled = 1;
  14019. *irq_toggled = 1;
  14020. goto exit;
  14021. }
  14022. exit:
  14023. kfree(read_buf);
  14024. return rc;
  14025. }
  14026. /*******************************************************************************
  14027. * FUNCTION: pt_bist_xres_test
  14028. *
  14029. * SUMMARY: Tests the connectivity of the TP_XRES net
  14030. *
  14031. * This test will ensure there is a good connection between the host
  14032. * and the DUT on the tp_xres pin. The pin will be toggled to
  14033. * generate a TP reset which will cause the DUT to output a reset
  14034. * sentinel. If the reset sentinel is seen the test passes. If it is
  14035. * not seen the test will attempt to send a soft reset to simply gain
  14036. * some additional information on the failure:
  14037. * - soft reset fails to send: XRES and IRQ likely open
  14038. * - soft reset passes: XRES likely open or stuck de-asserted
  14039. * - soft reset fails: XRES likely stuck asserted
  14040. *
  14041. * RETURN:
  14042. * 0 = Success
  14043. * !0 = Failure
  14044. *
  14045. * PARAMETERS:
  14046. * *dev - pointer to device structure
  14047. * *bus_toggled - pointer to where to store if bus toggled
  14048. * *irq_toggled - pointer to where to store if IRQ toggled
  14049. * *xres_toggled - pointer to where to store if XRES toggled
  14050. * *err_str - pointer to error string buffer
  14051. ******************************************************************************/
  14052. static int pt_bist_xres_test(struct device *dev,
  14053. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14054. {
  14055. struct pt_core_data *cd = dev_get_drvdata(dev);
  14056. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14057. u8 *read_buf = NULL;
  14058. u8 mode = PT_MODE_UNKNOWN;
  14059. int rc = 0;
  14060. int t = 0;
  14061. int timeout = 300;
  14062. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14063. if (read_buf == NULL) {
  14064. rc = -ENOMEM;
  14065. goto exit;
  14066. }
  14067. /* Clear the startup bit mask, reset and enum will re-populate it */
  14068. cd->startup_status = STARTUP_STATUS_START;
  14069. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  14070. if ((!pdata->core_pdata->rst_gpio) || (!pdata->core_pdata->xres)) {
  14071. strlcpy(err_str, "- Net not configured or available", PT_ERR_STR_SIZE);
  14072. rc = -ENODEV;
  14073. goto exit;
  14074. }
  14075. /* Ensure we have nothing pending on active bus */
  14076. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  14077. /* Perform a hard XRES toggle and wait for reset sentinel */
  14078. mutex_lock(&cd->system_lock);
  14079. cd->hid_reset_cmd_state = 1;
  14080. mutex_unlock(&cd->system_lock);
  14081. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14082. rc = pt_hw_hard_reset(cd);
  14083. /* Set timeout to 1s for the flashless case where a BL could be done */
  14084. if (cd->flashless_dut)
  14085. timeout = 1000;
  14086. /*
  14087. * To avoid the case that next PIP command can be confused by BL/FW
  14088. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  14089. * followed by "wakeup event" function can lower the failure rate.
  14090. */
  14091. t = wait_event_timeout(cd->wait_q,
  14092. ((cd->startup_status != STARTUP_STATUS_START)
  14093. && (cd->hid_reset_cmd_state == 0)),
  14094. msecs_to_jiffies(timeout));
  14095. if (IS_TMO(t)) {
  14096. pt_debug(cd->dev, DL_ERROR,
  14097. "%s: TMO waiting for sentinel\n", __func__);
  14098. *xres_toggled = 0;
  14099. strlcpy(err_str, "- likely open. (No Reset Sentinel)", PT_ERR_STR_SIZE);
  14100. /*
  14101. * Possibly bad FW, Try entering BL and wait for reset sentinel.
  14102. * To enter the BL we need to generate an XRES so first try to
  14103. * launch the applicaiton
  14104. */
  14105. if (cd->mode == PT_MODE_BOOTLOADER)
  14106. pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  14107. pt_debug(dev, DL_INFO, "%s: Enter BL with a hard reset\n",
  14108. __func__);
  14109. mode = PT_MODE_IGNORE;
  14110. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14111. if (rc) {
  14112. pt_debug(dev, DL_ERROR, "%s Failed to enter BL\n",
  14113. __func__);
  14114. *xres_toggled = 0;
  14115. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14116. goto exit;
  14117. } else {
  14118. /* Wait for the BL sentinel */
  14119. t = wait_event_timeout(cd->wait_q,
  14120. (cd->startup_status != STARTUP_STATUS_START),
  14121. msecs_to_jiffies(500));
  14122. if (IS_TMO(t)) {
  14123. pt_debug(cd->dev, DL_ERROR,
  14124. "%s: TMO waiting for BL sentinel\n",
  14125. __func__);
  14126. *xres_toggled = 0;
  14127. strlcpy(err_str,
  14128. "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14129. rc = -ETIME;
  14130. goto exit;
  14131. }
  14132. }
  14133. }
  14134. mutex_lock(&cd->system_lock);
  14135. cd->hid_reset_cmd_state = 0;
  14136. mutex_unlock(&cd->system_lock);
  14137. /* Look for BL or FW reset sentinels */
  14138. if (!rc && ((cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL) ||
  14139. (cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL))) {
  14140. pt_debug(dev, DL_INFO, "%s: hard XRES pass\n", __func__);
  14141. /* If a sentinel was seen, all nets are working */
  14142. *xres_toggled = 1;
  14143. *irq_toggled = 1;
  14144. /*
  14145. * For SPI test, bus read result can be confused as FW sentinel
  14146. * if MISO(slave) is connected to MISO(host).
  14147. */
  14148. if (cd->bus_ops->bustype == BUS_I2C)
  14149. *bus_toggled = 1;
  14150. } else {
  14151. /*
  14152. * Hard reset failed, however some additional information
  14153. * could be determined. Try a soft reset to see if DUT resets
  14154. * with the possible outcomes:
  14155. * - if it resets the line is not stuck asserted
  14156. * - if it does not reset the line could be stuck asserted
  14157. */
  14158. *xres_toggled = 0;
  14159. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  14160. msleep(30);
  14161. pt_debug(dev, DL_INFO, "%s: TP_XRES BIST soft reset rc=%d",
  14162. __func__, rc);
  14163. if (rc) {
  14164. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14165. pt_debug(dev, DL_ERROR,
  14166. "%s: Hard reset failed, soft reset failed %s\n",
  14167. __func__, err_str);
  14168. goto exit;
  14169. }
  14170. if (cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL ||
  14171. cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) {
  14172. strlcpy(err_str,
  14173. "- likely open or stuck high, soft reset OK", PT_ERR_STR_SIZE);
  14174. pt_debug(dev, DL_ERROR,
  14175. "%s: Hard reset failed, soft reset passed-%s\n",
  14176. __func__, err_str);
  14177. } else if (cd->startup_status == 0) {
  14178. strlcpy(err_str, "- likely stuck high.", PT_ERR_STR_SIZE);
  14179. pt_debug(dev, DL_ERROR,
  14180. "%s: Hard reset failed, soft reset failed-%s\n",
  14181. __func__, err_str);
  14182. } else {
  14183. strlcpy(err_str, "- open or stuck.", PT_ERR_STR_SIZE);
  14184. pt_debug(dev, DL_ERROR,
  14185. "%s: Hard and Soft reset failed - %s\n",
  14186. __func__, err_str);
  14187. }
  14188. }
  14189. exit:
  14190. kfree(read_buf);
  14191. return rc;
  14192. }
  14193. /*******************************************************************************
  14194. * FUNCTION: pt_bist_slave_irq_test
  14195. *
  14196. * SUMMARY: Tests the connectivity of the Master/Slave IRQ net
  14197. *
  14198. * This test will ensure there is a good IRQ connection between the master
  14199. * DUT and the slave DUT(s). After power up the STATUS command is sent
  14200. * and the 'Slave Detect' bits are verified to ensure the master DUT
  14201. * saw each slave trigger the IRQ with it's reset sentinel.
  14202. *
  14203. * RETURN:
  14204. * 0 = Success
  14205. * !0 = Failure
  14206. *
  14207. * PARAMETERS:
  14208. * *dev - pointer to device structure
  14209. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14210. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14211. * *err_str - pointer to error string buffer
  14212. * *slave_detect - pointer to slave detect buffer
  14213. * *boot_err - pointer to boot_err buffer
  14214. ******************************************************************************/
  14215. static int pt_bist_slave_irq_test(struct device *dev,
  14216. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str,
  14217. u8 *slave_detect, u8 *boot_err)
  14218. {
  14219. struct pt_core_data *cd = dev_get_drvdata(dev);
  14220. u8 mode = PT_MODE_UNKNOWN;
  14221. u8 status;
  14222. u8 boot;
  14223. u8 read_buf[12];
  14224. u8 detected = 0;
  14225. u8 last_err = -1;
  14226. u16 actual_read_len;
  14227. int result = 0;
  14228. int rc = 0;
  14229. /*
  14230. * Ensure DUT is in the BL where the STATUS cmd will report the slave
  14231. * detect bits. If the DUT was in FW, entering the BL will cause an
  14232. * XRES signal which will inadvertently test the XRES net as well
  14233. */
  14234. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14235. if (rc) {
  14236. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14237. __func__, rc);
  14238. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14239. goto exit;
  14240. }
  14241. /* Use the STATUS command to retrieve the slave detect bit(s) */
  14242. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14243. PIP2_CMD_ID_STATUS, NULL, 0, read_buf,
  14244. &actual_read_len);
  14245. if (!rc) {
  14246. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14247. "PIP2 STATUS");
  14248. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14249. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  14250. /* Slave detect is only valid if status ok and in boot exec */
  14251. if (status == PIP2_RSP_ERR_NONE &&
  14252. boot == PIP2_STATUS_BOOT_EXEC) {
  14253. detected = read_buf[PIP2_RESP_BODY_OFFSET + 2] &
  14254. SLAVE_DETECT_MASK;
  14255. } else {
  14256. strlcpy(err_str, "- State could not be determined", PT_ERR_STR_SIZE);
  14257. rc = -EPERM;
  14258. }
  14259. } else {
  14260. pt_debug(cd->dev, DL_ERROR, "%s: STATUS cmd failure\n",
  14261. __func__);
  14262. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14263. goto exit;
  14264. }
  14265. /*
  14266. * Retrieve boot error regardless of the state of the slave detect
  14267. * bit because the IRQ could have been stuck high or low.
  14268. */
  14269. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14270. PIP2_CMD_ID_GET_LAST_ERRNO, NULL, 0,
  14271. read_buf, &actual_read_len);
  14272. if (!rc) {
  14273. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14274. "PIP2 GET_LAST_ERRNO");
  14275. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14276. last_err = read_buf[PIP2_RESP_BODY_OFFSET];
  14277. if (last_err) {
  14278. pt_debug(cd->dev, DL_ERROR,
  14279. "%s: Master Boot Last Err = 0x%02X\n",
  14280. __func__, last_err);
  14281. }
  14282. } else {
  14283. pt_debug(cd->dev, DL_ERROR,
  14284. "%s: GET_LAST_ERRNO cmd failure\n", __func__);
  14285. strlcpy(err_str, "- stuck, likely shorted to GND.", PT_ERR_STR_SIZE);
  14286. }
  14287. exit:
  14288. pt_debug(cd->dev, DL_INFO,
  14289. "%s: rc=%d detected=0x%02X boot_err=0x%02X\n",
  14290. __func__, rc, detected, last_err);
  14291. if (err_str && last_err) {
  14292. if (detected)
  14293. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14294. "- Likely stuck low. Boot Error:",
  14295. last_err);
  14296. else
  14297. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14298. "- Likely stuck high. Boot Error:",
  14299. last_err);
  14300. }
  14301. /* Ignore an invalid image error as BIST doesn't need valid FW */
  14302. if (last_err == PIP2_RSP_ERR_INVALID_IMAGE)
  14303. last_err = PIP2_RSP_ERR_NONE;
  14304. if (slave_irq_toggled)
  14305. *slave_irq_toggled = (detected && !last_err) ? true : false;
  14306. if (slave_bus_toggled) {
  14307. /* Leave as UNTEST if slave not detected */
  14308. if (detected)
  14309. *slave_bus_toggled = !last_err ? true : false;
  14310. }
  14311. if (slave_detect)
  14312. *slave_detect = detected;
  14313. if (boot_err)
  14314. *boot_err = last_err;
  14315. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X, %s=0x%02X, %s=0x%02X\n",
  14316. __func__,
  14317. "Detected", detected,
  14318. "slave_irq_toggled", *slave_irq_toggled,
  14319. "slave_bus_toggled", *slave_bus_toggled);
  14320. return rc;
  14321. }
  14322. /*******************************************************************************
  14323. * FUNCTION: pt_bist_slave_xres_test
  14324. *
  14325. * SUMMARY: Tests the connectivity of the Master/Slave TP_XRES net
  14326. *
  14327. * This test will ensure there is a good TP_XRES connection between the
  14328. * master DUT and the slave DUT(s). After toggling the XRES pin to the
  14329. * master, the STATUS command is sent and the 'Slave Detect' bits are
  14330. * verified to ensure the master DUT saw each slave trigger the IRQ with
  14331. * it's reset sentinel.
  14332. *
  14333. * RETURN:
  14334. * 0 = Success
  14335. * !0 = Failure
  14336. *
  14337. * PARAMETERS:
  14338. * *dev - pointer to device structure
  14339. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14340. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14341. * *slave_xres_toggled - pointer to where to store if slave XRES toggled
  14342. * *err_str - pointer to error string buffer
  14343. ******************************************************************************/
  14344. static int pt_bist_slave_xres_test(struct device *dev,
  14345. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *slave_xres_toggled,
  14346. u8 *err_str)
  14347. {
  14348. struct pt_core_data *cd = dev_get_drvdata(dev);
  14349. u8 slave_detect = 0;
  14350. u8 boot_err = 0;
  14351. int rc = 0;
  14352. /* Force a reset to force the 'slave detect' bits to be re-acquired */
  14353. mutex_lock(&cd->system_lock);
  14354. cd->hid_reset_cmd_state = 1;
  14355. mutex_unlock(&cd->system_lock);
  14356. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14357. pt_hw_hard_reset(cd);
  14358. msleep(100);
  14359. rc = pt_bist_slave_irq_test(dev, slave_irq_toggled,
  14360. slave_bus_toggled, err_str, &slave_detect, &boot_err);
  14361. pt_debug(dev, DL_INFO, "%s: IRQ test rc = %d\n", __func__, rc);
  14362. if (!rc && *slave_irq_toggled == false) {
  14363. /*
  14364. * If the slave IRQ did not toggle, either the slave_detect
  14365. * bit was not set or we had a boot error. If the slave
  14366. * detect was not set the slave did not reset causing a boot
  14367. * error.
  14368. */
  14369. if (!slave_detect)
  14370. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14371. else
  14372. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14373. "- likely open or an IRQ issue. Boot Error:",
  14374. boot_err);
  14375. }
  14376. if (slave_xres_toggled) {
  14377. if (!rc)
  14378. *slave_xres_toggled = *slave_irq_toggled ? true : false;
  14379. else
  14380. *slave_xres_toggled = false;
  14381. }
  14382. return rc;
  14383. }
  14384. /*******************************************************************************
  14385. * FUNCTION: pt_bist_slave_bus_test
  14386. *
  14387. * SUMMARY: Tests the connectivity of the Master/Slave SPI bus net
  14388. *
  14389. * This test will ensure a good SPI bus connection between the
  14390. * master DUT and the slave DUT(s). This bus connection is ensured by
  14391. * opening file 0 (SRAM loader). If there is a slave and the open fails
  14392. * then there is a master/slave communication issue. Opening file 0 on
  14393. * the master will open it on the slave as well if the slave was detected.
  14394. *
  14395. * RETURN:
  14396. * 0 = Success
  14397. * !0 = Failure
  14398. *
  14399. * PARAMETERS:
  14400. * *dev - pointer to device structure
  14401. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14402. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14403. * *err_str - pointer to error string buffer
  14404. ******************************************************************************/
  14405. static int pt_bist_slave_bus_test(struct device *dev,
  14406. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str)
  14407. {
  14408. struct pt_core_data *cd = dev_get_drvdata(dev);
  14409. u8 mode = PT_MODE_UNKNOWN;
  14410. u8 bus_toggled = false;
  14411. u8 file_handle;
  14412. int result = 0;
  14413. int rc = 0;
  14414. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14415. if (rc) {
  14416. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14417. __func__, rc);
  14418. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14419. goto exit;
  14420. }
  14421. pt_debug(dev, DL_INFO, "%s Attempt open file 0\n", __func__);
  14422. file_handle = _pt_pip2_file_open(dev, PIP2_RAM_FILE);
  14423. if (file_handle != PIP2_RAM_FILE) {
  14424. rc = -ENOENT;
  14425. bus_toggled = false;
  14426. pt_debug(dev, DL_ERROR,
  14427. "%s Failed to open bin file\n", __func__);
  14428. strlcpy(err_str, "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14429. goto exit;
  14430. } else {
  14431. bus_toggled = true;
  14432. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  14433. pt_debug(dev, DL_ERROR,
  14434. "%s: File Close failed, file_handle=%d\n",
  14435. __func__, file_handle);
  14436. }
  14437. }
  14438. exit:
  14439. /* If the master was able to send/recv a PIP msg, the IRQ must be ok */
  14440. if (slave_irq_toggled)
  14441. *slave_irq_toggled = bus_toggled;
  14442. if (slave_bus_toggled)
  14443. *slave_bus_toggled = bus_toggled;
  14444. return rc;
  14445. }
  14446. /*******************************************************************************
  14447. * FUNCTION: pt_ttdl_bist_show
  14448. *
  14449. * SUMMARY: Show method for the ttdl_bist sysfs node. This built in self test
  14450. * will test that I2C/SPI, IRQ and TP_XRES pins are operational.
  14451. *
  14452. * NOTE: This function will reset the DUT and the startup_status bit
  14453. * mask. A pt_enum will be queued after completion.
  14454. *
  14455. * NOTE: The order of the net tests is done to optimize the time it takes
  14456. * to run. The first test is capable of verifying all nets, each subsequent
  14457. * test is only run if the previous was not able to see all nets toggle.
  14458. *
  14459. * RETURN:
  14460. * 0 = success
  14461. * !0 = failure
  14462. *
  14463. * PARAMETERS:
  14464. * *dev - pointer to device structure
  14465. * *attr - pointer to device attributes
  14466. * *buf - pointer to output buffer
  14467. ******************************************************************************/
  14468. static ssize_t pt_ttdl_bist_show(struct device *dev,
  14469. struct device_attribute *attr, char *buf)
  14470. {
  14471. struct pt_core_data *cd = dev_get_drvdata(dev);
  14472. ssize_t ret;
  14473. char *bus_err_str = NULL;
  14474. char *irq_err_str = NULL;
  14475. char *xres_err_str = NULL;
  14476. char *slave_bus_err_str = NULL;
  14477. char *slave_irq_err_str = NULL;
  14478. char *slave_xres_err_str = NULL;
  14479. u8 tests;
  14480. int rc = 0;
  14481. int num_tests = 0;
  14482. int status = 1; /* 0 = Pass, !0 = fail */
  14483. u8 bus_toggled = 0x0F; /* default to untested */
  14484. u8 i2c_toggled = 0x0F; /* default to untested */
  14485. u8 spi_toggled = 0x0F; /* default to untested */
  14486. u8 irq_toggled = 0x0F; /* default to untested */
  14487. u8 xres_toggled = 0x0F; /* default to untested */
  14488. u8 slave_bus_toggled = 0x0F; /* default to untested */
  14489. u8 slave_irq_toggled = 0x0F; /* default to untested */
  14490. u8 slave_xres_toggled = 0x0F; /* default to untested */
  14491. bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14492. irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14493. xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14494. if (!bus_err_str || !irq_err_str || !xres_err_str)
  14495. goto print_results;
  14496. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14497. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14498. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  14499. if (cd->multi_chip) {
  14500. slave_bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14501. slave_irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14502. slave_xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14503. if (!slave_bus_err_str ||
  14504. !slave_irq_err_str ||
  14505. !slave_xres_err_str)
  14506. goto print_results;
  14507. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  14508. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  14509. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  14510. }
  14511. /* Turn off the TTDL WD during the test */
  14512. pt_stop_wd_timer(cd);
  14513. /* Shorten default PIP cmd timeout while running BIST */
  14514. cd->pip_cmd_timeout = 200;
  14515. /* Count the number of tests to run */
  14516. tests = cd->ttdl_bist_select;
  14517. while (tests) {
  14518. num_tests += tests & 1;
  14519. tests >>= 1;
  14520. }
  14521. pt_debug(dev, DL_ERROR, "%s: BIST select = 0x%02X, run %d tests\n",
  14522. __func__, cd->ttdl_bist_select, num_tests);
  14523. /* Suppress auto BL to avoid loader thread sending PIP during xres */
  14524. if (cd->flashless_dut) {
  14525. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - SUPPRESS\n",
  14526. __func__);
  14527. mutex_lock(&cd->system_lock);
  14528. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  14529. mutex_unlock(&cd->system_lock);
  14530. }
  14531. /* --------------- TP_XRES BIST TEST --------------- */
  14532. if ((cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST) != 0) {
  14533. pt_debug(dev, DL_INFO,
  14534. "%s: ----- Start TP_XRES BIST -----", __func__);
  14535. rc = pt_bist_xres_test(dev, &bus_toggled, &irq_toggled,
  14536. &xres_toggled, xres_err_str);
  14537. /* Done if the rest of all nets toggled */
  14538. if (bus_toggled == 1 && irq_toggled == 1 && xres_toggled == 1)
  14539. goto host_nets_complete;
  14540. }
  14541. /* Flush bus in case a PIP response is waiting from previous test */
  14542. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14543. /* --------------- IRQ BIST TEST --------------- */
  14544. if ((cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST) != 0) {
  14545. pt_debug(dev, DL_INFO,
  14546. "%s: ----- Start IRQ BIST -----", __func__);
  14547. bus_toggled = 0xFF;
  14548. irq_toggled = 0xFF;
  14549. rc = pt_bist_irq_test(dev, &bus_toggled, &irq_toggled,
  14550. &xres_toggled, irq_err_str);
  14551. /* If this net failed clear results from previous net */
  14552. if (irq_toggled != 1) {
  14553. xres_toggled = 0x0F;
  14554. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14555. }
  14556. if (bus_toggled == 1 && irq_toggled == 1)
  14557. goto host_nets_complete;
  14558. }
  14559. /* Flush bus in case a PIP response is waiting from previous test */
  14560. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14561. /* --------------- BUS BIST TEST --------------- */
  14562. if ((cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST) != 0) {
  14563. pt_debug(dev, DL_INFO,
  14564. "%s: ----- Start BUS BIST -----", __func__);
  14565. bus_toggled = 0xFF;
  14566. rc = pt_bist_bus_test(dev, &bus_toggled, bus_err_str);
  14567. /* If this net failed clear results from previous net */
  14568. if (bus_toggled == 0) {
  14569. irq_toggled = 0x0F;
  14570. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14571. }
  14572. }
  14573. host_nets_complete:
  14574. /* --------------- SLAVE XRES BIST TEST --------------- */
  14575. if (cd->multi_chip &&
  14576. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST) != 0) {
  14577. pt_debug(dev, DL_INFO,
  14578. "%s: ----- Start Slave XRES BIST -----", __func__);
  14579. slave_xres_toggled = 0xFF;
  14580. rc = pt_bist_slave_xres_test(dev, &slave_irq_toggled,
  14581. &slave_bus_toggled, &slave_xres_toggled,
  14582. slave_xres_err_str);
  14583. if ((slave_bus_toggled == 1 && slave_irq_toggled == 1 &&
  14584. slave_xres_toggled == 1) || slave_xres_toggled == 0)
  14585. goto print_results;
  14586. }
  14587. /* --------------- SLAVE IRQ BIST TEST --------------- */
  14588. if (cd->multi_chip &&
  14589. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST) != 0) {
  14590. pt_debug(dev, DL_INFO,
  14591. "%s: ----- Start Slave IRQ BIST -----", __func__);
  14592. slave_irq_toggled = 0xFF;
  14593. rc = pt_bist_slave_irq_test(dev, &slave_irq_toggled,
  14594. &slave_bus_toggled, slave_irq_err_str, NULL, NULL);
  14595. pt_debug(dev, DL_INFO, "%s: slave_irq_toggled = 0x%02X\n",
  14596. __func__, slave_irq_toggled);
  14597. if (slave_irq_toggled == 1) {
  14598. slave_bus_toggled = 1;
  14599. goto print_results;
  14600. }
  14601. }
  14602. /* --------------- SLAVE BUS BIST TEST --------------- */
  14603. if (cd->multi_chip &&
  14604. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST) != 0) {
  14605. pt_debug(dev, DL_INFO,
  14606. "%s: ----- Start Slave BUS BIST -----", __func__);
  14607. slave_bus_toggled = 0xFF;
  14608. rc = pt_bist_slave_bus_test(dev, &slave_irq_toggled,
  14609. &slave_bus_toggled, slave_bus_err_str);
  14610. }
  14611. print_results:
  14612. /* Restore PIP command timeout */
  14613. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  14614. /*
  14615. * We're done! - Perform a hard XRES toggle, allowing BL
  14616. * to load FW if there is any in Flash
  14617. */
  14618. mutex_lock(&cd->system_lock);
  14619. cd->hid_reset_cmd_state = 0;
  14620. mutex_unlock(&cd->system_lock);
  14621. pt_debug(dev, DL_INFO,
  14622. "%s: TTDL BIST Complete - Final reset\n", __func__);
  14623. if (cd->flashless_dut) {
  14624. /*
  14625. * For Flashless solution, FW update is triggered after BL is
  14626. * seen that several miliseconds delay is needed.
  14627. */
  14628. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - ALLOW\n",
  14629. __func__);
  14630. mutex_lock(&cd->system_lock);
  14631. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  14632. mutex_unlock(&cd->system_lock);
  14633. /* Reset DUT and wait 100ms to see if loader started */
  14634. pt_hw_hard_reset(cd);
  14635. msleep(100);
  14636. if (cd->fw_updating) {
  14637. pt_debug(dev, DL_INFO,
  14638. "%s: ----- BIST Wait FW Loading ----",
  14639. __func__);
  14640. rc = _pt_request_wait_for_enum_state(
  14641. dev, 4000, STARTUP_STATUS_COMPLETE);
  14642. }
  14643. } else {
  14644. if (cd->mode == PT_MODE_BOOTLOADER) {
  14645. rc = pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  14646. if (rc) {
  14647. pt_debug(dev, DL_ERROR,
  14648. "%s Failed to launch app\n", __func__);
  14649. rc = pt_hw_hard_reset(cd);
  14650. }
  14651. }
  14652. /*
  14653. * If FW exists the BL may have just started or will start soon,
  14654. * so the FW sentinel may be on it's way but with no FW it will
  14655. * not arrive, wait for it before deciding if we need to queue
  14656. * an enum.
  14657. */
  14658. rc = _pt_request_wait_for_enum_state(
  14659. dev, 400, STARTUP_STATUS_FW_RESET_SENTINEL);
  14660. if ((cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) &&
  14661. (cd->startup_status & STARTUP_STATUS_COMPLETE) == 0) {
  14662. pt_debug(dev, DL_INFO, "%s: ----- BIST Enum ----",
  14663. __func__);
  14664. pt_queue_enum(cd);
  14665. rc = _pt_request_wait_for_enum_state(
  14666. dev, 2000, STARTUP_STATUS_COMPLETE);
  14667. }
  14668. }
  14669. msleep(20);
  14670. /* --------------- PRINT OUT BIST RESULTS ---------------*/
  14671. pt_debug(dev, DL_INFO, "%s: ----- BIST Print Results ----", __func__);
  14672. pt_start_wd_timer(cd);
  14673. /* Canned print if any memory allocation issues */
  14674. if (!bus_err_str || !irq_err_str || !xres_err_str) {
  14675. ret = scnprintf(buf, strlen(buf),
  14676. "Status: %d\n"
  14677. "I2C (SDA,SCL): [UNTEST]\n"
  14678. "SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  14679. "IRQ connection: [UNTEST]\n"
  14680. "TP_XRES connection: [UNTEST]\n", -ENOMEM);
  14681. if (cd->multi_chip) {
  14682. ret += scnprintf(buf + ret, strlen(buf),
  14683. "I/P SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  14684. "I/P IRQ connection: [UNTEST]\n"
  14685. "I/P TP_XRES connection: [UNTEST]\n");
  14686. }
  14687. } else {
  14688. status = 0;
  14689. if (bus_toggled == 1)
  14690. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  14691. if (irq_toggled == 1)
  14692. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14693. if (xres_toggled == 1)
  14694. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14695. if (cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST)
  14696. status += bus_toggled;
  14697. if (cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST)
  14698. status += irq_toggled;
  14699. if (cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST)
  14700. status += xres_toggled;
  14701. pt_debug(dev, DL_ERROR, "%s: status = %d (%d,%d,%d)\n",
  14702. __func__, status, bus_toggled, irq_toggled,
  14703. xres_toggled);
  14704. if (cd->multi_chip) {
  14705. if (slave_irq_toggled == 1)
  14706. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  14707. if (slave_xres_toggled == 1)
  14708. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  14709. if (slave_bus_toggled == 1)
  14710. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  14711. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST)
  14712. status += slave_bus_toggled;
  14713. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST)
  14714. status += slave_irq_toggled;
  14715. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST)
  14716. status += slave_xres_toggled;
  14717. pt_debug(dev, DL_ERROR,
  14718. "%s: status = %d (%d,%d,%d,%d,%d,%d)\n",
  14719. __func__, status, bus_toggled, irq_toggled,
  14720. xres_toggled, slave_bus_toggled,
  14721. slave_irq_toggled, slave_xres_toggled);
  14722. }
  14723. if (cd->bus_ops->bustype == BUS_I2C)
  14724. i2c_toggled = bus_toggled;
  14725. else
  14726. spi_toggled = bus_toggled;
  14727. ret = scnprintf(buf, strlen(buf),
  14728. "Status: %d\n"
  14729. "I2C (SDA,SCL): %s %s\n"
  14730. "SPI (MISO,MOSI,CS,CLK): %s %s\n"
  14731. "IRQ connection: %s %s\n"
  14732. "TP_XRES connection: %s %s\n",
  14733. status == num_tests ? 0 : 1,
  14734. i2c_toggled == 0x0F ? "[UNTEST]" :
  14735. i2c_toggled == 1 ? "[ OK ]" : "[FAILED]",
  14736. i2c_toggled == 0x0F ? "" : bus_err_str,
  14737. spi_toggled == 0x0F ? "[UNTEST]" :
  14738. spi_toggled == 1 ? "[ OK ]" : "[FAILED]",
  14739. spi_toggled == 0x0F ? "" : bus_err_str,
  14740. irq_toggled == 0x0F ? "[UNTEST]" :
  14741. irq_toggled == 1 ? "[ OK ]" : "[FAILED]",
  14742. irq_err_str,
  14743. xres_toggled == 0x0F ? "[UNTEST]" :
  14744. xres_toggled == 1 ? "[ OK ]" : "[FAILED]",
  14745. xres_err_str);
  14746. if (cd->multi_chip) {
  14747. ret += scnprintf(buf + ret, strlen(buf),
  14748. "I/P SPI (MISO,MOSI,CS,CLK): %s %s\n"
  14749. "I/P IRQ connection: %s %s\n"
  14750. "I/P TP_XRES connection: %s %s\n",
  14751. slave_bus_toggled == 0x0F ? "[UNTEST]" :
  14752. slave_bus_toggled == 1 ? "[ OK ]" :
  14753. "[FAILED]", slave_bus_err_str,
  14754. slave_irq_toggled == 0x0F ? "[UNTEST]" :
  14755. slave_irq_toggled == 1 ? "[ OK ]" :
  14756. "[FAILED]", slave_irq_err_str,
  14757. slave_xres_toggled == 0x0F ? "[UNTEST]" :
  14758. slave_xres_toggled == 1 ? "[ OK ]" :
  14759. "[FAILED]", slave_xres_err_str);
  14760. }
  14761. }
  14762. /* Put TTDL back into a known state, issue a ttdl enum if needed */
  14763. pt_debug(dev, DL_INFO, "%s: Startup_status = 0x%04X\n",
  14764. __func__, cd->startup_status);
  14765. kfree(bus_err_str);
  14766. kfree(irq_err_str);
  14767. kfree(xres_err_str);
  14768. return ret;
  14769. }
  14770. /*******************************************************************************
  14771. * FUNCTION: pt_ttdl_bist_store
  14772. *
  14773. * SUMMARY: Store method for the ttdl_bist sysfs node.
  14774. *
  14775. * RETURN: Size of passed in buffer if successful
  14776. *
  14777. * PARAMETERS:
  14778. * *dev - pointer to device structure
  14779. * *attr - pointer to device attributes
  14780. * *buf - pointer to command buffer
  14781. * size - size of buf
  14782. ******************************************************************************/
  14783. static ssize_t pt_ttdl_bist_store(struct device *dev,
  14784. struct device_attribute *attr, const char *buf, size_t size)
  14785. {
  14786. struct pt_core_data *cd = dev_get_drvdata(dev);
  14787. u32 input_data[2] = {0};
  14788. int length;
  14789. int rc = 0;
  14790. /* Maximum input of one value */
  14791. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14792. ARRAY_SIZE(input_data));
  14793. if (length != 1) {
  14794. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14795. __func__);
  14796. rc = -EINVAL;
  14797. goto exit;
  14798. } else {
  14799. mutex_lock(&cd->system_lock);
  14800. cd->ttdl_bist_select = input_data[0];
  14801. mutex_unlock(&cd->system_lock);
  14802. }
  14803. exit:
  14804. if (rc)
  14805. return rc;
  14806. return size;
  14807. }
  14808. static DEVICE_ATTR(ttdl_bist, 0644, pt_ttdl_bist_show,
  14809. pt_ttdl_bist_store);
  14810. /*******************************************************************************
  14811. * FUNCTION: pt_flush_bus_store
  14812. *
  14813. * SUMMARY: Store method for the flush_bus sysfs node.
  14814. *
  14815. * RETURN: Size of passed in buffer if successful
  14816. *
  14817. * PARAMETERS:
  14818. * *dev - pointer to device structure
  14819. * *attr - pointer to device attributes
  14820. * *buf - pointer to command buffer
  14821. * size - size of buf
  14822. ******************************************************************************/
  14823. static ssize_t pt_flush_bus_store(struct device *dev,
  14824. struct device_attribute *attr, const char *buf, size_t size)
  14825. {
  14826. struct pt_core_data *cd = dev_get_drvdata(dev);
  14827. u32 input_data[2] = {0};
  14828. int length;
  14829. int rc = 0;
  14830. /* Maximum input of one value */
  14831. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14832. ARRAY_SIZE(input_data));
  14833. if (length != 1) {
  14834. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14835. __func__);
  14836. rc = -EINVAL;
  14837. goto exit;
  14838. }
  14839. mutex_lock(&cd->system_lock);
  14840. if (input_data[0] == 0)
  14841. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  14842. else if (input_data[0] == 1)
  14843. cd->flush_bus_type = PT_FLUSH_BUS_FULL_256_READ;
  14844. else
  14845. rc = -EINVAL;
  14846. mutex_unlock(&cd->system_lock);
  14847. exit:
  14848. if (rc)
  14849. return rc;
  14850. return size;
  14851. }
  14852. /*******************************************************************************
  14853. * FUNCTION: pt_flush_bus_show
  14854. *
  14855. * SUMMARY: Show method for the flush_bus sysfs node that flushes the active bus
  14856. * based on either the size of the first two bytes or a blind 256 bytes.
  14857. *
  14858. * RETURN:
  14859. * 0 = success
  14860. * !0 = failure
  14861. *
  14862. * PARAMETERS:
  14863. * *dev - pointer to device structure
  14864. * *attr - pointer to device attributes
  14865. * *buf - pointer to output buffer
  14866. ******************************************************************************/
  14867. static ssize_t pt_flush_bus_show(struct device *dev,
  14868. struct device_attribute *attr, char *buf)
  14869. {
  14870. ssize_t ret = 0;
  14871. ssize_t bytes = 0;
  14872. struct pt_core_data *cd = dev_get_drvdata(dev);
  14873. mutex_lock(&cd->system_lock);
  14874. bytes = pt_flush_bus(cd, cd->flush_bus_type, NULL);
  14875. ret = scnprintf(buf, strlen(buf),
  14876. "Status: 0\n"
  14877. "%s: %zd\n",
  14878. "Bytes flushed", bytes);
  14879. mutex_unlock(&cd->system_lock);
  14880. return ret;
  14881. }
  14882. static DEVICE_ATTR(flush_bus, 0644, pt_flush_bus_show,
  14883. pt_flush_bus_store);
  14884. /*******************************************************************************
  14885. * FUNCTION: pt_pip2_ping_test_store
  14886. *
  14887. * SUMMARY: Store method for the pip2_ping_test sysfs node.
  14888. *
  14889. * NOTE: The max PIP2 packet size is 255 (payload for PING 247) however
  14890. * someone may want to test sending invalid packet lengths so any values
  14891. * up to 255 are allowed.
  14892. *
  14893. * RETURN: Size of passed in buffer if successful
  14894. *
  14895. * PARAMETERS:
  14896. * *dev - pointer to device structure
  14897. * *attr - pointer to device attributes
  14898. * *buf - pointer to command buffer
  14899. * size - size of buf
  14900. ******************************************************************************/
  14901. static ssize_t pt_pip2_ping_test_store(struct device *dev,
  14902. struct device_attribute *attr, const char *buf, size_t size)
  14903. {
  14904. struct pt_core_data *cd = dev_get_drvdata(dev);
  14905. u32 input_data[2];
  14906. int length;
  14907. int rc = 0;
  14908. /* Maximum input of one value */
  14909. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14910. ARRAY_SIZE(input_data));
  14911. if (length != 1) {
  14912. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14913. __func__);
  14914. rc = -EINVAL;
  14915. goto exit;
  14916. }
  14917. mutex_lock(&cd->system_lock);
  14918. if (input_data[1] >= 0 && input_data[0] <= PT_MAX_PIP2_MSG_SIZE)
  14919. cd->ping_test_size = input_data[0];
  14920. else
  14921. rc = -EINVAL;
  14922. mutex_unlock(&cd->system_lock);
  14923. exit:
  14924. if (rc)
  14925. return rc;
  14926. return size;
  14927. }
  14928. /*******************************************************************************
  14929. * FUNCTION: pt_ping_test_show
  14930. *
  14931. * SUMMARY: Show method for the ping_test_show sysfs node that sends the PIP2
  14932. * PING command and ramps up the optional payload from 0 to
  14933. * ping_test_size.
  14934. * The max payload size is 247:
  14935. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  14936. *
  14937. * RETURN:
  14938. * 0 = success
  14939. * !0 = failure
  14940. *
  14941. * PARAMETERS:
  14942. * *dev - pointer to device structure
  14943. * *attr - pointer to device attributes
  14944. * *buf - pointer to output buffer
  14945. ******************************************************************************/
  14946. static ssize_t pt_pip2_ping_test_show(struct device *dev,
  14947. struct device_attribute *attr, char *buf)
  14948. {
  14949. struct pt_core_data *cd = dev_get_drvdata(dev);
  14950. ssize_t ret;
  14951. int last_packet_size;
  14952. int rc = 0;
  14953. rc = pt_pip2_ping_test(dev, cd->ping_test_size, &last_packet_size);
  14954. if (rc) {
  14955. ret = scnprintf(buf, strlen(buf), "Status: %d\n", rc);
  14956. return ret;
  14957. }
  14958. ret = scnprintf(buf, strlen(buf),
  14959. "Status: %d\n"
  14960. "PING payload test passed with packet sizes 0 - %d\n",
  14961. (last_packet_size == cd->ping_test_size ? 0 : 1),
  14962. last_packet_size);
  14963. return ret;
  14964. }
  14965. /*******************************************************************************
  14966. * FUNCTION: pt_t_refresh_store
  14967. *
  14968. * SUMMARY: Store method for the t-refresh sysfs node that will takes a passed
  14969. * in integer as the number of interrupts to count. A timer is started to
  14970. * calculate the total time it takes to see that number of interrupts.
  14971. *
  14972. * RETURN: Size of passed in buffer if successful
  14973. *
  14974. * PARAMETERS:
  14975. * *dev - pointer to device structure
  14976. * *attr - pointer to device attributes
  14977. * *buf - pointer to buffer that hold the command parameters
  14978. * size - size of buf
  14979. ******************************************************************************/
  14980. static ssize_t pt_t_refresh_store(struct device *dev,
  14981. struct device_attribute *attr, const char *buf, size_t size)
  14982. {
  14983. struct pt_core_data *cd = dev_get_drvdata(dev);
  14984. u32 input_data[2];
  14985. int length;
  14986. int rc = 0;
  14987. /* Maximum input of one value */
  14988. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14989. ARRAY_SIZE(input_data));
  14990. if (length != 1) {
  14991. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14992. __func__);
  14993. rc = -EINVAL;
  14994. goto exit;
  14995. }
  14996. mutex_lock(&cd->system_lock);
  14997. pt_debug(dev, DL_INFO, "%s: Input value: %d\n", __func__,
  14998. input_data[0]);
  14999. if (input_data[0] >= 0 && input_data[0] <= 1000) {
  15000. cd->t_refresh_total = input_data[0];
  15001. cd->t_refresh_count = 0;
  15002. cd->t_refresh_active = 1;
  15003. } else {
  15004. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  15005. rc = -EINVAL;
  15006. }
  15007. mutex_unlock(&cd->system_lock);
  15008. exit:
  15009. pt_debug(dev, DL_ERROR, "%s: rc = %d\n", __func__, rc);
  15010. if (rc)
  15011. return rc;
  15012. return size;
  15013. }
  15014. /*******************************************************************************
  15015. * FUNCTION: pt_t_refresh_show
  15016. *
  15017. * SUMMARY: Show method for the t-refresh sysfs node that will show the results
  15018. * of the T-Refresh timer counting the time it takes to see a user defined
  15019. * number of interrupts.
  15020. *
  15021. * RETURN:
  15022. * 0 = success
  15023. * !0 = failure
  15024. *
  15025. * PARAMETERS:
  15026. * *dev - pointer to device structure
  15027. * *attr - pointer to device attributes
  15028. * *buf - pointer to output buffer
  15029. ******************************************************************************/
  15030. static ssize_t pt_t_refresh_show(struct device *dev,
  15031. struct device_attribute *attr, char *buf)
  15032. {
  15033. ssize_t ret = 0;
  15034. struct pt_core_data *cd = dev_get_drvdata(dev);
  15035. u32 whole;
  15036. u16 fraction;
  15037. mutex_lock(&cd->system_lock);
  15038. /* Check if we have counted the number requested */
  15039. if (cd->t_refresh_count != cd->t_refresh_total) {
  15040. ret = scnprintf(buf, strlen(buf),
  15041. "Status: 0\n"
  15042. "%s: %d\n",
  15043. "Still counting... current IRQ count",
  15044. cd->t_refresh_count);
  15045. } else {
  15046. /* Ensure T-Refresh is de-activated */
  15047. cd->t_refresh_active = 0;
  15048. whole = cd->t_refresh_time / cd->t_refresh_count;
  15049. fraction = cd->t_refresh_time % cd->t_refresh_count;
  15050. fraction = fraction * 1000 / cd->t_refresh_count;
  15051. ret = scnprintf(buf, strlen(buf),
  15052. "Status: 0\n"
  15053. "%s: %d\n"
  15054. "%s: %d\n"
  15055. "%s: %d\n"
  15056. "%s: %d.%02d\n",
  15057. "Requested IRQ Count ", cd->t_refresh_total,
  15058. "IRQ Counted ", cd->t_refresh_count,
  15059. "Total Time Elapsed (ms) ", (int)cd->t_refresh_time,
  15060. "Average T-Refresh (ms) ", whole, fraction);
  15061. }
  15062. mutex_unlock(&cd->system_lock);
  15063. return ret;
  15064. }
  15065. /*******************************************************************************
  15066. * FUNCTION: pt_dut_status_show
  15067. *
  15068. * SUMMARY: Show method for DUT status sysfs node. Display DUT's scan state, and
  15069. * more items such as operation mode,easywake state are added in the future.
  15070. *
  15071. * RETURN: Char buffer with printed scan status information
  15072. *
  15073. * PARAMETERS:
  15074. * *dev - pointer to device structure
  15075. * *attr - pointer to device attributes
  15076. * *buf - pointer to output buffer
  15077. ******************************************************************************/
  15078. static ssize_t pt_dut_status_show(struct device *dev,
  15079. struct device_attribute *attr, char *buf)
  15080. {
  15081. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  15082. u8 mode = PT_MODE_UNKNOWN;
  15083. char *outputstring[7] = {"BOOT", "SCANNING", "DEEP_SLEEP",
  15084. "TEST", "DEEP_STANDBY", "UNDEFINED", "n/a"};
  15085. struct pt_core_data *cd = dev_get_drvdata(dev);
  15086. ssize_t ret;
  15087. u16 calculated_crc = 0;
  15088. u16 stored_crc = 0;
  15089. u8 status;
  15090. int rc = 0;
  15091. /* In STANDBY the DUT will not repond to any PIP cmd */
  15092. if (cd->fw_sys_mode_in_standby_state) {
  15093. mode = PT_MODE_OPERATIONAL;
  15094. sys_mode = FW_SYS_MODE_DEEP_STANDBY;
  15095. goto print_limited_results;
  15096. }
  15097. /* Retrieve mode and FW system mode which can only be 0-4 */
  15098. rc = pt_get_fw_sys_mode(cd, &sys_mode, &mode);
  15099. if (rc || mode == PT_MODE_UNKNOWN) {
  15100. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15101. "%s: %d\n"
  15102. "%s: n/a\n"
  15103. "%s: n/a\n"
  15104. "%s: n/a\n"
  15105. "%s: n/a\n",
  15106. "Status", rc,
  15107. "Active Exec ",
  15108. "FW System Mode ",
  15109. "Stored CRC ",
  15110. "Calculated CRC ");
  15111. return ret;
  15112. } else {
  15113. if (mode == PT_MODE_OPERATIONAL) {
  15114. if (sys_mode > FW_SYS_MODE_MAX)
  15115. sys_mode = FW_SYS_MODE_UNDEFINED;
  15116. if (sys_mode != FW_SYS_MODE_TEST)
  15117. goto print_limited_results;
  15118. rc = pt_pip_verify_config_block_crc_(cd,
  15119. PT_TCH_PARM_EBID, &status,
  15120. &calculated_crc, &stored_crc);
  15121. if (rc)
  15122. goto print_limited_results;
  15123. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15124. "%s: %d\n"
  15125. "%s: %s\n"
  15126. "%s: %s\n"
  15127. "%s: 0x%04X\n"
  15128. "%s: 0x%04X\n",
  15129. "Status", rc,
  15130. "Active Exec ", "FW",
  15131. "FW System Mode ", outputstring[sys_mode],
  15132. "Stored CRC ", stored_crc,
  15133. "Calculated CRC ", calculated_crc);
  15134. return ret;
  15135. } else {
  15136. /* When in BL or unknon mode Active Exec is "n/a" */
  15137. sys_mode = FW_SYS_MODE_UNDEFINED + 1;
  15138. }
  15139. }
  15140. print_limited_results:
  15141. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15142. "%s: %d\n"
  15143. "%s: %s\n"
  15144. "%s: %s\n"
  15145. "%s: n/a\n"
  15146. "%s: n/a\n",
  15147. "Status", rc,
  15148. "Active Exec ",
  15149. mode == PT_MODE_OPERATIONAL ? "FW" : "BL",
  15150. "FW System Mode ", outputstring[sys_mode],
  15151. "Stored CRC ",
  15152. "Calculated CRC ");
  15153. return ret;
  15154. }
  15155. #endif /* TTDL_DIAGNOSTICS */
  15156. /*******************************************************************************
  15157. * Structures of sysfs attributes for all DUT dependent sysfs node
  15158. ******************************************************************************/
  15159. static struct attribute *early_attrs[] = {
  15160. &dev_attr_hw_version.attr,
  15161. &dev_attr_drv_version.attr,
  15162. &dev_attr_drv_ver.attr,
  15163. &dev_attr_fw_version.attr,
  15164. &dev_attr_sysinfo.attr,
  15165. &dev_attr_pip2_cmd_rsp.attr,
  15166. &dev_attr_command.attr,
  15167. &dev_attr_drv_debug.attr,
  15168. &dev_attr_hw_reset.attr,
  15169. &dev_attr_response.attr,
  15170. &dev_attr_ttdl_restart.attr,
  15171. #ifdef TTDL_DIAGNOSTICS
  15172. &dev_attr_ttdl_status.attr,
  15173. &dev_attr_pip2_enter_bl.attr,
  15174. &dev_attr_pip2_exit_bl.attr,
  15175. &dev_attr_err_gpio.attr,
  15176. &dev_attr_flush_bus.attr,
  15177. &dev_attr_ttdl_bist.attr,
  15178. #endif /* TTDL_DIAGNOSTICS */
  15179. NULL,
  15180. };
  15181. static struct attribute_group early_attr_group = {
  15182. .attrs = early_attrs,
  15183. };
  15184. static struct device_attribute pip2_attributes[] = {
  15185. __ATTR(pip2_version, 0444, pt_pip2_version_show, NULL),
  15186. __ATTR(pip2_gpio_read, 0444, pt_pip2_gpio_read_show, NULL),
  15187. #ifdef TTDL_DIAGNOSTICS
  15188. __ATTR(pip2_bin_hdr, 0444, pt_pip2_bin_hdr_show, NULL),
  15189. __ATTR(pip2_ping_test, 0644, pt_pip2_ping_test_show,
  15190. pt_pip2_ping_test_store),
  15191. #endif
  15192. };
  15193. static struct device_attribute attributes[] = {
  15194. __ATTR(dut_debug, 0644,
  15195. pt_dut_debug_show, pt_drv_debug_store),
  15196. __ATTR(sleep_status, 0444, pt_sleep_status_show, NULL),
  15197. __ATTR(panel_id, 0444, pt_panel_id_show, NULL),
  15198. __ATTR(get_param, 0644,
  15199. pt_get_param_show, pt_get_param_store),
  15200. #ifdef EASYWAKE_TSG6
  15201. __ATTR(easy_wakeup_gesture, 0644, pt_easy_wakeup_gesture_show,
  15202. pt_easy_wakeup_gesture_store),
  15203. __ATTR(easy_wakeup_gesture_id, 0444,
  15204. pt_easy_wakeup_gesture_id_show, NULL),
  15205. __ATTR(easy_wakeup_gesture_data, 0444,
  15206. pt_easy_wakeup_gesture_data_show, NULL),
  15207. #endif
  15208. #ifdef TTDL_DIAGNOSTICS
  15209. __ATTR(platform_data, 0444, pt_platform_data_show, NULL),
  15210. __ATTR(drv_irq, 0644, pt_drv_irq_show, pt_drv_irq_store),
  15211. __ATTR(dut_status, 0444, pt_dut_status_show, NULL),
  15212. __ATTR(t_refresh, 0644, pt_t_refresh_show, pt_t_refresh_store),
  15213. #endif /* TTDL_DIAGNOSTICS */
  15214. };
  15215. /*******************************************************************************
  15216. * FUNCTION: add_sysfs_interfaces
  15217. *
  15218. * SUMMARY: Creates all DUT dependent sysfs nodes owned by the core
  15219. *
  15220. * RETURN:
  15221. * 0 = success
  15222. * !0 = failure
  15223. *
  15224. * PARAMETERS:
  15225. * *dev - pointer to device structure
  15226. ******************************************************************************/
  15227. static int add_sysfs_interfaces(struct device *dev)
  15228. {
  15229. int i;
  15230. int j = 0;
  15231. struct pt_core_data *cd = dev_get_drvdata(dev);
  15232. for (i = 0; i < ARRAY_SIZE(attributes); i++) {
  15233. if (device_create_file(dev, attributes + i))
  15234. goto undo;
  15235. }
  15236. pt_debug(dev, DL_INFO, "%s: Active DUT Generation: %d",
  15237. __func__, cd->active_dut_generation);
  15238. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  15239. for (j = 0; j < ARRAY_SIZE(pip2_attributes); j++) {
  15240. if (device_create_file(dev, pip2_attributes + j))
  15241. goto undo;
  15242. }
  15243. }
  15244. return 0;
  15245. undo:
  15246. for (i--; i >= 0; i--)
  15247. device_remove_file(dev, attributes + i);
  15248. for (j--; j >= 0; j--)
  15249. device_remove_file(dev, pip2_attributes + j);
  15250. pt_debug(dev, DL_ERROR, "%s: failed to create sysfs interface\n",
  15251. __func__);
  15252. return -ENODEV;
  15253. }
  15254. /*******************************************************************************
  15255. * FUNCTION: remove_sysfs_interfaces
  15256. *
  15257. * SUMMARY: Removes all DUT dependent sysfs nodes owned by the core
  15258. *
  15259. * RETURN: void
  15260. *
  15261. * PARAMETERS:
  15262. * *dev - pointer to device structure
  15263. ******************************************************************************/
  15264. static void remove_sysfs_interfaces(struct device *dev)
  15265. {
  15266. int i;
  15267. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  15268. device_remove_file(dev, attributes + i);
  15269. for (i = 0; i < ARRAY_SIZE(pip2_attributes); i++)
  15270. device_remove_file(dev, pip2_attributes + i);
  15271. }
  15272. /*******************************************************************************
  15273. * FUNCTION: remove_sysfs_and_modules
  15274. *
  15275. * SUMMARY: Removes all DUT dependent sysfs nodes and modules
  15276. *
  15277. * RETURN: void
  15278. *
  15279. * PARAMETERS:
  15280. * *dev - pointer to device structure
  15281. ******************************************************************************/
  15282. static void remove_sysfs_and_modules(struct device *dev)
  15283. {
  15284. struct pt_core_data *cd = dev_get_drvdata(dev);
  15285. /* Queued work should be removed before to release loader module */
  15286. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  15287. pt_release_modules(cd);
  15288. pt_btn_release(dev);
  15289. pt_mt_release(dev);
  15290. remove_sysfs_interfaces(dev);
  15291. }
  15292. static int pt_ts_pinctrl_init(struct pt_core_data *cd)
  15293. {
  15294. int retval;
  15295. /* Get pinctrl if target uses pinctrl */
  15296. cd->ts_pinctrl = devm_pinctrl_get(cd->dev);
  15297. if (IS_ERR_OR_NULL(cd->ts_pinctrl)) {
  15298. retval = PTR_ERR(cd->ts_pinctrl);
  15299. dev_dbg(cd->dev,
  15300. "Target does not use pinctrl %d\n", retval);
  15301. goto err_pinctrl_get;
  15302. }
  15303. cd->pinctrl_state_active
  15304. = pinctrl_lookup_state(cd->ts_pinctrl,
  15305. PINCTRL_STATE_ACTIVE);
  15306. if (IS_ERR_OR_NULL(cd->pinctrl_state_active)) {
  15307. retval = PTR_ERR(cd->pinctrl_state_active);
  15308. dev_err(cd->dev,
  15309. "Can not lookup %s pinstate %d\n",
  15310. PINCTRL_STATE_ACTIVE, retval);
  15311. goto err_pinctrl_lookup;
  15312. }
  15313. cd->pinctrl_state_suspend
  15314. = pinctrl_lookup_state(cd->ts_pinctrl,
  15315. PINCTRL_STATE_SUSPEND);
  15316. if (IS_ERR_OR_NULL(cd->pinctrl_state_suspend)) {
  15317. retval = PTR_ERR(cd->pinctrl_state_suspend);
  15318. dev_err(cd->dev,
  15319. "Can not lookup %s pinstate %d\n",
  15320. PINCTRL_STATE_SUSPEND, retval);
  15321. goto err_pinctrl_lookup;
  15322. }
  15323. cd->pinctrl_state_release
  15324. = pinctrl_lookup_state(cd->ts_pinctrl,
  15325. PINCTRL_STATE_RELEASE);
  15326. if (IS_ERR_OR_NULL(cd->pinctrl_state_release)) {
  15327. retval = PTR_ERR(cd->pinctrl_state_release);
  15328. dev_dbg(cd->dev,
  15329. "Can not lookup %s pinstate %d\n",
  15330. PINCTRL_STATE_RELEASE, retval);
  15331. }
  15332. return 0;
  15333. err_pinctrl_lookup:
  15334. devm_pinctrl_put(cd->ts_pinctrl);
  15335. err_pinctrl_get:
  15336. cd->ts_pinctrl = NULL;
  15337. return retval;
  15338. }
  15339. /*******************************************************************************
  15340. *******************************************************************************
  15341. * FUNCTION: pt_probe
  15342. *
  15343. * SUMMARY: Probe of the core module.
  15344. *
  15345. * NOTE: For the Parade Technologies development platform (PtSBC) the
  15346. * probe functionality is split into two functions; pt_probe() and
  15347. * pt_probe_complete(). the initial setup is done in this function which
  15348. * then creates a WORK task which runs after the probe timer expires. This
  15349. * ensures the I2C/SPI is up on the PtSBC in time for TTDL.
  15350. *
  15351. * RETURN:
  15352. * 0 = success
  15353. * !0 = failure
  15354. *
  15355. * PARAMETERS:
  15356. * *ops - pointer to the bus
  15357. * *dev - pointer to the device structure
  15358. * irq - IRQ
  15359. * xfer_buf_size - size of the buffer
  15360. ******************************************************************************/
  15361. int pt_probe(const struct pt_bus_ops *ops, struct device *dev,
  15362. u16 irq, size_t xfer_buf_size)
  15363. {
  15364. struct pt_core_data *cd;
  15365. struct pt_platform_data *pdata = dev_get_platdata(dev);
  15366. enum pt_atten_type type;
  15367. struct i2c_client *client = to_i2c_client(dev);
  15368. int rc = 0;
  15369. u8 pip_ver_major;
  15370. u8 pip_ver_minor;
  15371. u32 status = STARTUP_STATUS_START;
  15372. if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
  15373. pt_debug(dev, DL_ERROR, "%s: Missing platform data\n",
  15374. __func__);
  15375. rc = -ENODEV;
  15376. goto error_no_pdata;
  15377. }
  15378. if (pdata->core_pdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  15379. if (!pdata->core_pdata->power) {
  15380. pt_debug(dev, DL_ERROR,
  15381. "%s: Missing platform data function\n",
  15382. __func__);
  15383. rc = -ENODEV;
  15384. goto error_no_pdata;
  15385. }
  15386. }
  15387. /* get context and debug print buffers */
  15388. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  15389. if (!cd) {
  15390. rc = -ENOMEM;
  15391. goto error_no_pdata;
  15392. }
  15393. /* Initialize device info */
  15394. cd->dev = dev;
  15395. cd->pdata = pdata;
  15396. cd->cpdata = pdata->core_pdata;
  15397. cd->bus_ops = ops;
  15398. cd->debug_level = PT_INITIAL_DEBUG_LEVEL;
  15399. cd->show_timestamp = PT_INITIAL_SHOW_TIME_STAMP;
  15400. scnprintf(cd->core_id, 20, "%s%d", PT_CORE_NAME, core_number++);
  15401. cd->hw_detected = false;
  15402. cd->pip2_prot_active = false;
  15403. cd->pip2_send_user_cmd = false;
  15404. cd->bl_pip_ver_ready = false;
  15405. cd->app_pip_ver_ready = false;
  15406. cd->pip2_cmd_tag_seq = 0x08; /* PIP2 TAG=1 and 3 bit SEQ=0 */
  15407. cd->get_param_id = 0;
  15408. cd->watchdog_enabled = 0;
  15409. cd->startup_retry_count = 0;
  15410. cd->core_probe_complete = 0;
  15411. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  15412. cd->pip_cmd_timeout = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15413. cd->pip_cmd_timeout_default = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15414. cd->flashless_dut = 0;
  15415. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  15416. cd->bl_with_no_int = 0;
  15417. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  15418. cd->multi_chip = PT_FEATURE_DISABLE;
  15419. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  15420. if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP2_CAPABLE) {
  15421. cd->set_dut_generation = true;
  15422. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  15423. } else if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP1_ONLY) {
  15424. cd->set_dut_generation = true;
  15425. cd->active_dut_generation = DUT_PIP1_ONLY;
  15426. } else {
  15427. cd->set_dut_generation = false;
  15428. cd->active_dut_generation = DUT_UNKNOWN;
  15429. }
  15430. /* Initialize with platform data */
  15431. cd->watchdog_force_stop = cd->cpdata->watchdog_force_stop;
  15432. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  15433. cd->hid_cmd_state = 1;
  15434. cd->fw_updating = false;
  15435. cd->multi_chip = 0;
  15436. #ifdef TTDL_DIAGNOSTICS
  15437. cd->t_refresh_active = 0;
  15438. cd->t_refresh_count = 0;
  15439. cd->pip2_crc_error_count = 0;
  15440. cd->wd_xres_count = 0;
  15441. cd->bl_retry_packet_count = 0;
  15442. cd->file_erase_timeout_count = 0;
  15443. cd->show_tt_data = false;
  15444. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15445. cd->err_gpio = 0;
  15446. cd->err_gpio_type = PT_ERR_GPIO_NONE;
  15447. cd->ttdl_bist_select = 0x07;
  15448. cd->force_pip2_seq = 0;
  15449. #endif /* TTDL_DIAGNOSTICS */
  15450. memset(cd->pip2_us_file_path, 0, PT_MAX_PATH_SIZE);
  15451. memcpy(cd->pip2_us_file_path, PT_PIP2_BIN_FILE_PATH,
  15452. sizeof(PT_PIP2_BIN_FILE_PATH));
  15453. pt_init_hid_descriptor(&cd->hid_desc);
  15454. /* Read and store the descriptor lengths */
  15455. cd->hid_core.hid_report_desc_len =
  15456. le16_to_cpu(cd->hid_desc.report_desc_len);
  15457. cd->hid_core.hid_max_input_len =
  15458. le16_to_cpu(cd->hid_desc.max_input_len);
  15459. cd->hid_core.hid_max_output_len =
  15460. le16_to_cpu(cd->hid_desc.max_output_len);
  15461. /* Initialize mutexes and spinlocks */
  15462. mutex_init(&cd->module_list_lock);
  15463. mutex_init(&cd->system_lock);
  15464. mutex_init(&cd->sysfs_lock);
  15465. mutex_init(&cd->ttdl_restart_lock);
  15466. mutex_init(&cd->firmware_class_lock);
  15467. spin_lock_init(&cd->spinlock);
  15468. /* Initialize module list */
  15469. INIT_LIST_HEAD(&cd->module_list);
  15470. /* Initialize attention lists */
  15471. for (type = 0; type < PT_ATTEN_NUM_ATTEN; type++)
  15472. INIT_LIST_HEAD(&cd->atten_list[type]);
  15473. /* Initialize parameter list */
  15474. INIT_LIST_HEAD(&cd->param_list);
  15475. /* Initialize wait queue */
  15476. init_waitqueue_head(&cd->wait_q);
  15477. rc = pt_ts_pinctrl_init(cd);
  15478. if (!rc && cd->ts_pinctrl) {
  15479. /*
  15480. * Pinctrl handle is optional. If pinctrl handle is found
  15481. * let pins to be configured in active state. If not
  15482. * found continue further without error.
  15483. */
  15484. rc = pinctrl_select_state(cd->ts_pinctrl,
  15485. cd->pinctrl_state_active);
  15486. if (rc < 0)
  15487. dev_err(&client->dev, "failed to select pin to active state\n");
  15488. }
  15489. rc = pt_get_regulator(cd, true);
  15490. if (rc) {
  15491. dev_err(&client->dev, "Failed to get voltage regulators\n");
  15492. goto error_alloc_data;
  15493. }
  15494. rc = pt_enable_regulator(cd, true);
  15495. if (rc) {
  15496. dev_err(dev, "Failed to enable regulators: rc=%d\n", rc);
  15497. goto error_get_regulator;
  15498. }
  15499. /* Initialize works */
  15500. INIT_WORK(&cd->enum_work, pt_enum_work_function);
  15501. INIT_WORK(&cd->ttdl_restart_work, pt_restart_work_function);
  15502. INIT_WORK(&cd->watchdog_work, pt_watchdog_work);
  15503. /* Initialize HID specific data */
  15504. cd->hid_core.hid_vendor_id = (cd->cpdata->vendor_id) ?
  15505. cd->cpdata->vendor_id : HID_VENDOR_ID;
  15506. cd->hid_core.hid_product_id = (cd->cpdata->product_id) ?
  15507. cd->cpdata->product_id : HID_APP_PRODUCT_ID;
  15508. cd->hid_core.hid_desc_register =
  15509. cpu_to_le16(cd->cpdata->hid_desc_register);
  15510. /* Set platform easywake value */
  15511. cd->easy_wakeup_gesture = cd->cpdata->easy_wakeup_gesture;
  15512. /* Set platform panel_id value */
  15513. cd->panel_id_support = cd->cpdata->panel_id_support;
  15514. if (cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO)
  15515. /* Set Panel ID to default to 0 */
  15516. cd->pid_for_loader = PT_PANEL_ID_DEFAULT;
  15517. else
  15518. /* Set Panel ID to Not Enabled */
  15519. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  15520. /* Initialize hw_version default to FFFF.FFFF.FF */
  15521. snprintf(cd->hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  15522. dev_set_drvdata(dev, cd);
  15523. /* PtSBC builds will call this function in pt_probe_complete() */
  15524. pt_add_core(dev);
  15525. rc = sysfs_create_group(&dev->kobj, &early_attr_group);
  15526. if (rc) {
  15527. pt_debug(cd->dev, DL_ERROR, "%s:create early attrs failed\n",
  15528. __func__);
  15529. goto error_enable_regulator;
  15530. }
  15531. /*
  15532. * Save the pointer to a global value, which will be used
  15533. * in ttdl_restart function
  15534. */
  15535. cd->bus_ops = ops;
  15536. /*
  15537. * When the IRQ GPIO is not direclty accessible and no function is
  15538. * defined to get the IRQ status, the IRQ passed in must be assigned
  15539. * directly as the gpio_to_irq will not work. e.g. CHROMEOS
  15540. */
  15541. if (!cd->cpdata->irq_stat) {
  15542. cd->irq = irq;
  15543. pt_debug(cd->dev, DL_ERROR, "%s:No irq_stat, Set cd->irq = %d\n",
  15544. __func__, cd->irq);
  15545. }
  15546. /* Call platform init function before setting up the GPIO's */
  15547. if (cd->cpdata->init) {
  15548. pt_debug(cd->dev, DL_INFO, "%s: Init HW\n", __func__);
  15549. rc = cd->cpdata->init(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  15550. } else {
  15551. pt_debug(cd->dev, DL_ERROR, "%s: No HW INIT function\n",
  15552. __func__);
  15553. rc = 0;
  15554. }
  15555. if (rc < 0) {
  15556. pt_debug(cd->dev, DL_ERROR, "%s: HW Init fail r=%d\n",
  15557. __func__, rc);
  15558. }
  15559. /* Power on any needed regulator(s) */
  15560. if (cd->cpdata->setup_power) {
  15561. pt_debug(cd->dev, DL_INFO, "%s: Device power on!\n", __func__);
  15562. rc = cd->cpdata->setup_power(cd->cpdata,
  15563. PT_MT_POWER_ON, cd->dev);
  15564. } else {
  15565. pt_debug(cd->dev, DL_ERROR, "%s: No setup power function\n",
  15566. __func__);
  15567. rc = 0;
  15568. }
  15569. if (rc < 0)
  15570. pt_debug(cd->dev, DL_ERROR, "%s: Setup power on fail r=%d\n",
  15571. __func__, rc);
  15572. #ifdef TTDL_DIAGNOSTICS
  15573. cd->watchdog_irq_stuck_count = 0;
  15574. cd->bus_transmit_error_count = 0;
  15575. #endif /* TTDL_DIAGNOSTICS */
  15576. if (cd->cpdata->detect) {
  15577. pt_debug(cd->dev, DL_INFO, "%s: Detect HW\n", __func__);
  15578. rc = cd->cpdata->detect(cd->cpdata, cd->dev,
  15579. pt_platform_detect_read);
  15580. if (!rc) {
  15581. cd->hw_detected = true;
  15582. pt_debug(cd->dev, DL_INFO,
  15583. "%s: HW detected\n", __func__);
  15584. } else {
  15585. cd->hw_detected = false;
  15586. pt_debug(cd->dev, DL_INFO,
  15587. "%s: No HW detected\n", __func__);
  15588. rc = -ENODEV;
  15589. goto error_detect;
  15590. }
  15591. } else {
  15592. pt_debug(dev, DL_ERROR,
  15593. "%s: PARADE No HW detect function pointer\n",
  15594. __func__);
  15595. /*
  15596. * "hw_reset" is not needed in the "if" statement,
  15597. * because "hw_reset" is already included in "hw_detect"
  15598. * function.
  15599. */
  15600. rc = pt_hw_hard_reset(cd);
  15601. if (rc)
  15602. pt_debug(cd->dev, DL_ERROR,
  15603. "%s: FAILED to execute HARD reset\n",
  15604. __func__);
  15605. }
  15606. if (cd->cpdata->setup_irq) {
  15607. pt_debug(cd->dev, DL_INFO, "%s: setup IRQ\n", __func__);
  15608. rc = cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_REG, cd->dev);
  15609. if (rc) {
  15610. pt_debug(dev, DL_ERROR,
  15611. "%s: Error, couldn't setup IRQ\n", __func__);
  15612. goto error_setup_irq;
  15613. }
  15614. } else {
  15615. pt_debug(dev, DL_ERROR,
  15616. "%s: IRQ function pointer not setup\n",
  15617. __func__);
  15618. goto error_setup_irq;
  15619. }
  15620. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  15621. setup_timer(&cd->watchdog_timer, pt_watchdog_timer,
  15622. (unsigned long)cd);
  15623. #else
  15624. timer_setup(&cd->watchdog_timer, pt_watchdog_timer, 0);
  15625. #endif
  15626. pt_stop_wd_timer(cd);
  15627. #ifdef TTHE_TUNER_SUPPORT
  15628. mutex_init(&cd->tthe_lock);
  15629. cd->tthe_debugfs = debugfs_create_file(PT_TTHE_TUNER_FILE_NAME,
  15630. 0644, NULL, cd, &tthe_debugfs_fops);
  15631. #endif
  15632. rc = device_init_wakeup(dev, 1);
  15633. if (rc < 0)
  15634. pt_debug(dev, DL_ERROR, "%s: Error, device_init_wakeup rc:%d\n",
  15635. __func__, rc);
  15636. pm_runtime_get_noresume(dev);
  15637. pm_runtime_set_active(dev);
  15638. pm_runtime_enable(dev);
  15639. /* If IRQ asserted, read out all from buffer to release INT pin */
  15640. if (cd->cpdata->irq_stat) {
  15641. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  15642. } else {
  15643. /* Force a read in case the reset sentinel already arrived */
  15644. rc = pt_read_input(cd);
  15645. if (!rc)
  15646. pt_parse_input(cd);
  15647. }
  15648. /* Without sleep DUT is not ready and will NAK the first write */
  15649. msleep(150);
  15650. /* Attempt to set the DUT generation if not yet set */
  15651. if (cd->active_dut_generation == DUT_UNKNOWN) {
  15652. if (cd->bl_pip_ver_ready ||
  15653. (cd->app_pip_ver_ready &&
  15654. IS_PIP_VER_GE(&cd->sysinfo, 1, 12))) {
  15655. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  15656. pt_debug(dev, DL_INFO, "%s: dut generation is %d\n",
  15657. __func__, cd->active_dut_generation);
  15658. } else {
  15659. rc = _pt_detect_dut_generation(cd->dev,
  15660. &status, &cd->active_dut_generation,
  15661. &cd->mode);
  15662. if ((cd->active_dut_generation == DUT_UNKNOWN)
  15663. || rc) {
  15664. pt_debug(cd->dev, DL_ERROR,
  15665. " === DUT Gen unknown, Skip Enum ===\n");
  15666. goto skip_enum;
  15667. }
  15668. }
  15669. }
  15670. _pt_request_active_pip_protocol(cd->dev, PT_CORE_CMD_PROTECTED,
  15671. &pip_ver_major, &pip_ver_minor);
  15672. if (pip_ver_major == 2) {
  15673. cd->bl_pip_ver_ready = true;
  15674. pt_debug(dev, DL_ERROR,
  15675. " === PIP2.%d Detected, Attempt to launch APP ===\n",
  15676. pip_ver_minor);
  15677. cd->hw_detected = true;
  15678. } else if (pip_ver_major == 1) {
  15679. cd->app_pip_ver_ready = true;
  15680. pt_debug(dev, DL_ERROR,
  15681. " === PIP1.%d Detected ===\n", pip_ver_minor);
  15682. cd->hw_detected = true;
  15683. } else {
  15684. cd->sysinfo.ttdata.pip_ver_major = 0;
  15685. cd->sysinfo.ttdata.pip_ver_minor = 0;
  15686. cd->app_pip_ver_ready = false;
  15687. cd->hw_detected = false;
  15688. pt_debug(dev, DL_ERROR,
  15689. " === PIP Version Not Detected, Skip Enum ===\n");
  15690. /* For legacy DUTS proceed, enum will attempt to launch app */
  15691. if (cd->active_dut_generation != DUT_PIP1_ONLY)
  15692. goto skip_enum;
  15693. }
  15694. rc = pt_enum_with_dut(cd, false, &status);
  15695. pt_debug(dev, DL_ERROR, "%s: cd->startup_status=0x%04X status=0x%04X\n",
  15696. __func__, cd->startup_status, status);
  15697. if (rc == -ENODEV) {
  15698. pt_debug(cd->dev, DL_ERROR,
  15699. "%s: Enumeration Failed r=%d\n", __func__, rc);
  15700. /* For PtSBC don't error out, allow TTDL to stay up */
  15701. goto error_after_startup;
  15702. }
  15703. /* Suspend scanning until probe is complete to avoid asyc touches */
  15704. pt_pip_suspend_scanning_(cd);
  15705. if (cd->hw_detected) {
  15706. pt_debug(dev, DL_INFO, "%s: Add sysfs interfaces\n",
  15707. __func__);
  15708. rc = add_sysfs_interfaces(dev);
  15709. if (rc < 0) {
  15710. pt_debug(dev, DL_ERROR,
  15711. "%s: Error, fail sysfs init\n", __func__);
  15712. goto error_after_startup;
  15713. }
  15714. } else {
  15715. pt_debug(dev, DL_ERROR,
  15716. "%s: No HW detected, sysfs interfaces not added\n",
  15717. __func__);
  15718. }
  15719. skip_enum:
  15720. pm_runtime_put_sync(dev);
  15721. pt_debug(dev, DL_INFO, "%s: Probe: MT, BTN\n", __func__);
  15722. rc = pt_mt_probe(dev);
  15723. if (rc < 0) {
  15724. pt_debug(dev, DL_ERROR, "%s: Error, fail mt probe\n",
  15725. __func__);
  15726. goto error_after_sysfs_create;
  15727. }
  15728. rc = pt_btn_probe(dev);
  15729. if (rc < 0) {
  15730. pt_debug(dev, DL_ERROR, "%s: Error, fail btn probe\n",
  15731. __func__);
  15732. goto error_after_startup_mt;
  15733. }
  15734. pt_probe_modules(cd);
  15735. #ifdef CONFIG_HAS_EARLYSUSPEND
  15736. pt_setup_early_suspend(cd);
  15737. #elif defined(CONFIG_FB)
  15738. pt_setup_fb_notifier(cd);
  15739. #endif
  15740. #ifdef NEED_SUSPEND_NOTIFIER
  15741. cd->pm_notifier.notifier_call = pt_pm_notifier;
  15742. register_pm_notifier(&cd->pm_notifier);
  15743. #endif
  15744. pt_pip_resume_scanning_(cd);
  15745. mutex_lock(&cd->system_lock);
  15746. cd->startup_status |= status;
  15747. cd->core_probe_complete = 1;
  15748. mutex_unlock(&cd->system_lock);
  15749. pt_debug(dev, DL_ERROR, "%s: TTDL Core Probe Completed Successfully\n",
  15750. __func__);
  15751. return 0;
  15752. error_after_startup_mt:
  15753. pr_err("%s PARADE error_after_startup_mt\n", __func__);
  15754. pt_mt_release(dev);
  15755. error_after_sysfs_create:
  15756. pr_err("%s PARADE error_after_sysfs_create\n", __func__);
  15757. pm_runtime_disable(dev);
  15758. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  15759. device_wakeup_disable(dev);
  15760. #endif
  15761. device_init_wakeup(dev, 0);
  15762. cancel_work_sync(&cd->enum_work);
  15763. pt_stop_wd_timer(cd);
  15764. pt_free_si_ptrs(cd);
  15765. remove_sysfs_interfaces(dev);
  15766. error_after_startup:
  15767. pr_err("%s PARADE error_after_startup\n", __func__);
  15768. del_timer(&cd->watchdog_timer);
  15769. if (cd->cpdata->setup_irq)
  15770. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  15771. error_setup_irq:
  15772. error_detect:
  15773. if (cd->cpdata->init)
  15774. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  15775. if (cd->cpdata->setup_power)
  15776. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  15777. sysfs_remove_group(&dev->kobj, &early_attr_group);
  15778. error_enable_regulator:
  15779. pt_del_core(dev);
  15780. dev_set_drvdata(dev, NULL);
  15781. pt_enable_regulator(cd, false);
  15782. error_get_regulator:
  15783. pt_get_regulator(cd, false);
  15784. error_alloc_data:
  15785. kfree(cd);
  15786. error_no_pdata:
  15787. pr_err("%s failed.\n", __func__);
  15788. return rc;
  15789. }
  15790. EXPORT_SYMBOL_GPL(pt_probe);
  15791. /*******************************************************************************
  15792. * FUNCTION: pt_release
  15793. *
  15794. * SUMMARY: This function does the following cleanup:
  15795. * - Releases all probed modules
  15796. * - Stops the watchdog
  15797. * - Cancels all pending work tasks
  15798. * - Removes all created sysfs nodes
  15799. * - Removes all created debugfs nodes
  15800. * - Frees the IRQ
  15801. * - Deletes the core
  15802. * - Frees all pointers and HID reports
  15803. *
  15804. * RETURN:
  15805. * 0 = success
  15806. * !0 = failure
  15807. *
  15808. * PARAMETERS:
  15809. * *cd - pointer to the core data structure
  15810. ******************************************************************************/
  15811. int pt_release(struct pt_core_data *cd)
  15812. {
  15813. struct device *dev = cd->dev;
  15814. /*
  15815. * Suspend the device before freeing the startup_work and stopping
  15816. * the watchdog since sleep function restarts watchdog on failure
  15817. */
  15818. pm_runtime_suspend(dev);
  15819. pm_runtime_disable(dev);
  15820. /*
  15821. * Any 'work' that can trigger a new thread should be canceled first.
  15822. * The watchdog is also stopped again because another thread could have
  15823. * restarted it. The irq_work is cancelled last because it is used for
  15824. * all I2C/SPI communication.
  15825. */
  15826. pt_stop_wd_timer(cd);
  15827. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  15828. cancel_work_sync(&cd->ttdl_restart_work);
  15829. cancel_work_sync(&cd->enum_work);
  15830. pt_stop_wd_timer(cd);
  15831. pt_release_modules(cd);
  15832. pt_proximity_release(dev);
  15833. pt_btn_release(dev);
  15834. pt_mt_release(dev);
  15835. #ifdef CONFIG_HAS_EARLYSUSPEND
  15836. unregister_early_suspend(&cd->es);
  15837. #elif defined(CONFIG_FB)
  15838. fb_unregister_client(&cd->fb_notifier);
  15839. #endif
  15840. #ifdef NEED_SUSPEND_NOTIFIER
  15841. unregister_pm_notifier(&cd->pm_notifier);
  15842. #endif
  15843. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  15844. device_wakeup_disable(dev);
  15845. #endif
  15846. device_init_wakeup(dev, 0);
  15847. #ifdef TTHE_TUNER_SUPPORT
  15848. mutex_lock(&cd->tthe_lock);
  15849. cd->tthe_exit = 1;
  15850. wake_up(&cd->wait_q);
  15851. mutex_unlock(&cd->tthe_lock);
  15852. debugfs_remove(cd->tthe_debugfs);
  15853. #endif
  15854. sysfs_remove_group(&dev->kobj, &early_attr_group);
  15855. remove_sysfs_interfaces(dev);
  15856. disable_irq_nosync(cd->irq);
  15857. if (cd->cpdata->setup_irq)
  15858. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  15859. if (cd->cpdata->init)
  15860. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  15861. if (cd->cpdata->setup_power)
  15862. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  15863. dev_set_drvdata(dev, NULL);
  15864. pt_del_core(dev);
  15865. pt_enable_regulator(cd, false);
  15866. pt_get_regulator(cd, false);
  15867. pt_free_si_ptrs(cd);
  15868. kfree(cd);
  15869. return 0;
  15870. }
  15871. EXPORT_SYMBOL_GPL(pt_release);
  15872. MODULE_LICENSE("GPL");
  15873. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product Core Driver");
  15874. MODULE_AUTHOR("Parade Technologies <[email protected]>");