pt_core.c 500 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363
  1. /*
  2. * pt_core.c
  3. * Parade TrueTouch(TM) Standard Product Core Module.
  4. * For use with Parade touchscreen controllers.
  5. * Supported parts include:
  6. * TMA5XX
  7. * TMA448
  8. * TMA445A
  9. * TT21XXX
  10. * TT31XXX
  11. * TT4XXXX
  12. * TT7XXX
  13. * TC3XXX
  14. *
  15. * Copyright (C) 2015-2020 Parade Technologies
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * version 2, and only version 2, as published by the
  20. * Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * Contact Parade Technologies at www.paradetech.com <[email protected]>
  28. */
  29. #include <linux/i2c.h>
  30. #include <linux/gpio.h>
  31. #include <linux/kthread.h>
  32. #include "pt_regs.h"
  33. #define PINCTRL_STATE_ACTIVE "pmx_ts_active"
  34. #define PINCTRL_STATE_SUSPEND "pmx_ts_suspend"
  35. #define PINCTRL_STATE_RELEASE "pmx_ts_release"
  36. #define FT_VTG_MIN_UV 2800000
  37. #define FT_VTG_MAX_UV 2800000
  38. #define FT_I2C_VTG_MIN_UV 1800000
  39. #define FT_I2C_VTG_MAX_UV 1800000
  40. #define PT_CORE_STARTUP_RETRY_COUNT 3
  41. #define PT_STATUS_STR_LEN (50)
  42. MODULE_FIRMWARE(PT_FW_FILE_NAME);
  43. static const char *pt_driver_core_name = PT_CORE_NAME;
  44. static const char *pt_driver_core_version = PT_DRIVER_VERSION;
  45. static const char *pt_driver_core_date = PT_DRIVER_DATE;
  46. struct pt_hid_field {
  47. int report_count;
  48. int report_size;
  49. int size; /* report_count * report_size */
  50. int offset;
  51. int data_type;
  52. int logical_min;
  53. int logical_max;
  54. /* Usage Page (Hi 16 bit) + Usage (Lo 16 bit) */
  55. u32 usage_page;
  56. u32 collection_usage_pages[PT_HID_MAX_COLLECTIONS];
  57. struct pt_hid_report *report;
  58. bool record_field;
  59. };
  60. struct pt_hid_report {
  61. u8 id;
  62. u8 type;
  63. int size;
  64. struct pt_hid_field *fields[PT_HID_MAX_FIELDS];
  65. int num_fields;
  66. int record_field_index;
  67. int header_size;
  68. int record_size;
  69. u32 usage_page;
  70. };
  71. struct atten_node {
  72. struct list_head node;
  73. char *id;
  74. struct device *dev;
  75. int (*func)(struct device *dev);
  76. int mode;
  77. };
  78. struct param_node {
  79. struct list_head node;
  80. u8 id;
  81. u32 value;
  82. u8 size;
  83. };
  84. struct module_node {
  85. struct list_head node;
  86. struct pt_module *module;
  87. void *data;
  88. };
  89. struct pt_hid_cmd {
  90. u8 opcode;
  91. u8 report_type;
  92. union {
  93. u8 report_id;
  94. u8 power_state;
  95. };
  96. u8 has_data_register;
  97. size_t write_length;
  98. u8 *write_buf;
  99. u8 *read_buf;
  100. u8 wait_interrupt;
  101. u8 reset_cmd;
  102. u16 timeout_ms;
  103. };
  104. struct pt_hid_output {
  105. u8 cmd_type;
  106. u16 length;
  107. u8 command_code;
  108. size_t write_length;
  109. u8 *write_buf;
  110. u8 novalidate;
  111. u8 reset_expected;
  112. u16 timeout_ms;
  113. };
  114. #define SET_CMD_OPCODE(byte, opcode) SET_CMD_LOW(byte, opcode)
  115. #define SET_CMD_REPORT_TYPE(byte, type) SET_CMD_HIGH(byte, ((type) << 4))
  116. #define SET_CMD_REPORT_ID(byte, id) SET_CMD_LOW(byte, id)
  117. #define CREATE_PIP1_FW_CMD(command) \
  118. .cmd_type = PIP1_CMD_TYPE_FW, \
  119. .command_code = command
  120. #define CREATE_PIP1_BL_CMD(command) \
  121. .cmd_type = PIP1_CMD_TYPE_BL, \
  122. .command_code = command
  123. #define PT_MAX_PR_BUF_SIZE 2048
  124. /*******************************************************************************
  125. * FUNCTION: pt_pr_buf
  126. *
  127. * SUMMARY: Print out the contents of a buffer to kmsg based on the debug level
  128. *
  129. * RETURN: Void
  130. *
  131. * PARAMETERS:
  132. * *dev - pointer to Device structure
  133. * debug_level - requested debug level to print at
  134. * *buf - pointer to buffer to print
  135. * buf_len - size of buf
  136. * *data_name - Descriptive name of data prefixed to data
  137. ******************************************************************************/
  138. void pt_pr_buf(struct device *dev, u8 debug_level, u8 *buf,
  139. u16 buf_len, const char *data_name)
  140. {
  141. struct pt_core_data *cd = dev_get_drvdata(dev);
  142. int i;
  143. ssize_t pr_buf_index = 0;
  144. int max_size;
  145. /* only proceed if valid debug level and there is data to print */
  146. if (debug_level <= cd->debug_level && buf_len > 0) {
  147. char *pr_buf = kzalloc(PT_MAX_PR_BUF_SIZE, GFP_KERNEL);
  148. if (!pr_buf)
  149. return;
  150. /*
  151. * With a space each printed char takes 3 bytes, subtract
  152. * the length of the data_name prefix as well as 11 bytes
  153. * for the " [0..xxx]: " printed before the data.
  154. */
  155. max_size = (PT_MAX_PR_BUF_SIZE - sizeof(data_name) - 11) / 3;
  156. /* Ensure pr_buf_index stays within the 1018 size */
  157. pr_buf_index += scnprintf(pr_buf, PT_MAX_PR_BUF_SIZE, "%s [0..%d]: ",
  158. data_name);
  159. for (i = 0; i < buf_len && i < max_size; i++)
  160. pr_buf_index += scnprintf(pr_buf + pr_buf_index,
  161. PT_MAX_PR_BUF_SIZE, "%02X ", buf[i]);
  162. pt_debug(dev, debug_level, "%s\n", pr_buf);
  163. kfree(pr_buf);
  164. }
  165. }
  166. EXPORT_SYMBOL_GPL(pt_pr_buf);
  167. #ifdef TTHE_TUNER_SUPPORT
  168. /*******************************************************************************
  169. * FUNCTION: tthe_print
  170. *
  171. * SUMMARY: Format data name and time stamp as the header and format the
  172. * content of input buffer with hex base to "tthe_buf". And then wake up event
  173. * semaphore for tthe debugfs node.
  174. *
  175. * RETURN:
  176. * 0 = success
  177. * !0 = failure
  178. *
  179. * PARAMETERS:
  180. * *cd - pointer to core data
  181. * *buf - pointer to input buffer
  182. * buf_len - size of input buffer
  183. * *data_name - pointer to data name
  184. ******************************************************************************/
  185. static int tthe_print(struct pt_core_data *cd, u8 *buf, int buf_len,
  186. const u8 *data_name)
  187. {
  188. int name_len = strlen(data_name);
  189. int i, n;
  190. u8 *p;
  191. int remain;
  192. u8 data_name_with_time_stamp[100];
  193. /* Prepend timestamp, if requested, to data_name */
  194. if (cd->show_timestamp) {
  195. scnprintf(data_name_with_time_stamp,
  196. sizeof(data_name_with_time_stamp),
  197. "[%u] %s", pt_get_time_stamp(), data_name);
  198. data_name = data_name_with_time_stamp;
  199. name_len = strlen(data_name);
  200. }
  201. mutex_lock(&cd->tthe_lock);
  202. if (!cd->tthe_buf)
  203. goto exit;
  204. /* Add 1 due to the '\n' that is appended at the end */
  205. if (cd->tthe_buf_len + name_len + buf_len + 1 > cd->tthe_buf_size)
  206. goto exit;
  207. if (name_len + buf_len == 0)
  208. goto exit;
  209. remain = cd->tthe_buf_size - cd->tthe_buf_len;
  210. if (remain < name_len)
  211. name_len = remain;
  212. p = cd->tthe_buf + cd->tthe_buf_len;
  213. memcpy(p, data_name, name_len);
  214. cd->tthe_buf_len += name_len;
  215. p += name_len;
  216. remain -= name_len;
  217. *p = 0;
  218. for (i = 0; i < buf_len; i++) {
  219. n = scnprintf(p, remain, "%02X ", buf[i]);
  220. if (n <= 0)
  221. break;
  222. p += n;
  223. remain -= n;
  224. cd->tthe_buf_len += n;
  225. }
  226. n = scnprintf(p, remain, "\n");
  227. cd->tthe_buf_len += n;
  228. exit:
  229. wake_up(&cd->wait_q);
  230. mutex_unlock(&cd->tthe_lock);
  231. return 0;
  232. }
  233. /*******************************************************************************
  234. * FUNCTION: _pt_request_tthe_print
  235. *
  236. * SUMMARY: Function pointer included in core_cmds to allow other modules
  237. * to request to print data to the "tthe_buffer".
  238. *
  239. * RETURN:
  240. * 0 = success
  241. * !0 = failure
  242. *
  243. * PARAMETERS:
  244. * *dev - pointer to device structure
  245. ******************************************************************************/
  246. static int _pt_request_tthe_print(struct device *dev, u8 *buf,
  247. int buf_len, const u8 *data_name)
  248. {
  249. struct pt_core_data *cd = dev_get_drvdata(dev);
  250. return tthe_print(cd, buf, buf_len, data_name);
  251. }
  252. #endif
  253. /*******************************************************************************
  254. * FUNCTION: pt_platform_detect_read
  255. *
  256. * SUMMARY: To be passed to platform dectect function to perform a read
  257. * operation.
  258. *
  259. * RETURN:
  260. * 0 = success
  261. * !0 = failure
  262. *
  263. * PARAMETERS:
  264. * *dev - pointer to Device structure
  265. * *buf - pointer to buffer where the data read will be stored
  266. * size - size to be read
  267. ******************************************************************************/
  268. static int pt_platform_detect_read(struct device *dev, void *buf, int size)
  269. {
  270. struct pt_core_data *cd = dev_get_drvdata(dev);
  271. return pt_adap_read_default(cd, buf, size);
  272. }
  273. /*******************************************************************************
  274. * FUNCTION: pt_add_parameter
  275. *
  276. * SUMMARY: Adds a parameter that has been altered to the parameter linked list.
  277. * On every reset of the DUT this linked list is traversed and all
  278. * parameters in it are restored to the DUT.
  279. *
  280. * RETURN:
  281. * 0 = success
  282. * !0 = failure
  283. *
  284. * PARAMETERS:
  285. * *cd - pointer to core data
  286. * param_id - parameter ID to add
  287. * param_value - Value corresponding to the ID
  288. * param_size - Size of param_value
  289. ******************************************************************************/
  290. static int pt_add_parameter(struct pt_core_data *cd,
  291. u8 param_id, u32 param_value, u8 param_size)
  292. {
  293. struct param_node *param, *param_new;
  294. /* Check if parameter already exists in the list */
  295. spin_lock(&cd->spinlock);
  296. list_for_each_entry(param, &cd->param_list, node) {
  297. if (param->id == param_id) {
  298. /* Update parameter */
  299. param->value = param_value;
  300. pt_debug(cd->dev, DL_INFO,
  301. "%s: Update parameter id:%d value:%d size:%d\n",
  302. __func__, param_id, param_value, param_size);
  303. goto exit_unlock;
  304. }
  305. }
  306. spin_unlock(&cd->spinlock);
  307. param_new = kzalloc(sizeof(*param_new), GFP_KERNEL);
  308. if (!param_new)
  309. return -ENOMEM;
  310. param_new->id = param_id;
  311. param_new->value = param_value;
  312. param_new->size = param_size;
  313. pt_debug(cd->dev, DL_INFO,
  314. "%s: Add parameter id:%d value:%d size:%d\n",
  315. __func__, param_id, param_value, param_size);
  316. spin_lock(&cd->spinlock);
  317. list_add(&param_new->node, &cd->param_list);
  318. exit_unlock:
  319. spin_unlock(&cd->spinlock);
  320. return 0;
  321. }
  322. #ifdef TTDL_DIAGNOSTICS
  323. /*******************************************************************************
  324. * FUNCTION: pt_erase_parameter_list
  325. *
  326. * SUMMARY: Empty out the entire parameter linked list of all parameter/value
  327. * pairs. In some test cases this functionality is needed to ensure DUT
  328. * returns to a virgin state after a reset and no parameters are restored.
  329. *
  330. * RETURN:
  331. * 0 = success
  332. * !0 = failure
  333. *
  334. * PARAMETERS:
  335. * *cd - pointer to core data
  336. ******************************************************************************/
  337. static int pt_erase_parameter_list(struct pt_core_data *cd)
  338. {
  339. struct param_node *pos, *temp;
  340. spin_lock(&cd->spinlock);
  341. list_for_each_entry_safe(pos, temp, &cd->param_list, node) {
  342. pt_debug(cd->dev, DL_INFO,
  343. "%s: Parameter Restore List - remove 0x%02x\n",
  344. __func__, pos->id);
  345. list_del(&pos->node);
  346. kfree(pos);
  347. }
  348. spin_unlock(&cd->spinlock);
  349. return 0;
  350. }
  351. /*******************************************************************************
  352. * FUNCTION: pt_count_parameter_list
  353. *
  354. * SUMMARY: Count the items in the RAM parameter restor list
  355. *
  356. * RETURN:
  357. * 0 = success
  358. * !0 = failure
  359. *
  360. * PARAMETERS:
  361. * *cd - pointer to core data
  362. ******************************************************************************/
  363. static int pt_count_parameter_list(struct pt_core_data *cd)
  364. {
  365. struct param_node *pos, *temp;
  366. int entries = 0;
  367. spin_lock(&cd->spinlock);
  368. list_for_each_entry_safe(pos, temp, &cd->param_list, node)
  369. entries++;
  370. spin_unlock(&cd->spinlock);
  371. return entries;
  372. }
  373. #endif /* TTDL_DIAGNOSTICS */
  374. /*******************************************************************************
  375. * FUNCTION: request_exclusive
  376. *
  377. * SUMMARY: Request exclusive access to the DUT
  378. *
  379. * RETURN:
  380. * 0 = success
  381. * !0 = failure
  382. *
  383. * PARAMETERS:
  384. * *cd - pointer to core data
  385. * *ownptr - pointer to device
  386. * timeout_ms - Timeout value
  387. ******************************************************************************/
  388. int request_exclusive(struct pt_core_data *cd, void *ownptr,
  389. int timeout_ms)
  390. {
  391. int t = msecs_to_jiffies(timeout_ms);
  392. bool with_timeout = (timeout_ms != 0);
  393. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Request EXCLUSIVE t=%d\n",
  394. __func__, timeout_ms);
  395. mutex_lock(&cd->system_lock);
  396. if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
  397. cd->exclusive_dev = ownptr;
  398. goto exit;
  399. }
  400. cd->exclusive_waits++;
  401. wait:
  402. mutex_unlock(&cd->system_lock);
  403. if (with_timeout) {
  404. t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
  405. if (IS_TMO(t)) {
  406. pt_debug(cd->dev, DL_ERROR,
  407. "%s: tmo waiting exclusive access\n", __func__);
  408. return -ETIME;
  409. }
  410. } else {
  411. wait_event(cd->wait_q, !cd->exclusive_dev);
  412. }
  413. mutex_lock(&cd->system_lock);
  414. if (cd->exclusive_dev)
  415. goto wait;
  416. cd->exclusive_dev = ownptr;
  417. cd->exclusive_waits--;
  418. exit:
  419. mutex_unlock(&cd->system_lock);
  420. pt_debug(cd->dev, DL_DEBUG, "%s: request exclusive ok=%p\n",
  421. __func__, ownptr);
  422. return 0;
  423. }
  424. /*******************************************************************************
  425. * FUNCTION: release_exclusive_
  426. *
  427. * SUMMARY: Release exclusive access to the DUT
  428. *
  429. * RETURN:
  430. * 0 = success
  431. *
  432. * PARAMETERS:
  433. * *cd - pointer to core data
  434. * *ownptr - pointer to device
  435. ******************************************************************************/
  436. static int release_exclusive_(struct pt_core_data *cd, void *ownptr)
  437. {
  438. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Release EXCLUSIVE\n",
  439. __func__);
  440. if (cd->exclusive_dev != ownptr)
  441. return -EINVAL;
  442. pt_debug(cd->dev, DL_DEBUG, "%s: exclusive_dev %p freed\n",
  443. __func__, cd->exclusive_dev);
  444. cd->exclusive_dev = NULL;
  445. wake_up(&cd->wait_q);
  446. return 0;
  447. }
  448. /*******************************************************************************
  449. * FUNCTION: release_exclusive
  450. *
  451. * SUMMARY: Protected wrapper to release_exclusive_()
  452. *
  453. * RETURN:
  454. * 0 = success
  455. * !0 = failure
  456. *
  457. * PARAMETERS:
  458. * *cd - pointer to core data
  459. * *ownptr - pointer to device
  460. ******************************************************************************/
  461. int release_exclusive(struct pt_core_data *cd, void *ownptr)
  462. {
  463. int rc;
  464. mutex_lock(&cd->system_lock);
  465. rc = release_exclusive_(cd, ownptr);
  466. mutex_unlock(&cd->system_lock);
  467. return rc;
  468. }
  469. /*******************************************************************************
  470. * FUNCTION: pt_hid_exec_cmd_
  471. *
  472. * SUMMARY: Send the HID command to the DUT
  473. *
  474. * RETURN:
  475. * 0 = success
  476. * !0 = failure
  477. *
  478. * PARAMETERS:
  479. * *cd - pointer to core data
  480. * *hid_cmd - pointer to the HID command to send
  481. ******************************************************************************/
  482. static int pt_hid_exec_cmd_(struct pt_core_data *cd,
  483. struct pt_hid_cmd *hid_cmd)
  484. {
  485. int rc = 0;
  486. u8 *cmd;
  487. u16 cmd_length;
  488. u8 cmd_offset = 0;
  489. cmd_length = 2 /* command register */
  490. + 2 /* command */
  491. + (hid_cmd->report_id >= 0XF ? 1 : 0) /* Report ID */
  492. + (hid_cmd->has_data_register ? 2 : 0) /* Data register */
  493. + hid_cmd->write_length; /* Data length */
  494. cmd = kzalloc(cmd_length, GFP_KERNEL);
  495. if (!cmd)
  496. return -ENOMEM;
  497. /* Set Command register */
  498. memcpy(&cmd[cmd_offset], &cd->hid_desc.command_register,
  499. sizeof(cd->hid_desc.command_register));
  500. cmd_offset += sizeof(cd->hid_desc.command_register);
  501. /* Set Command */
  502. SET_CMD_REPORT_TYPE(cmd[cmd_offset], hid_cmd->report_type);
  503. if (hid_cmd->report_id >= 0XF)
  504. SET_CMD_REPORT_ID(cmd[cmd_offset], 0xF);
  505. else
  506. SET_CMD_REPORT_ID(cmd[cmd_offset], hid_cmd->report_id);
  507. cmd_offset++;
  508. SET_CMD_OPCODE(cmd[cmd_offset], hid_cmd->opcode);
  509. cmd_offset++;
  510. if (hid_cmd->report_id >= 0XF) {
  511. cmd[cmd_offset] = hid_cmd->report_id;
  512. cmd_offset++;
  513. }
  514. /* Set Data register */
  515. if (hid_cmd->has_data_register) {
  516. memcpy(&cmd[cmd_offset], &cd->hid_desc.data_register,
  517. sizeof(cd->hid_desc.data_register));
  518. cmd_offset += sizeof(cd->hid_desc.data_register);
  519. }
  520. /* Set Data */
  521. if (hid_cmd->write_length && hid_cmd->write_buf) {
  522. memcpy(&cmd[cmd_offset], hid_cmd->write_buf,
  523. hid_cmd->write_length);
  524. cmd_offset += hid_cmd->write_length;
  525. }
  526. pt_debug(cd->dev, DL_INFO,
  527. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  528. __func__, cmd_length, hid_cmd->report_id);
  529. pt_pr_buf(cd->dev, DL_DEBUG, cmd, cmd_length, ">>> CMD");
  530. rc = pt_adap_write_read_specific(cd, cmd_length, cmd,
  531. hid_cmd->read_buf);
  532. if (rc)
  533. pt_debug(cd->dev, DL_ERROR,
  534. "%s: Fail pt_adap_transfer\n", __func__);
  535. kfree(cmd);
  536. return rc;
  537. }
  538. #ifdef TTDL_DIAGNOSTICS
  539. /*******************************************************************************
  540. * FUNCTION: pt_toggle_err_gpio
  541. *
  542. * SUMMARY: Toggles the pre-defined error GPIO
  543. *
  544. * RETURN: n/a
  545. *
  546. * PARAMETERS:
  547. * *cd - pointer to core data
  548. * type - type of err that occurred
  549. ******************************************************************************/
  550. void pt_toggle_err_gpio(struct pt_core_data *cd, u8 type)
  551. {
  552. pt_debug(cd->dev, DL_DEBUG, "%s called with type = %d\n",
  553. __func__, type);
  554. if (cd->err_gpio && type == cd->err_gpio_type) {
  555. pt_debug(cd->dev, DL_WARN, "%s: Toggle ERR GPIO\n", __func__);
  556. gpio_direction_output(cd->err_gpio,
  557. !gpio_get_value(cd->err_gpio));
  558. }
  559. }
  560. /*******************************************************************************
  561. * FUNCTION: _pt_request_toggle_err_gpio
  562. *
  563. * SUMMARY: Function pointer included in core_cmds to allow other modules
  564. * to request to toggle the err_gpio
  565. *
  566. * RETURN: n/a
  567. *
  568. * PARAMETERS:
  569. * *cd - pointer to core data
  570. * type - type of err that occurred
  571. ******************************************************************************/
  572. void _pt_request_toggle_err_gpio(struct device *dev, u8 type)
  573. {
  574. struct pt_core_data *cd = dev_get_drvdata(dev);
  575. pt_toggle_err_gpio(cd, type);
  576. }
  577. #endif /* TTDL_DIAGNOSTICS */
  578. /*******************************************************************************
  579. * FUNCTION: pt_hid_exec_cmd_and_wait_
  580. *
  581. * SUMMARY: Send the HID command to the DUT and wait for the response
  582. *
  583. * RETURN:
  584. * 0 = success
  585. * !0 = failure
  586. *
  587. * PARAMETERS:
  588. * *cd - pointer to core data
  589. * *hid_cmd - pointer to the HID command to send
  590. ******************************************************************************/
  591. static int pt_hid_exec_cmd_and_wait_(struct pt_core_data *cd,
  592. struct pt_hid_cmd *hid_cmd)
  593. {
  594. int rc = 0;
  595. int t;
  596. u16 timeout_ms;
  597. int *cmd_state;
  598. if (hid_cmd->reset_cmd)
  599. cmd_state = &cd->hid_reset_cmd_state;
  600. else
  601. cmd_state = &cd->hid_cmd_state;
  602. if (hid_cmd->wait_interrupt) {
  603. mutex_lock(&cd->system_lock);
  604. *cmd_state = 1;
  605. mutex_unlock(&cd->system_lock);
  606. }
  607. rc = pt_hid_exec_cmd_(cd, hid_cmd);
  608. if (rc) {
  609. if (hid_cmd->wait_interrupt)
  610. goto error;
  611. goto exit;
  612. }
  613. if (!hid_cmd->wait_interrupt)
  614. goto exit;
  615. if (hid_cmd->timeout_ms)
  616. timeout_ms = hid_cmd->timeout_ms;
  617. else
  618. timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT;
  619. t = wait_event_timeout(cd->wait_q, (*cmd_state == 0),
  620. msecs_to_jiffies(timeout_ms));
  621. if (IS_TMO(t)) {
  622. #ifdef TTDL_DIAGNOSTICS
  623. cd->bus_transmit_error_count++;
  624. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  625. #endif /* TTDL_DIAGNOSTICS */
  626. pt_debug(cd->dev, DL_ERROR,
  627. "%s: HID output cmd execution timed out\n",
  628. __func__);
  629. rc = -ETIME;
  630. goto error;
  631. }
  632. goto exit;
  633. error:
  634. mutex_lock(&cd->system_lock);
  635. *cmd_state = 0;
  636. mutex_unlock(&cd->system_lock);
  637. exit:
  638. return rc;
  639. }
  640. /*******************************************************************************
  641. * FUNCTION: pt_hid_cmd_reset_
  642. *
  643. * SUMMARY: Send the HID RESET command to the DUT
  644. *
  645. * RETURN:
  646. * 0 = success
  647. * !0 = failure
  648. *
  649. * PARAMETERS:
  650. * *cd - pointer to core data
  651. ******************************************************************************/
  652. static int pt_hid_cmd_reset_(struct pt_core_data *cd)
  653. {
  654. struct pt_hid_cmd hid_cmd = {
  655. .opcode = HID_CMD_RESET,
  656. .wait_interrupt = 1,
  657. .reset_cmd = 1,
  658. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  659. };
  660. return pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  661. }
  662. /*******************************************************************************
  663. * FUNCTION: pt_hid_cmd_reset
  664. *
  665. * SUMMARY: Wrapper function for pt_hid_cmd_reset_ that guarantees exclusive
  666. * access.
  667. *
  668. * RETURN:
  669. * 0 = success
  670. * !0 = failure
  671. *
  672. * PARAMETERS:
  673. * *cd - pointer to core data
  674. ******************************************************************************/
  675. static int pt_hid_cmd_reset(struct pt_core_data *cd)
  676. {
  677. int rc = 0;
  678. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  679. if (rc < 0) {
  680. pt_debug(cd->dev, DL_ERROR,
  681. "%s: fail get exclusive ex=%p own=%p\n",
  682. __func__, cd->exclusive_dev, cd->dev);
  683. return rc;
  684. }
  685. pt_debug(cd->dev, DL_INFO, "%s: Send HID Reset command\n", __func__);
  686. rc = pt_hid_cmd_reset_(cd);
  687. if (release_exclusive(cd, cd->dev) < 0)
  688. pt_debug(cd->dev, DL_ERROR,
  689. "%s: fail to release exclusive\n", __func__);
  690. return rc;
  691. }
  692. /*******************************************************************************
  693. * FUNCTION: pt_hid_cmd_set_power_
  694. *
  695. * SUMMARY: Send hid cmd to set power state for the DUT and wait for response
  696. *
  697. * RETURN:
  698. * 0 = success
  699. * !0 = failure
  700. *
  701. * PARAMETERS:
  702. * *cd - pointer to core data
  703. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  704. ******************************************************************************/
  705. static int pt_hid_cmd_set_power_(struct pt_core_data *cd,
  706. u8 power_state)
  707. {
  708. int rc = 0;
  709. struct pt_hid_cmd hid_cmd = {
  710. .opcode = HID_CMD_SET_POWER,
  711. .wait_interrupt = 1,
  712. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  713. };
  714. hid_cmd.power_state = power_state;
  715. /* The chip won't give response if goes to Deep Standby */
  716. if (power_state == HID_POWER_STANDBY) {
  717. rc = pt_hid_exec_cmd_(cd, &hid_cmd);
  718. if (rc)
  719. pt_debug(cd->dev, DL_ERROR,
  720. "%s: Failed to set power to state:%d\n",
  721. __func__, power_state);
  722. else
  723. cd->fw_sys_mode_in_standby_state = true;
  724. return rc;
  725. }
  726. cd->fw_sys_mode_in_standby_state = false;
  727. rc = pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  728. if (rc) {
  729. pt_debug(cd->dev, DL_ERROR,
  730. "%s: Failed to set power to state:%d\n",
  731. __func__, power_state);
  732. return rc;
  733. }
  734. /* validate */
  735. if ((cd->response_buf[2] != HID_RESPONSE_REPORT_ID)
  736. || ((cd->response_buf[3] & 0x3) != power_state)
  737. || ((cd->response_buf[4] & 0xF) != HID_CMD_SET_POWER))
  738. rc = -EINVAL;
  739. return rc;
  740. }
  741. /*******************************************************************************
  742. * FUNCTION: pt_hid_cmd_set_power
  743. *
  744. * SUMMARY: Wrapper function for pt_hid_cmd_set_power_ that guarantees
  745. * exclusive access.
  746. *
  747. * RETURN:
  748. * 0 = success
  749. * !0 = failure
  750. *
  751. * PARAMETERS:
  752. * *cd - pointer to core data
  753. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  754. ******************************************************************************/
  755. static int pt_hid_cmd_set_power(struct pt_core_data *cd,
  756. u8 power_state)
  757. {
  758. int rc;
  759. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  760. if (rc < 0) {
  761. pt_debug(cd->dev, DL_ERROR,
  762. "%s: fail get exclusive ex=%p own=%p\n",
  763. __func__, cd->exclusive_dev, cd->dev);
  764. return rc;
  765. }
  766. rc = pt_hid_cmd_set_power_(cd, power_state);
  767. if (release_exclusive(cd, cd->dev) < 0)
  768. pt_debug(cd->dev, DL_ERROR,
  769. "%s: fail to release exclusive\n", __func__);
  770. return rc;
  771. }
  772. static const u16 crc_table[16] = {
  773. 0x0000, 0x1021, 0x2042, 0x3063,
  774. 0x4084, 0x50a5, 0x60c6, 0x70e7,
  775. 0x8108, 0x9129, 0xa14a, 0xb16b,
  776. 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  777. };
  778. /*******************************************************************************
  779. * FUNCTION: _pt_compute_crc
  780. *
  781. * SUMMARY: Calculate CRC by CRC table.
  782. *
  783. * RETURN:
  784. * CRC calculation result
  785. *
  786. * PARAMETERS:
  787. * *buf - pointer to the data array to be calculated
  788. * size - size of data array
  789. ******************************************************************************/
  790. static u16 _pt_compute_crc(u8 *buf, u32 size)
  791. {
  792. u16 remainder = 0xFFFF;
  793. u16 xor_mask = 0x0000;
  794. u32 index;
  795. u32 byte_value;
  796. u32 table_index;
  797. u32 crc_bit_width = sizeof(u16) * 8;
  798. /* Divide the message by polynomial, via the table. */
  799. for (index = 0; index < size; index++) {
  800. byte_value = buf[index];
  801. table_index = ((byte_value >> 4) & 0x0F)
  802. ^ (remainder >> (crc_bit_width - 4));
  803. remainder = crc_table[table_index] ^ (remainder << 4);
  804. table_index = (byte_value & 0x0F)
  805. ^ (remainder >> (crc_bit_width - 4));
  806. remainder = crc_table[table_index] ^ (remainder << 4);
  807. }
  808. /* Perform the final remainder CRC. */
  809. return remainder ^ xor_mask;
  810. }
  811. u16 ccitt_Table[] = {
  812. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  813. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  814. 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  815. 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  816. 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  817. 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  818. 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  819. 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  820. 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  821. 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  822. 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  823. 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  824. 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  825. 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  826. 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  827. 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  828. 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  829. 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  830. 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  831. 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  832. 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  833. 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  834. 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  835. 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  836. 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  837. 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  838. 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  839. 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  840. 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  841. 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  842. 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  843. 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0,
  844. };
  845. /*******************************************************************************
  846. * FUNCTION: crc_ccitt_calculate
  847. *
  848. * SUMMARY: Calculate CRC with ccitt standard by CRC table.
  849. *
  850. * RETURN:
  851. * CRC calculation result
  852. *
  853. * PARAMETERS:
  854. * *q - pointer to the data array to be calculated
  855. * len - size of data array
  856. ******************************************************************************/
  857. static unsigned short crc_ccitt_calculate(unsigned char *q, int len)
  858. {
  859. unsigned short crc = 0xffff;
  860. while (len-- > 0)
  861. crc = ccitt_Table[(crc >> 8 ^ *q++) & 0xff] ^ (crc << 8);
  862. return crc;
  863. }
  864. /*******************************************************************************
  865. * FUNCTION: pt_pip2_cmd_calculate_crc
  866. *
  867. * SUMMARY: Calculate the CRC of a command packet
  868. *
  869. * RETURN: void
  870. *
  871. * PARAMETERS:
  872. * *cmd - pointer to command data
  873. * extra_bytes - Extra bytes included in command length
  874. ******************************************************************************/
  875. static void pt_pip2_cmd_calculate_crc(struct pip2_cmd_structure *cmd,
  876. u8 extra_bytes)
  877. {
  878. u8 buf[PT_MAX_PIP2_MSG_SIZE + 1] = {0};
  879. unsigned short crc;
  880. buf[0] = cmd->len & 0xff;
  881. buf[1] = (cmd->len & 0xff00) >> 8;
  882. buf[2] = cmd->seq;
  883. buf[3] = cmd->id;
  884. memcpy(&buf[4], cmd->data, cmd->len - extra_bytes);
  885. /* Calculate the CRC for the first 4 bytes above and the data payload */
  886. crc = crc_ccitt_calculate(buf, 4 + (cmd->len - extra_bytes));
  887. cmd->crc[0] = (crc & 0xff00) >> 8;
  888. cmd->crc[1] = (crc & 0xff);
  889. }
  890. /*******************************************************************************
  891. * FUNCTION: pt_pip2_get_next_cmd_seq
  892. *
  893. * SUMMARY: Gets the next sequence number for a PIP2 command. The sequence
  894. * number is a 3 bit value (bits [0-2]) but because TTDL will always have
  895. * the TAG bit set (bit 3), the counter starts at 0x08 and goes to 0x0F.
  896. * If the "force_pip2_seq" holds a valid seq value (0x08-0x0F) then do not
  897. * increment, just use the forced value.
  898. *
  899. * RETURN: Next command sequence number [0x08-0x0F]
  900. *
  901. * PARAMETERS:
  902. * *cd - pointer to core data
  903. ******************************************************************************/
  904. static u8 pt_pip2_get_next_cmd_seq(struct pt_core_data *cd)
  905. {
  906. #ifdef TTDL_DIAGNOSTICS
  907. if (cd->force_pip2_seq <= 0x07) {
  908. cd->pip2_cmd_tag_seq++;
  909. if (cd->pip2_cmd_tag_seq > 0x0F)
  910. cd->pip2_cmd_tag_seq = 0x08;
  911. } else {
  912. cd->pip2_cmd_tag_seq = cd->force_pip2_seq;
  913. }
  914. #else
  915. cd->pip2_cmd_tag_seq++;
  916. if (cd->pip2_cmd_tag_seq > 0x0F)
  917. cd->pip2_cmd_tag_seq = 0x08;
  918. #endif
  919. return cd->pip2_cmd_tag_seq;
  920. }
  921. /*
  922. * Following macros are to define the response time (the interval between PIP2
  923. * command finishes sending and INT pin falls). The unit is in microsecond.
  924. * It has different time settings between the solution GPIO polling and Bus
  925. * polling due to the considration for system load.
  926. */
  927. #ifdef PT_POLL_RESP_BY_BUS
  928. #define POLL_RETRY_DEFAULT_INTERVAL 50
  929. #define PIP2_RESP_DEFAULT_TIME_MIN 50
  930. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  931. #define PIP2_RESP_FILE_WRITE_TIME_MIN 220
  932. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  933. #else
  934. #define POLL_RETRY_DEFAULT_INTERVAL 20
  935. #define PIP2_RESP_DEFAULT_TIME_MIN 20
  936. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  937. #define PIP2_RESP_FILE_WRITE_TIME_MIN 20
  938. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  939. #endif
  940. /*
  941. * id: the command id defined in PIP2
  942. * response_len: the (maximum) length of response.
  943. * response_time_min: minimum response time in microsecond
  944. * response_time_max: maximum response time in microsecond
  945. */
  946. static const struct pip2_cmd_response_structure pip2_cmd_response[] = {
  947. {.id = PIP2_CMD_ID_PING,
  948. .response_len = 255,
  949. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  950. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  951. {.id = PIP2_CMD_ID_STATUS,
  952. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  953. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  954. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  955. {.id = PIP2_CMD_ID_CTRL,
  956. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  957. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  958. .response_time_max = PT_PIP2_CMD_FILE_ERASE_TIMEOUT},
  959. {.id = PIP2_CMD_ID_CONFIG,
  960. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  961. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  962. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  963. {.id = PIP2_CMD_ID_CLEAR,
  964. .response_len = PIP2_EXTRA_BYTES_NUM + 0,
  965. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  966. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  967. {.id = PIP2_CMD_ID_RESET,
  968. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  969. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  970. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  971. {.id = PIP2_CMD_ID_VERSION,
  972. .response_len = PIP2_EXTRA_BYTES_NUM + 23,
  973. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  974. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  975. {.id = PIP2_CMD_ID_FILE_OPEN,
  976. .response_len = PIP2_EXTRA_BYTES_NUM + 2,
  977. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  978. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  979. {.id = PIP2_CMD_ID_FILE_CLOSE,
  980. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  981. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  982. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  983. {.id = PIP2_CMD_ID_FILE_READ,
  984. .response_len = 255,
  985. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  986. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  987. {.id = PIP2_CMD_ID_FILE_WRITE,
  988. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  989. .response_time_min = PIP2_RESP_FILE_WRITE_TIME_MIN,
  990. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  991. {.id = PIP2_CMD_ID_FILE_IOCTL,
  992. .response_len = PIP2_EXTRA_BYTES_NUM + 10,
  993. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  994. .response_time_max = PIP2_RESP_FILE_IOCTL_TIME_MAX},
  995. {.id = PIP2_CMD_ID_FLASH_INFO,
  996. .response_len = PIP2_EXTRA_BYTES_NUM + 17,
  997. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  998. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  999. {.id = PIP2_CMD_ID_EXECUTE,
  1000. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1001. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1002. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1003. {.id = PIP2_CMD_ID_GET_LAST_ERRNO,
  1004. .response_len = PIP2_EXTRA_BYTES_NUM + 3,
  1005. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1006. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1007. {.id = PIP2_CMD_ID_EXIT_HOST_MODE,
  1008. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1009. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1010. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1011. {.id = PIP2_CMD_ID_READ_GPIO,
  1012. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  1013. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1014. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1015. {.id = PIP2_CMD_EXECUTE_SCAN,
  1016. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1017. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1018. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1019. {.id = PIP2_CMD_SET_PARAMETER,
  1020. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1021. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1022. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1023. {.id = PIP2_CMD_GET_PARAMETER,
  1024. .response_len = PIP2_EXTRA_BYTES_NUM + 7,
  1025. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1026. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1027. {.id = PIP2_CMD_SET_DDI_REG,
  1028. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1029. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1030. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1031. {.id = PIP2_CMD_GET_DDI_REG,
  1032. .response_len = PIP2_EXTRA_BYTES_NUM + 249,
  1033. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1034. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1035. {.id = PIP2_CMD_ID_END,
  1036. .response_len = 255,
  1037. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1038. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX}
  1039. };
  1040. /*******************************************************************************
  1041. * FUNCTION: pt_pip2_get_cmd_response_len
  1042. *
  1043. * SUMMARY: Gets the expected response length based on the command ID
  1044. *
  1045. * RETURN: Expected response length
  1046. *
  1047. * PARAMETERS:
  1048. * id - Command ID (-1 means input ID is not in list of PIP2 command)
  1049. ******************************************************************************/
  1050. static int pt_pip2_get_cmd_response_len(u8 id)
  1051. {
  1052. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1053. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1054. p++;
  1055. if (p->id != PIP2_CMD_ID_END)
  1056. return p->response_len;
  1057. else
  1058. return -EPERM;
  1059. }
  1060. /*******************************************************************************
  1061. * FUNCTION: pt_pip2_get_cmd_resp_time_min
  1062. *
  1063. * SUMMARY: Gets the minimum response time (the interval between PIP2 command
  1064. * finishes sending and INT pin falls) based on the command ID
  1065. *
  1066. * RETURN: Estimated minimum response time in microsecond
  1067. *
  1068. * PARAMETERS:
  1069. * id - Command ID
  1070. ******************************************************************************/
  1071. static u32 pt_pip2_get_cmd_resp_time_min(u8 id)
  1072. {
  1073. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1074. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1075. p++;
  1076. if (p->id != PIP2_CMD_ID_END)
  1077. return p->response_time_min;
  1078. else
  1079. return PIP2_RESP_DEFAULT_TIME_MIN;
  1080. }
  1081. /*******************************************************************************
  1082. * FUNCTION: pt_pip2_get_cmd_resp_time_max
  1083. *
  1084. * SUMMARY: Gets the maximum response time (the interval between PIP2 command
  1085. * finishes sending and INT pin falls) based on the command ID
  1086. *
  1087. * RETURN: Estimated maximum response time in microsecond
  1088. *
  1089. * PARAMETERS:
  1090. * id - Command ID
  1091. ******************************************************************************/
  1092. static u32 pt_pip2_get_cmd_resp_time_max(u8 id)
  1093. {
  1094. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1095. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1096. p++;
  1097. if (p->id != PIP2_CMD_ID_END)
  1098. return p->response_time_max;
  1099. else
  1100. return PIP2_RESP_DEFAULT_TIME_MAX;
  1101. }
  1102. /*******************************************************************************
  1103. * FUNCTION: pt_pip2_validate_response
  1104. *
  1105. * SUMMARY: Validate the response of PIP2 command.
  1106. *
  1107. * RETURN:
  1108. * 0 = success
  1109. * !0 = failure
  1110. *
  1111. * PARAMETERS:
  1112. * *cd - pointer to core data
  1113. * *pip2_cmd - pointer to PIP2 command to send
  1114. * *read_buf - pointer to response buffer
  1115. * actual_read_len - actual read length of the response
  1116. ******************************************************************************/
  1117. static int pt_pip2_validate_response(struct pt_core_data *cd,
  1118. struct pip2_cmd_structure *pip2_cmd, u8 *read_buf,
  1119. u16 actual_read_len)
  1120. {
  1121. int rc = 0;
  1122. u8 response_seq = 0;
  1123. u8 reserved_bits = 0;
  1124. u8 cmd_id = 0;
  1125. u8 response_bit = 0;
  1126. unsigned short calc_crc = 0;
  1127. unsigned short resp_crc = 0;
  1128. /* Verify the length of response buffer */
  1129. if (actual_read_len < PT_MIN_PIP2_PACKET_SIZE) {
  1130. pt_debug(cd->dev, DL_ERROR,
  1131. "%s cmd[0x%02X] read length ERR: read_len = %d\n",
  1132. __func__, pip2_cmd->id, actual_read_len);
  1133. rc = -EINVAL;
  1134. goto exit;
  1135. }
  1136. /* Verify the CRC */
  1137. calc_crc = crc_ccitt_calculate(read_buf, actual_read_len - 2);
  1138. resp_crc = read_buf[actual_read_len - 2] << 8;
  1139. resp_crc |= read_buf[actual_read_len - 1];
  1140. if (resp_crc != calc_crc) {
  1141. pt_debug(cd->dev, DL_ERROR,
  1142. "%s: cmd[0x%02X] CRC ERR: calc=0x%04X rsp=0x%04X\n",
  1143. __func__, pip2_cmd->id, calc_crc, resp_crc);
  1144. #ifdef TTDL_DIAGNOSTICS
  1145. cd->pip2_crc_error_count++;
  1146. #endif /* TTDL_DIAGNOSTICS */
  1147. rc = -EINVAL;
  1148. goto exit;
  1149. }
  1150. /* Verify the response bit is set */
  1151. response_bit = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x80;
  1152. if (!response_bit) {
  1153. pt_debug(cd->dev, DL_ERROR,
  1154. "%s cmd[0x%02X] response bit ERR: response_bit = %d\n",
  1155. __func__, pip2_cmd->id, response_bit);
  1156. rc = -EINVAL;
  1157. goto exit;
  1158. }
  1159. /* Verify the command ID matches from command to response */
  1160. cmd_id = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x7F;
  1161. if (cmd_id != pip2_cmd->id) {
  1162. pt_debug(cd->dev, DL_ERROR,
  1163. "%s cmd[0x%02X] command ID ERR: cmd_id = 0x%02X\n",
  1164. __func__, pip2_cmd->id, cmd_id);
  1165. rc = -EINVAL;
  1166. goto exit;
  1167. }
  1168. /* Verify the SEQ number matches from command to response */
  1169. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F;
  1170. if ((pip2_cmd->seq & 0x0F) != response_seq) {
  1171. pt_debug(cd->dev, DL_ERROR,
  1172. "%s cmd[0x%02X] send_seq = 0x%02X, resp_seq = 0x%02X\n",
  1173. __func__, pip2_cmd->id,
  1174. pip2_cmd->seq, response_seq);
  1175. rc = -EINVAL;
  1176. goto exit;
  1177. }
  1178. /* Verify the reserved bits are 0 */
  1179. reserved_bits = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0xF0;
  1180. if (reserved_bits)
  1181. pt_debug(cd->dev, DL_WARN,
  1182. "%s cmd[0x%02X] reserved_bits = 0x%02X\n",
  1183. __func__, pip2_cmd->id, reserved_bits);
  1184. exit:
  1185. if (rc)
  1186. pt_pr_buf(cd->dev, DL_WARN, cd->input_buf, actual_read_len,
  1187. "PIP RSP:");
  1188. return rc;
  1189. }
  1190. /*******************************************************************************
  1191. * FUNCTION: pt_hid_output_validate_bl_response
  1192. *
  1193. * SUMMARY: Validate the response of bootloader command.
  1194. *
  1195. * RETURN:
  1196. * 0 = success
  1197. * !0 = failure
  1198. *
  1199. * PARAMETERS:
  1200. * *cd - pointer to core data
  1201. * *hid_output - pointer to hid output data structure
  1202. ******************************************************************************/
  1203. static int pt_hid_output_validate_bl_response(
  1204. struct pt_core_data *cd,
  1205. struct pt_hid_output *hid_output)
  1206. {
  1207. u16 size;
  1208. u16 crc;
  1209. u8 status;
  1210. size = get_unaligned_le16(&cd->response_buf[0]);
  1211. if (hid_output->reset_expected && !size)
  1212. return 0;
  1213. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1214. != PT_PIP_BL_RESPONSE_REPORT_ID) {
  1215. pt_debug(cd->dev, DL_ERROR,
  1216. "%s: BL output response, wrong report_id\n", __func__);
  1217. return -EPROTO;
  1218. }
  1219. if (cd->response_buf[4] != PIP1_BL_SOP) {
  1220. pt_debug(cd->dev, DL_ERROR,
  1221. "%s: BL output response, wrong SOP\n", __func__);
  1222. return -EPROTO;
  1223. }
  1224. if (cd->response_buf[size - 1] != PIP1_BL_EOP) {
  1225. pt_debug(cd->dev, DL_ERROR,
  1226. "%s: BL output response, wrong EOP\n", __func__);
  1227. return -EPROTO;
  1228. }
  1229. crc = _pt_compute_crc(&cd->response_buf[4], size - 7);
  1230. if (cd->response_buf[size - 3] != LOW_BYTE(crc)
  1231. || cd->response_buf[size - 2] != HI_BYTE(crc)) {
  1232. pt_debug(cd->dev, DL_ERROR,
  1233. "%s: BL output response, wrong CRC 0x%X\n",
  1234. __func__, crc);
  1235. return -EPROTO;
  1236. }
  1237. status = cd->response_buf[5];
  1238. if (status) {
  1239. pt_debug(cd->dev, DL_ERROR,
  1240. "%s: BL output response, ERROR:%d\n",
  1241. __func__, status);
  1242. return -EPROTO;
  1243. }
  1244. return 0;
  1245. }
  1246. /*******************************************************************************
  1247. * FUNCTION: pt_hid_output_validate_app_response
  1248. *
  1249. * SUMMARY: Validate the response of application command.
  1250. *
  1251. * RETURN:
  1252. * 0 = success
  1253. * !0 = failure
  1254. *
  1255. * PARAMETERS:
  1256. * *cd - pointer to core data
  1257. * *hid_output - pointer to hid output data structure
  1258. ******************************************************************************/
  1259. static int pt_hid_output_validate_app_response(
  1260. struct pt_core_data *cd,
  1261. struct pt_hid_output *hid_output)
  1262. {
  1263. int command_code;
  1264. u16 size;
  1265. size = get_unaligned_le16(&cd->response_buf[0]);
  1266. if (hid_output->reset_expected && !size)
  1267. return 0;
  1268. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1269. != PT_PIP_NON_HID_RESPONSE_ID) {
  1270. pt_debug(cd->dev, DL_ERROR,
  1271. "%s: APP output response, wrong report_id\n", __func__);
  1272. return -EPROTO;
  1273. }
  1274. command_code = cd->response_buf[PIP1_RESP_COMMAND_ID_OFFSET]
  1275. & PIP1_RESP_COMMAND_ID_MASK;
  1276. if (command_code != hid_output->command_code) {
  1277. pt_debug(cd->dev, DL_ERROR,
  1278. "%s: APP output response, wrong command_code:%X\n",
  1279. __func__, command_code);
  1280. return -EPROTO;
  1281. }
  1282. return 0;
  1283. }
  1284. /*******************************************************************************
  1285. * FUNCTION: pt_check_set_parameter
  1286. *
  1287. * SUMMARY: Check command input and response for Set Parameter command.And
  1288. * store the parameter to the list for resume work if pass the check.
  1289. *
  1290. * PARAMETERS:
  1291. * *cd - pointer to core data
  1292. * *hid_output - pointer to hid output data structure
  1293. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1294. ******************************************************************************/
  1295. static void pt_check_set_parameter(struct pt_core_data *cd,
  1296. struct pt_hid_output *hid_output, bool raw)
  1297. {
  1298. u8 *param_buf;
  1299. u32 param_value = 0;
  1300. u8 param_size;
  1301. u8 param_id;
  1302. int i = 0;
  1303. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  1304. return;
  1305. /* Check command input for Set Parameter command */
  1306. if (raw && hid_output->length >= 10 && hid_output->length <= 13
  1307. && !memcmp(&hid_output->write_buf[0],
  1308. &cd->hid_desc.output_register,
  1309. sizeof(cd->hid_desc.output_register))
  1310. && hid_output->write_buf[4] ==
  1311. PT_PIP_NON_HID_COMMAND_ID
  1312. && hid_output->write_buf[6] ==
  1313. PIP1_CMD_ID_SET_PARAM)
  1314. param_buf = &hid_output->write_buf[7];
  1315. else if (!raw && hid_output->cmd_type == PIP1_CMD_TYPE_FW
  1316. && hid_output->command_code == PIP1_CMD_ID_SET_PARAM
  1317. && hid_output->write_length >= 3
  1318. && hid_output->write_length <= 6)
  1319. param_buf = &hid_output->write_buf[0];
  1320. else
  1321. return;
  1322. /* Get parameter ID, size and value */
  1323. param_id = param_buf[0];
  1324. param_size = param_buf[1];
  1325. if (param_size > 4) {
  1326. pt_debug(cd->dev, DL_ERROR,
  1327. "%s: Invalid parameter size\n", __func__);
  1328. return;
  1329. }
  1330. param_buf = &param_buf[2];
  1331. while (i < param_size)
  1332. param_value += *(param_buf++) << (8 * i++);
  1333. /* Check command response for Set Parameter command */
  1334. if (cd->response_buf[2] != PT_PIP_NON_HID_RESPONSE_ID
  1335. || (cd->response_buf[4] &
  1336. PIP1_RESP_COMMAND_ID_MASK) !=
  1337. PIP1_CMD_ID_SET_PARAM
  1338. || cd->response_buf[5] != param_id
  1339. || cd->response_buf[6] != param_size) {
  1340. pt_debug(cd->dev, DL_ERROR,
  1341. "%s: Set Parameter command not successful\n",
  1342. __func__);
  1343. return;
  1344. }
  1345. pt_add_parameter(cd, param_id, param_value, param_size);
  1346. }
  1347. /*******************************************************************************
  1348. * FUNCTION: pt_check_command
  1349. *
  1350. * SUMMARY: Check the output command. The function pt_check_set_parameter() is
  1351. * called here to check output command and store parameter to the list.
  1352. *
  1353. * PARAMETERS:
  1354. * *cd - pointer to core data
  1355. * *hid_output - pointer to hid output data structure
  1356. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1357. ******************************************************************************/
  1358. static void pt_check_command(struct pt_core_data *cd,
  1359. struct pt_hid_output *hid_output, bool raw)
  1360. {
  1361. pt_check_set_parameter(cd, hid_output, raw);
  1362. }
  1363. /*******************************************************************************
  1364. * FUNCTION: pt_hid_output_validate_response
  1365. *
  1366. * SUMMARY: Validate the response of application or bootloader command.
  1367. *
  1368. * RETURN:
  1369. * 0 = success
  1370. * !0 = failure
  1371. *
  1372. * PARAMETERS:
  1373. * *cd - pointer to core data
  1374. * *hid_output - pointer to hid output data structure
  1375. ******************************************************************************/
  1376. static int pt_hid_output_validate_response(struct pt_core_data *cd,
  1377. struct pt_hid_output *hid_output)
  1378. {
  1379. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1380. return pt_hid_output_validate_bl_response(cd, hid_output);
  1381. return pt_hid_output_validate_app_response(cd, hid_output);
  1382. }
  1383. /*******************************************************************************
  1384. * FUNCTION: pt_hid_send_output_user_
  1385. *
  1386. * SUMMARY: Blindly send user data to the DUT.
  1387. *
  1388. * RETURN:
  1389. * 0 = success
  1390. * !0 = failure
  1391. *
  1392. * PARAMETERS:
  1393. * *cd - pointer to core data
  1394. * *hid_output - pointer to the command to send
  1395. ******************************************************************************/
  1396. static int pt_hid_send_output_user_(struct pt_core_data *cd,
  1397. struct pt_hid_output *hid_output)
  1398. {
  1399. int rc = 0;
  1400. int cmd;
  1401. if (!hid_output->length || !hid_output->write_buf)
  1402. return -EINVAL;
  1403. if (cd->pip2_prot_active) {
  1404. cmd = hid_output->write_buf[PIP2_CMD_COMMAND_ID_OFFSET];
  1405. cmd &= PIP2_CMD_COMMAND_ID_MASK;
  1406. } else
  1407. cmd = hid_output->write_buf[PIP1_CMD_COMMAND_ID_OFFSET];
  1408. pt_debug(cd->dev, DL_INFO,
  1409. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1410. __func__, hid_output->length, cmd);
  1411. pt_pr_buf(cd->dev, DL_DEBUG, hid_output->write_buf,
  1412. hid_output->length, ">>> User CMD");
  1413. rc = pt_adap_write_read_specific(cd, hid_output->length,
  1414. hid_output->write_buf, NULL);
  1415. if (rc)
  1416. pt_debug(cd->dev, DL_ERROR,
  1417. "%s: Fail pt_adap_transfer\n", __func__);
  1418. return rc;
  1419. }
  1420. /*******************************************************************************
  1421. * FUNCTION: pt_hid_send_output_user_and_wait_
  1422. *
  1423. * SUMMARY: Blindly send user data to the DUT and wait for the response.
  1424. *
  1425. * RETURN:
  1426. * 0 = success
  1427. * !0 = failure
  1428. *
  1429. * PARAMETERS:
  1430. * *cd - pointer to core data
  1431. * *hid_output - pointer to the command to send
  1432. ******************************************************************************/
  1433. static int pt_hid_send_output_user_and_wait_(struct pt_core_data *cd,
  1434. struct pt_hid_output *hid_output)
  1435. {
  1436. int rc = 0;
  1437. int t;
  1438. mutex_lock(&cd->system_lock);
  1439. cd->hid_cmd_state = PIP1_CMD_ID_USER_CMD + 1;
  1440. mutex_unlock(&cd->system_lock);
  1441. rc = pt_hid_send_output_user_(cd, hid_output);
  1442. if (rc)
  1443. goto error;
  1444. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1445. msecs_to_jiffies(cd->pip_cmd_timeout));
  1446. if (IS_TMO(t)) {
  1447. #ifdef TTDL_DIAGNOSTICS
  1448. cd->bus_transmit_error_count++;
  1449. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1450. #endif /* TTDL_DIAGNOSTICS */
  1451. pt_debug(cd->dev, DL_ERROR,
  1452. "%s: HID output cmd execution timed out\n",
  1453. __func__);
  1454. rc = -ETIME;
  1455. goto error;
  1456. }
  1457. pt_check_command(cd, hid_output, true);
  1458. goto exit;
  1459. error:
  1460. mutex_lock(&cd->system_lock);
  1461. cd->hid_cmd_state = 0;
  1462. mutex_unlock(&cd->system_lock);
  1463. exit:
  1464. return rc;
  1465. }
  1466. /*******************************************************************************
  1467. * FUNCTION: pt_check_irq_asserted
  1468. *
  1469. * SUMMARY: Checks if the IRQ GPIO is asserted or not. There are times when
  1470. * the FW can hold the INT line low ~150us after the read is complete.
  1471. * NOTE: if irq_stat is not defined this function will return false
  1472. *
  1473. * RETURN:
  1474. * true = IRQ asserted
  1475. * false = IRQ not asserted
  1476. *
  1477. * PARAMETERS:
  1478. * *cd - pointer to core data
  1479. ******************************************************************************/
  1480. static bool pt_check_irq_asserted(struct pt_core_data *cd)
  1481. {
  1482. #ifdef ENABLE_WORKAROUND_FOR_GLITCH_AFTER_BL_LAUNCH_APP
  1483. /*
  1484. * Workaround for FW defect, CDT165308
  1485. * bl_launch app creates a glitch in IRQ line
  1486. */
  1487. if (cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1
  1488. && cd->cpdata->irq_stat) {
  1489. /*
  1490. * in X1S panel and GC1546 panel, the width for the INT
  1491. * glitch is about 4us,the normal INT width of response
  1492. * will last more than 200us, so use 10us delay
  1493. * for distinguish the glitch the normal INT is enough.
  1494. */
  1495. udelay(10);
  1496. }
  1497. #endif
  1498. if (cd->cpdata->irq_stat) {
  1499. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1500. == PT_IRQ_ASSERTED_VALUE) {
  1501. /* Debounce to allow FW to release INT */
  1502. usleep_range(100, 200);
  1503. }
  1504. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1505. == PT_IRQ_ASSERTED_VALUE)
  1506. return true;
  1507. else
  1508. return false;
  1509. }
  1510. return true;
  1511. }
  1512. /*******************************************************************************
  1513. * FUNCTION: pt_flush_bus
  1514. *
  1515. * SUMMARY: Force flushing the bus by reading len bytes or forced 255 bytes
  1516. * Used if IRQ is found to be stuck low
  1517. *
  1518. * RETURN: Length of bytes read from bus
  1519. *
  1520. * PARAMETERS:
  1521. * *cd - pointer to core data
  1522. * flush_type - type of flush
  1523. * - PT_FLUSH_BUS_BASED_ON_LEN (two reads)
  1524. * - PT_FLUSH_BUS_FULL_256_READ
  1525. * *read_buf - pointer to store read data
  1526. ******************************************************************************/
  1527. static ssize_t pt_flush_bus(struct pt_core_data *cd,
  1528. u8 flush_type, u8 *read_buf)
  1529. {
  1530. u8 buf[PT_MAX_PIP2_MSG_SIZE];
  1531. u16 pip_len;
  1532. int bytes_read;
  1533. int rc = 0;
  1534. if (flush_type == PT_FLUSH_BUS_BASED_ON_LEN) {
  1535. rc = pt_adap_read_default(cd, buf, 2);
  1536. if (rc) {
  1537. bytes_read = 0;
  1538. goto exit;
  1539. }
  1540. pip_len = get_unaligned_le16(&buf[0]);
  1541. if (pip_len == 2 || pip_len >= PT_PIP_1P7_EMPTY_BUF) {
  1542. #ifdef TTDL_DIAGNOSTICS
  1543. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EMPTY_PACKET);
  1544. #endif
  1545. bytes_read = 2;
  1546. pt_debug(cd->dev, DL_INFO,
  1547. "%s: Empty buf detected - len=0x%04X\n",
  1548. __func__, pip_len);
  1549. } else if (pip_len == 0) {
  1550. bytes_read = 0;
  1551. pt_debug(cd->dev, DL_INFO,
  1552. "%s: Sentinel detected\n", __func__);
  1553. } else if (pip_len > PT_MAX_PIP2_MSG_SIZE) {
  1554. pt_debug(cd->dev, DL_ERROR,
  1555. "%s: Illegal len=0x%04x, force %d byte read\n",
  1556. __func__, pip_len, PT_MAX_PIP2_MSG_SIZE);
  1557. rc = pt_adap_read_default(cd, buf,
  1558. PT_MAX_PIP2_MSG_SIZE);
  1559. if (!rc)
  1560. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1561. else
  1562. bytes_read = 0;
  1563. } else {
  1564. pt_debug(cd->dev, DL_INFO,
  1565. "%s: Flush read of %d bytes...\n",
  1566. __func__, pip_len);
  1567. rc = pt_adap_read_default(cd, buf, pip_len);
  1568. if (!rc)
  1569. bytes_read = pip_len;
  1570. else
  1571. bytes_read = 0;
  1572. }
  1573. } else {
  1574. pt_debug(cd->dev, DL_INFO,
  1575. "%s: Forced flush of max %d bytes...\n",
  1576. __func__, PT_MAX_PIP2_MSG_SIZE);
  1577. rc = pt_adap_read_default(cd, buf, PT_MAX_PIP2_MSG_SIZE);
  1578. if (!rc)
  1579. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1580. else
  1581. bytes_read = 0;
  1582. }
  1583. if (read_buf && (bytes_read > 3))
  1584. memcpy(read_buf, buf, bytes_read);
  1585. exit:
  1586. return bytes_read;
  1587. }
  1588. /*******************************************************************************
  1589. * FUNCTION: pt_flush_bus_if_irq_asserted
  1590. *
  1591. * SUMMARY: This function will flush the active bus if the INT is found to be
  1592. * asserted.
  1593. *
  1594. * RETURN: bytes cleared from bus
  1595. *
  1596. * PARAMETERS:
  1597. * *cd - pointer the core data structure
  1598. * flush_type - type of flush
  1599. * - PT_FLUSH_BUS_BASED_ON_LEN
  1600. * - PT_FLUSH_BUS_FULL_256_READ
  1601. ******************************************************************************/
  1602. static int pt_flush_bus_if_irq_asserted(struct pt_core_data *cd, u8 flush_type)
  1603. {
  1604. int count = 0;
  1605. int bytes_read = 0;
  1606. while (pt_check_irq_asserted(cd) && count < 5) {
  1607. count++;
  1608. bytes_read = pt_flush_bus(cd, flush_type, NULL);
  1609. if (bytes_read) {
  1610. pt_debug(cd->dev, DL_WARN,
  1611. "%s: Cleared %d bytes off bus\n",
  1612. __func__, bytes_read);
  1613. }
  1614. }
  1615. if (pt_check_irq_asserted(cd)) {
  1616. pt_debug(cd->dev, DL_ERROR,
  1617. "%s: IRQ still asserted, %d bytes read\n",
  1618. __func__, bytes_read);
  1619. } else {
  1620. pt_debug(cd->dev, DL_INFO,
  1621. "%s: IRQ cleared, %d bytes read\n",
  1622. __func__, bytes_read);
  1623. }
  1624. return bytes_read;
  1625. }
  1626. /*******************************************************************************
  1627. * FUNCTION: pt_hid_send_output_
  1628. *
  1629. * SUMMARY: Send a touch application command to the DUT
  1630. *
  1631. * RETURN:
  1632. * 0 = success
  1633. * !0 = failure
  1634. *
  1635. * PARAMETERS:
  1636. * *cd - pointer to core data
  1637. * *hid_output - pointer to the command to send
  1638. ******************************************************************************/
  1639. static int pt_hid_send_output_(struct pt_core_data *cd,
  1640. struct pt_hid_output *hid_output)
  1641. {
  1642. int rc = 0;
  1643. u8 *cmd;
  1644. u16 length;
  1645. u16 crc;
  1646. u8 report_id;
  1647. u8 cmd_offset = 0;
  1648. u8 cmd_allocated = 0;
  1649. switch (hid_output->cmd_type) {
  1650. case PIP1_CMD_TYPE_FW:
  1651. report_id = PT_PIP_NON_HID_COMMAND_ID;
  1652. length = 5;
  1653. break;
  1654. case PIP1_CMD_TYPE_BL:
  1655. report_id = PT_PIP_BL_COMMAND_REPORT_ID;
  1656. length = 11 /* 5 + SOP + LEN(2) + CRC(2) + EOP */;
  1657. break;
  1658. default:
  1659. return -EINVAL;
  1660. }
  1661. length += hid_output->write_length;
  1662. if (length + 2 > PT_PREALLOCATED_CMD_BUFFER) {
  1663. cmd = kzalloc(length + 2, GFP_KERNEL);
  1664. if (!cmd)
  1665. return -ENOMEM;
  1666. cmd_allocated = 1;
  1667. } else {
  1668. cmd = cd->cmd_buf;
  1669. }
  1670. /* Set Output register */
  1671. memcpy(&cmd[cmd_offset], &cd->hid_desc.output_register,
  1672. sizeof(cd->hid_desc.output_register));
  1673. cmd_offset += sizeof(cd->hid_desc.output_register);
  1674. cmd[cmd_offset++] = LOW_BYTE(length);
  1675. cmd[cmd_offset++] = HI_BYTE(length);
  1676. cmd[cmd_offset++] = report_id;
  1677. cmd[cmd_offset++] = 0x0; /* reserved */
  1678. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1679. cmd[cmd_offset++] = PIP1_BL_SOP;
  1680. cmd[cmd_offset++] = hid_output->command_code;
  1681. /* Set Data Length for bootloader */
  1682. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1683. cmd[cmd_offset++] = LOW_BYTE(hid_output->write_length);
  1684. cmd[cmd_offset++] = HI_BYTE(hid_output->write_length);
  1685. }
  1686. /* Set Data */
  1687. if (hid_output->write_length && hid_output->write_buf) {
  1688. memcpy(&cmd[cmd_offset], hid_output->write_buf,
  1689. hid_output->write_length);
  1690. cmd_offset += hid_output->write_length;
  1691. }
  1692. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1693. crc = _pt_compute_crc(&cmd[6],
  1694. hid_output->write_length + 4);
  1695. cmd[cmd_offset++] = LOW_BYTE(crc);
  1696. cmd[cmd_offset++] = HI_BYTE(crc);
  1697. cmd[cmd_offset++] = PIP1_BL_EOP;
  1698. }
  1699. pt_debug(cd->dev, DL_INFO,
  1700. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1701. __func__, length + 2, hid_output->command_code);
  1702. pt_pr_buf(cd->dev, DL_DEBUG, cmd, length + 2, ">>> CMD");
  1703. rc = pt_adap_write_read_specific(cd, length + 2, cmd, NULL);
  1704. if (rc)
  1705. pt_debug(cd->dev, DL_ERROR,
  1706. "%s: Fail pt_adap_transfer rc=%d\n", __func__, rc);
  1707. if (cmd_allocated)
  1708. kfree(cmd);
  1709. return rc;
  1710. }
  1711. /*******************************************************************************
  1712. * FUNCTION: pt_pip1_send_output_and_wait_
  1713. *
  1714. * SUMMARY: Send valid PIP1 command to the DUT and wait for the response.
  1715. *
  1716. * RETURN:
  1717. * 0 = success
  1718. * !0 = failure
  1719. *
  1720. * PARAMETERS:
  1721. * *cd - pointer to core data
  1722. * *hid_output - pointer to the command to send
  1723. ******************************************************************************/
  1724. static int pt_pip1_send_output_and_wait_(struct pt_core_data *cd,
  1725. struct pt_hid_output *hid_output)
  1726. {
  1727. int rc = 0;
  1728. int t;
  1729. u16 timeout_ms;
  1730. mutex_lock(&cd->system_lock);
  1731. cd->hid_cmd_state = hid_output->command_code + 1;
  1732. mutex_unlock(&cd->system_lock);
  1733. if (hid_output->timeout_ms)
  1734. timeout_ms = hid_output->timeout_ms;
  1735. else
  1736. timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT;
  1737. rc = pt_hid_send_output_(cd, hid_output);
  1738. if (rc)
  1739. goto error;
  1740. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1741. msecs_to_jiffies(timeout_ms));
  1742. if (IS_TMO(t)) {
  1743. #ifdef TTDL_DIAGNOSTICS
  1744. cd->bus_transmit_error_count++;
  1745. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1746. #endif /* TTDL_DIAGNOSTICS */
  1747. pt_debug(cd->dev, DL_ERROR,
  1748. "%s: HID output cmd execution timed out (%dms)\n",
  1749. __func__, timeout_ms);
  1750. rc = -ETIME;
  1751. goto error;
  1752. }
  1753. if (!hid_output->novalidate)
  1754. rc = pt_hid_output_validate_response(cd, hid_output);
  1755. pt_check_command(cd, hid_output, false);
  1756. goto exit;
  1757. error:
  1758. mutex_lock(&cd->system_lock);
  1759. cd->hid_cmd_state = 0;
  1760. mutex_unlock(&cd->system_lock);
  1761. exit:
  1762. return rc;
  1763. }
  1764. /*******************************************************************************
  1765. * FUNCTION: pt_hid_output_user_cmd_
  1766. *
  1767. * SUMMARY: Load the write buffer into a HID structure and send it as a HID cmd
  1768. * to the DUT waiting for the response and loading it into the read buffer
  1769. *
  1770. * RETURN:
  1771. * 0 = success
  1772. * !0 = failure
  1773. *
  1774. * PARAMETERS:
  1775. * *cd - pointer to core data
  1776. * read_len - expected read length of the response
  1777. * *read_buf - pointer to where the response will be loaded
  1778. * write_len - length of the write buffer
  1779. * *write_buf - pointer to the write buffer
  1780. * *actual_read_len - pointer to the actual amount of data read back
  1781. ******************************************************************************/
  1782. static int pt_hid_output_user_cmd_(struct pt_core_data *cd,
  1783. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1784. u16 *actual_read_len)
  1785. {
  1786. int rc = 0;
  1787. u16 size;
  1788. struct pt_hid_output hid_output = {
  1789. .length = write_len,
  1790. .write_buf = write_buf,
  1791. };
  1792. #ifdef TTHE_TUNER_SUPPORT
  1793. if (!cd->pip2_send_user_cmd) {
  1794. int command_code = 0;
  1795. int len;
  1796. /* Print up to cmd ID */
  1797. len = PIP1_CMD_COMMAND_ID_OFFSET + 1;
  1798. if (write_len < len)
  1799. len = write_len;
  1800. else
  1801. command_code = write_buf[PIP1_CMD_COMMAND_ID_OFFSET]
  1802. & PIP1_CMD_COMMAND_ID_MASK;
  1803. /* Don't print EXEC_PANEL_SCAN & RETRIEVE_PANEL_SCAN commands */
  1804. if (command_code != PIP1_CMD_ID_EXEC_PANEL_SCAN &&
  1805. command_code != PIP1_CMD_ID_RETRIEVE_PANEL_SCAN)
  1806. tthe_print(cd, write_buf, len, "CMD=");
  1807. }
  1808. #endif
  1809. rc = pt_hid_send_output_user_and_wait_(cd, &hid_output);
  1810. if (rc)
  1811. return rc;
  1812. /* Get the response size from the first 2 bytes in the response */
  1813. size = get_unaligned_le16(&cd->response_buf[0]);
  1814. /* Ensure size is not greater than max buffer size */
  1815. if (size > PT_MAX_PIP2_MSG_SIZE)
  1816. size = PT_MAX_PIP2_MSG_SIZE;
  1817. /* Minimum size to read is the 2 byte len field */
  1818. if (size == 0)
  1819. size = 2;
  1820. if (size > read_len) {
  1821. pt_debug(cd->dev, DL_ERROR,
  1822. "%s: PIP2 len field=%d, requested read_len=%d\n",
  1823. __func__, size, read_len);
  1824. *actual_read_len = 0;
  1825. return -EIO;
  1826. }
  1827. memcpy(read_buf, cd->response_buf, size);
  1828. *actual_read_len = size;
  1829. return 0;
  1830. }
  1831. /*******************************************************************************
  1832. * FUNCTION: pt_hid_output_user_cmd
  1833. *
  1834. * SUMMARY: Protected call to pt_hid_output_user_cmd_ by exclusive access to
  1835. * the DUT.
  1836. *
  1837. * RETURN:
  1838. * 0 = success
  1839. * !0 = failure
  1840. *
  1841. * PARAMETERS:
  1842. * *cd - pointer to core data
  1843. * read_len - length of data to read
  1844. * *read_buf - pointer to store read data
  1845. * write_len - length of data to write
  1846. * *write_buf - pointer to buffer to write
  1847. * *actual_read_len - pointer to store data length actually read
  1848. ******************************************************************************/
  1849. static int pt_hid_output_user_cmd(struct pt_core_data *cd,
  1850. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1851. u16 *actual_read_len)
  1852. {
  1853. int rc;
  1854. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  1855. if (rc < 0) {
  1856. pt_debug(cd->dev, DL_ERROR,
  1857. "%s: fail get exclusive ex=%p own=%p\n",
  1858. __func__, cd->exclusive_dev, cd->dev);
  1859. return rc;
  1860. }
  1861. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1862. write_len, write_buf, actual_read_len);
  1863. if (release_exclusive(cd, cd->dev) < 0)
  1864. pt_debug(cd->dev, DL_ERROR,
  1865. "%s: fail to release exclusive\n", __func__);
  1866. return rc;
  1867. }
  1868. /*******************************************************************************
  1869. * FUNCTION: _pt_request_pip2_send_cmd
  1870. *
  1871. * SUMMARY: Writes a PIP2 command packet to DUT, then waits for the
  1872. * interrupt and reads response data to read_buf
  1873. *
  1874. * RETURN:
  1875. * 0 = success
  1876. * !0 = failure
  1877. *
  1878. * PARAMETERS:
  1879. * *dev - pointer to device structure
  1880. * protect - flag to run in protected mode
  1881. * id - ID of PIP command
  1882. * *data - pointer to PIP data payload
  1883. * report_body_len - report length
  1884. * *read_buf - pointer to response buffer
  1885. * *actual_read_len - pointer to response buffer length
  1886. ******************************************************************************/
  1887. static int _pt_request_pip2_send_cmd(struct device *dev,
  1888. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  1889. u16 *actual_read_len)
  1890. {
  1891. struct pt_core_data *cd = dev_get_drvdata(dev);
  1892. struct pip2_cmd_structure pip2_cmd;
  1893. int rc = 0;
  1894. int i = 0;
  1895. int j = 0;
  1896. u16 write_len;
  1897. u8 *write_buf = NULL;
  1898. u16 read_len;
  1899. u8 extra_bytes;
  1900. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  1901. /* Hard coded register for PIP2.x */
  1902. pip2_cmd.reg[0] = 0x01;
  1903. pip2_cmd.reg[1] = 0x01;
  1904. /*
  1905. * For PIP2.1+ the length field value includes itself:
  1906. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  1907. *
  1908. * The overall write length must include only the register:
  1909. * ADD 2: 2 (Register)
  1910. */
  1911. extra_bytes = 6;
  1912. write_len = 2;
  1913. /* PIP2 the CMD ID is a 7bit field */
  1914. if (id > PIP2_CMD_ID_END) {
  1915. pt_debug(dev, DL_WARN, "%s: Invalid PIP2 CMD ID 0x%02X\n",
  1916. __func__, id);
  1917. rc = -EINVAL;
  1918. goto exit;
  1919. }
  1920. pip2_cmd.len = report_body_len + extra_bytes;
  1921. pip2_cmd.id = id & PIP2_CMD_COMMAND_ID_MASK;
  1922. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  1923. pip2_cmd.data = data;
  1924. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  1925. /* Add the command length to the extra bytes based on PIP version */
  1926. write_len += pip2_cmd.len;
  1927. pt_debug(dev, DL_INFO, "%s Length Field: %d, Write Len: %d",
  1928. __func__, pip2_cmd.len, write_len);
  1929. write_buf = kzalloc(write_len, GFP_KERNEL);
  1930. if (write_buf == NULL) {
  1931. rc = -ENOMEM;
  1932. goto exit;
  1933. }
  1934. write_buf[i++] = pip2_cmd.reg[0];
  1935. write_buf[i++] = pip2_cmd.reg[1];
  1936. write_buf[i++] = pip2_cmd.len & 0xff;
  1937. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  1938. write_buf[i++] = pip2_cmd.seq;
  1939. write_buf[i++] = pip2_cmd.id;
  1940. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  1941. write_buf[j] = pip2_cmd.data[j-i];
  1942. write_buf[j++] = pip2_cmd.crc[0];
  1943. write_buf[j++] = pip2_cmd.crc[1];
  1944. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  1945. if (read_len < 0)
  1946. read_len = 255;
  1947. pt_debug(dev, DL_INFO,
  1948. "%s cmd_id[0x%02X] expected response length:%d ",
  1949. __func__, pip2_cmd.id, read_len);
  1950. /*
  1951. * All PIP2 commands come through this function.
  1952. * Set flag for PIP2.x interface to allow response parsing to know
  1953. * how to decode the protocol header.
  1954. */
  1955. mutex_lock(&cd->system_lock);
  1956. cd->pip2_prot_active = true;
  1957. cd->pip2_send_user_cmd = true;
  1958. mutex_unlock(&cd->system_lock);
  1959. if (protect == PT_CORE_CMD_PROTECTED)
  1960. rc = pt_hid_output_user_cmd(cd, read_len, read_buf,
  1961. write_len, write_buf, actual_read_len);
  1962. else {
  1963. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1964. write_len, write_buf, actual_read_len);
  1965. }
  1966. if (rc) {
  1967. pt_debug(dev, DL_ERROR,
  1968. "%s: nonhid_cmd->user_cmd() Error = %d\n",
  1969. __func__, rc);
  1970. goto exit;
  1971. }
  1972. rc = pt_pip2_validate_response(cd, &pip2_cmd, read_buf,
  1973. *actual_read_len);
  1974. exit:
  1975. mutex_lock(&cd->system_lock);
  1976. cd->pip2_prot_active = false;
  1977. cd->pip2_send_user_cmd = false;
  1978. mutex_unlock(&cd->system_lock);
  1979. kfree(write_buf);
  1980. return rc;
  1981. }
  1982. /*******************************************************************************
  1983. * FUNCTION: _pt_pip2_send_cmd_no_int
  1984. *
  1985. * SUMMARY: Writes a PIP2 command packet to DUT, then poll the response and
  1986. * reads response data to read_buf if response is available.
  1987. *
  1988. * NOTE:
  1989. * Interrupt MUST be disabled before to call this function.
  1990. *
  1991. * RETURN:
  1992. * 0 = success
  1993. * !0 = failure
  1994. *
  1995. * PARAMETERS:
  1996. * *dev - pointer to device structure
  1997. * protect - flag to run in protected mode
  1998. * id - ID of PIP command
  1999. * *data - pointer to PIP data payload
  2000. * report_body_len - report length
  2001. * *read_buf - pointer to response buffer
  2002. * *actual_read_len - pointer to response buffer length
  2003. ******************************************************************************/
  2004. static int _pt_pip2_send_cmd_no_int(struct device *dev,
  2005. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  2006. u16 *actual_read_len)
  2007. {
  2008. int max_retry = 0;
  2009. int retry = 0;
  2010. int rc = 0;
  2011. int i = 0;
  2012. int j = 0;
  2013. u16 write_len;
  2014. u8 *write_buf = NULL;
  2015. u16 read_len;
  2016. u16 size = 0;
  2017. u8 response_seq = 0;
  2018. u8 extra_bytes;
  2019. u32 retry_interval = 0;
  2020. u32 retry_total_time = 0;
  2021. u32 resp_time_min = pt_pip2_get_cmd_resp_time_min(id);
  2022. u32 resp_time_max = pt_pip2_get_cmd_resp_time_max(id);
  2023. struct pt_core_data *cd = dev_get_drvdata(dev);
  2024. struct pip2_cmd_structure pip2_cmd;
  2025. if (protect == PT_CORE_CMD_PROTECTED) {
  2026. rc = request_exclusive(cd,
  2027. cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2028. if (rc < 0) {
  2029. pt_debug(cd->dev, DL_ERROR,
  2030. "%s: fail get exclusive ex=%p own=%p\n",
  2031. __func__, cd->exclusive_dev, cd->dev);
  2032. return rc;
  2033. }
  2034. }
  2035. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  2036. /* Hard coded register for PIP2.x */
  2037. pip2_cmd.reg[0] = 0x01;
  2038. pip2_cmd.reg[1] = 0x01;
  2039. /*
  2040. * For PIP2.1+ the length field value includes itself:
  2041. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  2042. *
  2043. * The overall write length must include only the register:
  2044. * ADD 2: 2 (Register)
  2045. */
  2046. extra_bytes = 6;
  2047. write_len = 2;
  2048. pip2_cmd.len = report_body_len + extra_bytes;
  2049. pip2_cmd.id = id;
  2050. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  2051. pip2_cmd.data = data;
  2052. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  2053. /* Add the command length to the extra bytes based on PIP version */
  2054. write_len += pip2_cmd.len;
  2055. write_buf = kzalloc(write_len, GFP_KERNEL);
  2056. if (write_buf == NULL) {
  2057. rc = -ENOMEM;
  2058. goto exit;
  2059. }
  2060. write_buf[i++] = pip2_cmd.reg[0];
  2061. write_buf[i++] = pip2_cmd.reg[1];
  2062. write_buf[i++] = pip2_cmd.len & 0xff;
  2063. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  2064. write_buf[i++] = pip2_cmd.seq;
  2065. write_buf[i++] = pip2_cmd.id;
  2066. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  2067. write_buf[j] = pip2_cmd.data[j-i];
  2068. write_buf[j++] = pip2_cmd.crc[0];
  2069. write_buf[j++] = pip2_cmd.crc[1];
  2070. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  2071. if (read_len < 0)
  2072. read_len = 255;
  2073. pt_debug(dev, DL_INFO,
  2074. "%s: ATM - cmd_id[0x%02X] expected response length:%d ",
  2075. __func__, pip2_cmd.id, read_len);
  2076. pt_pr_buf(cd->dev, DL_DEBUG, write_buf, write_len, ">>> NO_INT CMD");
  2077. rc = pt_adap_write_read_specific(cd, write_len, write_buf, NULL);
  2078. if (rc) {
  2079. pt_debug(dev, DL_ERROR,
  2080. "%s: SPI write Error = %d\n",
  2081. __func__, rc);
  2082. goto exit;
  2083. }
  2084. #ifdef PT_POLL_RESP_BY_BUS
  2085. /*
  2086. * Frequent bus read can increase system load obviously. The expected
  2087. * first bus read should be valid and timely. The tollerance for
  2088. * usleep_range should be limited. The minimum response delay (between
  2089. * command finishes sending and INT pin falls) is less than 50
  2090. * microseconds. So the 10 microseconds should be maximum tollerance
  2091. * with the consideration that the unit to calculate the response delay
  2092. * is 10 microseconds and more precise is not necessary. Every
  2093. * additional 10 microseconds only contribute less than 3 milliseconds
  2094. * for whole BL.
  2095. */
  2096. usleep_range(resp_time_min, resp_time_min+10);
  2097. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2098. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2099. rc = pt_adap_read_default(cd, read_buf, read_len);
  2100. if (rc) {
  2101. pt_debug(dev, DL_ERROR, "%s: SPI read Error = %d\n",
  2102. __func__, rc);
  2103. break;
  2104. }
  2105. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET];
  2106. size = get_unaligned_le16(&read_buf[0]);
  2107. if ((size <= read_len) &&
  2108. (size >= PIP2_EXTRA_BYTES_NUM) &&
  2109. (pip2_cmd.seq & 0x07) == (response_seq & 0x07)) {
  2110. break;
  2111. }
  2112. /*
  2113. * To reduce the bus and system load, increase the sleep
  2114. * step gradually:
  2115. * 1 ~ 19 : step=50 us, sleep_us=[50, 100, 150, 200, ..950]
  2116. * 20 ~ 39 : step=1000 us, sleep_us=[1950, 2950, ...20950]
  2117. * 40 ~ MAX: step=50 ms, sleep_ms=[71, 121, 191,..]
  2118. */
  2119. retry++;
  2120. if (retry < 20) {
  2121. retry_interval += POLL_RETRY_DEFAULT_INTERVAL;
  2122. usleep_range(retry_interval,
  2123. retry_interval + POLL_RETRY_DEFAULT_INTERVAL);
  2124. } else if (retry < 40) {
  2125. retry_interval += 1000;
  2126. usleep_range(retry_interval, retry_interval + 1000);
  2127. } else {
  2128. retry_interval += 50000;
  2129. msleep(retry_interval/1000);
  2130. }
  2131. retry_total_time += retry_interval;
  2132. }
  2133. #else
  2134. /*
  2135. * Frequent GPIO read will not increase CPU/system load heavily if the
  2136. * interval is longer than 10 us, so it is safe to poll GPIO with a
  2137. * fixed interval: 20 us.
  2138. */
  2139. usleep_range(resp_time_min, resp_time_min+10);
  2140. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2141. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2142. if (!gpio_get_value(cd->cpdata->irq_gpio)) {
  2143. rc = pt_adap_read_default(cd, read_buf, read_len);
  2144. size = get_unaligned_le16(&read_buf[0]);
  2145. if (rc)
  2146. pt_debug(dev, DL_ERROR,
  2147. "%s: SPI read Error = %d\n",
  2148. __func__, rc);
  2149. else if (size > read_len) {
  2150. pt_debug(cd->dev, DL_ERROR,
  2151. "%s: PIP2 len field=%d, requested read_len=%d\n",
  2152. __func__, size, read_len);
  2153. rc = -EIO;
  2154. }
  2155. break;
  2156. }
  2157. /*
  2158. * Poll GPIO with fixed interval 20 us, and tollerance is
  2159. * limited to 10 us to speed up the process.
  2160. */
  2161. retry_interval = POLL_RETRY_DEFAULT_INTERVAL;
  2162. usleep_range(retry_interval, retry_interval+10);
  2163. retry_total_time += retry_interval;
  2164. }
  2165. #endif
  2166. *actual_read_len = size;
  2167. if (rc || (retry >= max_retry) || (retry_total_time >= resp_time_max)) {
  2168. pt_debug(dev, DL_ERROR,
  2169. "%s cmd[0x%02X] timed out, send_seq=0x%02X, resp_seq=0x%02X\n",
  2170. __func__, pip2_cmd.id, pip2_cmd.seq, response_seq);
  2171. *actual_read_len = 0;
  2172. rc = -EINVAL;
  2173. }
  2174. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, *actual_read_len,
  2175. "<<< NO_INT Read");
  2176. exit:
  2177. kfree(write_buf);
  2178. if (protect == PT_CORE_CMD_PROTECTED) {
  2179. if (release_exclusive(cd, cd->dev) < 0)
  2180. pt_debug(cd->dev, DL_ERROR,
  2181. "%s: fail to release exclusive\n", __func__);
  2182. }
  2183. return rc;
  2184. }
  2185. /*******************************************************************************
  2186. * FUNCTION: pt_pip_null_
  2187. *
  2188. * SUMMARY: Send the PIP "ping"(0x00) command to the DUT and wait for response.
  2189. * This function is used by watchdog to check if the fw corrupts.
  2190. *
  2191. * RETURN:
  2192. * 0 = success
  2193. * !0 = failure
  2194. *
  2195. * PARAMETERS:
  2196. * *cd - pointer to core data
  2197. ******************************************************************************/
  2198. static int pt_pip_null_(struct pt_core_data *cd)
  2199. {
  2200. struct pt_hid_output hid_output = {
  2201. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_NULL),
  2202. };
  2203. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  2204. }
  2205. /*******************************************************************************
  2206. * FUNCTION: pt_pip_null
  2207. *
  2208. * SUMMARY: Wrapper function for pt_pip_null_ that guarantees exclusive access.
  2209. *
  2210. * RETURN:
  2211. * 0 = success
  2212. * !0 = failure
  2213. *
  2214. * PARAMETERS:
  2215. * *cd - pointer to core data
  2216. ******************************************************************************/
  2217. static int pt_pip_null(struct pt_core_data *cd)
  2218. {
  2219. int rc;
  2220. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2221. if (rc < 0) {
  2222. pt_debug(cd->dev, DL_ERROR,
  2223. "%s: fail get exclusive ex=%p own=%p\n",
  2224. __func__, cd->exclusive_dev, cd->dev);
  2225. return rc;
  2226. }
  2227. rc = pt_pip_null_(cd);
  2228. if (release_exclusive(cd, cd->dev) < 0)
  2229. pt_debug(cd->dev, DL_ERROR,
  2230. "%s: fail to release exclusive\n", __func__);
  2231. return rc;
  2232. }
  2233. static void pt_stop_wd_timer(struct pt_core_data *cd);
  2234. /*******************************************************************************
  2235. * FUNCTION: pt_pip_start_bootloader_
  2236. *
  2237. * SUMMARY: Sends the HID command start_bootloader [PIP cmd 0x01] to the DUT
  2238. *
  2239. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  2240. * the WD active could cause this function to fail!
  2241. *
  2242. * RETURN:
  2243. * 0 = success
  2244. * !0 = failure
  2245. *
  2246. * PARAMETERS:
  2247. * *cd - pointer to core data
  2248. ******************************************************************************/
  2249. static int pt_pip_start_bootloader_(struct pt_core_data *cd)
  2250. {
  2251. int rc = 0;
  2252. struct pt_hid_output hid_output = {
  2253. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_START_BOOTLOADER),
  2254. .timeout_ms = PT_PIP1_START_BOOTLOADER_TIMEOUT,
  2255. .reset_expected = 1,
  2256. };
  2257. if (cd->watchdog_enabled) {
  2258. pt_debug(cd->dev, DL_WARN,
  2259. "%s: watchdog isn't stopped before enter bl\n",
  2260. __func__);
  2261. goto exit;
  2262. }
  2263. /* Reset startup status after entering BL, new DUT enum required */
  2264. cd->startup_status = STARTUP_STATUS_START;
  2265. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  2266. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2267. if (rc) {
  2268. pt_debug(cd->dev, DL_ERROR,
  2269. "%s: Start BL PIP cmd failed. rc = %d\n",
  2270. __func__, rc);
  2271. }
  2272. exit:
  2273. return rc;
  2274. }
  2275. /*******************************************************************************
  2276. * FUNCTION: pt_pip_start_bootloader
  2277. *
  2278. * SUMMARY: Protected function to force DUT to enter the BL
  2279. *
  2280. * RETURN:
  2281. * 0 = success
  2282. * !0 = failure
  2283. *
  2284. * PARAMETERS:
  2285. * *cd - pointer to core data structure
  2286. ******************************************************************************/
  2287. static int pt_pip_start_bootloader(struct pt_core_data *cd)
  2288. {
  2289. int rc;
  2290. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2291. if (rc < 0) {
  2292. pt_debug(cd->dev, DL_ERROR,
  2293. "%s: fail get exclusive ex=%p own=%p\n",
  2294. __func__, cd->exclusive_dev, cd->dev);
  2295. return rc;
  2296. }
  2297. rc = pt_pip_start_bootloader_(cd);
  2298. if (release_exclusive(cd, cd->dev) < 0)
  2299. pt_debug(cd->dev, DL_ERROR,
  2300. "%s: fail to release exclusive\n", __func__);
  2301. return rc;
  2302. }
  2303. /*******************************************************************************
  2304. * FUNCTION: _pt_request_pip_start_bl
  2305. *
  2306. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other
  2307. * modules to request the DUT to enter the BL
  2308. *
  2309. * RETURN:
  2310. * 0 = success
  2311. * !0 = failure
  2312. *
  2313. * PARAMETERS:
  2314. * *dev - pointer to device structure
  2315. * protect - flag to run in protected mode
  2316. ******************************************************************************/
  2317. static int _pt_request_pip_start_bl(struct device *dev, int protect)
  2318. {
  2319. struct pt_core_data *cd = dev_get_drvdata(dev);
  2320. if (protect)
  2321. return pt_pip_start_bootloader(cd);
  2322. return pt_pip_start_bootloader_(cd);
  2323. }
  2324. /*******************************************************************************
  2325. * FUNCTION: pt_pip2_ver_load_ttdata
  2326. *
  2327. * SUMMARY: Function to load the Version information from the PIP2 VERSION
  2328. * command into the core data struct.
  2329. *
  2330. * RETURN: n/a
  2331. *
  2332. * PARAMETERS:
  2333. * *cd - pointer to core data structure
  2334. * len - Length of data in response_buf
  2335. ******************************************************************************/
  2336. static void pt_pip2_ver_load_ttdata(struct pt_core_data *cd, u16 len)
  2337. {
  2338. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2339. struct pt_pip2_version_full *full_ver;
  2340. struct pt_pip2_version *ver;
  2341. /*
  2342. * The PIP2 VERSION command can return different lengths of data.
  2343. * The additional LOT fields are included when the packet
  2344. * size is >= 29 bytes. Older FW sends a reduced packet size.
  2345. * NOTE:
  2346. * - The FW would swap the BL and FW versions when reporting
  2347. * the small packet.
  2348. * - Sub Lot bytes 16 and 17 are reserved.
  2349. */
  2350. if (len >= 0x1D) {
  2351. full_ver = (struct pt_pip2_version_full *)
  2352. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2353. ttdata->pip_ver_major = full_ver->pip2_version_msb;
  2354. ttdata->pip_ver_minor = full_ver->pip2_version_lsb;
  2355. ttdata->bl_ver_major = full_ver->bl_version_msb;
  2356. ttdata->bl_ver_minor = full_ver->bl_version_lsb;
  2357. ttdata->fw_ver_major = full_ver->fw_version_msb;
  2358. ttdata->fw_ver_minor = full_ver->fw_version_lsb;
  2359. /*
  2360. * BL PIP 2.02 and greater the version fields are
  2361. * swapped
  2362. */
  2363. if (ttdata->pip_ver_major >= 2 && ttdata->pip_ver_minor >= 2) {
  2364. ttdata->chip_rev =
  2365. get_unaligned_le16(&full_ver->chip_rev);
  2366. ttdata->chip_id =
  2367. get_unaligned_le16(&full_ver->chip_id);
  2368. } else {
  2369. ttdata->chip_rev =
  2370. get_unaligned_le16(&full_ver->chip_id);
  2371. ttdata->chip_id =
  2372. get_unaligned_le16(&full_ver->chip_rev);
  2373. }
  2374. memcpy(ttdata->uid, full_ver->uid, PT_UID_SIZE);
  2375. pt_pr_buf(cd->dev, DL_INFO, (u8 *)full_ver,
  2376. sizeof(struct pt_pip2_version_full),
  2377. "PIP2 VERSION FULL");
  2378. } else {
  2379. ver = (struct pt_pip2_version *)
  2380. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2381. ttdata->pip_ver_major = ver->pip2_version_msb;
  2382. ttdata->pip_ver_minor = ver->pip2_version_lsb;
  2383. ttdata->bl_ver_major = ver->bl_version_msb;
  2384. ttdata->bl_ver_minor = ver->bl_version_lsb;
  2385. ttdata->fw_ver_major = ver->fw_version_msb;
  2386. ttdata->fw_ver_minor = ver->fw_version_lsb;
  2387. ttdata->chip_rev = get_unaligned_le16(&ver->chip_rev);
  2388. ttdata->chip_id = get_unaligned_le16(&ver->chip_id);
  2389. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ver,
  2390. sizeof(struct pt_pip2_version), "PIP2 VERSION");
  2391. }
  2392. }
  2393. /*******************************************************************************
  2394. * FUNCTION: pt_si_get_ttdata
  2395. *
  2396. * SUMMARY: Function to load the version information from the system information
  2397. * PIP command into the core data struct.
  2398. *
  2399. * RETURN: n/a
  2400. *
  2401. * PARAMETERS:
  2402. * *cd - pointer to core data structure
  2403. ******************************************************************************/
  2404. static void pt_si_get_ttdata(struct pt_core_data *cd)
  2405. {
  2406. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2407. struct pt_ttdata_dev *ttdata_dev =
  2408. (struct pt_ttdata_dev *)
  2409. &cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  2410. ttdata->pip_ver_major = ttdata_dev->pip_ver_major;
  2411. ttdata->pip_ver_minor = ttdata_dev->pip_ver_minor;
  2412. ttdata->bl_ver_major = ttdata_dev->bl_ver_major;
  2413. ttdata->bl_ver_minor = ttdata_dev->bl_ver_minor;
  2414. ttdata->fw_ver_major = ttdata_dev->fw_ver_major;
  2415. ttdata->fw_ver_minor = ttdata_dev->fw_ver_minor;
  2416. ttdata->fw_pid = get_unaligned_le16(&ttdata_dev->fw_pid);
  2417. ttdata->fw_ver_conf = get_unaligned_le16(&ttdata_dev->fw_ver_conf);
  2418. ttdata->post_code = get_unaligned_le16(&ttdata_dev->post_code);
  2419. ttdata->revctrl = get_unaligned_le32(&ttdata_dev->revctrl);
  2420. ttdata->jtag_id_l = get_unaligned_le16(&ttdata_dev->jtag_si_id_l);
  2421. ttdata->jtag_id_h = get_unaligned_le16(&ttdata_dev->jtag_si_id_h);
  2422. memcpy(ttdata->mfg_id, ttdata_dev->mfg_id, PT_NUM_MFGID);
  2423. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ttdata_dev,
  2424. sizeof(struct pt_ttdata_dev), "sysinfo_ttdata");
  2425. }
  2426. /*******************************************************************************
  2427. * FUNCTION: pt_si_get_sensing_conf_data
  2428. *
  2429. * SUMMARY: Function to load the sensing information from the system information
  2430. * PIP command into the core data struct.
  2431. *
  2432. * RETURN: n/a
  2433. *
  2434. * PARAMETERS:
  2435. * *cd - pointer to core data structure
  2436. ******************************************************************************/
  2437. static void pt_si_get_sensing_conf_data(struct pt_core_data *cd)
  2438. {
  2439. struct pt_sensing_conf_data *scd = &cd->sysinfo.sensing_conf_data;
  2440. struct pt_sensing_conf_data_dev *scd_dev =
  2441. (struct pt_sensing_conf_data_dev *)
  2442. &cd->response_buf[PIP1_SYSINFO_SENSING_OFFSET];
  2443. scd->electrodes_x = scd_dev->electrodes_x;
  2444. scd->electrodes_y = scd_dev->electrodes_y;
  2445. scd->origin_x = scd_dev->origin_x;
  2446. scd->origin_y = scd_dev->origin_y;
  2447. /* PIP 1.4 (001-82649 *Q) add X_IS_TX bit in X_ORG */
  2448. if (scd->origin_x & 0x02) {
  2449. scd->tx_num = scd->electrodes_x;
  2450. scd->rx_num = scd->electrodes_y;
  2451. } else {
  2452. scd->tx_num = scd->electrodes_y;
  2453. scd->rx_num = scd->electrodes_x;
  2454. }
  2455. /*
  2456. * When the Panel ID is coming from an XY pin and not a dedicated
  2457. * GPIO, store the PID in pid_for_loader. This cannot be done for all
  2458. * other DUTs as the loader will use cd->pid_for_loader to generate
  2459. * the bin file name but will ignore it if pid_for_loader is still
  2460. * set to PANEL_ID_NOT_ENABLED
  2461. */
  2462. if (cd->panel_id_support &
  2463. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  2464. mutex_lock(&cd->system_lock);
  2465. cd->pid_for_loader = scd_dev->panel_id;
  2466. mutex_unlock(&cd->system_lock);
  2467. }
  2468. scd->panel_id = scd_dev->panel_id;
  2469. scd->btn = scd_dev->btn;
  2470. scd->scan_mode = scd_dev->scan_mode;
  2471. scd->max_tch = scd_dev->max_num_of_tch_per_refresh_cycle;
  2472. scd->res_x = get_unaligned_le16(&scd_dev->res_x);
  2473. scd->res_y = get_unaligned_le16(&scd_dev->res_y);
  2474. scd->max_z = get_unaligned_le16(&scd_dev->max_z);
  2475. scd->len_x = get_unaligned_le16(&scd_dev->len_x);
  2476. scd->len_y = get_unaligned_le16(&scd_dev->len_y);
  2477. pt_pr_buf(cd->dev, DL_INFO, (u8 *)scd_dev,
  2478. sizeof(struct pt_sensing_conf_data_dev),
  2479. "sensing_conf_data");
  2480. }
  2481. /*******************************************************************************
  2482. * FUNCTION: pt_si_setup
  2483. *
  2484. * SUMMARY: Setup the xy_data and xy_mode by allocating the needed memory
  2485. *
  2486. * RETURN:
  2487. * 0 = success
  2488. * !0 = failure
  2489. *
  2490. * PARAMETERS:
  2491. * *cd - pointer to core data structure
  2492. ******************************************************************************/
  2493. static int pt_si_setup(struct pt_core_data *cd)
  2494. {
  2495. struct pt_sysinfo *si = &cd->sysinfo;
  2496. int max_tch = si->sensing_conf_data.max_tch;
  2497. if (!si->xy_data)
  2498. si->xy_data = kzalloc(max_tch * si->desc.tch_record_size,
  2499. GFP_KERNEL);
  2500. if (!si->xy_data)
  2501. return -ENOMEM;
  2502. if (!si->xy_mode)
  2503. si->xy_mode = kzalloc(si->desc.tch_header_size, GFP_KERNEL);
  2504. if (!si->xy_mode) {
  2505. kfree(si->xy_data);
  2506. return -ENOMEM;
  2507. }
  2508. return 0;
  2509. }
  2510. /*******************************************************************************
  2511. * FUNCTION: pt_si_get_btn_data
  2512. *
  2513. * SUMMARY: Setup the core data button information based on the response of the
  2514. * System Information PIP command.
  2515. *
  2516. * RETURN:
  2517. * 0 = success
  2518. * !0 = failure
  2519. *
  2520. * PARAMETERS:
  2521. * *cd - pointer to core data structure
  2522. ******************************************************************************/
  2523. static int pt_si_get_btn_data(struct pt_core_data *cd)
  2524. {
  2525. struct pt_sysinfo *si = &cd->sysinfo;
  2526. int num_btns = 0;
  2527. int num_defined_keys;
  2528. u16 *key_table;
  2529. int btn;
  2530. int i;
  2531. int rc = 0;
  2532. unsigned int btns = cd->response_buf[PIP1_SYSINFO_BTN_OFFSET]
  2533. & PIP1_SYSINFO_BTN_MASK;
  2534. size_t btn_keys_size;
  2535. pt_debug(cd->dev, DL_INFO, "%s: get btn data\n", __func__);
  2536. for (i = 0; i < PIP1_SYSINFO_MAX_BTN; i++) {
  2537. if (btns & (1 << i))
  2538. num_btns++;
  2539. }
  2540. si->num_btns = num_btns;
  2541. if (num_btns) {
  2542. btn_keys_size = num_btns * sizeof(struct pt_btn);
  2543. if (!si->btn)
  2544. si->btn = kzalloc(btn_keys_size, GFP_KERNEL);
  2545. if (!si->btn)
  2546. return -ENOMEM;
  2547. if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS] == NULL)
  2548. num_defined_keys = 0;
  2549. else if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS]->data == NULL)
  2550. num_defined_keys = 0;
  2551. else
  2552. num_defined_keys = cd->cpdata->sett
  2553. [PT_IC_GRPNUM_BTN_KEYS]->size;
  2554. for (btn = 0; btn < num_btns && btn < num_defined_keys; btn++) {
  2555. key_table = (u16 *)cd->cpdata->sett
  2556. [PT_IC_GRPNUM_BTN_KEYS]->data;
  2557. si->btn[btn].key_code = key_table[btn];
  2558. si->btn[btn].enabled = true;
  2559. }
  2560. for (; btn < num_btns; btn++) {
  2561. si->btn[btn].key_code = KEY_RESERVED;
  2562. si->btn[btn].enabled = true;
  2563. }
  2564. return rc;
  2565. }
  2566. kfree(si->btn);
  2567. si->btn = NULL;
  2568. return rc;
  2569. }
  2570. /*******************************************************************************
  2571. * FUNCTION: pt_si_put_log_data
  2572. *
  2573. * SUMMARY: Prints all sys info data to kmsg log
  2574. *
  2575. * RETURN: n/a
  2576. *
  2577. * PARAMETERS:
  2578. * *cd - pointer to core data structure
  2579. ******************************************************************************/
  2580. static void pt_si_put_log_data(struct pt_core_data *cd)
  2581. {
  2582. struct pt_sysinfo *si = &cd->sysinfo;
  2583. struct pt_ttdata *ttdata = &si->ttdata;
  2584. struct pt_sensing_conf_data *scd = &si->sensing_conf_data;
  2585. int i;
  2586. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_major = 0x%02X (%d)\n",
  2587. __func__, ttdata->pip_ver_major, ttdata->pip_ver_major);
  2588. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_minor = 0x%02X (%d)\n",
  2589. __func__, ttdata->pip_ver_minor, ttdata->pip_ver_minor);
  2590. pt_debug(cd->dev, DL_DEBUG, "%s: fw_pid = 0x%04X (%d)\n",
  2591. __func__, ttdata->fw_pid, ttdata->fw_pid);
  2592. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_major = 0x%02X (%d)\n",
  2593. __func__, ttdata->fw_ver_major, ttdata->fw_ver_major);
  2594. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_minor = 0x%02X (%d)\n",
  2595. __func__, ttdata->fw_ver_minor, ttdata->fw_ver_minor);
  2596. pt_debug(cd->dev, DL_DEBUG, "%s: revctrl = 0x%08X (%d)\n",
  2597. __func__, ttdata->revctrl, ttdata->revctrl);
  2598. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_conf = 0x%04X (%d)\n",
  2599. __func__, ttdata->fw_ver_conf, ttdata->fw_ver_conf);
  2600. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_major = 0x%02X (%d)\n",
  2601. __func__, ttdata->bl_ver_major, ttdata->bl_ver_major);
  2602. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_minor = 0x%02X (%d)\n",
  2603. __func__, ttdata->bl_ver_minor, ttdata->bl_ver_minor);
  2604. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_h = 0x%04X (%d)\n",
  2605. __func__, ttdata->jtag_id_h, ttdata->jtag_id_h);
  2606. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_l = 0x%04X (%d)\n",
  2607. __func__, ttdata->jtag_id_l, ttdata->jtag_id_l);
  2608. for (i = 0; i < PT_NUM_MFGID; i++)
  2609. pt_debug(cd->dev, DL_DEBUG,
  2610. "%s: mfg_id[%d] = 0x%02X (%d)\n",
  2611. __func__, i, ttdata->mfg_id[i],
  2612. ttdata->mfg_id[i]);
  2613. pt_debug(cd->dev, DL_DEBUG, "%s: post_code = 0x%04X (%d)\n",
  2614. __func__, ttdata->post_code, ttdata->post_code);
  2615. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_x = 0x%02X (%d)\n",
  2616. __func__, scd->electrodes_x, scd->electrodes_x);
  2617. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_y = 0x%02X (%d)\n",
  2618. __func__, scd->electrodes_y, scd->electrodes_y);
  2619. pt_debug(cd->dev, DL_DEBUG, "%s: len_x = 0x%04X (%d)\n",
  2620. __func__, scd->len_x, scd->len_x);
  2621. pt_debug(cd->dev, DL_DEBUG, "%s: len_y = 0x%04X (%d)\n",
  2622. __func__, scd->len_y, scd->len_y);
  2623. pt_debug(cd->dev, DL_DEBUG, "%s: res_x = 0x%04X (%d)\n",
  2624. __func__, scd->res_x, scd->res_x);
  2625. pt_debug(cd->dev, DL_DEBUG, "%s: res_y = 0x%04X (%d)\n",
  2626. __func__, scd->res_y, scd->res_y);
  2627. pt_debug(cd->dev, DL_DEBUG, "%s: max_z = 0x%04X (%d)\n",
  2628. __func__, scd->max_z, scd->max_z);
  2629. pt_debug(cd->dev, DL_DEBUG, "%s: origin_x = 0x%02X (%d)\n",
  2630. __func__, scd->origin_x, scd->origin_x);
  2631. pt_debug(cd->dev, DL_DEBUG, "%s: origin_y = 0x%02X (%d)\n",
  2632. __func__, scd->origin_y, scd->origin_y);
  2633. pt_debug(cd->dev, DL_DEBUG, "%s: panel_id = 0x%02X (%d)\n",
  2634. __func__, scd->panel_id, scd->panel_id);
  2635. pt_debug(cd->dev, DL_DEBUG, "%s: btn =0x%02X (%d)\n",
  2636. __func__, scd->btn, scd->btn);
  2637. pt_debug(cd->dev, DL_DEBUG, "%s: scan_mode = 0x%02X (%d)\n",
  2638. __func__, scd->scan_mode, scd->scan_mode);
  2639. pt_debug(cd->dev, DL_DEBUG,
  2640. "%s: max_num_of_tch_per_refresh_cycle = 0x%02X (%d)\n",
  2641. __func__, scd->max_tch, scd->max_tch);
  2642. pt_debug(cd->dev, DL_DEBUG, "%s: xy_mode = %p\n",
  2643. __func__, si->xy_mode);
  2644. pt_debug(cd->dev, DL_DEBUG, "%s: xy_data = %p\n",
  2645. __func__, si->xy_data);
  2646. }
  2647. /*******************************************************************************
  2648. * FUNCTION: pt_get_sysinfo_regs
  2649. *
  2650. * SUMMARY: Setup all the core data System information based on the response
  2651. * of the System Information PIP command.
  2652. *
  2653. * RETURN:
  2654. * 0 = success
  2655. * !0 = failure
  2656. *
  2657. * PARAMETERS:
  2658. * *cd - pointer to core data structure
  2659. ******************************************************************************/
  2660. static int pt_get_sysinfo_regs(struct pt_core_data *cd)
  2661. {
  2662. struct pt_sysinfo *si = &cd->sysinfo;
  2663. int rc;
  2664. rc = pt_si_get_btn_data(cd);
  2665. if (rc < 0)
  2666. return rc;
  2667. pt_si_get_ttdata(cd);
  2668. pt_si_get_sensing_conf_data(cd);
  2669. pt_si_setup(cd);
  2670. pt_si_put_log_data(cd);
  2671. si->ready = true;
  2672. return rc;
  2673. }
  2674. /*******************************************************************************
  2675. * FUNCTION: pt_free_si_ptrs
  2676. *
  2677. * SUMMARY: Frees all memory associated with the System Information within
  2678. * core data
  2679. *
  2680. * RETURN: n/a
  2681. *
  2682. * PARAMETERS:
  2683. * *cd - pointer to core data structure
  2684. ******************************************************************************/
  2685. static void pt_free_si_ptrs(struct pt_core_data *cd)
  2686. {
  2687. struct pt_sysinfo *si = &cd->sysinfo;
  2688. kfree(si->btn);
  2689. kfree(si->xy_mode);
  2690. kfree(si->xy_data);
  2691. }
  2692. /*******************************************************************************
  2693. * FUNCTION: pt_hid_output_get_sysinfo_
  2694. *
  2695. * SUMMARY: Sends the PIP Get SYS INFO command to the DUT and waits for the
  2696. * response.
  2697. *
  2698. * RETURN::
  2699. * 0 = success
  2700. * !0 = failure
  2701. *
  2702. * PARAMETERS:
  2703. * *cd - pointer to core data structure
  2704. ******************************************************************************/
  2705. static int pt_hid_output_get_sysinfo_(struct pt_core_data *cd)
  2706. {
  2707. int rc = 0;
  2708. struct pt_hid_output hid_output = {
  2709. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  2710. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  2711. };
  2712. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2713. if (rc)
  2714. return rc;
  2715. /* Parse the sysinfo data */
  2716. rc = pt_get_sysinfo_regs(cd);
  2717. if (rc)
  2718. pt_free_si_ptrs(cd);
  2719. return rc;
  2720. }
  2721. /*******************************************************************************
  2722. * FUNCTION: pt_hid_output_get_sysinfo
  2723. *
  2724. * SUMMARY: Protected call to pt_hid_output_get_sysinfo_
  2725. *
  2726. * RETURN::
  2727. * 0 = success
  2728. * !0 = failure
  2729. *
  2730. * PARAMETERS:
  2731. * *cd - pointer to core data structure
  2732. ******************************************************************************/
  2733. static int pt_hid_output_get_sysinfo(struct pt_core_data *cd)
  2734. {
  2735. int rc;
  2736. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2737. if (rc < 0) {
  2738. pt_debug(cd->dev, DL_ERROR,
  2739. "%s: fail get exclusive ex=%p own=%p\n",
  2740. __func__, cd->exclusive_dev, cd->dev);
  2741. return rc;
  2742. }
  2743. rc = pt_hid_output_get_sysinfo_(cd);
  2744. if (release_exclusive(cd, cd->dev) < 0)
  2745. pt_debug(cd->dev, DL_ERROR,
  2746. "%s: fail to release exclusive\n", __func__);
  2747. return rc;
  2748. }
  2749. /*******************************************************************************
  2750. * FUNCTION: pt_pip_suspend_scanning_
  2751. *
  2752. * SUMMARY: Sends the PIP Suspend Scanning command to the DUT
  2753. *
  2754. * RETURN::
  2755. * 0 = success
  2756. * !0 = failure
  2757. *
  2758. * PARAMETERS:
  2759. * *cd - pointer to core data structure
  2760. ******************************************************************************/
  2761. static int pt_pip_suspend_scanning_(struct pt_core_data *cd)
  2762. {
  2763. int rc = 0;
  2764. struct pt_hid_output hid_output = {
  2765. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SUSPEND_SCANNING),
  2766. };
  2767. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2768. if (rc) {
  2769. pt_debug(cd->dev, DL_ERROR,
  2770. "%s: Suspend Scan PIP cmd failed. rc = %d\n",
  2771. __func__, rc);
  2772. }
  2773. return rc;
  2774. }
  2775. /*******************************************************************************
  2776. * FUNCTION: pt_pip_suspend_scanning
  2777. *
  2778. * SUMMARY: Protected wrapper for calling pt_hid_output_suspend_scanning_
  2779. *
  2780. * RETURN::
  2781. * 0 = success
  2782. * !0 = failure
  2783. *
  2784. * PARAMETERS:
  2785. * *cd - pointer to core data structure
  2786. ******************************************************************************/
  2787. static int pt_pip_suspend_scanning(struct pt_core_data *cd)
  2788. {
  2789. int rc;
  2790. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2791. if (rc < 0) {
  2792. pt_debug(cd->dev, DL_ERROR,
  2793. "%s: fail get exclusive ex=%p own=%p\n",
  2794. __func__, cd->exclusive_dev, cd->dev);
  2795. return rc;
  2796. }
  2797. rc = pt_pip_suspend_scanning_(cd);
  2798. if (release_exclusive(cd, cd->dev) < 0)
  2799. pt_debug(cd->dev, DL_ERROR,
  2800. "%s: fail to release exclusive\n", __func__);
  2801. return rc;
  2802. }
  2803. /*******************************************************************************
  2804. * FUNCTION: _pt_request_pip_suspend_scanning
  2805. *
  2806. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2807. * calls to the protected or unprotected call to pt_pip_suspend_scanning
  2808. *
  2809. * RETURN:
  2810. * 0 = success
  2811. * !0 = failure
  2812. *
  2813. * PARAMETERS:
  2814. * *dev - pointer to device structure
  2815. * protect - 0 = call non-protected function
  2816. * 1 = call protected function
  2817. ******************************************************************************/
  2818. static int _pt_request_pip_suspend_scanning(struct device *dev,
  2819. int protect)
  2820. {
  2821. struct pt_core_data *cd = dev_get_drvdata(dev);
  2822. if (protect)
  2823. return pt_pip_suspend_scanning(cd);
  2824. return pt_pip_suspend_scanning_(cd);
  2825. }
  2826. /*******************************************************************************
  2827. * FUNCTION: pt_pip_resume_scanning_
  2828. *
  2829. * SUMMARY: Sends the PIP Resume Scanning command to the DUT
  2830. *
  2831. * RETURN::
  2832. * 0 = success
  2833. * !0 = failure
  2834. *
  2835. * PARAMETERS:
  2836. * *cd - pointer to core data structure
  2837. ******************************************************************************/
  2838. static int pt_pip_resume_scanning_(struct pt_core_data *cd)
  2839. {
  2840. int rc = 0;
  2841. struct pt_hid_output hid_output = {
  2842. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RESUME_SCANNING),
  2843. };
  2844. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2845. if (rc) {
  2846. pt_debug(cd->dev, DL_ERROR,
  2847. "%s: Resume Scan PIP cmd failed. rc = %d\n",
  2848. __func__, rc);
  2849. }
  2850. return rc;
  2851. }
  2852. /*******************************************************************************
  2853. * FUNCTION: pt_pip_resume_scanning
  2854. *
  2855. * SUMMARY: Protected wrapper for calling pt_pip_resume_scanning_
  2856. *
  2857. * RETURN::
  2858. * 0 = success
  2859. * !0 = failure
  2860. *
  2861. * PARAMETERS:
  2862. * *cd - pointer to core data structure
  2863. ******************************************************************************/
  2864. static int pt_pip_resume_scanning(struct pt_core_data *cd)
  2865. {
  2866. int rc;
  2867. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2868. if (rc < 0) {
  2869. pt_debug(cd->dev, DL_ERROR,
  2870. "%s: fail get exclusive ex=%p own=%p\n",
  2871. __func__, cd->exclusive_dev, cd->dev);
  2872. return rc;
  2873. }
  2874. rc = pt_pip_resume_scanning_(cd);
  2875. if (release_exclusive(cd, cd->dev) < 0)
  2876. pt_debug(cd->dev, DL_ERROR,
  2877. "%s: fail to release exclusive\n", __func__);
  2878. return rc;
  2879. }
  2880. /*******************************************************************************
  2881. * FUNCTION: _pt_request_pip_resume_scanning
  2882. *
  2883. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2884. * calls to the protected or unprotected call to pt_pip_resume_scanning
  2885. *
  2886. * RETURN::
  2887. * 0 = success
  2888. * !0 = failure
  2889. *
  2890. * PARAMETERS:
  2891. * *dev - pointer to device structure
  2892. * protect - 0 = call non-protected function
  2893. * 1 = call protected function
  2894. ******************************************************************************/
  2895. static int _pt_request_pip_resume_scanning(struct device *dev,
  2896. int protect)
  2897. {
  2898. struct pt_core_data *cd = dev_get_drvdata(dev);
  2899. if (protect)
  2900. return pt_pip_resume_scanning(cd);
  2901. return pt_pip_resume_scanning_(cd);
  2902. }
  2903. /*******************************************************************************
  2904. * FUNCTION: pt_pip_get_param_
  2905. *
  2906. * SUMMARY: Sends a PIP command 0x05 Get Parameter to the DUT and returns
  2907. * the 32bit parameter value
  2908. *
  2909. * RETURN:
  2910. * 0 = success
  2911. * !0 = failure
  2912. *
  2913. * PARAMETERS:
  2914. * *cd - pointer to core data
  2915. * param_id - parameter ID to retrieve
  2916. * *value - value of DUT parameter
  2917. ******************************************************************************/
  2918. static int pt_pip_get_param_(struct pt_core_data *cd,
  2919. u8 param_id, u32 *value)
  2920. {
  2921. int write_length = 1;
  2922. u8 param[1] = { param_id };
  2923. u8 read_param_id;
  2924. int param_size;
  2925. u8 *ptr;
  2926. int rc = 0;
  2927. int i;
  2928. struct pt_hid_output hid_output = {
  2929. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_PARAM),
  2930. .write_length = write_length,
  2931. .write_buf = param,
  2932. };
  2933. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2934. if (rc)
  2935. return rc;
  2936. read_param_id = cd->response_buf[5];
  2937. if (read_param_id != param_id)
  2938. return -EPROTO;
  2939. param_size = cd->response_buf[6];
  2940. ptr = &cd->response_buf[7];
  2941. *value = 0;
  2942. for (i = 0; i < param_size; i++)
  2943. *value += ptr[i] << (i * 8);
  2944. return 0;
  2945. }
  2946. /*******************************************************************************
  2947. * FUNCTION: pt_pip_get_param
  2948. *
  2949. * SUMMARY: Protected call to pt_hid_output_get_param_ by a request exclusive
  2950. *
  2951. * RETURN:
  2952. * 0 = success
  2953. * !0 = failure
  2954. *
  2955. * PARAMETERS:
  2956. * *cd - pointer to core data
  2957. * param_id - parameter ID to retrieve
  2958. * *value - value of DUT parameter
  2959. ******************************************************************************/
  2960. static int pt_pip_get_param(struct pt_core_data *cd,
  2961. u8 param_id, u32 *value)
  2962. {
  2963. int rc;
  2964. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2965. if (rc < 0) {
  2966. pt_debug(cd->dev, DL_ERROR,
  2967. "%s: fail get exclusive ex=%p own=%p\n",
  2968. __func__, cd->exclusive_dev, cd->dev);
  2969. return rc;
  2970. }
  2971. rc = pt_pip_get_param_(cd, param_id, value);
  2972. if (release_exclusive(cd, cd->dev) < 0)
  2973. pt_debug(cd->dev, DL_ERROR,
  2974. "%s: fail to release exclusive\n", __func__);
  2975. return rc;
  2976. }
  2977. /*******************************************************************************
  2978. * FUNCTION: _pt_request_pip_get_param
  2979. *
  2980. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2981. * calls to the protected or unprotected call to pt_pip_get_param
  2982. *
  2983. * RETURN:
  2984. * 0 = success
  2985. * !0 = failure
  2986. *
  2987. * PARAMETERS:
  2988. * *dev - pointer to device structure
  2989. * protect - flag to call protected or non protected function
  2990. * param_id - parameter ID to retrieve
  2991. * *value - value of DUT parameter
  2992. ******************************************************************************/
  2993. int _pt_request_pip_get_param(struct device *dev,
  2994. int protect, u8 param_id, u32 *value)
  2995. {
  2996. struct pt_core_data *cd = dev_get_drvdata(dev);
  2997. if (protect)
  2998. return pt_pip_get_param(cd, param_id, value);
  2999. return pt_pip_get_param_(cd, param_id, value);
  3000. }
  3001. /*******************************************************************************
  3002. * FUNCTION: pt_pip_set_param_
  3003. *
  3004. * SUMMARY: Sends a PIP command 0x06 Set Parameter to the DUT writing the
  3005. * passed in value to flash
  3006. *
  3007. * RETURN:
  3008. * 0 = success
  3009. * !0 = failure
  3010. *
  3011. * PARAMETERS:
  3012. * *cd - pointer to core data
  3013. * param_id - parameter ID to set
  3014. * value - value to write
  3015. * size - size to write
  3016. ******************************************************************************/
  3017. static int pt_pip_set_param_(struct pt_core_data *cd,
  3018. u8 param_id, u32 value, u8 size)
  3019. {
  3020. u8 write_buf[6];
  3021. u8 *ptr = &write_buf[2];
  3022. int rc = 0;
  3023. int i;
  3024. struct pt_hid_output hid_output = {
  3025. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SET_PARAM),
  3026. .write_buf = write_buf,
  3027. };
  3028. write_buf[0] = param_id;
  3029. write_buf[1] = size;
  3030. for (i = 0; i < size; i++) {
  3031. ptr[i] = value & 0xFF;
  3032. value = value >> 8;
  3033. }
  3034. hid_output.write_length = 2 + size;
  3035. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3036. if (rc)
  3037. return rc;
  3038. if (param_id != cd->response_buf[5] || size != cd->response_buf[6])
  3039. return -EPROTO;
  3040. return 0;
  3041. }
  3042. /*******************************************************************************
  3043. * FUNCTION: pt_pip_set_param
  3044. *
  3045. * SUMMARY: Protected call to pt_hid_output_set_param_ by a request exclusive
  3046. *
  3047. * RETURN:
  3048. * 0 = success
  3049. * !0 = failure
  3050. *
  3051. * PARAMETERS:
  3052. * *cd - pointer to core data
  3053. * param_id - parameter ID to set
  3054. * value - value to write
  3055. * size - size to write
  3056. ******************************************************************************/
  3057. static int pt_pip_set_param(struct pt_core_data *cd,
  3058. u8 param_id, u32 value, u8 size)
  3059. {
  3060. int rc;
  3061. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3062. if (rc < 0) {
  3063. pt_debug(cd->dev, DL_ERROR,
  3064. "%s: fail get exclusive ex=%p own=%p\n",
  3065. __func__, cd->exclusive_dev, cd->dev);
  3066. return rc;
  3067. }
  3068. rc = pt_pip_set_param_(cd, param_id, value, size);
  3069. if (release_exclusive(cd, cd->dev) < 0)
  3070. pt_debug(cd->dev, DL_ERROR,
  3071. "%s: fail to release exclusive\n", __func__);
  3072. return rc;
  3073. }
  3074. /*******************************************************************************
  3075. * FUNCTION: _pt_request_pip_set_param
  3076. *
  3077. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3078. * calls to the protected or unprotected call to pt_pip_set_param
  3079. *
  3080. * RETURN:
  3081. * 0 = success
  3082. * !0 = failure
  3083. *
  3084. * PARAMETERS:
  3085. * *dev - pointer to device structure
  3086. * protect - flag to call protected or non-protected
  3087. * param_id - parameter ID to set
  3088. * value - value to write
  3089. * size - size to write
  3090. ******************************************************************************/
  3091. int _pt_request_pip_set_param(struct device *dev, int protect,
  3092. u8 param_id, u32 value, u8 size)
  3093. {
  3094. struct pt_core_data *cd = dev_get_drvdata(dev);
  3095. if (protect)
  3096. return pt_pip_set_param(cd, param_id, value, size);
  3097. return pt_pip_set_param_(cd, param_id, value, size);
  3098. }
  3099. /*******************************************************************************
  3100. * FUNCTION: _pt_pip_enter_easywake_state_
  3101. *
  3102. * SUMMARY: Sends a PIP command 0x09 Enter EasyWake State to the DUT
  3103. *
  3104. * RETURN:
  3105. * 0 = success
  3106. * !0 = failure
  3107. *
  3108. * PARAMETERS:
  3109. * *dev - pointer to device structure
  3110. * data - easywake guesture (Only used for PIP1.6 and earlier)
  3111. * *return_data - return status if easywake was entered
  3112. ******************************************************************************/
  3113. static int pt_hid_output_enter_easywake_state_(
  3114. struct pt_core_data *cd, u8 data, u8 *return_data)
  3115. {
  3116. int write_length = 1;
  3117. u8 param[1] = { data };
  3118. int rc = 0;
  3119. struct pt_hid_output hid_output = {
  3120. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_ENTER_EASYWAKE_STATE),
  3121. .write_length = write_length,
  3122. .write_buf = param,
  3123. };
  3124. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3125. if (rc)
  3126. return rc;
  3127. *return_data = cd->response_buf[5];
  3128. return rc;
  3129. }
  3130. /*******************************************************************************
  3131. * FUNCTION: pt_pip_verify_config_block_crc_
  3132. *
  3133. * SUMMARY: Sends the PIP "Verify Data Block CRC" (0x20) command to the DUT
  3134. *
  3135. * RETURN:
  3136. * 0 = success
  3137. * !0 = failure
  3138. *
  3139. * PARAMETERS:
  3140. * *cd - pointer the core data structure
  3141. * ebid - enumerated block ID
  3142. * *status - PIP command status
  3143. * calculated_crc - calculated CRC
  3144. * stored_crc - stored CRC in config area
  3145. ******************************************************************************/
  3146. static int pt_pip_verify_config_block_crc_(
  3147. struct pt_core_data *cd, u8 ebid, u8 *status,
  3148. u16 *calculated_crc, u16 *stored_crc)
  3149. {
  3150. int write_length = 1;
  3151. u8 param[1] = { ebid };
  3152. u8 *ptr;
  3153. int rc = 0;
  3154. struct pt_hid_output hid_output = {
  3155. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  3156. .write_length = write_length,
  3157. .write_buf = param,
  3158. };
  3159. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3160. if (rc)
  3161. return rc;
  3162. ptr = &cd->response_buf[5];
  3163. *status = ptr[0];
  3164. *calculated_crc = get_unaligned_le16(&ptr[1]);
  3165. *stored_crc = get_unaligned_le16(&ptr[3]);
  3166. return 0;
  3167. }
  3168. /*******************************************************************************
  3169. * FUNCTION: pt_pip_verify_config_block_crc
  3170. *
  3171. * SUMMARY: Protected call to pt_hid_output_verify_config_block_crc_() within
  3172. * an exclusive access to the DUT.
  3173. *
  3174. * RETURN:
  3175. * 0 = success
  3176. * !0 = failure
  3177. *
  3178. * PARAMETERS:
  3179. * *cd - pointer the core data structure
  3180. * ebid - enumerated block ID
  3181. * *status - PIP command status
  3182. * calculated_crc - calculated CRC
  3183. * stored_crc - stored CRC in config area
  3184. ******************************************************************************/
  3185. static int pt_pip_verify_config_block_crc(
  3186. struct pt_core_data *cd, u8 ebid, u8 *status,
  3187. u16 *calculated_crc, u16 *stored_crc)
  3188. {
  3189. int rc;
  3190. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3191. if (rc < 0) {
  3192. pt_debug(cd->dev, DL_ERROR,
  3193. "%s: fail get exclusive ex=%p own=%p\n",
  3194. __func__, cd->exclusive_dev, cd->dev);
  3195. return rc;
  3196. }
  3197. rc = pt_pip_verify_config_block_crc_(cd, ebid, status,
  3198. calculated_crc, stored_crc);
  3199. if (release_exclusive(cd, cd->dev) < 0)
  3200. pt_debug(cd->dev, DL_ERROR,
  3201. "%s: fail to release exclusive\n", __func__);
  3202. return rc;
  3203. }
  3204. /*******************************************************************************
  3205. * FUNCTION: _pt_request_pip_verify_config_block_crc
  3206. *
  3207. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3208. * calls to the protected or unprotected call to
  3209. * pt_pip_verify_config_block_crc_
  3210. *
  3211. * RETURN:
  3212. * 0 = success
  3213. * !0 = failure
  3214. *
  3215. * PARAMETERS:
  3216. * *dev - pointer to device structure
  3217. * protect - flag to call protected or non-protected
  3218. * ebid - enumerated block ID
  3219. * *status - PIP command status
  3220. * calculated_crc - calculated CRC
  3221. * stored_crc - stored CRC in config area
  3222. ******************************************************************************/
  3223. static int _pt_request_pip_verify_config_block_crc(
  3224. struct device *dev, int protect, u8 ebid, u8 *status,
  3225. u16 *calculated_crc, u16 *stored_crc)
  3226. {
  3227. struct pt_core_data *cd = dev_get_drvdata(dev);
  3228. if (protect)
  3229. return pt_pip_verify_config_block_crc(cd, ebid,
  3230. status, calculated_crc, stored_crc);
  3231. return pt_pip_verify_config_block_crc_(cd, ebid,
  3232. status, calculated_crc, stored_crc);
  3233. }
  3234. /*******************************************************************************
  3235. * FUNCTION: pt_pip_get_config_row_size_
  3236. *
  3237. * SUMMARY: Sends the PIP "Get Data Row Size" (0x21) command to the DUT
  3238. *
  3239. * RETURN:
  3240. * 0 = success
  3241. * !0 = failure
  3242. *
  3243. * PARAMETERS:
  3244. * *cd - pointer to core data
  3245. * protect - flag to call protected or non-protected
  3246. * *row_size - pointer to store the retrieved row size
  3247. ******************************************************************************/
  3248. static int pt_pip_get_config_row_size_(struct pt_core_data *cd,
  3249. u16 *row_size)
  3250. {
  3251. int rc = 0;
  3252. struct pt_hid_output hid_output = {
  3253. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_CONFIG_ROW_SIZE),
  3254. };
  3255. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3256. if (rc)
  3257. return rc;
  3258. *row_size = get_unaligned_le16(&cd->response_buf[5]);
  3259. return 0;
  3260. }
  3261. /*******************************************************************************
  3262. * FUNCTION: pt_pip_get_config_row_size
  3263. *
  3264. * SUMMARY: Protected call to pt_hid_output_get_config_row_size_ within
  3265. * an exclusive access to the DUT.
  3266. *
  3267. * RETURN:
  3268. * 0 = success
  3269. * !0 = failure
  3270. *
  3271. * PARAMETERS:
  3272. * *cd - pointer to core data
  3273. * protect - flag to call protected or non-protected
  3274. * *row_size - pointer to store the retrieved row size
  3275. ******************************************************************************/
  3276. static int pt_pip_get_config_row_size(struct pt_core_data *cd,
  3277. u16 *row_size)
  3278. {
  3279. int rc;
  3280. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3281. if (rc < 0) {
  3282. pt_debug(cd->dev, DL_ERROR,
  3283. "%s: fail get exclusive ex=%p own=%p\n",
  3284. __func__, cd->exclusive_dev, cd->dev);
  3285. return rc;
  3286. }
  3287. rc = pt_pip_get_config_row_size_(cd, row_size);
  3288. if (release_exclusive(cd, cd->dev) < 0)
  3289. pt_debug(cd->dev, DL_ERROR,
  3290. "%s: fail to release exclusive\n", __func__);
  3291. return rc;
  3292. }
  3293. /*******************************************************************************
  3294. * FUNCTION: _pt_request_pip_get_config_row_size
  3295. *
  3296. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3297. * calls to the protected or unprotected call to
  3298. * pt_pip_get_config_row_size_
  3299. *
  3300. * RETURN:
  3301. * 0 = success
  3302. * !0 = failure
  3303. *
  3304. * PARAMETERS:
  3305. * *dev - pointer to device structure
  3306. * protect - flag to call protected or non-protected
  3307. * *row_size - pointer to store the retrieved row size
  3308. ******************************************************************************/
  3309. static int _pt_request_pip_get_config_row_size(struct device *dev,
  3310. int protect, u16 *row_size)
  3311. {
  3312. struct pt_core_data *cd = dev_get_drvdata(dev);
  3313. if (protect)
  3314. return pt_pip_get_config_row_size(cd, row_size);
  3315. return pt_pip_get_config_row_size_(cd, row_size);
  3316. }
  3317. /*******************************************************************************
  3318. * FUNCTION: pt_pip1_read_data_block_
  3319. *
  3320. * SUMMARY: Sends the PIP "Read Data Block" (0x22) command to the DUT and print
  3321. * output data to the "read_buf" and update "crc".
  3322. *
  3323. * RETURN:
  3324. * 0 = success
  3325. * !0 = failure
  3326. *
  3327. * PARAMETERS:
  3328. * *cd - pointer to core data
  3329. * row_number - row number
  3330. * length - length of data to read
  3331. * ebid - block id
  3332. * *actual_read_len - Actual data length read
  3333. * *read_buf - pointer to the buffer to store read data
  3334. * read_buf_size - size of read_buf
  3335. * *crc - pointer to store CRC of row data
  3336. ******************************************************************************/
  3337. static int pt_pip1_read_data_block_(struct pt_core_data *cd,
  3338. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3339. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3340. {
  3341. int read_ebid;
  3342. int status;
  3343. int rc = 0;
  3344. int write_length = 5;
  3345. u8 write_buf[5];
  3346. u8 cmd_offset = 0;
  3347. u16 calc_crc;
  3348. struct pt_hid_output hid_output = {
  3349. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_READ_DATA_BLOCK),
  3350. .write_length = write_length,
  3351. .write_buf = write_buf,
  3352. };
  3353. write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3354. write_buf[cmd_offset++] = HI_BYTE(row_number);
  3355. write_buf[cmd_offset++] = LOW_BYTE(length);
  3356. write_buf[cmd_offset++] = HI_BYTE(length);
  3357. write_buf[cmd_offset++] = ebid;
  3358. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3359. if (rc)
  3360. return rc;
  3361. status = cd->response_buf[5];
  3362. if (status)
  3363. return status;
  3364. read_ebid = cd->response_buf[6];
  3365. if ((read_ebid != ebid) || (cd->response_buf[9] != 0))
  3366. return -EPROTO;
  3367. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  3368. if (length == 0 || *actual_read_len == 0)
  3369. return 0;
  3370. if (read_buf_size >= *actual_read_len)
  3371. memcpy(read_buf, &cd->response_buf[10], *actual_read_len);
  3372. else
  3373. return -EPROTO;
  3374. *crc = get_unaligned_le16(&cd->response_buf[*actual_read_len + 10]);
  3375. /* Validate Row Data CRC */
  3376. calc_crc = _pt_compute_crc(read_buf, *actual_read_len);
  3377. if (*crc == calc_crc) {
  3378. return 0;
  3379. } else {
  3380. pt_debug(cd->dev, DL_ERROR,
  3381. "%s: CRC Mismatch packet=0x%04X calc=0x%04X\n",
  3382. __func__, *crc, calc_crc);
  3383. return -EPROTO;
  3384. }
  3385. }
  3386. /*******************************************************************************
  3387. * FUNCTION: _pt_request_pip_read_data_block
  3388. *
  3389. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3390. * calls to pt_pip1_read_data_block_
  3391. *
  3392. * RETURN:
  3393. * 0 = success
  3394. * !0 = failure
  3395. *
  3396. * PARAMETERS:
  3397. * *dev - pointer to device structure
  3398. * row_number - row number
  3399. * length - length of data to read
  3400. * ebid - block id
  3401. * *actual_read_len - Actual data length read
  3402. * *read_buf - pointer to the buffer to store read data
  3403. * *crc - pointer to store CRC of row data
  3404. ******************************************************************************/
  3405. static int _pt_request_pip_read_data_block(struct device *dev,
  3406. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3407. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3408. {
  3409. struct pt_core_data *cd = dev_get_drvdata(dev);
  3410. return pt_pip1_read_data_block_(cd, row_number, length,
  3411. ebid, actual_read_len, read_buf, read_buf_size, crc);
  3412. }
  3413. /*******************************************************************************
  3414. * FUNCTION: pt_pip1_write_data_block_
  3415. *
  3416. * SUMMARY: Sends the PIP "Write Data Block" (0x23) command to the DUT and
  3417. * write data to the data block.
  3418. *
  3419. * RETURN:
  3420. * 0 = success
  3421. * !0 = failure
  3422. *
  3423. * PARAMETERS:
  3424. * *cd - pointer to core data
  3425. * row_number - row in config block to write to
  3426. * write_length - length of data to write
  3427. * ebid - enumerated block ID
  3428. * *write_buf - pointer to buffer to write
  3429. * *security_key - pointer to security key to allow write
  3430. * *actual_write_len - pointer to store data length actually written
  3431. ******************************************************************************/
  3432. static int pt_pip1_write_data_block_(struct pt_core_data *cd,
  3433. u16 row_number, u16 write_length, u8 ebid, u8 *write_buf,
  3434. u8 *security_key, u16 *actual_write_len)
  3435. {
  3436. /* row_number + write_len + ebid + security_key + crc */
  3437. int full_write_length = 2 + 2 + 1 + write_length + 8 + 2;
  3438. u8 *full_write_buf;
  3439. u8 cmd_offset = 0;
  3440. u16 crc;
  3441. int status;
  3442. int rc = 0;
  3443. int read_ebid;
  3444. u8 *data;
  3445. struct pt_hid_output hid_output = {
  3446. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_WRITE_DATA_BLOCK),
  3447. .write_length = full_write_length,
  3448. .timeout_ms = PT_PIP1_CMD_WRITE_CONF_BLOCK_TIMEOUT,
  3449. };
  3450. full_write_buf = kzalloc(full_write_length, GFP_KERNEL);
  3451. if (!full_write_buf)
  3452. return -ENOMEM;
  3453. hid_output.write_buf = full_write_buf;
  3454. full_write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3455. full_write_buf[cmd_offset++] = HI_BYTE(row_number);
  3456. full_write_buf[cmd_offset++] = LOW_BYTE(write_length);
  3457. full_write_buf[cmd_offset++] = HI_BYTE(write_length);
  3458. full_write_buf[cmd_offset++] = ebid;
  3459. data = &full_write_buf[cmd_offset];
  3460. memcpy(data, write_buf, write_length);
  3461. cmd_offset += write_length;
  3462. memcpy(&full_write_buf[cmd_offset], security_key, 8);
  3463. cmd_offset += 8;
  3464. crc = _pt_compute_crc(data, write_length);
  3465. full_write_buf[cmd_offset++] = LOW_BYTE(crc);
  3466. full_write_buf[cmd_offset++] = HI_BYTE(crc);
  3467. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3468. if (rc)
  3469. goto exit;
  3470. status = cd->response_buf[5];
  3471. if (status) {
  3472. rc = -EINVAL;
  3473. goto exit;
  3474. }
  3475. read_ebid = cd->response_buf[6];
  3476. if (read_ebid != ebid) {
  3477. rc = -EPROTO;
  3478. goto exit;
  3479. }
  3480. *actual_write_len = get_unaligned_le16(&cd->response_buf[7]);
  3481. exit:
  3482. kfree(full_write_buf);
  3483. return rc;
  3484. }
  3485. /*******************************************************************************
  3486. * FUNCTION: _pt_request_pip_write_data_block
  3487. *
  3488. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3489. * calls to the protected or unprotected call to pt_pip1_write_data_block_
  3490. *
  3491. * RETURN:
  3492. * 0 = success
  3493. * !0 = failure
  3494. *
  3495. * PARAMETERS:
  3496. * *dev - pointer to device structure
  3497. * protect - flag to call protected or non-protected
  3498. * row_number - row in config block to write to
  3499. * write_length - length of data to write
  3500. * ebid - enumerated block ID
  3501. * *write_buf - pointer to buffer to write
  3502. * *security_key - pointer to security key to allow write
  3503. * *actual_write_len - pointer to store data length actually written
  3504. ******************************************************************************/
  3505. static int _pt_request_pip_write_data_block(struct device *dev,
  3506. u16 row_number, u16 write_length, u8 ebid,
  3507. u8 *write_buf, u8 *security_key, u16 *actual_write_len)
  3508. {
  3509. struct pt_core_data *cd = dev_get_drvdata(dev);
  3510. return pt_pip1_write_data_block_(cd, row_number,
  3511. write_length, ebid, write_buf, security_key,
  3512. actual_write_len);
  3513. }
  3514. /*******************************************************************************
  3515. * FUNCTION: pt_pip_get_data_structure_
  3516. *
  3517. * SUMMARY: Sends the PIP "Retrieve Data Structure" (0x24) command to the DUT
  3518. * returning a structure of data defined by data_id
  3519. *
  3520. * RETURN:
  3521. * 0 = success
  3522. * !0 = failure
  3523. *
  3524. * PARAMETERS:
  3525. * *cd - pointer to core data
  3526. * read_offset - read pointer offset
  3527. * read_length - length of data to read
  3528. * data_id - data ID to read
  3529. * *status - pointer to store the read response status
  3530. * *data_format - pointer to store format of data read
  3531. * *actual_read_len - pointer to store data length actually read
  3532. * *data - pointer to store data read
  3533. ******************************************************************************/
  3534. static int pt_pip_get_data_structure_(
  3535. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3536. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3537. u8 *data)
  3538. {
  3539. int rc = 0;
  3540. u16 total_read_len = 0;
  3541. u16 read_len;
  3542. u16 off_buf = 0;
  3543. u8 write_buf[5];
  3544. u8 read_data_id;
  3545. struct pt_hid_output hid_output = {
  3546. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_DATA_STRUCTURE),
  3547. .write_length = 5,
  3548. .write_buf = write_buf,
  3549. };
  3550. again:
  3551. write_buf[0] = LOW_BYTE(read_offset);
  3552. write_buf[1] = HI_BYTE(read_offset);
  3553. write_buf[2] = LOW_BYTE(read_length);
  3554. write_buf[3] = HI_BYTE(read_length);
  3555. write_buf[4] = data_id;
  3556. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3557. if (rc)
  3558. return rc;
  3559. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  3560. goto set_status;
  3561. read_data_id = cd->response_buf[6];
  3562. if (read_data_id != data_id)
  3563. return -EPROTO;
  3564. read_len = get_unaligned_le16(&cd->response_buf[7]);
  3565. if (read_len && data) {
  3566. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  3567. total_read_len += read_len;
  3568. if (read_len < read_length) {
  3569. read_offset += read_len;
  3570. off_buf += read_len;
  3571. read_length -= read_len;
  3572. goto again;
  3573. }
  3574. }
  3575. if (data_format)
  3576. *data_format = cd->response_buf[9];
  3577. if (actual_read_len)
  3578. *actual_read_len = total_read_len;
  3579. set_status:
  3580. if (status)
  3581. *status = cd->response_buf[5];
  3582. return rc;
  3583. }
  3584. /*******************************************************************************
  3585. * FUNCTION: pt_pip_get_data_structure
  3586. *
  3587. * SUMMARY: Protected call to pt_hid_output_get_data_structure within
  3588. * an exclusive access to the DUT.
  3589. *
  3590. * RETURN:
  3591. * 0 = success
  3592. * !0 = failure
  3593. *
  3594. * PARAMETERS:
  3595. * *cd - pointer to core data
  3596. * read_offset - read pointer offset
  3597. * read_length - length of data to read
  3598. * data_id - data ID to read
  3599. * *status - pointer to store the read response status
  3600. * *data_format - pointer to store format of data read
  3601. * *actual_read_len - pointer to store data length actually read
  3602. * *data - pointer to store data read
  3603. ******************************************************************************/
  3604. static int pt_pip_get_data_structure(
  3605. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3606. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3607. u8 *data)
  3608. {
  3609. int rc;
  3610. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3611. if (rc < 0) {
  3612. pt_debug(cd->dev, DL_ERROR,
  3613. "%s: fail get exclusive ex=%p own=%p\n",
  3614. __func__, cd->exclusive_dev, cd->dev);
  3615. return rc;
  3616. }
  3617. rc = pt_pip_get_data_structure_(cd, read_offset,
  3618. read_length, data_id, status, data_format,
  3619. actual_read_len, data);
  3620. if (release_exclusive(cd, cd->dev) < 0)
  3621. pt_debug(cd->dev, DL_ERROR,
  3622. "%s: fail to release exclusive\n", __func__);
  3623. return rc;
  3624. }
  3625. /*******************************************************************************
  3626. * FUNCTION: _pt_request_pip_get_data_structure
  3627. *
  3628. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3629. * calls to the protected or unprotected call to
  3630. * pt_pip_get_data_structure
  3631. *
  3632. * RETURN:
  3633. * 0 = success
  3634. * !0 = failure
  3635. *
  3636. * PARAMETERS:
  3637. * *dev - pointer to device structure
  3638. * protect - flag to call protected or non-protected
  3639. * read_offset - read pointer offset
  3640. * read_length - length of data to read
  3641. * data_id - data ID to read
  3642. * *status - pointer to store the read response status
  3643. * *data_format - pointer to store format of data read
  3644. * *actual_read_len - pointer to store data length actually read
  3645. * *data - pointer to store data read
  3646. ******************************************************************************/
  3647. static int _pt_request_pip_get_data_structure(struct device *dev,
  3648. int protect, u16 read_offset, u16 read_length, u8 data_id,
  3649. u8 *status, u8 *data_format, u16 *actual_read_len, u8 *data)
  3650. {
  3651. struct pt_core_data *cd = dev_get_drvdata(dev);
  3652. if (protect)
  3653. return pt_pip_get_data_structure(cd,
  3654. read_offset, read_length, data_id, status,
  3655. data_format, actual_read_len, data);
  3656. return pt_pip_get_data_structure_(cd,
  3657. read_offset, read_length, data_id, status,
  3658. data_format, actual_read_len, data);
  3659. }
  3660. /*******************************************************************************
  3661. * FUNCTION: _pt_manage_local_cal_data
  3662. *
  3663. * SUMMARY: This function manages storing or restoring a copy of the Firmware
  3664. * CALIBRATION data. It stores it in a local static array and can be
  3665. * cleared, loaded or used to restore the CAL data back to the running FW.
  3666. * The CAL data is read or restored by use of the PIP1 commands:
  3667. * - READ_DATA_BLOCK (0x22)
  3668. * - WRITE_DATA_BLOCK (0x23)
  3669. *
  3670. * RETURN:
  3671. * 0 = success
  3672. * !0 = failure
  3673. *
  3674. * PARAMETERS:
  3675. * *dev - pointer to device structure
  3676. * action - One of the following actions:
  3677. * - PT_CAL_DATA_SAVE
  3678. * - PT_CAL_DATA_RESTORE
  3679. * - PT_CAL_DATA_CLEAR
  3680. * - PT_CAL_DATA_SIZE
  3681. * *size - pointer to the number of bytes transferred
  3682. * *crc - pointer to Chip ID CRC that the CAL data was retrieved from
  3683. ******************************************************************************/
  3684. static int _pt_manage_local_cal_data(struct device *dev, u8 action, u16 *size,
  3685. unsigned short *crc)
  3686. {
  3687. struct pt_core_data *cd = dev_get_drvdata(dev);
  3688. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  3689. unsigned short calc_id_crc = 0;
  3690. static u8 *cal_cache_data;
  3691. static u16 cal_cache_len;
  3692. static unsigned short cal_cache_chip_id;
  3693. int rc = 0;
  3694. u8 *tmp_data = NULL;
  3695. u8 row_number = 0;
  3696. u8 prefix[20];
  3697. u16 cal_size = 0;
  3698. u16 transfer_size;
  3699. u16 act_trans_len = 0;
  3700. u16 byte_offset = 0;
  3701. u16 cal_blk_size;
  3702. u16 total_rows;
  3703. u16 remain_bytes;
  3704. u16 data_block_crc;
  3705. u16 buf_size = 12;
  3706. pt_debug(dev, DL_INFO, "%s: ATM - CAL Cache action=%d\n",
  3707. __func__, action);
  3708. switch (action) {
  3709. case PT_CAL_DATA_SAVE:
  3710. /* Read the size of the CAL block and calculate # rows */
  3711. tmp_data = kzalloc(buf_size, GFP_KERNEL);
  3712. if (!tmp_data) {
  3713. rc = -ENOMEM;
  3714. goto exit;
  3715. }
  3716. /*
  3717. * Don't check rc as doing a read size will give a false
  3718. * error on the CRC check.
  3719. */
  3720. rc = pt_pip1_read_data_block_(cd, row_number, 0, PT_CAL_EBID,
  3721. &act_trans_len, tmp_data, buf_size, &data_block_crc);
  3722. cal_blk_size = act_trans_len;
  3723. kfree(tmp_data);
  3724. pt_debug(dev, DL_INFO,
  3725. "%s: CAL Cache size=%d FW CAL Size=%d\n",
  3726. __func__, cal_cache_len, cal_blk_size);
  3727. /* Safety net to ensure we didn't read incorrect size */
  3728. if (cal_blk_size > PT_CAL_DATA_MAX_SIZE) {
  3729. pt_debug(dev, DL_ERROR, "%s: Alloc struct Failed\n",
  3730. __func__);
  3731. rc = 1;
  3732. goto exit;
  3733. }
  3734. /* Panels could have diff CAL sizes, Re-allocate the cache */
  3735. if (cal_blk_size != cal_cache_len) {
  3736. kfree(cal_cache_data);
  3737. cal_cache_data = kzalloc(cal_blk_size + 2,
  3738. GFP_KERNEL);
  3739. if (!cal_cache_data) {
  3740. rc = -ENOMEM;
  3741. goto exit;
  3742. }
  3743. pt_debug(dev, DL_INFO, "%s: CAL Cache Allocated\n",
  3744. __func__);
  3745. }
  3746. memset(&cal_cache_data[0], 0, cal_blk_size + 2);
  3747. /* Calculate how many rows [0-n] (PIP Transactions) */
  3748. total_rows = (cal_blk_size / PT_CAL_DATA_ROW_SIZE) - 1;
  3749. remain_bytes = cal_blk_size % PT_CAL_DATA_ROW_SIZE;
  3750. /* Add row if we have a last partial row */
  3751. if (remain_bytes > 0)
  3752. total_rows++;
  3753. pt_debug(dev, DL_INFO,
  3754. "%s: CAL size=%d rows=[0-%d] partial row bytes=%d\n",
  3755. __func__, cal_blk_size, total_rows, remain_bytes);
  3756. /* Read all rows unless an error occurs */
  3757. rc = 0;
  3758. while (rc == 0 && row_number <= total_rows) {
  3759. act_trans_len = 0;
  3760. if (remain_bytes > 0 && row_number == total_rows)
  3761. transfer_size = remain_bytes;
  3762. else
  3763. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3764. rc = pt_pip1_read_data_block_(cd, row_number,
  3765. transfer_size, PT_CAL_EBID,
  3766. &act_trans_len,
  3767. &cal_cache_data[byte_offset], cal_blk_size + 2,
  3768. &data_block_crc);
  3769. if (rc) {
  3770. /* Error occurred, exit loop */
  3771. cal_size = 0;
  3772. break;
  3773. }
  3774. pt_debug(dev, DL_INFO,
  3775. "%s: CAL read rc=%d actual read len=%d\n",
  3776. __func__, rc, act_trans_len);
  3777. byte_offset += act_trans_len;
  3778. cal_size = byte_offset;
  3779. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3780. pt_pr_buf(dev, DL_INFO,
  3781. &cal_cache_data[byte_offset - act_trans_len],
  3782. act_trans_len, prefix);
  3783. row_number++;
  3784. }
  3785. if (cal_size > 0) {
  3786. /* Save a CRC of the chip info the CAL was saved from */
  3787. calc_id_crc = crc_ccitt_calculate(
  3788. (u8 *)&ttdata->chip_rev, 4 + PT_UID_SIZE);
  3789. cal_cache_chip_id = calc_id_crc;
  3790. cal_cache_len = cal_size;
  3791. pt_debug(dev, DL_INFO,
  3792. "%s: CAL Cache: CRC=0x%04X Total Size=%d\n",
  3793. __func__, calc_id_crc, cal_size);
  3794. }
  3795. *size = cal_size;
  3796. *crc = calc_id_crc;
  3797. break;
  3798. case PT_CAL_DATA_RESTORE:
  3799. cal_size = cal_cache_len;
  3800. while ((rc == 0) && (byte_offset < cal_size)) {
  3801. if (cal_size - byte_offset > PT_CAL_DATA_ROW_SIZE)
  3802. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3803. else
  3804. transfer_size = cal_size - byte_offset;
  3805. rc = pt_pip1_write_data_block_(cd, row_number,
  3806. transfer_size, PT_CAL_EBID,
  3807. &cal_cache_data[byte_offset],
  3808. (u8 *)pt_data_block_security_key,
  3809. &act_trans_len);
  3810. byte_offset += act_trans_len;
  3811. pt_debug(dev, DL_INFO, "%s: CAL write byte offset=%d\n",
  3812. __func__, byte_offset);
  3813. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3814. pt_pr_buf(dev, DL_INFO,
  3815. &cal_cache_data[byte_offset - act_trans_len],
  3816. act_trans_len, prefix);
  3817. if ((byte_offset > cal_size) ||
  3818. (act_trans_len != transfer_size))
  3819. rc = -EIO;
  3820. row_number++;
  3821. }
  3822. *size = byte_offset;
  3823. *crc = cal_cache_chip_id;
  3824. break;
  3825. case PT_CAL_DATA_CLEAR:
  3826. if (cal_cache_data)
  3827. memset(&cal_cache_data[0], 0, cal_cache_len);
  3828. cal_cache_len = 0;
  3829. cal_cache_chip_id = 0;
  3830. *size = 0;
  3831. *crc = 0;
  3832. break;
  3833. case PT_CAL_DATA_INFO:
  3834. default:
  3835. *size = cal_cache_len;
  3836. *crc = cal_cache_chip_id;
  3837. pt_debug(dev, DL_INFO,
  3838. "%s: CAL Cache: CRC=%04X Total Size=%d\n",
  3839. __func__, cal_cache_chip_id,
  3840. cal_cache_len);
  3841. break;
  3842. }
  3843. exit:
  3844. pt_debug(dev, DL_INFO,
  3845. "%s: CAL Cache exit: rc=%d CRC=0x%04X Total Size=%d\n",
  3846. __func__, rc, *crc, *size);
  3847. return rc;
  3848. }
  3849. /*******************************************************************************
  3850. * FUNCTION: pt_pip_run_selftest_
  3851. *
  3852. * SUMMARY: Sends the PIP "Run Self Test" (0x26) command to the DUT
  3853. * to execute a FW built in self test
  3854. *
  3855. * RETURN:
  3856. * 0 = success
  3857. * !0 = failure
  3858. *
  3859. * PARAMETERS:
  3860. * *cd - pointer to core data
  3861. * test_id - enumerated test ID to run
  3862. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3863. * *status - pointer to store the read response status
  3864. * *summary_results - pointer to store the results summary
  3865. * *results_available - pointer to store if results are available
  3866. *****************************************************************************/
  3867. static int pt_pip_run_selftest_(
  3868. struct pt_core_data *cd, u8 test_id,
  3869. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3870. u8 *results_available)
  3871. {
  3872. int rc = 0;
  3873. u8 write_buf[2];
  3874. struct pt_hid_output hid_output = {
  3875. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RUN_SELF_TEST),
  3876. .write_length = 2,
  3877. .write_buf = write_buf,
  3878. .timeout_ms = PT_PIP1_CMD_RUN_SELF_TEST_TIMEOUT,
  3879. };
  3880. write_buf[0] = test_id;
  3881. write_buf[1] = write_idacs_to_flash;
  3882. if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  3883. hid_output.write_length = 1;
  3884. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3885. if (rc)
  3886. return rc;
  3887. if (status)
  3888. *status = cd->response_buf[5];
  3889. if (summary_result)
  3890. *summary_result = cd->response_buf[6];
  3891. /* results_available only available before PIP 1.03 */
  3892. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 3)) {
  3893. if (results_available)
  3894. *results_available = cd->response_buf[7];
  3895. }
  3896. return rc;
  3897. }
  3898. /*******************************************************************************
  3899. * FUNCTION: pt_pip_run_selftest
  3900. *
  3901. * SUMMARY: Protected call to pt_hid_output_run_selftest within
  3902. * an exclusive access to the DUT.
  3903. *
  3904. * RETURN:
  3905. * 0 = success
  3906. * !0 = failure
  3907. *
  3908. * PARAMETERS:
  3909. * *cd - pointer to core data
  3910. * test_id - enumerated test ID to run
  3911. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3912. * *status - pointer to store the read response status
  3913. * *summary_results - pointer to store the results summary
  3914. * *results_available - pointer to store if results are available
  3915. ******************************************************************************/
  3916. static int pt_pip_run_selftest(
  3917. struct pt_core_data *cd, u8 test_id,
  3918. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3919. u8 *results_available)
  3920. {
  3921. int rc;
  3922. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3923. if (rc < 0) {
  3924. pt_debug(cd->dev, DL_ERROR,
  3925. "%s: fail get exclusive ex=%p own=%p\n",
  3926. __func__, cd->exclusive_dev, cd->dev);
  3927. return rc;
  3928. }
  3929. rc = pt_pip_run_selftest_(cd, test_id,
  3930. write_idacs_to_flash, status, summary_result,
  3931. results_available);
  3932. if (release_exclusive(cd, cd->dev) < 0)
  3933. pt_debug(cd->dev, DL_ERROR,
  3934. "%s: fail to release exclusive\n", __func__);
  3935. return rc;
  3936. }
  3937. /*******************************************************************************
  3938. * FUNCTION: _pt_request_pip_run_selftest
  3939. *
  3940. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3941. * calls to the protected or unprotected call to pt_pip_run_selftest
  3942. *
  3943. * RETURN:
  3944. * 0 = success
  3945. * !0 = failure
  3946. *
  3947. * PARAMETERS:
  3948. * *dev - pointer to device structure
  3949. * protect - flag to call protected or non-protected
  3950. * test_id - enumerated test ID to run
  3951. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3952. * *status - pointer to store the read response status
  3953. * *summary_results - pointer to store the results summary
  3954. * *results_available - pointer to store if results are available
  3955. ******************************************************************************/
  3956. static int _pt_request_pip_run_selftest(struct device *dev,
  3957. int protect, u8 test_id, u8 write_idacs_to_flash, u8 *status,
  3958. u8 *summary_result, u8 *results_available)
  3959. {
  3960. struct pt_core_data *cd = dev_get_drvdata(dev);
  3961. if (protect)
  3962. return pt_pip_run_selftest(cd, test_id,
  3963. write_idacs_to_flash, status, summary_result,
  3964. results_available);
  3965. return pt_pip_run_selftest_(cd, test_id,
  3966. write_idacs_to_flash, status, summary_result,
  3967. results_available);
  3968. }
  3969. /*******************************************************************************
  3970. * FUNCTION: _pt_pip_get_selftest_result_
  3971. *
  3972. * SUMMARY: Sends the PIP "Get Self Test Results" (0x27) command to the DUT
  3973. * to retrieve the self test results from the self test already executed
  3974. *
  3975. * RETURN:
  3976. * 0 = success
  3977. * !0 = failure
  3978. *
  3979. * PARAMETERS:
  3980. * *cd - pointer to core data
  3981. * read_offset - read pointer offset
  3982. * read_length - length of data to read
  3983. * test_id - enumerated test ID to read selftest results from
  3984. * *status - pointer to store the read response status
  3985. * *actual_read_len - pointer to store data length actually read
  3986. * *status - pointer to where the cmd response statas is stored
  3987. ******************************************************************************/
  3988. static int pt_pip_get_selftest_result_(
  3989. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3990. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  3991. {
  3992. int rc = 0;
  3993. u16 total_read_len = 0;
  3994. u16 read_len;
  3995. u16 off_buf = 0;
  3996. u8 write_buf[5];
  3997. u8 read_test_id;
  3998. bool repeat;
  3999. struct pt_hid_output hid_output = {
  4000. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SELF_TEST_RESULT),
  4001. .write_length = 5,
  4002. .write_buf = write_buf,
  4003. };
  4004. /*
  4005. * Do not repeat reading for Auto Shorts test
  4006. * when PIP version < 1.3
  4007. */
  4008. repeat = IS_PIP_VER_GE(&cd->sysinfo, 1, 3)
  4009. || test_id != PT_ST_ID_AUTOSHORTS;
  4010. again:
  4011. write_buf[0] = LOW_BYTE(read_offset);
  4012. write_buf[1] = HI_BYTE(read_offset);
  4013. write_buf[2] = LOW_BYTE(read_length);
  4014. write_buf[3] = HI_BYTE(read_length);
  4015. write_buf[4] = test_id;
  4016. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4017. if (rc)
  4018. return rc;
  4019. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  4020. goto set_status;
  4021. read_test_id = cd->response_buf[6];
  4022. if (read_test_id != test_id)
  4023. return -EPROTO;
  4024. read_len = get_unaligned_le16(&cd->response_buf[7]);
  4025. if (read_len && data) {
  4026. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  4027. total_read_len += read_len;
  4028. if (repeat && read_len < read_length) {
  4029. read_offset += read_len;
  4030. off_buf += read_len;
  4031. read_length -= read_len;
  4032. goto again;
  4033. }
  4034. }
  4035. if (actual_read_len)
  4036. *actual_read_len = total_read_len;
  4037. set_status:
  4038. if (status)
  4039. *status = cd->response_buf[5];
  4040. return rc;
  4041. }
  4042. /*******************************************************************************
  4043. * FUNCTION: _pt_pip_get_selftest_result
  4044. *
  4045. * SUMMARY: Protected call to pt_hid_output_get_selftest_result by exclusive
  4046. * access to the DUT
  4047. *
  4048. * RETURN:
  4049. * 0 = success
  4050. * !0 = failure
  4051. *
  4052. * PARAMETERS:
  4053. * *cd - pointer to core data
  4054. * read_offset - read pointer offset
  4055. * read_length - length of data to read
  4056. * test_id - enumerated test ID to read selftest results from
  4057. * *status - pointer to store the read response status
  4058. * *actual_read_len - pointer to store data length actually read
  4059. * *status - pointer to where the cmd response statas is stored
  4060. ******************************************************************************/
  4061. static int pt_pip_get_selftest_result(
  4062. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4063. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4064. {
  4065. int rc;
  4066. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4067. if (rc < 0) {
  4068. pt_debug(cd->dev, DL_ERROR,
  4069. "%s: fail get exclusive ex=%p own=%p\n",
  4070. __func__, cd->exclusive_dev, cd->dev);
  4071. return rc;
  4072. }
  4073. rc = pt_pip_get_selftest_result_(cd, read_offset,
  4074. read_length, test_id, status, actual_read_len, data);
  4075. if (release_exclusive(cd, cd->dev) < 0)
  4076. pt_debug(cd->dev, DL_ERROR,
  4077. "%s: fail to release exclusive\n", __func__);
  4078. return rc;
  4079. }
  4080. /*******************************************************************************
  4081. * FUNCTION: _pt_request_pip_get_selftest_result
  4082. *
  4083. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4084. * calls to the protected or unprotected call to pt_pip_get_selftest_result
  4085. *
  4086. * RETURN:
  4087. * 0 = success
  4088. * !0 = failure
  4089. *
  4090. * PARAMETERS:
  4091. * *dev - pointer to device structure
  4092. * protect - flag to call protected or non-protected
  4093. * read_offset - read pointer offset
  4094. * read_length - length of data to read
  4095. * test_id - enumerated test ID to read selftest results from
  4096. * *status - pointer to store the read response status
  4097. * *actual_read_len - pointer to store data length actually read
  4098. * *data - pointer to where the data read is stored
  4099. ******************************************************************************/
  4100. static int _pt_request_pip_get_selftest_result(struct device *dev,
  4101. int protect, u16 read_offset, u16 read_length, u8 test_id,
  4102. u8 *status, u16 *actual_read_len, u8 *data)
  4103. {
  4104. struct pt_core_data *cd = dev_get_drvdata(dev);
  4105. if (protect)
  4106. return pt_pip_get_selftest_result(cd, read_offset,
  4107. read_length, test_id, status, actual_read_len,
  4108. data);
  4109. return pt_pip_get_selftest_result_(cd, read_offset,
  4110. read_length, test_id, status, actual_read_len,
  4111. data);
  4112. }
  4113. /*******************************************************************************
  4114. * FUNCTION: _pt_pip_load_self_test_param
  4115. *
  4116. * SUMMARY: Sends the PIP "Load Self Test Parameters" (0x25) command to the DUT
  4117. * to load parameters needed by a self test
  4118. *
  4119. * RETURN:
  4120. * 0 = success
  4121. * !0 = failure
  4122. *
  4123. * PARAMETERS:
  4124. * *cd - pointer to core data
  4125. * self_test_id - enumerated test ID for which the parmeters belong
  4126. * load_offset - mem offset to where to load parameters
  4127. * load_length - length of parameter data to load
  4128. * *parameters - pointer to list of parameter data
  4129. * *status - pointer to store the response status
  4130. * *ret_test_id - pointer to returned test id the parameters were stored
  4131. * *act_load_len - pointer to store the actual load length that was writen
  4132. ******************************************************************************/
  4133. static int pt_pip_load_self_test_param_(struct pt_core_data *cd,
  4134. u8 self_test_id, u16 load_offset, u16 load_length,
  4135. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4136. {
  4137. int rc = 0;
  4138. int i;
  4139. u8 write_buf[PT_MAX_PIP1_MSG_SIZE];
  4140. struct pt_hid_output hid_output = {
  4141. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_LOAD_SELF_TEST_PARAM),
  4142. .write_length = 5 + load_length,
  4143. .write_buf = write_buf,
  4144. .timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT,
  4145. };
  4146. write_buf[0] = LOW_BYTE(load_offset);
  4147. write_buf[1] = HI_BYTE(load_offset);
  4148. write_buf[2] = LOW_BYTE(load_length);
  4149. write_buf[3] = HI_BYTE(load_length);
  4150. write_buf[4] = self_test_id;
  4151. for (i = 0; i < load_length; i++)
  4152. write_buf[i + 5] = parameters[i];
  4153. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4154. if (rc)
  4155. return rc;
  4156. if (status)
  4157. *status = cd->response_buf[5];
  4158. if (ret_test_id)
  4159. *ret_test_id = cd->response_buf[6];
  4160. if (act_load_len)
  4161. *act_load_len = get_unaligned_le16(&cd->response_buf[7]);
  4162. return rc;
  4163. }
  4164. /*******************************************************************************
  4165. * FUNCTION: pt_pip_load_self_test_param
  4166. *
  4167. * SUMMARY: Protected call to pt_pip_load_self_test_param_ within an exclusive
  4168. * access to the DUT.
  4169. *
  4170. * RETURN:
  4171. * 0 = success
  4172. * !0 = failure
  4173. *
  4174. * PARAMETERS:
  4175. * *cd - pointer to core data
  4176. * self_test_id - enumerated test ID for which the parmeters belong
  4177. * load_offset - mem offset to where to load parameters
  4178. * load_length - length of parameter data to load
  4179. * *parameters - pointer to list of parameter data
  4180. * *status - pointer to store the response status
  4181. * *ret_test_id - pointer to returned test id the parameters were stored
  4182. * *act_load_len - pointer to store the actual load length that was writen
  4183. ******************************************************************************/
  4184. static int pt_pip_load_self_test_param(struct pt_core_data *cd,
  4185. u8 self_test_id, u16 load_offset, u16 load_length,
  4186. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4187. {
  4188. int rc;
  4189. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4190. if (rc < 0) {
  4191. pt_debug(cd->dev, DL_ERROR,
  4192. "%s: fail get exclusive ex=%p own=%p\n",
  4193. __func__, cd->exclusive_dev, cd->dev);
  4194. return rc;
  4195. }
  4196. rc = pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4197. load_length, parameters, status, ret_test_id, act_load_len);
  4198. if (release_exclusive(cd, cd->dev) < 0)
  4199. pt_debug(cd->dev, DL_ERROR,
  4200. "%s: fail to release exclusive\n", __func__);
  4201. return rc;
  4202. }
  4203. /*******************************************************************************
  4204. * FUNCTION: _pt_request_pip_load_self_test_param
  4205. *
  4206. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4207. * calls to the protected or unprotected call to
  4208. * pt_pip_load_self_test_param
  4209. *
  4210. * RETURN:
  4211. * 0 = success
  4212. * !0 = failure
  4213. *
  4214. * PARAMETERS:
  4215. * *dev - pointer to device structure
  4216. * protect - flag to call protected or non-protected
  4217. * self_test_id - enumerated test ID for which the parmeters belong
  4218. * load_offset - mem offset to where to load parameters
  4219. * load_length - length of parameter data to load
  4220. * *parameters - pointer to list of parameter data
  4221. * *status - pointer to store the response status
  4222. * *ret_test_id - pointer to returned test id the parameters were stored
  4223. * *act_load_len - pointer to store the actual load length that was writen
  4224. ******************************************************************************/
  4225. static int _pt_request_pip_load_self_test_param(struct device *dev,
  4226. int protect, u8 self_test_id, u16 load_offset, u16 load_length,
  4227. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4228. {
  4229. struct pt_core_data *cd = dev_get_drvdata(dev);
  4230. if (protect)
  4231. return pt_pip_load_self_test_param(cd, self_test_id,
  4232. load_offset, load_length, parameters, status, ret_test_id,
  4233. act_load_len);
  4234. return pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4235. load_length, parameters, status, ret_test_id, act_load_len);
  4236. }
  4237. /*******************************************************************************
  4238. * FUNCTION: pt_pip_calibrate_ext_
  4239. *
  4240. * SUMMARY: Send the PIP1 Extended Calibrate command (0x30) to the DUT waiting
  4241. * for the response
  4242. *
  4243. * NOTE: This calibrate command requires the DUT to support PIP version >= 1.10
  4244. *
  4245. * RETURN:
  4246. * 0 = success
  4247. * !0 = failure
  4248. *
  4249. * PARAMETERS:
  4250. * *cd - pointer to core data
  4251. * *cal_data - pointer to extended calibration data structure
  4252. * *status - pointer to where the command response status is stored
  4253. ******************************************************************************/
  4254. static int pt_pip_calibrate_ext_(struct pt_core_data *cd,
  4255. struct pt_cal_ext_data *cal_data, u8 *status)
  4256. {
  4257. int rc = 0;
  4258. int write_length = 4;
  4259. u8 write_buf[4];
  4260. u16 size = 0;
  4261. unsigned short crc = 0;
  4262. struct pt_hid_output hid_output = {
  4263. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_DEVICE_EXTENDED),
  4264. .write_length = write_length,
  4265. .write_buf = write_buf,
  4266. .timeout_ms = PT_PIP1_CMD_CALIBRATE_EXT_TIMEOUT,
  4267. };
  4268. if (cal_data == NULL)
  4269. return -EINVAL;
  4270. memcpy(write_buf, cal_data, sizeof(struct pt_cal_ext_data));
  4271. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4272. if (rc)
  4273. return rc;
  4274. if (status)
  4275. *status = cd->response_buf[5];
  4276. /*
  4277. * When doing a calibration on a flashless DUT, save CAL data in
  4278. * the TTDL cache on any successful calibration
  4279. */
  4280. if (*status == 0 && cd->cal_cache_in_host) {
  4281. pt_debug(cd->dev, DL_INFO, "%s: Retrieve and Save CAL\n",
  4282. __func__);
  4283. rc = _pt_manage_local_cal_data(cd->dev, PT_CAL_DATA_SAVE,
  4284. &size, &crc);
  4285. if (rc)
  4286. pt_debug(cd->dev, DL_ERROR,
  4287. "%s: Error Saving CAL rc=%d\n", __func__, rc);
  4288. else
  4289. pt_debug(cd->dev, DL_INFO,
  4290. "%s: Saved CAL: chip ID=0x%04X size=%d\n",
  4291. __func__, crc, size);
  4292. }
  4293. return 0;
  4294. }
  4295. /*******************************************************************************
  4296. * FUNCTION: pt_pip_calibrate_ext
  4297. *
  4298. * SUMMARY: Protected call to pt_pip_calibrate_ext_ by exclusive access to the
  4299. * DUT
  4300. *
  4301. * RETURN:
  4302. * 0 = success
  4303. * !0 = failure
  4304. *
  4305. * PARAMETERS:
  4306. * *cd - pointer to core data
  4307. * *cal_data - pointer to extended calibration data structure
  4308. * *status - pointer to where the command response status is stored
  4309. ******************************************************************************/
  4310. static int pt_pip_calibrate_ext(struct pt_core_data *cd,
  4311. struct pt_cal_ext_data *cal_data, u8 *status)
  4312. {
  4313. int rc;
  4314. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4315. if (rc < 0) {
  4316. pt_debug(cd->dev, DL_ERROR,
  4317. "%s: fail get exclusive ex=%p own=%p\n",
  4318. __func__, cd->exclusive_dev, cd->dev);
  4319. return rc;
  4320. }
  4321. rc = pt_pip_calibrate_ext_(cd, cal_data, status);
  4322. if (release_exclusive(cd, cd->dev) < 0)
  4323. pt_debug(cd->dev, DL_ERROR,
  4324. "%s: fail to release exclusive\n", __func__);
  4325. return rc;
  4326. }
  4327. /*******************************************************************************
  4328. * FUNCTION: _pt_request_pip_calibrate_ext
  4329. *
  4330. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4331. * calls to the protected or unprotected call to pt_pip_calibrate_ext
  4332. *
  4333. * RETURN:
  4334. * 0 = success
  4335. * !0 = failure
  4336. *
  4337. * PARAMETERS:
  4338. * *dev - pointer to device structure
  4339. * protect - flag to call protected or non-protected
  4340. * *cal_data - pointer to extended calibration data structure
  4341. * *status - pointer to where the command response status is stored
  4342. ******************************************************************************/
  4343. static int _pt_request_pip_calibrate_ext(struct device *dev,
  4344. int protect, struct pt_cal_ext_data *cal_data, u8 *status)
  4345. {
  4346. struct pt_core_data *cd = dev_get_drvdata(dev);
  4347. if (protect)
  4348. return pt_pip_calibrate_ext(cd, cal_data, status);
  4349. return pt_pip_calibrate_ext_(cd, cal_data, status);
  4350. }
  4351. /*******************************************************************************
  4352. * FUNCTION: pt_pip_calibrate_idacs_
  4353. *
  4354. * SUMMARY: Send the PIP Calibrate IDACs command (0x28) to the DUT waiting
  4355. * for the response
  4356. *
  4357. * RETURN:
  4358. * 0 = success
  4359. * !0 = failure
  4360. *
  4361. * PARAMETERS:
  4362. * *cd - pointer to core data
  4363. * mode - sense mode to calibrate (0-5)
  4364. * *status - pointer to where the command response status is stored
  4365. ******************************************************************************/
  4366. static int pt_pip_calibrate_idacs_(struct pt_core_data *cd,
  4367. u8 mode, u8 *status)
  4368. {
  4369. int rc = 0;
  4370. int write_length = 1;
  4371. u8 write_buf[1];
  4372. u8 cmd_offset = 0;
  4373. struct pt_hid_output hid_output = {
  4374. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_IDACS),
  4375. .write_length = write_length,
  4376. .write_buf = write_buf,
  4377. .timeout_ms = PT_PIP1_CMD_CALIBRATE_IDAC_TIMEOUT,
  4378. };
  4379. write_buf[cmd_offset++] = mode;
  4380. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4381. if (rc)
  4382. return rc;
  4383. *status = cd->response_buf[5];
  4384. if (*status)
  4385. return -EINVAL;
  4386. return 0;
  4387. }
  4388. /*******************************************************************************
  4389. * FUNCTION: pt_pip_calibrate_idacs
  4390. *
  4391. * SUMMARY: Protected call to pt_hid_output_calibrate_idacs_ by exclusive
  4392. * access to the DUT
  4393. *
  4394. * RETURN:
  4395. * 0 = success
  4396. * !0 = failure
  4397. *
  4398. * PARAMETERS:
  4399. * *cd - pointer to core data
  4400. * mode - sense mode to calibrate (0-5)
  4401. * *status - pointer to where the command response status is stored
  4402. ******************************************************************************/
  4403. static int pt_pip_calibrate_idacs(struct pt_core_data *cd,
  4404. u8 mode, u8 *status)
  4405. {
  4406. int rc;
  4407. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4408. if (rc < 0) {
  4409. pt_debug(cd->dev, DL_ERROR,
  4410. "%s: fail get exclusive ex=%p own=%p\n",
  4411. __func__, cd->exclusive_dev, cd->dev);
  4412. return rc;
  4413. }
  4414. rc = pt_pip_calibrate_idacs_(cd, mode, status);
  4415. if (release_exclusive(cd, cd->dev) < 0)
  4416. pt_debug(cd->dev, DL_ERROR,
  4417. "%s: fail to release exclusive\n", __func__);
  4418. return rc;
  4419. }
  4420. /*******************************************************************************
  4421. * FUNCTION: _pt_request_pip_calibrate_idacs
  4422. *
  4423. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4424. * calls to the protected or unprotected call to pt_pip_calibrate_idacs
  4425. *
  4426. * RETURN:
  4427. * 0 = success
  4428. * !0 = failure
  4429. *
  4430. * PARAMETERS:
  4431. * *dev - pointer to device structure
  4432. * protect - flag to call protected or non-protected
  4433. * mode - sense mode to calibrate (0-5)
  4434. * *status - pointer to where the command response status is stored
  4435. ******************************************************************************/
  4436. static int _pt_request_pip_calibrate_idacs(struct device *dev,
  4437. int protect, u8 mode, u8 *status)
  4438. {
  4439. struct pt_core_data *cd = dev_get_drvdata(dev);
  4440. if (protect)
  4441. return pt_pip_calibrate_idacs(cd, mode, status);
  4442. return pt_pip_calibrate_idacs_(cd, mode, status);
  4443. }
  4444. /*******************************************************************************
  4445. * FUNCTION: pt_hid_output_initialize_baselines_
  4446. *
  4447. * SUMMARY: Send the PIP "Initialize Baselines" command (0x29) to the DUT
  4448. * waiting for the response.
  4449. *
  4450. * RETURN:
  4451. * 0 = success
  4452. * !0 = failure
  4453. *
  4454. * PARAMETERS:
  4455. * *cd - pointer to core data
  4456. * test_id - bit type flag to allow initialize baseline MUT,BTN,SELG
  4457. * each or together with a single command.
  4458. * *status - pointer to where the command response status is stored
  4459. ******************************************************************************/
  4460. static int pt_hid_output_initialize_baselines_(
  4461. struct pt_core_data *cd, u8 test_id, u8 *status)
  4462. {
  4463. int rc = 0;
  4464. int write_length = 1;
  4465. u8 write_buf[1];
  4466. u8 cmd_offset = 0;
  4467. struct pt_hid_output hid_output = {
  4468. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_INITIALIZE_BASELINES),
  4469. .write_length = write_length,
  4470. .write_buf = write_buf,
  4471. };
  4472. write_buf[cmd_offset++] = test_id;
  4473. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4474. if (rc)
  4475. return rc;
  4476. *status = cd->response_buf[5];
  4477. if (*status)
  4478. return -EINVAL;
  4479. return rc;
  4480. }
  4481. /*******************************************************************************
  4482. * FUNCTION: pt_hid_output_initialize_baselines
  4483. *
  4484. * SUMMARY: Protected call to pt_hid_output_initialize_baselines_ by exclusive
  4485. * access to the DUT
  4486. *
  4487. * RETURN:
  4488. * 0 = success
  4489. * !0 = failure
  4490. *
  4491. * PARAMETERS:
  4492. * *cd - pointer to core data
  4493. * test_id - enumerated ID against which to initialize the baseline
  4494. * *status - pointer to where the command response status is stored
  4495. ******************************************************************************/
  4496. static int pt_hid_output_initialize_baselines(struct pt_core_data *cd,
  4497. u8 test_id, u8 *status)
  4498. {
  4499. int rc;
  4500. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4501. if (rc < 0) {
  4502. pt_debug(cd->dev, DL_ERROR,
  4503. "%s: fail get exclusive ex=%p own=%p\n",
  4504. __func__, cd->exclusive_dev, cd->dev);
  4505. return rc;
  4506. }
  4507. rc = pt_hid_output_initialize_baselines_(cd, test_id, status);
  4508. if (release_exclusive(cd, cd->dev) < 0)
  4509. pt_debug(cd->dev, DL_ERROR,
  4510. "%s: fail to release exclusive\n", __func__);
  4511. return rc;
  4512. }
  4513. /*******************************************************************************
  4514. * FUNCTION: _pt_request_pip_initialize_baselines
  4515. *
  4516. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4517. * calls to the protected or unprotected call to
  4518. * pt_pip_initialize_baselines
  4519. *
  4520. * RETURN:
  4521. * 0 = success
  4522. * !0 = failure
  4523. *
  4524. * PARAMETERS:
  4525. * *dev - pointer to device structure
  4526. * protect - flag to call protected or non-protected
  4527. * test_id - enumerated ID against which to initialize the baseline
  4528. * *status - pointer to where the command response status is stored
  4529. ******************************************************************************/
  4530. static int _pt_request_pip_initialize_baselines(struct device *dev,
  4531. int protect, u8 test_id, u8 *status)
  4532. {
  4533. struct pt_core_data *cd = dev_get_drvdata(dev);
  4534. if (protect)
  4535. return pt_hid_output_initialize_baselines(cd, test_id,
  4536. status);
  4537. return pt_hid_output_initialize_baselines_(cd, test_id, status);
  4538. }
  4539. /*******************************************************************************
  4540. * FUNCTION: pt_hid_output_exec_panel_scan_
  4541. *
  4542. * SUMMARY: Sends the PIP "Execute Panel Scan" (0x2A) to the DUT and waits for
  4543. * the response
  4544. *
  4545. * RETURN:
  4546. * 0 = success
  4547. * !0 = failure
  4548. *
  4549. * PARAMETERS:
  4550. * *cd - pointer to core data
  4551. ******************************************************************************/
  4552. static int pt_hid_output_exec_panel_scan_(struct pt_core_data *cd)
  4553. {
  4554. struct pt_hid_output hid_output = {
  4555. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_EXEC_PANEL_SCAN),
  4556. };
  4557. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  4558. }
  4559. /*******************************************************************************
  4560. * FUNCTION: pt_hid_output_exec_panel_scan
  4561. *
  4562. * SUMMARY: Protected call to pt_hid_output_exec_panel_scan_ by exclusive
  4563. * access to the DUT.
  4564. *
  4565. * RETURN:
  4566. * 0 = success
  4567. * !0 = failure
  4568. *
  4569. * PARAMETERS:
  4570. * *cd - pointer to core data
  4571. ******************************************************************************/
  4572. static int pt_hid_output_exec_panel_scan(struct pt_core_data *cd)
  4573. {
  4574. int rc;
  4575. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4576. if (rc < 0) {
  4577. pt_debug(cd->dev, DL_ERROR,
  4578. "%s: fail get exclusive ex=%p own=%p\n",
  4579. __func__, cd->exclusive_dev, cd->dev);
  4580. return rc;
  4581. }
  4582. rc = pt_hid_output_exec_panel_scan_(cd);
  4583. if (release_exclusive(cd, cd->dev) < 0)
  4584. pt_debug(cd->dev, DL_ERROR,
  4585. "%s: fail to release exclusive\n", __func__);
  4586. return rc;
  4587. }
  4588. /*******************************************************************************
  4589. * FUNCTION: pt_pip2_exec_panel_scan_
  4590. *
  4591. * SUMMARY: Send the PIP2 "Execute Panel Scan" (0x21) to the DUT and waits for
  4592. * the response
  4593. *
  4594. * RETURN:
  4595. * 0 = success
  4596. * !0 = failure
  4597. *
  4598. * PARAMETERS:
  4599. * *cd - pointer to core data
  4600. * scan_type - type of panel scan to perform (PIP2 only)
  4601. ******************************************************************************/
  4602. static int pt_pip2_exec_panel_scan_(struct pt_core_data *cd, u8 scan_type)
  4603. {
  4604. int rc = 0;
  4605. u8 data[2];
  4606. u8 read_buf[10];
  4607. u16 actual_read_len;
  4608. pt_debug(cd->dev, DL_DEBUG, "%s: PIP2 Execute Scan %d\n",
  4609. __func__, scan_type);
  4610. data[0] = scan_type;
  4611. rc = _pt_request_pip2_send_cmd(cd->dev,
  4612. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_EXECUTE_SCAN,
  4613. data, 1, read_buf, &actual_read_len);
  4614. if (rc) {
  4615. pt_debug(cd->dev, DL_ERROR,
  4616. "%s EXECUTE_SCAN command for type %d failed. rc=%d\n",
  4617. __func__, scan_type, rc);
  4618. }
  4619. return rc;
  4620. }
  4621. /*******************************************************************************
  4622. * FUNCTION: pt_pip2_exec_panel_scan
  4623. *
  4624. * SUMMARY: Protected call to pt_pip2_exec_panel_scan_ by exclusive
  4625. * access to the DUT.
  4626. *
  4627. * RETURN:
  4628. * 0 = success
  4629. * !0 = failure
  4630. *
  4631. * PARAMETERS:
  4632. * *cd - pointer to core data
  4633. * scan_type - type of panel scan to perform (PIP2 only)
  4634. ******************************************************************************/
  4635. static int pt_pip2_exec_panel_scan(struct pt_core_data *cd, u8 scan_type)
  4636. {
  4637. int rc;
  4638. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4639. if (rc < 0) {
  4640. pt_debug(cd->dev, DL_ERROR,
  4641. "%s: fail get exclusive ex=%p own=%p\n",
  4642. __func__, cd->exclusive_dev, cd->dev);
  4643. return rc;
  4644. }
  4645. rc = pt_pip2_exec_panel_scan_(cd, scan_type);
  4646. if (release_exclusive(cd, cd->dev) < 0)
  4647. pt_debug(cd->dev, DL_ERROR,
  4648. "%s: fail to release exclusive\n", __func__);
  4649. return rc;
  4650. }
  4651. /*******************************************************************************
  4652. * FUNCTION: _pt_request_pip_exec_panel_scan
  4653. *
  4654. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4655. * calls to the protected or unprotected call to
  4656. * pt_pip2_exec_panel_scan or pt_hid_output_exec_panel_scan
  4657. *
  4658. * RETURN:
  4659. * 0 = success
  4660. * !0 = failure
  4661. *
  4662. * PARAMETERS:
  4663. * *dev - pointer to device structure
  4664. * protect - flag to call protected or non-protected
  4665. * scan_type - type of panel scan to perform (PIP2 only)
  4666. ******************************************************************************/
  4667. static int _pt_request_pip_exec_panel_scan(struct device *dev,
  4668. int protect, u8 scan_type)
  4669. {
  4670. struct pt_core_data *cd = dev_get_drvdata(dev);
  4671. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 12)) {
  4672. if (protect)
  4673. return pt_pip2_exec_panel_scan(cd, scan_type);
  4674. return pt_pip2_exec_panel_scan_(cd, scan_type);
  4675. }
  4676. if (protect)
  4677. return pt_hid_output_exec_panel_scan(cd);
  4678. return pt_hid_output_exec_panel_scan_(cd);
  4679. }
  4680. /*******************************************************************************
  4681. * FUNCTION: pt_hid_output_retrieve_panel_scan_
  4682. *
  4683. * SUMMARY: Sends the PIP "Retrieve Panel Scan" (0x2B) command to the DUT
  4684. * to retrieve the specified data type for a the last successful Execute
  4685. * Panel Scan command.
  4686. *
  4687. * RETURN:
  4688. * 0 = success
  4689. * !0 = failure
  4690. *
  4691. * PARAMETERS:
  4692. * *dev - pointer to device structure
  4693. * protect - flag to call protected or non-protected
  4694. * read_offset - read pointer offset
  4695. * read_count - length of data to read
  4696. * data_id - enumerated test ID to read selftest results from
  4697. * *response - pointer to store the read response status
  4698. * *config - pointer to store config data
  4699. * *actual_read_len - pointer to store data length actually read
  4700. * *read_buf - pointer to the read buffer
  4701. ******************************************************************************/
  4702. static int pt_hid_output_retrieve_panel_scan_(
  4703. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4704. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4705. u8 *read_buf)
  4706. {
  4707. int status;
  4708. u8 read_data_id;
  4709. int rc = 0;
  4710. int write_length = 5;
  4711. u8 write_buf[5];
  4712. u8 cmd_offset = 0;
  4713. u8 data_elem_size;
  4714. int size;
  4715. int data_size;
  4716. struct pt_hid_output hid_output = {
  4717. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RETRIEVE_PANEL_SCAN),
  4718. .write_length = write_length,
  4719. .write_buf = write_buf,
  4720. };
  4721. write_buf[cmd_offset++] = LOW_BYTE(read_offset);
  4722. write_buf[cmd_offset++] = HI_BYTE(read_offset);
  4723. write_buf[cmd_offset++] = LOW_BYTE(read_count);
  4724. write_buf[cmd_offset++] = HI_BYTE(read_count);
  4725. write_buf[cmd_offset++] = data_id;
  4726. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4727. if (rc)
  4728. return rc;
  4729. status = cd->response_buf[5];
  4730. if (status)
  4731. return -EINVAL;
  4732. read_data_id = cd->response_buf[6];
  4733. if (read_data_id != data_id)
  4734. return -EPROTO;
  4735. size = get_unaligned_le16(&cd->response_buf[0]);
  4736. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  4737. *config = cd->response_buf[9];
  4738. data_elem_size = *config & 0x07;
  4739. data_size = *actual_read_len * data_elem_size;
  4740. if (read_buf)
  4741. memcpy(read_buf, &cd->response_buf[10], data_size);
  4742. if (response)
  4743. memcpy(response, cd->response_buf, size);
  4744. return rc;
  4745. }
  4746. /*******************************************************************************
  4747. * FUNCTION: pt_hid_output_retrieve_panel_scan
  4748. *
  4749. * SUMMARY: Protected call to pt_hid_output_retrieve_panel_scan_ by exclusive
  4750. * access to the DUT.
  4751. *
  4752. * RETURN:
  4753. * 0 = success
  4754. * !0 = failure
  4755. *
  4756. * PARAMETERS:
  4757. * *dev - pointer to device structure
  4758. * protect - flag to call protected or non-protected
  4759. * read_offset - read pointer offset
  4760. * read_count - length of data to read
  4761. * data_id - enumerated test ID to read selftest results from
  4762. * *response - pointer to store the read response status
  4763. * *config - pointer to store config data
  4764. * *actual_read_len - pointer to store data length actually read
  4765. * *read_buf - pointer to the read buffer
  4766. ******************************************************************************/
  4767. static int pt_hid_output_retrieve_panel_scan(
  4768. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4769. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4770. u8 *read_buf)
  4771. {
  4772. int rc;
  4773. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4774. if (rc < 0) {
  4775. pt_debug(cd->dev, DL_ERROR,
  4776. "%s: fail get exclusive ex=%p own=%p\n",
  4777. __func__, cd->exclusive_dev, cd->dev);
  4778. return rc;
  4779. }
  4780. rc = pt_hid_output_retrieve_panel_scan_(cd, read_offset,
  4781. read_count, data_id, response, config,
  4782. actual_read_len, read_buf);
  4783. if (release_exclusive(cd, cd->dev) < 0)
  4784. pt_debug(cd->dev, DL_ERROR,
  4785. "%s: fail to release exclusive\n", __func__);
  4786. return rc;
  4787. }
  4788. /*******************************************************************************
  4789. * FUNCTION: _pt_request_pip_retrieve_panel_scan
  4790. *
  4791. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4792. * calls to the protected or unprotected call to
  4793. * pt_hid_output_retrieve_panel_scan
  4794. *
  4795. * RETURN:
  4796. * 0 = success
  4797. * !0 = failure
  4798. *
  4799. * PARAMETERS:
  4800. * *dev - pointer to device structure
  4801. * protect - flag to call protected or non-protected
  4802. * read_offset - read pointer offset
  4803. * read_count - length of data to read
  4804. * data_id - enumerated test ID to read selftest results from
  4805. * *response - pointer to store the read response status
  4806. * *config - pointer to store config data
  4807. * *actual_read_len - pointer to store data length actually read
  4808. * *read_buf - pointer to the read buffer
  4809. ******************************************************************************/
  4810. static int _pt_request_pip_retrieve_panel_scan(struct device *dev,
  4811. int protect, u16 read_offset, u16 read_count, u8 data_id,
  4812. u8 *response, u8 *config, u16 *actual_read_len, u8 *read_buf)
  4813. {
  4814. struct pt_core_data *cd = dev_get_drvdata(dev);
  4815. if (protect)
  4816. return pt_hid_output_retrieve_panel_scan(cd,
  4817. read_offset, read_count, data_id, response,
  4818. config, actual_read_len, read_buf);
  4819. return pt_hid_output_retrieve_panel_scan_(cd,
  4820. read_offset, read_count, data_id, response,
  4821. config, actual_read_len, read_buf);
  4822. }
  4823. /*******************************************************************************
  4824. * FUNCTION: _pt_request_pip_user_cmd
  4825. *
  4826. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4827. * calls to the protected or unprotected call to
  4828. * pt_hid_output_user_cmd
  4829. *
  4830. * RETURN:
  4831. * 0 = success
  4832. * !0 = failure
  4833. *
  4834. * PARAMETERS:
  4835. * *dev - pointer to device structure
  4836. * protect - flag to call protected or non-protected
  4837. * read_len - length of data to read
  4838. * *read_buf - pointer to store read data
  4839. * write_len - length of data to write
  4840. * *write_buf - pointer to buffer to write
  4841. * *actual_read_len - pointer to store data length actually read
  4842. ******************************************************************************/
  4843. static int _pt_request_pip_user_cmd(struct device *dev,
  4844. int protect, u16 read_len, u8 *read_buf, u16 write_len,
  4845. u8 *write_buf, u16 *actual_read_len)
  4846. {
  4847. struct pt_core_data *cd = dev_get_drvdata(dev);
  4848. if (protect)
  4849. return pt_hid_output_user_cmd(cd, read_len, read_buf,
  4850. write_len, write_buf, actual_read_len);
  4851. return pt_hid_output_user_cmd_(cd, read_len, read_buf,
  4852. write_len, write_buf, actual_read_len);
  4853. }
  4854. /*******************************************************************************
  4855. * FUNCTION: pt_hid_output_bl_get_information_
  4856. *
  4857. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x38) command to the
  4858. * DUT to retrieve bootloader version and chip identification information.
  4859. *
  4860. * RETURN:
  4861. * 0 = success
  4862. * !0 = failure
  4863. *
  4864. * PARAMETERS:
  4865. * *cd - pointer to core data
  4866. * *return_data - pointer to store the return data
  4867. *****************************************************************************/
  4868. static int pt_hid_output_bl_get_information_(struct pt_core_data *cd,
  4869. u8 *return_data)
  4870. {
  4871. int rc;
  4872. int data_len;
  4873. struct pt_hid_output hid_output = {
  4874. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_INFO),
  4875. };
  4876. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4877. if (rc)
  4878. return rc;
  4879. data_len = get_unaligned_le16(&cd->input_buf[6]);
  4880. if (!data_len)
  4881. return -EPROTO;
  4882. memcpy(return_data, &cd->response_buf[8], data_len);
  4883. return 0;
  4884. }
  4885. /*******************************************************************************
  4886. * FUNCTION: pt_hid_output_bl_get_information
  4887. *
  4888. * SUMMARY: Protected call to pt_hid_output_bl_get_information_ by exclusive
  4889. * access to the DUT.
  4890. *
  4891. * RETURN:
  4892. * 0 = success
  4893. * !0 = failure
  4894. *
  4895. * PARAMETERS:
  4896. * *cd - pointer to core data
  4897. * *return_data - pointer to store the return data
  4898. ******************************************************************************/
  4899. static int pt_hid_output_bl_get_information(struct pt_core_data *cd,
  4900. u8 *return_data)
  4901. {
  4902. int rc;
  4903. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4904. if (rc < 0) {
  4905. pt_debug(cd->dev, DL_ERROR,
  4906. "%s: fail get exclusive ex=%p own=%p\n",
  4907. __func__, cd->exclusive_dev, cd->dev);
  4908. return rc;
  4909. }
  4910. rc = pt_hid_output_bl_get_information_(cd, return_data);
  4911. if (release_exclusive(cd, cd->dev) < 0)
  4912. pt_debug(cd->dev, DL_ERROR,
  4913. "%s: fail to release exclusive\n", __func__);
  4914. return rc;
  4915. }
  4916. /*******************************************************************************
  4917. * FUNCTION: _pt_request_pip_bl_get_information
  4918. *
  4919. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4920. * calls to the protected or unprotected call to
  4921. * pt_hid_output_bl_get_information
  4922. *
  4923. * RETURN:
  4924. * 0 = success
  4925. * !0 = failure
  4926. *
  4927. * PARAMETERS:
  4928. * *dev - pointer to device structure
  4929. * protect - flag to call protected or non-protected
  4930. * *return_data - pointer to store bl data
  4931. ******************************************************************************/
  4932. static int _pt_request_pip_bl_get_information(struct device *dev,
  4933. int protect, u8 *return_data)
  4934. {
  4935. struct pt_core_data *cd = dev_get_drvdata(dev);
  4936. if (protect)
  4937. return pt_hid_output_bl_get_information(cd, return_data);
  4938. return pt_hid_output_bl_get_information_(cd, return_data);
  4939. }
  4940. /*******************************************************************************
  4941. * FUNCTION: pt_hid_output_bl_initiate_bl_
  4942. *
  4943. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x48) command to the
  4944. * DUT to erases the entire TrueTouch application, Configuration Data block,
  4945. * and Design Data block in flash and enables the host to execute the Program
  4946. * and Verify Row command to bootload the application image and data.
  4947. *
  4948. * RETURN:
  4949. * 0 = success
  4950. * !0 = failure
  4951. *
  4952. * PARAMETERS:
  4953. * *cd - pointer to core data
  4954. * protect - flag to call protected or non-protected
  4955. * key_size - size of key
  4956. * *key_buf - pointer to key data to allow operation
  4957. * row_size - size of the meta data row
  4958. * *metadata_row_buf - pointer to meta data to write
  4959. ******************************************************************************/
  4960. static int pt_hid_output_bl_initiate_bl_(struct pt_core_data *cd,
  4961. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  4962. {
  4963. u16 write_length = key_size + row_size;
  4964. u8 *write_buf;
  4965. int rc = 0;
  4966. struct pt_hid_output hid_output = {
  4967. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_INITIATE_BL),
  4968. .write_length = write_length,
  4969. .timeout_ms = PT_PIP1_CMD_INITIATE_BL_TIMEOUT,
  4970. };
  4971. write_buf = kzalloc(write_length, GFP_KERNEL);
  4972. if (!write_buf)
  4973. return -ENOMEM;
  4974. hid_output.write_buf = write_buf;
  4975. if (key_size)
  4976. memcpy(write_buf, key_buf, key_size);
  4977. if (row_size)
  4978. memcpy(&write_buf[key_size], metadata_row_buf, row_size);
  4979. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4980. kfree(write_buf);
  4981. return rc;
  4982. }
  4983. /*******************************************************************************
  4984. * FUNCTION: pt_hid_output_bl_initiate_bl
  4985. *
  4986. * SUMMARY: Protected call to pt_hid_output_bl_initiate_bl_ by exclusive
  4987. * access to the DUT.
  4988. *
  4989. * RETURN:
  4990. * 0 = success
  4991. * !0 = failure
  4992. *
  4993. * PARAMETERS:
  4994. * *cd - pointer to core data
  4995. * key_size - size of key
  4996. * *key_buf - pointer to key data to allow operation
  4997. * row_size - size of the meta data row
  4998. * *metadata_row_buf - pointer to meta data to write
  4999. ******************************************************************************/
  5000. static int pt_hid_output_bl_initiate_bl(struct pt_core_data *cd,
  5001. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  5002. {
  5003. int rc;
  5004. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5005. if (rc < 0) {
  5006. pt_debug(cd->dev, DL_ERROR,
  5007. "%s: fail get exclusive ex=%p own=%p\n",
  5008. __func__, cd->exclusive_dev, cd->dev);
  5009. return rc;
  5010. }
  5011. rc = pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5012. row_size, metadata_row_buf);
  5013. if (release_exclusive(cd, cd->dev) < 0)
  5014. pt_debug(cd->dev, DL_ERROR,
  5015. "%s: fail to release exclusive\n", __func__);
  5016. return rc;
  5017. }
  5018. /*******************************************************************************
  5019. * FUNCTION: _pt_request_pip_bl_initiate_bl
  5020. *
  5021. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  5022. * calls to the protected or unprotected call to
  5023. * pt_hid_output_bl_initiate_bl
  5024. *
  5025. * RETURN:
  5026. * 0 = success
  5027. * !0 = failure
  5028. *
  5029. * PARAMETERS:
  5030. * *dev - pointer to device structure
  5031. * protect - flag to call protected or non-protected
  5032. * key_size - size of key
  5033. * *key_buf - pointer to key data to allow operation
  5034. * row_size - size of the meta data row
  5035. * *metadata_row_buf - pointer to meta data to write
  5036. ******************************************************************************/
  5037. static int _pt_request_pip_bl_initiate_bl(struct device *dev,
  5038. int protect, u16 key_size, u8 *key_buf, u16 row_size,
  5039. u8 *metadata_row_buf)
  5040. {
  5041. struct pt_core_data *cd = dev_get_drvdata(dev);
  5042. if (protect)
  5043. return pt_hid_output_bl_initiate_bl(cd, key_size, key_buf,
  5044. row_size, metadata_row_buf);
  5045. return pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5046. row_size, metadata_row_buf);
  5047. }
  5048. /*******************************************************************************
  5049. * FUNCTION: pt_hid_output_bl_program_and_verify_
  5050. *
  5051. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x39) command to upload
  5052. * and program a 128-byte row into the flash, and then verifies written data.
  5053. *
  5054. * RETURN:
  5055. * 0 = success
  5056. * !0 = failure
  5057. *
  5058. * PARAMETERS:
  5059. * *cd - pointer to core data
  5060. * data_len - length of data_buf
  5061. * *data_buf - firmware image to program
  5062. ******************************************************************************/
  5063. static int pt_hid_output_bl_program_and_verify_(
  5064. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5065. {
  5066. struct pt_hid_output hid_output = {
  5067. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY),
  5068. .write_length = data_len,
  5069. .write_buf = data_buf,
  5070. .timeout_ms = PT_PIP1_CMD_PROGRAM_AND_VERIFY_TIMEOUT,
  5071. };
  5072. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5073. }
  5074. /*******************************************************************************
  5075. * FUNCTION: pt_hid_output_bl_program_and_verify
  5076. *
  5077. * SUMMARY: Protected call to pt_hid_output_bl_program_and_verify_ by exclusive
  5078. * access to the DUT.
  5079. *
  5080. * RETURN:
  5081. * 0 = success
  5082. * !0 = failure
  5083. *
  5084. * PARAMETERS:
  5085. * *cd - pointer to core data
  5086. * data_len - length of data_buf
  5087. * *data_buf - firmware image to program
  5088. ******************************************************************************/
  5089. static int pt_hid_output_bl_program_and_verify(
  5090. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5091. {
  5092. int rc;
  5093. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5094. if (rc < 0) {
  5095. pt_debug(cd->dev, DL_ERROR,
  5096. "%s: fail get exclusive ex=%p own=%p\n",
  5097. __func__, cd->exclusive_dev, cd->dev);
  5098. return rc;
  5099. }
  5100. rc = pt_hid_output_bl_program_and_verify_(cd, data_len, data_buf);
  5101. if (release_exclusive(cd, cd->dev) < 0)
  5102. pt_debug(cd->dev, DL_ERROR,
  5103. "%s: fail to release exclusive\n", __func__);
  5104. return rc;
  5105. }
  5106. /*******************************************************************************
  5107. * FUNCTION: _pt_request_pip_bl_program_and_verify
  5108. *
  5109. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5110. * to request to have the BL program and verify a FW image
  5111. *
  5112. * RETURN:
  5113. * 0 = success
  5114. * !0 = failure
  5115. *
  5116. * PARAMETERS:
  5117. * *dev - pointer to device structure
  5118. * protect - boolean to determine to call the protected function
  5119. * data_len - length of data_buf
  5120. * *data_buf - firmware image to program
  5121. ******************************************************************************/
  5122. static int _pt_request_pip_bl_program_and_verify(
  5123. struct device *dev, int protect, u16 data_len, u8 *data_buf)
  5124. {
  5125. struct pt_core_data *cd = dev_get_drvdata(dev);
  5126. if (protect)
  5127. return pt_hid_output_bl_program_and_verify(cd, data_len,
  5128. data_buf);
  5129. return pt_hid_output_bl_program_and_verify_(cd, data_len,
  5130. data_buf);
  5131. }
  5132. /*******************************************************************************
  5133. * FUNCTION: pt_hid_output_bl_verify_app_integrity_
  5134. *
  5135. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x31) command to
  5136. * perform a full verification of the application integrity by calculating the
  5137. * CRC of the image in flash and compare it to the expected CRC stored in the
  5138. * Metadata row.
  5139. *
  5140. * RETURN:
  5141. * 0 = success
  5142. * !0 = failure
  5143. *
  5144. * PARAMETERS:
  5145. * *cd - pointer to core data
  5146. * *result - pointer to store result
  5147. ******************************************************************************/
  5148. static int pt_hid_output_bl_verify_app_integrity_(
  5149. struct pt_core_data *cd, u8 *result)
  5150. {
  5151. int rc;
  5152. struct pt_hid_output hid_output = {
  5153. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY),
  5154. };
  5155. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5156. if (rc) {
  5157. *result = 0;
  5158. return rc;
  5159. }
  5160. *result = cd->response_buf[8];
  5161. return 0;
  5162. }
  5163. /*******************************************************************************
  5164. * FUNCTION: pt_hid_output_bl_verify_app_integrity
  5165. *
  5166. * SUMMARY: Protected call to pt_hid_output_bl_verify_app_integrity_ by
  5167. * exclusive access to the DUT.
  5168. *
  5169. * RETURN:
  5170. * 0 = success
  5171. * !0 = failure
  5172. *
  5173. * PARAMETERS:
  5174. * *cd - pointer to core data
  5175. * *result - pointer to store result
  5176. ******************************************************************************/
  5177. static int pt_hid_output_bl_verify_app_integrity(
  5178. struct pt_core_data *cd, u8 *result)
  5179. {
  5180. int rc;
  5181. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5182. if (rc < 0) {
  5183. pt_debug(cd->dev, DL_ERROR,
  5184. "%s: fail get exclusive ex=%p own=%p\n",
  5185. __func__, cd->exclusive_dev, cd->dev);
  5186. return rc;
  5187. }
  5188. rc = pt_hid_output_bl_verify_app_integrity_(cd, result);
  5189. if (release_exclusive(cd, cd->dev) < 0)
  5190. pt_debug(cd->dev, DL_ERROR,
  5191. "%s: fail to release exclusive\n", __func__);
  5192. return rc;
  5193. }
  5194. /*******************************************************************************
  5195. * FUNCTION: _pt_request_pip_bl_verify_app_integrity
  5196. *
  5197. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5198. * to request to have the BL verify the application integrity (PIP1.x only)
  5199. *
  5200. * RETURN:
  5201. * 0 = success
  5202. * !0 = failure
  5203. *
  5204. * PARAMETERS:
  5205. * *dev - pointer to device structure
  5206. * protect - boolean to determine to call the protected function
  5207. * *result - pointer to store result
  5208. ******************************************************************************/
  5209. static int _pt_request_pip_bl_verify_app_integrity(
  5210. struct device *dev, int protect, u8 *result)
  5211. {
  5212. struct pt_core_data *cd = dev_get_drvdata(dev);
  5213. if (protect)
  5214. return pt_hid_output_bl_verify_app_integrity(cd, result);
  5215. return pt_hid_output_bl_verify_app_integrity_(cd, result);
  5216. }
  5217. /*******************************************************************************
  5218. * FUNCTION: pt_hid_output_bl_launch_app_
  5219. *
  5220. * SUMMARY: Sends the PIP "Launch Application" (0x3B) command to launch the
  5221. * application from bootloader (PIP1.x only).
  5222. *
  5223. * RETURN:
  5224. * 0 = success
  5225. * !0 = failure
  5226. *
  5227. * PARAMETERS:
  5228. * *cd - pointer to core data
  5229. ******************************************************************************/
  5230. static int pt_hid_output_bl_launch_app_(struct pt_core_data *cd)
  5231. {
  5232. struct pt_hid_output hid_output = {
  5233. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_LAUNCH_APP),
  5234. .reset_expected = 1,
  5235. };
  5236. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5237. }
  5238. /*******************************************************************************
  5239. * FUNCTION: pt_hid_output_bl_launch_app
  5240. *
  5241. * SUMMARY: Protected call to pt_hid_output_bl_launch_app_ by exclusive access
  5242. * to the DUT.
  5243. *
  5244. * RETURN:
  5245. * 0 = success
  5246. * !0 = failure
  5247. *
  5248. * PARAMETERS:
  5249. * *cd - pointer to core data
  5250. ******************************************************************************/
  5251. static int pt_hid_output_bl_launch_app(struct pt_core_data *cd)
  5252. {
  5253. int rc;
  5254. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5255. if (rc < 0) {
  5256. pt_debug(cd->dev, DL_ERROR,
  5257. "%s: fail get exclusive ex=%p own=%p\n",
  5258. __func__, cd->exclusive_dev, cd->dev);
  5259. return rc;
  5260. }
  5261. rc = pt_hid_output_bl_launch_app_(cd);
  5262. if (release_exclusive(cd, cd->dev) < 0)
  5263. pt_debug(cd->dev, DL_ERROR,
  5264. "%s: fail to release exclusive\n", __func__);
  5265. return rc;
  5266. }
  5267. /*******************************************************************************
  5268. * FUNCTION: _pt_request_pip_launch_app
  5269. *
  5270. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5271. * to request to have the BL launch the application. (PIP1.x only)
  5272. *
  5273. * RETURN:
  5274. * 0 = success
  5275. * !0 = failure
  5276. *
  5277. * PARAMETERS:
  5278. * *dev - pointer to device structure
  5279. * protect - boolean to determine to call the protected function
  5280. ******************************************************************************/
  5281. static int _pt_request_pip_launch_app(struct device *dev,
  5282. int protect)
  5283. {
  5284. struct pt_core_data *cd = dev_get_drvdata(dev);
  5285. if (protect)
  5286. return pt_hid_output_bl_launch_app(cd);
  5287. return pt_hid_output_bl_launch_app_(cd);
  5288. }
  5289. /*******************************************************************************
  5290. * FUNCTION: pt_hid_output_bl_get_panel_id_
  5291. *
  5292. * SUMMARY: Sends the PIP "Get Panel ID" (0x3E) command to return the Panel ID
  5293. * value store in the System Information.
  5294. *
  5295. * RETURN:
  5296. * 0 = success
  5297. * !0 = failure
  5298. *
  5299. * PARAMETERS:
  5300. * *cd - pointer to core data
  5301. * *panel_id - pointer to where the panel ID will be stored
  5302. ******************************************************************************/
  5303. static int pt_hid_output_bl_get_panel_id_(
  5304. struct pt_core_data *cd, u8 *panel_id)
  5305. {
  5306. int rc;
  5307. struct pt_hid_output hid_output = {
  5308. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_PANEL_ID),
  5309. };
  5310. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5311. if (rc == -EPROTO && cd->response_buf[5] == ERROR_COMMAND) {
  5312. pt_debug(cd->dev, DL_ERROR,
  5313. "%s: Get Panel ID command not supported\n",
  5314. __func__);
  5315. *panel_id = PANEL_ID_NOT_ENABLED;
  5316. return 0;
  5317. } else if (rc < 0) {
  5318. pt_debug(cd->dev, DL_ERROR,
  5319. "%s: Error on Get Panel ID command\n", __func__);
  5320. return rc;
  5321. }
  5322. *panel_id = cd->response_buf[8];
  5323. return 0;
  5324. }
  5325. /*******************************************************************************
  5326. * FUNCTION: pt_hid_output_bl_get_panel_id
  5327. *
  5328. * SUMMARY: Protected call to pt_hid_output_bl_get_panel_id_ by exclusive access
  5329. * to the DUT.
  5330. *
  5331. * RETURN:
  5332. * 0 = success
  5333. * !0 = failure
  5334. *
  5335. * PARAMETERS:
  5336. * *cd - pointer to core data
  5337. * *panel_id - pointer to where the panel ID will be stored
  5338. ******************************************************************************/
  5339. static int pt_hid_output_bl_get_panel_id(
  5340. struct pt_core_data *cd, u8 *panel_id)
  5341. {
  5342. int rc;
  5343. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5344. if (rc < 0) {
  5345. pt_debug(cd->dev, DL_ERROR,
  5346. "%s: fail get exclusive ex=%p own=%p\n",
  5347. __func__, cd->exclusive_dev, cd->dev);
  5348. return rc;
  5349. }
  5350. rc = pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5351. if (release_exclusive(cd, cd->dev) < 0)
  5352. pt_debug(cd->dev, DL_ERROR,
  5353. "%s: fail to release exclusive\n", __func__);
  5354. return rc;
  5355. }
  5356. /*******************************************************************************
  5357. * FUNCTION: _pt_request_pip_bl_get_panel_id
  5358. *
  5359. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5360. * to have the BL retrieve the panel ID
  5361. *
  5362. * RETURN:
  5363. * 0 = success
  5364. * !0 = failure
  5365. *
  5366. * PARAMETERS:
  5367. * *dev - pointer to device structure
  5368. * protect - flag to run in protected mode
  5369. * *panel_id - pointer to where the panel ID will be stored
  5370. ******************************************************************************/
  5371. static int _pt_request_pip_bl_get_panel_id(
  5372. struct device *dev, int protect, u8 *panel_id)
  5373. {
  5374. struct pt_core_data *cd = dev_get_drvdata(dev);
  5375. if (protect)
  5376. return pt_hid_output_bl_get_panel_id(cd, panel_id);
  5377. return pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5378. }
  5379. /*******************************************************************************
  5380. * FUNCTION: pt_pip2_get_mode_sysmode_
  5381. *
  5382. * SUMMARY: Determine the current mode and system mode of the DUT by use of the
  5383. * PIP2 STATUS command.
  5384. *
  5385. * RETURN:
  5386. * 0 = success
  5387. * !0 = failure
  5388. *
  5389. * PARAMETERS:
  5390. * *cd - pointer to core data structure
  5391. * *mode - pointer to store the retrieved mode
  5392. * *sys_mode - pointer to store the FW system mode
  5393. ******************************************************************************/
  5394. static int pt_pip2_get_mode_sysmode_(struct pt_core_data *cd,
  5395. u8 *mode, u8 *sys_mode)
  5396. {
  5397. u16 actual_read_len;
  5398. u8 read_buf[12];
  5399. u8 status, boot;
  5400. int rc = 0;
  5401. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  5402. PIP2_CMD_ID_STATUS, NULL, 0, read_buf, &actual_read_len);
  5403. pt_debug(cd->dev, DL_INFO, "%s: PIP2 STATUS command rc = %d\n",
  5404. __func__, rc);
  5405. if (!rc) {
  5406. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, actual_read_len,
  5407. "PIP2 STATUS");
  5408. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5409. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  5410. if (sys_mode) {
  5411. if (status == PIP2_RSP_ERR_NONE &&
  5412. boot == PIP2_STATUS_APP_EXEC)
  5413. *sys_mode = read_buf[PIP2_RESP_BODY_OFFSET + 1];
  5414. else
  5415. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5416. }
  5417. if (mode) {
  5418. if (status == PIP2_RSP_ERR_NONE &&
  5419. boot == PIP2_STATUS_BOOT_EXEC)
  5420. *mode = PT_MODE_BOOTLOADER;
  5421. else if (status == PIP2_RSP_ERR_NONE &&
  5422. boot == PIP2_STATUS_APP_EXEC)
  5423. *mode = PT_MODE_OPERATIONAL;
  5424. else
  5425. *mode = PT_MODE_UNKNOWN;
  5426. }
  5427. } else {
  5428. if (mode)
  5429. *mode = PT_MODE_UNKNOWN;
  5430. if (sys_mode)
  5431. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5432. pt_debug(cd->dev, DL_WARN,
  5433. "%s: Mode and sys_mode could not be determined\n",
  5434. __func__);
  5435. }
  5436. return rc;
  5437. }
  5438. /*******************************************************************************
  5439. * FUNCTION: pt_pip2_get_mode_sysmode
  5440. *
  5441. * SUMMARY: Protected call to pt_pip2_get_mode_sysmode_ by exclusive
  5442. * access to the DUT.
  5443. *
  5444. * RETURN:
  5445. * 0 = success
  5446. * !0 = failure
  5447. *
  5448. * PARAMETERS:
  5449. * *cd - pointer to core data structure
  5450. * *mode - pointer to store the retrieved mode
  5451. * *sys_mode - pointer to store the FW system mode
  5452. ******************************************************************************/
  5453. static int pt_pip2_get_mode_sysmode(struct pt_core_data *cd,
  5454. u8 *mode, u8 *sys_mode)
  5455. {
  5456. int rc;
  5457. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5458. if (rc < 0) {
  5459. pt_debug(cd->dev, DL_ERROR,
  5460. "%s: fail get exclusive ex=%p own=%p\n",
  5461. __func__, cd->exclusive_dev, cd->dev);
  5462. return rc;
  5463. }
  5464. rc = pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5465. if (release_exclusive(cd, cd->dev) < 0)
  5466. pt_debug(cd->dev, DL_ERROR,
  5467. "%s: fail to release exclusive\n", __func__);
  5468. return rc;
  5469. }
  5470. /*******************************************************************************
  5471. * FUNCTION: _pt_request_pip2_get_mode_sysmode
  5472. *
  5473. * SUMMARY: Function pointer included in core_commands struct for external
  5474. * calls to the protected or unprotected call to
  5475. * pt_pip2_get_mode_sysmode
  5476. *
  5477. * RETURN:
  5478. * 0 = success
  5479. * !0 = failure
  5480. *
  5481. * PARAMETERS:
  5482. * *dev - pointer to device structure
  5483. * *mode - pointer to store the retrieved mode
  5484. * *sys_mode - pointer to store the FW system mode
  5485. ******************************************************************************/
  5486. static int _pt_request_pip2_get_mode_sysmode(struct device *dev,
  5487. int protect, u8 *mode, u8 *sys_mode)
  5488. {
  5489. struct pt_core_data *cd = dev_get_drvdata(dev);
  5490. if (protect)
  5491. return pt_pip2_get_mode_sysmode(cd, mode, sys_mode);
  5492. return pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5493. }
  5494. /*******************************************************************************
  5495. * FUNCTION: _pt_poll_for_fw_exit_boot_mode
  5496. *
  5497. * SUMMARY: Verify and or poll for the FW to exit BOOT mode. During the FW BOOT
  5498. * mode only the following PIP commands will be serviced, any other PIP
  5499. * command the FW will respond with an "Invalid PIP Command" response.
  5500. * - Get HID Descriptor (Register 0x0001, no command ID)
  5501. * - Reset (Register 0x0005, RESET HID request)
  5502. * - Ping (Register 0x0004, Command ID 0x00
  5503. * - Get System Information (Register 0x0004, Command ID 0x02)
  5504. * - PIP2 Status (Register 0x0101, Command ID 0x01)
  5505. * - PIP2 Version (Register 0x0101, Command ID 0x07)
  5506. * This function will loop on the results of the STATUS command until
  5507. * the FW reports it is out of BOOT mode.
  5508. *
  5509. * NOTE:
  5510. * - This function will update cd->fw_system_mode
  5511. * - The STATUS cmd only supports this functionality for PIP 1.11+
  5512. *
  5513. * RETURN:
  5514. * 0 = success
  5515. * !0 = failure
  5516. *
  5517. * PARAMETERS:
  5518. * *cd - pointer to core data
  5519. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5520. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5521. ******************************************************************************/
  5522. static int _pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5523. int *actual_wait)
  5524. {
  5525. int loop = 0;
  5526. u8 sys_mode = cd->fw_system_mode;
  5527. u8 pause = 10; /* in ms */
  5528. int rc = 0;
  5529. int max_loop = (timeout / pause) + 1; /* Add 1 due to int math */
  5530. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5531. /*
  5532. * For PIP <1.11, no support for polling wait so do a hard
  5533. * coded wait and assume the FW is out of BOOT. Added 1 to
  5534. * timeout to make it clear in kmsg if non polling was done.
  5535. */
  5536. *actual_wait = PT_FW_EXIT_BOOT_MODE_TIMEOUT + 1;
  5537. pt_debug(cd->dev, DL_ERROR,
  5538. "%s: PIP %d.%d no support for ext STATUS, sleep %d\n",
  5539. __func__,
  5540. cd->sysinfo.ttdata.pip_ver_major,
  5541. cd->sysinfo.ttdata.pip_ver_minor, *actual_wait);
  5542. msleep(*actual_wait);
  5543. sys_mode = FW_SYS_MODE_SCANNING;
  5544. }
  5545. if (sys_mode == FW_SYS_MODE_BOOT) {
  5546. while (!rc && loop <= max_loop &&
  5547. (sys_mode == FW_SYS_MODE_BOOT)) {
  5548. loop++;
  5549. usleep_range(9000, pause * 1000);
  5550. rc = pt_pip2_get_mode_sysmode_(cd, NULL, &sys_mode);
  5551. pt_debug(cd->dev, DL_DEBUG,
  5552. "%s: FW in BOOT mode-sleep %dms, sys_mode=%d\n",
  5553. __func__, loop * pause, sys_mode);
  5554. }
  5555. *actual_wait = (int)(loop * pause);
  5556. pt_debug(cd->dev, DL_WARN,
  5557. "%s: FW exited BOOT mode in %dms, sys_mode=%d\n",
  5558. __func__, *actual_wait, sys_mode);
  5559. if (rc)
  5560. sys_mode = FW_SYS_MODE_UNDEFINED;
  5561. else if (sys_mode == FW_SYS_MODE_BOOT ||
  5562. sys_mode == FW_SYS_MODE_UNDEFINED)
  5563. rc = -EBUSY;
  5564. }
  5565. mutex_lock(&cd->system_lock);
  5566. cd->fw_system_mode = sys_mode;
  5567. mutex_unlock(&cd->system_lock);
  5568. return rc;
  5569. }
  5570. /*******************************************************************************
  5571. * FUNCTION: pt_poll_for_fw_exit_boot_mode
  5572. *
  5573. * SUMMARY: Protected call to _pt_poll_for_fw_exit_boot_mode by exclusive
  5574. * access to the DUT.
  5575. *
  5576. * RETURN:
  5577. * 0 = success
  5578. * !0 = failure
  5579. *
  5580. * PARAMETERS:
  5581. * *cd - pointer to core data
  5582. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5583. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5584. ******************************************************************************/
  5585. static int pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5586. int *actual_wait)
  5587. {
  5588. int rc;
  5589. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5590. if (rc < 0) {
  5591. pt_debug(cd->dev, DL_ERROR,
  5592. "%s: fail get exclusive ex=%p own=%p\n",
  5593. __func__, cd->exclusive_dev, cd->dev);
  5594. return rc;
  5595. }
  5596. rc = _pt_poll_for_fw_exit_boot_mode(cd, timeout, actual_wait);
  5597. if (release_exclusive(cd, cd->dev) < 0)
  5598. pt_debug(cd->dev, DL_ERROR,
  5599. "%s: fail to release exclusive\n", __func__);
  5600. return rc;
  5601. }
  5602. /*******************************************************************************
  5603. * FUNCTION: _pt_get_fw_sys_mode
  5604. *
  5605. * SUMMARY: Determine the FW system mode. For PIP 1.11+ the
  5606. * PIP2 STATUS command is used to directly query the FW system mode. For older
  5607. * PIP versions, there is no direct PIP commamnd that will directly provide this
  5608. * information but any PIP command above 0x1F requires scanning to be disabled
  5609. * before it will be operational. If scanning was not disabled before sending
  5610. * these PIP commands the FW will respond with a 6 byte error response. So to
  5611. * safely determine the scanning state, a PIP message that will not affect the
  5612. * operation of the FW was chosen:
  5613. * "Verify Data Block CRC (ID 0x20)" is sent and if a 6 byte error code is
  5614. * received scanning is active.
  5615. *
  5616. * RETURN:
  5617. * 0 = success
  5618. * !0 = failure
  5619. *
  5620. * PARAMETERS:
  5621. * *cd - pointer to core data
  5622. * *sys_mode - pointer to FW System mode
  5623. * *mode - pointer to mode (BL/FW)
  5624. ******************************************************************************/
  5625. static int _pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5626. {
  5627. int write_length = 1;
  5628. int report_length;
  5629. int rc = 0;
  5630. u8 tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5631. u8 tmp_mode = PT_MODE_UNKNOWN;
  5632. u8 param[1] = { PT_TCH_PARM_EBID };
  5633. struct pt_hid_output hid_output = {
  5634. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  5635. .write_length = write_length,
  5636. .write_buf = param,
  5637. .novalidate = true,
  5638. };
  5639. /* AFter PIP1.11 the preferred method is using STATUS cmd */
  5640. if (IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5641. rc = pt_pip2_get_mode_sysmode_(cd, &tmp_mode, &tmp_sys_mode);
  5642. pt_debug(cd->dev, DL_DEBUG, "%s: tmp_sys_mode=%d tmp_mode=%d\n",
  5643. __func__, tmp_sys_mode, tmp_mode);
  5644. if (!rc) {
  5645. if (tmp_mode != PT_MODE_OPERATIONAL)
  5646. tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5647. }
  5648. goto exit;
  5649. }
  5650. /* Older systems use PIP1 CONFIG_BLOCK_CRC to best determine sys_mode */
  5651. if (cd->mode != PT_MODE_OPERATIONAL) {
  5652. tmp_mode = cd->mode;
  5653. goto exit;
  5654. }
  5655. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5656. if (rc)
  5657. goto exit;
  5658. report_length = (cd->response_buf[1] << 8) | (cd->response_buf[0]);
  5659. if ((report_length == 0x06) &&
  5660. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) == 0x00) &&
  5661. (cd->response_buf[5] == PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5662. tmp_mode = PIP2_STATUS_APP_EXEC;
  5663. tmp_sys_mode = FW_SYS_MODE_SCANNING;
  5664. } else if ((report_length == 0x0A) &&
  5665. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) ==
  5666. PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5667. tmp_mode = PIP2_STATUS_APP_EXEC;
  5668. tmp_sys_mode = FW_SYS_MODE_TEST;
  5669. }
  5670. exit:
  5671. if (mode)
  5672. *mode = tmp_mode;
  5673. if (sys_mode)
  5674. *sys_mode = tmp_sys_mode;
  5675. pt_debug(cd->dev, DL_INFO, "%s: Return Mode=%d sys_mode=%d\n",
  5676. __func__, tmp_mode, tmp_sys_mode);
  5677. return rc;
  5678. }
  5679. /*******************************************************************************
  5680. * FUNCTION: pt_get_fw_sys_mode
  5681. *
  5682. * SUMMARY: Protected call to _pt_get_fw_sys_mode() to determine if FW scanning
  5683. * is active or not.
  5684. *
  5685. * RETURN:
  5686. * 0 = success
  5687. * !0 = failure
  5688. *
  5689. * PARAMETERS:
  5690. * *cd - pointer to core data
  5691. * *sys_mode - pointer to fw system mode
  5692. * *mode - pointer to mode
  5693. ******************************************************************************/
  5694. static int pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5695. {
  5696. int rc;
  5697. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5698. if (rc < 0) {
  5699. pt_debug(cd->dev, DL_ERROR,
  5700. "%s: fail get exclusive ex=%p own=%p\n",
  5701. __func__, cd->exclusive_dev, cd->dev);
  5702. return rc;
  5703. }
  5704. rc = _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5705. if (release_exclusive(cd, cd->dev) < 0)
  5706. pt_debug(cd->dev, DL_ERROR,
  5707. "%s: fail to release exclusive\n", __func__);
  5708. return rc;
  5709. }
  5710. /*******************************************************************************
  5711. * FUNCTION: _pt_request_get_fw_sys_mode
  5712. *
  5713. * SUMMARY: Function pointer included in core_cmds to allow other modules
  5714. * to request to get scan state
  5715. *
  5716. * RETURN:
  5717. * 0 = success
  5718. * !0 = failure
  5719. *
  5720. * PARAMETERS:
  5721. * *dev - pointer to device structure
  5722. * protect - flag to call protected or non-protected
  5723. * *sys_mode - pointer to FW system mode
  5724. * *mode - pointer to mode
  5725. ******************************************************************************/
  5726. static int _pt_request_get_fw_sys_mode(struct device *dev, int protect,
  5727. u8 *sys_mode, u8 *mode)
  5728. {
  5729. struct pt_core_data *cd = dev_get_drvdata(dev);
  5730. if (protect)
  5731. return pt_get_fw_sys_mode(cd, sys_mode, mode);
  5732. return _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5733. }
  5734. /* Default hid descriptor to provide basic register map */
  5735. const struct pt_hid_desc hid_desc_default = {
  5736. 230, /* hid_desc_len */
  5737. HID_APP_REPORT_ID, /* packet_id */
  5738. 0x00, /* reserved_byte */
  5739. 0x0100, /* bcd_version */
  5740. 0x00EC, /* report_desc_len */
  5741. 0x0002, /* report_desc_register */
  5742. 0x0003, /* input_register */
  5743. 0x00FE, /* max_input_len */
  5744. 0x0004, /* output_register */
  5745. 0x00FE, /* max_output_len */
  5746. 0x0005, /* command_register */
  5747. 0x0006, /* data_register */
  5748. 0x04B4, /* vendor_id */
  5749. 0xC101, /* product_id */
  5750. 0x0100, /* version_id */
  5751. {0x00, 0x00, 0x00, 0x00} /* reserved[4] */
  5752. };
  5753. /*******************************************************************************
  5754. * FUNCTION: pt_init_hid_descriptor
  5755. *
  5756. * SUMMARY: Setup default values for HID descriptor structure
  5757. *
  5758. *
  5759. * PARAMETERS:
  5760. * *desc - pointer to the HID descriptor data read back from DUT
  5761. ******************************************************************************/
  5762. static inline void pt_init_hid_descriptor(struct pt_hid_desc *desc)
  5763. {
  5764. memcpy(desc, &hid_desc_default, sizeof(hid_desc_default));
  5765. }
  5766. /*******************************************************************************
  5767. * FUNCTION: pt_get_hid_descriptor_
  5768. *
  5769. * SUMMARY: Send the get HID descriptor command to the DUT and load the response
  5770. * into the HID descriptor structure
  5771. *
  5772. * RETURN:
  5773. * 0 = success
  5774. * !0 = failure
  5775. *
  5776. * PARAMETERS:
  5777. * *cd - pointer to core data
  5778. * *desc - pointer to the HID descriptor data read back from DUT
  5779. ******************************************************************************/
  5780. static int pt_get_hid_descriptor_(struct pt_core_data *cd,
  5781. struct pt_hid_desc *desc)
  5782. {
  5783. struct device *dev = cd->dev;
  5784. int rc = 0;
  5785. int t;
  5786. u8 cmd[2];
  5787. /*
  5788. * During startup the HID descriptor is required for all future
  5789. * processing. If IRQ is already asserted due to an early touch report
  5790. * the report must be cleared before sending command.
  5791. */
  5792. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5793. /* Read HID descriptor length and version */
  5794. mutex_lock(&cd->system_lock);
  5795. cd->hid_cmd_state = 1;
  5796. mutex_unlock(&cd->system_lock);
  5797. /* Set HID descriptor register */
  5798. memcpy(cmd, &cd->hid_core.hid_desc_register,
  5799. sizeof(cd->hid_core.hid_desc_register));
  5800. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer [%zu]",
  5801. __func__, sizeof(cmd));
  5802. pt_pr_buf(cd->dev, DL_DEBUG, cmd, sizeof(cmd), ">>> Get HID Desc");
  5803. rc = pt_adap_write_read_specific(cd, 2, cmd, NULL);
  5804. if (rc) {
  5805. pt_debug(dev, DL_ERROR,
  5806. "%s: failed to get HID descriptor, rc=%d\n",
  5807. __func__, rc);
  5808. goto error;
  5809. }
  5810. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  5811. msecs_to_jiffies(PT_GET_HID_DESCRIPTOR_TIMEOUT));
  5812. if (IS_TMO(t)) {
  5813. #ifdef TTDL_DIAGNOSTICS
  5814. cd->bus_transmit_error_count++;
  5815. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  5816. #endif /* TTDL_DIAGNOSTICS */
  5817. pt_debug(cd->dev, DL_ERROR,
  5818. "%s: HID get descriptor timed out\n", __func__);
  5819. rc = -ETIME;
  5820. goto error;
  5821. } else {
  5822. cd->hw_detected = true;
  5823. }
  5824. /* Load the HID descriptor including all registers */
  5825. memcpy((u8 *)desc, cd->response_buf, sizeof(struct pt_hid_desc));
  5826. /* Check HID descriptor length and version */
  5827. pt_debug(dev, DL_INFO, "%s: HID len:%X HID ver:%X\n", __func__,
  5828. le16_to_cpu(desc->hid_desc_len),
  5829. le16_to_cpu(desc->bcd_version));
  5830. if (le16_to_cpu(desc->hid_desc_len) != sizeof(*desc) ||
  5831. le16_to_cpu(desc->bcd_version) != HID_VERSION) {
  5832. pt_debug(dev, DL_ERROR, "%s: Unsupported HID version\n",
  5833. __func__);
  5834. return -ENODEV;
  5835. }
  5836. goto exit;
  5837. error:
  5838. mutex_lock(&cd->system_lock);
  5839. cd->hid_cmd_state = 0;
  5840. mutex_unlock(&cd->system_lock);
  5841. exit:
  5842. return rc;
  5843. }
  5844. /*******************************************************************************
  5845. * FUNCTION: pt_get_hid_descriptor
  5846. *
  5847. * SUMMARY: Protected call to pt_get_hid_descriptor_()
  5848. *
  5849. * RETURN:
  5850. * 0 = success
  5851. * !0 = failure
  5852. *
  5853. * PARAMETERS:
  5854. * *cd - pointer to core data
  5855. * *desc - pointer to the HID descriptor data read back from DUT
  5856. ******************************************************************************/
  5857. static int pt_get_hid_descriptor(struct pt_core_data *cd,
  5858. struct pt_hid_desc *desc)
  5859. {
  5860. int rc;
  5861. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5862. if (rc < 0) {
  5863. pt_debug(cd->dev, DL_ERROR,
  5864. "%s: fail get exclusive ex=%p own=%p\n",
  5865. __func__, cd->exclusive_dev, cd->dev);
  5866. return rc;
  5867. }
  5868. rc = pt_get_hid_descriptor_(cd, desc);
  5869. if (release_exclusive(cd, cd->dev) < 0)
  5870. pt_debug(cd->dev, DL_ERROR,
  5871. "%s: fail to release exclusive\n", __func__);
  5872. return rc;
  5873. }
  5874. /*******************************************************************************
  5875. * FUNCTION: pt_pip2_get_version_
  5876. *
  5877. * SUMMARY: Sends a PIP2 VERSION command to the DUT and stores the data in
  5878. * cd-ttdata
  5879. *
  5880. * RETURN:
  5881. * 0 = success
  5882. * !0 = failure
  5883. *
  5884. * PARAMETERS:
  5885. * *cd - pointer to core data
  5886. ******************************************************************************/
  5887. static int pt_pip2_get_version_(struct pt_core_data *cd)
  5888. {
  5889. int rc = 0;
  5890. int status;
  5891. u8 read_buf[64];
  5892. u16 actual_read_len;
  5893. rc = _pt_request_pip2_send_cmd(cd->dev,
  5894. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  5895. NULL, 0, read_buf, &actual_read_len);
  5896. if (rc) {
  5897. pt_debug(cd->dev, DL_ERROR,
  5898. "%s: Error Sending PIP2 VERSION Cmd rc=%d\n",
  5899. __func__, rc);
  5900. return rc;
  5901. }
  5902. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5903. if (status == 0) {
  5904. /* Parse the PIP2 VERSION response into ttdata */
  5905. pt_pip2_ver_load_ttdata(cd, actual_read_len);
  5906. } else {
  5907. pt_debug(cd->dev, DL_ERROR,
  5908. "%s: Error in PIP2 VERSION Cmd status=%d\n",
  5909. __func__, status);
  5910. return status;
  5911. }
  5912. return rc;
  5913. }
  5914. /*******************************************************************************
  5915. * FUNCTION: pt_pip2_get_version
  5916. *
  5917. * SUMMARY: Protected call to pt_pip2_get_version_ by exclusive
  5918. * access to the DUT.
  5919. *
  5920. * RETURN:
  5921. * 0 = success
  5922. * !0 = failure
  5923. *
  5924. * PARAMETERS:
  5925. * *cd - pointer to core data structure
  5926. ******************************************************************************/
  5927. static int pt_pip2_get_version(struct pt_core_data *cd)
  5928. {
  5929. int rc;
  5930. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5931. if (rc < 0) {
  5932. pt_debug(cd->dev, DL_ERROR,
  5933. "%s: fail get exclusive ex=%p own=%p\n",
  5934. __func__, cd->exclusive_dev, cd->dev);
  5935. return rc;
  5936. }
  5937. rc = pt_pip2_get_version_(cd);
  5938. if (release_exclusive(cd, cd->dev) < 0)
  5939. pt_debug(cd->dev, DL_ERROR,
  5940. "%s: fail to release exclusive\n", __func__);
  5941. return rc;
  5942. }
  5943. /*******************************************************************************
  5944. * FUNCTION: _pt_request_active_pip_protocol
  5945. *
  5946. * SUMMARY: Get active PIP protocol version using the PIP2 version command.
  5947. * Function will return PIP version of BL or application based on
  5948. * when it's called.
  5949. *
  5950. * RETURN:
  5951. * 0 = success
  5952. * !0 = failure
  5953. *
  5954. * PARAMETERS:
  5955. * *dev - pointer to device structure
  5956. * protect - flag to run in protected mode
  5957. * *pip_version_major - pointer to store PIP major version
  5958. * *pip_version_minor - pointer to store PIP minor version
  5959. ******************************************************************************/
  5960. int _pt_request_active_pip_protocol(struct device *dev, int protect,
  5961. u8 *pip_version_major, u8 *pip_version_minor)
  5962. {
  5963. struct pt_core_data *cd = dev_get_drvdata(dev);
  5964. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  5965. int rc = 0;
  5966. struct pt_hid_output sys_info = {
  5967. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  5968. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  5969. };
  5970. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5971. /* Skip PIP2 command if DUT generation is confirmed */
  5972. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  5973. goto skip_pip2_command;
  5974. rc = pt_pip2_get_version_(cd);
  5975. if (!rc) {
  5976. *pip_version_major = ttdata->pip_ver_major;
  5977. *pip_version_minor = ttdata->pip_ver_minor;
  5978. pt_debug(dev, DL_INFO,
  5979. "%s: pip_version = %d.%d\n", __func__,
  5980. *pip_version_major, *pip_version_minor);
  5981. } else {
  5982. /*
  5983. * Legacy products do not support the pip2 protocol to get
  5984. * pip version. However, they do support the "get sysinfo"
  5985. * command to get pip version from FW, but the bootloader
  5986. * does not support it. This function will try "get sysinfo"
  5987. * command if the pip2 command failed but this cmd could also
  5988. * fail if DUT is stuck in bootloader mode.
  5989. */
  5990. pt_debug(dev, DL_INFO,
  5991. "%s: PIP2 no response rc = %d, try legacy cmd\n",
  5992. __func__, rc);
  5993. skip_pip2_command:
  5994. rc = pt_pip1_send_output_and_wait_(cd, &sys_info);
  5995. if (!rc) {
  5996. *pip_version_minor =
  5997. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET + 1];
  5998. *pip_version_major =
  5999. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  6000. pt_debug(dev, DL_INFO,
  6001. "%s: pip_version = %d.%d\n", __func__,
  6002. *pip_version_major, *pip_version_minor);
  6003. } else {
  6004. *pip_version_major = 0;
  6005. *pip_version_minor = 0;
  6006. pt_debug(dev, DL_ERROR,
  6007. "%s: pip_version Not Detected\n", __func__);
  6008. }
  6009. }
  6010. return rc;
  6011. }
  6012. EXPORT_SYMBOL_GPL(_pt_request_active_pip_protocol);
  6013. /*******************************************************************************
  6014. * FUNCTION: _pt_detect_dut_generation
  6015. *
  6016. * SUMMARY: Determine the generation of device that we are communicating with:
  6017. * DUT_PIP1_ONLY (Gen5 or Gen6)
  6018. * DUT_PIP2_CAPABLE (TC33xx or TT7xxx)
  6019. * The HID_DESC command is supported in Gen5/6 BL and FW as well as
  6020. * TT/TC FW. The packet ID in the descriptor, however, is unique when
  6021. * coming form the BL or the FW:
  6022. * Packet_ID in BL = HID_BL_REPORT_ID (0xFF)
  6023. * Packet_ID in FW = HID_APP_REPORT_ID (0xF7)
  6024. * This function will return a modified status if it detects the DUT
  6025. * is in the BL. In the case of a Gen5/6 BL, which also sends out a FW
  6026. * reset sentinel, the status is "corrected" from a FW to BL sentinel.
  6027. *
  6028. * RETURN:
  6029. * 0 = success
  6030. * !0 = failure
  6031. *
  6032. * PARAMETERS:
  6033. * *dev - pointer to device structure
  6034. * *status - pointer to status bitmask
  6035. * *dut_gen - pointer to store the dut_generation
  6036. * *mode - pointer to store the PT_MODE
  6037. ******************************************************************************/
  6038. static int _pt_detect_dut_generation(struct device *dev,
  6039. u32 *status, u8 *dut_gen, enum pt_mode *mode)
  6040. {
  6041. int rc = 0;
  6042. u8 dut_gen_tmp = DUT_UNKNOWN;
  6043. u8 mode_tmp = PT_MODE_UNKNOWN;
  6044. u8 attempt = 1;
  6045. u32 status_tmp = STARTUP_STATUS_START;
  6046. struct pt_core_data *cd = dev_get_drvdata(dev);
  6047. struct pt_hid_desc hid_desc;
  6048. memset(&hid_desc, 0, sizeof(hid_desc));
  6049. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6050. while (rc && attempt < 3) {
  6051. attempt++;
  6052. usleep_range(2000, 5000);
  6053. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6054. }
  6055. if (!rc && hid_desc.packet_id == HID_BL_REPORT_ID) {
  6056. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 BL */
  6057. mode_tmp = PT_MODE_BOOTLOADER;
  6058. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6059. } else if (!rc && hid_desc.packet_id == HID_APP_REPORT_ID) {
  6060. rc = pt_pip2_get_version_(cd);
  6061. if (!rc)
  6062. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC FW */
  6063. else
  6064. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 FW */
  6065. mode_tmp = PT_MODE_OPERATIONAL;
  6066. status_tmp = STARTUP_STATUS_FW_RESET_SENTINEL;
  6067. rc = 0; /* To return success instead of error code */
  6068. } else if (rc) {
  6069. rc = pt_pip2_get_version_(cd);
  6070. if (!rc) {
  6071. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC BL */
  6072. mode_tmp = PT_MODE_BOOTLOADER;
  6073. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6074. }
  6075. }
  6076. mutex_lock(&cd->system_lock);
  6077. if (dut_gen)
  6078. *dut_gen = dut_gen_tmp;
  6079. if (mode)
  6080. *mode = mode_tmp;
  6081. if (status)
  6082. *status = status_tmp;
  6083. mutex_unlock(&cd->system_lock);
  6084. #ifdef TTDL_DIAGNOSTICS
  6085. pt_debug(cd->dev, DL_INFO, "%s: Generation=%d Mode=%d\n",
  6086. __func__, dut_gen_tmp, mode_tmp);
  6087. #endif /* TTDL_DIAGNOSTICS */
  6088. return rc;
  6089. }
  6090. /*******************************************************************************
  6091. * FUNCTION: _pt_request_dut_generation
  6092. *
  6093. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6094. * to get current dut generation.
  6095. *
  6096. * NOTE: This function WILL NOT try to determine dut generation.
  6097. *
  6098. * RETURN:
  6099. * The current dut generation.
  6100. *
  6101. * PARAMETERS:
  6102. * *dev - pointer to device structure
  6103. ******************************************************************************/
  6104. static int _pt_request_dut_generation(struct device *dev)
  6105. {
  6106. struct pt_core_data *cd = dev_get_drvdata(dev);
  6107. return cd->active_dut_generation;
  6108. }
  6109. #define HW_VERSION_LEN_MAX 13
  6110. /*******************************************************************************
  6111. * FUNCTION: _legacy_generate_hw_version
  6112. *
  6113. * SUMMARY: Format chip information from struct ttdata (maintained by PIP1
  6114. * SYSINFO command) or struct bl_info (maintained by PIP1 BL INFORMATION
  6115. * command) to the hw_version.
  6116. *
  6117. * RETURN:
  6118. * 0 = success
  6119. * !0 = failure
  6120. *
  6121. * PARAMETERS:
  6122. * *cd - pointer to core data
  6123. * *hw_version - pointer to store the hardware version
  6124. ******************************************************************************/
  6125. static int _legacy_generate_hw_version(struct pt_core_data *cd,
  6126. char *hw_version)
  6127. {
  6128. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6129. if (cd->sysinfo.ready) {
  6130. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6131. ttdata->jtag_id_h, cd->pid_for_loader);
  6132. return 0;
  6133. } else if (cd->bl_info.ready) {
  6134. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6135. cd->bl_info.chip_id, cd->pid_for_loader);
  6136. return 0;
  6137. } else {
  6138. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6139. pt_debug(cd->dev, DL_ERROR,
  6140. "%s: SYSINFO and BL_INFO are not ready\n", __func__);
  6141. return -ENODATA;
  6142. }
  6143. }
  6144. /*******************************************************************************
  6145. * FUNCTION: _pip2_generate_hw_version
  6146. *
  6147. * SUMMARY: Format chip information from struct ttdata (maintained by PIP2
  6148. * VERSION command) to the hw_version.
  6149. *
  6150. * RETURN:
  6151. * 0 = success
  6152. * !0 = failure
  6153. *
  6154. * PARAMETERS:
  6155. * *cd - pointer to core data
  6156. * *hw_version - pointer to store the hardware version
  6157. ******************************************************************************/
  6158. static int _pip2_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6159. {
  6160. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6161. if (cd->app_pip_ver_ready | cd->bl_pip_ver_ready) {
  6162. snprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.%04X.%02X",
  6163. ttdata->chip_id, ttdata->chip_rev, cd->pid_for_loader);
  6164. return 0;
  6165. } else {
  6166. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6167. pt_debug(cd->dev, DL_ERROR,
  6168. "%s: PIP Version are not ready\n", __func__);
  6169. return -ENODATA;
  6170. }
  6171. }
  6172. /*******************************************************************************
  6173. * FUNCTION: pt_generate_hw_version
  6174. *
  6175. * SUMMARY: Wraaper function for both legacy and TT/TC products generate the
  6176. * hw_version from static data.
  6177. *
  6178. * RETURN:
  6179. * 0 = success
  6180. * !0 = failure
  6181. *
  6182. * PARAMETERS:
  6183. * *cd - pointer to core data
  6184. * *hw_version - pointer to store the hardware version
  6185. ******************************************************************************/
  6186. static int pt_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6187. {
  6188. int rc = 0;
  6189. if (!hw_version)
  6190. return -ENOMEM;
  6191. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  6192. rc = _legacy_generate_hw_version(cd, hw_version);
  6193. else if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  6194. rc = _pip2_generate_hw_version(cd, hw_version);
  6195. else {
  6196. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6197. rc = -ENODATA;
  6198. }
  6199. return rc;
  6200. }
  6201. /*******************************************************************************
  6202. * SUMMARY: Attempt to retrieve the HW version of the connected DUT
  6203. *
  6204. * NOTE: The calling function must ensure to free *hw_version
  6205. *
  6206. * RETURN:
  6207. * 0 = success
  6208. * !0 = Failure
  6209. *
  6210. * PARAMETERS:
  6211. * *dev - pointer to device structure
  6212. * *hw_version - pointer to where the hw_version string will be stored
  6213. ******************************************************************************/
  6214. static int _pt_request_hw_version(struct device *dev, char *hw_version)
  6215. {
  6216. int rc = 0;
  6217. u16 actual_read_len;
  6218. u16 pip_ver;
  6219. u8 rd_buf[256];
  6220. u8 status;
  6221. u8 index = PIP2_RESP_STATUS_OFFSET;
  6222. u8 return_data[8];
  6223. u8 panel_id;
  6224. struct pt_core_data *cd = dev_get_drvdata(dev);
  6225. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6226. if (!hw_version)
  6227. return -ENOMEM;
  6228. if (!cd->hw_detected) {
  6229. /* No HW detected */
  6230. rc = -ENODEV;
  6231. pt_debug(dev, DL_ERROR, "%s: no hardware is detected!\n",
  6232. __func__);
  6233. goto exit_error;
  6234. }
  6235. /* For Parade TC or TT parts */
  6236. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  6237. rc = _pt_request_pip2_send_cmd(dev,
  6238. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  6239. NULL, 0, rd_buf, &actual_read_len);
  6240. if (rc) {
  6241. pt_debug(dev, DL_ERROR,
  6242. "%s: Failed to send PIP2 VERSION cmd\n",
  6243. __func__);
  6244. goto exit_error;
  6245. }
  6246. status = rd_buf[index];
  6247. if (status == 0) {
  6248. pip_ver = 256 * rd_buf[index + 2] + rd_buf[index + 1];
  6249. /*
  6250. * BL PIP 2.02 and greater the version fields are
  6251. * swapped
  6252. */
  6253. if (pip_ver >= 0x0202) {
  6254. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6255. "%02X%02X.%02X%02X.FF",
  6256. rd_buf[index + 10], rd_buf[index + 9],
  6257. rd_buf[index + 8], rd_buf[index + 7]);
  6258. } else {
  6259. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6260. "%02X%02X.%02X%02X.FF",
  6261. rd_buf[index + 8], rd_buf[index + 7],
  6262. rd_buf[index + 10], rd_buf[index + 9]);
  6263. }
  6264. return STATUS_SUCCESS;
  6265. } else {
  6266. rc = status;
  6267. pt_debug(dev, DL_WARN,
  6268. "%s: PIP2 VERSION cmd response error\n",
  6269. __func__);
  6270. }
  6271. } else if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  6272. /*
  6273. * For Parade/Cypress legacy parts the RevID and FamilyID are
  6274. * hard coded to FFFF
  6275. */
  6276. if (cd->mode == PT_MODE_OPERATIONAL) {
  6277. rc = pt_hid_output_get_sysinfo(cd);
  6278. if (!rc) {
  6279. panel_id =
  6280. cd->sysinfo.sensing_conf_data.panel_id;
  6281. } else {
  6282. panel_id = PANEL_ID_NOT_ENABLED;
  6283. }
  6284. /* In FW - simply retrieve from ttdata struct */
  6285. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6286. "%04X.FFFF.%02X",
  6287. ttdata->jtag_id_h,
  6288. panel_id);
  6289. return STATUS_SUCCESS;
  6290. } else {
  6291. /*
  6292. * Return the stored value if PT_PANEL_ID_BY_BL
  6293. * is not supported while other feature is.
  6294. */
  6295. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  6296. rc = pt_hid_output_bl_get_information(
  6297. cd, return_data);
  6298. if (!rc) {
  6299. rc = pt_hid_output_bl_get_panel_id(
  6300. cd, &panel_id);
  6301. }
  6302. } else
  6303. panel_id = cd->pid_for_loader;
  6304. if (!rc) {
  6305. snprintf(hw_version,
  6306. HW_VERSION_LEN_MAX,
  6307. "%02X%02X.FFFF.%02X",
  6308. return_data[3], return_data[2],
  6309. panel_id);
  6310. return STATUS_SUCCESS;
  6311. }
  6312. }
  6313. } else {
  6314. /* Unknown generation */
  6315. rc = -ENODEV;
  6316. pt_debug(dev, DL_ERROR, "%s: generation is unknown!\n",
  6317. __func__);
  6318. }
  6319. exit_error:
  6320. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6321. return rc;
  6322. }
  6323. /*******************************************************************************
  6324. * FUNCTION: pt_start_wd_timer
  6325. *
  6326. * SUMMARY: Starts the TTDL watchdog timer if the timer interval is > 0
  6327. *
  6328. * RETURN: void
  6329. *
  6330. * PARAMETERS:
  6331. * *cd - pointer to core data
  6332. ******************************************************************************/
  6333. static void pt_start_wd_timer(struct pt_core_data *cd)
  6334. {
  6335. if (cd->watchdog_interval < 100) {
  6336. pt_debug(cd->dev, DL_ERROR,
  6337. "%s: WARNING: Invalid watchdog interval: %d\n",
  6338. __func__, cd->watchdog_interval);
  6339. return;
  6340. }
  6341. if (cd->watchdog_force_stop) {
  6342. pt_debug(cd->dev, DL_INFO,
  6343. "%s: TTDL WD Forced stop\n", __func__);
  6344. return;
  6345. }
  6346. mod_timer(&cd->watchdog_timer, jiffies +
  6347. msecs_to_jiffies(cd->watchdog_interval));
  6348. cd->watchdog_enabled = 1;
  6349. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Started\n", __func__);
  6350. }
  6351. /*******************************************************************************
  6352. * FUNCTION: pt_stop_wd_timer
  6353. *
  6354. * SUMMARY: Stops the TTDL watchdog timer if the timer interval is > 0
  6355. *
  6356. * RETURN: void
  6357. *
  6358. * PARAMETERS:
  6359. * *cd - pointer to core data
  6360. ******************************************************************************/
  6361. static void pt_stop_wd_timer(struct pt_core_data *cd)
  6362. {
  6363. if (!cd->watchdog_interval)
  6364. return;
  6365. /*
  6366. * Ensure we wait until the watchdog timer
  6367. * running on a different CPU finishes
  6368. */
  6369. del_timer_sync(&cd->watchdog_timer);
  6370. cancel_work_sync(&cd->watchdog_work);
  6371. del_timer_sync(&cd->watchdog_timer);
  6372. cd->watchdog_enabled = 0;
  6373. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Stopped\n", __func__);
  6374. }
  6375. /*******************************************************************************
  6376. * FUNCTION: pt_hw_soft_reset
  6377. *
  6378. * SUMMARY: Sends a PIP reset command to the DUT. Disable/re-enable the
  6379. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6380. * schedule an enum if it fires when the DUT is being reset.
  6381. * This can cause a double reset.
  6382. *
  6383. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6384. * the WD active could cause this function to fail!
  6385. *
  6386. * RETURN:
  6387. * 0 = success
  6388. * !0 = failure
  6389. *
  6390. * PARAMETERS:
  6391. * *cd - pointer to core data struct
  6392. * protect - flag to call protected or non-protected
  6393. ******************************************************************************/
  6394. static int pt_hw_soft_reset(struct pt_core_data *cd, int protect)
  6395. {
  6396. int rc = 0;
  6397. mutex_lock(&cd->system_lock);
  6398. cd->startup_status = STARTUP_STATUS_START;
  6399. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  6400. mutex_unlock(&cd->system_lock);
  6401. if (protect)
  6402. rc = pt_hid_cmd_reset(cd);
  6403. else
  6404. rc = pt_hid_cmd_reset_(cd);
  6405. if (rc < 0) {
  6406. pt_debug(cd->dev, DL_ERROR,
  6407. "%s: FAILED to execute SOFT reset\n", __func__);
  6408. return rc;
  6409. }
  6410. pt_debug(cd->dev, DL_INFO, "%s: SOFT reset successful\n",
  6411. __func__);
  6412. return 0;
  6413. }
  6414. /*******************************************************************************
  6415. * FUNCTION: pt_hw_hard_reset
  6416. *
  6417. * SUMMARY: Calls the platform xres function if it exists to perform a hard
  6418. * reset on the DUT by toggling the XRES gpio. Disable/re-enable the
  6419. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6420. * schedule an enum if it fires when the DUT is being reset.
  6421. * This can cause a double reset.
  6422. *
  6423. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6424. * the WD active could cause this function to fail!
  6425. *
  6426. * RETURN:
  6427. * 0 = success
  6428. * !0 = failure
  6429. *
  6430. * PARAMETERS:
  6431. * *cd - pointer to core data struct
  6432. ******************************************************************************/
  6433. static int pt_hw_hard_reset(struct pt_core_data *cd)
  6434. {
  6435. if (cd->cpdata->xres) {
  6436. cd->startup_status = STARTUP_STATUS_START;
  6437. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n",
  6438. __func__);
  6439. cd->cpdata->xres(cd->cpdata, cd->dev);
  6440. pt_debug(cd->dev, DL_WARN, "%s: executed HARD reset\n",
  6441. __func__);
  6442. return 0;
  6443. }
  6444. pt_debug(cd->dev, DL_ERROR,
  6445. "%s: FAILED to execute HARD reset\n", __func__);
  6446. return -ENODEV;
  6447. }
  6448. /*******************************************************************************
  6449. * FUNCTION: pt_dut_reset
  6450. *
  6451. * SUMMARY: Attempts to reset the DUT by a hard reset and if that fails a
  6452. * soft reset.
  6453. *
  6454. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6455. * the WD active could cause this function to fail!
  6456. * NOTE: "protect" flag is only used for soft reset.
  6457. *
  6458. * RETURN:
  6459. * 0 = success
  6460. * !0 = failure
  6461. *
  6462. * PARAMETERS:
  6463. * *cd - pointer to core data structure
  6464. * protect - flag to call protected or non-protected
  6465. ******************************************************************************/
  6466. static int pt_dut_reset(struct pt_core_data *cd, int protect)
  6467. {
  6468. int rc = 0;
  6469. pt_debug(cd->dev, DL_INFO, "%s: reset hw...\n", __func__);
  6470. mutex_lock(&cd->system_lock);
  6471. cd->hid_reset_cmd_state = 1;
  6472. rc = pt_hw_hard_reset(cd);
  6473. mutex_unlock(&cd->system_lock);
  6474. if (rc == -ENODEV) {
  6475. mutex_lock(&cd->system_lock);
  6476. cd->hid_reset_cmd_state = 0;
  6477. mutex_unlock(&cd->system_lock);
  6478. pt_debug(cd->dev, DL_ERROR,
  6479. "%s: Hard reset failed, try soft reset\n", __func__);
  6480. rc = pt_hw_soft_reset(cd, protect);
  6481. }
  6482. if (rc)
  6483. pt_debug(cd->dev, DL_ERROR, "%s: %s dev='%s' r=%d\n",
  6484. __func__, "Fail hw reset", dev_name(cd->dev), rc);
  6485. return rc;
  6486. }
  6487. /*******************************************************************************
  6488. * FUNCTION: pt_dut_reset_and_wait
  6489. *
  6490. * SUMMARY: Wrapper function for pt_dut_reset that waits for the reset to
  6491. * complete
  6492. *
  6493. * RETURN:
  6494. * 0 = success
  6495. * !0 = failure
  6496. *
  6497. * PARAMETERS:
  6498. * *cd - pointer to core data structure
  6499. ******************************************************************************/
  6500. static int pt_dut_reset_and_wait(struct pt_core_data *cd)
  6501. {
  6502. int rc = 0;
  6503. int t;
  6504. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  6505. if (rc < 0)
  6506. goto exit;
  6507. t = wait_event_timeout(cd->wait_q,
  6508. (cd->hid_reset_cmd_state == 0),
  6509. msecs_to_jiffies(PT_HID_CMD_DEFAULT_TIMEOUT));
  6510. if (IS_TMO(t)) {
  6511. #ifdef TTDL_DIAGNOSTICS
  6512. cd->bus_transmit_error_count++;
  6513. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  6514. #endif /* TTDL_DIAGNOSTICS */
  6515. pt_debug(cd->dev, DL_ERROR, "%s: reset timed out\n",
  6516. __func__);
  6517. rc = -ETIME;
  6518. goto exit;
  6519. }
  6520. exit:
  6521. return rc;
  6522. }
  6523. /*
  6524. * touch default parameters (from report descriptor) to resolve protocol for
  6525. * touch report
  6526. */
  6527. const struct pt_tch_abs_params tch_hdr_default[PT_TCH_NUM_HDR] = {
  6528. /* byte offset, size, min, max, bit offset, report */
  6529. {0x00, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* SCAN TIME */
  6530. {0x02, 0x01, 0x00, 0x20, 0x00, 0x01}, /* NUMBER OF RECORDS */
  6531. {0x02, 0x01, 0x00, 0x02, 0x05, 0x01}, /* LARGE OBJECT */
  6532. {0x03, 0x01, 0x00, 0x08, 0x00, 0x01}, /* NOISE EFFECT */
  6533. {0x03, 0x01, 0x00, 0x04, 0x06, 0x01}, /* REPORT_COUNTER */
  6534. };
  6535. /*
  6536. * button default parameters (from report descriptor) to resolve protocol for
  6537. * button report
  6538. */
  6539. const struct pt_tch_abs_params tch_abs_default[PT_TCH_NUM_ABS] = {
  6540. /* byte offset, size, min, max, bit offset, report */
  6541. {0x02, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* X */
  6542. {0x04, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* Y */
  6543. {0x06, 0x01, 0x00, 0x100, 0x00, 0x01}, /* P (Z) */
  6544. {0x01, 0x01, 0x00, 0x20, 0x00, 0x01}, /* TOUCH ID */
  6545. {0x01, 0x01, 0x00, 0x04, 0x05, 0x01}, /* EVENT ID */
  6546. {0x00, 0x01, 0x00, 0x08, 0x00, 0x01}, /* OBJECT ID */
  6547. {0x01, 0x01, 0x00, 0x02, 0x07, 0x01}, /* LIFTOFF */
  6548. {0x07, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MAJOR */
  6549. {0x08, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MINOR */
  6550. {0x09, 0x01, 0x00, 0x100, 0x00, 0x01}, /* ORIENTATION */
  6551. };
  6552. /*******************************************************************************
  6553. * FUNCTION: pt_init_pip_report_fields
  6554. *
  6555. * SUMMARY: Setup default values for touch/button report parsing.
  6556. *
  6557. * PARAMETERS:
  6558. * *cd - pointer to core data structure
  6559. ******************************************************************************/
  6560. static void pt_init_pip_report_fields(struct pt_core_data *cd)
  6561. {
  6562. struct pt_sysinfo *si = &cd->sysinfo;
  6563. memcpy(si->tch_hdr, tch_hdr_default, sizeof(tch_hdr_default));
  6564. memcpy(si->tch_abs, tch_abs_default, sizeof(tch_abs_default));
  6565. si->desc.tch_report_id = PT_PIP_TOUCH_REPORT_ID;
  6566. si->desc.tch_record_size = TOUCH_REPORT_SIZE;
  6567. si->desc.tch_header_size = TOUCH_INPUT_HEADER_SIZE;
  6568. si->desc.btn_report_id = PT_PIP_CAPSENSE_BTN_REPORT_ID;
  6569. cd->features.easywake = 1;
  6570. cd->features.noise_metric = 1;
  6571. cd->features.tracking_heatmap = 1;
  6572. cd->features.sensor_data = 1;
  6573. }
  6574. /*******************************************************************************
  6575. * FUNCTION: pt_get_mode
  6576. *
  6577. * SUMMARY: Determine the current mode from the contents of a HID descriptor
  6578. * message
  6579. *
  6580. * RETURN: Enum of the current mode
  6581. *
  6582. * PARAMETERS:
  6583. * *cd - pointer to the Core Data structure
  6584. * protect - run command in protected mode
  6585. * *mode - pointer to store the retrieved mode
  6586. ******************************************************************************/
  6587. static int pt_get_mode(struct pt_core_data *cd, struct pt_hid_desc *desc)
  6588. {
  6589. if (desc->packet_id == HID_APP_REPORT_ID)
  6590. return PT_MODE_OPERATIONAL;
  6591. else if (desc->packet_id == HID_BL_REPORT_ID)
  6592. return PT_MODE_BOOTLOADER;
  6593. return PT_MODE_UNKNOWN;
  6594. }
  6595. /*******************************************************************************
  6596. * FUNCTION: _pt_request_get_mode
  6597. *
  6598. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6599. * to determine the current mode of the DUT by use of the Get HID
  6600. * Descriptor command.
  6601. *
  6602. * RETURN:
  6603. * 0 = success
  6604. * !0 = failure
  6605. *
  6606. * PARAMETERS:
  6607. * *dev - pointer to device structure
  6608. * protect - run command in protected mode
  6609. * *mode - pointer to store the retrieved mode
  6610. ******************************************************************************/
  6611. static int _pt_request_get_mode(struct device *dev, int protect, u8 *mode)
  6612. {
  6613. struct pt_core_data *cd = dev_get_drvdata(dev);
  6614. struct pt_hid_desc hid_desc;
  6615. int rc = 0;
  6616. memset(&hid_desc, 0, sizeof(hid_desc));
  6617. if (protect)
  6618. rc = pt_get_hid_descriptor(cd, &hid_desc);
  6619. else
  6620. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6621. if (rc)
  6622. *mode = PT_MODE_UNKNOWN;
  6623. else
  6624. *mode = pt_get_mode(cd, &hid_desc);
  6625. return rc;
  6626. }
  6627. /*******************************************************************************
  6628. * FUNCTION: pt_queue_enum_
  6629. *
  6630. * SUMMARY: Queues a TTDL enum by scheduling work with the pt_enum_with_dut()
  6631. * function. It won't try to add/delete sysfs node or modules.
  6632. *
  6633. * RETURN: void
  6634. *
  6635. * PARAMETERS:
  6636. * *cd - pointer to core data
  6637. ******************************************************************************/
  6638. static void pt_queue_enum_(struct pt_core_data *cd)
  6639. {
  6640. if (cd->startup_state == STARTUP_NONE) {
  6641. cd->startup_state = STARTUP_QUEUED;
  6642. #ifdef TTDL_DIAGNOSTICS
  6643. if (!cd->bridge_mode)
  6644. schedule_work(&cd->enum_work);
  6645. else
  6646. cd->startup_state = STARTUP_NONE;
  6647. #else
  6648. schedule_work(&cd->enum_work);
  6649. #endif
  6650. pt_debug(cd->dev, DL_INFO,
  6651. "%s: enum_work queued\n", __func__);
  6652. } else {
  6653. pt_debug(cd->dev, DL_WARN,
  6654. "%s: Enum not queued - startup_state = %d\n",
  6655. __func__, cd->startup_state);
  6656. }
  6657. }
  6658. /*******************************************************************************
  6659. * FUNCTION: pt_queue_enum
  6660. *
  6661. * SUMMARY: Queues a TTDL enum within a mutex lock
  6662. *
  6663. * RETURN: void
  6664. *
  6665. * PARAMETERS:
  6666. * *cd - pointer to core data
  6667. ******************************************************************************/
  6668. static void pt_queue_enum(struct pt_core_data *cd)
  6669. {
  6670. mutex_lock(&cd->system_lock);
  6671. pt_queue_enum_(cd);
  6672. mutex_unlock(&cd->system_lock);
  6673. }
  6674. static void remove_sysfs_and_modules(struct device *dev);
  6675. /*******************************************************************************
  6676. * FUNCTION: pt_queue_restart
  6677. *
  6678. * SUMMARY: Queues a TTDL restart within a mutex lock
  6679. *
  6680. * RETURN: void
  6681. *
  6682. * PARAMETERS:
  6683. * *cd - pointer to core data
  6684. * remove_sysfs_module - True: remove all DUT relative sysfs nodes and modules
  6685. * False: will not perform remove action
  6686. ******************************************************************************/
  6687. static void pt_queue_restart(struct pt_core_data *cd)
  6688. {
  6689. mutex_lock(&cd->system_lock);
  6690. if (cd->startup_state == STARTUP_NONE) {
  6691. cd->startup_state = STARTUP_QUEUED;
  6692. schedule_work(&cd->ttdl_restart_work);
  6693. pt_debug(cd->dev, DL_INFO,
  6694. "%s: pt_ttdl_restart queued\n", __func__);
  6695. } else {
  6696. pt_debug(cd->dev, DL_INFO, "%s: startup_state = %d\n",
  6697. __func__, cd->startup_state);
  6698. }
  6699. mutex_unlock(&cd->system_lock);
  6700. }
  6701. /*******************************************************************************
  6702. * FUNCTION: call_atten_cb
  6703. *
  6704. * SUMMARY: Iterate over attention list call the function that registered.
  6705. *
  6706. * RETURN: void
  6707. *
  6708. * PARAMETERS:
  6709. * *cd - pointer to core data
  6710. * type - type of attention list
  6711. * mode - condition for execution
  6712. ******************************************************************************/
  6713. static void call_atten_cb(struct pt_core_data *cd,
  6714. enum pt_atten_type type, int mode)
  6715. {
  6716. struct atten_node *atten, *atten_n;
  6717. pt_debug(cd->dev, DL_DEBUG, "%s: check list type=%d mode=%d\n",
  6718. __func__, type, mode);
  6719. spin_lock(&cd->spinlock);
  6720. list_for_each_entry_safe(atten, atten_n,
  6721. &cd->atten_list[type], node) {
  6722. if (!mode || atten->mode & mode) {
  6723. spin_unlock(&cd->spinlock);
  6724. pt_debug(cd->dev, DL_DEBUG,
  6725. "%s: attention for '%s'",
  6726. __func__, dev_name(atten->dev));
  6727. atten->func(atten->dev);
  6728. spin_lock(&cd->spinlock);
  6729. }
  6730. }
  6731. spin_unlock(&cd->spinlock);
  6732. }
  6733. /*******************************************************************************
  6734. * FUNCTION: start_fw_upgrade
  6735. *
  6736. * SUMMARY: Calling "PT_ATTEN_LOADER" attention list that loader registered to
  6737. * start firmware upgrade.
  6738. *
  6739. * RETURN:
  6740. * 0 = success
  6741. *
  6742. * PARAMETERS:
  6743. * *data - pointer to core data
  6744. ******************************************************************************/
  6745. static int start_fw_upgrade(void *data)
  6746. {
  6747. struct pt_core_data *cd = (struct pt_core_data *)data;
  6748. call_atten_cb(cd, PT_ATTEN_LOADER, 0);
  6749. return 0;
  6750. }
  6751. /*******************************************************************************
  6752. * FUNCTION: pt_put_device_into_easy_wakeup_
  6753. *
  6754. * SUMMARY: Call the enter_easywake_state function and set the device into easy
  6755. * wake up state.
  6756. *
  6757. * RETURN:
  6758. * 0 = success
  6759. * !0 = failure
  6760. *
  6761. * PARAMETERS:
  6762. * *cd - pointer to core data
  6763. ******************************************************************************/
  6764. static int pt_put_device_into_easy_wakeup_(struct pt_core_data *cd)
  6765. {
  6766. int rc = 0;
  6767. u8 status = 0;
  6768. mutex_lock(&cd->system_lock);
  6769. cd->wait_until_wake = 0;
  6770. mutex_unlock(&cd->system_lock);
  6771. rc = pt_hid_output_enter_easywake_state_(cd,
  6772. cd->easy_wakeup_gesture, &status);
  6773. if (rc || status == 0)
  6774. return -EBUSY;
  6775. return rc;
  6776. }
  6777. /*******************************************************************************
  6778. * FUNCTION: pt_put_device_into_deep_sleep_
  6779. *
  6780. * SUMMARY: Call the set_power function and set the DUT to deep sleep
  6781. *
  6782. * RETURN:
  6783. * 0 = success
  6784. * !0 = error
  6785. *
  6786. * PARAMETERS:
  6787. * *cd - pointer to core data
  6788. ******************************************************************************/
  6789. static int pt_put_device_into_deep_sleep_(struct pt_core_data *cd)
  6790. {
  6791. int rc = 0;
  6792. rc = pt_hid_cmd_set_power_(cd, HID_POWER_SLEEP);
  6793. if (rc)
  6794. rc = -EBUSY;
  6795. return rc;
  6796. }
  6797. /*******************************************************************************
  6798. * FUNCTION: pt_put_device_into_deep_standby_
  6799. *
  6800. * SUMMARY: Call the set_power function and set the DUT to Deep Standby
  6801. *
  6802. * RETURN:
  6803. * 0 = success
  6804. * !0 = error
  6805. *
  6806. * PARAMETERS:
  6807. * *cd - pointer to core data
  6808. ******************************************************************************/
  6809. static int pt_put_device_into_deep_standby_(struct pt_core_data *cd)
  6810. {
  6811. int rc = 0;
  6812. rc = pt_hid_cmd_set_power_(cd, HID_POWER_STANDBY);
  6813. if (rc)
  6814. rc = -EBUSY;
  6815. return rc;
  6816. }
  6817. /*******************************************************************************
  6818. * FUNCTION: pt_core_poweroff_device_
  6819. *
  6820. * SUMMARY: Disable IRQ and HW power down the device.
  6821. *
  6822. * RETURN:
  6823. * 0 = success
  6824. * !0 = failure
  6825. *
  6826. * PARAMETERS:
  6827. * *cd - pointer to core data
  6828. ******************************************************************************/
  6829. static int pt_core_poweroff_device_(struct pt_core_data *cd)
  6830. {
  6831. int rc;
  6832. if (cd->irq_enabled) {
  6833. cd->irq_enabled = false;
  6834. disable_irq_nosync(cd->irq);
  6835. }
  6836. rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, 0);
  6837. if (rc < 0)
  6838. pt_debug(cd->dev, DL_ERROR, "%s: HW Power down fails r=%d\n",
  6839. __func__, rc);
  6840. return rc;
  6841. }
  6842. /*******************************************************************************
  6843. * FUNCTION: pt_core_sleep_
  6844. *
  6845. * SUMMARY: Suspend the device with power off or deep sleep based on the
  6846. * configuration in the core platform data structure.
  6847. *
  6848. * RETURN:
  6849. * 0 = success
  6850. * !0 = failure
  6851. *
  6852. * PARAMETERS:
  6853. * *cd - pointer to core data
  6854. ******************************************************************************/
  6855. static int pt_core_sleep_(struct pt_core_data *cd)
  6856. {
  6857. int rc = 0;
  6858. mutex_lock(&cd->system_lock);
  6859. if (cd->sleep_state == SS_SLEEP_OFF) {
  6860. cd->sleep_state = SS_SLEEPING;
  6861. } else {
  6862. mutex_unlock(&cd->system_lock);
  6863. return 1;
  6864. }
  6865. mutex_unlock(&cd->system_lock);
  6866. /* Ensure watchdog and startup works stopped */
  6867. pt_stop_wd_timer(cd);
  6868. cancel_work_sync(&cd->enum_work);
  6869. pt_stop_wd_timer(cd);
  6870. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  6871. rc = pt_put_device_into_easy_wakeup_(cd);
  6872. else if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP)
  6873. rc = pt_core_poweroff_device_(cd);
  6874. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  6875. rc = pt_put_device_into_deep_standby_(cd);
  6876. else
  6877. rc = pt_put_device_into_deep_sleep_(cd);
  6878. mutex_lock(&cd->system_lock);
  6879. cd->sleep_state = SS_SLEEP_ON;
  6880. mutex_unlock(&cd->system_lock);
  6881. return rc;
  6882. }
  6883. /*******************************************************************************
  6884. * FUNCTION: pt_core_sleep
  6885. *
  6886. * SUMMARY: Protected call to pt_core_sleep_ by exclusive access to the DUT.
  6887. *
  6888. * RETURN:
  6889. * 0 = success
  6890. * !0 = failure
  6891. *
  6892. * PARAMETERS:
  6893. * *cd - pointer to core data
  6894. ******************************************************************************/
  6895. static int pt_core_sleep(struct pt_core_data *cd)
  6896. {
  6897. int rc = 0;
  6898. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6899. if (rc < 0) {
  6900. pt_debug(cd->dev, DL_ERROR,
  6901. "%s: fail get exclusive ex=%p own=%p\n",
  6902. __func__, cd->exclusive_dev, cd->dev);
  6903. return rc;
  6904. }
  6905. rc = pt_core_sleep_(cd);
  6906. if (release_exclusive(cd, cd->dev) < 0)
  6907. pt_debug(cd->dev, DL_ERROR,
  6908. "%s: fail to release exclusive\n", __func__);
  6909. else
  6910. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  6911. __func__);
  6912. return rc;
  6913. }
  6914. /*******************************************************************************
  6915. * FUNCTION: pt_wakeup_host
  6916. *
  6917. * SUMMARY: Check wake up report and call the PT_ATTEN_WAKE attention list.
  6918. *
  6919. * NOTE: TSG5 EasyWake and TSG6 EasyWake use different protocol.
  6920. *
  6921. * RETURN:
  6922. * 0 = success
  6923. * !0 = failure
  6924. *
  6925. * PARAMETERS:
  6926. * *cd - pointer to core data
  6927. ******************************************************************************/
  6928. static int pt_wakeup_host(struct pt_core_data *cd)
  6929. {
  6930. #ifndef EASYWAKE_TSG6
  6931. /* TSG5 EasyWake */
  6932. int rc = 0;
  6933. int event_id;
  6934. int size = get_unaligned_le16(&cd->input_buf[0]);
  6935. /* Validate report */
  6936. if (size != 4 || cd->input_buf[2] != 4)
  6937. rc = -EINVAL;
  6938. event_id = cd->input_buf[3];
  6939. pt_debug(cd->dev, DL_INFO, "%s: e=%d, rc=%d\n",
  6940. __func__, event_id, rc);
  6941. if (rc) {
  6942. pt_core_sleep_(cd);
  6943. goto exit;
  6944. }
  6945. /* attention WAKE */
  6946. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  6947. exit:
  6948. return rc;
  6949. #else
  6950. /* TSG6 FW1.3 EasyWake */
  6951. int rc = 0;
  6952. int i = 0;
  6953. int report_length;
  6954. /* Validate report */
  6955. if (cd->input_buf[2] != PT_PIP_WAKEUP_REPORT_ID) {
  6956. rc = -EINVAL;
  6957. pt_core_sleep_(cd);
  6958. goto exit;
  6959. }
  6960. /* Get gesture id and gesture data length */
  6961. cd->gesture_id = cd->input_buf[3];
  6962. report_length = (cd->input_buf[1] << 8) | (cd->input_buf[0]);
  6963. cd->gesture_data_length = report_length - 4;
  6964. pt_debug(cd->dev, DL_INFO,
  6965. "%s: gesture_id = %d, gesture_data_length = %d\n",
  6966. __func__, cd->gesture_id, cd->gesture_data_length);
  6967. for (i = 0; i < cd->gesture_data_length; i++)
  6968. cd->gesture_data[i] = cd->input_buf[4 + i];
  6969. /* attention WAKE */
  6970. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  6971. exit:
  6972. return rc;
  6973. #endif
  6974. }
  6975. /*******************************************************************************
  6976. * FUNCTION: pt_get_touch_axis
  6977. *
  6978. * SUMMARY: Function to calculate touch axis
  6979. *
  6980. * PARAMETERS:
  6981. * *cd - pointer to core data structure
  6982. * *axis - pointer to axis calculation result
  6983. * size - size in bytes
  6984. * max - max value of result
  6985. * *xy_data - pointer to input data to be parsed
  6986. * bofs - bit offset
  6987. ******************************************************************************/
  6988. static void pt_get_touch_axis(struct pt_core_data *cd,
  6989. int *axis, int size, int max, u8 *data, int bofs)
  6990. {
  6991. int nbyte;
  6992. int next;
  6993. for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
  6994. *axis = *axis + ((data[next] >> bofs) << (nbyte * 8));
  6995. next++;
  6996. }
  6997. *axis &= max - 1;
  6998. }
  6999. /*******************************************************************************
  7000. * FUNCTION: move_tracking_heatmap_data
  7001. *
  7002. * SUMMARY: Move the valid tracking heatmap data from the input buffer into the
  7003. * system information structure, xy_mode and xy_data.
  7004. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7005. * the tthe_tuner sysfs node under the label "THM"
  7006. *
  7007. * RETURN:
  7008. * 0 = success
  7009. * !0 = failure
  7010. *
  7011. * PARAMETERS:
  7012. * *cd - pointer to core data
  7013. * *si - pointer to the system information structure
  7014. ******************************************************************************/
  7015. static int move_tracking_heatmap_data(struct pt_core_data *cd,
  7016. struct pt_sysinfo *si)
  7017. {
  7018. #ifdef TTHE_TUNER_SUPPORT
  7019. int size = get_unaligned_le16(&cd->input_buf[0]);
  7020. if (size)
  7021. tthe_print(cd, cd->input_buf, size, "THM=");
  7022. #endif
  7023. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7024. return 0;
  7025. }
  7026. /*******************************************************************************
  7027. * FUNCTION: move_sensor_data
  7028. *
  7029. * SUMMARY: Move the valid sensor data from the input buffer into the system
  7030. * information structure, xy_mode and xy_data.
  7031. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7032. * the tthe_tuner sysfs node under the label "sensor_monitor"
  7033. *
  7034. * RETURN:
  7035. * 0 = success
  7036. * !0 = failure
  7037. *
  7038. * PARAMETERS:
  7039. * *cd - pointer to core data
  7040. * *si - pointer to the system information structure
  7041. ******************************************************************************/
  7042. static int move_sensor_data(struct pt_core_data *cd,
  7043. struct pt_sysinfo *si)
  7044. {
  7045. #ifdef TTHE_TUNER_SUPPORT
  7046. int size = get_unaligned_le16(&cd->input_buf[0]);
  7047. if (size)
  7048. tthe_print(cd, cd->input_buf, size, "sensor_monitor=");
  7049. #endif
  7050. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7051. return 0;
  7052. }
  7053. /*******************************************************************************
  7054. * FUNCTION: move_button_data
  7055. *
  7056. * SUMMARY: Move the valid button data from the input buffer into the system
  7057. * information structure, xy_mode and xy_data.
  7058. * - If TTHE_TUNER_SUPPORT is defined print the raw button data into
  7059. * the tthe_tuner sysfs node under the label "OpModeData"
  7060. *
  7061. * RETURN:
  7062. * 0 = success
  7063. * !0 = failure
  7064. *
  7065. * PARAMETERS:
  7066. * *cd - pointer to core data
  7067. * *si - pointer to the system information structure
  7068. ******************************************************************************/
  7069. static int move_button_data(struct pt_core_data *cd,
  7070. struct pt_sysinfo *si)
  7071. {
  7072. #ifdef TTHE_TUNER_SUPPORT
  7073. int size = get_unaligned_le16(&cd->input_buf[0]);
  7074. if (size)
  7075. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7076. #endif
  7077. memcpy(si->xy_mode, cd->input_buf, BTN_INPUT_HEADER_SIZE);
  7078. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode, BTN_INPUT_HEADER_SIZE,
  7079. "xy_mode");
  7080. memcpy(si->xy_data, &cd->input_buf[BTN_INPUT_HEADER_SIZE],
  7081. BTN_REPORT_SIZE);
  7082. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, BTN_REPORT_SIZE,
  7083. "xy_data");
  7084. return 0;
  7085. }
  7086. /*******************************************************************************
  7087. * FUNCTION: move_touch_data
  7088. *
  7089. * SUMMARY: Move the valid touch data from the input buffer into the system
  7090. * information structure, xy_mode and xy_data.
  7091. * - If TTHE_TUNER_SUPPORT is defined print the raw touch data into
  7092. * the tthe_tuner sysfs node under the label "OpModeData"
  7093. *
  7094. * RETURN:
  7095. * 0 = success
  7096. * !0 = failure
  7097. *
  7098. * PARAMETERS:
  7099. * *cd - pointer to core data
  7100. * *si - pointer to the system information structure
  7101. ******************************************************************************/
  7102. static int move_touch_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7103. {
  7104. int max_tch = si->sensing_conf_data.max_tch;
  7105. int num_cur_tch;
  7106. int length;
  7107. struct pt_tch_abs_params *tch = &si->tch_hdr[PT_TCH_NUM];
  7108. #ifdef TTHE_TUNER_SUPPORT
  7109. int size = get_unaligned_le16(&cd->input_buf[0]);
  7110. if (size)
  7111. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7112. #endif
  7113. memcpy(si->xy_mode, cd->input_buf, si->desc.tch_header_size);
  7114. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode,
  7115. si->desc.tch_header_size, "xy_mode");
  7116. pt_get_touch_axis(cd, &num_cur_tch, tch->size,
  7117. tch->max, si->xy_mode + 3 + tch->ofs, tch->bofs);
  7118. if (unlikely(num_cur_tch > max_tch))
  7119. num_cur_tch = max_tch;
  7120. length = num_cur_tch * si->desc.tch_record_size;
  7121. memcpy(si->xy_data, &cd->input_buf[si->desc.tch_header_size], length);
  7122. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, length, "xy_data");
  7123. return 0;
  7124. }
  7125. /*******************************************************************************
  7126. * FUNCTION: move_hid_pen_data
  7127. *
  7128. * SUMMARY: TODO Move the valid pen data from the input buffer into the system
  7129. * information structure, xy_mode and xy_data.
  7130. * - If TTHE_TUNER_SUPPORT is defined print the raw pen data into
  7131. * the tthe_tuner sysfs node under the label "HID" starting with the
  7132. * report ID.
  7133. *
  7134. * RETURN:
  7135. * 0 = success
  7136. * !0 = failure
  7137. *
  7138. * PARAMETERS:
  7139. * *cd - pointer to core data
  7140. * *si - pointer to the system information structure
  7141. ******************************************************************************/
  7142. static int move_hid_pen_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7143. {
  7144. #ifdef TTHE_TUNER_SUPPORT
  7145. int size = get_unaligned_le16(&cd->input_buf[0]);
  7146. if (size) {
  7147. /*
  7148. * HID over USB does not require the two byte length field, so
  7149. * this should print from input_buf[2] but to keep both finger
  7150. * and pen reports the same the length is included
  7151. */
  7152. if (cd->tthe_hid_usb_format == PT_FEATURE_ENABLE)
  7153. tthe_print(cd, &(cd->input_buf[2]), size - 2,
  7154. "HID-USB=");
  7155. else
  7156. tthe_print(cd, &(cd->input_buf[0]), size,
  7157. "HID-I2C=");
  7158. }
  7159. #endif
  7160. pt_pr_buf(cd->dev, DL_INFO, (u8 *)&(cd->input_buf[0]), size, "HID Pen");
  7161. return 0;
  7162. }
  7163. /*******************************************************************************
  7164. * FUNCTION: parse_touch_input
  7165. *
  7166. * SUMMARY: Parse the touch report and take action based on the touch
  7167. * report_id.
  7168. *
  7169. * RETURN:
  7170. * 0 = success
  7171. * !0 = failure
  7172. *
  7173. * PARAMETERS:
  7174. * *cd - pointer to core data
  7175. * size - size of touch record
  7176. ******************************************************************************/
  7177. static int parse_touch_input(struct pt_core_data *cd, int size)
  7178. {
  7179. struct pt_sysinfo *si = &cd->sysinfo;
  7180. int report_id = cd->input_buf[2];
  7181. int rc = -EINVAL;
  7182. pt_debug(cd->dev, DL_DEBUG, "%s: Received touch report\n",
  7183. __func__);
  7184. if (!si->ready) {
  7185. pt_debug(cd->dev, DL_ERROR,
  7186. "%s: Need system information to parse touches\n",
  7187. __func__);
  7188. return 0;
  7189. }
  7190. if (!si->xy_mode || !si->xy_data)
  7191. return rc;
  7192. if (report_id == PT_PIP_TOUCH_REPORT_ID)
  7193. rc = move_touch_data(cd, si);
  7194. else if (report_id == PT_HID_PEN_REPORT_ID)
  7195. rc = move_hid_pen_data(cd, si);
  7196. else if (report_id == PT_PIP_CAPSENSE_BTN_REPORT_ID)
  7197. rc = move_button_data(cd, si);
  7198. else if (report_id == PT_PIP_SENSOR_DATA_REPORT_ID)
  7199. rc = move_sensor_data(cd, si);
  7200. else if (report_id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7201. rc = move_tracking_heatmap_data(cd, si);
  7202. if (rc)
  7203. return rc;
  7204. /* attention IRQ */
  7205. call_atten_cb(cd, PT_ATTEN_IRQ, cd->mode);
  7206. return 0;
  7207. }
  7208. /*******************************************************************************
  7209. * FUNCTION: parse_command_input
  7210. *
  7211. * SUMMARY: Move the response data from the input buffer to the response buffer
  7212. *
  7213. * RETURN:
  7214. * 0 = success
  7215. * !0 = failure
  7216. *
  7217. * PARAMETERS:
  7218. * *cd - pointer to core data
  7219. * size - size of response data
  7220. ******************************************************************************/
  7221. static int parse_command_input(struct pt_core_data *cd, int size)
  7222. {
  7223. pt_debug(cd->dev, DL_DEBUG, "%s: Received cmd interrupt\n",
  7224. __func__);
  7225. memcpy(cd->response_buf, cd->input_buf, size);
  7226. #if defined(TTHE_TUNER_SUPPORT) && defined(TTDL_DIAGNOSTICS)
  7227. if (size && cd->show_tt_data) {
  7228. if (cd->pip2_prot_active)
  7229. tthe_print(cd, cd->input_buf, size, "TT_DATA_PIP2=");
  7230. else
  7231. tthe_print(cd, cd->input_buf, size, "TT_DATA=");
  7232. }
  7233. #endif
  7234. mutex_lock(&cd->system_lock);
  7235. cd->hid_cmd_state = 0;
  7236. mutex_unlock(&cd->system_lock);
  7237. wake_up(&cd->wait_q);
  7238. return 0;
  7239. }
  7240. /*******************************************************************************
  7241. * FUNCTION: pt_allow_enumeration
  7242. *
  7243. * SUMMARY: Determine if an enumeration or fully re-probe should perform when
  7244. * FW sentinel is seen.
  7245. *
  7246. * RETURN:
  7247. * true = allow enumeration or fully re-probe
  7248. * false = skip enumeration and fully re-probe
  7249. *
  7250. * PARAMETERS:
  7251. * *cd - pointer to core data
  7252. ******************************************************************************/
  7253. static inline bool pt_allow_enumeration(struct pt_core_data *cd)
  7254. {
  7255. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7256. (!cd->hid_reset_cmd_state) &&
  7257. (cd->core_probe_complete) &&
  7258. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7259. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7260. (cd->mode == PT_MODE_OPERATIONAL)) {
  7261. return true;
  7262. }
  7263. if ((!cd->hid_reset_cmd_state) &&
  7264. (cd->core_probe_complete) &&
  7265. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7266. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7267. (cd->active_dut_generation != DUT_PIP1_ONLY)) {
  7268. return true;
  7269. }
  7270. pt_debug(cd->dev, DL_INFO,
  7271. "%s: Dissallow - %s=%d %s=%d %s=0x%02X %s=%d\n",
  7272. __func__,
  7273. "hid_reset_cmd_state(0)", cd->hid_reset_cmd_state,
  7274. "core_probe_complete(1)", cd->core_probe_complete,
  7275. "hid_cmd_state(Not 0x02 or 0x39)", cd->hid_cmd_state,
  7276. "active_dut_gen(0,2)", cd->active_dut_generation);
  7277. return false;
  7278. }
  7279. /*******************************************************************************
  7280. * FUNCTION: pt_is_touch_report
  7281. *
  7282. * SUMMARY: Determine if a report ID should be treated as a touch report
  7283. *
  7284. * RETURN:
  7285. * true = report ID is a touch report
  7286. * false = report ID is not a touch report
  7287. *
  7288. * PARAMETERS:
  7289. * id - Report ID
  7290. ******************************************************************************/
  7291. static bool pt_is_touch_report(int id)
  7292. {
  7293. if (id == PT_PIP_TOUCH_REPORT_ID ||
  7294. id == PT_HID_PEN_REPORT_ID ||
  7295. id == PT_PIP_CAPSENSE_BTN_REPORT_ID ||
  7296. id == PT_PIP_SENSOR_DATA_REPORT_ID ||
  7297. id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7298. return true;
  7299. else
  7300. return false;
  7301. }
  7302. /*******************************************************************************
  7303. * FUNCTION: pt_parse_input
  7304. *
  7305. * SUMMARY: Parse the input data read from DUT due to IRQ. Handle data based
  7306. * on if its a response to a command or asynchronous touch data / reset
  7307. * sentinel. PIP2.x messages have additional error checking that is
  7308. * parsed (SEQ match from cmd to rsp, CRC valid).
  7309. * Look for special packets based on unique lengths:
  7310. * 0 bytes - APP(FW) reset sentinel or Gen5/6 BL sentinel
  7311. * 2 bytes - Empty buffer (PIP 1.7 and earlier)
  7312. * 11 bytes - possible PIP2.x reset sentinel (TAG and SEQ must = 0)
  7313. * 0xFFXX - Empty buffer (PIP 1.7+)
  7314. * Queue a startup after any asynchronous FW reset sentinel is seen, unless
  7315. * the initial probe has not yet been done.
  7316. *
  7317. * RETURN:
  7318. * 0 = success
  7319. * !0 = failure
  7320. *
  7321. * PARAMETERS:
  7322. * *cd - pointer to core data
  7323. ******************************************************************************/
  7324. static int pt_parse_input(struct pt_core_data *cd)
  7325. {
  7326. int report_id;
  7327. int cmd_id;
  7328. int is_command = 0;
  7329. int size;
  7330. int print_size;
  7331. bool touch_report = true;
  7332. unsigned short calc_crc;
  7333. unsigned short resp_crc;
  7334. cd->fw_sys_mode_in_standby_state = false;
  7335. size = get_unaligned_le16(&cd->input_buf[0]);
  7336. print_size = size;
  7337. pt_debug(cd->dev, DL_DEBUG, "<<< %s: IRQ Triggered, read len [%d]\n",
  7338. __func__, print_size);
  7339. if (print_size <= PT_MAX_INPUT)
  7340. pt_pr_buf(cd->dev, DL_DEBUG, cd->input_buf, print_size,
  7341. "<<< Read buf");
  7342. if (size == 0 ||
  7343. (size == 11 &&
  7344. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] &
  7345. PIP2_RESP_SEQUENCE_MASK) == 0 &&
  7346. (cd->input_buf[PIP2_RESP_REPORT_ID_OFFSET] &
  7347. PIP2_CMD_COMMAND_ID_MASK) ==
  7348. PIP2_CMD_ID_STATUS)) {
  7349. touch_report = false;
  7350. cd->hw_detected = true;
  7351. cd->bl_pip_ver_ready = false;
  7352. cd->app_pip_ver_ready = false;
  7353. if (size == 0) {
  7354. mutex_lock(&cd->system_lock);
  7355. cd->pip2_prot_active = false;
  7356. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  7357. /*
  7358. * For Gen5/6 this sentinel could be from
  7359. * the BL or FW. Attempt to set the correct
  7360. * mode based on the previous PIP command.
  7361. */
  7362. if (cd->hid_cmd_state ==
  7363. PIP1_BL_CMD_ID_LAUNCH_APP + 1) {
  7364. cd->mode = PT_MODE_OPERATIONAL;
  7365. cd->startup_status =
  7366. STARTUP_STATUS_FW_RESET_SENTINEL;
  7367. } else if (cd->hid_cmd_state ==
  7368. PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7369. cd->hid_reset_cmd_state) {
  7370. cd->mode = PT_MODE_BOOTLOADER;
  7371. cd->startup_status =
  7372. STARTUP_STATUS_BL_RESET_SENTINEL;
  7373. } else {
  7374. cd->mode = PT_MODE_UNKNOWN;
  7375. cd->startup_status =
  7376. STARTUP_STATUS_START;
  7377. }
  7378. cd->fw_system_mode = FW_SYS_MODE_UNDEFINED;
  7379. pt_debug(cd->dev, DL_INFO,
  7380. "%s: ATM Gen5/6 %s sentinel received\n",
  7381. __func__,
  7382. (cd->mode == PT_MODE_OPERATIONAL ?
  7383. "FW" :
  7384. (cd->mode == PT_MODE_BOOTLOADER ?
  7385. "BL" : "Unknown")));
  7386. } else {
  7387. cd->mode = PT_MODE_OPERATIONAL;
  7388. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  7389. cd->startup_status =
  7390. STARTUP_STATUS_FW_RESET_SENTINEL;
  7391. pt_debug(cd->dev, DL_INFO,
  7392. "%s: ATM PT/TT FW sentinel received\n",
  7393. __func__);
  7394. }
  7395. mutex_unlock(&cd->system_lock);
  7396. if (pt_allow_enumeration(cd)) {
  7397. if (cd->active_dut_generation == DUT_UNKNOWN) {
  7398. pt_debug(cd->dev, DL_INFO,
  7399. "%s: Queue Restart\n", __func__);
  7400. pt_queue_restart(cd);
  7401. } else {
  7402. pt_debug(cd->dev, DL_INFO,
  7403. "%s: Queue Enum\n", __func__);
  7404. pt_queue_enum(cd);
  7405. }
  7406. } else {
  7407. pt_debug(cd->dev, DL_INFO,
  7408. "%s: Sentinel - No Queued Action\n",
  7409. __func__);
  7410. }
  7411. } else {
  7412. /* Sentinel must be from TT/TC BL */
  7413. mutex_lock(&cd->system_lock);
  7414. cd->pip2_prot_active = true;
  7415. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  7416. cd->mode = PT_MODE_BOOTLOADER;
  7417. cd->sysinfo.ready = false;
  7418. mutex_unlock(&cd->system_lock);
  7419. pt_debug(cd->dev, DL_INFO,
  7420. "%s: BL Reset sentinel received\n", __func__);
  7421. if (cd->flashless_dut &&
  7422. cd->flashless_auto_bl == PT_ALLOW_AUTO_BL) {
  7423. pt_debug(cd->dev, DL_INFO,
  7424. "%s: BL to RAM for flashless DUT\n",
  7425. __func__);
  7426. kthread_run(start_fw_upgrade, cd, "pt_loader");
  7427. }
  7428. }
  7429. mutex_lock(&cd->system_lock);
  7430. memcpy(cd->response_buf, cd->input_buf, 2);
  7431. if (!cd->hid_reset_cmd_state && !cd->hid_cmd_state) {
  7432. mutex_unlock(&cd->system_lock);
  7433. pt_debug(cd->dev, DL_WARN,
  7434. "%s: Device Initiated Reset\n", __func__);
  7435. wake_up(&cd->wait_q);
  7436. return 0;
  7437. }
  7438. cd->hid_reset_cmd_state = 0;
  7439. if (cd->hid_cmd_state == PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7440. cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1 ||
  7441. cd->hid_cmd_state == PIP1_CMD_ID_USER_CMD + 1)
  7442. cd->hid_cmd_state = 0;
  7443. wake_up(&cd->wait_q);
  7444. mutex_unlock(&cd->system_lock);
  7445. return 0;
  7446. } else if (size == 2 || size >= PT_PIP_1P7_EMPTY_BUF) {
  7447. /*
  7448. * This debug message below is used by PBATS to calculate the
  7449. * time from the last lift off IRQ to when FW exits LFT mode.
  7450. */
  7451. touch_report = false;
  7452. pt_debug(cd->dev, DL_WARN,
  7453. "%s: DUT - Empty buffer detected\n", __func__);
  7454. return 0;
  7455. } else if (size > PT_MAX_INPUT) {
  7456. pt_debug(cd->dev, DL_ERROR,
  7457. "%s: DUT - Unexpected len field in active bus data!\n",
  7458. __func__);
  7459. return -EINVAL;
  7460. }
  7461. if (cd->pip2_prot_active) {
  7462. pt_debug(cd->dev, DL_DEBUG,
  7463. "%s: Decode PIP2.x Response\n", __func__);
  7464. /* PIP2 does not have a report id, hard code it */
  7465. report_id = 0x00;
  7466. cmd_id = cd->input_buf[PIP2_RESP_COMMAND_ID_OFFSET];
  7467. calc_crc = crc_ccitt_calculate(cd->input_buf, size - 2);
  7468. resp_crc = cd->input_buf[size - 2] << 8;
  7469. resp_crc |= cd->input_buf[size - 1];
  7470. if ((cd->pip2_cmd_tag_seq !=
  7471. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F)) &&
  7472. (resp_crc != calc_crc) &&
  7473. ((cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7474. == PT_PIP_TOUCH_REPORT_ID) ||
  7475. (cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7476. == PT_PIP_CAPSENSE_BTN_REPORT_ID))) {
  7477. pt_debug(cd->dev, DL_WARN,
  7478. "%s: %s %d %s\n",
  7479. __func__,
  7480. "Received PIP1 report id =",
  7481. cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET],
  7482. "when expecting a PIP2 report - IGNORE report");
  7483. return 0;
  7484. }
  7485. is_command = 1;
  7486. touch_report = false;
  7487. } else {
  7488. report_id = cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET];
  7489. cmd_id = cd->input_buf[PIP1_RESP_COMMAND_ID_OFFSET];
  7490. }
  7491. #ifdef TTDL_DIAGNOSTICS
  7492. pt_debug(cd->dev, DL_DEBUG,
  7493. "%s: pip2 = %d report_id: 0x%02X, cmd_code: 0x%02X\n",
  7494. __func__, cd->pip2_prot_active, report_id,
  7495. (cmd_id & PIP2_CMD_COMMAND_ID_MASK));
  7496. #endif /* TTDL_DIAGNOSTICS */
  7497. if (report_id == PT_PIP_WAKEUP_REPORT_ID) {
  7498. pt_wakeup_host(cd);
  7499. #ifdef TTHE_TUNER_SUPPORT
  7500. tthe_print(cd, cd->input_buf, size, "TT_WAKEUP=");
  7501. #endif
  7502. return 0;
  7503. }
  7504. mod_timer_pending(&cd->watchdog_timer, jiffies +
  7505. msecs_to_jiffies(cd->watchdog_interval));
  7506. /*
  7507. * If it is PIP2 response, the report_id has been set to 0,
  7508. * so it will not be parsed as a touch packet.
  7509. */
  7510. if (!pt_is_touch_report(report_id)) {
  7511. is_command = 1;
  7512. touch_report = false;
  7513. }
  7514. if (unlikely(is_command)) {
  7515. parse_command_input(cd, size);
  7516. return 0;
  7517. }
  7518. if (touch_report)
  7519. parse_touch_input(cd, size);
  7520. return 0;
  7521. }
  7522. /*******************************************************************************
  7523. * FUNCTION: pt_read_input
  7524. *
  7525. * SUMMARY: Reads incoming data off of the active bus
  7526. *
  7527. * RETURN:
  7528. * 0 = success
  7529. * !0 = failure
  7530. *
  7531. * PARAMETERS:
  7532. * *cd - pointer to core data
  7533. ******************************************************************************/
  7534. static int pt_read_input(struct pt_core_data *cd)
  7535. {
  7536. struct device *dev = cd->dev;
  7537. int rc = 0;
  7538. int t;
  7539. int retry = PT_BUS_READ_INPUT_RETRY_COUNT;
  7540. /*
  7541. * Workaround for easywake failure
  7542. * Interrupt for easywake, wait for bus controller to wake
  7543. */
  7544. mutex_lock(&cd->system_lock);
  7545. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  7546. if (cd->sleep_state == SS_SLEEP_ON) {
  7547. mutex_unlock(&cd->system_lock);
  7548. if (!dev->power.is_suspended)
  7549. goto read;
  7550. t = wait_event_timeout(cd->wait_q,
  7551. (cd->wait_until_wake == 1),
  7552. msecs_to_jiffies(2000));
  7553. #ifdef TTDL_DIAGNOSTICS
  7554. if (IS_TMO(t)) {
  7555. cd->bus_transmit_error_count++;
  7556. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  7557. pt_debug(dev, DL_ERROR,
  7558. "%s: !!!I2C Transmission Error %d\n",
  7559. __func__,
  7560. cd->bus_transmit_error_count);
  7561. }
  7562. #else
  7563. if (IS_TMO(t))
  7564. pt_queue_enum(cd);
  7565. #endif /* TTDL_DIAGNOSTICS */
  7566. goto read;
  7567. }
  7568. }
  7569. mutex_unlock(&cd->system_lock);
  7570. read:
  7571. /* Try reading up to 'retry' times */
  7572. while (retry-- != 0) {
  7573. rc = pt_adap_read_default_nosize(cd, cd->input_buf,
  7574. PT_MAX_INPUT);
  7575. if (!rc) {
  7576. pt_debug(dev, DL_DEBUG,
  7577. "%s: Read input successfully\n", __func__);
  7578. goto read_exit;
  7579. }
  7580. usleep_range(1000, 2000);
  7581. }
  7582. pt_debug(dev, DL_ERROR,
  7583. "%s: Error getting report, rc=%d\n", __func__, rc);
  7584. read_exit:
  7585. return rc;
  7586. }
  7587. /*******************************************************************************
  7588. * FUNCTION: pt_irq
  7589. *
  7590. * SUMMARY: Process all detected interrupts
  7591. *
  7592. * RETURN:
  7593. * IRQ_HANDLED - Finished processing the interrupt
  7594. *
  7595. * PARAMETERS:
  7596. * irq - IRQ number
  7597. * *handle - pointer to core data struct
  7598. ******************************************************************************/
  7599. irqreturn_t pt_irq(int irq, void *handle)
  7600. {
  7601. struct pt_core_data *cd = handle;
  7602. int rc = 0;
  7603. if (!pt_check_irq_asserted(cd))
  7604. return IRQ_HANDLED;
  7605. rc = pt_read_input(cd);
  7606. #ifdef TTDL_DIAGNOSTICS
  7607. cd->irq_count++;
  7608. /* Used to calculate T-Refresh */
  7609. if (cd->t_refresh_active) {
  7610. if (cd->t_refresh_count == 0) {
  7611. cd->t_refresh_time = jiffies;
  7612. cd->t_refresh_count++;
  7613. } else if (cd->t_refresh_count < cd->t_refresh_total) {
  7614. cd->t_refresh_count++;
  7615. } else {
  7616. cd->t_refresh_active = 0;
  7617. cd->t_refresh_time =
  7618. jiffies_to_msecs(jiffies - cd->t_refresh_time);
  7619. }
  7620. }
  7621. #endif /* TTDL_DIAGNOSTICS */
  7622. if (!rc)
  7623. pt_parse_input(cd);
  7624. return IRQ_HANDLED;
  7625. }
  7626. /*******************************************************************************
  7627. * FUNCTION: _pt_subscribe_attention
  7628. *
  7629. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7630. * to subscribe themselves into the TTDL attention list
  7631. *
  7632. * RETURN:
  7633. * 0 = success
  7634. * !0 = failure
  7635. *
  7636. * PARAMETERS:
  7637. * *dev - pointer to device structure
  7638. * type - attention type enum
  7639. * *id - ID of the module calling this function
  7640. * *func - callback function pointer to be called when notified
  7641. * mode - attention mode
  7642. ******************************************************************************/
  7643. int _pt_subscribe_attention(struct device *dev,
  7644. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7645. int mode)
  7646. {
  7647. struct pt_core_data *cd = dev_get_drvdata(dev);
  7648. struct atten_node *atten, *atten_new;
  7649. atten_new = kzalloc(sizeof(*atten_new), GFP_KERNEL);
  7650. if (!atten_new)
  7651. return -ENOMEM;
  7652. pt_debug(cd->dev, DL_INFO, "%s from '%s'\n", __func__,
  7653. dev_name(cd->dev));
  7654. spin_lock(&cd->spinlock);
  7655. list_for_each_entry(atten, &cd->atten_list[type], node) {
  7656. if (atten->id == id && atten->mode == mode) {
  7657. spin_unlock(&cd->spinlock);
  7658. kfree(atten_new);
  7659. pt_debug(cd->dev, DL_INFO, "%s: %s=%p %s=%d\n",
  7660. __func__,
  7661. "already subscribed attention",
  7662. dev, "mode", mode);
  7663. return 0;
  7664. }
  7665. }
  7666. atten_new->id = id;
  7667. atten_new->dev = dev;
  7668. atten_new->mode = mode;
  7669. atten_new->func = func;
  7670. list_add(&atten_new->node, &cd->atten_list[type]);
  7671. spin_unlock(&cd->spinlock);
  7672. return 0;
  7673. }
  7674. /*******************************************************************************
  7675. * FUNCTION: _pt_unsubscribe_attention
  7676. *
  7677. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7678. * to unsubscribe themselves from the TTDL attention list
  7679. *
  7680. * RETURN:
  7681. * 0 = success
  7682. * !0 = failure
  7683. *
  7684. * PARAMETERS:
  7685. * *dev - pointer to device structure
  7686. * type - attention type enum
  7687. * *id - ID of the module calling this function
  7688. * *func - function pointer
  7689. * mode - attention mode
  7690. ******************************************************************************/
  7691. int _pt_unsubscribe_attention(struct device *dev,
  7692. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7693. int mode)
  7694. {
  7695. struct pt_core_data *cd = dev_get_drvdata(dev);
  7696. struct atten_node *atten, *atten_n;
  7697. spin_lock(&cd->spinlock);
  7698. list_for_each_entry_safe(atten, atten_n, &cd->atten_list[type], node) {
  7699. if (atten->id == id && atten->mode == mode) {
  7700. list_del(&atten->node);
  7701. spin_unlock(&cd->spinlock);
  7702. pt_debug(cd->dev, DL_DEBUG, "%s: %s=%p %s=%d\n",
  7703. __func__,
  7704. "unsub for atten->dev", atten->dev,
  7705. "atten->mode", atten->mode);
  7706. kfree(atten);
  7707. return 0;
  7708. }
  7709. }
  7710. spin_unlock(&cd->spinlock);
  7711. return -ENODEV;
  7712. }
  7713. /*******************************************************************************
  7714. * FUNCTION: _pt_request_exclusive
  7715. *
  7716. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7717. * to request exclusive access
  7718. *
  7719. * RETURN:
  7720. * 0 = success
  7721. * !0 = failure
  7722. *
  7723. * PARAMETERS:
  7724. * *dev - pointer to device structure
  7725. * timeout_ms - timeout to wait for exclusive access
  7726. ******************************************************************************/
  7727. static int _pt_request_exclusive(struct device *dev,
  7728. int timeout_ms)
  7729. {
  7730. struct pt_core_data *cd = dev_get_drvdata(dev);
  7731. return request_exclusive(cd, (void *)dev, timeout_ms);
  7732. }
  7733. /*******************************************************************************
  7734. * FUNCTION: _pt_release_exclusive
  7735. *
  7736. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7737. * to release exclusive access
  7738. *
  7739. * RETURN:
  7740. * 0 = success
  7741. * !0 = failure
  7742. *
  7743. * PARAMETERS:
  7744. * *dev - pointer to device structure
  7745. ******************************************************************************/
  7746. static int _pt_release_exclusive(struct device *dev)
  7747. {
  7748. struct pt_core_data *cd = dev_get_drvdata(dev);
  7749. return release_exclusive(cd, (void *)dev);
  7750. }
  7751. /*******************************************************************************
  7752. * FUNCTION: _pt_request_reset
  7753. *
  7754. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7755. * to request the DUT to be reset. Function returns err if refused or
  7756. * timeout occurs (Note: core uses fixed timeout period).
  7757. *
  7758. * NOTE: Function blocks until ISR occurs.
  7759. *
  7760. * RETURN:
  7761. * 0 = success
  7762. * !0 = failure
  7763. *
  7764. * PARAMETERS:
  7765. * *dev - pointer to device structure
  7766. * protect - flag to call protected or non-protected
  7767. ******************************************************************************/
  7768. static int _pt_request_reset(struct device *dev, int protect)
  7769. {
  7770. struct pt_core_data *cd = dev_get_drvdata(dev);
  7771. int rc;
  7772. rc = pt_dut_reset(cd, protect);
  7773. if (rc < 0) {
  7774. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  7775. __func__, rc);
  7776. }
  7777. return rc;
  7778. }
  7779. /*******************************************************************************
  7780. * FUNCTION: _pt_request_enum
  7781. *
  7782. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7783. * to request TTDL to queue an enum. This function will return err
  7784. * if refused; if no error then enum has completed and system is in
  7785. * normal operation mode.
  7786. *
  7787. * RETURN:
  7788. * 0 = success
  7789. * !0 = failure
  7790. *
  7791. * PARAMETERS:
  7792. * *dev - pointer to device structure
  7793. * wait - boolean to determine if to wait for startup event
  7794. ******************************************************************************/
  7795. static int _pt_request_enum(struct device *dev, bool wait)
  7796. {
  7797. struct pt_core_data *cd = dev_get_drvdata(dev);
  7798. pt_queue_enum(cd);
  7799. if (wait)
  7800. wait_event_timeout(cd->wait_q,
  7801. cd->startup_state == STARTUP_NONE,
  7802. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  7803. return 0;
  7804. }
  7805. /*******************************************************************************
  7806. * FUNCTION: _pt_request_sysinfo
  7807. *
  7808. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7809. * to request the pointer to the system information structure. This
  7810. * function will return NULL if sysinfo has not been acquired from the
  7811. * DUT yet.
  7812. *
  7813. * RETURN: Pointer to the system information struct
  7814. *
  7815. * PARAMETERS:
  7816. * *dev - pointer to device structure
  7817. ******************************************************************************/
  7818. struct pt_sysinfo *_pt_request_sysinfo(struct device *dev)
  7819. {
  7820. struct pt_core_data *cd = dev_get_drvdata(dev);
  7821. int rc = 0;
  7822. /* Attempt to get sysinfo if not ready and panel_id is from XY pin */
  7823. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7824. !cd->sysinfo.ready) {
  7825. rc = pt_hid_output_get_sysinfo_(cd);
  7826. if (rc) {
  7827. pt_debug(cd->dev, DL_ERROR,
  7828. "%s: Error getting sysinfo rc=%d\n",
  7829. __func__, rc);
  7830. }
  7831. }
  7832. if (cd->sysinfo.ready)
  7833. return &cd->sysinfo;
  7834. return NULL;
  7835. }
  7836. /*******************************************************************************
  7837. * FUNCTION: _pt_request_loader_pdata
  7838. *
  7839. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7840. * to request the pointer to the loader platform data
  7841. *
  7842. * RETURN: Pointer to the loader platform data struct
  7843. *
  7844. * PARAMETERS:
  7845. * *dev - pointer to device structure
  7846. ******************************************************************************/
  7847. static struct pt_loader_platform_data *_pt_request_loader_pdata(
  7848. struct device *dev)
  7849. {
  7850. struct pt_core_data *cd = dev_get_drvdata(dev);
  7851. return cd->pdata->loader_pdata;
  7852. }
  7853. /*******************************************************************************
  7854. * FUNCTION: _pt_request_start_wd
  7855. *
  7856. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7857. * to request to start the TTDL watchdog
  7858. *
  7859. * RETURN:
  7860. * 0 = success
  7861. * !0 = failure
  7862. *
  7863. * PARAMETERS:
  7864. * *dev - pointer to device structure
  7865. ******************************************************************************/
  7866. static int _pt_request_start_wd(struct device *dev)
  7867. {
  7868. struct pt_core_data *cd = dev_get_drvdata(dev);
  7869. pt_start_wd_timer(cd);
  7870. return 0;
  7871. }
  7872. /*******************************************************************************
  7873. * FUNCTION: _pt_request_stop_wd
  7874. *
  7875. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7876. * to request to stop the TTDL watchdog
  7877. *
  7878. * RETURN:
  7879. * 0 = success
  7880. * !0 = failure
  7881. *
  7882. * PARAMETERS:
  7883. * *dev - pointer to device structure
  7884. ******************************************************************************/
  7885. static int _pt_request_stop_wd(struct device *dev)
  7886. {
  7887. struct pt_core_data *cd = dev_get_drvdata(dev);
  7888. pt_stop_wd_timer(cd);
  7889. return 0;
  7890. }
  7891. /*******************************************************************************
  7892. * FUNCTION: pt_pip2_launch_app
  7893. *
  7894. * SUMMARY: Sends the PIP2 EXECUTE command to launch the APP and then wait for
  7895. * the FW reset sentinel to indicate the function succeeded.
  7896. *
  7897. * NOTE: Calling this function when the DUT is in Application mode WILL result
  7898. * in a timeout delay and with the DUT being reset with an XRES.
  7899. *
  7900. * RETURN:
  7901. * 0 = success
  7902. * !0 = failure
  7903. *
  7904. * PARAMETERS:
  7905. * *cd - pointer to core data structure
  7906. * protect - flag to call protected or non-protected
  7907. ******************************************************************************/
  7908. static int pt_pip2_launch_app(struct device *dev, int protect)
  7909. {
  7910. struct pt_core_data *cd = dev_get_drvdata(dev);
  7911. u16 actual_read_len;
  7912. u16 tmp_startup_status = cd->startup_status;
  7913. u8 read_buf[12];
  7914. u8 status;
  7915. int time = 0;
  7916. int rc = 0;
  7917. mutex_lock(&cd->system_lock);
  7918. cd->startup_status = STARTUP_STATUS_START;
  7919. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  7920. mutex_unlock(&cd->system_lock);
  7921. rc = _pt_request_pip2_send_cmd(dev, protect,
  7922. PIP2_CMD_ID_EXECUTE, NULL, 0, read_buf,
  7923. &actual_read_len);
  7924. if (rc) {
  7925. pt_debug(dev, DL_ERROR,
  7926. "%s: PIP2 EXECUTE cmd failed rc = %d\n",
  7927. __func__, rc);
  7928. } else {
  7929. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  7930. /* Test for no or invalid image in FLASH, no point to reset */
  7931. if (status == PIP2_RSP_ERR_INVALID_IMAGE) {
  7932. rc = status;
  7933. goto exit;
  7934. }
  7935. /* Any other boot failure */
  7936. if (status != 0) {
  7937. pt_debug(dev, DL_ERROR,
  7938. "%s: FW did not EXECUTE, status = %d\n",
  7939. __func__, status);
  7940. rc = status;
  7941. }
  7942. }
  7943. if (rc) {
  7944. pt_debug(dev, DL_ERROR,
  7945. "%s: Failed to launch APP, XRES DUT rc = %d\n",
  7946. __func__, rc);
  7947. goto exit;
  7948. }
  7949. while ((cd->startup_status == STARTUP_STATUS_START) && time < 240) {
  7950. msleep(20);
  7951. pt_debug(cd->dev, DL_INFO, "%s: wait for %d for enum=0x%04X\n",
  7952. __func__, time, cd->startup_status);
  7953. time += 20;
  7954. }
  7955. if (cd->startup_status == STARTUP_STATUS_START) {
  7956. pt_debug(cd->dev, DL_WARN,
  7957. "%s: TMO waiting for FW reset sentinel\n", __func__);
  7958. rc = -ETIME;
  7959. }
  7960. exit:
  7961. if (cd->startup_status == STARTUP_STATUS_START) {
  7962. /* Reset to original state because we could be stuck in BL */
  7963. mutex_lock(&cd->system_lock);
  7964. cd->startup_status = tmp_startup_status;
  7965. mutex_unlock(&cd->system_lock);
  7966. }
  7967. return rc;
  7968. }
  7969. /*******************************************************************************
  7970. * FUNCTION: _pt_request_pip2_launch_app
  7971. *
  7972. * SUMMARY: Calls pt_pip2_launch_app() when configured to. A small delay is
  7973. * inserted to ensure the reset has allowed the BL reset sentinel to be
  7974. * consumed.
  7975. *
  7976. * RETURN:
  7977. * 0 = success
  7978. * !0 = failure
  7979. *
  7980. * PARAMETERS:
  7981. * *cd - pointer to core data structure
  7982. ******************************************************************************/
  7983. static int _pt_request_pip2_launch_app(struct device *dev, int protect)
  7984. {
  7985. return pt_pip2_launch_app(dev, protect);
  7986. }
  7987. /*******************************************************************************
  7988. * FUNCTION: _pt_request_wait_for_enum_state
  7989. *
  7990. * SUMMARY: Loops for up to timeout waiting for the startup_status to reach
  7991. * the state passed in or STARTUP_STATUS_COMPLETE whichever comes first
  7992. *
  7993. * RETURN:
  7994. * 0 = success
  7995. * !0 = failure
  7996. *
  7997. * PARAMETERS:
  7998. * *cd - pointer to core data structure
  7999. * timeout - timeout for how long to wait
  8000. * state - enum state to wait for
  8001. ******************************************************************************/
  8002. static int _pt_request_wait_for_enum_state(struct device *dev, int timeout,
  8003. int state)
  8004. {
  8005. int rc = 0;
  8006. int t;
  8007. struct pt_core_data *cd = dev_get_drvdata(dev);
  8008. t = wait_event_timeout(cd->wait_q,
  8009. (cd->startup_status & state) || (cd->startup_status & 0x0100),
  8010. msecs_to_jiffies(timeout));
  8011. if (IS_TMO(t)) {
  8012. pt_debug(cd->dev, DL_ERROR,
  8013. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8014. __func__, state, timeout);
  8015. pt_debug(cd->dev, DL_WARN,
  8016. "%s: enum state reached 0x%04X\n",
  8017. __func__, cd->startup_status);
  8018. rc = -ETIME;
  8019. } else if (cd->startup_status & state) {
  8020. pt_debug(cd->dev, DL_INFO,
  8021. "%s: Enum state reached: enum=0x%04X in %dms\n",
  8022. __func__, cd->startup_status,
  8023. (t == 1) ? timeout : (timeout - jiffies_to_msecs(t)));
  8024. } else {
  8025. if (t == 1) {
  8026. pt_debug(
  8027. cd->dev, DL_ERROR,
  8028. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8029. __func__, state, timeout);
  8030. rc = -ETIME;
  8031. } else {
  8032. pt_debug(
  8033. cd->dev, DL_ERROR,
  8034. "%s: Enum state 0x%04X not reached in %dms\n",
  8035. __func__, state, timeout - jiffies_to_msecs(t));
  8036. rc = -EINVAL;
  8037. }
  8038. pt_debug(cd->dev, DL_INFO, "%s: enum state reached 0x%04X\n",
  8039. __func__, cd->startup_status);
  8040. }
  8041. return rc;
  8042. }
  8043. /*******************************************************************************
  8044. * FUNCTION: pt_core_wake_device_from_deep_sleep_
  8045. *
  8046. * SUMMARY: Call the set_power function and set the DUT to wake up from
  8047. * deep sleep.
  8048. *
  8049. * RETURN:
  8050. * 0 = success
  8051. * !0 = error
  8052. *
  8053. * PARAMETERS:
  8054. * *cd - pointer to core data
  8055. ******************************************************************************/
  8056. static int pt_core_wake_device_from_deep_sleep_(
  8057. struct pt_core_data *cd)
  8058. {
  8059. int rc;
  8060. rc = pt_hid_cmd_set_power_(cd, HID_POWER_ON);
  8061. if (rc)
  8062. rc = -EAGAIN;
  8063. /* Prevent failure on sequential wake/sleep requests from OS */
  8064. msleep(20);
  8065. return rc;
  8066. }
  8067. /*******************************************************************************
  8068. * FUNCTION: pt_core_wake_device_from_easy_wake_
  8069. *
  8070. * SUMMARY: Wake up device from Easy-Wake state.
  8071. *
  8072. * RETURN:
  8073. * 0 = success
  8074. * !0 = failure
  8075. *
  8076. * PARAMETERS:
  8077. * *cd - pointer to core data
  8078. ******************************************************************************/
  8079. static int pt_core_wake_device_from_easy_wake_(struct pt_core_data *cd)
  8080. {
  8081. mutex_lock(&cd->system_lock);
  8082. cd->wait_until_wake = 1;
  8083. mutex_unlock(&cd->system_lock);
  8084. wake_up(&cd->wait_q);
  8085. msleep(20);
  8086. return pt_core_wake_device_from_deep_sleep_(cd);
  8087. }
  8088. /*******************************************************************************
  8089. * FUNCTION: pt_restore_parameters_
  8090. *
  8091. * SUMMARY: This function sends all RAM parameters stored in the linked list
  8092. * back to the DUT
  8093. *
  8094. * RETURN:
  8095. * 0 = success
  8096. * !0 = failure
  8097. *
  8098. * PARAMETERS:
  8099. * *cd - pointer the core data structure
  8100. ******************************************************************************/
  8101. static int pt_restore_parameters_(struct pt_core_data *cd)
  8102. {
  8103. struct param_node *param;
  8104. int rc = 0;
  8105. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  8106. goto exit;
  8107. spin_lock(&cd->spinlock);
  8108. list_for_each_entry(param, &cd->param_list, node) {
  8109. spin_unlock(&cd->spinlock);
  8110. pt_debug(cd->dev, DL_INFO, "%s: Parameter id:%d value:%d\n",
  8111. __func__, param->id, param->value);
  8112. rc = pt_pip_set_param_(cd, param->id,
  8113. param->value, param->size);
  8114. if (rc)
  8115. goto exit;
  8116. spin_lock(&cd->spinlock);
  8117. }
  8118. spin_unlock(&cd->spinlock);
  8119. exit:
  8120. return rc;
  8121. }
  8122. /*******************************************************************************
  8123. * FUNCTION: pt_pip2_exit_bl_
  8124. *
  8125. * SUMMARY: Attempt to exit the BL and run the application, taking into account
  8126. * a DUT that may not have flash and will need FW to be loaded into RAM
  8127. *
  8128. * RETURN:
  8129. * 0 = success
  8130. * !0 = failure
  8131. *
  8132. * PARAMETERS:
  8133. * *cd - pointer the core data structure
  8134. * *status_str - pointer to optional status string buffer
  8135. * buf_size - size of status_str buffer
  8136. ******************************************************************************/
  8137. int pt_pip2_exit_bl_(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8138. {
  8139. int rc;
  8140. int wait_time = 0;
  8141. u8 mode = PT_MODE_UNKNOWN;
  8142. bool load_status_str = false;
  8143. /*
  8144. * Below function has protective call to ensure no enum is still on
  8145. * going, while this kind of protection should be applied widely in
  8146. * future (TODO).
  8147. */
  8148. rc = pt_pip2_get_mode_sysmode(cd, &mode, NULL);
  8149. if (status_str && buf_size <= 50)
  8150. load_status_str = true;
  8151. if (mode == PT_MODE_BOOTLOADER) {
  8152. if (cd->flashless_dut == 1) {
  8153. rc = pt_hw_hard_reset(cd);
  8154. } else {
  8155. rc = pt_pip2_launch_app(cd->dev,
  8156. PT_CORE_CMD_UNPROTECTED);
  8157. if (rc == PIP2_RSP_ERR_INVALID_IMAGE) {
  8158. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8159. __func__, "Invalid image in FLASH rc", rc);
  8160. } else if (rc) {
  8161. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8162. __func__, "Failed to launch app rc", rc);
  8163. }
  8164. }
  8165. if (!rc) {
  8166. if (cd->flashless_dut == 1) {
  8167. /* Wait for BL to complete before enum */
  8168. rc = _pt_request_wait_for_enum_state(cd->dev,
  8169. 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  8170. if (rc && load_status_str) {
  8171. strlcpy(status_str, "No FW sentinel after BL",
  8172. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8173. goto exit;
  8174. }
  8175. }
  8176. /*
  8177. * If the host wants to interact with the FW or do a
  8178. * forced calibration, the FW must be out of BOOT mode
  8179. * and the system information must have been retrieved.
  8180. * Reaching the FW_OUT_OF_BOOT state guarantees both.
  8181. * If, however, the enumeration does not reach this
  8182. * point, the DUT may still be in APP mode so test
  8183. * for all conditions.
  8184. */
  8185. rc = _pt_request_wait_for_enum_state(cd->dev, 4500,
  8186. STARTUP_STATUS_FW_OUT_OF_BOOT);
  8187. if (!rc || cd->startup_status >=
  8188. STARTUP_STATUS_FW_RESET_SENTINEL) {
  8189. mutex_lock(&cd->system_lock);
  8190. cd->mode = PT_MODE_OPERATIONAL;
  8191. mutex_unlock(&cd->system_lock);
  8192. }
  8193. if (rc) {
  8194. pt_debug(cd->dev, DL_WARN, "%s: %s: 0x%04X\n",
  8195. __func__, "Failed to enum with DUT",
  8196. cd->startup_status);
  8197. if (load_status_str && !(cd->startup_status &
  8198. STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  8199. strlcpy(status_str, "FW Stuck in Boot mode",
  8200. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8201. goto exit;
  8202. }
  8203. }
  8204. /*
  8205. * The coming FW sentinel could wake up the event
  8206. * queue, which has chance to be taken by next command
  8207. * wrongly. Following delay is a workaround to avoid
  8208. * this issue for most situations.
  8209. */
  8210. msleep(20);
  8211. pt_start_wd_timer(cd);
  8212. }
  8213. if (load_status_str) {
  8214. if (rc == PIP2_RSP_ERR_INVALID_IMAGE)
  8215. strlcpy(status_str, "Failed - Invalid image in FLASH",
  8216. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8217. else if (!rc)
  8218. strlcpy(status_str, "Entered APP from BL mode",
  8219. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8220. else
  8221. strlcpy(status_str, "Failed to enter APP from BL mode",
  8222. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8223. }
  8224. } else if (mode == PT_MODE_OPERATIONAL) {
  8225. mutex_lock(&cd->system_lock);
  8226. cd->mode = mode;
  8227. mutex_unlock(&cd->system_lock);
  8228. rc = pt_poll_for_fw_exit_boot_mode(cd, 1500, &wait_time);
  8229. if (load_status_str) {
  8230. if (!rc)
  8231. strlcpy(status_str, "Already in APP mode",
  8232. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8233. else
  8234. strlcpy(status_str, "Already in APP mode - FW stuck in Boot mode",
  8235. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8236. }
  8237. } else if (rc || mode == PT_MODE_UNKNOWN) {
  8238. mutex_lock(&cd->system_lock);
  8239. cd->mode = mode;
  8240. mutex_unlock(&cd->system_lock);
  8241. if (load_status_str)
  8242. strlcpy(status_str, "Failed to determine active mode",
  8243. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8244. }
  8245. exit:
  8246. if (!rc)
  8247. pt_start_wd_timer(cd);
  8248. return rc;
  8249. }
  8250. /*******************************************************************************
  8251. * FUNCTION: pt_pip2_exit_bl
  8252. *
  8253. * SUMMARY: Wrapper function for _pt_pip2_exit_bl that guarantees exclusive
  8254. * access.
  8255. *
  8256. * RETURN:
  8257. * 0 = success
  8258. * !0 = failure
  8259. *
  8260. * PARAMETERS:
  8261. * *cd - pointer the core data structure
  8262. * *status_str - pointer to optional status string buffer
  8263. * buf_size - size of status_str buffer
  8264. ******************************************************************************/
  8265. int pt_pip2_exit_bl(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8266. {
  8267. int rc;
  8268. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8269. if (rc < 0) {
  8270. pt_debug(cd->dev, DL_ERROR,
  8271. "%s: fail get exclusive ex=%p own=%p\n", __func__,
  8272. cd->exclusive_dev, cd->dev);
  8273. return rc;
  8274. }
  8275. rc = pt_pip2_exit_bl_(cd, status_str, buf_size);
  8276. if (release_exclusive(cd, cd->dev) < 0)
  8277. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8278. __func__);
  8279. return rc;
  8280. }
  8281. /*******************************************************************************
  8282. * FUNCTION: _fast_startup
  8283. *
  8284. * SUMMARY: Perform fast startup after resume device by power on/off stratergy.
  8285. *
  8286. * RETURN:
  8287. * 0 = success
  8288. * !0 = failure
  8289. *
  8290. * PARAMETERS:
  8291. * *cd - pointer the core data structure
  8292. ******************************************************************************/
  8293. static int _fast_startup(struct pt_core_data *cd)
  8294. {
  8295. int retry = PT_CORE_STARTUP_RETRY_COUNT;
  8296. int rc = 0;
  8297. u8 mode = PT_MODE_UNKNOWN;
  8298. struct pt_hid_desc hid_desc;
  8299. int wait_time = 0;
  8300. memset(&hid_desc, 0, sizeof(hid_desc));
  8301. reset:
  8302. if (retry != PT_CORE_STARTUP_RETRY_COUNT)
  8303. pt_debug(cd->dev, DL_INFO, "%s: Retry %d\n", __func__,
  8304. PT_CORE_STARTUP_RETRY_COUNT - retry);
  8305. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8306. pt_debug(cd->dev, DL_INFO, "%s: PIP1 Enumeration start\n",
  8307. __func__);
  8308. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8309. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8310. if (rc < 0) {
  8311. pt_debug(cd->dev, DL_ERROR,
  8312. "%s: Error on getting HID descriptor r=%d\n",
  8313. __func__, rc);
  8314. if (retry--)
  8315. goto reset;
  8316. goto exit;
  8317. }
  8318. cd->mode = pt_get_mode(cd, &hid_desc);
  8319. if (cd->mode == PT_MODE_BOOTLOADER) {
  8320. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8321. __func__);
  8322. rc = pt_hid_output_bl_launch_app_(cd);
  8323. if (rc < 0) {
  8324. pt_debug(cd->dev, DL_ERROR,
  8325. "%s: Error on launch app r=%d\n",
  8326. __func__, rc);
  8327. if (retry--)
  8328. goto reset;
  8329. goto exit;
  8330. }
  8331. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8332. if (rc < 0) {
  8333. pt_debug(cd->dev, DL_ERROR,
  8334. "%s: Error on getting HID descriptor r=%d\n",
  8335. __func__, rc);
  8336. if (retry--)
  8337. goto reset;
  8338. goto exit;
  8339. }
  8340. cd->mode = pt_get_mode(cd, &hid_desc);
  8341. if (cd->mode == PT_MODE_BOOTLOADER) {
  8342. if (retry--)
  8343. goto reset;
  8344. goto exit;
  8345. }
  8346. }
  8347. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8348. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8349. } else {
  8350. pt_debug(cd->dev, DL_INFO, "%s: PIP2 Enumeration start\n",
  8351. __func__);
  8352. if (retry == PT_CORE_STARTUP_RETRY_COUNT) {
  8353. /* Wait for any sentinel before first try */
  8354. rc = _pt_request_wait_for_enum_state(
  8355. cd->dev, 150,
  8356. STARTUP_STATUS_BL_RESET_SENTINEL |
  8357. STARTUP_STATUS_FW_RESET_SENTINEL);
  8358. if (rc)
  8359. pt_debug(cd->dev, DL_ERROR,
  8360. "%s: No Sentinel detected rc = %d\n",
  8361. __func__, rc);
  8362. } else
  8363. pt_flush_bus_if_irq_asserted(cd,
  8364. PT_FLUSH_BUS_BASED_ON_LEN);
  8365. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8366. if (rc) {
  8367. pt_debug(cd->dev, DL_ERROR,
  8368. "%s: Get mode failed, mode unknown\n",
  8369. __func__);
  8370. mode = PT_MODE_UNKNOWN;
  8371. }
  8372. cd->mode = mode;
  8373. if (cd->mode == PT_MODE_BOOTLOADER) {
  8374. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8375. __func__);
  8376. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  8377. if (rc) {
  8378. pt_debug(cd->dev, DL_ERROR,
  8379. "%s Failed to exit bootloader\n",
  8380. __func__);
  8381. msleep(50);
  8382. rc = -ENODEV;
  8383. if (retry--)
  8384. goto reset;
  8385. goto exit;
  8386. } else {
  8387. pt_debug(cd->dev, DL_INFO,
  8388. "%s: Exit bootloader successfully\n",
  8389. __func__);
  8390. }
  8391. if (cd->mode != PT_MODE_OPERATIONAL) {
  8392. pt_debug(cd->dev, DL_WARN,
  8393. "%s: restore mode failure mode = %d\n",
  8394. __func__, cd->mode);
  8395. if (retry--)
  8396. goto reset;
  8397. goto exit;
  8398. }
  8399. }
  8400. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8401. }
  8402. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  8403. rc = _pt_poll_for_fw_exit_boot_mode(cd, 500, &wait_time);
  8404. if (!rc) {
  8405. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8406. pt_debug(cd->dev, DL_WARN,
  8407. "%s: Exit FW BOOT Mode after %dms\n",
  8408. __func__, wait_time);
  8409. } else {
  8410. pt_debug(cd->dev, DL_WARN,
  8411. "%s: FW stuck in BOOT Mode after %dms\n",
  8412. __func__, wait_time);
  8413. goto exit;
  8414. }
  8415. if (!cd->sysinfo.ready) {
  8416. rc = pt_hid_output_get_sysinfo_(cd);
  8417. if (rc) {
  8418. pt_debug(cd->dev, DL_ERROR,
  8419. "%s: Error on getting sysinfo r=%d\n",
  8420. __func__, rc);
  8421. if (retry--)
  8422. goto reset;
  8423. goto exit;
  8424. }
  8425. }
  8426. cd->startup_status |= STARTUP_STATUS_GET_SYS_INFO;
  8427. rc = pt_restore_parameters_(cd);
  8428. if (rc)
  8429. pt_debug(cd->dev, DL_ERROR,
  8430. "%s: failed to restore parameters rc=%d\n",
  8431. __func__, rc);
  8432. else
  8433. cd->startup_status |= STARTUP_STATUS_RESTORE_PARM;
  8434. exit:
  8435. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  8436. return rc;
  8437. }
  8438. /*******************************************************************************
  8439. * FUNCTION: pt_core_poweron_device_
  8440. *
  8441. * SUMMARY: Power on device, enable IRQ, and then perform a fast startup.
  8442. *
  8443. * RETURN:
  8444. * 0 = success
  8445. * !0 = failure
  8446. *
  8447. * PARAMETERS:
  8448. * *cd - pointer to core data
  8449. ******************************************************************************/
  8450. static int pt_core_poweron_device_(struct pt_core_data *cd)
  8451. {
  8452. struct device *dev = cd->dev;
  8453. int rc = 0;
  8454. /*
  8455. * After power on action, the chip can general FW sentinel. It can
  8456. * trigger an enumeration without hid_reset_cmd_state flag. Since the
  8457. * _fast_startup() can perform a quick enumeration too, here doesn't
  8458. * need another enumeration.
  8459. */
  8460. mutex_lock(&cd->system_lock);
  8461. cd->startup_status = STARTUP_STATUS_START;
  8462. cd->hid_reset_cmd_state = 1;
  8463. mutex_unlock(&cd->system_lock);
  8464. rc = cd->cpdata->power(cd->cpdata, 1, dev, 0);
  8465. if (rc < 0) {
  8466. pt_debug(dev, DL_ERROR, "%s: HW Power up fails r=%d\n",
  8467. __func__, rc);
  8468. goto exit;
  8469. }
  8470. if (!cd->irq_enabled) {
  8471. cd->irq_enabled = true;
  8472. enable_irq(cd->irq);
  8473. }
  8474. /* TBD: following function doesn't update startup_status */
  8475. rc = _fast_startup(cd);
  8476. exit:
  8477. return rc;
  8478. }
  8479. /*******************************************************************************
  8480. * FUNCTION: pt_core_wake_device_from_deep_standby_
  8481. *
  8482. * SUMMARY: Reset device, and then trigger a full enumeration.
  8483. *
  8484. * RETURN:
  8485. * 0 = success
  8486. * !0 = failure
  8487. *
  8488. * PARAMETERS:
  8489. * *cd - pointer to core data
  8490. ******************************************************************************/
  8491. static int pt_core_wake_device_from_deep_standby_(struct pt_core_data *cd)
  8492. {
  8493. int rc;
  8494. rc = pt_dut_reset_and_wait(cd);
  8495. if (rc < 0) {
  8496. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  8497. __func__, rc);
  8498. goto exit;
  8499. }
  8500. rc = _fast_startup(cd);
  8501. exit:
  8502. return rc;
  8503. }
  8504. /*******************************************************************************
  8505. * FUNCTION: pt_core_wake_
  8506. *
  8507. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8508. * the configuration in the core platform data structure.
  8509. *
  8510. * RETURN:
  8511. * 0 = success
  8512. * !0 = failure
  8513. *
  8514. * PARAMETERS:
  8515. * *cd - pointer to core data
  8516. ******************************************************************************/
  8517. static int pt_core_wake_(struct pt_core_data *cd)
  8518. {
  8519. int rc = 0;
  8520. mutex_lock(&cd->system_lock);
  8521. if (cd->sleep_state == SS_SLEEP_ON) {
  8522. cd->sleep_state = SS_WAKING;
  8523. } else {
  8524. mutex_unlock(&cd->system_lock);
  8525. return 1;
  8526. }
  8527. mutex_unlock(&cd->system_lock);
  8528. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8529. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  8530. rc = pt_core_wake_device_from_easy_wake_(cd);
  8531. else if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP)
  8532. rc = pt_core_poweron_device_(cd);
  8533. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  8534. rc = pt_core_wake_device_from_deep_standby_(cd);
  8535. else /* Default action to exit DeepSleep */
  8536. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8537. }
  8538. mutex_lock(&cd->system_lock);
  8539. cd->sleep_state = SS_SLEEP_OFF;
  8540. mutex_unlock(&cd->system_lock);
  8541. pt_start_wd_timer(cd);
  8542. return rc;
  8543. }
  8544. /*******************************************************************************
  8545. * FUNCTION: pt_core_wake_
  8546. *
  8547. * SUMMARY: Protected call to pt_core_wake_ by exclusive access to the DUT.
  8548. *
  8549. * RETURN:
  8550. * 0 = success
  8551. * !0 = failure
  8552. *
  8553. * PARAMETERS:
  8554. * *cd - pointer to core data
  8555. ******************************************************************************/
  8556. static int pt_core_wake(struct pt_core_data *cd)
  8557. {
  8558. int rc;
  8559. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8560. if (rc < 0) {
  8561. pt_debug(cd->dev, DL_ERROR,
  8562. "%s: fail get exclusive ex=%p own=%p\n",
  8563. __func__, cd->exclusive_dev, cd->dev);
  8564. return rc;
  8565. }
  8566. rc = pt_core_wake_(cd);
  8567. if (release_exclusive(cd, cd->dev) < 0)
  8568. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8569. __func__);
  8570. else
  8571. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8572. __func__);
  8573. return rc;
  8574. }
  8575. /*******************************************************************************
  8576. * FUNCTION: pt_get_ic_crc_
  8577. *
  8578. * SUMMARY: This function retrieves the config block CRC
  8579. *
  8580. * NOTE: The post condition of calling this function will be that the DUT will
  8581. * be in SCANNINING mode if no failures occur
  8582. *
  8583. * RETURN:
  8584. * 0 = success
  8585. * !0 = failure
  8586. *
  8587. * PARAMETERS:
  8588. * *cd - pointer the core data structure
  8589. * ebid - enumerated block ID
  8590. ******************************************************************************/
  8591. static int pt_get_ic_crc_(struct pt_core_data *cd, u8 ebid)
  8592. {
  8593. struct pt_sysinfo *si = &cd->sysinfo;
  8594. int rc = 0;
  8595. u8 status;
  8596. u16 calculated_crc = 0;
  8597. u16 stored_crc = 0;
  8598. rc = pt_pip_suspend_scanning_(cd);
  8599. if (rc)
  8600. goto error;
  8601. rc = pt_pip_verify_config_block_crc_(cd, ebid, &status,
  8602. &calculated_crc, &stored_crc);
  8603. if (rc)
  8604. goto exit;
  8605. if (status) {
  8606. rc = -EINVAL;
  8607. goto exit;
  8608. }
  8609. si->ttconfig.crc = stored_crc;
  8610. exit:
  8611. pt_pip_resume_scanning_(cd);
  8612. error:
  8613. pt_debug(cd->dev, DL_INFO,
  8614. "%s: CRC: ebid:%d, calc:0x%04X, stored:0x%04X, rc=%d\n",
  8615. __func__, ebid, calculated_crc, stored_crc, rc);
  8616. return rc;
  8617. }
  8618. /*******************************************************************************
  8619. * FUNCTION: pt_pip2_read_gpio
  8620. *
  8621. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and stores the 32 gpio
  8622. * bits into the passed in variable
  8623. *
  8624. * NOTE: PIP2 READ_GPIO command is only supported in bootloader
  8625. *
  8626. * RETURN:
  8627. * 0 = success
  8628. * !0 = failure
  8629. *
  8630. * PARAMETERS:
  8631. * *dev - pointer to device structure
  8632. * *status - pointer to where the command response status is stored
  8633. * *gpio - pointer to device attributes structure
  8634. ******************************************************************************/
  8635. static int pt_pip2_read_gpio(struct device *dev, u8 *status, u32 *gpio)
  8636. {
  8637. int rc = 0;
  8638. u16 actual_read_len;
  8639. u8 read_buf[12];
  8640. u8 tmp_status = 0;
  8641. u8 index = PIP2_RESP_STATUS_OFFSET;
  8642. memset(read_buf, 0, ARRAY_SIZE(read_buf));
  8643. rc = _pt_request_pip2_send_cmd(dev,
  8644. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_READ_GPIO,
  8645. NULL, 0, read_buf, &actual_read_len);
  8646. if (rc) {
  8647. pt_debug(dev, DL_ERROR,
  8648. "%s: Failed to send PIP2 READ_GPIO cmd\n", __func__);
  8649. rc = -ECOMM;
  8650. } else {
  8651. tmp_status = read_buf[index];
  8652. }
  8653. if (status)
  8654. *status = tmp_status;
  8655. if (!rc && gpio && (tmp_status == 0)) {
  8656. *gpio = ((read_buf[index + 4] << 24) |
  8657. (read_buf[index + 3] << 16) |
  8658. (read_buf[index + 2] << 8) |
  8659. (read_buf[index + 1]));
  8660. }
  8661. return rc;
  8662. }
  8663. /*******************************************************************************
  8664. * FUNCTION: _pt_pip2_get_panel_id_by_gpio
  8665. *
  8666. * SUMMARY: Wrapper function to call pt_pip2_read_gpio() to get panel ID
  8667. *
  8668. * RETURN:
  8669. * 0 = success
  8670. * !0 = failure
  8671. *
  8672. * PARAMETERS:
  8673. * *cd - pointer the core data structure
  8674. * *pid - pointer to store panel ID
  8675. ******************************************************************************/
  8676. static int _pt_pip2_get_panel_id_by_gpio(struct pt_core_data *cd, u8 *pid)
  8677. {
  8678. u32 gpio_value = 0;
  8679. u8 status = 0;
  8680. u8 panel_id = PANEL_ID_NOT_ENABLED;
  8681. int rc = 0;
  8682. if (!pid)
  8683. return -ENOMEM;
  8684. rc = pt_pip2_read_gpio(cd->dev, &status, &gpio_value);
  8685. if (!rc) {
  8686. if (status == 0) {
  8687. panel_id = (gpio_value & PT_PANEL_ID_BITMASK) >>
  8688. PT_PANEL_ID_SHIFT;
  8689. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X %s=0x%08X\n",
  8690. __func__,
  8691. "BL mode PID", panel_id, "gpio", gpio_value);
  8692. *pid = panel_id;
  8693. } else {
  8694. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8695. __func__,
  8696. "BL read gpio failed status", status);
  8697. }
  8698. } else {
  8699. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8700. __func__,
  8701. "BL read gpio failed status", status);
  8702. }
  8703. return rc;
  8704. }
  8705. /*******************************************************************************
  8706. * FUNCTION: pt_enum_with_dut_
  8707. *
  8708. * SUMMARY: This function does the full enumeration of the DUT with TTDL.
  8709. * The core data (cd) startup_status will store, as a bitmask, each
  8710. * state of the enumeration process. The startup will be attempted
  8711. * PT_CORE_STARTUP_RETRY_COUNT times before giving up.
  8712. *
  8713. * RETURN:
  8714. * 0 = success
  8715. * !0 = failure
  8716. *
  8717. * PARAMETERS:
  8718. * *cd - pointer the core data structure
  8719. * reset - Flag to reset the DUT before attempting to enumerate
  8720. * *status - poionter to store the enum status bitmask flags
  8721. ******************************************************************************/
  8722. static int pt_enum_with_dut_(struct pt_core_data *cd, bool reset,
  8723. u32 *enum_status)
  8724. {
  8725. int try = 1;
  8726. int rc = 0;
  8727. int wait_time = 0;
  8728. bool detected = false;
  8729. u8 return_data[8];
  8730. u8 mode = PT_MODE_UNKNOWN;
  8731. u8 pid = PANEL_ID_NOT_ENABLED;
  8732. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  8733. struct pt_hid_desc hid_desc;
  8734. memset(&hid_desc, 0, sizeof(hid_desc));
  8735. #ifdef TTHE_TUNER_SUPPORT
  8736. tthe_print(cd, NULL, 0, "enter startup");
  8737. #endif
  8738. pt_debug(cd->dev, DL_INFO, "%s: Start enum... 0x%04X, reset=%d\n",
  8739. __func__, cd->startup_status, reset);
  8740. pt_stop_wd_timer(cd);
  8741. reset:
  8742. if (try > 1)
  8743. pt_debug(cd->dev, DL_WARN, "%s: DUT Enum Attempt %d\n",
  8744. __func__, try);
  8745. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8746. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8747. pt_debug(cd->dev, DL_INFO,
  8748. "%s: PIP1 Enumeration start\n", __func__);
  8749. /* Only reset the DUT after the first try */
  8750. if (reset || (try > 1)) {
  8751. /*
  8752. * Reset hardware only for Legacy parts. Skip for TT/TC
  8753. * parts because if the FW image was loaded directly
  8754. * to SRAM issueing a reset ill wipe out what was just
  8755. * loaded.
  8756. */
  8757. rc = pt_dut_reset_and_wait(cd);
  8758. if (rc < 0) {
  8759. pt_debug(cd->dev, DL_ERROR,
  8760. "%s: Error on h/w reset r=%d\n",
  8761. __func__, rc);
  8762. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8763. goto reset;
  8764. goto exit;
  8765. }
  8766. /* sleep to allow FW to be launched if available */
  8767. msleep(120);
  8768. }
  8769. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8770. if (rc < 0) {
  8771. pt_debug(cd->dev, DL_ERROR,
  8772. "%s: Error getting HID Descriptor r=%d\n",
  8773. __func__, rc);
  8774. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8775. goto reset;
  8776. rc = -EIO;
  8777. goto exit;
  8778. }
  8779. detected = true;
  8780. cd->mode = pt_get_mode(cd, &hid_desc);
  8781. /*
  8782. * Most systems do not use an XY pin as the panel_id and so
  8783. * the BL is used to retrieve the panel_id, however for
  8784. * systems that do use an XY pin, the panel_id MUST be
  8785. * retrieved from the system information when running FW
  8786. * (done below) and so this section of code is skipped.
  8787. * Entering the BL here is only needed on XY_PIN systems.
  8788. */
  8789. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  8790. if (cd->mode == PT_MODE_OPERATIONAL) {
  8791. rc = pt_pip_start_bootloader_(cd);
  8792. if (rc < 0) {
  8793. pt_debug(cd->dev, DL_ERROR,
  8794. "%s: Error on start bootloader r=%d\n",
  8795. __func__, rc);
  8796. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8797. goto reset;
  8798. goto exit;
  8799. }
  8800. cd->mode = PT_MODE_BOOTLOADER;
  8801. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8802. __func__);
  8803. }
  8804. rc = pt_hid_output_bl_get_information_(cd, return_data);
  8805. if (!rc) {
  8806. cd->bl_info.ready = true;
  8807. cd->bl_info.chip_id =
  8808. get_unaligned_le16(&return_data[2]);
  8809. pt_debug(cd->dev, DL_INFO, "%s: chip ID %04X\n",
  8810. __func__, cd->bl_info.chip_id);
  8811. } else {
  8812. pt_debug(cd->dev, DL_ERROR,
  8813. "%s: failed to get chip ID, r=%d\n",
  8814. __func__, rc);
  8815. }
  8816. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  8817. mutex_lock(&cd->system_lock);
  8818. if (!rc) {
  8819. cd->pid_for_loader = pid;
  8820. pt_debug(cd->dev, DL_INFO, "%s: Panel ID: 0x%02X\n",
  8821. __func__, cd->pid_for_loader);
  8822. } else {
  8823. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  8824. pt_debug(cd->dev, DL_WARN,
  8825. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  8826. __func__, cd->pid_for_loader);
  8827. }
  8828. mutex_unlock(&cd->system_lock);
  8829. }
  8830. /* Exit BL due to XY_PIN case or any other cause to be in BL */
  8831. if (cd->mode == PT_MODE_BOOTLOADER) {
  8832. rc = pt_hid_output_bl_launch_app_(cd);
  8833. if (rc < 0) {
  8834. pt_debug(cd->dev, DL_ERROR,
  8835. "%s: Error on launch app r=%d\n",
  8836. __func__, rc);
  8837. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8838. goto reset;
  8839. rc = -ENODEV;
  8840. goto exit;
  8841. }
  8842. /* sleep to allow FW to be launched if available */
  8843. msleep(120);
  8844. /* Ensure the DUT is now in Application mode */
  8845. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8846. if (rc < 0) {
  8847. pt_debug(cd->dev, DL_ERROR,
  8848. "%s: Error getting HID Desc r=%d\n",
  8849. __func__, rc);
  8850. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8851. goto reset;
  8852. rc = -EIO;
  8853. goto exit;
  8854. }
  8855. cd->mode = pt_get_mode(cd, &hid_desc);
  8856. if (cd->mode == PT_MODE_BOOTLOADER) {
  8857. pt_debug(cd->dev, DL_WARN,
  8858. "%s: Error confiming exit BL\n",
  8859. __func__);
  8860. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8861. goto reset;
  8862. rc = -ENODEV;
  8863. goto exit;
  8864. }
  8865. }
  8866. pt_debug(cd->dev, DL_INFO, "%s: Operational mode\n", __func__);
  8867. cd->mode = PT_MODE_OPERATIONAL;
  8868. *enum_status |= STARTUP_STATUS_GET_DESC;
  8869. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8870. } else {
  8871. /* Generation is PIP2 Capable */
  8872. pt_debug(cd->dev, DL_INFO,
  8873. "%s: PIP2 Enumeration start\n", __func__);
  8874. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8875. if (rc) {
  8876. pt_debug(cd->dev, DL_ERROR,
  8877. "%s: Get mode failed, mode unknown\n",
  8878. __func__);
  8879. mode = PT_MODE_UNKNOWN;
  8880. } else
  8881. detected = true;
  8882. cd->mode = mode;
  8883. switch (cd->mode) {
  8884. case PT_MODE_OPERATIONAL:
  8885. pt_debug(cd->dev, DL_INFO,
  8886. "%s: Operational mode\n", __func__);
  8887. if (cd->app_pip_ver_ready == false) {
  8888. rc = pt_pip2_get_version_(cd);
  8889. if (!rc)
  8890. cd->app_pip_ver_ready = true;
  8891. else {
  8892. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8893. goto reset;
  8894. goto exit;
  8895. }
  8896. }
  8897. break;
  8898. case PT_MODE_BOOTLOADER:
  8899. pt_debug(cd->dev, DL_INFO,
  8900. "%s: Bootloader mode\n", __func__);
  8901. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  8902. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  8903. mutex_lock(&cd->system_lock);
  8904. if (!rc) {
  8905. cd->pid_for_loader = pid;
  8906. pt_debug(cd->dev, DL_INFO,
  8907. "%s: Panel ID: 0x%02X\n",
  8908. __func__, cd->pid_for_loader);
  8909. } else {
  8910. cd->pid_for_loader =
  8911. PANEL_ID_NOT_ENABLED;
  8912. pt_debug(cd->dev, DL_WARN,
  8913. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  8914. __func__, cd->pid_for_loader);
  8915. }
  8916. mutex_unlock(&cd->system_lock);
  8917. }
  8918. if (cd->bl_pip_ver_ready == false) {
  8919. rc = pt_pip2_get_version_(cd);
  8920. if (!rc)
  8921. cd->bl_pip_ver_ready = true;
  8922. else {
  8923. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8924. goto reset;
  8925. goto exit;
  8926. }
  8927. }
  8928. /*
  8929. * Launch app command will fail in flashless mode.
  8930. * Skip launch app command here to save time for
  8931. * enumeration flow.
  8932. */
  8933. if (cd->flashless_dut)
  8934. goto exit;
  8935. /*
  8936. * pt_pip2_launch_app() is needed here instead of
  8937. * pt_pip2_exit_bl() because exit_bl will cause several
  8938. * hw_resets to occur and the auto BL on a flashless
  8939. * DUT will fail.
  8940. */
  8941. rc = pt_pip2_launch_app(cd->dev,
  8942. PT_CORE_CMD_UNPROTECTED);
  8943. if (rc) {
  8944. pt_debug(cd->dev, DL_ERROR,
  8945. "%s: Error on launch app r=%d\n",
  8946. __func__, rc);
  8947. msleep(50);
  8948. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8949. goto reset;
  8950. goto exit;
  8951. }
  8952. /*
  8953. * IRQ thread can be delayed if the serial log print is
  8954. * enabled. It causes next command to get wrong response
  8955. * Here the delay is to ensure pt_parse_input() to be
  8956. * finished.
  8957. */
  8958. msleep(60);
  8959. /* Check and update the mode */
  8960. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8961. if (rc) {
  8962. pt_debug(cd->dev, DL_ERROR,
  8963. "%s: Get mode failed, mode unknown\n",
  8964. __func__);
  8965. mode = PT_MODE_UNKNOWN;
  8966. }
  8967. cd->mode = mode;
  8968. if (cd->mode == PT_MODE_OPERATIONAL) {
  8969. pt_debug(cd->dev, DL_INFO,
  8970. "%s: Launched to Operational mode\n",
  8971. __func__);
  8972. } else if (cd->mode == PT_MODE_BOOTLOADER) {
  8973. pt_debug(cd->dev, DL_ERROR,
  8974. "%s: Launch failed, Bootloader mode\n",
  8975. __func__);
  8976. goto exit;
  8977. } else if (cd->mode == PT_MODE_UNKNOWN) {
  8978. pt_debug(cd->dev, DL_ERROR,
  8979. "%s: Launch failed, Unknown mode\n",
  8980. __func__);
  8981. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8982. goto reset;
  8983. goto exit;
  8984. }
  8985. if (cd->app_pip_ver_ready == false) {
  8986. rc = pt_pip2_get_version_(cd);
  8987. if (!rc)
  8988. cd->app_pip_ver_ready = true;
  8989. else {
  8990. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8991. goto reset;
  8992. goto exit;
  8993. }
  8994. }
  8995. break;
  8996. default:
  8997. pt_debug(cd->dev, DL_ERROR,
  8998. "%s: Unknown mode\n", __func__);
  8999. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9000. goto reset;
  9001. goto exit;
  9002. }
  9003. *enum_status |= STARTUP_STATUS_GET_DESC;
  9004. }
  9005. pt_init_pip_report_fields(cd);
  9006. *enum_status |= STARTUP_STATUS_GET_RPT_DESC;
  9007. if (!cd->features.easywake)
  9008. cd->easy_wakeup_gesture = PT_CORE_EWG_NONE;
  9009. pt_debug(cd->dev, DL_INFO, "%s: Reading sysinfo\n", __func__);
  9010. rc = pt_hid_output_get_sysinfo_(cd);
  9011. if (rc) {
  9012. pt_debug(cd->dev, DL_ERROR,
  9013. "%s: Error on getting sysinfo r=%d\n", __func__, rc);
  9014. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9015. goto reset;
  9016. goto exit;
  9017. }
  9018. *enum_status |= STARTUP_STATUS_GET_SYS_INFO;
  9019. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  9020. rc = _pt_poll_for_fw_exit_boot_mode(cd, 10000, &wait_time);
  9021. if (!rc) {
  9022. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9023. pt_debug(cd->dev, DL_WARN,
  9024. "%s: Exit FW BOOT Mode after %dms\n",
  9025. __func__, wait_time);
  9026. } else {
  9027. pt_debug(cd->dev, DL_WARN,
  9028. "%s: FW stuck in BOOT Mode after %dms\n",
  9029. __func__, wait_time);
  9030. goto exit;
  9031. }
  9032. pt_debug(cd->dev, DL_INFO, "%s pt Prot Version: %d.%d\n",
  9033. __func__,
  9034. cd->sysinfo.ttdata.pip_ver_major,
  9035. cd->sysinfo.ttdata.pip_ver_minor);
  9036. rc = pt_get_ic_crc_(cd, PT_TCH_PARM_EBID);
  9037. if (rc) {
  9038. pt_debug(cd->dev, DL_ERROR,
  9039. "%s: DUT Config block CRC failure rc=%d\n",
  9040. __func__, rc);
  9041. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9042. goto reset;
  9043. } else {
  9044. _pt_get_fw_sys_mode(cd, &sys_mode, NULL);
  9045. if (sys_mode != FW_SYS_MODE_SCANNING) {
  9046. pt_debug(cd->dev, DL_ERROR,
  9047. "%s: scan state: %d, retry: %d\n",
  9048. __func__, sys_mode, try);
  9049. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9050. goto reset;
  9051. } else
  9052. *enum_status |= STARTUP_STATUS_GET_CFG_CRC;
  9053. }
  9054. rc = pt_restore_parameters_(cd);
  9055. if (rc) {
  9056. pt_debug(cd->dev, DL_ERROR,
  9057. "%s: Failed to restore parameters rc=%d\n",
  9058. __func__, rc);
  9059. } else
  9060. *enum_status |= STARTUP_STATUS_RESTORE_PARM;
  9061. call_atten_cb(cd, PT_ATTEN_STARTUP, 0);
  9062. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  9063. cd->startup_retry_count = 0;
  9064. exit:
  9065. /* Generate the HW Version of the connected DUT and store in cd */
  9066. pt_generate_hw_version(cd, cd->hw_version);
  9067. pt_debug(cd->dev, DL_WARN, "%s: HW Version: %s\n", __func__,
  9068. cd->hw_version);
  9069. pt_start_wd_timer(cd);
  9070. if (!detected)
  9071. rc = -ENODEV;
  9072. #ifdef TTHE_TUNER_SUPPORT
  9073. tthe_print(cd, NULL, 0, "exit startup");
  9074. #endif
  9075. pt_debug(cd->dev, DL_WARN,
  9076. "%s: Completed Enumeration rc=%d On Attempt %d\n",
  9077. __func__, rc, try);
  9078. return rc;
  9079. }
  9080. /*******************************************************************************
  9081. * FUNCTION: pt_enum_with_dut
  9082. *
  9083. * SUMMARY: This is the safe function wrapper for pt_enum_with_dut_() by
  9084. * requesting exclusive access around the call.
  9085. *
  9086. * RETURN:
  9087. * 0 = success
  9088. * !0 = failure
  9089. *
  9090. * PARAMETERS:
  9091. * *cd - pointer the core data structure
  9092. * reset - Flag to reset the DUT before attempting to enumerate
  9093. * *status - pointer to store the enum status bitmask flags
  9094. ******************************************************************************/
  9095. static int pt_enum_with_dut(struct pt_core_data *cd, bool reset, u32 *status)
  9096. {
  9097. int rc = 0;
  9098. mutex_lock(&cd->system_lock);
  9099. cd->startup_state = STARTUP_RUNNING;
  9100. mutex_unlock(&cd->system_lock);
  9101. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  9102. if (rc) {
  9103. pt_debug(cd->dev, DL_ERROR,
  9104. "%s: fail get exclusive ex=%p own=%p\n",
  9105. __func__, cd->exclusive_dev, cd->dev);
  9106. goto exit;
  9107. }
  9108. rc = pt_enum_with_dut_(cd, reset, status);
  9109. if (release_exclusive(cd, cd->dev) < 0)
  9110. /* Don't return fail code, mode is already changed. */
  9111. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  9112. __func__);
  9113. else
  9114. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  9115. __func__);
  9116. exit:
  9117. mutex_lock(&cd->system_lock);
  9118. /* Clear startup state for any tasks waiting for startup completion */
  9119. cd->startup_state = STARTUP_NONE;
  9120. mutex_unlock(&cd->system_lock);
  9121. /* Set STATUS_COMPLETE bit to indicate the status is ready to be read */
  9122. *status |= STARTUP_STATUS_COMPLETE;
  9123. /* Wake the waiters for end of startup */
  9124. wake_up(&cd->wait_q);
  9125. return rc;
  9126. }
  9127. static int add_sysfs_interfaces(struct device *dev);
  9128. static void remove_sysfs_interfaces(struct device *dev);
  9129. static void remove_sysfs_and_modules(struct device *dev);
  9130. static void pt_release_modules(struct pt_core_data *cd);
  9131. static void pt_probe_modules(struct pt_core_data *cd);
  9132. /*******************************************************************************
  9133. * FUNCTION: _pt_ttdl_restart
  9134. *
  9135. * SUMMARY: Restarts TTDL enumeration with the DUT and re-probes all modules
  9136. *
  9137. * NOTE: The function DOSE NOT remove sysfs and modules. Trying to create
  9138. * existing sysfs nodes will produce an error.
  9139. *
  9140. * RETURN:
  9141. * 0 = success
  9142. * !0 = failure
  9143. *
  9144. * PARAMETERS:
  9145. * *dev - pointer to core device
  9146. ******************************************************************************/
  9147. static int _pt_ttdl_restart(struct device *dev)
  9148. {
  9149. int rc = 0;
  9150. struct pt_core_data *cd = dev_get_drvdata(dev);
  9151. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9152. struct i2c_client *client =
  9153. (struct i2c_client *)container_of(dev, struct i2c_client, dev);
  9154. #endif
  9155. /*
  9156. * Make sure the device is awake, pt_mt_release function will
  9157. * cause pm sleep function and lead to deadlock.
  9158. */
  9159. pm_runtime_get_sync(dev);
  9160. /* Use ttdl_restart_lock to avoid reentry */
  9161. mutex_lock(&cd->ttdl_restart_lock);
  9162. remove_sysfs_and_modules(cd->dev);
  9163. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9164. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  9165. pt_debug(dev, DL_ERROR,
  9166. "%s I2C functionality not Supported\n", __func__);
  9167. rc = -EIO;
  9168. goto ttdl_no_error;
  9169. }
  9170. #endif
  9171. if (cd->active_dut_generation == DUT_UNKNOWN) {
  9172. rc = _pt_detect_dut_generation(cd->dev,
  9173. &cd->startup_status, &cd->active_dut_generation,
  9174. &cd->mode);
  9175. if ((cd->active_dut_generation == DUT_UNKNOWN) || (rc)) {
  9176. pt_debug(dev, DL_ERROR,
  9177. "%s: Error, Unknown DUT Generation rc=%d\n",
  9178. __func__, rc);
  9179. }
  9180. }
  9181. rc = add_sysfs_interfaces(cd->dev);
  9182. if (rc < 0)
  9183. pt_debug(cd->dev, DL_ERROR,
  9184. "%s: Error, failed sysfs nodes rc=%d\n",
  9185. __func__, rc);
  9186. if (!(cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL)) {
  9187. pt_debug(dev, DL_INFO, "%s: Call pt_enum_with_dut\n", __func__);
  9188. rc = pt_enum_with_dut(cd, true, &cd->startup_status);
  9189. if (rc < 0)
  9190. pt_debug(dev, DL_ERROR,
  9191. "%s: Error, Failed to Enumerate\n", __func__);
  9192. }
  9193. rc = pt_mt_probe(dev);
  9194. if (rc < 0) {
  9195. pt_debug(dev, DL_ERROR,
  9196. "%s: Error, fail mt probe\n", __func__);
  9197. }
  9198. rc = pt_btn_probe(dev);
  9199. if (rc < 0) {
  9200. pt_debug(dev, DL_ERROR,
  9201. "%s: Error, fail btn probe\n", __func__);
  9202. }
  9203. pt_probe_modules(cd);
  9204. pt_debug(cd->dev, DL_WARN,
  9205. "%s: Well Done! TTDL Restart Completed\n", __func__);
  9206. rc = 0;
  9207. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9208. ttdl_no_error:
  9209. #endif
  9210. mutex_unlock(&cd->ttdl_restart_lock);
  9211. mutex_lock(&cd->system_lock);
  9212. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  9213. cd->startup_state = STARTUP_NONE;
  9214. mutex_unlock(&cd->system_lock);
  9215. pm_runtime_put(dev);
  9216. return rc;
  9217. }
  9218. /*******************************************************************************
  9219. * FUNCTION: pt_restart_work_function
  9220. *
  9221. * SUMMARY: This is the wrapper function placed in a work queue to call
  9222. * _pt_ttdl_restart()
  9223. *
  9224. * RETURN: none
  9225. *
  9226. * PARAMETERS:
  9227. * *work - pointer to the work_struct
  9228. ******************************************************************************/
  9229. static void pt_restart_work_function(struct work_struct *work)
  9230. {
  9231. struct pt_core_data *cd = container_of(work,
  9232. struct pt_core_data, ttdl_restart_work);
  9233. int rc = 0;
  9234. rc = _pt_ttdl_restart(cd->dev);
  9235. if (rc < 0)
  9236. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9237. __func__, rc);
  9238. }
  9239. /*******************************************************************************
  9240. * FUNCTION: pt_enum_work_function
  9241. *
  9242. * SUMMARY: This is the wrapper function placed in a work queue to call
  9243. * pt_enum_with_dut()
  9244. *
  9245. * RETURN: none
  9246. *
  9247. * PARAMETERS:
  9248. * *work - pointer to the work_struct
  9249. ******************************************************************************/
  9250. static void pt_enum_work_function(struct work_struct *work)
  9251. {
  9252. struct pt_core_data *cd = container_of(work,
  9253. struct pt_core_data, enum_work);
  9254. int rc;
  9255. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  9256. if (rc < 0)
  9257. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9258. __func__, rc);
  9259. }
  9260. #if (KERNEL_VERSION(3, 19, 0) <= LINUX_VERSION_CODE)
  9261. #define KERNEL_VER_GT_3_19
  9262. #endif
  9263. #if defined(CONFIG_PM_RUNTIME) || defined(KERNEL_VER_GT_3_19)
  9264. /* CONFIG_PM_RUNTIME option is removed in 3.19.0 */
  9265. #if defined(CONFIG_PM_SLEEP)
  9266. /*******************************************************************************
  9267. * FUNCTION: pt_core_rt_suspend
  9268. *
  9269. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_sleep.
  9270. *
  9271. * RETURN:
  9272. * 0 = success
  9273. * !0 = failure
  9274. *
  9275. * PARAMETERS:
  9276. * *dev - pointer to core device
  9277. ******************************************************************************/
  9278. static int pt_core_rt_suspend(struct device *dev)
  9279. {
  9280. struct pt_core_data *cd = dev_get_drvdata(dev);
  9281. int rc = 0;
  9282. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  9283. return 0;
  9284. rc = pt_core_sleep(cd);
  9285. if (rc < 0) {
  9286. pt_debug(dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  9287. return -EAGAIN;
  9288. }
  9289. return 0;
  9290. }
  9291. /*******************************************************************************
  9292. * FUNCTION: pt_core_rt_resume
  9293. *
  9294. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_wake.
  9295. *
  9296. * RETURN:
  9297. * 0 = success
  9298. * !0 = failure
  9299. *
  9300. * PARAMETERS:
  9301. * *dev - pointer to core device
  9302. ******************************************************************************/
  9303. static int pt_core_rt_resume(struct device *dev)
  9304. {
  9305. struct pt_core_data *cd = dev_get_drvdata(dev);
  9306. int rc = 0;
  9307. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  9308. return 0;
  9309. rc = pt_core_wake(cd);
  9310. if (rc < 0) {
  9311. pt_debug(dev, DL_ERROR, "%s: Error on wake\n", __func__);
  9312. return -EAGAIN;
  9313. }
  9314. return 0;
  9315. }
  9316. #endif /* CONFIG_PM_SLEEP */
  9317. #endif /* CONFIG_PM_RUNTIME || LINUX_VERSION_CODE */
  9318. #if defined(CONFIG_PM_SLEEP)
  9319. /*******************************************************************************
  9320. * FUNCTION: pt_core_suspend_
  9321. *
  9322. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9323. * pt_core_sleep. This function may disable IRQ during sleep state.
  9324. *
  9325. * RETURN:
  9326. * 0 = success
  9327. * !0 = failure
  9328. *
  9329. * PARAMETERS:
  9330. * *dev - pointer to core device
  9331. ******************************************************************************/
  9332. static int pt_core_suspend_(struct device *dev)
  9333. {
  9334. struct pt_core_data *cd = dev_get_drvdata(dev);
  9335. pt_core_sleep(cd);
  9336. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9337. return 0;
  9338. /* Required to prevent interrupts before bus awake */
  9339. disable_irq(cd->irq);
  9340. cd->irq_disabled = 1;
  9341. if (device_may_wakeup(dev)) {
  9342. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9343. __func__);
  9344. if (!enable_irq_wake(cd->irq))
  9345. cd->irq_wake = 1;
  9346. } else {
  9347. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9348. __func__);
  9349. }
  9350. return 0;
  9351. }
  9352. /*******************************************************************************
  9353. * FUNCTION: pt_core_suspend
  9354. *
  9355. * SUMMARY: Wrapper function of pt_core_suspend_() to help avoid TP from being
  9356. * woke up or put to sleep based on Kernel power state even when the display
  9357. * is off based on the check of TTDL core platform flag.
  9358. *
  9359. * RETURN:
  9360. * 0 = success
  9361. * !0 = failure
  9362. *
  9363. * PARAMETERS:
  9364. * *dev - pointer to core device
  9365. ******************************************************************************/
  9366. static int pt_core_suspend(struct device *dev)
  9367. {
  9368. struct pt_core_data *cd = dev_get_drvdata(dev);
  9369. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9370. return 0;
  9371. return pt_core_suspend_(dev);
  9372. }
  9373. /*******************************************************************************
  9374. * FUNCTION: pt_core_resume_
  9375. *
  9376. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9377. * pt_core_wake. This function may enable IRQ before wake up.
  9378. *
  9379. * RETURN:
  9380. * 0 = success
  9381. * !0 = failure
  9382. *
  9383. * PARAMETERS:
  9384. * *dev - pointer to core device
  9385. ******************************************************************************/
  9386. static int pt_core_resume_(struct device *dev)
  9387. {
  9388. struct pt_core_data *cd = dev_get_drvdata(dev);
  9389. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9390. goto exit;
  9391. /*
  9392. * Bus pm does not call suspend if device runtime suspended
  9393. * This flag is covers that case
  9394. */
  9395. if (cd->irq_disabled) {
  9396. enable_irq(cd->irq);
  9397. cd->irq_disabled = 0;
  9398. }
  9399. if (device_may_wakeup(dev)) {
  9400. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9401. __func__);
  9402. if (cd->irq_wake) {
  9403. disable_irq_wake(cd->irq);
  9404. cd->irq_wake = 0;
  9405. }
  9406. } else {
  9407. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9408. __func__);
  9409. }
  9410. exit:
  9411. pt_core_wake(cd);
  9412. return 0;
  9413. }
  9414. /*******************************************************************************
  9415. * FUNCTION: pt_core_resume
  9416. *
  9417. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9418. * along with kernel power state even the display is off based on the check of
  9419. * TTDL core platform flag.
  9420. *
  9421. * RETURN:
  9422. * 0 = success
  9423. * !0 = failure
  9424. *
  9425. * PARAMETERS:
  9426. * *dev - pointer to core device
  9427. ******************************************************************************/
  9428. static int pt_core_resume(struct device *dev)
  9429. {
  9430. struct pt_core_data *cd = dev_get_drvdata(dev);
  9431. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9432. return 0;
  9433. return pt_core_resume_(dev);
  9434. }
  9435. #endif
  9436. #ifdef NEED_SUSPEND_NOTIFIER
  9437. /*******************************************************************************
  9438. * FUNCTION: pt_pm_notifier
  9439. *
  9440. * SUMMARY: This function is registered to notifier chain and will perform
  9441. * suspend operation if match event PM_SUSPEND_PREPARE.
  9442. *
  9443. * RETURN:
  9444. * 0 = success
  9445. * !0 = failure
  9446. *
  9447. * PARAMETERS:
  9448. * *nb - pointer to notifier_block structure
  9449. * action - notifier event type
  9450. * *data - void pointer
  9451. ******************************************************************************/
  9452. static int pt_pm_notifier(struct notifier_block *nb,
  9453. unsigned long action, void *data)
  9454. {
  9455. struct pt_core_data *cd = container_of(nb,
  9456. struct pt_core_data, pm_notifier);
  9457. if (action == PM_SUSPEND_PREPARE) {
  9458. pt_debug(cd->dev, DL_INFO, "%s: Suspend prepare\n",
  9459. __func__);
  9460. /*
  9461. * If PM runtime is not suspended, either call runtime
  9462. * PM suspend callback or wait until it finishes
  9463. */
  9464. if (!pm_runtime_suspended(cd->dev))
  9465. pm_runtime_suspend(cd->dev);
  9466. (void) pt_core_suspend(cd->dev);
  9467. }
  9468. return NOTIFY_DONE;
  9469. }
  9470. #endif
  9471. const struct dev_pm_ops pt_pm_ops = {
  9472. SET_SYSTEM_SLEEP_PM_OPS(pt_core_suspend, pt_core_resume)
  9473. SET_RUNTIME_PM_OPS(pt_core_rt_suspend, pt_core_rt_resume,
  9474. NULL)
  9475. };
  9476. EXPORT_SYMBOL_GPL(pt_pm_ops);
  9477. /*******************************************************************************
  9478. * FUNCTION: _pt_request_pip2_enter_bl
  9479. *
  9480. * SUMMARY: Force the DUT to enter the BL by resetting the DUT by use of the
  9481. * XRES pin or a soft reset.
  9482. *
  9483. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  9484. * the WD active could cause this function to fail!
  9485. * NOTE: If start_mode is passed in as PT_MODE_IGNORE, this function
  9486. * will not try to determine the current mode but will proceed with
  9487. * resetting the DUT and entering the BL.
  9488. *
  9489. * NOTE: The definition of result code:
  9490. * PT_ENTER_BL_PASS (0)
  9491. * PT_ENTER_BL_ERROR (1)
  9492. * PT_ENTER_BL_RESET_FAIL (2)
  9493. * PT_ENTER_BL_HID_START_BL_FAIL (3)
  9494. * PT_ENTER_BL_CONFIRM_FAIL (4)
  9495. * PT_ENTER_BL_GET_FLASH_INFO_FAIL (5)
  9496. *
  9497. * RETURNS:
  9498. * 0 = success
  9499. * !0 = failure
  9500. *
  9501. *
  9502. * PARAMETERS:
  9503. * *dev - pointer to device structure
  9504. * *start_mode - pointer to the mode the DUT was in when this function
  9505. * starts
  9506. * *result - pointer to store the result when to enter BL
  9507. ******************************************************************************/
  9508. int _pt_request_pip2_enter_bl(struct device *dev, u8 *start_mode, int *result)
  9509. {
  9510. int rc = 0;
  9511. int t;
  9512. int tmp_result = PT_ENTER_BL_ERROR;
  9513. int flash_info_retry = 2;
  9514. u8 mode = PT_MODE_UNKNOWN;
  9515. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  9516. u8 read_buf[32];
  9517. u16 actual_read_len;
  9518. struct pt_core_data *cd = dev_get_drvdata(dev);
  9519. u8 host_mode_cmd[4] = {0xA5, 0xA5, 0xA5, 0xA5};
  9520. u8 time = 0;
  9521. u8 saved_flashless_auto_bl_mode = cd->flashless_auto_bl;
  9522. if (cd->watchdog_enabled) {
  9523. pt_debug(dev, DL_WARN,
  9524. "%s: Watchdog must be stopped before entering BL\n",
  9525. __func__);
  9526. goto exit;
  9527. }
  9528. cancel_work_sync(&cd->enum_work);
  9529. cancel_work_sync(&cd->watchdog_work);
  9530. /* if undefined assume operational/test to bypass all checks */
  9531. if (*start_mode == PT_MODE_IGNORE) {
  9532. mode = PT_MODE_OPERATIONAL;
  9533. sys_mode = FW_SYS_MODE_TEST;
  9534. pt_debug(dev, DL_INFO, "%s: Assume Mode = %d", __func__, mode);
  9535. } else if (*start_mode == PT_MODE_UNKNOWN) {
  9536. rc = pt_pip2_get_mode_sysmode_(cd, &mode, &sys_mode);
  9537. if (rc) {
  9538. pt_debug(dev, DL_ERROR,
  9539. "%s: Get mode failed, mode unknown\n",
  9540. __func__);
  9541. }
  9542. *start_mode = mode;
  9543. pt_debug(dev, DL_INFO, "%s: Get Mode = %d", __func__, mode);
  9544. } else if (*start_mode == PT_MODE_OPERATIONAL) {
  9545. /* Assume SCANNIING mode to avoid doing an extra get_mode */
  9546. sys_mode = FW_SYS_MODE_SCANNING;
  9547. }
  9548. _retry:
  9549. /* For Flashless DUTs - Suppress auto BL on next BL sentinel */
  9550. pt_debug(dev, DL_INFO, "%s: Flashless Auto_BL - SUPPRESS\n", __func__);
  9551. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  9552. switch (mode) {
  9553. case PT_MODE_UNKNOWN:
  9554. /*
  9555. * When the mode could not be determined the DUT could be
  9556. * in App mode running corrupted FW or FW that is not
  9557. * responding to the mode request, assume no communication
  9558. * and do a hard reset
  9559. */
  9560. mutex_lock(&cd->system_lock);
  9561. cd->startup_status = STARTUP_STATUS_START;
  9562. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9563. mutex_unlock(&cd->system_lock);
  9564. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  9565. if (rc) {
  9566. tmp_result = PT_ENTER_BL_RESET_FAIL;
  9567. goto exit;
  9568. }
  9569. break;
  9570. case PT_MODE_OPERATIONAL:
  9571. if (sys_mode == FW_SYS_MODE_SCANNING) {
  9572. pt_debug(dev, DL_INFO, "%s: Suspend Scanning\n",
  9573. __func__);
  9574. rc = pt_pip_suspend_scanning_(cd);
  9575. if (rc) {
  9576. /*
  9577. * Print to log but don't exit, the FW could be
  9578. * running but be hung or fail to respond to
  9579. * this request
  9580. */
  9581. pt_debug(dev, DL_ERROR,
  9582. "%s Suspend Scan Failed\n", __func__);
  9583. }
  9584. /* sleep to allow the suspend scan to be processed */
  9585. usleep_range(1000, 2000);
  9586. }
  9587. mutex_lock(&cd->system_lock);
  9588. cd->startup_status = STARTUP_STATUS_START;
  9589. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9590. mutex_unlock(&cd->system_lock);
  9591. /* Reset device to enter the BL */
  9592. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  9593. if (rc) {
  9594. tmp_result = PT_ENTER_BL_RESET_FAIL;
  9595. goto exit;
  9596. }
  9597. break;
  9598. case PT_MODE_BOOTLOADER:
  9599. /* Do nothing as we are already in the BL */
  9600. tmp_result = PT_ENTER_BL_PASS;
  9601. goto exit;
  9602. default:
  9603. /* Should NEVER get here */
  9604. tmp_result = PT_ENTER_BL_ERROR;
  9605. pt_debug(dev, DL_ERROR, "%s: Unknown mode code\n", __func__);
  9606. goto exit;
  9607. }
  9608. if (!cd->flashless_dut &&
  9609. (mode == PT_MODE_UNKNOWN || mode == PT_MODE_OPERATIONAL)) {
  9610. /*
  9611. * Sending the special "Host Mode" command will instruct the
  9612. * BL to not execute the FW it has loaded into RAM.
  9613. * The command must be sent within a 40ms window from releasing
  9614. * the XRES pin. If the messages is sent too early it will NAK,
  9615. * so keep sending it every 2ms until it is accepted by the BL.
  9616. * A no-flash DUT does not require this command as there is no
  9617. * FW for the BL to load and execute.
  9618. */
  9619. usleep_range(4000, 6000);
  9620. pt_debug(cd->dev, DL_INFO,
  9621. ">>> %s: Write Buffer Size[%d] Stay in BL\n",
  9622. __func__, (int)sizeof(host_mode_cmd));
  9623. pt_pr_buf(cd->dev, DL_DEBUG, host_mode_cmd,
  9624. (int)sizeof(host_mode_cmd), ">>> User CMD");
  9625. rc = 1;
  9626. while (rc && time < 34) {
  9627. rc = pt_adap_write_read_specific(cd, 4,
  9628. host_mode_cmd, NULL);
  9629. usleep_range(1800, 2000);
  9630. time += 2;
  9631. }
  9632. /* Sleep to allow the BL to come up */
  9633. usleep_range(1000, 2000);
  9634. }
  9635. /*
  9636. * To avoid the case that next PIP command can be confused by BL/FW
  9637. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  9638. * followed by "wakeup event" function can lower the failure rate.
  9639. */
  9640. t = wait_event_timeout(cd->wait_q,
  9641. ((cd->startup_status != STARTUP_STATUS_START)
  9642. && (cd->hid_reset_cmd_state == 0)),
  9643. msecs_to_jiffies(300));
  9644. if (IS_TMO(t)) {
  9645. pt_debug(cd->dev, DL_ERROR,
  9646. "%s: TMO waiting for BL sentinel\n", __func__);
  9647. }
  9648. /* Check if device is now in BL mode */
  9649. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9650. pt_debug(dev, DL_INFO, "%s: Mode=%d, Status=0x%04X\n", __func__, mode,
  9651. cd->startup_status);
  9652. if (!rc && mode == PT_MODE_BOOTLOADER) {
  9653. pt_debug(dev, DL_INFO, "%s In bootloader mode now\n", __func__);
  9654. mutex_lock(&cd->system_lock);
  9655. cd->pip2_prot_active = true;
  9656. cd->mode = PT_MODE_BOOTLOADER;
  9657. mutex_unlock(&cd->system_lock);
  9658. tmp_result = PT_ENTER_BL_PASS;
  9659. } else {
  9660. /*
  9661. * If the device doesn't enter BL mode as expected and rc is
  9662. * tested pass by above function pt_pip2_get_mode_sysmode_(),
  9663. * the function should return an error code to indicate this
  9664. * failure PT_ENTER_BL_CONFIRM_FAIL.
  9665. */
  9666. if (!rc)
  9667. rc = -EINVAL;
  9668. tmp_result = PT_ENTER_BL_CONFIRM_FAIL;
  9669. mutex_lock(&cd->system_lock);
  9670. cd->mode = mode;
  9671. mutex_unlock(&cd->system_lock);
  9672. pt_debug(dev, DL_ERROR,
  9673. "%s ERROR: Not in BL as expected", __func__);
  9674. }
  9675. exit:
  9676. if (!cd->flashless_dut && (tmp_result == PT_ENTER_BL_PASS)) {
  9677. /* Check to get (buffered) flash information */
  9678. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  9679. PIP2_CMD_ID_FLASH_INFO, NULL, 0,
  9680. read_buf, &actual_read_len);
  9681. if (!rc) {
  9682. if (read_buf[PIP2_RESP_BODY_OFFSET] == 0) {
  9683. pt_debug(
  9684. dev, DL_WARN,
  9685. "%s Unavailable Manufacturer ID: 0x%02x\n",
  9686. __func__,
  9687. read_buf[PIP2_RESP_BODY_OFFSET]);
  9688. /*
  9689. * If the BL was unable to cache the correct
  9690. * values when entering the first time due to
  9691. * the Flash part not having been powered up
  9692. * long enough, re-enter the BL to trigger the
  9693. * BL to re-attempt to cache the values.
  9694. */
  9695. if (flash_info_retry-- > 0) {
  9696. mode = PT_MODE_UNKNOWN;
  9697. pt_debug(dev, DL_WARN,
  9698. "%s Assume mode to UNKNOWN to enter BL again, retry=%d\n",
  9699. __func__, flash_info_retry);
  9700. goto _retry;
  9701. } else {
  9702. pt_debug(dev, DL_WARN,
  9703. "%s Manufacturer ID Unknown\n",
  9704. __func__);
  9705. tmp_result = PT_ENTER_BL_PASS;
  9706. }
  9707. }
  9708. } else {
  9709. tmp_result = PT_ENTER_BL_GET_FLASH_INFO_FAIL;
  9710. pt_debug(
  9711. dev, DL_ERROR,
  9712. "%s: Failed to send PIP2 READ_FLASH_INFO cmd\n",
  9713. __func__);
  9714. }
  9715. }
  9716. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - %s\n", __func__,
  9717. saved_flashless_auto_bl_mode == PT_ALLOW_AUTO_BL ? "ALLOW" :
  9718. "SUPPRESS");
  9719. cd->flashless_auto_bl = saved_flashless_auto_bl_mode;
  9720. if (result)
  9721. *result = tmp_result;
  9722. return rc;
  9723. }
  9724. /*******************************************************************************
  9725. * FUNCTION: _pt_pip2_file_open
  9726. *
  9727. * SUMMARY: Using the BL PIP2 commands open a file and return the file handle
  9728. *
  9729. * NOTE: The DUT must be in BL mode for this command to work
  9730. *
  9731. * RETURNS:
  9732. * <0 = Error
  9733. * >0 = file handle opened
  9734. *
  9735. * PARAMETERS:
  9736. * *dev - pointer to device structure
  9737. * file_no - PIP2 file number to open
  9738. ******************************************************************************/
  9739. int _pt_pip2_file_open(struct device *dev, u8 file_no)
  9740. {
  9741. int ret = 0;
  9742. u16 status;
  9743. u16 actual_read_len;
  9744. u8 file_handle;
  9745. u8 data[2];
  9746. u8 read_buf[10];
  9747. pt_debug(dev, DL_DEBUG, "%s: OPEN file %d\n", __func__, file_no);
  9748. data[0] = file_no;
  9749. ret = _pt_request_pip2_send_cmd(dev,
  9750. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_OPEN,
  9751. data, 1, read_buf, &actual_read_len);
  9752. if (ret) {
  9753. pt_debug(dev, DL_ERROR,
  9754. "%s ROM BL FILE_OPEN timeout for file = %d\n",
  9755. __func__, file_no);
  9756. return -PIP2_RSP_ERR_NOT_OPEN;
  9757. }
  9758. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  9759. file_handle = read_buf[PIP2_RESP_BODY_OFFSET];
  9760. if (ret || ((status != 0x00) && (status != 0x03)) ||
  9761. (file_handle != file_no)) {
  9762. pt_debug(dev, DL_ERROR,
  9763. "%s ROM BL FILE_OPEN failure: 0x%02X for file = %d\n",
  9764. __func__, status, file_handle);
  9765. return -status;
  9766. }
  9767. return file_handle;
  9768. }
  9769. /*******************************************************************************
  9770. * FUNCTION: _pt_pip2_file_close
  9771. *
  9772. * SUMMARY: Using the BL PIP2 commands close a file
  9773. *
  9774. * NOTE: The DUT must be in BL mode for this command to work
  9775. *
  9776. * RETURNS:
  9777. * <0 = Error
  9778. * >0 = file handle closed
  9779. *
  9780. * PARAMETERS:
  9781. * *dev - pointer to device structure
  9782. * file_handle - handle to the file to be closed
  9783. ******************************************************************************/
  9784. int _pt_pip2_file_close(struct device *dev, u8 file_handle)
  9785. {
  9786. int ret = 0;
  9787. u16 status;
  9788. u16 actual_read_len;
  9789. u8 data[2];
  9790. u8 read_buf[32];
  9791. pt_debug(dev, DL_DEBUG, "%s: CLOSE file %d\n", __func__, file_handle);
  9792. data[0] = file_handle;
  9793. ret = _pt_request_pip2_send_cmd(dev,
  9794. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_CLOSE,
  9795. data, 1, read_buf, &actual_read_len);
  9796. if (ret) {
  9797. pt_debug(dev, DL_ERROR,
  9798. "%s ROM BL FILE_CLOSE timeout for file = %d\n",
  9799. __func__, file_handle);
  9800. return -ETIME;
  9801. }
  9802. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  9803. if (status != 0x00) {
  9804. pt_debug(dev, DL_ERROR,
  9805. "%s ROM BL FILE_CLOSE failure: 0x%02X for file = %d\n",
  9806. __func__, status, file_handle);
  9807. return -status;
  9808. }
  9809. return file_handle;
  9810. }
  9811. /*******************************************************************************
  9812. * FUNCTION: _pt_pip2_file_erase
  9813. *
  9814. * SUMMARY: Using the BL PIP2 commands erase a file
  9815. *
  9816. * NOTE: The DUT must be in BL mode for this command to work
  9817. * NOTE: Some FLASH parts can time out while erasing one or more sectors,
  9818. * one retry is attempted for each sector in a file.
  9819. *
  9820. * RETURNS:
  9821. * <0 = Error
  9822. * >0 = file handle closed
  9823. *
  9824. * PARAMETERS:
  9825. * *dev - pointer to device structure
  9826. * file_handle - handle to the file to be erased
  9827. * *status - PIP2 erase status code
  9828. ******************************************************************************/
  9829. static int _pt_pip2_file_erase(struct device *dev, u8 file_handle, int *status)
  9830. {
  9831. int ret = 0;
  9832. int max_retry = PT_PIP2_MAX_FILE_SIZE/PT_PIP2_FILE_SECTOR_SIZE;
  9833. int retry = 1;
  9834. u16 actual_read_len;
  9835. u8 data[2];
  9836. u8 read_buf[10];
  9837. struct pt_core_data *cd = dev_get_drvdata(dev);
  9838. pt_debug(dev, DL_DEBUG, "%s: ERASE file %d\n", __func__, file_handle);
  9839. data[0] = file_handle;
  9840. data[1] = PIP2_FILE_IOCTL_CODE_ERASE_FILE;
  9841. *status = PIP2_RSP_ERR_TIMEOUT;
  9842. /* Increase waiting time for large file erase */
  9843. mutex_lock(&cd->system_lock);
  9844. cd->pip_cmd_timeout = PT_PIP2_CMD_FILE_ERASE_TIMEOUT;
  9845. mutex_unlock(&cd->system_lock);
  9846. while (*status == PIP2_RSP_ERR_TIMEOUT && retry < max_retry) {
  9847. ret = _pt_request_pip2_send_cmd(dev,
  9848. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  9849. data, 2, read_buf, &actual_read_len);
  9850. if (ret)
  9851. break;
  9852. *status = read_buf[PIP2_RESP_STATUS_OFFSET];
  9853. if (*status == PIP2_RSP_ERR_TIMEOUT) {
  9854. #ifdef TTDL_DIAGNOSTICS
  9855. cd->file_erase_timeout_count++;
  9856. #endif
  9857. pt_debug(dev, DL_WARN,
  9858. "%s: ERASE timeout %d for file = %d\n",
  9859. __func__, retry, file_handle);
  9860. }
  9861. retry++;
  9862. }
  9863. mutex_lock(&cd->system_lock);
  9864. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  9865. mutex_unlock(&cd->system_lock);
  9866. if (ret) {
  9867. pt_debug(dev, DL_ERROR,
  9868. "%s ROM FILE_ERASE cmd failure: %d for file = %d\n",
  9869. __func__, ret, file_handle);
  9870. return -EIO;
  9871. }
  9872. if (*status != 0x00) {
  9873. pt_debug(dev, DL_ERROR,
  9874. "%s ROM FILE_ERASE failure: 0x%02X for file = %d\n",
  9875. __func__, *status, file_handle);
  9876. return -EIO;
  9877. }
  9878. return file_handle;
  9879. }
  9880. /*******************************************************************************
  9881. * FUNCTION: _pt_pip2_file_read
  9882. *
  9883. * SUMMARY: Using the BL PIP2 commands read n bytes from a already opened file
  9884. *
  9885. * NOTE: The DUT must be in BL mode for this command to work
  9886. *
  9887. * RETURNS:
  9888. * <0 = Error
  9889. * >0 = number of bytes read
  9890. *
  9891. * PARAMETERS:
  9892. * *dev - pointer to device structure
  9893. * file_handle - File handle to read from
  9894. * num_bytes - number of bytes to read
  9895. ******************************************************************************/
  9896. int _pt_pip2_file_read(struct device *dev, u8 file_handle, u16 num_bytes,
  9897. u8 *read_buf)
  9898. {
  9899. int ret = 0;
  9900. u16 status;
  9901. u16 actual_read_len;
  9902. u8 data[3];
  9903. data[0] = file_handle;
  9904. data[1] = (num_bytes & 0x00FF);
  9905. data[2] = (num_bytes & 0xFF00) >> 8;
  9906. ret = _pt_request_pip2_send_cmd(dev,
  9907. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_READ,
  9908. data, 3, read_buf, &actual_read_len);
  9909. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  9910. if (ret || ((status != 0x00) && (status != 0x03))) {
  9911. pt_debug(dev, DL_ERROR,
  9912. "%s File open failure with error code = %d\n",
  9913. __func__, status);
  9914. return -EPERM;
  9915. }
  9916. ret = num_bytes;
  9917. return ret;
  9918. }
  9919. /*******************************************************************************
  9920. * FUNCTION: _pt_read_us_file
  9921. *
  9922. * SUMMARY: Open a user space file and read 'size' bytes into buf. If size = 0
  9923. * then read the entire file.
  9924. * NOTE: The file size must be less than PT_PIP2_MAX_FILE_SIZE
  9925. *
  9926. * RETURN:
  9927. * 0 = success
  9928. * !0 = failure
  9929. *
  9930. * PARAMETERS:
  9931. * *file_path - pointer to the file path
  9932. * *buf - pointer to the buffer to store the file contents
  9933. * *size - pointer to the size of the file
  9934. ******************************************************************************/
  9935. int _pt_read_us_file(struct device *dev, u8 *file_path, u8 *buf, int *size)
  9936. {
  9937. struct file *filp = NULL;
  9938. struct inode *inode = NULL;
  9939. unsigned int file_len = 0;
  9940. unsigned int read_len = 0;
  9941. mm_segment_t oldfs;
  9942. int rc = 0;
  9943. if (file_path == NULL || buf == NULL) {
  9944. pt_debug(dev, DL_ERROR, "%s: path || buf is NULL.\n", __func__);
  9945. return -EINVAL;
  9946. }
  9947. pt_debug(dev, DL_WARN, "%s: path = %s\n", __func__, file_path);
  9948. oldfs = get_fs();
  9949. set_fs(KERNEL_DS);
  9950. filp = filp_open(file_path, O_RDONLY, 0400);
  9951. if (IS_ERR(filp)) {
  9952. pt_debug(dev, DL_ERROR, "%s: Failed to open %s\n", __func__,
  9953. file_path);
  9954. rc = -ENOENT;
  9955. goto err;
  9956. }
  9957. if (filp->f_op == NULL) {
  9958. pt_debug(dev, DL_ERROR, "%s: File Operation Method Error\n",
  9959. __func__);
  9960. rc = -EINVAL;
  9961. goto exit;
  9962. }
  9963. inode = filp->f_path.dentry->d_inode;
  9964. if (inode == NULL) {
  9965. pt_debug(dev, DL_ERROR, "%s: Get inode from filp failed\n",
  9966. __func__);
  9967. rc = -EINVAL;
  9968. goto exit;
  9969. }
  9970. file_len = i_size_read(inode->i_mapping->host);
  9971. if (file_len == 0) {
  9972. pt_debug(dev, DL_ERROR, "%s: file size error,file_len = %d\n",
  9973. __func__, file_len);
  9974. rc = -EINVAL;
  9975. goto exit;
  9976. }
  9977. if (*size == 0)
  9978. read_len = file_len;
  9979. else
  9980. read_len = *size;
  9981. if (read_len > PT_PIP2_MAX_FILE_SIZE) {
  9982. pt_debug(dev, DL_ERROR, "%s: file size ( %d ) exception.\n",
  9983. __func__, read_len);
  9984. rc = -EINVAL;
  9985. goto exit;
  9986. }
  9987. filp->private_data = inode->i_private;
  9988. if (vfs_read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  9989. pt_debug(dev, DL_ERROR, "%s: file read error.\n", __func__);
  9990. rc = -EINVAL;
  9991. goto exit;
  9992. }
  9993. *size = read_len;
  9994. exit:
  9995. if (filp_close(filp, NULL) != 0)
  9996. pt_debug(dev, DL_ERROR, "%s: file close error.\n", __func__);
  9997. err:
  9998. set_fs(oldfs);
  9999. return rc;
  10000. }
  10001. /*******************************************************************************
  10002. * FUNCTION: _pt_request_pip2_bin_hdr
  10003. *
  10004. * SUMMARY: Read the stored bin file header from Flash or the User Space file
  10005. * in the case of a flashless DUT, and parse the contents
  10006. *
  10007. * RETURNS:
  10008. * 0 = Success
  10009. * !0 = Error condition
  10010. *
  10011. * PARAMETERS:
  10012. * *dev - pointer to device structure
  10013. ******************************************************************************/
  10014. int _pt_request_pip2_bin_hdr(struct device *dev, struct pt_bin_file_hdr *hdr)
  10015. {
  10016. struct pt_core_data *cd = dev_get_drvdata(dev);
  10017. u8 file_handle;
  10018. u8 read_buf[255];
  10019. u8 hdr_len = 0;
  10020. u8 i;
  10021. int bytes_read;
  10022. int read_size;
  10023. int ret = 0;
  10024. int rc = 0;
  10025. bool load_hdr_struct = false;
  10026. if (cd->flashless_dut) {
  10027. read_size = sizeof(read_buf);
  10028. rc = _pt_read_us_file(dev, cd->pip2_us_file_path,
  10029. read_buf, &read_size);
  10030. if (rc) {
  10031. ret = rc;
  10032. pt_debug(dev, DL_ERROR,
  10033. "%s Failed to read fw image from US, rc=%d\n",
  10034. __func__, rc);
  10035. goto exit;
  10036. }
  10037. load_hdr_struct = true;
  10038. hdr_len = read_buf[0];
  10039. i = 0;
  10040. } else {
  10041. if (cd->mode != PT_MODE_BOOTLOADER) {
  10042. ret = -EPERM;
  10043. goto exit;
  10044. }
  10045. /* Open the bin file in Flash */
  10046. pt_debug(dev, DL_INFO, "%s Open File 1\n", __func__);
  10047. file_handle = _pt_pip2_file_open(dev, PIP2_FW_FILE);
  10048. if (file_handle != PIP2_FW_FILE) {
  10049. ret = -ENOENT;
  10050. pt_debug(dev, DL_ERROR,
  10051. "%s Failed to open bin file\n", __func__);
  10052. goto exit;
  10053. }
  10054. /* Read the header length from the file */
  10055. pt_debug(dev, DL_INFO, "%s Read length of header\n", __func__);
  10056. read_size = 1;
  10057. bytes_read = _pt_pip2_file_read(dev, file_handle, read_size,
  10058. read_buf);
  10059. if (bytes_read != read_size) {
  10060. ret = -EX_ERR_FREAD;
  10061. pt_debug(dev, DL_ERROR,
  10062. "%s Failed to bin file header len\n", __func__);
  10063. goto exit_close_file;
  10064. }
  10065. hdr_len = read_buf[PIP2_RESP_BODY_OFFSET];
  10066. if (hdr_len == 0xFF) {
  10067. ret = -EX_ERR_FLEN;
  10068. pt_debug(dev, DL_ERROR,
  10069. "%s Bin header len is invalid\n", __func__);
  10070. goto exit_close_file;
  10071. }
  10072. /* Read the rest of the header from the bin file */
  10073. pt_debug(dev, DL_INFO, "%s Read bin file header\n", __func__);
  10074. memset(read_buf, 0, sizeof(read_buf));
  10075. bytes_read = _pt_pip2_file_read(dev, file_handle, hdr_len,
  10076. read_buf);
  10077. if (bytes_read != hdr_len) {
  10078. ret = -EX_ERR_FREAD;
  10079. pt_debug(dev, DL_ERROR,
  10080. "%s Failed to read bin file\n", __func__);
  10081. goto exit_close_file;
  10082. }
  10083. load_hdr_struct = true;
  10084. exit_close_file:
  10085. /* Close the file */
  10086. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  10087. ret = -EX_ERR_FCLOSE;
  10088. pt_debug(dev, DL_ERROR,
  10089. "%s Failed to close bin file\n", __func__);
  10090. }
  10091. /*
  10092. * The length was already read so subtract 1 to make the rest of
  10093. * the offsets match the spec
  10094. */
  10095. i = PIP2_RESP_BODY_OFFSET - 1;
  10096. }
  10097. if (load_hdr_struct) {
  10098. hdr->length = hdr_len;
  10099. hdr->ttpid = (read_buf[i+1] << 8) | read_buf[i+2];
  10100. hdr->fw_major = (read_buf[i+3]);
  10101. hdr->fw_minor = (read_buf[i+4]);
  10102. hdr->fw_crc = (read_buf[i+5] << 24) |
  10103. (read_buf[i+6] << 16) |
  10104. (read_buf[i+7] << 8) |
  10105. (read_buf[i+8]);
  10106. hdr->fw_rev_ctrl = (read_buf[i+9] << 24) |
  10107. (read_buf[i+10] << 16) |
  10108. (read_buf[i+11] << 8) |
  10109. (read_buf[i+12]);
  10110. hdr->si_rev = (read_buf[i+14] << 8) | (read_buf[i+13]);
  10111. hdr->si_id = (read_buf[i+16] << 8) | (read_buf[i+15]);
  10112. hdr->config_ver = (read_buf[i+17] << 8) | (read_buf[i+18]);
  10113. if (hdr_len >= 22) {
  10114. hdr->hex_file_size = (read_buf[i+19] << 24) |
  10115. (read_buf[i+20] << 16) |
  10116. (read_buf[i+21] << 8) |
  10117. (read_buf[i+22]);
  10118. } else {
  10119. hdr->hex_file_size = 0;
  10120. }
  10121. }
  10122. exit:
  10123. return ret;
  10124. }
  10125. /*******************************************************************************
  10126. * FUNCTION: _pt_pip2_file_get_stats
  10127. *
  10128. * SUMMARY: Using the BL PIP2 commands get file information from an already
  10129. * opened file
  10130. *
  10131. * NOTE: The DUT must be in BL mode for this command to work
  10132. *
  10133. * RETURNS:
  10134. * !0 = Error
  10135. * 0 = Success
  10136. *
  10137. * PARAMETERS:
  10138. * *dev - pointer to device structure
  10139. * file_handle - File handle to read from
  10140. * *address - pointer to store address of file
  10141. * *file_size _ pointer to store size of file
  10142. ******************************************************************************/
  10143. int _pt_pip2_file_get_stats(struct device *dev, u8 file_handle, u32 *address,
  10144. u32 *file_size)
  10145. {
  10146. int ret = 1;
  10147. u16 status;
  10148. u16 actual_read_len;
  10149. u8 data[2];
  10150. u8 read_buf[16];
  10151. data[0] = file_handle;
  10152. data[1] = PIP2_FILE_IOCTL_CODE_FILE_STATS;
  10153. ret = _pt_request_pip2_send_cmd(dev,
  10154. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10155. data, 2, read_buf, &actual_read_len);
  10156. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10157. if (ret || (status != 0x00)) {
  10158. pt_debug(dev, DL_ERROR,
  10159. "%s ROM FILE_STATS failure: 0x%02X for file = %d, ret = %d\n",
  10160. __func__, status, file_handle, ret);
  10161. ret = -EIO;
  10162. goto exit;
  10163. }
  10164. pt_debug(dev, DL_DEBUG,
  10165. "%s --- FILE %d Information ---\n", __func__, file_handle);
  10166. if (address) {
  10167. *address = read_buf[PIP2_RESP_BODY_OFFSET] +
  10168. (read_buf[PIP2_RESP_BODY_OFFSET + 1] << 8) +
  10169. (read_buf[PIP2_RESP_BODY_OFFSET + 2] << 16) +
  10170. (read_buf[PIP2_RESP_BODY_OFFSET + 3] << 24);
  10171. pt_debug(dev, DL_DEBUG, "%s Address: 0x%08x\n",
  10172. __func__, *address);
  10173. }
  10174. if (file_size) {
  10175. *file_size = read_buf[PIP2_RESP_BODY_OFFSET + 4] +
  10176. (read_buf[PIP2_RESP_BODY_OFFSET + 5] << 8) +
  10177. (read_buf[PIP2_RESP_BODY_OFFSET + 6] << 16) +
  10178. (read_buf[PIP2_RESP_BODY_OFFSET + 7] << 24);
  10179. pt_debug(dev, DL_DEBUG, "%s Size : 0x%08x\n",
  10180. __func__, *file_size);
  10181. }
  10182. exit:
  10183. return ret;
  10184. }
  10185. /*******************************************************************************
  10186. * FUNCTION: _pt_pip2_file_seek_offset
  10187. *
  10188. * SUMMARY: Using the BL PIP2 commands seek read/write offset for an already
  10189. * opened file
  10190. *
  10191. * NOTE: The DUT must be in BL mode for this command to work
  10192. * NOTE: File open/erase command can reset the offset
  10193. *
  10194. * RETURNS:
  10195. * !0 = Error
  10196. * 0 = Success
  10197. *
  10198. * PARAMETERS:
  10199. * *dev - pointer to device structure
  10200. * file_handle - File handle to read from
  10201. * read_offset - read offset of file
  10202. * write_offset - write offset of file
  10203. ******************************************************************************/
  10204. int _pt_pip2_file_seek_offset(struct device *dev, u8 file_handle,
  10205. u32 read_offset, u32 write_offset)
  10206. {
  10207. int ret = 1;
  10208. u16 status;
  10209. u16 actual_read_len;
  10210. u8 data[10];
  10211. u8 read_buf[16];
  10212. data[0] = file_handle;
  10213. data[1] = PIP2_FILE_IOCTL_CODE_SEEK_POINTER;
  10214. data[2] = 0xff & read_offset;
  10215. data[3] = 0xff & (read_offset >> 8);
  10216. data[4] = 0xff & (read_offset >> 16);
  10217. data[5] = 0xff & (read_offset >> 24);
  10218. data[6] = 0xff & write_offset;
  10219. data[7] = 0xff & (write_offset >> 8);
  10220. data[8] = 0xff & (write_offset >> 16);
  10221. data[9] = 0xff & (write_offset >> 24);
  10222. ret = _pt_request_pip2_send_cmd(dev,
  10223. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10224. data, 10, read_buf, &actual_read_len);
  10225. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10226. if (ret || (status != 0x00)) {
  10227. pt_debug(dev, DL_ERROR,
  10228. "%s ROM FILE_SEEK failure: 0x%02X for file = %d, ret = %d\n",
  10229. __func__, status, ret, file_handle);
  10230. ret = -EIO;
  10231. }
  10232. return ret;
  10233. }
  10234. /*******************************************************************************
  10235. * FUNCTION: _pt_pip2_file_crc
  10236. *
  10237. * SUMMARY: Using the BL PIP2 commands to calculate CRC for a file or portion of
  10238. * the file.
  10239. *
  10240. * NOTE: The DUT must be in BL mode for this command to work
  10241. * NOTE: This command only can be used for BL version 1.8 or greater.
  10242. * BL version 1.8 added this change according to PGV-173.
  10243. *
  10244. * RETURNS:
  10245. * !0 = Error
  10246. * 0 = Success
  10247. *
  10248. * PARAMETERS:
  10249. * *dev - pointer to device structure
  10250. * file_handle - File handle to read from
  10251. * offset - start offset for CRC calculation
  10252. * length - number of bytes to calculate CRC over
  10253. * read_buf - pointer to the read buffer
  10254. ******************************************************************************/
  10255. int _pt_pip2_file_crc(struct device *dev, u8 file_handle,
  10256. u32 offset, u32 length, u8 *read_buf)
  10257. {
  10258. int rc = 1;
  10259. u16 actual_read_len;
  10260. u8 data[10];
  10261. data[0] = file_handle;
  10262. data[1] = PIP2_FILE_IOCTL_CODE_FILE_CRC;
  10263. data[2] = 0xff & offset;
  10264. data[3] = 0xff & (offset >> 8);
  10265. data[4] = 0xff & (offset >> 16);
  10266. data[5] = 0xff & (offset >> 24);
  10267. data[6] = 0xff & length;
  10268. data[7] = 0xff & (length >> 8);
  10269. data[8] = 0xff & (length >> 16);
  10270. data[9] = 0xff & (length >> 24);
  10271. rc = _pt_request_pip2_send_cmd(dev,
  10272. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10273. data, 10, read_buf, &actual_read_len);
  10274. if (rc)
  10275. pt_debug(dev, DL_ERROR,
  10276. "%s Return FILE_CRC failure, rc = %d\n",
  10277. __func__, rc);
  10278. return rc;
  10279. }
  10280. /*******************************************************************************
  10281. * FUNCTION: pt_pip2_ping_test
  10282. *
  10283. * SUMMARY: BIST type test that uses the PIP2 PING command and ramps up the
  10284. * optional payload from 0 bytes to max_bytes and ensures the PIP2
  10285. * response payload matches what was sent.
  10286. * The max payload size is 247:
  10287. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  10288. *
  10289. * RETURN:
  10290. * 0 = success
  10291. * !0 = failure
  10292. *
  10293. * PARAMETERS:
  10294. * *dev - pointer to device structure
  10295. * *attr - pointer to device attributes
  10296. * *buf - pointer to output buffer
  10297. ******************************************************************************/
  10298. int pt_pip2_ping_test(struct device *dev, int max_bytes, int *last_packet_size)
  10299. {
  10300. u16 actual_read_len;
  10301. u8 *read_buf = NULL;
  10302. u8 *data = NULL;
  10303. int data_offset = PIP2_RESP_STATUS_OFFSET;
  10304. int i = 1;
  10305. int j = 0;
  10306. int rc = 0;
  10307. read_buf = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10308. if (!read_buf)
  10309. goto ping_test_exit;
  10310. data = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10311. if (!data)
  10312. goto ping_test_exit;
  10313. /* Load data payload with an array of walking 1's */
  10314. for (i = 0; i < 255; i++)
  10315. data[i] = 0x01 << (i % 8);
  10316. /* Send 'max_bytes' PING cmds using 'i' bytes as payload for each */
  10317. for (i = 0; i <= max_bytes; i++) {
  10318. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10319. PIP2_CMD_ID_PING, data, i, read_buf,
  10320. &actual_read_len);
  10321. if (rc) {
  10322. pt_debug(dev, DL_ERROR,
  10323. "%s: PING failed with %d byte payload\n",
  10324. __func__, i);
  10325. break;
  10326. }
  10327. /* Verify data returned matches data sent */
  10328. for (j = 0; j < i; j++) {
  10329. if (read_buf[data_offset + j] != data[j]) {
  10330. pt_debug(dev, DL_DEBUG,
  10331. "%s: PING packet size %d: sent[%d]=0x%02X recv[%d]=0x%02X\n",
  10332. __func__, i, j, data[j], j,
  10333. read_buf[data_offset + j]);
  10334. goto ping_test_exit;
  10335. }
  10336. }
  10337. }
  10338. ping_test_exit:
  10339. *last_packet_size = i - 1;
  10340. kfree(read_buf);
  10341. kfree(data);
  10342. return rc;
  10343. }
  10344. /*******************************************************************************
  10345. * FUNCTION: _pt_ic_parse_input_hex
  10346. *
  10347. * SUMMARY: Parse a char data array as space delimited hex values into
  10348. * an int array.
  10349. *
  10350. * NOTE: _pt_ic_parse_input() function may have similar work while the type of
  10351. * buffer to store data is "u32". This function is still needed by the
  10352. * "command" sysfs node because the buffer type to store data is "u8".
  10353. *
  10354. * RETURN: Length of parsed data
  10355. *
  10356. * PARAMETERS:
  10357. * *dev - pointer to device structure
  10358. * *buf - pointer to buffer that holds the input array to parse
  10359. * buf_size - size of buf
  10360. * *ic_buf - pointer to array to store parsed data
  10361. * ic_buf_size - max size of ic_buf
  10362. ******************************************************************************/
  10363. static int _pt_ic_parse_input_hex(struct device *dev, const char *buf,
  10364. size_t buf_size, u8 *ic_buf, size_t ic_buf_size)
  10365. {
  10366. const char *pbuf = buf;
  10367. unsigned long value;
  10368. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10369. u32 i = 0;
  10370. u32 j;
  10371. int last = 0;
  10372. int ret;
  10373. pt_debug(dev, DL_DEBUG,
  10374. "%s: pbuf=%p buf=%p size=%zu %s=%d buf=%s\n",
  10375. __func__, pbuf, buf, buf_size, "scan buf size",
  10376. PT_MAX_PIP2_MSG_SIZE, buf);
  10377. while (pbuf <= (buf + buf_size)) {
  10378. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10379. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10380. __func__, "Max cmd size exceeded", i,
  10381. PT_MAX_PIP2_MSG_SIZE);
  10382. return -EINVAL;
  10383. }
  10384. if (i >= ic_buf_size) {
  10385. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10386. __func__, "Buffer size exceeded", i,
  10387. ic_buf_size);
  10388. return -EINVAL;
  10389. }
  10390. while (((*pbuf == ' ') || (*pbuf == ','))
  10391. && (pbuf < (buf + buf_size))) {
  10392. last = *pbuf;
  10393. pbuf++;
  10394. }
  10395. if (pbuf >= (buf + buf_size))
  10396. break;
  10397. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10398. if ((last == ',') && (*pbuf == ',')) {
  10399. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10400. __func__, "Invalid data format.");
  10401. return -EINVAL;
  10402. }
  10403. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10404. && (pbuf < (buf + buf_size))
  10405. && (*pbuf != ' ')
  10406. && (*pbuf != ','); j++) {
  10407. last = *pbuf;
  10408. scan_buf[j] = *pbuf++;
  10409. }
  10410. ret = kstrtoul(scan_buf, 16, &value);
  10411. if (ret < 0) {
  10412. pt_debug(dev, DL_ERROR,
  10413. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10414. "Invalid data format. ", scan_buf,
  10415. "Use \"0xHH,...,0xHH\"", " instead.",
  10416. i, ret);
  10417. return ret;
  10418. }
  10419. ic_buf[i] = value;
  10420. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx",
  10421. __func__, i, value);
  10422. i++;
  10423. }
  10424. return i;
  10425. }
  10426. /*******************************************************************************
  10427. * FUNCTION: _pt_ic_parse_input
  10428. *
  10429. * SUMMARY: Parse user sysfs input data as a space or comma delimited list of
  10430. * hex values or dec values into an int array with the following rules:
  10431. * 1) Hex values must have a "0x" prefix for each element or the first element
  10432. * only
  10433. * 2) Dec values do not have any prefix
  10434. * 3) It is not allowed to have a mix of dec and hex values in the user input
  10435. * string
  10436. *
  10437. * RETURN: Number of values parsed
  10438. *
  10439. * PARAMETERS:
  10440. * *dev - pointer to device structure
  10441. * *buf - pointer to buffer that holds the input array to parse
  10442. * buf_size - size of buf
  10443. * *out_buf - pointer to array to store parsed data
  10444. * out_buf_size - max size of buffer to be stored
  10445. ******************************************************************************/
  10446. static int _pt_ic_parse_input(struct device *dev,
  10447. const char *buf, size_t buf_size,
  10448. u32 *out_buf, size_t out_buf_size)
  10449. {
  10450. const char *pbuf = buf;
  10451. unsigned long value;
  10452. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10453. u32 i = 0;
  10454. u32 j;
  10455. int last = 0;
  10456. int ret = 0;
  10457. u8 str_base = 0;
  10458. pt_debug(dev, DL_DEBUG,
  10459. "%s: in_buf_size=%zu out_buf_size=%zu %s=%d buf=%s\n",
  10460. __func__, buf_size, out_buf_size, "scan buf size",
  10461. PT_MAX_PIP2_MSG_SIZE, buf);
  10462. while (pbuf <= (buf + buf_size)) {
  10463. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10464. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10465. __func__, "Max cmd size exceeded", i,
  10466. PT_MAX_PIP2_MSG_SIZE);
  10467. ret = -EINVAL;
  10468. goto error;
  10469. }
  10470. if (i >= out_buf_size) {
  10471. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10472. __func__, "Buffer size exceeded", i,
  10473. out_buf_size);
  10474. ret = -EINVAL;
  10475. goto error;
  10476. }
  10477. while (((*pbuf == ' ') || (*pbuf == ','))
  10478. && (pbuf < (buf + buf_size))) {
  10479. last = *pbuf;
  10480. pbuf++;
  10481. }
  10482. if (pbuf >= (buf + buf_size))
  10483. break;
  10484. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10485. if ((last == ',') && (*pbuf == ',')) {
  10486. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10487. __func__, "Invalid data format.");
  10488. ret = -EINVAL;
  10489. goto error;
  10490. }
  10491. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10492. && (pbuf < (buf + buf_size))
  10493. && (*pbuf != ' ')
  10494. && (*pbuf != ','); j++) {
  10495. last = *pbuf;
  10496. scan_buf[j] = *pbuf++;
  10497. }
  10498. if (i == 0) {
  10499. if ((strncmp(scan_buf, "0x", 2) == 0) ||
  10500. (strncmp(scan_buf, "0X", 2) == 0))
  10501. str_base = 16;
  10502. else
  10503. str_base = 10;
  10504. } else {
  10505. if (((strncmp(scan_buf, "0x", 2) == 0) ||
  10506. (strncmp(scan_buf, "0X", 2) == 0)) &&
  10507. (str_base == 10)) {
  10508. pt_debug(dev, DL_ERROR,
  10509. "%s: Decimal and Heximal data mixed\n",
  10510. __func__);
  10511. ret = -EINVAL;
  10512. goto error;
  10513. }
  10514. }
  10515. ret = kstrtoul(scan_buf, str_base, &value);
  10516. if (ret < 0) {
  10517. pt_debug(dev, DL_ERROR,
  10518. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10519. "Invalid data format. ", scan_buf,
  10520. "Use \"0xHH,...,0xHH\" or \"DD DD DD ... DD\"",
  10521. " instead.", i, ret);
  10522. goto error;
  10523. }
  10524. out_buf[i] = value;
  10525. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx(%lu)",
  10526. __func__, i, value, value);
  10527. i++;
  10528. }
  10529. ret = i;
  10530. error:
  10531. return ret;
  10532. }
  10533. #ifdef TTHE_TUNER_SUPPORT
  10534. /*******************************************************************************
  10535. * FUNCTION: tthe_debugfs_open
  10536. *
  10537. * SUMMARY: Open method for tthe_tuner debugfs node. On some hosts the size of
  10538. * PT_MAX_PRBUF_SIZE (equal to PAGE_SIZE) is not large enough to handle
  10539. * printing a large number of fingers and sensor data without overflowing
  10540. * the buffer. tthe_tuner needs ~4K and so the buffer is sized to some
  10541. * even multiple of PAGE_SIZE
  10542. *
  10543. * RETURN:
  10544. * 0 = success
  10545. * !0 = failure
  10546. *
  10547. * PARAMETERS:
  10548. * *inode - file inode number
  10549. * *filp - file pointer to debugfs file
  10550. ******************************************************************************/
  10551. static int tthe_debugfs_open(struct inode *inode, struct file *filp)
  10552. {
  10553. struct pt_core_data *cd = inode->i_private;
  10554. u32 buf_size = PT_MAX_PRBUF_SIZE;
  10555. filp->private_data = inode->i_private;
  10556. if (cd->tthe_buf)
  10557. return -EBUSY;
  10558. while (buf_size < 4096)
  10559. buf_size = buf_size << 1;
  10560. pt_debug(cd->dev, DL_INFO, "%s:PT_MAX_BRBUF_SIZE=%d buf_size=%d\n",
  10561. __func__, (int)PT_MAX_PRBUF_SIZE, (int)buf_size);
  10562. cd->tthe_buf_size = buf_size;
  10563. cd->tthe_buf = kzalloc(cd->tthe_buf_size, GFP_KERNEL);
  10564. if (!cd->tthe_buf)
  10565. return -ENOMEM;
  10566. return 0;
  10567. }
  10568. /*******************************************************************************
  10569. * FUNCTION: tthe_debugfs_close
  10570. *
  10571. * SUMMARY: Close method for tthe_tuner debugfs node.
  10572. *
  10573. * RETURN:
  10574. * 0 = success
  10575. * !0 = failure
  10576. *
  10577. * PARAMETERS:
  10578. * *inode - file inode number
  10579. * *filp - file pointer to debugfs file
  10580. ******************************************************************************/
  10581. static int tthe_debugfs_close(struct inode *inode, struct file *filp)
  10582. {
  10583. struct pt_core_data *cd = filp->private_data;
  10584. filp->private_data = NULL;
  10585. kfree(cd->tthe_buf);
  10586. cd->tthe_buf = NULL;
  10587. return 0;
  10588. }
  10589. /*******************************************************************************
  10590. * FUNCTION: tthe_debugfs_read
  10591. *
  10592. * SUMMARY: Read method for tthe_tuner debugfs node. This function prints
  10593. * tthe_buf to user buffer.
  10594. *
  10595. * RETURN: Size of debugfs data print
  10596. *
  10597. * PARAMETERS:
  10598. * *filp - file pointer to debugfs file
  10599. * *buf - the user space buffer to read to
  10600. * count - the maximum number of bytes to read
  10601. * *ppos - the current position in the buffer
  10602. ******************************************************************************/
  10603. static ssize_t tthe_debugfs_read(struct file *filp, char __user *buf,
  10604. size_t count, loff_t *ppos)
  10605. {
  10606. struct pt_core_data *cd = filp->private_data;
  10607. int size;
  10608. int ret;
  10609. static int partial_read;
  10610. wait_event_interruptible(cd->wait_q,
  10611. cd->tthe_buf_len != 0 || cd->tthe_exit);
  10612. mutex_lock(&cd->tthe_lock);
  10613. if (cd->tthe_exit) {
  10614. mutex_unlock(&cd->tthe_lock);
  10615. return 0;
  10616. }
  10617. if (count > cd->tthe_buf_len)
  10618. size = cd->tthe_buf_len;
  10619. else
  10620. size = count;
  10621. if (!size) {
  10622. mutex_unlock(&cd->tthe_lock);
  10623. return 0;
  10624. }
  10625. if (partial_read) {
  10626. ret = copy_to_user(buf, cd->tthe_buf + partial_read, size);
  10627. partial_read = 0;
  10628. } else {
  10629. ret = copy_to_user(buf, cd->tthe_buf, size);
  10630. }
  10631. if (size == count)
  10632. partial_read = count;
  10633. if (ret == size)
  10634. return -EFAULT;
  10635. size -= ret;
  10636. cd->tthe_buf_len -= size;
  10637. mutex_unlock(&cd->tthe_lock);
  10638. *ppos += size;
  10639. return size;
  10640. }
  10641. static const struct file_operations tthe_debugfs_fops = {
  10642. .open = tthe_debugfs_open,
  10643. .release = tthe_debugfs_close,
  10644. .read = tthe_debugfs_read,
  10645. };
  10646. #endif
  10647. static struct pt_core_nonhid_cmd _pt_core_nonhid_cmd = {
  10648. .start_bl = _pt_request_pip_start_bl,
  10649. .suspend_scanning = _pt_request_pip_suspend_scanning,
  10650. .resume_scanning = _pt_request_pip_resume_scanning,
  10651. .get_param = _pt_request_pip_get_param,
  10652. .set_param = _pt_request_pip_set_param,
  10653. .verify_cfg_block_crc = _pt_request_pip_verify_config_block_crc,
  10654. .get_config_row_size = _pt_request_pip_get_config_row_size,
  10655. .get_data_structure = _pt_request_pip_get_data_structure,
  10656. .run_selftest = _pt_request_pip_run_selftest,
  10657. .get_selftest_result = _pt_request_pip_get_selftest_result,
  10658. .load_self_test_param = _pt_request_pip_load_self_test_param,
  10659. .calibrate_idacs = _pt_request_pip_calibrate_idacs,
  10660. .calibrate_ext = _pt_request_pip_calibrate_ext,
  10661. .initialize_baselines = _pt_request_pip_initialize_baselines,
  10662. .exec_panel_scan = _pt_request_pip_exec_panel_scan,
  10663. .retrieve_panel_scan = _pt_request_pip_retrieve_panel_scan,
  10664. .read_data_block = _pt_request_pip_read_data_block,
  10665. .write_data_block = _pt_request_pip_write_data_block,
  10666. .user_cmd = _pt_request_pip_user_cmd,
  10667. .get_bl_info = _pt_request_pip_bl_get_information,
  10668. .initiate_bl = _pt_request_pip_bl_initiate_bl,
  10669. .launch_app = _pt_request_pip_launch_app,
  10670. .prog_and_verify = _pt_request_pip_bl_program_and_verify,
  10671. .verify_app_integrity = _pt_request_pip_bl_verify_app_integrity,
  10672. .get_panel_id = _pt_request_pip_bl_get_panel_id,
  10673. .pip2_send_cmd = _pt_request_pip2_send_cmd,
  10674. .pip2_send_cmd_no_int = _pt_pip2_send_cmd_no_int,
  10675. .pip2_file_open = _pt_pip2_file_open,
  10676. .pip2_file_close = _pt_pip2_file_close,
  10677. .pip2_file_erase = _pt_pip2_file_erase,
  10678. .read_us_file = _pt_read_us_file,
  10679. .manage_cal_data = _pt_manage_local_cal_data,
  10680. .calc_crc = crc_ccitt_calculate,
  10681. #ifdef TTDL_DIAGNOSTICS
  10682. .pip2_file_read = _pt_pip2_file_read,
  10683. .pip2_file_seek_offset = _pt_pip2_file_seek_offset,
  10684. .pip2_file_get_stats = _pt_pip2_file_get_stats,
  10685. .pip2_file_crc = _pt_pip2_file_crc,
  10686. #endif
  10687. };
  10688. static struct pt_core_commands _pt_core_commands = {
  10689. .subscribe_attention = _pt_subscribe_attention,
  10690. .unsubscribe_attention = _pt_unsubscribe_attention,
  10691. .request_exclusive = _pt_request_exclusive,
  10692. .release_exclusive = _pt_release_exclusive,
  10693. .request_reset = _pt_request_reset,
  10694. .request_pip2_launch_app = _pt_request_pip2_launch_app,
  10695. .request_enum = _pt_request_enum,
  10696. .request_sysinfo = _pt_request_sysinfo,
  10697. .request_loader_pdata = _pt_request_loader_pdata,
  10698. .request_stop_wd = _pt_request_stop_wd,
  10699. .request_start_wd = _pt_request_start_wd,
  10700. .request_get_mode = _pt_request_get_mode,
  10701. .request_active_pip_prot = _pt_request_active_pip_protocol,
  10702. .request_pip2_get_mode_sysmode = _pt_request_pip2_get_mode_sysmode,
  10703. .request_pip2_enter_bl = _pt_request_pip2_enter_bl,
  10704. .request_pip2_bin_hdr = _pt_request_pip2_bin_hdr,
  10705. .request_dut_generation = _pt_request_dut_generation,
  10706. .request_hw_version = _pt_request_hw_version,
  10707. .parse_sysfs_input = _pt_ic_parse_input,
  10708. #ifdef TTHE_TUNER_SUPPORT
  10709. .request_tthe_print = _pt_request_tthe_print,
  10710. #endif
  10711. #ifdef TTDL_DIAGNOSTICS
  10712. .request_toggle_err_gpio = _pt_request_toggle_err_gpio,
  10713. #endif
  10714. .nonhid_cmd = &_pt_core_nonhid_cmd,
  10715. .request_get_fw_mode = _pt_request_get_fw_sys_mode,
  10716. };
  10717. struct pt_core_commands *pt_get_commands(void)
  10718. {
  10719. return &_pt_core_commands;
  10720. }
  10721. EXPORT_SYMBOL_GPL(pt_get_commands);
  10722. static DEFINE_MUTEX(core_list_lock);
  10723. static LIST_HEAD(core_list);
  10724. static DEFINE_MUTEX(module_list_lock);
  10725. static LIST_HEAD(module_list);
  10726. static int core_number;
  10727. /*******************************************************************************
  10728. * FUNCTION: pt_probe_module
  10729. *
  10730. * SUMMARY: Add the module pointer to module_node and call the probe pointer.
  10731. *
  10732. * RETURN:
  10733. * 0 = success
  10734. * !0 = failure
  10735. *
  10736. * PARAMETERS:
  10737. * *cd - pointer to core data
  10738. * *module - pointer to module structure
  10739. ******************************************************************************/
  10740. static int pt_probe_module(struct pt_core_data *cd,
  10741. struct pt_module *module)
  10742. {
  10743. struct module_node *module_node;
  10744. int rc = 0;
  10745. module_node = kzalloc(sizeof(*module_node), GFP_KERNEL);
  10746. if (!module_node)
  10747. return -ENOMEM;
  10748. module_node->module = module;
  10749. mutex_lock(&cd->module_list_lock);
  10750. list_add(&module_node->node, &cd->module_list);
  10751. mutex_unlock(&cd->module_list_lock);
  10752. rc = module->probe(cd->dev, &module_node->data);
  10753. if (rc) {
  10754. /*
  10755. * Remove from the list when probe fails
  10756. * in order not to call release
  10757. */
  10758. mutex_lock(&cd->module_list_lock);
  10759. list_del(&module_node->node);
  10760. mutex_unlock(&cd->module_list_lock);
  10761. kfree(module_node);
  10762. goto exit;
  10763. }
  10764. exit:
  10765. return rc;
  10766. }
  10767. /*******************************************************************************
  10768. * FUNCTION: pt_release_module
  10769. *
  10770. * SUMMARY: Call the release pointer and remove the module pointer from
  10771. * module_list.
  10772. *
  10773. * PARAMETERS:
  10774. * *cd - pointer to core data
  10775. * *module - pointer to module structure
  10776. ******************************************************************************/
  10777. static void pt_release_module(struct pt_core_data *cd,
  10778. struct pt_module *module)
  10779. {
  10780. struct module_node *m, *m_n;
  10781. mutex_lock(&cd->module_list_lock);
  10782. list_for_each_entry_safe(m, m_n, &cd->module_list, node)
  10783. if (m->module == module) {
  10784. module->release(cd->dev, m->data);
  10785. list_del(&m->node);
  10786. kfree(m);
  10787. break;
  10788. }
  10789. mutex_unlock(&cd->module_list_lock);
  10790. }
  10791. /*******************************************************************************
  10792. * FUNCTION: pt_probe_modules
  10793. *
  10794. * SUMMARY: Iterate module_list and probe each module.
  10795. *
  10796. * PARAMETERS:
  10797. * *cd - pointer to core data
  10798. ******************************************************************************/
  10799. static void pt_probe_modules(struct pt_core_data *cd)
  10800. {
  10801. struct pt_module *m;
  10802. int rc = 0;
  10803. mutex_lock(&module_list_lock);
  10804. list_for_each_entry(m, &module_list, node) {
  10805. pt_debug(cd->dev, DL_ERROR, "%s: Probe module %s\n",
  10806. __func__, m->name);
  10807. rc = pt_probe_module(cd, m);
  10808. if (rc)
  10809. pt_debug(cd->dev, DL_ERROR,
  10810. "%s: Probe fails for module %s\n",
  10811. __func__, m->name);
  10812. }
  10813. mutex_unlock(&module_list_lock);
  10814. }
  10815. /*******************************************************************************
  10816. * FUNCTION: pt_release_modules
  10817. *
  10818. * SUMMARY: Iterate module_list and remove each module.
  10819. *
  10820. * PARAMETERS:
  10821. * *cd - pointer to core data
  10822. ******************************************************************************/
  10823. static void pt_release_modules(struct pt_core_data *cd)
  10824. {
  10825. struct pt_module *m;
  10826. mutex_lock(&module_list_lock);
  10827. list_for_each_entry(m, &module_list, node)
  10828. pt_release_module(cd, m);
  10829. mutex_unlock(&module_list_lock);
  10830. }
  10831. /*******************************************************************************
  10832. * FUNCTION: pt_get_core_data
  10833. *
  10834. * SUMMARY: Iterate core_list and get core data.
  10835. *
  10836. * RETURN:
  10837. * pointer to core data or null pointer if fail
  10838. *
  10839. * PARAMETERS:
  10840. * *id - pointer to core id
  10841. ******************************************************************************/
  10842. struct pt_core_data *pt_get_core_data(char *id)
  10843. {
  10844. struct pt_core_data *d;
  10845. list_for_each_entry(d, &core_list, node)
  10846. if (!strncmp(d->core_id, id, 20))
  10847. return d;
  10848. return NULL;
  10849. }
  10850. EXPORT_SYMBOL_GPL(pt_get_core_data);
  10851. /*******************************************************************************
  10852. * FUNCTION: pt_add_core
  10853. *
  10854. * SUMMARY: Add core data to the core_list.
  10855. *
  10856. * PARAMETERS:
  10857. * *dev - pointer to device structure
  10858. ******************************************************************************/
  10859. static void pt_add_core(struct device *dev)
  10860. {
  10861. struct pt_core_data *d;
  10862. struct pt_core_data *cd = dev_get_drvdata(dev);
  10863. mutex_lock(&core_list_lock);
  10864. list_for_each_entry(d, &core_list, node)
  10865. if (d->dev == dev)
  10866. goto unlock;
  10867. list_add(&cd->node, &core_list);
  10868. unlock:
  10869. mutex_unlock(&core_list_lock);
  10870. }
  10871. /*******************************************************************************
  10872. * FUNCTION: pt_del_core
  10873. *
  10874. * SUMMARY: Remove core data from the core_list.
  10875. *
  10876. * PARAMETERS:
  10877. * *dev - pointer to device structure
  10878. ******************************************************************************/
  10879. static void pt_del_core(struct device *dev)
  10880. {
  10881. struct pt_core_data *d, *d_n;
  10882. mutex_lock(&core_list_lock);
  10883. list_for_each_entry_safe(d, d_n, &core_list, node)
  10884. if (d->dev == dev) {
  10885. list_del(&d->node);
  10886. goto unlock;
  10887. }
  10888. unlock:
  10889. mutex_unlock(&core_list_lock);
  10890. }
  10891. /*******************************************************************************
  10892. * FUNCTION: pt_register_module
  10893. *
  10894. * SUMMARY: Register the module to module_list and probe the module for each
  10895. * core.
  10896. *
  10897. * RETURN:
  10898. * 0 = success
  10899. * !0 = failure
  10900. *
  10901. * PARAMETERS:
  10902. * *module - pointer to module structure
  10903. ******************************************************************************/
  10904. int pt_register_module(struct pt_module *module)
  10905. {
  10906. struct pt_module *m;
  10907. struct pt_core_data *cd;
  10908. int rc = 0;
  10909. if (!module || !module->probe || !module->release)
  10910. return -EINVAL;
  10911. mutex_lock(&module_list_lock);
  10912. list_for_each_entry(m, &module_list, node)
  10913. if (m == module) {
  10914. rc = -EEXIST;
  10915. goto unlock;
  10916. }
  10917. list_add(&module->node, &module_list);
  10918. /* Probe the module for each core */
  10919. mutex_lock(&core_list_lock);
  10920. list_for_each_entry(cd, &core_list, node)
  10921. pt_probe_module(cd, module);
  10922. mutex_unlock(&core_list_lock);
  10923. unlock:
  10924. mutex_unlock(&module_list_lock);
  10925. return rc;
  10926. }
  10927. EXPORT_SYMBOL_GPL(pt_register_module);
  10928. /*******************************************************************************
  10929. * FUNCTION: pt_unregister_module
  10930. *
  10931. * SUMMARY: Release the module for each core and remove the module from
  10932. * module_list.
  10933. *
  10934. * RETURN:
  10935. * 0 = success
  10936. * !0 = failure
  10937. *
  10938. * PARAMETERS:
  10939. * *module - pointer to module structure
  10940. ******************************************************************************/
  10941. void pt_unregister_module(struct pt_module *module)
  10942. {
  10943. struct pt_module *m, *m_n;
  10944. struct pt_core_data *cd;
  10945. if (!module)
  10946. return;
  10947. mutex_lock(&module_list_lock);
  10948. /* Release the module for each core */
  10949. mutex_lock(&core_list_lock);
  10950. list_for_each_entry(cd, &core_list, node)
  10951. pt_release_module(cd, module);
  10952. mutex_unlock(&core_list_lock);
  10953. list_for_each_entry_safe(m, m_n, &module_list, node)
  10954. if (m == module) {
  10955. list_del(&m->node);
  10956. break;
  10957. }
  10958. mutex_unlock(&module_list_lock);
  10959. }
  10960. EXPORT_SYMBOL_GPL(pt_unregister_module);
  10961. /*******************************************************************************
  10962. * FUNCTION: pt_get_module_data
  10963. *
  10964. * SUMMARY: Get module data from module_node by module_list.
  10965. *
  10966. * RETURN:
  10967. * pointer to module data
  10968. *
  10969. * PARAMETERS:
  10970. * *dev - pointer to device structure
  10971. * *module - pointer to module structure
  10972. ******************************************************************************/
  10973. void *pt_get_module_data(struct device *dev, struct pt_module *module)
  10974. {
  10975. struct pt_core_data *cd = dev_get_drvdata(dev);
  10976. struct module_node *m;
  10977. void *data = NULL;
  10978. mutex_lock(&cd->module_list_lock);
  10979. list_for_each_entry(m, &cd->module_list, node)
  10980. if (m->module == module) {
  10981. data = m->data;
  10982. break;
  10983. }
  10984. mutex_unlock(&cd->module_list_lock);
  10985. return data;
  10986. }
  10987. EXPORT_SYMBOL(pt_get_module_data);
  10988. #ifdef CONFIG_HAS_EARLYSUSPEND
  10989. /*******************************************************************************
  10990. * FUNCTION: pt_early_suspend
  10991. *
  10992. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_SUSPEND"
  10993. * attention list.
  10994. *
  10995. * PARAMETERS:
  10996. * *h - pointer to early_suspend structure
  10997. ******************************************************************************/
  10998. static void pt_early_suspend(struct early_suspend *h)
  10999. {
  11000. struct pt_core_data *cd =
  11001. container_of(h, struct pt_core_data, es);
  11002. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11003. }
  11004. /*******************************************************************************
  11005. * FUNCTION: pt_late_resume
  11006. *
  11007. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_RESUME"
  11008. * attention list.
  11009. *
  11010. * PARAMETERS:
  11011. * *h - pointer to early_suspend structure
  11012. ******************************************************************************/
  11013. static void pt_late_resume(struct early_suspend *h)
  11014. {
  11015. struct pt_core_data *cd =
  11016. container_of(h, struct pt_core_data, es);
  11017. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11018. }
  11019. /*******************************************************************************
  11020. * FUNCTION: pt_setup_early_suspend
  11021. *
  11022. * SUMMARY: Register early/suspend function to the system.
  11023. *
  11024. * PARAMETERS:
  11025. * *cd - pointer to core data
  11026. ******************************************************************************/
  11027. static void pt_setup_early_suspend(struct pt_core_data *cd)
  11028. {
  11029. cd->es.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  11030. cd->es.suspend = pt_early_suspend;
  11031. cd->es.resume = pt_late_resume;
  11032. register_early_suspend(&cd->es);
  11033. }
  11034. #elif defined(CONFIG_FB)
  11035. /*******************************************************************************
  11036. * FUNCTION: fb_notifier_callback
  11037. *
  11038. * SUMMARY: Call back function for FrameBuffer notifier to allow to call
  11039. * resume/suspend attention list.
  11040. *
  11041. * RETURN:
  11042. * 0 = success
  11043. *
  11044. * PARAMETERS:
  11045. * *self - pointer to notifier_block structure
  11046. * event - event type of fb notifier
  11047. * *data - pointer to fb_event structure
  11048. ******************************************************************************/
  11049. static int fb_notifier_callback(struct notifier_block *self,
  11050. unsigned long event, void *data)
  11051. {
  11052. struct pt_core_data *cd =
  11053. container_of(self, struct pt_core_data, fb_notifier);
  11054. struct fb_event *evdata = data;
  11055. int *blank;
  11056. if (event != FB_EVENT_BLANK || !evdata)
  11057. goto exit;
  11058. blank = evdata->data;
  11059. if (*blank == FB_BLANK_UNBLANK) {
  11060. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11061. if (cd->fb_state != FB_ON) {
  11062. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11063. #if defined(CONFIG_PM_SLEEP)
  11064. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11065. pt_core_resume_(cd->dev);
  11066. #endif
  11067. cd->fb_state = FB_ON;
  11068. }
  11069. } else if (*blank == FB_BLANK_POWERDOWN) {
  11070. pt_debug(cd->dev, DL_INFO, "%s: POWERDOWN!\n", __func__);
  11071. if (cd->fb_state != FB_OFF) {
  11072. #if defined(CONFIG_PM_SLEEP)
  11073. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11074. pt_core_suspend_(cd->dev);
  11075. #endif
  11076. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11077. cd->fb_state = FB_OFF;
  11078. }
  11079. }
  11080. exit:
  11081. return 0;
  11082. }
  11083. /*******************************************************************************
  11084. * FUNCTION: pt_setup_fb_notifier
  11085. *
  11086. * SUMMARY: Set up call back function into fb notifier.
  11087. *
  11088. * PARAMETERS:
  11089. * *cd - pointer to core data
  11090. ******************************************************************************/
  11091. static void pt_setup_fb_notifier(struct pt_core_data *cd)
  11092. {
  11093. int rc = 0;
  11094. cd->fb_state = FB_ON;
  11095. cd->fb_notifier.notifier_call = fb_notifier_callback;
  11096. rc = fb_register_client(&cd->fb_notifier);
  11097. if (rc)
  11098. pt_debug(cd->dev, DL_ERROR,
  11099. "Unable to register fb_notifier: %d\n", rc);
  11100. }
  11101. #endif
  11102. /*******************************************************************************
  11103. * FUNCTION: pt_watchdog_work
  11104. *
  11105. * SUMMARY: This is where the watchdog work is done except if the DUT is
  11106. * sleeping then this function simply returns. If the DUT is awake the
  11107. * first thing is to ensure the IRQ is not stuck asserted meaning that
  11108. * somehow a response is waiting on the DUT that has not been read. If
  11109. * this occurs the message is simply consumed. If or once the IRQ is
  11110. * cleared, a PIP PING message is sent to the DUT and if the response
  11111. * is received the watchdog succeeds and exits, if no response is seen
  11112. * a startup is queued unless the maximum number of startups have already
  11113. * been attempted, in that case a BL is attempted.
  11114. *
  11115. * NOTE: pt_stop_wd_timer() cannot be called within the context of this
  11116. * work thread
  11117. *
  11118. * RETURN: void
  11119. *
  11120. * PARAMETERS:
  11121. * *work - pointer to a work structure for the watchdog work queue
  11122. ******************************************************************************/
  11123. static void pt_watchdog_work(struct work_struct *work)
  11124. {
  11125. int rc = 0;
  11126. struct pt_core_data *cd = container_of(work,
  11127. struct pt_core_data, watchdog_work);
  11128. /*
  11129. * if found the current sleep_state is SS_SLEEPING
  11130. * then no need to request_exclusive, directly return
  11131. */
  11132. if (cd->sleep_state == SS_SLEEPING)
  11133. return;
  11134. #ifdef TTDL_DIAGNOSTICS
  11135. cd->watchdog_count++;
  11136. #endif /* TTDL_DIAGNOSTICS */
  11137. /*
  11138. * The first WD interval was extended to allow DDI to come up.
  11139. * If the WD interval is not the default then adjust timer to the
  11140. * current setting. The user can override value with drv_debug sysfs.
  11141. */
  11142. if (cd->watchdog_interval != PT_WATCHDOG_TIMEOUT) {
  11143. mod_timer_pending(&cd->watchdog_timer, jiffies +
  11144. msecs_to_jiffies(cd->watchdog_interval));
  11145. }
  11146. if (pt_check_irq_asserted(cd)) {
  11147. #ifdef TTDL_DIAGNOSTICS
  11148. cd->watchdog_irq_stuck_count++;
  11149. pt_toggle_err_gpio(cd, PT_ERR_GPIO_IRQ_STUCK);
  11150. #endif /* TTDL_DIAGNOSTICS */
  11151. pt_debug(cd->dev, DL_ERROR,
  11152. "%s: TTDL WD found IRQ asserted, attempt to clear\n",
  11153. __func__);
  11154. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  11155. }
  11156. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11157. if (rc < 0) {
  11158. pt_debug(cd->dev, DL_ERROR,
  11159. "%s: fail get exclusive ex=%p own=%p\n",
  11160. __func__, cd->exclusive_dev, cd->dev);
  11161. goto queue_startup;
  11162. }
  11163. rc = pt_pip_null_(cd);
  11164. if (release_exclusive(cd, cd->dev) < 0)
  11165. pt_debug(cd->dev, DL_ERROR,
  11166. "%s: fail to release exclusive\n", __func__);
  11167. queue_startup:
  11168. if (rc) {
  11169. #ifdef TTDL_DIAGNOSTICS
  11170. cd->watchdog_failed_access_count++;
  11171. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EXCLUSIVE_ACCESS);
  11172. #endif /* TTDL_DIAGNOSTICS */
  11173. pt_debug(cd->dev, DL_ERROR,
  11174. "%s: failed to access device in WD, retry count=%d\n",
  11175. __func__, cd->startup_retry_count);
  11176. /* Already tried FW upgrade because of watchdog but failed */
  11177. if (cd->startup_retry_count > PT_WATCHDOG_RETRY_COUNT)
  11178. return;
  11179. if (cd->startup_retry_count++ < PT_WATCHDOG_RETRY_COUNT) {
  11180. /*
  11181. * Any wrapper function that trys to disable the
  11182. * WD killing this worker cannot be called here.
  11183. */
  11184. rc = request_exclusive(cd, cd->dev,
  11185. PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11186. if (rc < 0) {
  11187. pt_debug(cd->dev, DL_ERROR,
  11188. "%s: fail get exclusive ex=%p own=%p\n",
  11189. __func__, cd->exclusive_dev, cd->dev);
  11190. goto exit;
  11191. }
  11192. cd->hw_detected = false;
  11193. cd->startup_status = STARTUP_STATUS_START;
  11194. pt_debug(cd->dev, DL_DEBUG,
  11195. "%s: Startup Status Reset\n", __func__);
  11196. rc = pt_dut_reset_and_wait(cd);
  11197. if (release_exclusive(cd, cd->dev) < 0)
  11198. pt_debug(cd->dev, DL_ERROR,
  11199. "%s: fail to release exclusive\n",
  11200. __func__);
  11201. if (!rc) {
  11202. cd->hw_detected = true;
  11203. if (!cd->flashless_dut)
  11204. pt_queue_enum(cd);
  11205. }
  11206. #ifdef TTDL_DIAGNOSTICS
  11207. cd->wd_xres_count++;
  11208. pt_debug(cd->dev, DL_ERROR,
  11209. "%s: Comm Failed - DUT reset [#%d]\n",
  11210. __func__, cd->wd_xres_count);
  11211. #endif /* TTDL_DIAGNOSTICS */
  11212. } else {
  11213. /*
  11214. * After trying PT_WATCHDOG_RETRY_COUNT times to
  11215. * reset the part to regain communications, try to BL
  11216. */
  11217. pt_debug(cd->dev, DL_ERROR,
  11218. "%s: WD DUT access failure, Start FW Upgrade\n",
  11219. __func__);
  11220. #ifdef TTDL_DIAGNOSTICS
  11221. /*
  11222. * When diagnostics is enabled allow TTDL to keep
  11223. * trying to find the DUT. This allows the DUT to be
  11224. * hot swap-able while the host stays running. In
  11225. * production this may not be wanted as a customer
  11226. * may have several touch drivers and any driver
  11227. * that doesn't match the current DUT should give
  11228. * up trying and give up using the bus.
  11229. */
  11230. pt_debug(cd->dev, DL_INFO,
  11231. "%s: Resetting startup_retry_count\n",
  11232. __func__);
  11233. cd->startup_retry_count = 0;
  11234. #endif /* TTDL_DIAGNOSTICS */
  11235. /*
  11236. * Since fw may be broken,reset sysinfo ready flag
  11237. * to let upgrade function work.
  11238. */
  11239. mutex_lock(&cd->system_lock);
  11240. cd->sysinfo.ready = false;
  11241. mutex_unlock(&cd->system_lock);
  11242. if (cd->active_dut_generation == DUT_UNKNOWN) {
  11243. pt_debug(cd->dev, DL_ERROR,
  11244. "%s: Queue Restart\n", __func__);
  11245. pt_queue_restart(cd);
  11246. } else
  11247. kthread_run(start_fw_upgrade, cd, "pt_loader");
  11248. }
  11249. } else {
  11250. cd->hw_detected = true;
  11251. if (cd->startup_status <= (STARTUP_STATUS_FW_RESET_SENTINEL |
  11252. STARTUP_STATUS_BL_RESET_SENTINEL)) {
  11253. pt_debug(cd->dev, DL_ERROR,
  11254. "%s: HW detected but not enumerated\n",
  11255. __func__);
  11256. pt_queue_enum(cd);
  11257. }
  11258. }
  11259. exit:
  11260. pt_start_wd_timer(cd);
  11261. }
  11262. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  11263. /*******************************************************************************
  11264. * FUNCTION: pt_watchdog_timer
  11265. *
  11266. * SUMMARY: The function that is called when the WD timer expires. If the
  11267. * watchdog work is not already busy schedule the watchdog work queue.
  11268. *
  11269. * RETURN: void
  11270. *
  11271. * PARAMETERS:
  11272. * handle - Handle to the watchdog timer
  11273. ******************************************************************************/
  11274. static void pt_watchdog_timer(unsigned long handle)
  11275. {
  11276. struct pt_core_data *cd = (struct pt_core_data *)handle;
  11277. if (!cd)
  11278. return;
  11279. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11280. __func__);
  11281. if (!work_pending(&cd->watchdog_work))
  11282. schedule_work(&cd->watchdog_work);
  11283. }
  11284. #else
  11285. /*******************************************************************************
  11286. * FUNCTION: pt_watchdog_timer
  11287. *
  11288. * SUMMARY: The function that is called when the WD timer expires. If the
  11289. * watchdog work is not already busy schedule the watchdog work queue.
  11290. *
  11291. * RETURN: void
  11292. *
  11293. * PARAMETERS:
  11294. * *t - Pointer to timer list
  11295. ******************************************************************************/
  11296. static void pt_watchdog_timer(struct timer_list *t)
  11297. {
  11298. struct pt_core_data *cd = from_timer(cd, t, watchdog_timer);
  11299. if (!cd)
  11300. return;
  11301. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11302. __func__);
  11303. if (!work_pending(&cd->watchdog_work))
  11304. schedule_work(&cd->watchdog_work);
  11305. }
  11306. #endif
  11307. /*******************************************************************************
  11308. * Core sysfs show and store functions
  11309. ******************************************************************************/
  11310. /*******************************************************************************
  11311. * FUNCTION: pt_hw_version_show
  11312. *
  11313. * SUMMARY: Gets the HW version for either PIP1.x or PIP2.x DUTS
  11314. * Output data format: [SiliconID].[RevID FamilyID].[PanelID]
  11315. *
  11316. * RETURN: size of data written to sysfs node
  11317. *
  11318. * PARAMETERS:
  11319. * *dev - pointer to device structure
  11320. * *attr - pointer to device attributes structure
  11321. * *buf - pointer to print output buffer
  11322. ******************************************************************************/
  11323. static ssize_t pt_hw_version_show(struct device *dev,
  11324. struct device_attribute *attr, char *buf)
  11325. {
  11326. struct pt_core_data *cd = dev_get_drvdata(dev);
  11327. _pt_request_hw_version(dev, cd->hw_version);
  11328. return scnprintf(buf, PT_MAX_PRBUF_SIZE, "%s\n", cd->hw_version);
  11329. }
  11330. static DEVICE_ATTR(hw_version, 0444, pt_hw_version_show, NULL);
  11331. /*******************************************************************************
  11332. * FUNCTION: pt_drv_version_show
  11333. *
  11334. * SUMMARY: Show method for the drv_version sysfs node that will show the
  11335. * TTDL version information
  11336. *
  11337. * RETURN: Char buffer with printed TTDL version information
  11338. *
  11339. * PARAMETERS:
  11340. * *dev - pointer to device structure
  11341. * *attr - pointer to device attributes
  11342. * *buf - pointer to output buffer
  11343. ******************************************************************************/
  11344. static ssize_t pt_drv_version_show(struct device *dev,
  11345. struct device_attribute *attr, char *buf)
  11346. {
  11347. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  11348. "Driver: %s\nVersion: %s\nDate: %s\n",
  11349. pt_driver_core_name, pt_driver_core_version,
  11350. pt_driver_core_date);
  11351. }
  11352. static DEVICE_ATTR(drv_version, 0444, pt_drv_version_show, NULL);
  11353. static DEVICE_ATTR(drv_ver, 0444, pt_drv_version_show, NULL);
  11354. /*******************************************************************************
  11355. * FUNCTION: pt_fw_version_show
  11356. *
  11357. * SUMMARY: Show method for the fw_version sysfs node that will
  11358. * show the firmware, bootloader and PIP version information
  11359. *
  11360. * RETURN: Size of printed buffer
  11361. *
  11362. * PARAMETERS:
  11363. * *dev - pointer to device structure
  11364. * *attr - pointer to device attributes
  11365. * *buf - pointer to output buffer
  11366. ******************************************************************************/
  11367. static ssize_t pt_fw_version_show(struct device *dev,
  11368. struct device_attribute *attr, char *buf)
  11369. {
  11370. struct pt_core_data *cd = dev_get_drvdata(dev);
  11371. struct pt_ttdata *ttdata;
  11372. int rc = 0;
  11373. if (cd->mode == PT_MODE_OPERATIONAL)
  11374. rc = pt_hid_output_get_sysinfo_(cd);
  11375. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11376. __func__, cd->mode, cd->sysinfo.ready);
  11377. if (cd->sysinfo.ready)
  11378. ttdata = &cd->sysinfo.ttdata;
  11379. else
  11380. rc = -ENODATA;
  11381. if (!rc) {
  11382. return scnprintf(buf, strlen(buf),
  11383. "Status: %d\n"
  11384. "FW : %d.%d.%d\n"
  11385. "Config: %d\n"
  11386. "BL : %d.%d\n"
  11387. "PIP : %d.%d\n",
  11388. rc,
  11389. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  11390. ttdata->revctrl,
  11391. ttdata->fw_ver_conf,
  11392. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  11393. ttdata->pip_ver_major, ttdata->pip_ver_minor);
  11394. } else {
  11395. return scnprintf(buf, strlen(buf),
  11396. "Status: %d\n"
  11397. "FW : n/a\n"
  11398. "Config: n/a\n"
  11399. "BL : n/a\n"
  11400. "PIP : n/a\n",
  11401. rc);
  11402. }
  11403. }
  11404. static DEVICE_ATTR(fw_version, 0444, pt_fw_version_show, NULL);
  11405. /*******************************************************************************
  11406. * FUNCTION: pt_sysinfo_show
  11407. *
  11408. * SUMMARY: Show method for the sysinfo sysfs node that will
  11409. * show all the information from get system information command.
  11410. *
  11411. * RETURN: Size of printed buffer
  11412. *
  11413. * PARAMETERS:
  11414. * *dev - pointer to device structure
  11415. * *attr - pointer to device attributes
  11416. * *buf - pointer to output buffer
  11417. ******************************************************************************/
  11418. static ssize_t pt_sysinfo_show(struct device *dev,
  11419. struct device_attribute *attr, char *buf)
  11420. {
  11421. struct pt_core_data *cd = dev_get_drvdata(dev);
  11422. struct pt_sysinfo *si;
  11423. struct pt_ttdata *ttdata = NULL;
  11424. struct pt_sensing_conf_data *scd = NULL;
  11425. int rc = 0;
  11426. if (cd->mode == PT_MODE_OPERATIONAL) {
  11427. rc = pt_hid_output_get_sysinfo_(cd);
  11428. if (cd->sysinfo.ready) {
  11429. si = &cd->sysinfo;
  11430. ttdata = &si->ttdata;
  11431. scd = &si->sensing_conf_data;
  11432. } else
  11433. rc = -ENODATA;
  11434. } else
  11435. rc = -EPERM;
  11436. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11437. __func__, cd->mode, cd->sysinfo.ready);
  11438. if (!rc && ttdata && scd) {
  11439. return scnprintf(buf, strlen(buf),
  11440. "Status: %d\n"
  11441. "pip_ver_major: 0x%02X\n"
  11442. "pip_ver_minor: 0x%02X\n"
  11443. "fw_pid : 0x%04X\n"
  11444. "fw_ver_major : 0x%02X\n"
  11445. "fw_ver_minor : 0x%02X\n"
  11446. "revctrl : 0x%08X\n"
  11447. "fw_ver_conf : 0x%04X\n"
  11448. "bl_ver_major : 0x%02X\n"
  11449. "bl_ver_minor : 0x%02X\n"
  11450. "jtag_id_h : 0x%04X\n"
  11451. "jtag_id_l : 0x%04X\n"
  11452. "mfg_id[0] : 0x%02X\n"
  11453. "mfg_id[1] : 0x%02X\n"
  11454. "mfg_id[2] : 0x%02X\n"
  11455. "mfg_id[3] : 0x%02X\n"
  11456. "mfg_id[4] : 0x%02X\n"
  11457. "mfg_id[5] : 0x%02X\n"
  11458. "mfg_id[6] : 0x%02X\n"
  11459. "mfg_id[7] : 0x%02X\n"
  11460. "post_code : 0x%04X\n"
  11461. "electrodes_x : 0x%02X\n"
  11462. "electrodes_y : 0x%02X\n"
  11463. "len_x : 0x%04X\n"
  11464. "len_y : 0x%04X\n"
  11465. "res_x : 0x%04X\n"
  11466. "res_y : 0x%04X\n"
  11467. "max_z : 0x%04X\n"
  11468. "origin_x : 0x%02X\n"
  11469. "origin_y : 0x%02X\n"
  11470. "panel_id : 0x%02X\n"
  11471. "btn : 0x%02X\n"
  11472. "scan_mode : 0x%02X\n"
  11473. "max_num_of_tch_per_refresh_cycle: 0x%02X\n",
  11474. rc,
  11475. ttdata->pip_ver_major,
  11476. ttdata->pip_ver_minor,
  11477. ttdata->fw_pid,
  11478. ttdata->fw_ver_major,
  11479. ttdata->fw_ver_minor,
  11480. ttdata->revctrl,
  11481. ttdata->fw_ver_conf,
  11482. ttdata->bl_ver_major,
  11483. ttdata->bl_ver_minor,
  11484. ttdata->jtag_id_h,
  11485. ttdata->jtag_id_l,
  11486. ttdata->mfg_id[0],
  11487. ttdata->mfg_id[1],
  11488. ttdata->mfg_id[2],
  11489. ttdata->mfg_id[3],
  11490. ttdata->mfg_id[4],
  11491. ttdata->mfg_id[5],
  11492. ttdata->mfg_id[6],
  11493. ttdata->mfg_id[7],
  11494. ttdata->post_code,
  11495. scd->electrodes_x,
  11496. scd->electrodes_y,
  11497. scd->len_x,
  11498. scd->len_y,
  11499. scd->res_x,
  11500. scd->res_y,
  11501. scd->max_z,
  11502. scd->origin_x,
  11503. scd->origin_y,
  11504. scd->panel_id,
  11505. scd->btn,
  11506. scd->scan_mode,
  11507. scd->max_tch);
  11508. } else {
  11509. return scnprintf(buf, strlen(buf),
  11510. "Status: %d\n",
  11511. rc);
  11512. }
  11513. }
  11514. static DEVICE_ATTR(sysinfo, 0444, pt_sysinfo_show, NULL);
  11515. /*******************************************************************************
  11516. * FUNCTION: pt_hw_reset_show
  11517. *
  11518. * SUMMARY: The show method for the hw_reset sysfs node that does a hw reset
  11519. * by toggling the XRES line and then calls the startup function to
  11520. * allow TTDL to re-enumerate the DUT.
  11521. * The printed value reflects the status of the full reset/enum.
  11522. *
  11523. * PARAMETERS:
  11524. * *dev - pointer to Device structure
  11525. * *attr - pointer to the device attribute structure
  11526. * *buf - pointer to buffer to print
  11527. ******************************************************************************/
  11528. static ssize_t pt_hw_reset_show(struct device *dev,
  11529. struct device_attribute *attr, char *buf)
  11530. {
  11531. struct pt_core_data *cd = dev_get_drvdata(dev);
  11532. int rc = 0;
  11533. int time = 0;
  11534. u8 reset_status = 0;
  11535. int t;
  11536. struct pt_hid_desc hid_desc;
  11537. memset(&hid_desc, 0, sizeof(hid_desc));
  11538. /* Only allow DUT reset if no active BL in progress */
  11539. mutex_lock(&cd->firmware_class_lock);
  11540. mutex_lock(&cd->system_lock);
  11541. cd->startup_state = STARTUP_NONE;
  11542. mutex_unlock(&(cd->system_lock));
  11543. pt_stop_wd_timer(cd);
  11544. /* ensure no left over exclusive access is still locked */
  11545. release_exclusive(cd, cd->dev);
  11546. rc = pt_dut_reset(cd, PT_CORE_CMD_PROTECTED);
  11547. if (rc) {
  11548. mutex_unlock(&cd->firmware_class_lock);
  11549. pt_debug(cd->dev, DL_ERROR,
  11550. "%s: HW reset failed rc = %d\n", __func__, rc);
  11551. goto exit_hw_reset;
  11552. }
  11553. reset_status |= 0x01 << 0;
  11554. if (cd->flashless_dut) {
  11555. mutex_unlock(&cd->firmware_class_lock);
  11556. t = wait_event_timeout(cd->wait_q, (cd->fw_updating == true),
  11557. msecs_to_jiffies(200));
  11558. if (IS_TMO(t)) {
  11559. pt_debug(dev, DL_ERROR,
  11560. "%s: Timeout waiting for FW update",
  11561. __func__);
  11562. rc = -ETIME;
  11563. goto exit_hw_reset;
  11564. } else {
  11565. pt_debug(dev, DL_INFO,
  11566. "%s: ----- Wait FW Loading ----",
  11567. __func__);
  11568. rc = _pt_request_wait_for_enum_state(
  11569. dev, 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  11570. if (rc) {
  11571. pt_debug(cd->dev, DL_ERROR,
  11572. "%s: No FW Sentinel detected rc = %d\n",
  11573. __func__, rc);
  11574. goto exit_hw_reset;
  11575. }
  11576. reset_status |= 0x01 << 1;
  11577. }
  11578. } else {
  11579. /* Wait for any sentinel */
  11580. rc = _pt_request_wait_for_enum_state(dev, 150,
  11581. STARTUP_STATUS_BL_RESET_SENTINEL |
  11582. STARTUP_STATUS_FW_RESET_SENTINEL);
  11583. if (rc) {
  11584. mutex_unlock(&cd->firmware_class_lock);
  11585. pt_debug(cd->dev, DL_ERROR,
  11586. "%s: No Sentinel detected rc = %d\n",
  11587. __func__, rc);
  11588. goto exit_hw_reset;
  11589. }
  11590. /* sleep needed to ensure no cmd is sent while DUT will NAK */
  11591. msleep(30);
  11592. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  11593. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  11594. if (rc < 0) {
  11595. pt_debug(cd->dev, DL_ERROR,
  11596. "%s: Error on getting HID descriptor r=%d\n",
  11597. __func__, rc);
  11598. goto exit_hw_reset;
  11599. }
  11600. cd->mode = pt_get_mode(cd, &hid_desc);
  11601. if (cd->mode == PT_MODE_BOOTLOADER)
  11602. rc = pt_hid_output_bl_launch_app_(cd);
  11603. } else {
  11604. if (cd->mode == PT_MODE_BOOTLOADER)
  11605. rc = pt_pip2_launch_app(dev,
  11606. PT_CORE_CMD_UNPROTECTED);
  11607. }
  11608. if (rc) {
  11609. mutex_unlock(&cd->firmware_class_lock);
  11610. pt_debug(cd->dev, DL_ERROR,
  11611. "%s: PIP launch app failed rc = %d\n",
  11612. __func__, rc);
  11613. goto exit_hw_reset;
  11614. }
  11615. mutex_unlock(&cd->firmware_class_lock);
  11616. reset_status |= 0x01 << 1;
  11617. msleep(20);
  11618. if ((cd->active_dut_generation == DUT_UNKNOWN) ||
  11619. (cd->mode != PT_MODE_OPERATIONAL))
  11620. pt_queue_restart(cd);
  11621. else
  11622. pt_queue_enum(cd);
  11623. }
  11624. while (!(cd->startup_status & STARTUP_STATUS_COMPLETE) && time < 2000) {
  11625. msleep(50);
  11626. pt_debug(cd->dev, DL_INFO,
  11627. "%s: wait %dms for 0x%04X, current enum=0x%04X\n",
  11628. __func__, time, STARTUP_STATUS_COMPLETE,
  11629. cd->startup_status);
  11630. time += 50;
  11631. }
  11632. if (!(cd->startup_status & STARTUP_STATUS_COMPLETE)) {
  11633. rc = -ETIME;
  11634. goto exit_hw_reset;
  11635. }
  11636. pt_debug(cd->dev, DL_INFO, "%s: HW Reset complete. enum=0x%04X\n",
  11637. __func__, cd->startup_status);
  11638. reset_status |= 0x01 << 2;
  11639. pt_start_wd_timer(cd);
  11640. exit_hw_reset:
  11641. return scnprintf(buf, strlen(buf),
  11642. "Status: %d\n"
  11643. "Reset Status: 0x%02X\n", rc, reset_status);
  11644. }
  11645. static DEVICE_ATTR(hw_reset, 0444, pt_hw_reset_show, NULL);
  11646. /*******************************************************************************
  11647. * FUNCTION: pt_pip2_cmd_rsp_store
  11648. *
  11649. * SUMMARY: This is the store method for the raw PIP2 cmd/rsp sysfs node. Any
  11650. * raw PIP2 command echo'd to this node will be sent directly to the DUT.
  11651. * Command byte order:
  11652. * Byte [0] - PIP2 command ID
  11653. * Byte [1-n] - PIP2 command payload
  11654. *
  11655. * RETURN: Size of passed in buffer
  11656. *
  11657. * PARAMETERS:
  11658. * *dev - pointer to device structure
  11659. * *attr - pointer to device attributes
  11660. * *buf - pointer to buffer that hold the command parameters
  11661. * size - size of buf
  11662. ******************************************************************************/
  11663. static ssize_t pt_pip2_cmd_rsp_store(struct device *dev,
  11664. struct device_attribute *attr, const char *buf, size_t size)
  11665. {
  11666. struct pt_core_data *cd = dev_get_drvdata(dev);
  11667. u16 actual_read_len;
  11668. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  11669. u8 read_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  11670. u8 pip2_cmd_id = 0x00;
  11671. u8 *pip2_cmd_data = NULL;
  11672. int data_len = 0;
  11673. int length;
  11674. int rc = 0;
  11675. /* clear shared data */
  11676. mutex_lock(&cd->sysfs_lock);
  11677. cd->raw_cmd_status = 0;
  11678. cd->cmd_rsp_buf_len = 0;
  11679. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  11680. mutex_unlock(&cd->sysfs_lock);
  11681. length = _pt_ic_parse_input_hex(dev, buf, size,
  11682. input_data, PT_MAX_PIP2_MSG_SIZE);
  11683. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  11684. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  11685. __func__);
  11686. rc = -EINVAL;
  11687. goto exit;
  11688. }
  11689. /* Send PIP2 command if enough data was provided */
  11690. if (length >= 1) {
  11691. pip2_cmd_id = input_data[0];
  11692. pip2_cmd_data = &input_data[1];
  11693. data_len = length - 1;
  11694. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  11695. pip2_cmd_id, pip2_cmd_data, data_len,
  11696. read_buf, &actual_read_len);
  11697. cd->raw_cmd_status = rc;
  11698. if (rc) {
  11699. pt_debug(dev, DL_ERROR,
  11700. "%s: PIP2 cmd 0x%02x failed rc = %d\n",
  11701. __func__, pip2_cmd_id, rc);
  11702. goto exit;
  11703. } else {
  11704. cd->cmd_rsp_buf_len = actual_read_len;
  11705. memcpy(cd->cmd_rsp_buf, read_buf, actual_read_len);
  11706. pt_debug(dev, DL_ERROR,
  11707. "%s: PIP2 actual_read_len = %d\n",
  11708. __func__, actual_read_len);
  11709. }
  11710. } else {
  11711. rc = -EINVAL;
  11712. pt_debug(dev, DL_ERROR,
  11713. "%s: Insufficient data provided for PIP2 cmd\n",
  11714. __func__);
  11715. }
  11716. exit:
  11717. if (rc)
  11718. return rc;
  11719. return size;
  11720. }
  11721. /*******************************************************************************
  11722. * FUNCTION: pt_pip2_cmd_rsp_show
  11723. *
  11724. * SUMMARY: The show method for the raw pip2_cmd_rsp sysfs node. Any PIP2
  11725. * response generated after using the store method of the pip2_cmd_rsp
  11726. * sysfs node, are available to be read here.
  11727. *
  11728. * PARAMETERS:
  11729. * *dev - pointer to Device structure
  11730. * *attr - pointer to the device attribute structure
  11731. * *buf - pointer to buffer to print
  11732. ******************************************************************************/
  11733. static ssize_t pt_pip2_cmd_rsp_show(struct device *dev,
  11734. struct device_attribute *attr, char *buf)
  11735. {
  11736. struct pt_core_data *cd = dev_get_drvdata(dev);
  11737. int i;
  11738. ssize_t data_len;
  11739. int index;
  11740. mutex_lock(&cd->sysfs_lock);
  11741. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  11742. "Status: %d\n", cd->raw_cmd_status);
  11743. if (cd->raw_cmd_status)
  11744. goto error;
  11745. /* Remove the CRC from the length of the response */
  11746. data_len = cd->cmd_rsp_buf_len - 2;
  11747. /* Start printing from the data payload */
  11748. for (i = PIP1_RESP_COMMAND_ID_OFFSET; i < data_len; i++)
  11749. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  11750. "%02X ", cd->cmd_rsp_buf[i]);
  11751. if (data_len >= PIP1_RESP_COMMAND_ID_OFFSET) {
  11752. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  11753. "\n(%zd bytes)\n",
  11754. data_len - PIP1_RESP_COMMAND_ID_OFFSET);
  11755. } else {
  11756. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  11757. "\n(%zd bytes)\n", 0);
  11758. }
  11759. error:
  11760. mutex_unlock(&cd->sysfs_lock);
  11761. return index;
  11762. }
  11763. static DEVICE_ATTR(pip2_cmd_rsp, 0644, pt_pip2_cmd_rsp_show,
  11764. pt_pip2_cmd_rsp_store);
  11765. /*******************************************************************************
  11766. * FUNCTION: pt_command_store
  11767. *
  11768. * SUMMARY: This is the store method for the raw PIP command sysfs node. Any
  11769. * raw PIP command echo'd to this node will be sent directly to the DUT.
  11770. * TTDL will not parse the command.
  11771. *
  11772. * RETURN: Size of passed in buffer
  11773. *
  11774. * PARAMETERS:
  11775. * *dev - pointer to device structure
  11776. * *attr - pointer to device attributes
  11777. * *buf - pointer to buffer that hold the command parameters
  11778. * size - size of buf
  11779. ******************************************************************************/
  11780. static ssize_t pt_command_store(struct device *dev,
  11781. struct device_attribute *attr, const char *buf, size_t size)
  11782. {
  11783. struct pt_core_data *cd = dev_get_drvdata(dev);
  11784. unsigned short crc;
  11785. u16 actual_read_len;
  11786. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  11787. int length;
  11788. int len_field;
  11789. int rc = 0;
  11790. mutex_lock(&cd->sysfs_lock);
  11791. cd->cmd_rsp_buf_len = 0;
  11792. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  11793. mutex_unlock(&cd->sysfs_lock);
  11794. length = _pt_ic_parse_input_hex(dev, buf, size,
  11795. input_data, PT_MAX_PIP2_MSG_SIZE);
  11796. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  11797. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  11798. __func__);
  11799. rc = -EINVAL;
  11800. goto pt_command_store_exit;
  11801. }
  11802. /* PIP2 messages begin with 01 01 */
  11803. if (length >= 2 && input_data[0] == 0x01 && input_data[1] == 0x01) {
  11804. cd->pip2_prot_active = 1;
  11805. /* Override next seq tag with what was sent */
  11806. cd->pip2_cmd_tag_seq = input_data[4] & 0x0F;
  11807. /* For PIP2 cmd if length does not include crc, add it */
  11808. len_field = (input_data[3] << 8) | input_data[2];
  11809. if (len_field == length && length <= 254) {
  11810. crc = crc_ccitt_calculate(&input_data[2],
  11811. length - 2);
  11812. pt_debug(dev, DL_ERROR, "%s: len=%d crc=0x%02X\n",
  11813. __func__, length, crc);
  11814. input_data[length] = (crc & 0xFF00) >> 8;
  11815. input_data[length + 1] = crc & 0x00FF;
  11816. length = length + 2;
  11817. }
  11818. }
  11819. /* write PIP command to log */
  11820. pt_pr_buf(dev, DL_INFO, input_data, length, "command_buf");
  11821. pm_runtime_get_sync(dev);
  11822. rc = pt_hid_output_user_cmd(cd, PT_MAX_INPUT, cd->cmd_rsp_buf,
  11823. length, input_data, &actual_read_len);
  11824. pm_runtime_put(dev);
  11825. mutex_lock(&cd->sysfs_lock);
  11826. cd->raw_cmd_status = rc;
  11827. if (rc) {
  11828. cd->cmd_rsp_buf_len = 0;
  11829. pt_debug(dev, DL_ERROR, "%s: Failed to send command: %s\n",
  11830. __func__, buf);
  11831. } else {
  11832. cd->cmd_rsp_buf_len = actual_read_len;
  11833. }
  11834. cd->pip2_prot_active = 0;
  11835. mutex_unlock(&cd->sysfs_lock);
  11836. pt_command_store_exit:
  11837. if (rc)
  11838. return rc;
  11839. return size;
  11840. }
  11841. static DEVICE_ATTR(command, 0220, NULL, pt_command_store);
  11842. /*******************************************************************************
  11843. * FUNCTION: pt_response_show
  11844. *
  11845. * SUMMARY: The show method for the raw PIP response sysfs node. Any PIP
  11846. * response generated after using the pt_command_store sysfs node, are
  11847. * available to be read here.
  11848. *
  11849. * PARAMETERS:
  11850. * *dev - pointer to Device structure
  11851. * *attr - pointer to the device attribute structure
  11852. * *buf - pointer to buffer to print
  11853. ******************************************************************************/
  11854. static ssize_t pt_response_show(struct device *dev,
  11855. struct device_attribute *attr, char *buf)
  11856. {
  11857. struct pt_core_data *cd = dev_get_drvdata(dev);
  11858. int i;
  11859. ssize_t num_read;
  11860. int index;
  11861. mutex_lock(&cd->sysfs_lock);
  11862. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  11863. "Status: %d\n", cd->raw_cmd_status);
  11864. if (cd->raw_cmd_status)
  11865. goto error;
  11866. num_read = cd->cmd_rsp_buf_len;
  11867. for (i = 0; i < num_read; i++)
  11868. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  11869. "0x%02X\n", cd->cmd_rsp_buf[i]);
  11870. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  11871. "(%zd bytes)\n", num_read);
  11872. error:
  11873. mutex_unlock(&cd->sysfs_lock);
  11874. return index;
  11875. }
  11876. static DEVICE_ATTR(response, 0444, pt_response_show, NULL);
  11877. /*******************************************************************************
  11878. * FUNCTION: pt_dut_debug_show
  11879. *
  11880. * SUMMARY: Show method for the dut_debug sysfs node. Shows what parameters
  11881. * are available for the store method.
  11882. *
  11883. * RETURN:
  11884. * 0 = success
  11885. * !0 = failure
  11886. *
  11887. * PARAMETERS:
  11888. * *dev - pointer to device structure
  11889. * *attr - pointer to device attributes
  11890. * *buf - pointer to output buffer
  11891. ******************************************************************************/
  11892. static ssize_t pt_dut_debug_show(struct device *dev,
  11893. struct device_attribute *attr, char *buf)
  11894. {
  11895. ssize_t ret;
  11896. ret = scnprintf(buf, strlen(buf),
  11897. "Status: 0\n"
  11898. "dut_debug sends the following commands to the DUT:\n"
  11899. "%d %s \t- %s\n"
  11900. "%d %s \t- %s\n"
  11901. "%d %s \t- %s\n"
  11902. "%d %s \t- %s\n"
  11903. "%d %s \t- %s\n"
  11904. "%d %s \t- %s\n"
  11905. "%d %s \t- %s\n"
  11906. "%d %s \t- %s\n"
  11907. "%d %s \t- %s\n"
  11908. "%d %s \t- %s\n"
  11909. "%d %s \t- %s\n"
  11910. "%d %s \t- %s\n"
  11911. "%d %s \t- %s\n"
  11912. "%d %s \t- %s\n"
  11913. "%d %s \t- %s\n"
  11914. "%d %s \t- %s\n"
  11915. "%d %s \t- %s\n"
  11916. ,
  11917. PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY, "", "BL Verify APP",
  11918. PT_DUT_DBG_HID_RESET, "", "HID Reset",
  11919. PT_DUT_DBG_HID_SET_POWER_ON, "", "HID SET_POWER ON",
  11920. PT_DUT_DBG_HID_SET_POWER_SLEEP, "", "HID SET_POWER SLEEP",
  11921. PT_DUT_DBG_HID_SET_POWER_STANDBY, "", "HID SET_POWER STANDBY",
  11922. PIP1_BL_CMD_ID_GET_INFO, "", "BL Get Info",
  11923. PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY, "", "BL Program & Verify",
  11924. PIP1_BL_CMD_ID_LAUNCH_APP, "", "BL Launch APP",
  11925. PIP1_BL_CMD_ID_INITIATE_BL, "", "BL Initiate BL",
  11926. PT_DUT_DBG_PIP_SOFT_RESET, "", "PIP Soft Reset",
  11927. PT_DUT_DBG_RESET, "", "Toggle the TP_XRES GPIO",
  11928. PT_DUT_DBG_PIP_NULL, "", "PIP NULL (PING)",
  11929. PT_DUT_DBG_PIP_ENTER_BL, "", "PIP enter BL",
  11930. PT_DUT_DBG_HID_SYSINFO, "", "HID system info",
  11931. PT_DUT_DBG_PIP_SUSPEND_SCAN, "", "Suspend Scan",
  11932. PT_DUT_DBG_PIP_RESUME_SCAN, "", "Resume Scan",
  11933. PT_DUT_DBG_HID_DESC, "", "Get HID Desc"
  11934. );
  11935. return ret;
  11936. }
  11937. /*******************************************************************************
  11938. * FUNCTION: pt_drv_debug_show
  11939. *
  11940. * SUMMARY: Show method for the drv_debug sysfs node. Shows what parameters
  11941. * are available for the store method.
  11942. *
  11943. * RETURN:
  11944. * 0 = success
  11945. * !0 = failure
  11946. *
  11947. * PARAMETERS:
  11948. * *dev - pointer to device structure
  11949. * *attr - pointer to device attributes
  11950. * *buf - pointer to output buffer
  11951. ******************************************************************************/
  11952. static ssize_t pt_drv_debug_show(struct device *dev,
  11953. struct device_attribute *attr, char *buf)
  11954. {
  11955. ssize_t ret;
  11956. ret = scnprintf(buf, strlen(buf),
  11957. "Status: 0\n"
  11958. "drv_debug supports the following values:\n"
  11959. "%d %s \t- %s\n"
  11960. "%d %s \t- %s\n"
  11961. "%d %s \t- %s\n"
  11962. "%d %s \t- %s\n"
  11963. "%d %s \t- %s\n"
  11964. "%d %s \t- %s\n"
  11965. "%d %s \t- %s\n"
  11966. "%d %s \t- %s\n"
  11967. "%d %s \t- %s\n"
  11968. "%d %s - %s\n"
  11969. "%d %s \t- %s\n"
  11970. "%d %s \t- %s\n"
  11971. #ifdef TTDL_DIAGNOSTICS
  11972. "%d %s \t- %s\n"
  11973. "%d %s \t- %s\n"
  11974. "%d %s \t- %s\n"
  11975. #endif /* TTDL_DIAGNOSTICS */
  11976. "%d %s \t- %s\n"
  11977. #ifdef TTDL_DIAGNOSTICS
  11978. "%d %s \t- %s\n"
  11979. "%d %s \t- %s\n"
  11980. "%d %s \t- %s\n"
  11981. "%d %s \t- %s\n"
  11982. "%d %s \t- %s\n"
  11983. "%d %s \t- %s\n"
  11984. "%d %s \t- %s\n"
  11985. "%d %s \t- %s\n"
  11986. "%d %s \t- %s\n"
  11987. #endif /* TTDL_DIAGNOSTICS */
  11988. ,
  11989. PT_DRV_DBG_SUSPEND, " ", "Suspend TTDL responding to INT",
  11990. PT_DRV_DBG_RESUME, " ", "Resume TTDL responding to INT",
  11991. PT_DRV_DBG_STOP_WD, " ", "Stop TTDL WD",
  11992. PT_DRV_DBG_START_WD, " ", "Start TTDL WD",
  11993. PT_DRV_DBG_TTHE_TUNER_EXIT, " ", "Exit TTHE Tuner Logging",
  11994. PT_DRV_DBG_TTHE_BUF_CLEAN, " ", "Clear TTHE Tuner buffer",
  11995. PT_DRV_DBG_CLEAR_PARM_LIST, " ", "Clear RAM Param list",
  11996. PT_DRV_DBG_FORCE_BUS_READ, " ", "Force bus read",
  11997. PT_DRV_DBG_CLEAR_CAL_DATA, " ", "Clear CAL Cache",
  11998. PT_DRV_DBG_REPORT_LEVEL, "[0|1|2|3|4]", "Set TTDL Debug Level",
  11999. PT_DRV_DBG_WATCHDOG_INTERVAL, "[n] ", "TTDL WD Interval in ms",
  12000. PT_DRV_DBG_SHOW_TIMESTAMP, "[0|1]", "Show Timestamps"
  12001. #ifdef TTDL_DIAGNOSTICS
  12002. , PT_DRV_DBG_SETUP_PWR, "[0|1]", "Power DUT up/down",
  12003. PT_DRV_DBG_GET_PUT_SYNC, "[0|1]", "Get/Put Linux Sleep",
  12004. PT_DRV_DBG_SET_TT_DATA, "[0|1]", "Display TT_DATA"
  12005. #endif /* TTDL_DIAGNOSTICS */
  12006. , PT_DRV_DBG_SET_GENERATION, "[0|1|2]", "Set DUT generation"
  12007. #ifdef TTDL_DIAGNOSTICS
  12008. , PT_DRV_DBG_SET_BRIDGE_MODE, "[0|1]", "On/Off Bridge Mode",
  12009. PT_DRV_DBG_SET_I2C_ADDRESS, "[0-127]", "I2C DUT Address",
  12010. PT_DRV_DBG_SET_FLASHLESS_DUT, "[0|1]", "Flashless DUT yes/no",
  12011. PT_DRV_DBG_SET_FORCE_SEQ, "[8-15]", "Force PIP2 Sequence #",
  12012. PT_DRV_DBG_BL_WITH_NO_INT, "[0|1]", "BL with no INT",
  12013. PT_DRV_DBG_CAL_CACHE_IN_HOST, "[0|1]", "CAL Cache in host",
  12014. PT_DRV_DBG_MULTI_CHIP, "[0|1]", "Multi Chip Support",
  12015. PT_DRV_DBG_PIP_TIMEOUT, "[100-7000]", "PIP Resp Timeout (ms)",
  12016. PT_DRV_DBG_TTHE_HID_USB_FORMAT, "[0|1]",
  12017. "TTHE_TUNER HID USB Format"
  12018. #endif /* TTDL_DIAGNOSTICS */
  12019. );
  12020. return ret;
  12021. }
  12022. /*******************************************************************************
  12023. * FUNCTION: pt_drv_debug_store
  12024. *
  12025. * SUMMARY: Currently the store method for both sysfs nodes: drv_debug and
  12026. * dut_debug. Drv_debug will contain all functionality that can be run
  12027. * without a DUT preset and is available anytime TTDL is running.
  12028. * Dut_debug requires a DUT to be available and will only be created after
  12029. * a DUT has been detected.
  12030. * This function will eventually be split into two but until the overlap
  12031. * has been depricated this function contains all commands that can be
  12032. * used for TTDL/DUT debugging status and control.
  12033. * All commands require at least one value to be passed in *buf with some
  12034. * requiring two.
  12035. *
  12036. * RETURN: Size of passed in buffer
  12037. *
  12038. * PARAMETERS:
  12039. * *dev - pointer to device structure
  12040. * *attr - pointer to device attributes
  12041. * *buf - pointer to buffer that hold the command parameters
  12042. * size - size of buf
  12043. ******************************************************************************/
  12044. static ssize_t pt_drv_debug_store(struct device *dev,
  12045. struct device_attribute *attr, const char *buf, size_t size)
  12046. {
  12047. struct pt_core_data *cd = dev_get_drvdata(dev);
  12048. unsigned long value;
  12049. int rc = 0;
  12050. u8 return_data[8];
  12051. static u8 wd_disabled;
  12052. u32 input_data[3];
  12053. int length;
  12054. #ifdef TTDL_DIAGNOSTICS
  12055. struct i2c_client *client = to_i2c_client(dev);
  12056. unsigned short crc = 0;
  12057. u16 cal_size;
  12058. #endif
  12059. input_data[0] = 0;
  12060. input_data[1] = 0;
  12061. /* Maximmum input is two elements */
  12062. length = _pt_ic_parse_input(dev, buf, size,
  12063. input_data, ARRAY_SIZE(input_data));
  12064. if (length < 1 || length > 2) {
  12065. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12066. __func__);
  12067. rc = -EINVAL;
  12068. goto pt_drv_debug_store_exit;
  12069. }
  12070. value = input_data[0];
  12071. if (length == 1) {
  12072. pt_debug(dev, DL_DEBUG,
  12073. "%s: Debug Cmd Received (id=%d)\n",
  12074. __func__, input_data[0]);
  12075. } else if (length == 2) {
  12076. pt_debug(dev, DL_DEBUG,
  12077. "%s: Debug Cmd Received (id=%d, data=%d)\n",
  12078. __func__, input_data[0], input_data[1]);
  12079. } else {
  12080. pt_debug(dev, DL_DEBUG,
  12081. "%s: Invalid arguments received\n", __func__);
  12082. rc = -EINVAL;
  12083. goto pt_drv_debug_store_exit;
  12084. }
  12085. /* Start watchdog timer command */
  12086. if (value == PT_DRV_DBG_START_WD) {
  12087. pt_debug(dev, DL_INFO, "%s: Cmd: Start Watchdog\n", __func__);
  12088. wd_disabled = 0;
  12089. cd->watchdog_force_stop = false;
  12090. pt_start_wd_timer(cd);
  12091. goto pt_drv_debug_store_exit;
  12092. }
  12093. /* Stop watchdog timer temporarily */
  12094. pt_stop_wd_timer(cd);
  12095. if (value == PT_DRV_DBG_STOP_WD) {
  12096. pt_debug(dev, DL_INFO, "%s: Cmd: Stop Watchdog\n", __func__);
  12097. wd_disabled = 1;
  12098. cd->watchdog_force_stop = true;
  12099. goto pt_drv_debug_store_exit;
  12100. }
  12101. switch (value) {
  12102. case PT_DRV_DBG_SUSPEND: /* 4 */
  12103. pt_debug(dev, DL_INFO, "%s: TTDL: Core Sleep\n", __func__);
  12104. wd_disabled = 1;
  12105. rc = pt_core_sleep(cd);
  12106. if (rc)
  12107. pt_debug(dev, DL_ERROR, "%s: Suspend failed rc=%d\n",
  12108. __func__, rc);
  12109. else
  12110. pt_debug(dev, DL_INFO, "%s: Suspend succeeded\n",
  12111. __func__);
  12112. break;
  12113. case PT_DRV_DBG_RESUME: /* 5 */
  12114. pt_debug(dev, DL_INFO, "%s: TTDL: Wake\n", __func__);
  12115. rc = pt_core_wake(cd);
  12116. if (rc)
  12117. pt_debug(dev, DL_ERROR, "%s: Resume failed rc=%d\n",
  12118. __func__, rc);
  12119. else
  12120. pt_debug(dev, DL_INFO, "%s: Resume succeeded\n",
  12121. __func__);
  12122. break;
  12123. case PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY: /* BL - 49 */
  12124. pt_debug(dev, DL_INFO, "%s: Cmd: verify app integ\n", __func__);
  12125. pt_hid_output_bl_verify_app_integrity(cd, &return_data[0]);
  12126. break;
  12127. case PT_DUT_DBG_HID_RESET: /* 50 */
  12128. pt_debug(dev, DL_INFO, "%s: Cmd: hid_reset\n", __func__);
  12129. pt_hid_cmd_reset(cd);
  12130. break;
  12131. case PT_DUT_DBG_HID_SET_POWER_ON: /* 53 */
  12132. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_on\n", __func__);
  12133. pt_hid_cmd_set_power(cd, HID_POWER_ON);
  12134. wd_disabled = 0;
  12135. break;
  12136. case PT_DUT_DBG_HID_SET_POWER_SLEEP: /* 54 */
  12137. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_off\n",
  12138. __func__);
  12139. wd_disabled = 1;
  12140. pt_hid_cmd_set_power(cd, HID_POWER_SLEEP);
  12141. break;
  12142. case PT_DUT_DBG_HID_SET_POWER_STANDBY: /* 55 */
  12143. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_standby\n",
  12144. __func__);
  12145. wd_disabled = 1;
  12146. pt_hid_cmd_set_power(cd, HID_POWER_STANDBY);
  12147. break;
  12148. case PIP1_BL_CMD_ID_GET_INFO: /* BL - 56 */
  12149. pt_debug(dev, DL_INFO, "%s: Cmd: bl get info\n", __func__);
  12150. pt_hid_output_bl_get_information(cd, return_data);
  12151. break;
  12152. case PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY: /* BL - 57 */
  12153. pt_debug(dev, DL_INFO, "%s: Cmd: program and verify\n",
  12154. __func__);
  12155. pt_hid_output_bl_program_and_verify(cd, 0, NULL);
  12156. break;
  12157. case PIP1_BL_CMD_ID_LAUNCH_APP: /* BL - 59 */
  12158. pt_debug(dev, DL_INFO, "%s: Cmd: launch app\n", __func__);
  12159. pt_hid_output_bl_launch_app(cd);
  12160. break;
  12161. case PIP1_BL_CMD_ID_INITIATE_BL: /* BL - 72 */
  12162. pt_debug(dev, DL_INFO, "%s: Cmd: initiate bl\n", __func__);
  12163. pt_hid_output_bl_initiate_bl(cd, 0, NULL, 0, NULL);
  12164. break;
  12165. case PT_DUT_DBG_PIP_SOFT_RESET: /* 97 */
  12166. pt_debug(dev, DL_INFO, "%s: Cmd: Soft Reset\n", __func__);
  12167. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  12168. break;
  12169. case PT_DUT_DBG_RESET: /* 98 */
  12170. pt_debug(dev, DL_INFO, "%s: Cmd: Hard Reset\n", __func__);
  12171. rc = pt_hw_hard_reset(cd);
  12172. break;
  12173. case PT_DUT_DBG_PIP_NULL: /* 100 */
  12174. pt_debug(dev, DL_INFO, "%s: Cmd: Ping (null)\n", __func__);
  12175. pt_pip_null(cd);
  12176. break;
  12177. case PT_DUT_DBG_PIP_ENTER_BL: /* 101 */
  12178. pt_debug(dev, DL_INFO, "%s: Cmd: start_bootloader\n", __func__);
  12179. rc = pt_pip_start_bootloader(cd);
  12180. if (!rc) {
  12181. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  12182. cd->mode = PT_MODE_BOOTLOADER;
  12183. }
  12184. break;
  12185. case PT_DUT_DBG_HID_SYSINFO: /* 102 */
  12186. pt_debug(dev, DL_INFO, "%s: Cmd: get_sysinfo\n", __func__);
  12187. pt_hid_output_get_sysinfo(cd);
  12188. break;
  12189. case PT_DUT_DBG_PIP_SUSPEND_SCAN: /* 103 */
  12190. pt_debug(dev, DL_INFO, "%s: Cmd: suspend_scanning\n", __func__);
  12191. pt_pip_suspend_scanning(cd);
  12192. break;
  12193. case PT_DUT_DBG_PIP_RESUME_SCAN: /* 104 */
  12194. pt_debug(dev, DL_INFO, "%s: Cmd: resume_scanning\n", __func__);
  12195. pt_pip_resume_scanning(cd);
  12196. break;
  12197. #ifdef TTHE_TUNER_SUPPORT
  12198. case PT_DRV_DBG_TTHE_TUNER_EXIT: /* 107 */
  12199. cd->tthe_exit = 1;
  12200. wake_up(&cd->wait_q);
  12201. kfree(cd->tthe_buf);
  12202. cd->tthe_buf = NULL;
  12203. cd->tthe_exit = 0;
  12204. break;
  12205. case PT_DRV_DBG_TTHE_BUF_CLEAN: /* 108 */
  12206. if (cd->tthe_buf)
  12207. memset(cd->tthe_buf, 0, PT_MAX_PRBUF_SIZE);
  12208. else
  12209. pt_debug(dev, DL_INFO, "%s : tthe_buf not existed\n",
  12210. __func__);
  12211. break;
  12212. #endif
  12213. #ifdef TTDL_DIAGNOSTICS
  12214. case PT_DUT_DBG_HID_DESC: /* 109 */
  12215. pt_debug(dev, DL_INFO, "%s: Cmd: get_hid_desc\n", __func__);
  12216. pt_get_hid_descriptor(cd, &cd->hid_desc);
  12217. break;
  12218. case PT_DRV_DBG_CLEAR_PARM_LIST: /* 110 */
  12219. pt_debug(dev, DL_INFO, "%s: TTDL: Clear Parameter List\n",
  12220. __func__);
  12221. pt_erase_parameter_list(cd);
  12222. break;
  12223. case PT_DRV_DBG_FORCE_BUS_READ: /* 111 */
  12224. rc = pt_read_input(cd);
  12225. if (!rc)
  12226. pt_parse_input(cd);
  12227. break;
  12228. case PT_DRV_DBG_CLEAR_CAL_DATA: /* 112 */
  12229. rc = _pt_manage_local_cal_data(dev, PT_CAL_DATA_CLEAR,
  12230. &cal_size, &crc);
  12231. if (rc)
  12232. pt_debug(dev, DL_ERROR,
  12233. "%s: CAL Data clear failed rc=%d\n",
  12234. __func__, rc);
  12235. else
  12236. pt_debug(dev, DL_INFO,
  12237. "%s: CAL Cleared, Chip ID=0x%04X size=%d\n",
  12238. __func__, crc, size);
  12239. break;
  12240. case PT_DRV_DBG_REPORT_LEVEL: /* 200 */
  12241. mutex_lock(&cd->system_lock);
  12242. if (input_data[1] >= 0 && input_data[1] < DL_MAX) {
  12243. cd->debug_level = input_data[1];
  12244. pt_debug(dev, DL_INFO, "%s: Set debug_level: %d\n",
  12245. __func__, cd->debug_level);
  12246. } else {
  12247. rc = -EINVAL;
  12248. pt_debug(dev, DL_ERROR, "%s: Invalid debug_level: %d\n",
  12249. __func__, input_data[1]);
  12250. }
  12251. mutex_unlock(&(cd->system_lock));
  12252. break;
  12253. #endif
  12254. case PT_DRV_DBG_WATCHDOG_INTERVAL: /* 201 */
  12255. mutex_lock(&cd->system_lock);
  12256. if (input_data[1] > 100) {
  12257. cd->watchdog_interval = input_data[1];
  12258. pt_debug(dev, DL_INFO,
  12259. "%s: Set watchdog_ interval to: %d\n",
  12260. __func__, cd->watchdog_interval);
  12261. pt_start_wd_timer(cd);
  12262. } else {
  12263. rc = -EINVAL;
  12264. pt_debug(dev, DL_ERROR,
  12265. "%s: Invalid watchdog interval: %d\n",
  12266. __func__, input_data[1]);
  12267. }
  12268. mutex_unlock(&(cd->system_lock));
  12269. break;
  12270. #ifdef TTDL_DIAGNOSTICS
  12271. case PT_DRV_DBG_SHOW_TIMESTAMP: /* 202 */
  12272. mutex_lock(&cd->system_lock);
  12273. if (input_data[1] == 0) {
  12274. cd->show_timestamp = 0;
  12275. pt_debug(dev, DL_INFO, "%s: Disable show_timestamp\n",
  12276. __func__);
  12277. } else if (input_data[1] == 1) {
  12278. cd->show_timestamp = 1;
  12279. pt_debug(dev, DL_INFO, "%s: Enable show_timestamp\n",
  12280. __func__);
  12281. } else {
  12282. rc = -EINVAL;
  12283. pt_debug(dev, DL_ERROR,
  12284. "%s: Invalid parameter: %d\n",
  12285. __func__, input_data[1]);
  12286. }
  12287. mutex_unlock(&(cd->system_lock));
  12288. break;
  12289. case PT_DRV_DBG_SETUP_PWR: /* 205 */
  12290. if (input_data[1] == 0) {
  12291. cd->cpdata->setup_power(cd->cpdata,
  12292. PT_MT_POWER_OFF, cd->dev);
  12293. pt_debug(dev, DL_INFO,
  12294. "%s: Initiate Power Off\n", __func__);
  12295. } else if (input_data[1] == 1) {
  12296. cd->cpdata->setup_power(cd->cpdata,
  12297. PT_MT_POWER_ON, cd->dev);
  12298. pt_debug(dev, DL_INFO,
  12299. "%s: Initiate Power On\n", __func__);
  12300. } else {
  12301. rc = -EINVAL;
  12302. pt_debug(dev, DL_ERROR,
  12303. "%s: Invalid parameter: %d\n",
  12304. __func__, input_data[1]);
  12305. }
  12306. break;
  12307. case PT_DRV_DBG_GET_PUT_SYNC: /* 206 */
  12308. if (input_data[1] == 0) {
  12309. pm_runtime_put(dev);
  12310. pt_debug(dev, DL_ERROR,
  12311. "%s: Force call pm_runtime_put()\n", __func__);
  12312. } else if (input_data[1] == 1) {
  12313. pm_runtime_get_sync(dev);
  12314. pt_debug(dev, DL_ERROR,
  12315. "%s: Force call pm_runtime_get_sync()\n",
  12316. __func__);
  12317. } else {
  12318. rc = -EINVAL;
  12319. pt_debug(dev, DL_ERROR,
  12320. "%s: WARNING: Invalid parameter: %d\n",
  12321. __func__, input_data[1]);
  12322. }
  12323. break;
  12324. case PT_DRV_DBG_SET_TT_DATA: /* 208 */
  12325. mutex_lock(&cd->system_lock);
  12326. if (input_data[1] == 0) {
  12327. cd->show_tt_data = false;
  12328. pt_debug(dev, DL_INFO,
  12329. "%s: Disable TT_DATA\n", __func__);
  12330. } else if (input_data[1] == 1) {
  12331. cd->show_tt_data = true;
  12332. pt_debug(dev, DL_INFO,
  12333. "%s: Enable TT_DATA\n", __func__);
  12334. } else {
  12335. rc = -EINVAL;
  12336. pt_debug(dev, DL_ERROR,
  12337. "%s: Invalid parameter: %d\n",
  12338. __func__, input_data[1]);
  12339. }
  12340. mutex_unlock(&(cd->system_lock));
  12341. break;
  12342. case PT_DRV_DBG_SET_GENERATION: /* 210 */
  12343. if (input_data[1] == cd->active_dut_generation) {
  12344. mutex_lock(&cd->system_lock);
  12345. cd->set_dut_generation = true;
  12346. mutex_unlock(&(cd->system_lock));
  12347. } else {
  12348. mutex_lock(&cd->system_lock);
  12349. if (input_data[1] == 0) {
  12350. cd->active_dut_generation = DUT_UNKNOWN;
  12351. cd->set_dut_generation = false;
  12352. } else if (input_data[1] == 1) {
  12353. cd->active_dut_generation = DUT_PIP1_ONLY;
  12354. cd->set_dut_generation = true;
  12355. } else if (input_data[1] == 2) {
  12356. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  12357. cd->set_dut_generation = true;
  12358. } else {
  12359. rc = -EINVAL;
  12360. pt_debug(dev, DL_ERROR,
  12361. "%s: Invalid parameter: %d\n",
  12362. __func__, input_data[1]);
  12363. break;
  12364. }
  12365. cd->startup_status = STARTUP_STATUS_START;
  12366. pt_debug(cd->dev, DL_DEBUG,
  12367. "%s: Startup Status Reset\n", __func__);
  12368. mutex_unlock(&(cd->system_lock));
  12369. pt_debug(dev, DL_INFO,
  12370. "%s: Active DUT Generation Set to: %d\n",
  12371. __func__, cd->active_dut_generation);
  12372. /* Changing DUT generations full restart needed */
  12373. pt_queue_restart(cd);
  12374. }
  12375. break;
  12376. case PT_DRV_DBG_SET_BRIDGE_MODE: /* 211 */
  12377. mutex_lock(&cd->system_lock);
  12378. if (input_data[1] == 0) {
  12379. cd->bridge_mode = false;
  12380. pt_debug(dev, DL_INFO,
  12381. "%s: Disable Bridge Mode\n", __func__);
  12382. } else if (input_data[1] == 1) {
  12383. cd->bridge_mode = true;
  12384. pt_debug(dev, DL_INFO,
  12385. "%s: Enable Bridge Mode\n", __func__);
  12386. } else {
  12387. rc = -EINVAL;
  12388. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12389. __func__, input_data[1]);
  12390. }
  12391. mutex_unlock(&(cd->system_lock));
  12392. break;
  12393. case PT_DRV_DBG_SET_I2C_ADDRESS: /* 212 */
  12394. mutex_lock(&cd->system_lock);
  12395. /* Only a 7bit address is valid */
  12396. if (input_data[1] >= 0 && input_data[1] <= 0x7F) {
  12397. client->addr = input_data[1];
  12398. pt_debug(dev, DL_INFO,
  12399. "%s: Set I2C Address: 0x%2X\n",
  12400. __func__, client->addr);
  12401. } else {
  12402. rc = -EINVAL;
  12403. pt_debug(dev, DL_ERROR, "%s: Invalid I2C Address %d\n",
  12404. __func__, input_data[1]);
  12405. client->addr = 0x24;
  12406. }
  12407. mutex_unlock(&(cd->system_lock));
  12408. break;
  12409. case PT_DRV_DBG_SET_FLASHLESS_DUT: /* 213 */
  12410. mutex_lock(&cd->system_lock);
  12411. if (input_data[1] == 0) {
  12412. cd->flashless_dut = 0;
  12413. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  12414. pt_debug(dev, DL_INFO, "%s: Disable FLAHLESS DUT\n",
  12415. __func__);
  12416. } else if (input_data[1] == 1) {
  12417. cd->flashless_dut = 1;
  12418. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  12419. pt_debug(dev, DL_INFO, "%s: Enable FLAHLESS DUT\n",
  12420. __func__);
  12421. }
  12422. mutex_unlock(&(cd->system_lock));
  12423. break;
  12424. case PT_DRV_DBG_SET_FORCE_SEQ: /* 214 */
  12425. mutex_lock(&cd->system_lock);
  12426. if (input_data[1] >= 0x8 && input_data[1] <= 0xF) {
  12427. cd->force_pip2_seq = input_data[1];
  12428. cd->pip2_cmd_tag_seq = input_data[1];
  12429. pt_debug(dev, DL_INFO,
  12430. "%s: Force PIP2 Seq to: 0x%02X\n",
  12431. __func__, input_data[1]);
  12432. } else {
  12433. cd->force_pip2_seq = 0;
  12434. pt_debug(dev, DL_INFO,
  12435. "%s: Clear Forced PIP2 Seq\n", __func__);
  12436. }
  12437. mutex_unlock(&(cd->system_lock));
  12438. break;
  12439. case PT_DRV_DBG_BL_WITH_NO_INT: /* 215 */
  12440. mutex_lock(&cd->system_lock);
  12441. if (input_data[1] == 0) {
  12442. cd->bl_with_no_int = 0;
  12443. pt_debug(dev, DL_INFO, "%s: BL using IRQ\n", __func__);
  12444. } else if (input_data[1] == 1) {
  12445. cd->bl_with_no_int = 1;
  12446. pt_debug(dev, DL_INFO, "%s: BL using Polling\n",
  12447. __func__);
  12448. } else {
  12449. rc = -EINVAL;
  12450. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12451. __func__, input_data[1]);
  12452. }
  12453. mutex_unlock(&(cd->system_lock));
  12454. break;
  12455. case PT_DRV_DBG_CAL_CACHE_IN_HOST: /* 216 */
  12456. mutex_lock(&cd->system_lock);
  12457. if (input_data[1] == 0) {
  12458. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  12459. pt_debug(dev, DL_INFO,
  12460. "%s: Disable Calibration cache in host\n",
  12461. __func__);
  12462. } else if (input_data[1] == 1) {
  12463. cd->cal_cache_in_host = PT_FEATURE_ENABLE;
  12464. pt_debug(dev, DL_INFO,
  12465. "%s: Enable Calibration cache in host\n",
  12466. __func__);
  12467. } else {
  12468. rc = -EINVAL;
  12469. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12470. __func__, input_data[1]);
  12471. }
  12472. mutex_unlock(&(cd->system_lock));
  12473. break;
  12474. case PT_DRV_DBG_MULTI_CHIP: /* 217 */
  12475. mutex_lock(&cd->system_lock);
  12476. if (input_data[1] == 0) {
  12477. cd->multi_chip = PT_FEATURE_DISABLE;
  12478. cd->ttdl_bist_select = 0x07;
  12479. pt_debug(dev, DL_INFO,
  12480. "%s: Disable Multi-chip support\n", __func__);
  12481. } else if (input_data[1] == 1) {
  12482. cd->multi_chip = PT_FEATURE_ENABLE;
  12483. cd->ttdl_bist_select = 0x3F;
  12484. pt_debug(dev, DL_INFO,
  12485. "%s: Enable Multi-chip support\n", __func__);
  12486. } else {
  12487. rc = -EINVAL;
  12488. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12489. __func__, input_data[1]);
  12490. }
  12491. mutex_unlock(&(cd->system_lock));
  12492. break;
  12493. case PT_DRV_DBG_SET_PANEL_ID_TYPE: /* 218 */
  12494. mutex_lock(&cd->system_lock);
  12495. if (input_data[1] <= 0x07) {
  12496. cd->panel_id_support = input_data[1];
  12497. pt_debug(dev, DL_INFO,
  12498. "%s: Set panel_id_support to %d\n",
  12499. __func__, cd->panel_id_support);
  12500. } else {
  12501. rc = -EINVAL;
  12502. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12503. __func__, input_data[1]);
  12504. }
  12505. mutex_unlock(&(cd->system_lock));
  12506. break;
  12507. case PT_DRV_DBG_PIP_TIMEOUT: /* 219 */
  12508. mutex_lock(&cd->system_lock);
  12509. if (input_data[1] >= 100 && input_data[1] <= 7000) {
  12510. /*
  12511. * The timeout is changed for some cases so the
  12512. * pip_cmd_timeout_default is used to retore back to
  12513. * what the user requested as the new timeout.
  12514. */
  12515. cd->pip_cmd_timeout_default = input_data[1];
  12516. cd->pip_cmd_timeout = input_data[1];
  12517. pt_debug(dev, DL_INFO,
  12518. "%s: PIP Timeout = %d\n", __func__,
  12519. cd->pip_cmd_timeout_default);
  12520. } else {
  12521. rc = -EINVAL;
  12522. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12523. __func__, input_data[1]);
  12524. }
  12525. mutex_unlock(&(cd->system_lock));
  12526. break;
  12527. case PT_DRV_DBG_TTHE_HID_USB_FORMAT: /* 220 */
  12528. mutex_lock(&cd->system_lock);
  12529. if (input_data[1] == 0) {
  12530. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  12531. pt_debug(dev, DL_INFO,
  12532. "%s: Disable tthe_tuner HID-USB format\n",
  12533. __func__);
  12534. } else if (input_data[1] == 1) {
  12535. cd->tthe_hid_usb_format = PT_FEATURE_ENABLE;
  12536. pt_debug(dev, DL_INFO,
  12537. "%s: Enable tthe_tuner HID-USB format\n",
  12538. __func__);
  12539. } else {
  12540. rc = -EINVAL;
  12541. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12542. __func__, input_data[1]);
  12543. }
  12544. mutex_unlock(&(cd->system_lock));
  12545. break;
  12546. #endif /* TTDL_DIAGNOSTICS */
  12547. default:
  12548. rc = -EINVAL;
  12549. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  12550. }
  12551. /* Enable watchdog timer */
  12552. if (!wd_disabled)
  12553. pt_start_wd_timer(cd);
  12554. pt_drv_debug_store_exit:
  12555. if (rc)
  12556. return rc;
  12557. return size;
  12558. }
  12559. static DEVICE_ATTR(drv_debug, 0644, pt_drv_debug_show,
  12560. pt_drv_debug_store);
  12561. /*******************************************************************************
  12562. * FUNCTION: pt_sleep_status_show
  12563. *
  12564. * SUMMARY: Show method for the sleep_status sysfs node that will show the
  12565. * sleep status as either ON or OFF
  12566. *
  12567. * RETURN:
  12568. * 0 = success
  12569. * !0 = failure
  12570. *
  12571. * PARAMETERS:
  12572. * *dev - pointer to device structure
  12573. * *attr - pointer to device attributes
  12574. * *buf - pointer to output buffer
  12575. ******************************************************************************/
  12576. static ssize_t pt_sleep_status_show(struct device *dev,
  12577. struct device_attribute *attr, char *buf)
  12578. {
  12579. struct pt_core_data *cd = dev_get_drvdata(dev);
  12580. ssize_t ret;
  12581. mutex_lock(&cd->system_lock);
  12582. if (cd->sleep_state == SS_SLEEP_ON)
  12583. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "off\n");
  12584. else
  12585. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "on\n");
  12586. mutex_unlock(&cd->system_lock);
  12587. return ret;
  12588. }
  12589. /*******************************************************************************
  12590. * FUNCTION: pt_panel_id_show
  12591. *
  12592. * SUMMARY: Show method for the panel_id sysfs node that will show the
  12593. * detected panel ID from the DUT
  12594. *
  12595. * RETURN:
  12596. * 0 = success
  12597. * !0 = failure
  12598. *
  12599. * PARAMETERS:
  12600. * *dev - pointer to device structure
  12601. * *attr - pointer to device attributes
  12602. * *buf - pointer to output buffer
  12603. ******************************************************************************/
  12604. static ssize_t pt_panel_id_show(struct device *dev,
  12605. struct device_attribute *attr, char *buf)
  12606. {
  12607. struct pt_core_data *cd = dev_get_drvdata(dev);
  12608. ssize_t ret;
  12609. u8 pid = PANEL_ID_NOT_ENABLED;
  12610. int rc = 0;
  12611. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  12612. /*
  12613. * The DUT should report the same panel ID from both the BL and
  12614. * the FW unless the panel_id feature is set to only
  12615. * PT_PANEL_ID_BY_SYS_INFO, in which case the BL is not able
  12616. * to retrieve the panel_id.
  12617. */
  12618. if (cd->mode == PT_MODE_BOOTLOADER) {
  12619. /*
  12620. * Return the stored value if PT_PANEL_ID_BY_BL
  12621. * is not supported while other feature exits.
  12622. */
  12623. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  12624. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  12625. if (rc) {
  12626. pt_debug(dev, DL_ERROR, "%s: %s %s\n",
  12627. "Failed to retrieve Panel ID. ",
  12628. "Using cached value\n",
  12629. __func__);
  12630. }
  12631. }
  12632. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  12633. if (cd->panel_id_support &
  12634. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  12635. /* For all systems sysinfo has the panel_id */
  12636. rc = pt_hid_output_get_sysinfo(cd);
  12637. if (!rc)
  12638. pid =
  12639. cd->sysinfo.sensing_conf_data.panel_id;
  12640. pt_debug(dev, DL_ERROR,
  12641. "%s: Gen6 FW mode rc=%d PID=0x%02X\n",
  12642. __func__, rc, pid);
  12643. }
  12644. } else {
  12645. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  12646. __func__);
  12647. rc = -EPERM;
  12648. }
  12649. } else if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  12650. if (cd->mode == PT_MODE_BOOTLOADER) {
  12651. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  12652. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  12653. if (rc) {
  12654. pt_debug(dev, DL_ERROR,
  12655. "%s: BL get panel ID failed rc=%d\n",
  12656. __func__, rc);
  12657. }
  12658. }
  12659. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  12660. if (cd->panel_id_support &
  12661. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  12662. rc = pt_hid_output_get_sysinfo(cd);
  12663. if (!rc)
  12664. pid =
  12665. cd->sysinfo.sensing_conf_data.panel_id;
  12666. pt_debug(dev, DL_ERROR,
  12667. "%s: TT/TC FW mode rc=%d PID=0x%02X\n",
  12668. __func__, rc, pid);
  12669. }
  12670. } else {
  12671. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  12672. __func__);
  12673. rc = -EPERM;
  12674. }
  12675. } else {
  12676. pt_debug(dev, DL_ERROR, "%s: Dut generation is unknown\n",
  12677. __func__);
  12678. rc = -EPERM;
  12679. }
  12680. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n0x%02X\n",
  12681. rc, pid);
  12682. return ret;
  12683. }
  12684. /*******************************************************************************
  12685. * FUNCTION: pt_get_param_store
  12686. *
  12687. * SUMMARY: Store method for the get_param sysfs node. Stores what parameter
  12688. * ID to retrieve with the show method.
  12689. *
  12690. * NOTE: This sysfs node is only available after a DUT has been enumerated
  12691. *
  12692. * RETURN: Size of passed in buffer if successful
  12693. *
  12694. * PARAMETERS:
  12695. * *dev - pointer to device structure
  12696. * *attr - pointer to device attributes
  12697. * *buf - pointer to output buffer
  12698. * size - size of buf
  12699. ******************************************************************************/
  12700. static ssize_t pt_get_param_store(struct device *dev,
  12701. struct device_attribute *attr, const char *buf, size_t size)
  12702. {
  12703. struct pt_core_data *cd = dev_get_drvdata(dev);
  12704. u32 input_data[2];
  12705. int length;
  12706. int rc = 0;
  12707. /* Maximum input of one value */
  12708. length = _pt_ic_parse_input(dev, buf, size, input_data,
  12709. ARRAY_SIZE(input_data));
  12710. if (length != 1) {
  12711. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12712. __func__);
  12713. rc = -EINVAL;
  12714. goto exit;
  12715. }
  12716. mutex_lock(&cd->system_lock);
  12717. cd->get_param_id = input_data[0];
  12718. mutex_unlock(&(cd->system_lock));
  12719. exit:
  12720. if (rc)
  12721. return rc;
  12722. return size;
  12723. }
  12724. /*******************************************************************************
  12725. * FUNCTION: pt_get_param_show
  12726. *
  12727. * SUMMARY: Show method for the get_param sysfs node. Retrieves the
  12728. * parameter data from the DUT based on the ID stored in the core
  12729. * data variable "get_param_id". If the ID is invalid, the DUT cannot
  12730. * communicate or some other error occures, an error status is returned
  12731. * with no value following.
  12732. * Output is in the form:
  12733. * Status: x
  12734. * 0xyyyyyyyy
  12735. * The 32bit data will only follow the status code if the status == 0
  12736. *
  12737. * NOTE: This sysfs node is only available after a DUT has been enumerated
  12738. *
  12739. * RETURN:
  12740. * 0 = success
  12741. * !0 = failure
  12742. *
  12743. * PARAMETERS:
  12744. * *dev - pointer to device structure
  12745. * *attr - pointer to device attributes
  12746. * *buf - pointer to output buffer
  12747. ******************************************************************************/
  12748. static ssize_t pt_get_param_show(struct device *dev,
  12749. struct device_attribute *attr, char *buf)
  12750. {
  12751. struct pt_core_data *cd = dev_get_drvdata(dev);
  12752. ssize_t ret = 0;
  12753. int status;
  12754. u32 value = 0;
  12755. status = pt_pip_get_param(cd, cd->get_param_id, &value);
  12756. if (status) {
  12757. pt_debug(dev, DL_ERROR, "%s: %s Failed, status = %d\n",
  12758. __func__, "pt_get_param", status);
  12759. ret = scnprintf(buf, strlen(buf),
  12760. "%s %d\n",
  12761. "Status:", status);
  12762. } else {
  12763. pt_debug(dev, DL_DEBUG, "%s: Param [%d] = 0x%04X\n",
  12764. __func__, cd->get_param_id, value);
  12765. ret = scnprintf(buf, strlen(buf),
  12766. "Status: %d\n"
  12767. "0x%04X\n",
  12768. status, value);
  12769. }
  12770. return ret;
  12771. }
  12772. /*******************************************************************************
  12773. * FUNCTION: pt_ttdl_restart_show
  12774. *
  12775. * SUMMARY: Show method for ttdl_restart sysfs node. This node releases all
  12776. * probed modules, calls startup() and then re-probes modules.
  12777. *
  12778. * RETURN: size of data written to sysfs node
  12779. *
  12780. * PARAMETERS:
  12781. * *dev - pointer to device structure
  12782. * *attr - pointer to device attributes structure
  12783. * *buf - pointer to print output buffer
  12784. ******************************************************************************/
  12785. static ssize_t pt_ttdl_restart_show(struct device *dev,
  12786. struct device_attribute *attr, char *buf)
  12787. {
  12788. struct pt_core_data *cd = dev_get_drvdata(dev);
  12789. int t;
  12790. int rc = 0;
  12791. mutex_lock(&cd->system_lock);
  12792. cd->startup_state = STARTUP_NONE;
  12793. mutex_unlock(&(cd->system_lock));
  12794. /* ensure no left over exclusive access is still locked */
  12795. release_exclusive(cd, cd->dev);
  12796. pt_queue_restart(cd);
  12797. t = wait_event_timeout(cd->wait_q,
  12798. (cd->startup_status >= STARTUP_STATUS_COMPLETE),
  12799. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  12800. if (IS_TMO(t)) {
  12801. pt_debug(cd->dev, DL_ERROR,
  12802. "%s: TMO waiting for FW sentinel\n", __func__);
  12803. rc = -ETIME;
  12804. }
  12805. return scnprintf(buf, strlen(buf),
  12806. "Status: %d\n"
  12807. "Enum Status: 0x%04X\n", rc, cd->startup_status);
  12808. }
  12809. static DEVICE_ATTR(ttdl_restart, 0444, pt_ttdl_restart_show, NULL);
  12810. /*******************************************************************************
  12811. * FUNCTION: pt_pip2_gpio_read_show
  12812. *
  12813. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and prints the
  12814. * contents of the response to the passed in output buffer.
  12815. *
  12816. * RETURN: size of data written to sysfs node
  12817. *
  12818. * PARAMETERS:
  12819. * *dev - pointer to device structure
  12820. * *attr - pointer to device attributes structure
  12821. * *buf - pointer to print output buffer
  12822. ******************************************************************************/
  12823. static ssize_t pt_pip2_gpio_read_show(struct device *dev,
  12824. struct device_attribute *attr, char *buf)
  12825. {
  12826. struct pt_core_data *cd = dev_get_drvdata(dev);
  12827. u8 status = 0;
  12828. u32 gpio_value = 0;
  12829. int rc = 0;
  12830. /* This functionality is only available in the BL */
  12831. if (cd->mode == PT_MODE_BOOTLOADER)
  12832. rc = pt_pip2_read_gpio(dev, &status, &gpio_value);
  12833. else
  12834. rc = -EPERM;
  12835. if (!rc) {
  12836. if (status == 0)
  12837. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12838. "Status: %d\n"
  12839. "DUT GPIO Reg: 0x%08X\n",
  12840. rc, gpio_value);
  12841. else
  12842. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12843. "Status: %d\n"
  12844. "DUT GPIO Reg: n/a\n",
  12845. status);
  12846. } else
  12847. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12848. "Status: %d\n"
  12849. "DUT GPIO Reg: n/a\n",
  12850. rc);
  12851. }
  12852. /*******************************************************************************
  12853. * FUNCTION: pt_pip2_version_show
  12854. *
  12855. * SUMMARY: Sends a PIP2 VERSION command to the DUT and prints the
  12856. * contents of the response to the passed in output buffer.
  12857. *
  12858. * RETURN: size of data written to sysfs node
  12859. *
  12860. * PARAMETERS:
  12861. * *dev - pointer to device structure
  12862. * *attr - pointer to device attributes structure
  12863. * *buf - pointer to print output buffer
  12864. ******************************************************************************/
  12865. static ssize_t pt_pip2_version_show(struct device *dev,
  12866. struct device_attribute *attr, char *buf)
  12867. {
  12868. int rc = 0;
  12869. struct pt_core_data *cd = dev_get_drvdata(dev);
  12870. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  12871. rc = pt_pip2_get_version(cd);
  12872. if (!rc) {
  12873. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12874. "PIP VERSION : %02X.%02X\n"
  12875. "BL VERSION : %02X.%02X\n"
  12876. "FW VERSION : %02X.%02X\n"
  12877. "SILICON ID : %04X.%04X\n"
  12878. "UID : 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
  12879. ttdata->pip_ver_major, ttdata->pip_ver_minor,
  12880. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  12881. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  12882. ttdata->chip_id, ttdata->chip_rev,
  12883. ttdata->uid[0], ttdata->uid[1],
  12884. ttdata->uid[2], ttdata->uid[3],
  12885. ttdata->uid[4], ttdata->uid[5],
  12886. ttdata->uid[6], ttdata->uid[7],
  12887. ttdata->uid[8], ttdata->uid[9],
  12888. ttdata->uid[10], ttdata->uid[11]);
  12889. } else {
  12890. pt_debug(dev, DL_ERROR,
  12891. "%s: Failed to retriev PIP2 VERSION data\n", __func__);
  12892. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12893. "PIP VERSION : -\n"
  12894. "BL VERSION : -\n"
  12895. "FW VERSION : -\n"
  12896. "SILICON ID : -\n"
  12897. "UID : -\n");
  12898. }
  12899. }
  12900. #ifdef TTDL_DIAGNOSTICS
  12901. /*******************************************************************************
  12902. * FUNCTION: pt_ttdl_status_show
  12903. *
  12904. * SUMMARY: Show method for the ttdl_status sysfs node. Displays TTDL internal
  12905. * variable states and GPIO levels. Additional information printed when
  12906. * TTDL_DIAGNOSTICS is enabled.
  12907. *
  12908. * NOTE: All counters will be reset to 0 when this function is called.
  12909. *
  12910. * RETURN:
  12911. * 0 = success
  12912. * !0 = failure
  12913. *
  12914. * PARAMETERS:
  12915. * *dev - pointer to device structure
  12916. * *attr - pointer to device attributes
  12917. * *buf - pointer to output buffer
  12918. ******************************************************************************/
  12919. static ssize_t pt_ttdl_status_show(struct device *dev,
  12920. struct device_attribute *attr, char *buf)
  12921. {
  12922. struct pt_core_data *cd = dev_get_drvdata(dev);
  12923. struct pt_platform_data *pdata = dev_get_platdata(dev);
  12924. struct i2c_client *client = to_i2c_client(dev);
  12925. ssize_t ret;
  12926. u16 cal_size = 0;
  12927. unsigned short crc = 0;
  12928. if (cd->cal_cache_in_host)
  12929. _pt_manage_local_cal_data(dev,
  12930. PT_CAL_DATA_INFO, &cal_size, &crc);
  12931. ret = scnprintf(buf, strlen(buf),
  12932. "%s: 0x%04X\n"
  12933. "%s: %d\n"
  12934. "%s: %s\n"
  12935. "%s: %s %s\n"
  12936. "%s: %s\n"
  12937. "%s: 0x%02X\n"
  12938. "%s: %s\n"
  12939. "%s: %s\n"
  12940. "%s: %s\n"
  12941. "%s: %s\n"
  12942. "%s: %d\n"
  12943. "%s: %d\n"
  12944. "%s: %s\n"
  12945. "%s: %s\n"
  12946. "%s: %d\n"
  12947. #ifdef TTDL_DIAGNOSTICS
  12948. "%s: %d\n"
  12949. "%s: %d\n"
  12950. "%s: %d\n"
  12951. "%s: %d\n"
  12952. "%s: %d\n"
  12953. "%s: %d\n"
  12954. "%s: %d\n"
  12955. "%s: %d\n"
  12956. "%s: %d\n"
  12957. "%s: %d\n"
  12958. "%s: %s\n"
  12959. "%s: %s\n"
  12960. "%s: %s\n"
  12961. "%s: %d\n"
  12962. "%s: 0x%04X\n"
  12963. "%s: %s\n"
  12964. #endif /* TTDL_DIAGNOSTICS */
  12965. ,
  12966. "Startup Status ", cd->startup_status,
  12967. "TTDL Debug Level ", cd->debug_level,
  12968. "Mode ",
  12969. cd->mode ? (cd->mode == PT_MODE_OPERATIONAL ?
  12970. "Operational" : "BL") : "Unknown",
  12971. "DUT Generation ",
  12972. cd->active_dut_generation ?
  12973. (cd->active_dut_generation == DUT_PIP2_CAPABLE ?
  12974. "PT TC/TT" : "Gen5/6") : "Unknown",
  12975. cd->active_dut_generation ?
  12976. (cd->set_dut_generation == true ?
  12977. "(Set)" : "(Detected)") : "",
  12978. "HW Detected ",
  12979. cd->hw_detected ? "True" : "False",
  12980. "I2C Address ",
  12981. cd->bus_ops->bustype == BUS_I2C ? client->addr : 0,
  12982. "Active Bus Module ",
  12983. cd->bus_ops->bustype == BUS_I2C ? "I2C" : "SPI",
  12984. "Flashless Mode ",
  12985. cd->flashless_dut == 1 ? "Yes" : "No",
  12986. "GPIO state - IRQ ",
  12987. cd->cpdata->irq_stat ?
  12988. (cd->cpdata->irq_stat(cd->cpdata, dev) ?
  12989. "High" : "Low") : "not defined",
  12990. "GPIO state - TP_XRES ",
  12991. pdata->core_pdata->rst_gpio ?
  12992. (gpio_get_value(pdata->core_pdata->rst_gpio) ?
  12993. "High" : "Low") : "not defined",
  12994. "RAM Parm restore list ", pt_count_parameter_list(cd),
  12995. "Startup Retry Count ", cd->startup_retry_count,
  12996. "WD - Manual Force Stop ",
  12997. cd->watchdog_force_stop ? "True" : "False",
  12998. "WD - Enabled ",
  12999. cd->watchdog_enabled ? "True" : "False",
  13000. "WD - Interval (ms) ", cd->watchdog_interval
  13001. #ifdef TTDL_DIAGNOSTICS
  13002. , "WD - Triggered Count ", cd->watchdog_count,
  13003. "WD - IRQ Stuck low count ", cd->watchdog_irq_stuck_count,
  13004. "WD - Device Access Errors ", cd->watchdog_failed_access_count,
  13005. "WD - XRES Count ", cd->wd_xres_count,
  13006. "IRQ Triggered Count ", cd->irq_count,
  13007. "BL Packet Retry Count ", cd->bl_retry_packet_count,
  13008. "PIP2 CRC Error Count ", cd->pip2_crc_error_count,
  13009. "Bus Transmit Error Count ", cd->bus_transmit_error_count,
  13010. "File Erase Timeout Count ", cd->file_erase_timeout_count,
  13011. "Error GPIO trigger type ", cd->err_gpio_type,
  13012. "Exclusive Access Lock ", cd->exclusive_dev ? "Set":"Free",
  13013. "Suppress No-Flash Auto BL ",
  13014. cd->flashless_auto_bl == PT_SUPPRESS_AUTO_BL ?
  13015. "Yes" : "No",
  13016. "Calibration Cache on host ",
  13017. cd->cal_cache_in_host == PT_FEATURE_ENABLE ?
  13018. "Yes" : "No",
  13019. "Calibration Cache size ", cal_size,
  13020. "Calibration Cache chip ID ", crc,
  13021. "Multi-Chip Support ",
  13022. cd->multi_chip == PT_FEATURE_ENABLE ? "Yes" : "No"
  13023. #endif /* TTDL_DIAGNOSTICS */
  13024. );
  13025. #ifdef TTDL_DIAGNOSTICS
  13026. /* Reset all diagnostic counters */
  13027. cd->watchdog_count = 0;
  13028. cd->watchdog_irq_stuck_count = 0;
  13029. cd->watchdog_failed_access_count = 0;
  13030. cd->wd_xres_count = 0;
  13031. cd->irq_count = 0;
  13032. cd->bl_retry_packet_count = 0;
  13033. cd->pip2_crc_error_count = 0;
  13034. cd->bus_transmit_error_count = 0;
  13035. #endif
  13036. return ret;
  13037. }
  13038. static DEVICE_ATTR(ttdl_status, 0444, pt_ttdl_status_show, NULL);
  13039. /*******************************************************************************
  13040. * FUNCTION: pt_pip2_enter_bl_show
  13041. *
  13042. * SUMMARY: Show method for the pip2_enter_bl sysfs node that will force
  13043. * the DUT into the BL and show the success or failure of entering the BL
  13044. *
  13045. * RETURN:
  13046. * 0 = success
  13047. * !0 = failure
  13048. *
  13049. * PARAMETERS:
  13050. * *dev - pointer to device structure
  13051. * *attr - pointer to device attributes
  13052. * *buf - pointer to output buffer
  13053. ******************************************************************************/
  13054. static ssize_t pt_pip2_enter_bl_show(struct device *dev,
  13055. struct device_attribute *attr, char *buf)
  13056. {
  13057. ssize_t ret = 0;
  13058. int rc = 0;
  13059. int result = 0;
  13060. u8 mode = PT_MODE_UNKNOWN;
  13061. struct pt_core_data *cd = dev_get_drvdata(dev);
  13062. /* Turn off the TTDL WD before enter bootloader */
  13063. pt_stop_wd_timer(cd);
  13064. /* Ensure NO enumeration work is queued or will be queued */
  13065. cancel_work_sync(&cd->enum_work);
  13066. mutex_lock(&cd->system_lock);
  13067. cd->bridge_mode = true;
  13068. mutex_unlock(&cd->system_lock);
  13069. /* set mode to operational to avoid any extra PIP traffic */
  13070. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  13071. switch (result) {
  13072. case PT_ENTER_BL_PASS:
  13073. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13074. "Status: %d\nEntered BL\n", PT_ENTER_BL_PASS);
  13075. break;
  13076. case PT_ENTER_BL_ERROR:
  13077. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13078. rc,
  13079. " Unknown Error");
  13080. break;
  13081. case PT_ENTER_BL_RESET_FAIL:
  13082. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13083. rc,
  13084. " Soft Reset Failed");
  13085. break;
  13086. case PT_ENTER_BL_HID_START_BL_FAIL:
  13087. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13088. rc,
  13089. " PIP Start BL Cmd Failed");
  13090. break;
  13091. case PT_ENTER_BL_CONFIRM_FAIL:
  13092. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13093. rc,
  13094. " Error confirming DUT entered BL");
  13095. break;
  13096. default:
  13097. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13098. rc, " Unknown Error");
  13099. break;
  13100. };
  13101. /* Allow enumeration work to be queued again */
  13102. cd->bridge_mode = false;
  13103. return ret;
  13104. }
  13105. static DEVICE_ATTR(pip2_enter_bl, 0444, pt_pip2_enter_bl_show, NULL);
  13106. /*******************************************************************************
  13107. * FUNCTION: pt_pip2_exit_bl_show
  13108. *
  13109. * SUMMARY: Show method for the pip2_exit_bl sysfs node that will attempt to
  13110. * launch the APP and put the DUT Application mode
  13111. *
  13112. * RETURN:
  13113. * 0 = success
  13114. * !0 = failure
  13115. *
  13116. * PARAMETERS:
  13117. * *dev - pointer to device structure
  13118. * *attr - pointer to device attributes
  13119. * *buf - pointer to output buffer
  13120. ******************************************************************************/
  13121. static ssize_t pt_pip2_exit_bl_show(struct device *dev,
  13122. struct device_attribute *attr, char *buf)
  13123. {
  13124. struct pt_core_data *cd = dev_get_drvdata(dev);
  13125. ssize_t ret = 0;
  13126. int rc = 0;
  13127. u8 status_str[PT_STATUS_STR_LEN];
  13128. rc = pt_pip2_exit_bl_(cd, status_str, PT_STATUS_STR_LEN);
  13129. /*
  13130. * Perform enum if startup_status doesn't reach to
  13131. * STARTUP_STATUS_FW_OUT_OF_BOOT.
  13132. */
  13133. if (!rc && (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT))) {
  13134. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  13135. if (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  13136. strlcpy(status_str,
  13137. "Already in APP mode - FW stuck in Boot mode", sizeof(status_str));
  13138. }
  13139. }
  13140. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13141. rc, status_str);
  13142. return ret;
  13143. }
  13144. static DEVICE_ATTR(pip2_exit_bl, 0444, pt_pip2_exit_bl_show, NULL);
  13145. #endif
  13146. #ifdef EASYWAKE_TSG6
  13147. /*******************************************************************************
  13148. * FUNCTION: pt_easy_wakeup_gesture_show
  13149. *
  13150. * SUMMARY: Show method for the easy_wakeup_gesture sysfs node that will show
  13151. * current easy wakeup gesture
  13152. *
  13153. * RETURN:
  13154. * 0 = success
  13155. * !0 = failure
  13156. *
  13157. * PARAMETERS:
  13158. * *dev - pointer to device structure
  13159. * *attr - pointer to device attributes
  13160. * *buf - pointer to output buffer
  13161. ******************************************************************************/
  13162. static ssize_t pt_easy_wakeup_gesture_show(struct device *dev,
  13163. struct device_attribute *attr, char *buf)
  13164. {
  13165. struct pt_core_data *cd = dev_get_drvdata(dev);
  13166. ssize_t ret;
  13167. mutex_lock(&cd->system_lock);
  13168. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "0x%02X\n",
  13169. cd->easy_wakeup_gesture);
  13170. mutex_unlock(&cd->system_lock);
  13171. return ret;
  13172. }
  13173. /*******************************************************************************
  13174. * FUNCTION: pt_easy_wakeup_gesture_store
  13175. *
  13176. * SUMMARY: The store method for the easy_wakeup_gesture sysfs node that
  13177. * allows the wake gesture to be set to a custom value.
  13178. *
  13179. * RETURN: Size of passed in buffer is success
  13180. *
  13181. * PARAMETERS:
  13182. * *dev - pointer to device structure
  13183. * *attr - pointer to device attributes
  13184. * *buf - pointer to buffer that hold the command parameters
  13185. * size - size of buf
  13186. ******************************************************************************/
  13187. static ssize_t pt_easy_wakeup_gesture_store(struct device *dev,
  13188. struct device_attribute *attr, const char *buf, size_t size)
  13189. {
  13190. struct pt_core_data *cd = dev_get_drvdata(dev);
  13191. u32 input_data[2];
  13192. int length;
  13193. int rc = 0;
  13194. /* Maximum input of one value */
  13195. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13196. ARRAY_SIZE(input_data));
  13197. if (length != 1) {
  13198. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13199. __func__);
  13200. rc = -EINVAL;
  13201. goto exit;
  13202. }
  13203. pt_debug(dev, DL_INFO, "%s: features.easywake = 0x%02X\n",
  13204. __func__, cd->features.easywake);
  13205. if (!cd->features.easywake || input_data[0] > 0xFF) {
  13206. rc = -EINVAL;
  13207. goto exit;
  13208. }
  13209. pm_runtime_get_sync(dev);
  13210. mutex_lock(&cd->system_lock);
  13211. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 2)) {
  13212. cd->easy_wakeup_gesture = (u8)input_data[0];
  13213. pt_debug(dev, DL_INFO,
  13214. "%s: Updated easy_wakeup_gesture = 0x%02X\n",
  13215. __func__, cd->easy_wakeup_gesture);
  13216. } else
  13217. rc = -ENODEV;
  13218. mutex_unlock(&cd->system_lock);
  13219. pm_runtime_put(dev);
  13220. exit:
  13221. if (rc)
  13222. return rc;
  13223. return size;
  13224. }
  13225. /*******************************************************************************
  13226. * FUNCTION: pt_easy_wakeup_gesture_id_show
  13227. *
  13228. * SUMMARY: Show method for the easy_wakeup_gesture_id sysfs node that will
  13229. * show the TSG6 easywake gesture ID
  13230. *
  13231. * RETURN:
  13232. * 0 = success
  13233. * !0 = failure
  13234. *
  13235. * PARAMETERS:
  13236. * *dev - pointer to device structure
  13237. * *attr - pointer to device attributes
  13238. * *buf - pointer to output buffer
  13239. ******************************************************************************/
  13240. static ssize_t pt_easy_wakeup_gesture_id_show(struct device *dev,
  13241. struct device_attribute *attr, char *buf)
  13242. {
  13243. struct pt_core_data *cd = dev_get_drvdata(dev);
  13244. ssize_t ret;
  13245. mutex_lock(&cd->system_lock);
  13246. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: 0\n0x%02X\n",
  13247. cd->gesture_id);
  13248. mutex_unlock(&cd->system_lock);
  13249. return ret;
  13250. }
  13251. /*******************************************************************************
  13252. * FUNCTION: pt_easy_wakeup_gesture_data_show
  13253. *
  13254. * SUMMARY: Show method for the easy_wakeup_gesture_data sysfs node that will
  13255. * show the TSG6 easywake gesture data in the following format:
  13256. * x1(LSB),x1(MSB), y1(LSB),y1(MSB), x2(LSB),x2(MSB), y2(LSB),y2(MSB),...
  13257. *
  13258. * RETURN:
  13259. * 0 = success
  13260. * !0 = failure
  13261. *
  13262. * PARAMETERS:
  13263. * *dev - pointer to device structure
  13264. * *attr - pointer to device attributes
  13265. * *buf - pointer to output buffer
  13266. ******************************************************************************/
  13267. static ssize_t pt_easy_wakeup_gesture_data_show(struct device *dev,
  13268. struct device_attribute *attr, char *buf)
  13269. {
  13270. struct pt_core_data *cd = dev_get_drvdata(dev);
  13271. ssize_t ret = 0;
  13272. int i;
  13273. mutex_lock(&cd->system_lock);
  13274. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret, "Status: %d\n", 0);
  13275. for (i = 0; i < cd->gesture_data_length; i++)
  13276. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13277. "0x%02X\n", cd->gesture_data[i]);
  13278. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13279. "(%d bytes)\n", cd->gesture_data_length);
  13280. mutex_unlock(&cd->system_lock);
  13281. return ret;
  13282. }
  13283. #endif /* EASYWAKE_TSG6 */
  13284. #ifdef TTDL_DIAGNOSTICS
  13285. /*******************************************************************************
  13286. * FUNCTION: pt_err_gpio_show
  13287. *
  13288. * SUMMARY: Show method for the err_gpio sysfs node that will show if
  13289. * setting up the gpio was successful
  13290. *
  13291. * RETURN: Char buffer with printed GPIO creation state
  13292. *
  13293. * PARAMETERS:
  13294. * *dev - pointer to device structure
  13295. * *attr - pointer to device attributes
  13296. * *buf - pointer to output buffer
  13297. ******************************************************************************/
  13298. static ssize_t pt_err_gpio_show(struct device *dev,
  13299. struct device_attribute *attr, char *buf)
  13300. {
  13301. struct pt_core_data *cd = dev_get_drvdata(dev);
  13302. return scnprintf(buf, strlen(buf), "Status: 0\n"
  13303. "Err GPIO (%d) : %s\n"
  13304. "Err GPIO trig type: %d\n",
  13305. cd->err_gpio,
  13306. (cd->err_gpio ? (gpio_get_value(cd->err_gpio) ?
  13307. "HIGH" : "low") : "not defined"),
  13308. cd->err_gpio_type);
  13309. }
  13310. /*******************************************************************************
  13311. * FUNCTION: pt_err_gpio_store
  13312. *
  13313. * SUMMARY: The store method for the err_gpio sysfs node that allows any
  13314. * available host GPIO to be used to trigger when TTDL detects a PIP
  13315. * command/response timeout.
  13316. *
  13317. * RETURN:
  13318. * 0 = success
  13319. * !0 = failure
  13320. *
  13321. * PARAMETERS:
  13322. * *dev - pointer to device structure
  13323. * *attr - pointer to device attributes
  13324. * *buf - pointer to buffer that hold the command parameters
  13325. * size - size of buf
  13326. ******************************************************************************/
  13327. static ssize_t pt_err_gpio_store(struct device *dev,
  13328. struct device_attribute *attr, const char *buf, size_t size)
  13329. {
  13330. struct pt_core_data *cd = dev_get_drvdata(dev);
  13331. unsigned long gpio;
  13332. int rc = 0;
  13333. u32 input_data[3];
  13334. int length;
  13335. u8 err_type;
  13336. input_data[0] = 0;
  13337. input_data[1] = 0;
  13338. /* Maximmum input is two elements */
  13339. length = _pt_ic_parse_input(dev, buf, size,
  13340. input_data, ARRAY_SIZE(input_data));
  13341. if (length < 1 || length > 2) {
  13342. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13343. __func__);
  13344. rc = -EINVAL;
  13345. goto exit;
  13346. }
  13347. gpio = input_data[0];
  13348. err_type = (u8)input_data[1];
  13349. if (err_type < 0 || err_type > PT_ERR_GPIO_MAX_TYPE) {
  13350. rc = -EINVAL;
  13351. goto exit;
  13352. }
  13353. mutex_lock(&cd->system_lock);
  13354. gpio_free(gpio);
  13355. rc = gpio_request(gpio, NULL);
  13356. if (rc) {
  13357. pt_debug(dev, DL_ERROR, "error requesting gpio %lu\n", gpio);
  13358. rc = -ENODEV;
  13359. } else {
  13360. cd->err_gpio = gpio;
  13361. cd->err_gpio_type = err_type;
  13362. gpio_direction_output(gpio, 0);
  13363. }
  13364. mutex_unlock(&cd->system_lock);
  13365. exit:
  13366. if (rc)
  13367. return rc;
  13368. return size;
  13369. }
  13370. static DEVICE_ATTR(err_gpio, 0644, pt_err_gpio_show,
  13371. pt_err_gpio_store);
  13372. /*******************************************************************************
  13373. * FUNCTION: pt_drv_irq_show
  13374. *
  13375. * SUMMARY: Show method for the drv_irq sysfs node that will show if the
  13376. * TTDL interrupt is enabled/disabled
  13377. *
  13378. * RETURN:
  13379. * 0 = success
  13380. * !0 = failure
  13381. *
  13382. * PARAMETERS:
  13383. * *dev - pointer to device structure
  13384. * *attr - pointer to device attributes
  13385. * *buf - pointer to output buffer
  13386. ******************************************************************************/
  13387. static ssize_t pt_drv_irq_show(struct device *dev,
  13388. struct device_attribute *attr, char *buf)
  13389. {
  13390. struct pt_core_data *cd = dev_get_drvdata(dev);
  13391. ssize_t ret = 0;
  13392. mutex_lock(&cd->system_lock);
  13393. ret += scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n", 0);
  13394. if (cd->irq_enabled)
  13395. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13396. "Driver interrupt: ENABLED\n");
  13397. else
  13398. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13399. "Driver interrupt: DISABLED\n");
  13400. mutex_unlock(&cd->system_lock);
  13401. return ret;
  13402. }
  13403. /*******************************************************************************
  13404. * FUNCTION: pt_drv_irq_store
  13405. *
  13406. * SUMMARY: The store method for the drv_irq sysfs node that allows the TTDL
  13407. * IRQ to be enabled/disabled.
  13408. *
  13409. * RETURN: Size of passed in buffer
  13410. *
  13411. * PARAMETERS:
  13412. * *dev - pointer to device structure
  13413. * *attr - pointer to device attributes
  13414. * *buf - pointer to buffer that hold the command parameters
  13415. * size - size of buf
  13416. ******************************************************************************/
  13417. static ssize_t pt_drv_irq_store(struct device *dev,
  13418. struct device_attribute *attr, const char *buf, size_t size)
  13419. {
  13420. struct pt_core_data *cd = dev_get_drvdata(dev);
  13421. u32 input_data[2];
  13422. int length;
  13423. int rc = 0;
  13424. /* Maximum input of one value */
  13425. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13426. ARRAY_SIZE(input_data));
  13427. if (length != 1) {
  13428. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13429. __func__);
  13430. rc = -EINVAL;
  13431. goto exit;
  13432. }
  13433. mutex_lock(&cd->system_lock);
  13434. switch (input_data[0]) {
  13435. case 0:
  13436. if (cd->irq_enabled) {
  13437. cd->irq_enabled = false;
  13438. /* Disable IRQ has no return value to check */
  13439. disable_irq_nosync(cd->irq);
  13440. pt_debug(dev, DL_INFO,
  13441. "%s: Driver IRQ now disabled\n",
  13442. __func__);
  13443. } else
  13444. pt_debug(dev, DL_INFO,
  13445. "%s: Driver IRQ already disabled\n",
  13446. __func__);
  13447. break;
  13448. case 1:
  13449. if (cd->irq_enabled == false) {
  13450. cd->irq_enabled = true;
  13451. enable_irq(cd->irq);
  13452. pt_debug(dev, DL_INFO,
  13453. "%s: Driver IRQ now enabled\n",
  13454. __func__);
  13455. } else
  13456. pt_debug(dev, DL_INFO,
  13457. "%s: Driver IRQ already enabled\n",
  13458. __func__);
  13459. break;
  13460. default:
  13461. rc = -EINVAL;
  13462. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  13463. }
  13464. mutex_unlock(&(cd->system_lock));
  13465. exit:
  13466. if (rc)
  13467. return rc;
  13468. return size;
  13469. }
  13470. /*******************************************************************************
  13471. * FUNCTION: pt_pip2_bin_hdr_show
  13472. *
  13473. * SUMMARY: Show method for the pip2_bin_hdr sysfs node that will read
  13474. * the bin file header from flash and show each field
  13475. *
  13476. * RETURN:
  13477. * 0 = success
  13478. * !0 = failure
  13479. *
  13480. * PARAMETERS:
  13481. * *dev - pointer to device structure
  13482. * *attr - pointer to device attributes
  13483. * *buf - pointer to output buffer
  13484. ******************************************************************************/
  13485. static ssize_t pt_pip2_bin_hdr_show(struct device *dev,
  13486. struct device_attribute *attr, char *buf)
  13487. {
  13488. ssize_t ret = 0;
  13489. struct pt_bin_file_hdr hdr = {0};
  13490. int rc;
  13491. rc = _pt_request_pip2_bin_hdr(dev, &hdr);
  13492. ret = scnprintf(buf, strlen(buf),
  13493. "%s: %d\n"
  13494. "%s: %d\n"
  13495. "%s: 0x%04X\n"
  13496. "%s: %d\n"
  13497. "%s: %d\n"
  13498. "%s: %d\n"
  13499. "%s: 0x%08X\n"
  13500. "%s: 0x%04X\n"
  13501. "%s: 0x%04X\n"
  13502. "%s: %d\n"
  13503. "%s: %d\n",
  13504. "Status", rc,
  13505. "Header Length ", hdr.length,
  13506. "TTPID ", hdr.ttpid,
  13507. "FW Major Ver ", hdr.fw_major,
  13508. "FW Minor Ver ", hdr.fw_minor,
  13509. "FW Rev Control ", hdr.fw_rev_ctrl,
  13510. "FW CRC ", hdr.fw_crc,
  13511. "Silicon Rev ", hdr.si_rev,
  13512. "Silicon ID ", hdr.si_id,
  13513. "Config Ver ", hdr.config_ver,
  13514. "HEX File Size ", hdr.hex_file_size
  13515. );
  13516. return ret;
  13517. }
  13518. /*******************************************************************************
  13519. * FUNCTION: pt_platform_data_show
  13520. *
  13521. * SUMMARY: Show method for the platform_data sysfs node that will show the
  13522. * active platform data including: GPIOs, Vendor and Product IDs,
  13523. * Virtual Key coordinates, Core/MT/Loader flags, Level trigger delay,
  13524. * HID registers, and Easy wake gesture
  13525. *
  13526. * RETURN: Size of printed data
  13527. *
  13528. * PARAMETERS:
  13529. * *dev - pointer to device structure
  13530. * *attr - pointer to device attributes
  13531. * *buf - pointer to output buffer
  13532. ******************************************************************************/
  13533. static ssize_t pt_platform_data_show(struct device *dev,
  13534. struct device_attribute *attr, char *buf)
  13535. {
  13536. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13537. struct pt_core_data *cd = dev_get_drvdata(dev);
  13538. ssize_t ret;
  13539. ret = scnprintf(buf, strlen(buf),
  13540. "%s: %d\n"
  13541. "%s: %d\n"
  13542. "%s: %d\n"
  13543. "%s: %d\n"
  13544. "%s: %d\n"
  13545. "%s: %d\n"
  13546. "%s: %d\n"
  13547. "%s: %d\n"
  13548. "%s: %d\n"
  13549. "%s: %d\n"
  13550. "%s: %d\n"
  13551. "%s: %d\n"
  13552. "%s: %d\n"
  13553. "%s: %d\n"
  13554. "%s: %d\n"
  13555. "%s: %d\n"
  13556. "%s: %d\n"
  13557. "%s: %d\n"
  13558. "%s: %d\n"
  13559. "%s: %d\n"
  13560. "%s: %d\n"
  13561. "%s: %d\n"
  13562. "%s: %d\n"
  13563. "%s: %d\n"
  13564. "%s: %d\n"
  13565. "%s: %d\n"
  13566. "%s: %d\n"
  13567. "%s: %s\n"
  13568. "%s: %s\n"
  13569. "%s: %d\n",
  13570. "Status", 0,
  13571. "Interrupt GPIO ", pdata->core_pdata->irq_gpio,
  13572. "Interrupt GPIO Value ",
  13573. pdata->core_pdata->irq_gpio ?
  13574. gpio_get_value(pdata->core_pdata->irq_gpio) : 0,
  13575. "Reset GPIO ", pdata->core_pdata->rst_gpio,
  13576. "Reset GPIO Value ",
  13577. pdata->core_pdata->rst_gpio ?
  13578. gpio_get_value(pdata->core_pdata->rst_gpio) : 0,
  13579. "DDI Reset GPIO ", pdata->core_pdata->ddi_rst_gpio,
  13580. "DDI Reset GPIO Value ",
  13581. pdata->core_pdata->ddi_rst_gpio ?
  13582. gpio_get_value(pdata->core_pdata->ddi_rst_gpio) : 0,
  13583. "VDDI GPIO ", pdata->core_pdata->vddi_gpio,
  13584. "VDDI GPIO Value ",
  13585. pdata->core_pdata->vddi_gpio ?
  13586. gpio_get_value(pdata->core_pdata->vddi_gpio) : 0,
  13587. "VCC GPIO ", pdata->core_pdata->vcc_gpio,
  13588. "VCC GPIO Value ",
  13589. pdata->core_pdata->vcc_gpio ?
  13590. gpio_get_value(pdata->core_pdata->vcc_gpio) : 0,
  13591. "AVDD GPIO ", pdata->core_pdata->avdd_gpio,
  13592. "AVDD GPIO Value ",
  13593. pdata->core_pdata->avdd_gpio ?
  13594. gpio_get_value(pdata->core_pdata->avdd_gpio) : 0,
  13595. "AVEE GPIO ", pdata->core_pdata->avee_gpio,
  13596. "AVEE GPIO Value ",
  13597. pdata->core_pdata->avee_gpio ?
  13598. gpio_get_value(pdata->core_pdata->avee_gpio) : 0,
  13599. "Vendor ID ", pdata->core_pdata->vendor_id,
  13600. "Product ID ", pdata->core_pdata->product_id,
  13601. "Vkeys x ", pdata->mt_pdata->vkeys_x,
  13602. "Vkeys y ", pdata->mt_pdata->vkeys_y,
  13603. "Core data flags ", pdata->core_pdata->flags,
  13604. "MT data flags ", pdata->mt_pdata->flags,
  13605. "Loader data flags ", pdata->loader_pdata->flags,
  13606. "Level trigger delay (us) ",
  13607. pdata->core_pdata->level_irq_udelay,
  13608. "HID Descriptor Register ",
  13609. pdata->core_pdata->hid_desc_register,
  13610. "HID Output Register ",
  13611. cd->hid_desc.output_register,
  13612. "HID Command Register ",
  13613. cd->hid_desc.command_register,
  13614. "Easy wakeup gesture ",
  13615. pdata->core_pdata->easy_wakeup_gesture,
  13616. "Config DUT generation ",
  13617. pdata->core_pdata->config_dut_generation ?
  13618. (pdata->core_pdata->config_dut_generation ==
  13619. CONFIG_DUT_PIP2_CAPABLE ?
  13620. "PT TC/TT" : "Gen5/6") : "Auto",
  13621. "Watchdog Force Stop ",
  13622. pdata->core_pdata->watchdog_force_stop ?
  13623. "True" : "False",
  13624. "Panel ID Support ",
  13625. pdata->core_pdata->panel_id_support);
  13626. return ret;
  13627. }
  13628. #define PT_ERR_STR_SIZE 64
  13629. /*******************************************************************************
  13630. * FUNCTION: pt_bist_bus_test
  13631. *
  13632. * SUMMARY: Tests the connectivity of the active bus pins:
  13633. * I2C - SDA and SCL
  13634. * SPI - MOSI, MISO, CLK
  13635. *
  13636. * Disable TTDL interrupts, send a PIP cmd and then manually read the
  13637. * bus. If any data is read we know the I2C/SPI pins are connected
  13638. *
  13639. * RETURN:
  13640. * 0 = Success
  13641. * !0 = Failure
  13642. *
  13643. * PARAMETERS:
  13644. * *dev - pointer to device structure
  13645. * *net_toggled - pointer to where to store if bus toggled
  13646. * *err_str - pointer to error string buffer
  13647. ******************************************************************************/
  13648. static int pt_bist_bus_test(struct device *dev, u8 *net_toggled, u8 *err_str)
  13649. {
  13650. struct pt_core_data *cd = dev_get_drvdata(dev);
  13651. u8 ver_cmd[8] = {0x01, 0x01, 0x06, 0x00, 0x0E, 0x07, 0xF0, 0xB1};
  13652. u8 *read_buf = NULL;
  13653. int bytes_read = 0;
  13654. int rc = 0;
  13655. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  13656. if (read_buf == NULL) {
  13657. rc = -ENOMEM;
  13658. goto exit;
  13659. }
  13660. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  13661. pt_debug(dev, DL_INFO, "%s: TTDL Core Suspend\n", __func__);
  13662. disable_irq(cd->irq);
  13663. mutex_lock(&cd->system_lock);
  13664. cd->irq_disabled = true;
  13665. mutex_unlock(&cd->system_lock);
  13666. /*
  13667. * Sleep >4ms to allow any pending TTDL IRQ to finish. Without this
  13668. * the disable_irq_nosync() could cause the IRQ to get stuck asserted
  13669. */
  13670. usleep_range(5000, 6000);
  13671. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer Size[%d] VERSION\n",
  13672. __func__, (int)sizeof(ver_cmd));
  13673. pt_pr_buf(cd->dev, DL_DEBUG, ver_cmd, (int)sizeof(ver_cmd),
  13674. ">>> User CMD");
  13675. rc = pt_adap_write_read_specific(cd, sizeof(ver_cmd), ver_cmd, NULL);
  13676. if (rc) {
  13677. pt_debug(dev, DL_ERROR,
  13678. "%s: BUS Test - Failed to send VER cmd\n", __func__);
  13679. *net_toggled = 0;
  13680. strlcpy(err_str,
  13681. "- Write failed, bus open or shorted or DUT in reset", PT_ERR_STR_SIZE);
  13682. goto exit_enable_irq;
  13683. }
  13684. usleep_range(4000, 5000);
  13685. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, read_buf);
  13686. pt_debug(dev, DL_INFO, "%s: BUS Test - %d bytes forced read\n",
  13687. __func__, bytes_read);
  13688. if (bytes_read == 0) {
  13689. *net_toggled = 0;
  13690. pt_debug(dev, DL_INFO, "%s: BUS Read Failed, 0 bytes read\n",
  13691. __func__);
  13692. strlcpy(err_str,
  13693. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  13694. rc = -EIO;
  13695. goto exit_enable_irq;
  13696. } else {
  13697. if (cd->bus_ops->bustype == BUS_I2C)
  13698. *net_toggled = 1;
  13699. else {
  13700. if ((bytes_read > 3) &&
  13701. (read_buf[3] & PIP2_CMD_COMMAND_ID_MASK) ==
  13702. PIP2_CMD_ID_VERSION)
  13703. *net_toggled = 1;
  13704. else {
  13705. *net_toggled = 0;
  13706. pt_debug(dev, DL_INFO,
  13707. "%s: BUS Read Failed, %d bytes read\n",
  13708. __func__, bytes_read);
  13709. strlcpy(err_str,
  13710. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  13711. }
  13712. }
  13713. }
  13714. exit_enable_irq:
  13715. enable_irq(cd->irq);
  13716. usleep_range(5000, 6000);
  13717. mutex_lock(&cd->system_lock);
  13718. cd->irq_disabled = false;
  13719. mutex_unlock(&cd->system_lock);
  13720. pt_debug(dev, DL_INFO, "%s: TTDL Core Resumed\n", __func__);
  13721. exit:
  13722. kfree(read_buf);
  13723. return rc;
  13724. }
  13725. /*******************************************************************************
  13726. * FUNCTION: pt_bist_irq_test
  13727. *
  13728. * SUMMARY: Tests the connectivity of the IRQ net
  13729. *
  13730. * This test will ensure there is a good connection between the host
  13731. * and the DUT on the irq pin. First determine if the IRQ is stuck
  13732. * asserted and if so keep reading messages off of the bus until
  13733. * it de-asserts. Possible outcomes:
  13734. * - IRQ was already de-asserted: Send a PIP command and if an
  13735. * interrupt is generated the test passes.
  13736. * - IRQ was asserted: Reading off the bus de-assertes the IRQ,
  13737. * test passes.
  13738. * - IRQ stays asserted: After reading the bus multiple times
  13739. * the IRQ stays asserted. Likely open or shorted to GND
  13740. *
  13741. * RETURN:
  13742. * 0 = Success
  13743. * !0 = Failure
  13744. *
  13745. * PARAMETERS:
  13746. * *dev - pointer to device structure
  13747. * *bus_toggled - pointer to where to store if bus toggled
  13748. * *irq_toggled - pointer to where to store if IRQ toggled
  13749. * *xres_toggled - pointer to where to store if XRES toggled
  13750. * *err_str - pointer to error string buffer
  13751. ******************************************************************************/
  13752. static int pt_bist_irq_test(struct device *dev,
  13753. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  13754. {
  13755. struct pt_core_data *cd = dev_get_drvdata(dev);
  13756. u8 *read_buf = NULL;
  13757. u8 mode = PT_MODE_UNKNOWN;
  13758. u16 actual_read_len;
  13759. int bytes_read = 0;
  13760. int count = 0;
  13761. int rc = 0;
  13762. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  13763. if (read_buf == NULL) {
  13764. rc = -ENOMEM;
  13765. goto exit;
  13766. }
  13767. /* Clear IRQ triggered count, and re-evaluate at the end of test */
  13768. cd->irq_count = 0;
  13769. /*
  13770. * Check if IRQ is stuck asserted, if so try a non forced flush of
  13771. * the bus based on the 2 byte initial length read. Try up to 5x.
  13772. */
  13773. while (pt_check_irq_asserted(cd) && count < 5) {
  13774. count++;
  13775. bytes_read += pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  13776. }
  13777. if (count > 1 && count < 5 && bytes_read > 0) {
  13778. /*
  13779. * IRQ was stuck but data was successfully read from the
  13780. * bus releasing the IRQ line.
  13781. */
  13782. pt_debug(dev, DL_INFO, "%s: count=%d bytes_read=%d\n",
  13783. __func__, count, bytes_read);
  13784. *bus_toggled = 1;
  13785. *irq_toggled = 1;
  13786. goto exit;
  13787. }
  13788. if (count == 5 && bytes_read == 0) {
  13789. /*
  13790. * Looped 5x and read nothing off the bus yet the IRQ is still
  13791. * asserted. Possible conditions:
  13792. * - IRQ open circuit
  13793. * - IRQ shorted to GND
  13794. * - I2C/SPI bus is disconnected
  13795. * - FW holding the pin low
  13796. * Try entering the BL to see if communication works there.
  13797. */
  13798. mode = PT_MODE_IGNORE;
  13799. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  13800. if (rc) {
  13801. pt_debug(dev, DL_ERROR,
  13802. "%s Failed to enter BL\n", __func__);
  13803. strlcpy(err_str,
  13804. "- likely shorted to GND or FW holding it.", PT_ERR_STR_SIZE);
  13805. *irq_toggled = 0;
  13806. goto exit;
  13807. }
  13808. /*
  13809. * If original mode was operational and we successfully
  13810. * entered the BL, then the XRES net must have toggled
  13811. */
  13812. if (mode == PT_MODE_OPERATIONAL)
  13813. *xres_toggled = 1;
  13814. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  13815. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  13816. &actual_read_len);
  13817. if (rc) {
  13818. /*
  13819. * Could not communicate to DUT in BL mode. Save the
  13820. * error string, slim chance but the XRES test below may
  13821. * show the IRQ is actually working.
  13822. */
  13823. strlcpy(err_str, "- likely shorted to GND.", PT_ERR_STR_SIZE);
  13824. pt_debug(dev, DL_ERROR,
  13825. "%s: %s, count=%d bytes_read=%d\n",
  13826. __func__, err_str, count, bytes_read);
  13827. *irq_toggled = 0;
  13828. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  13829. goto exit;
  13830. } else {
  13831. *bus_toggled = 1;
  13832. *irq_toggled = 1;
  13833. goto exit;
  13834. }
  13835. }
  13836. if (pt_check_irq_asserted(cd)) {
  13837. strlcpy(err_str, "- likely shorted to GND", PT_ERR_STR_SIZE);
  13838. rc = -EIO;
  13839. *irq_toggled = 0;
  13840. goto exit;
  13841. }
  13842. /* Try sending a PIP command to see if we get a response */
  13843. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  13844. PIP2_CMD_ID_VERSION, NULL, 0, read_buf, &actual_read_len);
  13845. if (rc) {
  13846. /*
  13847. * Potential IRQ issue, no communication in App mode, attempt
  13848. * the same command in the BL
  13849. */
  13850. mode = PT_MODE_IGNORE;
  13851. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  13852. if (rc) {
  13853. pt_debug(dev, DL_ERROR,
  13854. "%s Failed to enter BL\n", __func__);
  13855. *irq_toggled = 0;
  13856. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  13857. goto exit;
  13858. }
  13859. /*
  13860. * If original mode was operational and we successfully
  13861. * entered the BL, this will be useful info for the tp_xres
  13862. * test below.
  13863. */
  13864. if (mode == PT_MODE_OPERATIONAL)
  13865. *xres_toggled = 1;
  13866. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  13867. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  13868. &actual_read_len);
  13869. if (rc) {
  13870. /*
  13871. * Could not communicate in FW mode or BL mode. Save the
  13872. * error string, slim chance but the XRES test below may
  13873. * show the IRQ is actually working.
  13874. */
  13875. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  13876. pt_debug(dev, DL_ERROR,
  13877. "%s: request_active_pip_prot failed\n",
  13878. __func__);
  13879. *irq_toggled = 0;
  13880. goto exit;
  13881. }
  13882. }
  13883. if (cd->irq_count > 0) {
  13884. pt_debug(dev, DL_INFO, "%s: irq_count=%d\n", __func__,
  13885. cd->irq_count);
  13886. *bus_toggled = 1;
  13887. *irq_toggled = 1;
  13888. goto exit;
  13889. }
  13890. exit:
  13891. kfree(read_buf);
  13892. return rc;
  13893. }
  13894. /*******************************************************************************
  13895. * FUNCTION: pt_bist_xres_test
  13896. *
  13897. * SUMMARY: Tests the connectivity of the TP_XRES net
  13898. *
  13899. * This test will ensure there is a good connection between the host
  13900. * and the DUT on the tp_xres pin. The pin will be toggled to
  13901. * generate a TP reset which will cause the DUT to output a reset
  13902. * sentinel. If the reset sentinel is seen the test passes. If it is
  13903. * not seen the test will attempt to send a soft reset to simply gain
  13904. * some additional information on the failure:
  13905. * - soft reset fails to send: XRES and IRQ likely open
  13906. * - soft reset passes: XRES likely open or stuck de-asserted
  13907. * - soft reset fails: XRES likely stuck asserted
  13908. *
  13909. * RETURN:
  13910. * 0 = Success
  13911. * !0 = Failure
  13912. *
  13913. * PARAMETERS:
  13914. * *dev - pointer to device structure
  13915. * *bus_toggled - pointer to where to store if bus toggled
  13916. * *irq_toggled - pointer to where to store if IRQ toggled
  13917. * *xres_toggled - pointer to where to store if XRES toggled
  13918. * *err_str - pointer to error string buffer
  13919. ******************************************************************************/
  13920. static int pt_bist_xres_test(struct device *dev,
  13921. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  13922. {
  13923. struct pt_core_data *cd = dev_get_drvdata(dev);
  13924. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13925. u8 *read_buf = NULL;
  13926. u8 mode = PT_MODE_UNKNOWN;
  13927. int rc = 0;
  13928. int t = 0;
  13929. int timeout = 300;
  13930. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  13931. if (read_buf == NULL) {
  13932. rc = -ENOMEM;
  13933. goto exit;
  13934. }
  13935. /* Clear the startup bit mask, reset and enum will re-populate it */
  13936. cd->startup_status = STARTUP_STATUS_START;
  13937. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  13938. if ((!pdata->core_pdata->rst_gpio) || (!pdata->core_pdata->xres)) {
  13939. strlcpy(err_str, "- Net not configured or available", PT_ERR_STR_SIZE);
  13940. rc = -ENODEV;
  13941. goto exit;
  13942. }
  13943. /* Ensure we have nothing pending on active bus */
  13944. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  13945. /* Perform a hard XRES toggle and wait for reset sentinel */
  13946. mutex_lock(&cd->system_lock);
  13947. cd->hid_reset_cmd_state = 1;
  13948. mutex_unlock(&cd->system_lock);
  13949. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  13950. rc = pt_hw_hard_reset(cd);
  13951. /* Set timeout to 1s for the flashless case where a BL could be done */
  13952. if (cd->flashless_dut)
  13953. timeout = 1000;
  13954. /*
  13955. * To avoid the case that next PIP command can be confused by BL/FW
  13956. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  13957. * followed by "wakeup event" function can lower the failure rate.
  13958. */
  13959. t = wait_event_timeout(cd->wait_q,
  13960. ((cd->startup_status != STARTUP_STATUS_START)
  13961. && (cd->hid_reset_cmd_state == 0)),
  13962. msecs_to_jiffies(timeout));
  13963. if (IS_TMO(t)) {
  13964. pt_debug(cd->dev, DL_ERROR,
  13965. "%s: TMO waiting for sentinel\n", __func__);
  13966. *xres_toggled = 0;
  13967. strlcpy(err_str, "- likely open. (No Reset Sentinel)", PT_ERR_STR_SIZE);
  13968. /*
  13969. * Possibly bad FW, Try entering BL and wait for reset sentinel.
  13970. * To enter the BL we need to generate an XRES so first try to
  13971. * launch the applicaiton
  13972. */
  13973. if (cd->mode == PT_MODE_BOOTLOADER)
  13974. pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  13975. pt_debug(dev, DL_INFO, "%s: Enter BL with a hard reset\n",
  13976. __func__);
  13977. mode = PT_MODE_IGNORE;
  13978. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  13979. if (rc) {
  13980. pt_debug(dev, DL_ERROR, "%s Failed to enter BL\n",
  13981. __func__);
  13982. *xres_toggled = 0;
  13983. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  13984. goto exit;
  13985. } else {
  13986. /* Wait for the BL sentinel */
  13987. t = wait_event_timeout(cd->wait_q,
  13988. (cd->startup_status != STARTUP_STATUS_START),
  13989. msecs_to_jiffies(500));
  13990. if (IS_TMO(t)) {
  13991. pt_debug(cd->dev, DL_ERROR,
  13992. "%s: TMO waiting for BL sentinel\n",
  13993. __func__);
  13994. *xres_toggled = 0;
  13995. strlcpy(err_str,
  13996. "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  13997. rc = -ETIME;
  13998. goto exit;
  13999. }
  14000. }
  14001. }
  14002. mutex_lock(&cd->system_lock);
  14003. cd->hid_reset_cmd_state = 0;
  14004. mutex_unlock(&cd->system_lock);
  14005. /* Look for BL or FW reset sentinels */
  14006. if (!rc && ((cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL) ||
  14007. (cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL))) {
  14008. pt_debug(dev, DL_INFO, "%s: hard XRES pass\n", __func__);
  14009. /* If a sentinel was seen, all nets are working */
  14010. *xres_toggled = 1;
  14011. *irq_toggled = 1;
  14012. /*
  14013. * For SPI test, bus read result can be confused as FW sentinel
  14014. * if MISO(slave) is connected to MISO(host).
  14015. */
  14016. if (cd->bus_ops->bustype == BUS_I2C)
  14017. *bus_toggled = 1;
  14018. } else {
  14019. /*
  14020. * Hard reset failed, however some additional information
  14021. * could be determined. Try a soft reset to see if DUT resets
  14022. * with the possible outcomes:
  14023. * - if it resets the line is not stuck asserted
  14024. * - if it does not reset the line could be stuck asserted
  14025. */
  14026. *xres_toggled = 0;
  14027. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  14028. msleep(30);
  14029. pt_debug(dev, DL_INFO, "%s: TP_XRES BIST soft reset rc=%d",
  14030. __func__, rc);
  14031. if (rc) {
  14032. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14033. pt_debug(dev, DL_ERROR,
  14034. "%s: Hard reset failed, soft reset failed %s\n",
  14035. __func__, err_str);
  14036. goto exit;
  14037. }
  14038. if (cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL ||
  14039. cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) {
  14040. strlcpy(err_str,
  14041. "- likely open or stuck high, soft reset OK", PT_ERR_STR_SIZE);
  14042. pt_debug(dev, DL_ERROR,
  14043. "%s: Hard reset failed, soft reset passed-%s\n",
  14044. __func__, err_str);
  14045. } else if (cd->startup_status == 0) {
  14046. strlcpy(err_str, "- likely stuck high.", PT_ERR_STR_SIZE);
  14047. pt_debug(dev, DL_ERROR,
  14048. "%s: Hard reset failed, soft reset failed-%s\n",
  14049. __func__, err_str);
  14050. } else {
  14051. strlcpy(err_str, "- open or stuck.", PT_ERR_STR_SIZE);
  14052. pt_debug(dev, DL_ERROR,
  14053. "%s: Hard and Soft reset failed - %s\n",
  14054. __func__, err_str);
  14055. }
  14056. }
  14057. exit:
  14058. kfree(read_buf);
  14059. return rc;
  14060. }
  14061. /*******************************************************************************
  14062. * FUNCTION: pt_bist_slave_irq_test
  14063. *
  14064. * SUMMARY: Tests the connectivity of the Master/Slave IRQ net
  14065. *
  14066. * This test will ensure there is a good IRQ connection between the master
  14067. * DUT and the slave DUT(s). After power up the STATUS command is sent
  14068. * and the 'Slave Detect' bits are verified to ensure the master DUT
  14069. * saw each slave trigger the IRQ with it's reset sentinel.
  14070. *
  14071. * RETURN:
  14072. * 0 = Success
  14073. * !0 = Failure
  14074. *
  14075. * PARAMETERS:
  14076. * *dev - pointer to device structure
  14077. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14078. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14079. * *err_str - pointer to error string buffer
  14080. * *slave_detect - pointer to slave detect buffer
  14081. * *boot_err - pointer to boot_err buffer
  14082. ******************************************************************************/
  14083. static int pt_bist_slave_irq_test(struct device *dev,
  14084. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str,
  14085. u8 *slave_detect, u8 *boot_err)
  14086. {
  14087. struct pt_core_data *cd = dev_get_drvdata(dev);
  14088. u8 mode = PT_MODE_UNKNOWN;
  14089. u8 status;
  14090. u8 boot;
  14091. u8 read_buf[12];
  14092. u8 detected = 0;
  14093. u8 last_err = -1;
  14094. u16 actual_read_len;
  14095. int result = 0;
  14096. int rc = 0;
  14097. /*
  14098. * Ensure DUT is in the BL where the STATUS cmd will report the slave
  14099. * detect bits. If the DUT was in FW, entering the BL will cause an
  14100. * XRES signal which will inadvertently test the XRES net as well
  14101. */
  14102. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14103. if (rc) {
  14104. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14105. __func__, rc);
  14106. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14107. goto exit;
  14108. }
  14109. /* Use the STATUS command to retrieve the slave detect bit(s) */
  14110. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14111. PIP2_CMD_ID_STATUS, NULL, 0, read_buf,
  14112. &actual_read_len);
  14113. if (!rc) {
  14114. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14115. "PIP2 STATUS");
  14116. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14117. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  14118. /* Slave detect is only valid if status ok and in boot exec */
  14119. if (status == PIP2_RSP_ERR_NONE &&
  14120. boot == PIP2_STATUS_BOOT_EXEC) {
  14121. detected = read_buf[PIP2_RESP_BODY_OFFSET + 2] &
  14122. SLAVE_DETECT_MASK;
  14123. } else {
  14124. strlcpy(err_str, "- State could not be determined", PT_ERR_STR_SIZE);
  14125. rc = -EPERM;
  14126. }
  14127. } else {
  14128. pt_debug(cd->dev, DL_ERROR, "%s: STATUS cmd failure\n",
  14129. __func__);
  14130. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14131. goto exit;
  14132. }
  14133. /*
  14134. * Retrieve boot error regardless of the state of the slave detect
  14135. * bit because the IRQ could have been stuck high or low.
  14136. */
  14137. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14138. PIP2_CMD_ID_GET_LAST_ERRNO, NULL, 0,
  14139. read_buf, &actual_read_len);
  14140. if (!rc) {
  14141. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14142. "PIP2 GET_LAST_ERRNO");
  14143. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14144. last_err = read_buf[PIP2_RESP_BODY_OFFSET];
  14145. if (last_err) {
  14146. pt_debug(cd->dev, DL_ERROR,
  14147. "%s: Master Boot Last Err = 0x%02X\n",
  14148. __func__, last_err);
  14149. }
  14150. } else {
  14151. pt_debug(cd->dev, DL_ERROR,
  14152. "%s: GET_LAST_ERRNO cmd failure\n", __func__);
  14153. strlcpy(err_str, "- stuck, likely shorted to GND.", PT_ERR_STR_SIZE);
  14154. }
  14155. exit:
  14156. pt_debug(cd->dev, DL_INFO,
  14157. "%s: rc=%d detected=0x%02X boot_err=0x%02X\n",
  14158. __func__, rc, detected, last_err);
  14159. if (err_str && last_err) {
  14160. if (detected)
  14161. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14162. "- Likely stuck low. Boot Error:",
  14163. last_err);
  14164. else
  14165. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14166. "- Likely stuck high. Boot Error:",
  14167. last_err);
  14168. }
  14169. /* Ignore an invalid image error as BIST doesn't need valid FW */
  14170. if (last_err == PIP2_RSP_ERR_INVALID_IMAGE)
  14171. last_err = PIP2_RSP_ERR_NONE;
  14172. if (slave_irq_toggled)
  14173. *slave_irq_toggled = (detected && !last_err) ? true : false;
  14174. if (slave_bus_toggled) {
  14175. /* Leave as UNTEST if slave not detected */
  14176. if (detected)
  14177. *slave_bus_toggled = !last_err ? true : false;
  14178. }
  14179. if (slave_detect)
  14180. *slave_detect = detected;
  14181. if (boot_err)
  14182. *boot_err = last_err;
  14183. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X, %s=0x%02X, %s=0x%02X\n",
  14184. __func__,
  14185. "Detected", detected,
  14186. "slave_irq_toggled", *slave_irq_toggled,
  14187. "slave_bus_toggled", *slave_bus_toggled);
  14188. return rc;
  14189. }
  14190. /*******************************************************************************
  14191. * FUNCTION: pt_bist_slave_xres_test
  14192. *
  14193. * SUMMARY: Tests the connectivity of the Master/Slave TP_XRES net
  14194. *
  14195. * This test will ensure there is a good TP_XRES connection between the
  14196. * master DUT and the slave DUT(s). After toggling the XRES pin to the
  14197. * master, the STATUS command is sent and the 'Slave Detect' bits are
  14198. * verified to ensure the master DUT saw each slave trigger the IRQ with
  14199. * it's reset sentinel.
  14200. *
  14201. * RETURN:
  14202. * 0 = Success
  14203. * !0 = Failure
  14204. *
  14205. * PARAMETERS:
  14206. * *dev - pointer to device structure
  14207. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14208. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14209. * *slave_xres_toggled - pointer to where to store if slave XRES toggled
  14210. * *err_str - pointer to error string buffer
  14211. ******************************************************************************/
  14212. static int pt_bist_slave_xres_test(struct device *dev,
  14213. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *slave_xres_toggled,
  14214. u8 *err_str)
  14215. {
  14216. struct pt_core_data *cd = dev_get_drvdata(dev);
  14217. u8 slave_detect = 0;
  14218. u8 boot_err = 0;
  14219. int rc = 0;
  14220. /* Force a reset to force the 'slave detect' bits to be re-acquired */
  14221. mutex_lock(&cd->system_lock);
  14222. cd->hid_reset_cmd_state = 1;
  14223. mutex_unlock(&cd->system_lock);
  14224. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14225. pt_hw_hard_reset(cd);
  14226. msleep(100);
  14227. rc = pt_bist_slave_irq_test(dev, slave_irq_toggled,
  14228. slave_bus_toggled, err_str, &slave_detect, &boot_err);
  14229. pt_debug(dev, DL_INFO, "%s: IRQ test rc = %d\n", __func__, rc);
  14230. if (!rc && *slave_irq_toggled == false) {
  14231. /*
  14232. * If the slave IRQ did not toggle, either the slave_detect
  14233. * bit was not set or we had a boot error. If the slave
  14234. * detect was not set the slave did not reset causing a boot
  14235. * error.
  14236. */
  14237. if (!slave_detect)
  14238. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14239. else
  14240. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14241. "- likely open or an IRQ issue. Boot Error:",
  14242. boot_err);
  14243. }
  14244. if (slave_xres_toggled) {
  14245. if (!rc)
  14246. *slave_xres_toggled = *slave_irq_toggled ? true : false;
  14247. else
  14248. *slave_xres_toggled = false;
  14249. }
  14250. return rc;
  14251. }
  14252. /*******************************************************************************
  14253. * FUNCTION: pt_bist_slave_bus_test
  14254. *
  14255. * SUMMARY: Tests the connectivity of the Master/Slave SPI bus net
  14256. *
  14257. * This test will ensure a good SPI bus connection between the
  14258. * master DUT and the slave DUT(s). This bus connection is ensured by
  14259. * opening file 0 (SRAM loader). If there is a slave and the open fails
  14260. * then there is a master/slave communication issue. Opening file 0 on
  14261. * the master will open it on the slave as well if the slave was detected.
  14262. *
  14263. * RETURN:
  14264. * 0 = Success
  14265. * !0 = Failure
  14266. *
  14267. * PARAMETERS:
  14268. * *dev - pointer to device structure
  14269. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14270. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14271. * *err_str - pointer to error string buffer
  14272. ******************************************************************************/
  14273. static int pt_bist_slave_bus_test(struct device *dev,
  14274. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str)
  14275. {
  14276. struct pt_core_data *cd = dev_get_drvdata(dev);
  14277. u8 mode = PT_MODE_UNKNOWN;
  14278. u8 bus_toggled = false;
  14279. u8 file_handle;
  14280. int result = 0;
  14281. int rc = 0;
  14282. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14283. if (rc) {
  14284. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14285. __func__, rc);
  14286. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14287. goto exit;
  14288. }
  14289. pt_debug(dev, DL_INFO, "%s Attempt open file 0\n", __func__);
  14290. file_handle = _pt_pip2_file_open(dev, PIP2_RAM_FILE);
  14291. if (file_handle != PIP2_RAM_FILE) {
  14292. rc = -ENOENT;
  14293. bus_toggled = false;
  14294. pt_debug(dev, DL_ERROR,
  14295. "%s Failed to open bin file\n", __func__);
  14296. strlcpy(err_str, "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14297. goto exit;
  14298. } else {
  14299. bus_toggled = true;
  14300. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  14301. pt_debug(dev, DL_ERROR,
  14302. "%s: File Close failed, file_handle=%d\n",
  14303. __func__, file_handle);
  14304. }
  14305. }
  14306. exit:
  14307. /* If the master was able to send/recv a PIP msg, the IRQ must be ok */
  14308. if (slave_irq_toggled)
  14309. *slave_irq_toggled = bus_toggled;
  14310. if (slave_bus_toggled)
  14311. *slave_bus_toggled = bus_toggled;
  14312. return rc;
  14313. }
  14314. /*******************************************************************************
  14315. * FUNCTION: pt_ttdl_bist_show
  14316. *
  14317. * SUMMARY: Show method for the ttdl_bist sysfs node. This built in self test
  14318. * will test that I2C/SPI, IRQ and TP_XRES pins are operational.
  14319. *
  14320. * NOTE: This function will reset the DUT and the startup_status bit
  14321. * mask. A pt_enum will be queued after completion.
  14322. *
  14323. * NOTE: The order of the net tests is done to optimize the time it takes
  14324. * to run. The first test is capable of verifying all nets, each subsequent
  14325. * test is only run if the previous was not able to see all nets toggle.
  14326. *
  14327. * RETURN:
  14328. * 0 = success
  14329. * !0 = failure
  14330. *
  14331. * PARAMETERS:
  14332. * *dev - pointer to device structure
  14333. * *attr - pointer to device attributes
  14334. * *buf - pointer to output buffer
  14335. ******************************************************************************/
  14336. static ssize_t pt_ttdl_bist_show(struct device *dev,
  14337. struct device_attribute *attr, char *buf)
  14338. {
  14339. struct pt_core_data *cd = dev_get_drvdata(dev);
  14340. ssize_t ret;
  14341. char *bus_err_str = NULL;
  14342. char *irq_err_str = NULL;
  14343. char *xres_err_str = NULL;
  14344. char *slave_bus_err_str = NULL;
  14345. char *slave_irq_err_str = NULL;
  14346. char *slave_xres_err_str = NULL;
  14347. u8 tests;
  14348. int rc = 0;
  14349. int num_tests = 0;
  14350. int status = 1; /* 0 = Pass, !0 = fail */
  14351. u8 bus_toggled = 0x0F; /* default to untested */
  14352. u8 i2c_toggled = 0x0F; /* default to untested */
  14353. u8 spi_toggled = 0x0F; /* default to untested */
  14354. u8 irq_toggled = 0x0F; /* default to untested */
  14355. u8 xres_toggled = 0x0F; /* default to untested */
  14356. u8 slave_bus_toggled = 0x0F; /* default to untested */
  14357. u8 slave_irq_toggled = 0x0F; /* default to untested */
  14358. u8 slave_xres_toggled = 0x0F; /* default to untested */
  14359. bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14360. irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14361. xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14362. if (!bus_err_str || !irq_err_str || !xres_err_str)
  14363. goto print_results;
  14364. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14365. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14366. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  14367. if (cd->multi_chip) {
  14368. slave_bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14369. slave_irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14370. slave_xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14371. if (!slave_bus_err_str ||
  14372. !slave_irq_err_str ||
  14373. !slave_xres_err_str)
  14374. goto print_results;
  14375. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  14376. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  14377. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  14378. }
  14379. /* Turn off the TTDL WD during the test */
  14380. pt_stop_wd_timer(cd);
  14381. /* Shorten default PIP cmd timeout while running BIST */
  14382. cd->pip_cmd_timeout = 200;
  14383. /* Count the number of tests to run */
  14384. tests = cd->ttdl_bist_select;
  14385. while (tests) {
  14386. num_tests += tests & 1;
  14387. tests >>= 1;
  14388. }
  14389. pt_debug(dev, DL_ERROR, "%s: BIST select = 0x%02X, run %d tests\n",
  14390. __func__, cd->ttdl_bist_select, num_tests);
  14391. /* Suppress auto BL to avoid loader thread sending PIP during xres */
  14392. if (cd->flashless_dut) {
  14393. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - SUPPRESS\n",
  14394. __func__);
  14395. mutex_lock(&cd->system_lock);
  14396. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  14397. mutex_unlock(&cd->system_lock);
  14398. }
  14399. /* --------------- TP_XRES BIST TEST --------------- */
  14400. if ((cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST) != 0) {
  14401. pt_debug(dev, DL_INFO,
  14402. "%s: ----- Start TP_XRES BIST -----", __func__);
  14403. rc = pt_bist_xres_test(dev, &bus_toggled, &irq_toggled,
  14404. &xres_toggled, xres_err_str);
  14405. /* Done if the rest of all nets toggled */
  14406. if (bus_toggled == 1 && irq_toggled == 1 && xres_toggled == 1)
  14407. goto host_nets_complete;
  14408. }
  14409. /* Flush bus in case a PIP response is waiting from previous test */
  14410. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14411. /* --------------- IRQ BIST TEST --------------- */
  14412. if ((cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST) != 0) {
  14413. pt_debug(dev, DL_INFO,
  14414. "%s: ----- Start IRQ BIST -----", __func__);
  14415. bus_toggled = 0xFF;
  14416. irq_toggled = 0xFF;
  14417. rc = pt_bist_irq_test(dev, &bus_toggled, &irq_toggled,
  14418. &xres_toggled, irq_err_str);
  14419. /* If this net failed clear results from previous net */
  14420. if (irq_toggled != 1) {
  14421. xres_toggled = 0x0F;
  14422. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14423. }
  14424. if (bus_toggled == 1 && irq_toggled == 1)
  14425. goto host_nets_complete;
  14426. }
  14427. /* Flush bus in case a PIP response is waiting from previous test */
  14428. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14429. /* --------------- BUS BIST TEST --------------- */
  14430. if ((cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST) != 0) {
  14431. pt_debug(dev, DL_INFO,
  14432. "%s: ----- Start BUS BIST -----", __func__);
  14433. bus_toggled = 0xFF;
  14434. rc = pt_bist_bus_test(dev, &bus_toggled, bus_err_str);
  14435. /* If this net failed clear results from previous net */
  14436. if (bus_toggled == 0) {
  14437. irq_toggled = 0x0F;
  14438. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14439. }
  14440. }
  14441. host_nets_complete:
  14442. /* --------------- SLAVE XRES BIST TEST --------------- */
  14443. if (cd->multi_chip &&
  14444. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST) != 0) {
  14445. pt_debug(dev, DL_INFO,
  14446. "%s: ----- Start Slave XRES BIST -----", __func__);
  14447. slave_xres_toggled = 0xFF;
  14448. rc = pt_bist_slave_xres_test(dev, &slave_irq_toggled,
  14449. &slave_bus_toggled, &slave_xres_toggled,
  14450. slave_xres_err_str);
  14451. if ((slave_bus_toggled == 1 && slave_irq_toggled == 1 &&
  14452. slave_xres_toggled == 1) || slave_xres_toggled == 0)
  14453. goto print_results;
  14454. }
  14455. /* --------------- SLAVE IRQ BIST TEST --------------- */
  14456. if (cd->multi_chip &&
  14457. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST) != 0) {
  14458. pt_debug(dev, DL_INFO,
  14459. "%s: ----- Start Slave IRQ BIST -----", __func__);
  14460. slave_irq_toggled = 0xFF;
  14461. rc = pt_bist_slave_irq_test(dev, &slave_irq_toggled,
  14462. &slave_bus_toggled, slave_irq_err_str, NULL, NULL);
  14463. pt_debug(dev, DL_INFO, "%s: slave_irq_toggled = 0x%02X\n",
  14464. __func__, slave_irq_toggled);
  14465. if (slave_irq_toggled == 1) {
  14466. slave_bus_toggled = 1;
  14467. goto print_results;
  14468. }
  14469. }
  14470. /* --------------- SLAVE BUS BIST TEST --------------- */
  14471. if (cd->multi_chip &&
  14472. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST) != 0) {
  14473. pt_debug(dev, DL_INFO,
  14474. "%s: ----- Start Slave BUS BIST -----", __func__);
  14475. slave_bus_toggled = 0xFF;
  14476. rc = pt_bist_slave_bus_test(dev, &slave_irq_toggled,
  14477. &slave_bus_toggled, slave_bus_err_str);
  14478. }
  14479. print_results:
  14480. /* Restore PIP command timeout */
  14481. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  14482. /*
  14483. * We're done! - Perform a hard XRES toggle, allowing BL
  14484. * to load FW if there is any in Flash
  14485. */
  14486. mutex_lock(&cd->system_lock);
  14487. cd->hid_reset_cmd_state = 0;
  14488. mutex_unlock(&cd->system_lock);
  14489. pt_debug(dev, DL_INFO,
  14490. "%s: TTDL BIST Complete - Final reset\n", __func__);
  14491. if (cd->flashless_dut) {
  14492. /*
  14493. * For Flashless solution, FW update is triggered after BL is
  14494. * seen that several miliseconds delay is needed.
  14495. */
  14496. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - ALLOW\n",
  14497. __func__);
  14498. mutex_lock(&cd->system_lock);
  14499. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  14500. mutex_unlock(&cd->system_lock);
  14501. /* Reset DUT and wait 100ms to see if loader started */
  14502. pt_hw_hard_reset(cd);
  14503. msleep(100);
  14504. if (cd->fw_updating) {
  14505. pt_debug(dev, DL_INFO,
  14506. "%s: ----- BIST Wait FW Loading ----",
  14507. __func__);
  14508. rc = _pt_request_wait_for_enum_state(
  14509. dev, 4000, STARTUP_STATUS_COMPLETE);
  14510. }
  14511. } else {
  14512. if (cd->mode == PT_MODE_BOOTLOADER) {
  14513. rc = pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  14514. if (rc) {
  14515. pt_debug(dev, DL_ERROR,
  14516. "%s Failed to launch app\n", __func__);
  14517. rc = pt_hw_hard_reset(cd);
  14518. }
  14519. }
  14520. /*
  14521. * If FW exists the BL may have just started or will start soon,
  14522. * so the FW sentinel may be on it's way but with no FW it will
  14523. * not arrive, wait for it before deciding if we need to queue
  14524. * an enum.
  14525. */
  14526. rc = _pt_request_wait_for_enum_state(
  14527. dev, 400, STARTUP_STATUS_FW_RESET_SENTINEL);
  14528. if ((cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) &&
  14529. (cd->startup_status & STARTUP_STATUS_COMPLETE) == 0) {
  14530. pt_debug(dev, DL_INFO, "%s: ----- BIST Enum ----",
  14531. __func__);
  14532. pt_queue_enum(cd);
  14533. rc = _pt_request_wait_for_enum_state(
  14534. dev, 2000, STARTUP_STATUS_COMPLETE);
  14535. }
  14536. }
  14537. msleep(20);
  14538. /* --------------- PRINT OUT BIST RESULTS ---------------*/
  14539. pt_debug(dev, DL_INFO, "%s: ----- BIST Print Results ----", __func__);
  14540. pt_start_wd_timer(cd);
  14541. /* Canned print if any memory allocation issues */
  14542. if (!bus_err_str || !irq_err_str || !xres_err_str) {
  14543. ret = scnprintf(buf, strlen(buf),
  14544. "Status: %d\n"
  14545. "I2C (SDA,SCL): [UNTEST]\n"
  14546. "SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  14547. "IRQ connection: [UNTEST]\n"
  14548. "TP_XRES connection: [UNTEST]\n", -ENOMEM);
  14549. if (cd->multi_chip) {
  14550. ret += scnprintf(buf + ret, strlen(buf),
  14551. "I/P SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  14552. "I/P IRQ connection: [UNTEST]\n"
  14553. "I/P TP_XRES connection: [UNTEST]\n");
  14554. }
  14555. } else {
  14556. status = 0;
  14557. if (bus_toggled == 1)
  14558. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  14559. if (irq_toggled == 1)
  14560. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14561. if (xres_toggled == 1)
  14562. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14563. if (cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST)
  14564. status += bus_toggled;
  14565. if (cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST)
  14566. status += irq_toggled;
  14567. if (cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST)
  14568. status += xres_toggled;
  14569. pt_debug(dev, DL_ERROR, "%s: status = %d (%d,%d,%d)\n",
  14570. __func__, status, bus_toggled, irq_toggled,
  14571. xres_toggled);
  14572. if (cd->multi_chip) {
  14573. if (slave_irq_toggled == 1)
  14574. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  14575. if (slave_xres_toggled == 1)
  14576. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  14577. if (slave_bus_toggled == 1)
  14578. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  14579. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST)
  14580. status += slave_bus_toggled;
  14581. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST)
  14582. status += slave_irq_toggled;
  14583. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST)
  14584. status += slave_xres_toggled;
  14585. pt_debug(dev, DL_ERROR,
  14586. "%s: status = %d (%d,%d,%d,%d,%d,%d)\n",
  14587. __func__, status, bus_toggled, irq_toggled,
  14588. xres_toggled, slave_bus_toggled,
  14589. slave_irq_toggled, slave_xres_toggled);
  14590. }
  14591. if (cd->bus_ops->bustype == BUS_I2C)
  14592. i2c_toggled = bus_toggled;
  14593. else
  14594. spi_toggled = bus_toggled;
  14595. ret = scnprintf(buf, strlen(buf),
  14596. "Status: %d\n"
  14597. "I2C (SDA,SCL): %s %s\n"
  14598. "SPI (MISO,MOSI,CS,CLK): %s %s\n"
  14599. "IRQ connection: %s %s\n"
  14600. "TP_XRES connection: %s %s\n",
  14601. status == num_tests ? 0 : 1,
  14602. i2c_toggled == 0x0F ? "[UNTEST]" :
  14603. i2c_toggled == 1 ? "[ OK ]" : "[FAILED]",
  14604. i2c_toggled == 0x0F ? "" : bus_err_str,
  14605. spi_toggled == 0x0F ? "[UNTEST]" :
  14606. spi_toggled == 1 ? "[ OK ]" : "[FAILED]",
  14607. spi_toggled == 0x0F ? "" : bus_err_str,
  14608. irq_toggled == 0x0F ? "[UNTEST]" :
  14609. irq_toggled == 1 ? "[ OK ]" : "[FAILED]",
  14610. irq_err_str,
  14611. xres_toggled == 0x0F ? "[UNTEST]" :
  14612. xres_toggled == 1 ? "[ OK ]" : "[FAILED]",
  14613. xres_err_str);
  14614. if (cd->multi_chip) {
  14615. ret += scnprintf(buf + ret, strlen(buf),
  14616. "I/P SPI (MISO,MOSI,CS,CLK): %s %s\n"
  14617. "I/P IRQ connection: %s %s\n"
  14618. "I/P TP_XRES connection: %s %s\n",
  14619. slave_bus_toggled == 0x0F ? "[UNTEST]" :
  14620. slave_bus_toggled == 1 ? "[ OK ]" :
  14621. "[FAILED]", slave_bus_err_str,
  14622. slave_irq_toggled == 0x0F ? "[UNTEST]" :
  14623. slave_irq_toggled == 1 ? "[ OK ]" :
  14624. "[FAILED]", slave_irq_err_str,
  14625. slave_xres_toggled == 0x0F ? "[UNTEST]" :
  14626. slave_xres_toggled == 1 ? "[ OK ]" :
  14627. "[FAILED]", slave_xres_err_str);
  14628. }
  14629. }
  14630. /* Put TTDL back into a known state, issue a ttdl enum if needed */
  14631. pt_debug(dev, DL_INFO, "%s: Startup_status = 0x%04X\n",
  14632. __func__, cd->startup_status);
  14633. kfree(bus_err_str);
  14634. kfree(irq_err_str);
  14635. kfree(xres_err_str);
  14636. return ret;
  14637. }
  14638. /*******************************************************************************
  14639. * FUNCTION: pt_ttdl_bist_store
  14640. *
  14641. * SUMMARY: Store method for the ttdl_bist sysfs node.
  14642. *
  14643. * RETURN: Size of passed in buffer if successful
  14644. *
  14645. * PARAMETERS:
  14646. * *dev - pointer to device structure
  14647. * *attr - pointer to device attributes
  14648. * *buf - pointer to command buffer
  14649. * size - size of buf
  14650. ******************************************************************************/
  14651. static ssize_t pt_ttdl_bist_store(struct device *dev,
  14652. struct device_attribute *attr, const char *buf, size_t size)
  14653. {
  14654. struct pt_core_data *cd = dev_get_drvdata(dev);
  14655. u32 input_data[2] = {0};
  14656. int length;
  14657. int rc = 0;
  14658. /* Maximum input of one value */
  14659. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14660. ARRAY_SIZE(input_data));
  14661. if (length != 1) {
  14662. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14663. __func__);
  14664. rc = -EINVAL;
  14665. goto exit;
  14666. } else {
  14667. mutex_lock(&cd->system_lock);
  14668. cd->ttdl_bist_select = input_data[0];
  14669. mutex_unlock(&cd->system_lock);
  14670. }
  14671. exit:
  14672. if (rc)
  14673. return rc;
  14674. return size;
  14675. }
  14676. static DEVICE_ATTR(ttdl_bist, 0644, pt_ttdl_bist_show,
  14677. pt_ttdl_bist_store);
  14678. /*******************************************************************************
  14679. * FUNCTION: pt_flush_bus_store
  14680. *
  14681. * SUMMARY: Store method for the flush_bus sysfs node.
  14682. *
  14683. * RETURN: Size of passed in buffer if successful
  14684. *
  14685. * PARAMETERS:
  14686. * *dev - pointer to device structure
  14687. * *attr - pointer to device attributes
  14688. * *buf - pointer to command buffer
  14689. * size - size of buf
  14690. ******************************************************************************/
  14691. static ssize_t pt_flush_bus_store(struct device *dev,
  14692. struct device_attribute *attr, const char *buf, size_t size)
  14693. {
  14694. struct pt_core_data *cd = dev_get_drvdata(dev);
  14695. u32 input_data[2] = {0};
  14696. int length;
  14697. int rc = 0;
  14698. /* Maximum input of one value */
  14699. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14700. ARRAY_SIZE(input_data));
  14701. if (length != 1) {
  14702. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14703. __func__);
  14704. rc = -EINVAL;
  14705. goto exit;
  14706. }
  14707. mutex_lock(&cd->system_lock);
  14708. if (input_data[0] == 0)
  14709. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  14710. else if (input_data[0] == 1)
  14711. cd->flush_bus_type = PT_FLUSH_BUS_FULL_256_READ;
  14712. else
  14713. rc = -EINVAL;
  14714. mutex_unlock(&cd->system_lock);
  14715. exit:
  14716. if (rc)
  14717. return rc;
  14718. return size;
  14719. }
  14720. /*******************************************************************************
  14721. * FUNCTION: pt_flush_bus_show
  14722. *
  14723. * SUMMARY: Show method for the flush_bus sysfs node that flushes the active bus
  14724. * based on either the size of the first two bytes or a blind 256 bytes.
  14725. *
  14726. * RETURN:
  14727. * 0 = success
  14728. * !0 = failure
  14729. *
  14730. * PARAMETERS:
  14731. * *dev - pointer to device structure
  14732. * *attr - pointer to device attributes
  14733. * *buf - pointer to output buffer
  14734. ******************************************************************************/
  14735. static ssize_t pt_flush_bus_show(struct device *dev,
  14736. struct device_attribute *attr, char *buf)
  14737. {
  14738. ssize_t ret = 0;
  14739. ssize_t bytes = 0;
  14740. struct pt_core_data *cd = dev_get_drvdata(dev);
  14741. mutex_lock(&cd->system_lock);
  14742. bytes = pt_flush_bus(cd, cd->flush_bus_type, NULL);
  14743. ret = scnprintf(buf, strlen(buf),
  14744. "Status: 0\n"
  14745. "%s: %zd\n",
  14746. "Bytes flushed", bytes);
  14747. mutex_unlock(&cd->system_lock);
  14748. return ret;
  14749. }
  14750. static DEVICE_ATTR(flush_bus, 0644, pt_flush_bus_show,
  14751. pt_flush_bus_store);
  14752. /*******************************************************************************
  14753. * FUNCTION: pt_pip2_ping_test_store
  14754. *
  14755. * SUMMARY: Store method for the pip2_ping_test sysfs node.
  14756. *
  14757. * NOTE: The max PIP2 packet size is 255 (payload for PING 247) however
  14758. * someone may want to test sending invalid packet lengths so any values
  14759. * up to 255 are allowed.
  14760. *
  14761. * RETURN: Size of passed in buffer if successful
  14762. *
  14763. * PARAMETERS:
  14764. * *dev - pointer to device structure
  14765. * *attr - pointer to device attributes
  14766. * *buf - pointer to command buffer
  14767. * size - size of buf
  14768. ******************************************************************************/
  14769. static ssize_t pt_pip2_ping_test_store(struct device *dev,
  14770. struct device_attribute *attr, const char *buf, size_t size)
  14771. {
  14772. struct pt_core_data *cd = dev_get_drvdata(dev);
  14773. u32 input_data[2];
  14774. int length;
  14775. int rc = 0;
  14776. /* Maximum input of one value */
  14777. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14778. ARRAY_SIZE(input_data));
  14779. if (length != 1) {
  14780. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14781. __func__);
  14782. rc = -EINVAL;
  14783. goto exit;
  14784. }
  14785. mutex_lock(&cd->system_lock);
  14786. if (input_data[1] >= 0 && input_data[0] <= PT_MAX_PIP2_MSG_SIZE)
  14787. cd->ping_test_size = input_data[0];
  14788. else
  14789. rc = -EINVAL;
  14790. mutex_unlock(&cd->system_lock);
  14791. exit:
  14792. if (rc)
  14793. return rc;
  14794. return size;
  14795. }
  14796. /*******************************************************************************
  14797. * FUNCTION: pt_ping_test_show
  14798. *
  14799. * SUMMARY: Show method for the ping_test_show sysfs node that sends the PIP2
  14800. * PING command and ramps up the optional payload from 0 to
  14801. * ping_test_size.
  14802. * The max payload size is 247:
  14803. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  14804. *
  14805. * RETURN:
  14806. * 0 = success
  14807. * !0 = failure
  14808. *
  14809. * PARAMETERS:
  14810. * *dev - pointer to device structure
  14811. * *attr - pointer to device attributes
  14812. * *buf - pointer to output buffer
  14813. ******************************************************************************/
  14814. static ssize_t pt_pip2_ping_test_show(struct device *dev,
  14815. struct device_attribute *attr, char *buf)
  14816. {
  14817. struct pt_core_data *cd = dev_get_drvdata(dev);
  14818. ssize_t ret;
  14819. int last_packet_size;
  14820. int rc = 0;
  14821. rc = pt_pip2_ping_test(dev, cd->ping_test_size, &last_packet_size);
  14822. if (rc) {
  14823. ret = scnprintf(buf, strlen(buf), "Status: %d\n", rc);
  14824. return ret;
  14825. }
  14826. ret = scnprintf(buf, strlen(buf),
  14827. "Status: %d\n"
  14828. "PING payload test passed with packet sizes 0 - %d\n",
  14829. (last_packet_size == cd->ping_test_size ? 0 : 1),
  14830. last_packet_size);
  14831. return ret;
  14832. }
  14833. /*******************************************************************************
  14834. * FUNCTION: pt_t_refresh_store
  14835. *
  14836. * SUMMARY: Store method for the t-refresh sysfs node that will takes a passed
  14837. * in integer as the number of interrupts to count. A timer is started to
  14838. * calculate the total time it takes to see that number of interrupts.
  14839. *
  14840. * RETURN: Size of passed in buffer if successful
  14841. *
  14842. * PARAMETERS:
  14843. * *dev - pointer to device structure
  14844. * *attr - pointer to device attributes
  14845. * *buf - pointer to buffer that hold the command parameters
  14846. * size - size of buf
  14847. ******************************************************************************/
  14848. static ssize_t pt_t_refresh_store(struct device *dev,
  14849. struct device_attribute *attr, const char *buf, size_t size)
  14850. {
  14851. struct pt_core_data *cd = dev_get_drvdata(dev);
  14852. u32 input_data[2];
  14853. int length;
  14854. int rc = 0;
  14855. /* Maximum input of one value */
  14856. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14857. ARRAY_SIZE(input_data));
  14858. if (length != 1) {
  14859. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14860. __func__);
  14861. rc = -EINVAL;
  14862. goto exit;
  14863. }
  14864. mutex_lock(&cd->system_lock);
  14865. pt_debug(dev, DL_INFO, "%s: Input value: %d\n", __func__,
  14866. input_data[0]);
  14867. if (input_data[0] >= 0 && input_data[0] <= 1000) {
  14868. cd->t_refresh_total = input_data[0];
  14869. cd->t_refresh_count = 0;
  14870. cd->t_refresh_active = 1;
  14871. } else {
  14872. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  14873. rc = -EINVAL;
  14874. }
  14875. mutex_unlock(&cd->system_lock);
  14876. exit:
  14877. pt_debug(dev, DL_ERROR, "%s: rc = %d\n", __func__, rc);
  14878. if (rc)
  14879. return rc;
  14880. return size;
  14881. }
  14882. /*******************************************************************************
  14883. * FUNCTION: pt_t_refresh_show
  14884. *
  14885. * SUMMARY: Show method for the t-refresh sysfs node that will show the results
  14886. * of the T-Refresh timer counting the time it takes to see a user defined
  14887. * number of interrupts.
  14888. *
  14889. * RETURN:
  14890. * 0 = success
  14891. * !0 = failure
  14892. *
  14893. * PARAMETERS:
  14894. * *dev - pointer to device structure
  14895. * *attr - pointer to device attributes
  14896. * *buf - pointer to output buffer
  14897. ******************************************************************************/
  14898. static ssize_t pt_t_refresh_show(struct device *dev,
  14899. struct device_attribute *attr, char *buf)
  14900. {
  14901. ssize_t ret = 0;
  14902. struct pt_core_data *cd = dev_get_drvdata(dev);
  14903. u32 whole;
  14904. u16 fraction;
  14905. mutex_lock(&cd->system_lock);
  14906. /* Check if we have counted the number requested */
  14907. if (cd->t_refresh_count != cd->t_refresh_total) {
  14908. ret = scnprintf(buf, strlen(buf),
  14909. "Status: 0\n"
  14910. "%s: %d\n",
  14911. "Still counting... current IRQ count",
  14912. cd->t_refresh_count);
  14913. } else {
  14914. /* Ensure T-Refresh is de-activated */
  14915. cd->t_refresh_active = 0;
  14916. whole = cd->t_refresh_time / cd->t_refresh_count;
  14917. fraction = cd->t_refresh_time % cd->t_refresh_count;
  14918. fraction = fraction * 1000 / cd->t_refresh_count;
  14919. ret = scnprintf(buf, strlen(buf),
  14920. "Status: 0\n"
  14921. "%s: %d\n"
  14922. "%s: %d\n"
  14923. "%s: %d\n"
  14924. "%s: %d.%02d\n",
  14925. "Requested IRQ Count ", cd->t_refresh_total,
  14926. "IRQ Counted ", cd->t_refresh_count,
  14927. "Total Time Elapsed (ms) ", (int)cd->t_refresh_time,
  14928. "Average T-Refresh (ms) ", whole, fraction);
  14929. }
  14930. mutex_unlock(&cd->system_lock);
  14931. return ret;
  14932. }
  14933. /*******************************************************************************
  14934. * FUNCTION: pt_dut_status_show
  14935. *
  14936. * SUMMARY: Show method for DUT status sysfs node. Display DUT's scan state, and
  14937. * more items such as operation mode,easywake state are added in the future.
  14938. *
  14939. * RETURN: Char buffer with printed scan status information
  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_dut_status_show(struct device *dev,
  14947. struct device_attribute *attr, char *buf)
  14948. {
  14949. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  14950. u8 mode = PT_MODE_UNKNOWN;
  14951. char *outputstring[7] = {"BOOT", "SCANNING", "DEEP_SLEEP",
  14952. "TEST", "DEEP_STANDBY", "UNDEFINED", "n/a"};
  14953. struct pt_core_data *cd = dev_get_drvdata(dev);
  14954. ssize_t ret;
  14955. u16 calculated_crc = 0;
  14956. u16 stored_crc = 0;
  14957. u8 status;
  14958. int rc = 0;
  14959. /* In STANDBY the DUT will not repond to any PIP cmd */
  14960. if (cd->fw_sys_mode_in_standby_state) {
  14961. mode = PT_MODE_OPERATIONAL;
  14962. sys_mode = FW_SYS_MODE_DEEP_STANDBY;
  14963. goto print_limited_results;
  14964. }
  14965. /* Retrieve mode and FW system mode which can only be 0-4 */
  14966. rc = pt_get_fw_sys_mode(cd, &sys_mode, &mode);
  14967. if (rc || mode == PT_MODE_UNKNOWN) {
  14968. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  14969. "%s: %d\n"
  14970. "%s: n/a\n"
  14971. "%s: n/a\n"
  14972. "%s: n/a\n"
  14973. "%s: n/a\n",
  14974. "Status", rc,
  14975. "Active Exec ",
  14976. "FW System Mode ",
  14977. "Stored CRC ",
  14978. "Calculated CRC ");
  14979. return ret;
  14980. } else {
  14981. if (mode == PT_MODE_OPERATIONAL) {
  14982. if (sys_mode > FW_SYS_MODE_MAX)
  14983. sys_mode = FW_SYS_MODE_UNDEFINED;
  14984. if (sys_mode != FW_SYS_MODE_TEST)
  14985. goto print_limited_results;
  14986. rc = pt_pip_verify_config_block_crc_(cd,
  14987. PT_TCH_PARM_EBID, &status,
  14988. &calculated_crc, &stored_crc);
  14989. if (rc)
  14990. goto print_limited_results;
  14991. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  14992. "%s: %d\n"
  14993. "%s: %s\n"
  14994. "%s: %s\n"
  14995. "%s: 0x%04X\n"
  14996. "%s: 0x%04X\n",
  14997. "Status", rc,
  14998. "Active Exec ", "FW",
  14999. "FW System Mode ", outputstring[sys_mode],
  15000. "Stored CRC ", stored_crc,
  15001. "Calculated CRC ", calculated_crc);
  15002. return ret;
  15003. } else {
  15004. /* When in BL or unknon mode Active Exec is "n/a" */
  15005. sys_mode = FW_SYS_MODE_UNDEFINED + 1;
  15006. }
  15007. }
  15008. print_limited_results:
  15009. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15010. "%s: %d\n"
  15011. "%s: %s\n"
  15012. "%s: %s\n"
  15013. "%s: n/a\n"
  15014. "%s: n/a\n",
  15015. "Status", rc,
  15016. "Active Exec ",
  15017. mode == PT_MODE_OPERATIONAL ? "FW" : "BL",
  15018. "FW System Mode ", outputstring[sys_mode],
  15019. "Stored CRC ",
  15020. "Calculated CRC ");
  15021. return ret;
  15022. }
  15023. #endif /* TTDL_DIAGNOSTICS */
  15024. /*******************************************************************************
  15025. * Structures of sysfs attributes for all DUT dependent sysfs node
  15026. ******************************************************************************/
  15027. static struct attribute *early_attrs[] = {
  15028. &dev_attr_hw_version.attr,
  15029. &dev_attr_drv_version.attr,
  15030. &dev_attr_drv_ver.attr,
  15031. &dev_attr_fw_version.attr,
  15032. &dev_attr_sysinfo.attr,
  15033. &dev_attr_pip2_cmd_rsp.attr,
  15034. &dev_attr_command.attr,
  15035. &dev_attr_drv_debug.attr,
  15036. &dev_attr_hw_reset.attr,
  15037. &dev_attr_response.attr,
  15038. &dev_attr_ttdl_restart.attr,
  15039. #ifdef TTDL_DIAGNOSTICS
  15040. &dev_attr_ttdl_status.attr,
  15041. &dev_attr_pip2_enter_bl.attr,
  15042. &dev_attr_pip2_exit_bl.attr,
  15043. &dev_attr_err_gpio.attr,
  15044. &dev_attr_flush_bus.attr,
  15045. &dev_attr_ttdl_bist.attr,
  15046. #endif /* TTDL_DIAGNOSTICS */
  15047. NULL,
  15048. };
  15049. static struct attribute_group early_attr_group = {
  15050. .attrs = early_attrs,
  15051. };
  15052. static struct device_attribute pip2_attributes[] = {
  15053. __ATTR(pip2_version, 0444, pt_pip2_version_show, NULL),
  15054. __ATTR(pip2_gpio_read, 0444, pt_pip2_gpio_read_show, NULL),
  15055. #ifdef TTDL_DIAGNOSTICS
  15056. __ATTR(pip2_bin_hdr, 0444, pt_pip2_bin_hdr_show, NULL),
  15057. __ATTR(pip2_ping_test, 0644, pt_pip2_ping_test_show,
  15058. pt_pip2_ping_test_store),
  15059. #endif
  15060. };
  15061. static struct device_attribute attributes[] = {
  15062. __ATTR(dut_debug, 0644,
  15063. pt_dut_debug_show, pt_drv_debug_store),
  15064. __ATTR(sleep_status, 0444, pt_sleep_status_show, NULL),
  15065. __ATTR(panel_id, 0444, pt_panel_id_show, NULL),
  15066. __ATTR(get_param, 0644,
  15067. pt_get_param_show, pt_get_param_store),
  15068. #ifdef EASYWAKE_TSG6
  15069. __ATTR(easy_wakeup_gesture, 0644, pt_easy_wakeup_gesture_show,
  15070. pt_easy_wakeup_gesture_store),
  15071. __ATTR(easy_wakeup_gesture_id, 0444,
  15072. pt_easy_wakeup_gesture_id_show, NULL),
  15073. __ATTR(easy_wakeup_gesture_data, 0444,
  15074. pt_easy_wakeup_gesture_data_show, NULL),
  15075. #endif
  15076. #ifdef TTDL_DIAGNOSTICS
  15077. __ATTR(platform_data, 0444, pt_platform_data_show, NULL),
  15078. __ATTR(drv_irq, 0644, pt_drv_irq_show, pt_drv_irq_store),
  15079. __ATTR(dut_status, 0444, pt_dut_status_show, NULL),
  15080. __ATTR(t_refresh, 0644, pt_t_refresh_show, pt_t_refresh_store),
  15081. #endif /* TTDL_DIAGNOSTICS */
  15082. };
  15083. /*******************************************************************************
  15084. * FUNCTION: add_sysfs_interfaces
  15085. *
  15086. * SUMMARY: Creates all DUT dependent sysfs nodes owned by the core
  15087. *
  15088. * RETURN:
  15089. * 0 = success
  15090. * !0 = failure
  15091. *
  15092. * PARAMETERS:
  15093. * *dev - pointer to device structure
  15094. ******************************************************************************/
  15095. static int add_sysfs_interfaces(struct device *dev)
  15096. {
  15097. int i;
  15098. int j = 0;
  15099. struct pt_core_data *cd = dev_get_drvdata(dev);
  15100. for (i = 0; i < ARRAY_SIZE(attributes); i++) {
  15101. if (device_create_file(dev, attributes + i))
  15102. goto undo;
  15103. }
  15104. pt_debug(dev, DL_INFO, "%s: Active DUT Generation: %d",
  15105. __func__, cd->active_dut_generation);
  15106. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  15107. for (j = 0; j < ARRAY_SIZE(pip2_attributes); j++) {
  15108. if (device_create_file(dev, pip2_attributes + j))
  15109. goto undo;
  15110. }
  15111. }
  15112. return 0;
  15113. undo:
  15114. for (i--; i >= 0; i--)
  15115. device_remove_file(dev, attributes + i);
  15116. for (j--; j >= 0; j--)
  15117. device_remove_file(dev, pip2_attributes + j);
  15118. pt_debug(dev, DL_ERROR, "%s: failed to create sysfs interface\n",
  15119. __func__);
  15120. return -ENODEV;
  15121. }
  15122. /*******************************************************************************
  15123. * FUNCTION: remove_sysfs_interfaces
  15124. *
  15125. * SUMMARY: Removes all DUT dependent sysfs nodes owned by the core
  15126. *
  15127. * RETURN: void
  15128. *
  15129. * PARAMETERS:
  15130. * *dev - pointer to device structure
  15131. ******************************************************************************/
  15132. static void remove_sysfs_interfaces(struct device *dev)
  15133. {
  15134. int i;
  15135. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  15136. device_remove_file(dev, attributes + i);
  15137. for (i = 0; i < ARRAY_SIZE(pip2_attributes); i++)
  15138. device_remove_file(dev, pip2_attributes + i);
  15139. }
  15140. /*******************************************************************************
  15141. * FUNCTION: remove_sysfs_and_modules
  15142. *
  15143. * SUMMARY: Removes all DUT dependent sysfs nodes and modules
  15144. *
  15145. * RETURN: void
  15146. *
  15147. * PARAMETERS:
  15148. * *dev - pointer to device structure
  15149. ******************************************************************************/
  15150. static void remove_sysfs_and_modules(struct device *dev)
  15151. {
  15152. struct pt_core_data *cd = dev_get_drvdata(dev);
  15153. /* Queued work should be removed before to release loader module */
  15154. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  15155. pt_release_modules(cd);
  15156. pt_btn_release(dev);
  15157. pt_mt_release(dev);
  15158. remove_sysfs_interfaces(dev);
  15159. }
  15160. static int pt_power_init(struct pt_core_data *cd, bool on)
  15161. {
  15162. int rc;
  15163. if (!on)
  15164. goto pwr_deinit;
  15165. cd->vdd = regulator_get(cd->dev, "vdd");
  15166. if (IS_ERR(cd->vdd)) {
  15167. rc = PTR_ERR(cd->vdd);
  15168. dev_err(cd->dev,
  15169. "Regulator get failed vdd rc=%d\n", rc);
  15170. return rc;
  15171. }
  15172. if (regulator_count_voltages(cd->vdd) > 0) {
  15173. rc = regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  15174. FT_VTG_MAX_UV);
  15175. if (rc) {
  15176. dev_err(cd->dev,
  15177. "Regulator set_vtg failed vdd rc=%d\n", rc);
  15178. goto reg_vdd_put;
  15179. }
  15180. }
  15181. cd->vcc_i2c = regulator_get(cd->dev, "vcc_i2c");
  15182. if (IS_ERR(cd->vcc_i2c)) {
  15183. rc = PTR_ERR(cd->vcc_i2c);
  15184. dev_err(cd->dev,
  15185. "Regulator get failed vcc_i2c rc=%d\n", rc);
  15186. goto reg_vdd_set_vtg;
  15187. }
  15188. if (regulator_count_voltages(cd->vcc_i2c) > 0) {
  15189. rc = regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  15190. FT_I2C_VTG_MAX_UV);
  15191. if (rc) {
  15192. dev_err(cd->dev,
  15193. "Regulator set_vtg failed vcc_i2c rc=%d\n", rc);
  15194. goto reg_vcc_i2c_put;
  15195. }
  15196. }
  15197. return 0;
  15198. reg_vcc_i2c_put:
  15199. regulator_put(cd->vcc_i2c);
  15200. reg_vdd_set_vtg:
  15201. if (regulator_count_voltages(cd->vdd) > 0)
  15202. regulator_set_voltage(cd->vdd, 0, FT_VTG_MAX_UV);
  15203. reg_vdd_put:
  15204. regulator_put(cd->vdd);
  15205. return rc;
  15206. pwr_deinit:
  15207. if (regulator_count_voltages(cd->vdd) > 0)
  15208. regulator_set_voltage(cd->vdd, 0, FT_VTG_MAX_UV);
  15209. regulator_put(cd->vdd);
  15210. if (regulator_count_voltages(cd->vcc_i2c) > 0)
  15211. regulator_set_voltage(cd->vcc_i2c, 0, FT_I2C_VTG_MAX_UV);
  15212. regulator_put(cd->vcc_i2c);
  15213. return 0;
  15214. }
  15215. static int pt_ts_pinctrl_init(struct pt_core_data *cd)
  15216. {
  15217. int retval;
  15218. /* Get pinctrl if target uses pinctrl */
  15219. cd->ts_pinctrl = devm_pinctrl_get(cd->dev);
  15220. if (IS_ERR_OR_NULL(cd->ts_pinctrl)) {
  15221. retval = PTR_ERR(cd->ts_pinctrl);
  15222. dev_dbg(cd->dev,
  15223. "Target does not use pinctrl %d\n", retval);
  15224. goto err_pinctrl_get;
  15225. }
  15226. cd->pinctrl_state_active
  15227. = pinctrl_lookup_state(cd->ts_pinctrl,
  15228. PINCTRL_STATE_ACTIVE);
  15229. if (IS_ERR_OR_NULL(cd->pinctrl_state_active)) {
  15230. retval = PTR_ERR(cd->pinctrl_state_active);
  15231. dev_err(cd->dev,
  15232. "Can not lookup %s pinstate %d\n",
  15233. PINCTRL_STATE_ACTIVE, retval);
  15234. goto err_pinctrl_lookup;
  15235. }
  15236. cd->pinctrl_state_suspend
  15237. = pinctrl_lookup_state(cd->ts_pinctrl,
  15238. PINCTRL_STATE_SUSPEND);
  15239. if (IS_ERR_OR_NULL(cd->pinctrl_state_suspend)) {
  15240. retval = PTR_ERR(cd->pinctrl_state_suspend);
  15241. dev_err(cd->dev,
  15242. "Can not lookup %s pinstate %d\n",
  15243. PINCTRL_STATE_SUSPEND, retval);
  15244. goto err_pinctrl_lookup;
  15245. }
  15246. cd->pinctrl_state_release
  15247. = pinctrl_lookup_state(cd->ts_pinctrl,
  15248. PINCTRL_STATE_RELEASE);
  15249. if (IS_ERR_OR_NULL(cd->pinctrl_state_release)) {
  15250. retval = PTR_ERR(cd->pinctrl_state_release);
  15251. dev_dbg(cd->dev,
  15252. "Can not lookup %s pinstate %d\n",
  15253. PINCTRL_STATE_RELEASE, retval);
  15254. }
  15255. return 0;
  15256. err_pinctrl_lookup:
  15257. devm_pinctrl_put(cd->ts_pinctrl);
  15258. err_pinctrl_get:
  15259. cd->ts_pinctrl = NULL;
  15260. return retval;
  15261. }
  15262. /*******************************************************************************
  15263. *******************************************************************************
  15264. * FUNCTION: pt_probe
  15265. *
  15266. * SUMMARY: Probe of the core module.
  15267. *
  15268. * NOTE: For the Parade Technologies development platform (PtSBC) the
  15269. * probe functionality is split into two functions; pt_probe() and
  15270. * pt_probe_complete(). the initial setup is done in this function which
  15271. * then creates a WORK task which runs after the probe timer expires. This
  15272. * ensures the I2C/SPI is up on the PtSBC in time for TTDL.
  15273. *
  15274. * RETURN:
  15275. * 0 = success
  15276. * !0 = failure
  15277. *
  15278. * PARAMETERS:
  15279. * *ops - pointer to the bus
  15280. * *dev - pointer to the device structure
  15281. * irq - IRQ
  15282. * xfer_buf_size - size of the buffer
  15283. ******************************************************************************/
  15284. int pt_probe(const struct pt_bus_ops *ops, struct device *dev,
  15285. u16 irq, size_t xfer_buf_size)
  15286. {
  15287. struct pt_core_data *cd;
  15288. struct pt_platform_data *pdata = dev_get_platdata(dev);
  15289. enum pt_atten_type type;
  15290. struct i2c_client *client = to_i2c_client(dev);
  15291. int rc = 0;
  15292. u8 pip_ver_major;
  15293. u8 pip_ver_minor;
  15294. u32 status = STARTUP_STATUS_START;
  15295. if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
  15296. pt_debug(dev, DL_ERROR, "%s: Missing platform data\n",
  15297. __func__);
  15298. rc = -ENODEV;
  15299. goto error_no_pdata;
  15300. }
  15301. if (pdata->core_pdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  15302. if (!pdata->core_pdata->power) {
  15303. pt_debug(dev, DL_ERROR,
  15304. "%s: Missing platform data function\n",
  15305. __func__);
  15306. rc = -ENODEV;
  15307. goto error_no_pdata;
  15308. }
  15309. }
  15310. /* get context and debug print buffers */
  15311. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  15312. if (!cd) {
  15313. rc = -ENOMEM;
  15314. goto error_alloc_data;
  15315. }
  15316. /* Initialize device info */
  15317. cd->dev = dev;
  15318. cd->pdata = pdata;
  15319. cd->cpdata = pdata->core_pdata;
  15320. cd->bus_ops = ops;
  15321. cd->debug_level = PT_INITIAL_DEBUG_LEVEL;
  15322. cd->show_timestamp = PT_INITIAL_SHOW_TIME_STAMP;
  15323. scnprintf(cd->core_id, 20, "%s%d", PT_CORE_NAME, core_number++);
  15324. cd->hw_detected = false;
  15325. cd->pip2_prot_active = false;
  15326. cd->pip2_send_user_cmd = false;
  15327. cd->bl_pip_ver_ready = false;
  15328. cd->app_pip_ver_ready = false;
  15329. cd->pip2_cmd_tag_seq = 0x08; /* PIP2 TAG=1 and 3 bit SEQ=0 */
  15330. cd->get_param_id = 0;
  15331. cd->watchdog_enabled = 0;
  15332. cd->startup_retry_count = 0;
  15333. cd->core_probe_complete = 0;
  15334. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  15335. cd->pip_cmd_timeout = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15336. cd->pip_cmd_timeout_default = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15337. cd->flashless_dut = 0;
  15338. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  15339. cd->bl_with_no_int = 0;
  15340. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  15341. cd->multi_chip = PT_FEATURE_DISABLE;
  15342. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  15343. if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP2_CAPABLE) {
  15344. cd->set_dut_generation = true;
  15345. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  15346. } else if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP1_ONLY) {
  15347. cd->set_dut_generation = true;
  15348. cd->active_dut_generation = DUT_PIP1_ONLY;
  15349. } else {
  15350. cd->set_dut_generation = false;
  15351. cd->active_dut_generation = DUT_UNKNOWN;
  15352. }
  15353. /* Initialize with platform data */
  15354. cd->watchdog_force_stop = cd->cpdata->watchdog_force_stop;
  15355. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  15356. cd->hid_cmd_state = 1;
  15357. cd->fw_updating = false;
  15358. cd->multi_chip = 0;
  15359. #ifdef TTDL_DIAGNOSTICS
  15360. cd->t_refresh_active = 0;
  15361. cd->t_refresh_count = 0;
  15362. cd->pip2_crc_error_count = 0;
  15363. cd->wd_xres_count = 0;
  15364. cd->bl_retry_packet_count = 0;
  15365. cd->file_erase_timeout_count = 0;
  15366. cd->show_tt_data = false;
  15367. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15368. cd->err_gpio = 0;
  15369. cd->err_gpio_type = PT_ERR_GPIO_NONE;
  15370. cd->ttdl_bist_select = 0x07;
  15371. cd->force_pip2_seq = 0;
  15372. #endif /* TTDL_DIAGNOSTICS */
  15373. memset(cd->pip2_us_file_path, 0, PT_MAX_PATH_SIZE);
  15374. memcpy(cd->pip2_us_file_path, PT_PIP2_BIN_FILE_PATH,
  15375. sizeof(PT_PIP2_BIN_FILE_PATH));
  15376. pt_init_hid_descriptor(&cd->hid_desc);
  15377. /* Read and store the descriptor lengths */
  15378. cd->hid_core.hid_report_desc_len =
  15379. le16_to_cpu(cd->hid_desc.report_desc_len);
  15380. cd->hid_core.hid_max_input_len =
  15381. le16_to_cpu(cd->hid_desc.max_input_len);
  15382. cd->hid_core.hid_max_output_len =
  15383. le16_to_cpu(cd->hid_desc.max_output_len);
  15384. /* Initialize mutexes and spinlocks */
  15385. mutex_init(&cd->module_list_lock);
  15386. mutex_init(&cd->system_lock);
  15387. mutex_init(&cd->sysfs_lock);
  15388. mutex_init(&cd->ttdl_restart_lock);
  15389. mutex_init(&cd->firmware_class_lock);
  15390. spin_lock_init(&cd->spinlock);
  15391. /* Initialize module list */
  15392. INIT_LIST_HEAD(&cd->module_list);
  15393. /* Initialize attention lists */
  15394. for (type = 0; type < PT_ATTEN_NUM_ATTEN; type++)
  15395. INIT_LIST_HEAD(&cd->atten_list[type]);
  15396. /* Initialize parameter list */
  15397. INIT_LIST_HEAD(&cd->param_list);
  15398. /* Initialize wait queue */
  15399. init_waitqueue_head(&cd->wait_q);
  15400. rc = pt_ts_pinctrl_init(cd);
  15401. if (!rc && cd->ts_pinctrl) {
  15402. /*
  15403. * Pinctrl handle is optional. If pinctrl handle is found
  15404. * let pins to be configured in active state. If not
  15405. * found continue further without error.
  15406. */
  15407. rc = pinctrl_select_state(cd->ts_pinctrl,
  15408. cd->pinctrl_state_active);
  15409. if (rc < 0)
  15410. dev_err(&client->dev, "failed to select pin to active state\n");
  15411. }
  15412. rc = pt_power_init(cd, true);
  15413. if (rc < 0)
  15414. dev_err(&client->dev, "failed to cyttsp5_power_init\n");
  15415. rc = regulator_enable(cd->vdd);
  15416. if (rc) {
  15417. dev_err(dev, "Regulator vdd enable failed rc=%d\n", rc);
  15418. goto error_power;
  15419. }
  15420. rc = regulator_enable(cd->vcc_i2c);
  15421. if (rc) {
  15422. dev_err(dev, "Regulator vcc_i2c enable failed rc=%d\n", rc);
  15423. regulator_disable(cd->vdd);
  15424. goto error_power;
  15425. }
  15426. /* Initialize works */
  15427. INIT_WORK(&cd->enum_work, pt_enum_work_function);
  15428. INIT_WORK(&cd->ttdl_restart_work, pt_restart_work_function);
  15429. INIT_WORK(&cd->watchdog_work, pt_watchdog_work);
  15430. /* Initialize HID specific data */
  15431. cd->hid_core.hid_vendor_id = (cd->cpdata->vendor_id) ?
  15432. cd->cpdata->vendor_id : HID_VENDOR_ID;
  15433. cd->hid_core.hid_product_id = (cd->cpdata->product_id) ?
  15434. cd->cpdata->product_id : HID_APP_PRODUCT_ID;
  15435. cd->hid_core.hid_desc_register =
  15436. cpu_to_le16(cd->cpdata->hid_desc_register);
  15437. /* Set platform easywake value */
  15438. cd->easy_wakeup_gesture = cd->cpdata->easy_wakeup_gesture;
  15439. /* Set platform panel_id value */
  15440. cd->panel_id_support = cd->cpdata->panel_id_support;
  15441. if (cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO)
  15442. /* Set Panel ID to default to 0 */
  15443. cd->pid_for_loader = PT_PANEL_ID_DEFAULT;
  15444. else
  15445. /* Set Panel ID to Not Enabled */
  15446. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  15447. /* Initialize hw_version default to FFFF.FFFF.FF */
  15448. snprintf(cd->hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  15449. dev_set_drvdata(dev, cd);
  15450. /* PtSBC builds will call this function in pt_probe_complete() */
  15451. pt_add_core(dev);
  15452. rc = sysfs_create_group(&dev->kobj, &early_attr_group);
  15453. if (rc)
  15454. pt_debug(cd->dev, DL_ERROR, "%s:create early attrs failed\n",
  15455. __func__);
  15456. /*
  15457. * Save the pointer to a global value, which will be used
  15458. * in ttdl_restart function
  15459. */
  15460. cd->bus_ops = ops;
  15461. /*
  15462. * When the IRQ GPIO is not direclty accessible and no function is
  15463. * defined to get the IRQ status, the IRQ passed in must be assigned
  15464. * directly as the gpio_to_irq will not work. e.g. CHROMEOS
  15465. */
  15466. if (!cd->cpdata->irq_stat) {
  15467. cd->irq = irq;
  15468. pt_debug(cd->dev, DL_ERROR, "%s:No irq_stat, Set cd->irq = %d\n",
  15469. __func__, cd->irq);
  15470. }
  15471. /* Call platform init function before setting up the GPIO's */
  15472. if (cd->cpdata->init) {
  15473. pt_debug(cd->dev, DL_INFO, "%s: Init HW\n", __func__);
  15474. rc = cd->cpdata->init(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  15475. } else {
  15476. pt_debug(cd->dev, DL_ERROR, "%s: No HW INIT function\n",
  15477. __func__);
  15478. rc = 0;
  15479. }
  15480. if (rc < 0) {
  15481. pt_debug(cd->dev, DL_ERROR, "%s: HW Init fail r=%d\n",
  15482. __func__, rc);
  15483. }
  15484. /* Power on any needed regulator(s) */
  15485. if (cd->cpdata->setup_power) {
  15486. pt_debug(cd->dev, DL_INFO, "%s: Device power on!\n", __func__);
  15487. rc = cd->cpdata->setup_power(cd->cpdata,
  15488. PT_MT_POWER_ON, cd->dev);
  15489. } else {
  15490. pt_debug(cd->dev, DL_ERROR, "%s: No setup power function\n",
  15491. __func__);
  15492. rc = 0;
  15493. }
  15494. if (rc < 0)
  15495. pt_debug(cd->dev, DL_ERROR, "%s: Setup power on fail r=%d\n",
  15496. __func__, rc);
  15497. #ifdef TTDL_DIAGNOSTICS
  15498. cd->watchdog_irq_stuck_count = 0;
  15499. cd->bus_transmit_error_count = 0;
  15500. #endif /* TTDL_DIAGNOSTICS */
  15501. if (cd->cpdata->detect) {
  15502. pt_debug(cd->dev, DL_INFO, "%s: Detect HW\n", __func__);
  15503. rc = cd->cpdata->detect(cd->cpdata, cd->dev,
  15504. pt_platform_detect_read);
  15505. if (!rc) {
  15506. cd->hw_detected = true;
  15507. pt_debug(cd->dev, DL_INFO,
  15508. "%s: HW detected\n", __func__);
  15509. } else {
  15510. cd->hw_detected = false;
  15511. pt_debug(cd->dev, DL_INFO,
  15512. "%s: No HW detected\n", __func__);
  15513. rc = -ENODEV;
  15514. goto error_detect;
  15515. }
  15516. } else {
  15517. pt_debug(dev, DL_ERROR,
  15518. "%s: PARADE No HW detect function pointer\n",
  15519. __func__);
  15520. /*
  15521. * "hw_reset" is not needed in the "if" statement,
  15522. * because "hw_reset" is already included in "hw_detect"
  15523. * function.
  15524. */
  15525. rc = pt_hw_hard_reset(cd);
  15526. if (rc)
  15527. pt_debug(cd->dev, DL_ERROR,
  15528. "%s: FAILED to execute HARD reset\n",
  15529. __func__);
  15530. }
  15531. if (cd->cpdata->setup_irq) {
  15532. pt_debug(cd->dev, DL_INFO, "%s: setup IRQ\n", __func__);
  15533. rc = cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_REG, cd->dev);
  15534. if (rc) {
  15535. pt_debug(dev, DL_ERROR,
  15536. "%s: Error, couldn't setup IRQ\n", __func__);
  15537. goto error_setup_irq;
  15538. }
  15539. } else {
  15540. pt_debug(dev, DL_ERROR,
  15541. "%s: IRQ function pointer not setup\n",
  15542. __func__);
  15543. goto error_setup_irq;
  15544. }
  15545. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  15546. setup_timer(&cd->watchdog_timer, pt_watchdog_timer,
  15547. (unsigned long)cd);
  15548. #else
  15549. timer_setup(&cd->watchdog_timer, pt_watchdog_timer, 0);
  15550. #endif
  15551. pt_stop_wd_timer(cd);
  15552. #ifdef TTHE_TUNER_SUPPORT
  15553. mutex_init(&cd->tthe_lock);
  15554. cd->tthe_debugfs = debugfs_create_file(PT_TTHE_TUNER_FILE_NAME,
  15555. 0644, NULL, cd, &tthe_debugfs_fops);
  15556. #endif
  15557. rc = device_init_wakeup(dev, 1);
  15558. if (rc < 0)
  15559. pt_debug(dev, DL_ERROR, "%s: Error, device_init_wakeup rc:%d\n",
  15560. __func__, rc);
  15561. pm_runtime_get_noresume(dev);
  15562. pm_runtime_set_active(dev);
  15563. pm_runtime_enable(dev);
  15564. /* If IRQ asserted, read out all from buffer to release INT pin */
  15565. if (cd->cpdata->irq_stat) {
  15566. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  15567. } else {
  15568. /* Force a read in case the reset sentinel already arrived */
  15569. rc = pt_read_input(cd);
  15570. if (!rc)
  15571. pt_parse_input(cd);
  15572. }
  15573. /* Without sleep DUT is not ready and will NAK the first write */
  15574. msleep(150);
  15575. /* Attempt to set the DUT generation if not yet set */
  15576. if (cd->active_dut_generation == DUT_UNKNOWN) {
  15577. if (cd->bl_pip_ver_ready ||
  15578. (cd->app_pip_ver_ready &&
  15579. IS_PIP_VER_GE(&cd->sysinfo, 1, 12))) {
  15580. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  15581. pt_debug(dev, DL_INFO, "%s: dut generation is %d\n",
  15582. __func__, cd->active_dut_generation);
  15583. } else {
  15584. rc = _pt_detect_dut_generation(cd->dev,
  15585. &status, &cd->active_dut_generation,
  15586. &cd->mode);
  15587. if ((cd->active_dut_generation == DUT_UNKNOWN)
  15588. || rc) {
  15589. pt_debug(cd->dev, DL_ERROR,
  15590. " === DUT Gen unknown, Skip Enum ===\n");
  15591. goto skip_enum;
  15592. }
  15593. }
  15594. }
  15595. _pt_request_active_pip_protocol(cd->dev, PT_CORE_CMD_PROTECTED,
  15596. &pip_ver_major, &pip_ver_minor);
  15597. if (pip_ver_major == 2) {
  15598. cd->bl_pip_ver_ready = true;
  15599. pt_debug(dev, DL_ERROR,
  15600. " === PIP2.%d Detected, Attempt to launch APP ===\n",
  15601. pip_ver_minor);
  15602. cd->hw_detected = true;
  15603. } else if (pip_ver_major == 1) {
  15604. cd->app_pip_ver_ready = true;
  15605. pt_debug(dev, DL_ERROR,
  15606. " === PIP1.%d Detected ===\n", pip_ver_minor);
  15607. cd->hw_detected = true;
  15608. } else {
  15609. cd->sysinfo.ttdata.pip_ver_major = 0;
  15610. cd->sysinfo.ttdata.pip_ver_minor = 0;
  15611. cd->app_pip_ver_ready = false;
  15612. cd->hw_detected = false;
  15613. pt_debug(dev, DL_ERROR,
  15614. " === PIP Version Not Detected, Skip Enum ===\n");
  15615. /* For legacy DUTS proceed, enum will attempt to launch app */
  15616. if (cd->active_dut_generation != DUT_PIP1_ONLY)
  15617. goto skip_enum;
  15618. }
  15619. rc = pt_enum_with_dut(cd, false, &status);
  15620. pt_debug(dev, DL_ERROR, "%s: cd->startup_status=0x%04X status=0x%04X\n",
  15621. __func__, cd->startup_status, status);
  15622. if (rc == -ENODEV) {
  15623. pt_debug(cd->dev, DL_ERROR,
  15624. "%s: Enumeration Failed r=%d\n", __func__, rc);
  15625. /* For PtSBC don't error out, allow TTDL to stay up */
  15626. goto error_after_startup;
  15627. }
  15628. /* Suspend scanning until probe is complete to avoid asyc touches */
  15629. pt_pip_suspend_scanning_(cd);
  15630. if (cd->hw_detected) {
  15631. pt_debug(dev, DL_INFO, "%s: Add sysfs interfaces\n",
  15632. __func__);
  15633. rc = add_sysfs_interfaces(dev);
  15634. if (rc < 0) {
  15635. pt_debug(dev, DL_ERROR,
  15636. "%s: Error, fail sysfs init\n", __func__);
  15637. goto error_after_startup;
  15638. }
  15639. } else {
  15640. pt_debug(dev, DL_ERROR,
  15641. "%s: No HW detected, sysfs interfaces not added\n",
  15642. __func__);
  15643. }
  15644. skip_enum:
  15645. pm_runtime_put_sync(dev);
  15646. pt_debug(dev, DL_INFO, "%s: Probe: MT, BTN\n", __func__);
  15647. rc = pt_mt_probe(dev);
  15648. if (rc < 0) {
  15649. pt_debug(dev, DL_ERROR, "%s: Error, fail mt probe\n",
  15650. __func__);
  15651. goto error_after_sysfs_create;
  15652. }
  15653. rc = pt_btn_probe(dev);
  15654. if (rc < 0) {
  15655. pt_debug(dev, DL_ERROR, "%s: Error, fail btn probe\n",
  15656. __func__);
  15657. goto error_after_startup_mt;
  15658. }
  15659. pt_probe_modules(cd);
  15660. #ifdef CONFIG_HAS_EARLYSUSPEND
  15661. pt_setup_early_suspend(cd);
  15662. #elif defined(CONFIG_FB)
  15663. pt_setup_fb_notifier(cd);
  15664. #endif
  15665. #ifdef NEED_SUSPEND_NOTIFIER
  15666. cd->pm_notifier.notifier_call = pt_pm_notifier;
  15667. register_pm_notifier(&cd->pm_notifier);
  15668. #endif
  15669. pt_pip_resume_scanning_(cd);
  15670. mutex_lock(&cd->system_lock);
  15671. cd->startup_status |= status;
  15672. cd->core_probe_complete = 1;
  15673. mutex_unlock(&cd->system_lock);
  15674. pt_debug(dev, DL_ERROR, "%s: TTDL Core Probe Completed Successfully\n",
  15675. __func__);
  15676. return 0;
  15677. error_after_startup_mt:
  15678. pr_err("%s PARADE error_after_startup_mt\n", __func__);
  15679. pt_mt_release(dev);
  15680. error_after_sysfs_create:
  15681. pr_err("%s PARADE error_after_sysfs_create\n", __func__);
  15682. pm_runtime_disable(dev);
  15683. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  15684. device_wakeup_disable(dev);
  15685. #endif
  15686. device_init_wakeup(dev, 0);
  15687. cancel_work_sync(&cd->enum_work);
  15688. pt_stop_wd_timer(cd);
  15689. pt_free_si_ptrs(cd);
  15690. remove_sysfs_interfaces(dev);
  15691. error_after_startup:
  15692. pr_err("%s PARADE error_after_startup\n", __func__);
  15693. del_timer(&cd->watchdog_timer);
  15694. if (cd->cpdata->setup_irq)
  15695. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  15696. error_setup_irq:
  15697. error_detect:
  15698. if (cd->cpdata->init)
  15699. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  15700. if (cd->cpdata->setup_power)
  15701. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  15702. sysfs_remove_group(&dev->kobj, &early_attr_group);
  15703. pt_del_core(dev);
  15704. dev_set_drvdata(dev, NULL);
  15705. error_power:
  15706. pt_power_init(cd, false);
  15707. kfree(cd);
  15708. error_alloc_data:
  15709. error_no_pdata:
  15710. pr_err("%s failed.\n", __func__);
  15711. return rc;
  15712. }
  15713. EXPORT_SYMBOL_GPL(pt_probe);
  15714. /*******************************************************************************
  15715. * FUNCTION: pt_release
  15716. *
  15717. * SUMMARY: This function does the following cleanup:
  15718. * - Releases all probed modules
  15719. * - Stops the watchdog
  15720. * - Cancels all pending work tasks
  15721. * - Removes all created sysfs nodes
  15722. * - Removes all created debugfs nodes
  15723. * - Frees the IRQ
  15724. * - Deletes the core
  15725. * - Frees all pointers and HID reports
  15726. *
  15727. * RETURN:
  15728. * 0 = success
  15729. * !0 = failure
  15730. *
  15731. * PARAMETERS:
  15732. * *cd - pointer to the core data structure
  15733. ******************************************************************************/
  15734. int pt_release(struct pt_core_data *cd)
  15735. {
  15736. struct device *dev = cd->dev;
  15737. /*
  15738. * Suspend the device before freeing the startup_work and stopping
  15739. * the watchdog since sleep function restarts watchdog on failure
  15740. */
  15741. pm_runtime_suspend(dev);
  15742. pm_runtime_disable(dev);
  15743. /*
  15744. * Any 'work' that can trigger a new thread should be canceled first.
  15745. * The watchdog is also stopped again because another thread could have
  15746. * restarted it. The irq_work is cancelled last because it is used for
  15747. * all I2C/SPI communication.
  15748. */
  15749. pt_stop_wd_timer(cd);
  15750. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  15751. cancel_work_sync(&cd->ttdl_restart_work);
  15752. cancel_work_sync(&cd->enum_work);
  15753. pt_stop_wd_timer(cd);
  15754. pt_release_modules(cd);
  15755. pt_proximity_release(dev);
  15756. pt_btn_release(dev);
  15757. pt_mt_release(dev);
  15758. #ifdef CONFIG_HAS_EARLYSUSPEND
  15759. unregister_early_suspend(&cd->es);
  15760. #elif defined(CONFIG_FB)
  15761. fb_unregister_client(&cd->fb_notifier);
  15762. #endif
  15763. #ifdef NEED_SUSPEND_NOTIFIER
  15764. unregister_pm_notifier(&cd->pm_notifier);
  15765. #endif
  15766. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  15767. device_wakeup_disable(dev);
  15768. #endif
  15769. device_init_wakeup(dev, 0);
  15770. #ifdef TTHE_TUNER_SUPPORT
  15771. mutex_lock(&cd->tthe_lock);
  15772. cd->tthe_exit = 1;
  15773. wake_up(&cd->wait_q);
  15774. mutex_unlock(&cd->tthe_lock);
  15775. debugfs_remove(cd->tthe_debugfs);
  15776. #endif
  15777. sysfs_remove_group(&dev->kobj, &early_attr_group);
  15778. remove_sysfs_interfaces(dev);
  15779. disable_irq_nosync(cd->irq);
  15780. if (cd->cpdata->setup_irq)
  15781. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  15782. if (cd->cpdata->init)
  15783. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  15784. if (cd->cpdata->setup_power)
  15785. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  15786. dev_set_drvdata(dev, NULL);
  15787. pt_del_core(dev);
  15788. pt_free_si_ptrs(cd);
  15789. kfree(cd);
  15790. return 0;
  15791. }
  15792. EXPORT_SYMBOL_GPL(pt_release);
  15793. MODULE_LICENSE("GPL");
  15794. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product Core Driver");
  15795. MODULE_AUTHOR("Parade Technologies <[email protected]>");