0004-ipts.patch 125 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502
  1. From cea5cd0e4ff93372adcdc46a9741d2a037fc4c68 Mon Sep 17 00:00:00 2001
  2. From: Dorian Stoll <dorian.stoll@tmsp.io>
  3. Date: Thu, 30 Jul 2020 13:21:53 +0200
  4. Subject: [PATCH] misc: mei: Add missing IPTS device IDs
  5. Patchset: ipts
  6. ---
  7. drivers/misc/mei/hw-me-regs.h | 1 +
  8. drivers/misc/mei/pci-me.c | 1 +
  9. 2 files changed, 2 insertions(+)
  10. diff --git a/drivers/misc/mei/hw-me-regs.h b/drivers/misc/mei/hw-me-regs.h
  11. index bdc65d50b945..08723c01d727 100644
  12. --- a/drivers/misc/mei/hw-me-regs.h
  13. +++ b/drivers/misc/mei/hw-me-regs.h
  14. @@ -92,6 +92,7 @@
  15. #define MEI_DEV_ID_CDF 0x18D3 /* Cedar Fork */
  16. #define MEI_DEV_ID_ICP_LP 0x34E0 /* Ice Lake Point LP */
  17. +#define MEI_DEV_ID_ICP_LP_3 0x34E4 /* Ice Lake Point LP 3 (iTouch) */
  18. #define MEI_DEV_ID_ICP_N 0x38E0 /* Ice Lake Point N */
  19. #define MEI_DEV_ID_JSP_N 0x4DE0 /* Jasper Lake Point N */
  20. diff --git a/drivers/misc/mei/pci-me.c b/drivers/misc/mei/pci-me.c
  21. index 676d566f38dd..6b37dd1f8b2a 100644
  22. --- a/drivers/misc/mei/pci-me.c
  23. +++ b/drivers/misc/mei/pci-me.c
  24. @@ -97,6 +97,7 @@ static const struct pci_device_id mei_me_pci_tbl[] = {
  25. {MEI_PCI_DEVICE(MEI_DEV_ID_CMP_H_3, MEI_ME_PCH8_ITOUCH_CFG)},
  26. {MEI_PCI_DEVICE(MEI_DEV_ID_ICP_LP, MEI_ME_PCH12_CFG)},
  27. + {MEI_PCI_DEVICE(MEI_DEV_ID_ICP_LP_3, MEI_ME_PCH12_CFG)},
  28. {MEI_PCI_DEVICE(MEI_DEV_ID_ICP_N, MEI_ME_PCH12_CFG)},
  29. {MEI_PCI_DEVICE(MEI_DEV_ID_TGP_LP, MEI_ME_PCH15_CFG)},
  30. --
  31. 2.41.0
  32. From b2b9508281521369adb6a45996ec1130b10ce172 Mon Sep 17 00:00:00 2001
  33. From: Liban Hannan <liban.p@gmail.com>
  34. Date: Tue, 12 Apr 2022 23:31:12 +0100
  35. Subject: [PATCH] iommu: ipts: use IOMMU passthrough mode for IPTS
  36. Adds a quirk so that IOMMU uses passthrough mode for the IPTS device.
  37. Otherwise, when IOMMU is enabled, IPTS produces DMAR errors like:
  38. DMAR: [DMA Read NO_PASID] Request device [00:16.4] fault addr
  39. 0x104ea3000 [fault reason 0x06] PTE Read access is not set
  40. This is very similar to the bug described at:
  41. https://bugs.launchpad.net/bugs/1958004
  42. Fixed with the following patch which this patch basically copies:
  43. https://launchpadlibrarian.net/586396847/43255ca.diff
  44. Patchset: ipts
  45. ---
  46. drivers/iommu/intel/iommu.c | 24 ++++++++++++++++++++++++
  47. 1 file changed, 24 insertions(+)
  48. diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
  49. index b871a6afd803..0d4542b7365d 100644
  50. --- a/drivers/iommu/intel/iommu.c
  51. +++ b/drivers/iommu/intel/iommu.c
  52. @@ -37,6 +37,8 @@
  53. #define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
  54. #define IS_USB_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_SERIAL_USB)
  55. #define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
  56. +#define IS_IPTS(pdev) ((pdev)->vendor == PCI_VENDOR_ID_INTEL && \
  57. + ((pdev)->device == 0x9d3e))
  58. #define IS_AZALIA(pdev) ((pdev)->vendor == 0x8086 && (pdev)->device == 0x3a3e)
  59. #define IOAPIC_RANGE_START (0xfee00000)
  60. @@ -287,12 +289,14 @@ int intel_iommu_enabled = 0;
  61. EXPORT_SYMBOL_GPL(intel_iommu_enabled);
  62. static int dmar_map_gfx = 1;
  63. +static int dmar_map_ipts = 1;
  64. static int intel_iommu_superpage = 1;
  65. static int iommu_identity_mapping;
  66. static int iommu_skip_te_disable;
  67. #define IDENTMAP_GFX 2
  68. #define IDENTMAP_AZALIA 4
  69. +#define IDENTMAP_IPTS 16
  70. const struct iommu_ops intel_iommu_ops;
  71. @@ -2560,6 +2564,9 @@ static int device_def_domain_type(struct device *dev)
  72. if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev))
  73. return IOMMU_DOMAIN_IDENTITY;
  74. +
  75. + if ((iommu_identity_mapping & IDENTMAP_IPTS) && IS_IPTS(pdev))
  76. + return IOMMU_DOMAIN_IDENTITY;
  77. }
  78. return 0;
  79. @@ -2867,6 +2874,9 @@ static int __init init_dmars(void)
  80. if (!dmar_map_gfx)
  81. iommu_identity_mapping |= IDENTMAP_GFX;
  82. + if (!dmar_map_ipts)
  83. + iommu_identity_mapping |= IDENTMAP_IPTS;
  84. +
  85. check_tylersburg_isoch();
  86. ret = si_domain_init(hw_pass_through);
  87. @@ -4778,6 +4788,17 @@ static void quirk_iommu_igfx(struct pci_dev *dev)
  88. dmar_map_gfx = 0;
  89. }
  90. +static void quirk_iommu_ipts(struct pci_dev *dev)
  91. +{
  92. + if (!IS_IPTS(dev))
  93. + return;
  94. +
  95. + if (risky_device(dev))
  96. + return;
  97. +
  98. + pci_info(dev, "Passthrough IOMMU for IPTS\n");
  99. + dmar_map_ipts = 0;
  100. +}
  101. /* G4x/GM45 integrated gfx dmar support is totally busted. */
  102. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_igfx);
  103. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_igfx);
  104. @@ -4813,6 +4834,9 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x1632, quirk_iommu_igfx);
  105. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x163A, quirk_iommu_igfx);
  106. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x163D, quirk_iommu_igfx);
  107. +/* disable IPTS dmar support */
  108. +DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x9D3E, quirk_iommu_ipts);
  109. +
  110. static void quirk_iommu_rwbf(struct pci_dev *dev)
  111. {
  112. if (risky_device(dev))
  113. --
  114. 2.41.0
  115. From 3a1c50173d069ef23f36869fdce1d648dc61c349 Mon Sep 17 00:00:00 2001
  116. From: Dorian Stoll <dorian.stoll@tmsp.io>
  117. Date: Sun, 11 Dec 2022 12:00:59 +0100
  118. Subject: [PATCH] hid: Add support for Intel Precise Touch and Stylus
  119. Based on linux-surface/intel-precise-touch@8abe268
  120. Signed-off-by: Dorian Stoll <dorian.stoll@tmsp.io>
  121. Patchset: ipts
  122. ---
  123. drivers/hid/Kconfig | 2 +
  124. drivers/hid/Makefile | 2 +
  125. drivers/hid/ipts/Kconfig | 14 +
  126. drivers/hid/ipts/Makefile | 14 +
  127. drivers/hid/ipts/cmd.c | 62 +++++
  128. drivers/hid/ipts/cmd.h | 61 ++++
  129. drivers/hid/ipts/context.h | 51 ++++
  130. drivers/hid/ipts/control.c | 495 +++++++++++++++++++++++++++++++++
  131. drivers/hid/ipts/control.h | 127 +++++++++
  132. drivers/hid/ipts/desc.h | 81 ++++++
  133. drivers/hid/ipts/hid.c | 348 +++++++++++++++++++++++
  134. drivers/hid/ipts/hid.h | 22 ++
  135. drivers/hid/ipts/main.c | 127 +++++++++
  136. drivers/hid/ipts/mei.c | 189 +++++++++++++
  137. drivers/hid/ipts/mei.h | 67 +++++
  138. drivers/hid/ipts/receiver.c | 249 +++++++++++++++++
  139. drivers/hid/ipts/receiver.h | 17 ++
  140. drivers/hid/ipts/resources.c | 108 +++++++
  141. drivers/hid/ipts/resources.h | 39 +++
  142. drivers/hid/ipts/spec-data.h | 100 +++++++
  143. drivers/hid/ipts/spec-device.h | 285 +++++++++++++++++++
  144. drivers/hid/ipts/spec-hid.h | 35 +++
  145. drivers/hid/ipts/thread.c | 85 ++++++
  146. drivers/hid/ipts/thread.h | 60 ++++
  147. 24 files changed, 2640 insertions(+)
  148. create mode 100644 drivers/hid/ipts/Kconfig
  149. create mode 100644 drivers/hid/ipts/Makefile
  150. create mode 100644 drivers/hid/ipts/cmd.c
  151. create mode 100644 drivers/hid/ipts/cmd.h
  152. create mode 100644 drivers/hid/ipts/context.h
  153. create mode 100644 drivers/hid/ipts/control.c
  154. create mode 100644 drivers/hid/ipts/control.h
  155. create mode 100644 drivers/hid/ipts/desc.h
  156. create mode 100644 drivers/hid/ipts/hid.c
  157. create mode 100644 drivers/hid/ipts/hid.h
  158. create mode 100644 drivers/hid/ipts/main.c
  159. create mode 100644 drivers/hid/ipts/mei.c
  160. create mode 100644 drivers/hid/ipts/mei.h
  161. create mode 100644 drivers/hid/ipts/receiver.c
  162. create mode 100644 drivers/hid/ipts/receiver.h
  163. create mode 100644 drivers/hid/ipts/resources.c
  164. create mode 100644 drivers/hid/ipts/resources.h
  165. create mode 100644 drivers/hid/ipts/spec-data.h
  166. create mode 100644 drivers/hid/ipts/spec-device.h
  167. create mode 100644 drivers/hid/ipts/spec-hid.h
  168. create mode 100644 drivers/hid/ipts/thread.c
  169. create mode 100644 drivers/hid/ipts/thread.h
  170. diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
  171. index b977450cac75..dcedd5b90f35 100644
  172. --- a/drivers/hid/Kconfig
  173. +++ b/drivers/hid/Kconfig
  174. @@ -1316,4 +1316,6 @@ source "drivers/hid/amd-sfh-hid/Kconfig"
  175. source "drivers/hid/surface-hid/Kconfig"
  176. +source "drivers/hid/ipts/Kconfig"
  177. +
  178. endif # HID_SUPPORT
  179. diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
  180. index 5d37cacbde33..285e12d95b0e 100644
  181. --- a/drivers/hid/Makefile
  182. +++ b/drivers/hid/Makefile
  183. @@ -167,3 +167,5 @@ obj-$(INTEL_ISH_FIRMWARE_DOWNLOADER) += intel-ish-hid/
  184. obj-$(CONFIG_AMD_SFH_HID) += amd-sfh-hid/
  185. obj-$(CONFIG_SURFACE_HID_CORE) += surface-hid/
  186. +
  187. +obj-$(CONFIG_HID_IPTS) += ipts/
  188. diff --git a/drivers/hid/ipts/Kconfig b/drivers/hid/ipts/Kconfig
  189. new file mode 100644
  190. index 000000000000..297401bd388d
  191. --- /dev/null
  192. +++ b/drivers/hid/ipts/Kconfig
  193. @@ -0,0 +1,14 @@
  194. +# SPDX-License-Identifier: GPL-2.0-or-later
  195. +
  196. +config HID_IPTS
  197. + tristate "Intel Precise Touch & Stylus"
  198. + depends on INTEL_MEI
  199. + depends on HID
  200. + help
  201. + Say Y here if your system has a touchscreen using Intels
  202. + Precise Touch & Stylus (IPTS) technology.
  203. +
  204. + If unsure say N.
  205. +
  206. + To compile this driver as a module, choose M here: the
  207. + module will be called ipts.
  208. diff --git a/drivers/hid/ipts/Makefile b/drivers/hid/ipts/Makefile
  209. new file mode 100644
  210. index 000000000000..0fe655bccdc0
  211. --- /dev/null
  212. +++ b/drivers/hid/ipts/Makefile
  213. @@ -0,0 +1,14 @@
  214. +# SPDX-License-Identifier: GPL-2.0-or-later
  215. +#
  216. +# Makefile for the IPTS touchscreen driver
  217. +#
  218. +
  219. +obj-$(CONFIG_HID_IPTS) += ipts.o
  220. +ipts-objs := cmd.o
  221. +ipts-objs += control.o
  222. +ipts-objs += hid.o
  223. +ipts-objs += main.o
  224. +ipts-objs += mei.o
  225. +ipts-objs += receiver.o
  226. +ipts-objs += resources.o
  227. +ipts-objs += thread.o
  228. diff --git a/drivers/hid/ipts/cmd.c b/drivers/hid/ipts/cmd.c
  229. new file mode 100644
  230. index 000000000000..7fd69271ccd5
  231. --- /dev/null
  232. +++ b/drivers/hid/ipts/cmd.c
  233. @@ -0,0 +1,62 @@
  234. +// SPDX-License-Identifier: GPL-2.0-or-later
  235. +/*
  236. + * Copyright (c) 2016 Intel Corporation
  237. + * Copyright (c) 2020-2023 Dorian Stoll
  238. + *
  239. + * Linux driver for Intel Precise Touch & Stylus
  240. + */
  241. +
  242. +#include <linux/errno.h>
  243. +#include <linux/types.h>
  244. +
  245. +#include "cmd.h"
  246. +#include "context.h"
  247. +#include "mei.h"
  248. +#include "spec-device.h"
  249. +
  250. +int ipts_cmd_recv_timeout(struct ipts_context *ipts, enum ipts_command_code code,
  251. + struct ipts_response *rsp, u64 timeout)
  252. +{
  253. + int ret = 0;
  254. +
  255. + if (!ipts)
  256. + return -EFAULT;
  257. +
  258. + if (!rsp)
  259. + return -EFAULT;
  260. +
  261. + /*
  262. + * In a response, the command code will have the most significant bit flipped to 1.
  263. + * If code is passed to ipts_mei_recv as is, no messages will be received.
  264. + */
  265. + ret = ipts_mei_recv(&ipts->mei, code | IPTS_RSP_BIT, rsp, timeout);
  266. + if (ret < 0)
  267. + return ret;
  268. +
  269. + dev_dbg(ipts->dev, "Received 0x%02X with status 0x%02X\n", code, rsp->status);
  270. +
  271. + /*
  272. + * Some devices will always return this error.
  273. + * It is allowed to ignore it and to try continuing.
  274. + */
  275. + if (rsp->status == IPTS_STATUS_COMPAT_CHECK_FAIL)
  276. + rsp->status = IPTS_STATUS_SUCCESS;
  277. +
  278. + return 0;
  279. +}
  280. +
  281. +int ipts_cmd_send(struct ipts_context *ipts, enum ipts_command_code code, void *data, size_t size)
  282. +{
  283. + struct ipts_command cmd = { 0 };
  284. +
  285. + if (!ipts)
  286. + return -EFAULT;
  287. +
  288. + cmd.cmd = code;
  289. +
  290. + if (data && size > 0)
  291. + memcpy(cmd.payload, data, size);
  292. +
  293. + dev_dbg(ipts->dev, "Sending 0x%02X with %ld bytes payload\n", code, size);
  294. + return ipts_mei_send(&ipts->mei, &cmd, sizeof(cmd.cmd) + size);
  295. +}
  296. diff --git a/drivers/hid/ipts/cmd.h b/drivers/hid/ipts/cmd.h
  297. new file mode 100644
  298. index 000000000000..924758ffee67
  299. --- /dev/null
  300. +++ b/drivers/hid/ipts/cmd.h
  301. @@ -0,0 +1,61 @@
  302. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  303. +/*
  304. + * Copyright (c) 2016 Intel Corporation
  305. + * Copyright (c) 2020-2023 Dorian Stoll
  306. + *
  307. + * Linux driver for Intel Precise Touch & Stylus
  308. + */
  309. +
  310. +#ifndef IPTS_CMD_H
  311. +#define IPTS_CMD_H
  312. +
  313. +#include <linux/types.h>
  314. +
  315. +#include "context.h"
  316. +#include "spec-device.h"
  317. +
  318. +/*
  319. + * The default timeout for receiving responses
  320. + */
  321. +#define IPTS_CMD_DEFAULT_TIMEOUT 1000
  322. +
  323. +/*
  324. + * ipts_cmd_recv_timeout() - Receives a response to a command.
  325. + * @ipts: The IPTS driver context.
  326. + * @code: The type of the command / response.
  327. + * @rsp: The address that the received response will be copied to.
  328. + * @timeout: How many milliseconds the function will wait at most.
  329. + *
  330. + * A negative timeout means to wait forever.
  331. + *
  332. + * Returns: 0 on success, <0 on error, -EAGAIN if no response has been received.
  333. + */
  334. +int ipts_cmd_recv_timeout(struct ipts_context *ipts, enum ipts_command_code code,
  335. + struct ipts_response *rsp, u64 timeout);
  336. +
  337. +/*
  338. + * ipts_cmd_recv() - Receives a response to a command.
  339. + * @ipts: The IPTS driver context.
  340. + * @code: The type of the command / response.
  341. + * @rsp: The address that the received response will be copied to.
  342. + *
  343. + * Returns: 0 on success, <0 on error, -EAGAIN if no response has been received.
  344. + */
  345. +static inline int ipts_cmd_recv(struct ipts_context *ipts, enum ipts_command_code code,
  346. + struct ipts_response *rsp)
  347. +{
  348. + return ipts_cmd_recv_timeout(ipts, code, rsp, IPTS_CMD_DEFAULT_TIMEOUT);
  349. +}
  350. +
  351. +/*
  352. + * ipts_cmd_send() - Executes a command on the device.
  353. + * @ipts: The IPTS driver context.
  354. + * @code: The type of the command to execute.
  355. + * @data: The payload containing parameters for the command.
  356. + * @size: The size of the payload.
  357. + *
  358. + * Returns: 0 on success, <0 on error.
  359. + */
  360. +int ipts_cmd_send(struct ipts_context *ipts, enum ipts_command_code code, void *data, size_t size);
  361. +
  362. +#endif /* IPTS_CMD_H */
  363. diff --git a/drivers/hid/ipts/context.h b/drivers/hid/ipts/context.h
  364. new file mode 100644
  365. index 000000000000..3450a95e66ee
  366. --- /dev/null
  367. +++ b/drivers/hid/ipts/context.h
  368. @@ -0,0 +1,51 @@
  369. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  370. +/*
  371. + * Copyright (c) 2016 Intel Corporation
  372. + * Copyright (c) 2020-2023 Dorian Stoll
  373. + *
  374. + * Linux driver for Intel Precise Touch & Stylus
  375. + */
  376. +
  377. +#ifndef IPTS_CONTEXT_H
  378. +#define IPTS_CONTEXT_H
  379. +
  380. +#include <linux/completion.h>
  381. +#include <linux/device.h>
  382. +#include <linux/hid.h>
  383. +#include <linux/mei_cl_bus.h>
  384. +#include <linux/mutex.h>
  385. +#include <linux/sched.h>
  386. +#include <linux/types.h>
  387. +
  388. +#include "mei.h"
  389. +#include "resources.h"
  390. +#include "spec-device.h"
  391. +#include "thread.h"
  392. +
  393. +struct ipts_context {
  394. + struct device *dev;
  395. + struct ipts_mei mei;
  396. +
  397. + enum ipts_mode mode;
  398. +
  399. + /*
  400. + * Prevents concurrent GET_FEATURE reports.
  401. + */
  402. + struct mutex feature_lock;
  403. + struct completion feature_event;
  404. +
  405. + /*
  406. + * These are not inside of struct ipts_resources
  407. + * because they don't own the memory they point to.
  408. + */
  409. + struct ipts_buffer feature_report;
  410. + struct ipts_buffer descriptor;
  411. +
  412. + struct hid_device *hid;
  413. + struct ipts_device_info info;
  414. + struct ipts_resources resources;
  415. +
  416. + struct ipts_thread receiver_loop;
  417. +};
  418. +
  419. +#endif /* IPTS_CONTEXT_H */
  420. diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c
  421. new file mode 100644
  422. index 000000000000..2f61500b5119
  423. --- /dev/null
  424. +++ b/drivers/hid/ipts/control.c
  425. @@ -0,0 +1,495 @@
  426. +// SPDX-License-Identifier: GPL-2.0-or-later
  427. +/*
  428. + * Copyright (c) 2016 Intel Corporation
  429. + * Copyright (c) 2020-2023 Dorian Stoll
  430. + *
  431. + * Linux driver for Intel Precise Touch & Stylus
  432. + */
  433. +
  434. +#include <linux/delay.h>
  435. +#include <linux/dev_printk.h>
  436. +#include <linux/errno.h>
  437. +#include <linux/kernel.h>
  438. +#include <linux/kthread.h>
  439. +#include <linux/types.h>
  440. +
  441. +#include "cmd.h"
  442. +#include "context.h"
  443. +#include "control.h"
  444. +#include "desc.h"
  445. +#include "hid.h"
  446. +#include "receiver.h"
  447. +#include "resources.h"
  448. +#include "spec-data.h"
  449. +#include "spec-device.h"
  450. +
  451. +static int ipts_control_get_device_info(struct ipts_context *ipts, struct ipts_device_info *info)
  452. +{
  453. + int ret = 0;
  454. + struct ipts_response rsp = { 0 };
  455. +
  456. + if (!ipts)
  457. + return -EFAULT;
  458. +
  459. + if (!info)
  460. + return -EFAULT;
  461. +
  462. + ret = ipts_cmd_send(ipts, IPTS_CMD_GET_DEVICE_INFO, NULL, 0);
  463. + if (ret) {
  464. + dev_err(ipts->dev, "GET_DEVICE_INFO: send failed: %d\n", ret);
  465. + return ret;
  466. + }
  467. +
  468. + ret = ipts_cmd_recv(ipts, IPTS_CMD_GET_DEVICE_INFO, &rsp);
  469. + if (ret) {
  470. + dev_err(ipts->dev, "GET_DEVICE_INFO: recv failed: %d\n", ret);
  471. + return ret;
  472. + }
  473. +
  474. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  475. + dev_err(ipts->dev, "GET_DEVICE_INFO: cmd failed: %d\n", rsp.status);
  476. + return -EBADR;
  477. + }
  478. +
  479. + memcpy(info, rsp.payload, sizeof(*info));
  480. + return 0;
  481. +}
  482. +
  483. +static int ipts_control_set_mode(struct ipts_context *ipts, enum ipts_mode mode)
  484. +{
  485. + int ret = 0;
  486. + struct ipts_set_mode cmd = { 0 };
  487. + struct ipts_response rsp = { 0 };
  488. +
  489. + if (!ipts)
  490. + return -EFAULT;
  491. +
  492. + cmd.mode = mode;
  493. +
  494. + ret = ipts_cmd_send(ipts, IPTS_CMD_SET_MODE, &cmd, sizeof(cmd));
  495. + if (ret) {
  496. + dev_err(ipts->dev, "SET_MODE: send failed: %d\n", ret);
  497. + return ret;
  498. + }
  499. +
  500. + ret = ipts_cmd_recv(ipts, IPTS_CMD_SET_MODE, &rsp);
  501. + if (ret) {
  502. + dev_err(ipts->dev, "SET_MODE: recv failed: %d\n", ret);
  503. + return ret;
  504. + }
  505. +
  506. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  507. + dev_err(ipts->dev, "SET_MODE: cmd failed: %d\n", rsp.status);
  508. + return -EBADR;
  509. + }
  510. +
  511. + return 0;
  512. +}
  513. +
  514. +static int ipts_control_set_mem_window(struct ipts_context *ipts, struct ipts_resources *res)
  515. +{
  516. + int ret = 0;
  517. + struct ipts_mem_window cmd = { 0 };
  518. + struct ipts_response rsp = { 0 };
  519. +
  520. + if (!ipts)
  521. + return -EFAULT;
  522. +
  523. + if (!res)
  524. + return -EFAULT;
  525. +
  526. + for (int i = 0; i < IPTS_BUFFERS; i++) {
  527. + cmd.data_addr_lower[i] = lower_32_bits(res->data[i].dma_address);
  528. + cmd.data_addr_upper[i] = upper_32_bits(res->data[i].dma_address);
  529. + cmd.feedback_addr_lower[i] = lower_32_bits(res->feedback[i].dma_address);
  530. + cmd.feedback_addr_upper[i] = upper_32_bits(res->feedback[i].dma_address);
  531. + }
  532. +
  533. + cmd.workqueue_addr_lower = lower_32_bits(res->workqueue.dma_address);
  534. + cmd.workqueue_addr_upper = upper_32_bits(res->workqueue.dma_address);
  535. +
  536. + cmd.doorbell_addr_lower = lower_32_bits(res->doorbell.dma_address);
  537. + cmd.doorbell_addr_upper = upper_32_bits(res->doorbell.dma_address);
  538. +
  539. + cmd.hid2me_addr_lower = lower_32_bits(res->hid2me.dma_address);
  540. + cmd.hid2me_addr_upper = upper_32_bits(res->hid2me.dma_address);
  541. +
  542. + cmd.workqueue_size = IPTS_WORKQUEUE_SIZE;
  543. + cmd.workqueue_item_size = IPTS_WORKQUEUE_ITEM_SIZE;
  544. +
  545. + ret = ipts_cmd_send(ipts, IPTS_CMD_SET_MEM_WINDOW, &cmd, sizeof(cmd));
  546. + if (ret) {
  547. + dev_err(ipts->dev, "SET_MEM_WINDOW: send failed: %d\n", ret);
  548. + return ret;
  549. + }
  550. +
  551. + ret = ipts_cmd_recv(ipts, IPTS_CMD_SET_MEM_WINDOW, &rsp);
  552. + if (ret) {
  553. + dev_err(ipts->dev, "SET_MEM_WINDOW: recv failed: %d\n", ret);
  554. + return ret;
  555. + }
  556. +
  557. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  558. + dev_err(ipts->dev, "SET_MEM_WINDOW: cmd failed: %d\n", rsp.status);
  559. + return -EBADR;
  560. + }
  561. +
  562. + return 0;
  563. +}
  564. +
  565. +static int ipts_control_get_descriptor(struct ipts_context *ipts)
  566. +{
  567. + int ret = 0;
  568. + struct ipts_data_header *header = NULL;
  569. + struct ipts_get_descriptor cmd = { 0 };
  570. + struct ipts_response rsp = { 0 };
  571. +
  572. + if (!ipts)
  573. + return -EFAULT;
  574. +
  575. + if (!ipts->resources.descriptor.address)
  576. + return -EFAULT;
  577. +
  578. + memset(ipts->resources.descriptor.address, 0, ipts->resources.descriptor.size);
  579. +
  580. + cmd.addr_lower = lower_32_bits(ipts->resources.descriptor.dma_address);
  581. + cmd.addr_upper = upper_32_bits(ipts->resources.descriptor.dma_address);
  582. + cmd.magic = 8;
  583. +
  584. + ret = ipts_cmd_send(ipts, IPTS_CMD_GET_DESCRIPTOR, &cmd, sizeof(cmd));
  585. + if (ret) {
  586. + dev_err(ipts->dev, "GET_DESCRIPTOR: send failed: %d\n", ret);
  587. + return ret;
  588. + }
  589. +
  590. + ret = ipts_cmd_recv(ipts, IPTS_CMD_GET_DESCRIPTOR, &rsp);
  591. + if (ret) {
  592. + dev_err(ipts->dev, "GET_DESCRIPTOR: recv failed: %d\n", ret);
  593. + return ret;
  594. + }
  595. +
  596. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  597. + dev_err(ipts->dev, "GET_DESCRIPTOR: cmd failed: %d\n", rsp.status);
  598. + return -EBADR;
  599. + }
  600. +
  601. + header = (struct ipts_data_header *)ipts->resources.descriptor.address;
  602. +
  603. + if (header->type == IPTS_DATA_TYPE_DESCRIPTOR) {
  604. + ipts->descriptor.address = &header->data[8];
  605. + ipts->descriptor.size = header->size - 8;
  606. +
  607. + return 0;
  608. + }
  609. +
  610. + return -ENODATA;
  611. +}
  612. +
  613. +int ipts_control_request_flush(struct ipts_context *ipts)
  614. +{
  615. + int ret = 0;
  616. + struct ipts_quiesce_io cmd = { 0 };
  617. +
  618. + if (!ipts)
  619. + return -EFAULT;
  620. +
  621. + ret = ipts_cmd_send(ipts, IPTS_CMD_QUIESCE_IO, &cmd, sizeof(cmd));
  622. + if (ret)
  623. + dev_err(ipts->dev, "QUIESCE_IO: send failed: %d\n", ret);
  624. +
  625. + return ret;
  626. +}
  627. +
  628. +int ipts_control_wait_flush(struct ipts_context *ipts)
  629. +{
  630. + int ret = 0;
  631. + struct ipts_response rsp = { 0 };
  632. +
  633. + if (!ipts)
  634. + return -EFAULT;
  635. +
  636. + ret = ipts_cmd_recv(ipts, IPTS_CMD_QUIESCE_IO, &rsp);
  637. + if (ret) {
  638. + dev_err(ipts->dev, "QUIESCE_IO: recv failed: %d\n", ret);
  639. + return ret;
  640. + }
  641. +
  642. + if (rsp.status == IPTS_STATUS_TIMEOUT)
  643. + return -EAGAIN;
  644. +
  645. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  646. + dev_err(ipts->dev, "QUIESCE_IO: cmd failed: %d\n", rsp.status);
  647. + return -EBADR;
  648. + }
  649. +
  650. + return 0;
  651. +}
  652. +
  653. +int ipts_control_request_data(struct ipts_context *ipts)
  654. +{
  655. + int ret = 0;
  656. +
  657. + if (!ipts)
  658. + return -EFAULT;
  659. +
  660. + ret = ipts_cmd_send(ipts, IPTS_CMD_READY_FOR_DATA, NULL, 0);
  661. + if (ret)
  662. + dev_err(ipts->dev, "READY_FOR_DATA: send failed: %d\n", ret);
  663. +
  664. + return ret;
  665. +}
  666. +
  667. +int ipts_control_wait_data(struct ipts_context *ipts, bool shutdown)
  668. +{
  669. + int ret = 0;
  670. + struct ipts_response rsp = { 0 };
  671. +
  672. + if (!ipts)
  673. + return -EFAULT;
  674. +
  675. + if (!shutdown)
  676. + ret = ipts_cmd_recv_timeout(ipts, IPTS_CMD_READY_FOR_DATA, &rsp, 0);
  677. + else
  678. + ret = ipts_cmd_recv(ipts, IPTS_CMD_READY_FOR_DATA, &rsp);
  679. +
  680. + if (ret) {
  681. + if (ret != -EAGAIN)
  682. + dev_err(ipts->dev, "READY_FOR_DATA: recv failed: %d\n", ret);
  683. +
  684. + return ret;
  685. + }
  686. +
  687. + /*
  688. + * During shutdown, it is possible that the sensor has already been disabled.
  689. + */
  690. + if (rsp.status == IPTS_STATUS_SENSOR_DISABLED)
  691. + return 0;
  692. +
  693. + if (rsp.status == IPTS_STATUS_TIMEOUT)
  694. + return -EAGAIN;
  695. +
  696. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  697. + dev_err(ipts->dev, "READY_FOR_DATA: cmd failed: %d\n", rsp.status);
  698. + return -EBADR;
  699. + }
  700. +
  701. + return 0;
  702. +}
  703. +
  704. +int ipts_control_send_feedback(struct ipts_context *ipts, u32 buffer)
  705. +{
  706. + int ret = 0;
  707. + struct ipts_feedback cmd = { 0 };
  708. + struct ipts_response rsp = { 0 };
  709. +
  710. + if (!ipts)
  711. + return -EFAULT;
  712. +
  713. + cmd.buffer = buffer;
  714. +
  715. + ret = ipts_cmd_send(ipts, IPTS_CMD_FEEDBACK, &cmd, sizeof(cmd));
  716. + if (ret) {
  717. + dev_err(ipts->dev, "FEEDBACK: send failed: %d\n", ret);
  718. + return ret;
  719. + }
  720. +
  721. + ret = ipts_cmd_recv(ipts, IPTS_CMD_FEEDBACK, &rsp);
  722. + if (ret) {
  723. + dev_err(ipts->dev, "FEEDBACK: recv failed: %d\n", ret);
  724. + return ret;
  725. + }
  726. +
  727. + /*
  728. + * We don't know what feedback data looks like so we are sending zeros.
  729. + * See also ipts_control_refill_buffer.
  730. + */
  731. + if (rsp.status == IPTS_STATUS_INVALID_PARAMS)
  732. + return 0;
  733. +
  734. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  735. + dev_err(ipts->dev, "FEEDBACK: cmd failed: %d\n", rsp.status);
  736. + return -EBADR;
  737. + }
  738. +
  739. + return 0;
  740. +}
  741. +
  742. +int ipts_control_hid2me_feedback(struct ipts_context *ipts, enum ipts_feedback_cmd_type cmd,
  743. + enum ipts_feedback_data_type type, void *data, size_t size)
  744. +{
  745. + struct ipts_feedback_header *header = NULL;
  746. +
  747. + if (!ipts)
  748. + return -EFAULT;
  749. +
  750. + if (!ipts->resources.hid2me.address)
  751. + return -EFAULT;
  752. +
  753. + memset(ipts->resources.hid2me.address, 0, ipts->resources.hid2me.size);
  754. + header = (struct ipts_feedback_header *)ipts->resources.hid2me.address;
  755. +
  756. + header->cmd_type = cmd;
  757. + header->data_type = type;
  758. + header->size = size;
  759. + header->buffer = IPTS_HID2ME_BUFFER;
  760. +
  761. + if (size + sizeof(*header) > ipts->resources.hid2me.size)
  762. + return -EINVAL;
  763. +
  764. + if (data && size > 0)
  765. + memcpy(header->payload, data, size);
  766. +
  767. + return ipts_control_send_feedback(ipts, IPTS_HID2ME_BUFFER);
  768. +}
  769. +
  770. +static inline int ipts_control_reset_sensor(struct ipts_context *ipts)
  771. +{
  772. + return ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_SOFT_RESET,
  773. + IPTS_FEEDBACK_DATA_TYPE_VENDOR, NULL, 0);
  774. +}
  775. +
  776. +int ipts_control_start(struct ipts_context *ipts)
  777. +{
  778. + int ret = 0;
  779. + struct ipts_device_info info = { 0 };
  780. +
  781. + if (!ipts)
  782. + return -EFAULT;
  783. +
  784. + dev_info(ipts->dev, "Starting IPTS\n");
  785. +
  786. + ret = ipts_control_get_device_info(ipts, &info);
  787. + if (ret) {
  788. + dev_err(ipts->dev, "Failed to get device info: %d\n", ret);
  789. + return ret;
  790. + }
  791. +
  792. + ipts->info = info;
  793. +
  794. + ret = ipts_resources_init(&ipts->resources, ipts->dev, info.data_size, info.feedback_size);
  795. + if (ret) {
  796. + dev_err(ipts->dev, "Failed to allocate buffers: %d", ret);
  797. + return ret;
  798. + }
  799. +
  800. + dev_info(ipts->dev, "IPTS EDS Version: %d\n", info.intf_eds);
  801. +
  802. + /*
  803. + * Handle newer devices
  804. + */
  805. + if (info.intf_eds > 1) {
  806. + /*
  807. + * Fetching the descriptor will only work on newer devices.
  808. + * For older devices, a fallback descriptor will be used.
  809. + */
  810. + ret = ipts_control_get_descriptor(ipts);
  811. + if (ret) {
  812. + dev_err(ipts->dev, "Failed to fetch HID descriptor: %d\n", ret);
  813. + return ret;
  814. + }
  815. +
  816. + /*
  817. + * Newer devices can be directly initialized in doorbell mode.
  818. + */
  819. + ipts->mode = IPTS_MODE_DOORBELL;
  820. + }
  821. +
  822. + ret = ipts_control_set_mode(ipts, ipts->mode);
  823. + if (ret) {
  824. + dev_err(ipts->dev, "Failed to set mode: %d\n", ret);
  825. + return ret;
  826. + }
  827. +
  828. + ret = ipts_control_set_mem_window(ipts, &ipts->resources);
  829. + if (ret) {
  830. + dev_err(ipts->dev, "Failed to set memory window: %d\n", ret);
  831. + return ret;
  832. + }
  833. +
  834. + ret = ipts_receiver_start(ipts);
  835. + if (ret) {
  836. + dev_err(ipts->dev, "Failed to start receiver: %d\n", ret);
  837. + return ret;
  838. + }
  839. +
  840. + ret = ipts_control_request_data(ipts);
  841. + if (ret) {
  842. + dev_err(ipts->dev, "Failed to request data: %d\n", ret);
  843. + return ret;
  844. + }
  845. +
  846. + ret = ipts_hid_init(ipts, info);
  847. + if (ret) {
  848. + dev_err(ipts->dev, "Failed to initialize HID device: %d\n", ret);
  849. + return ret;
  850. + }
  851. +
  852. + return 0;
  853. +}
  854. +
  855. +static int _ipts_control_stop(struct ipts_context *ipts)
  856. +{
  857. + int ret = 0;
  858. +
  859. + if (!ipts)
  860. + return -EFAULT;
  861. +
  862. + dev_info(ipts->dev, "Stopping IPTS\n");
  863. +
  864. + ret = ipts_receiver_stop(ipts);
  865. + if (ret) {
  866. + dev_err(ipts->dev, "Failed to stop receiver: %d\n", ret);
  867. + return ret;
  868. + }
  869. +
  870. + ret = ipts_control_reset_sensor(ipts);
  871. + if (ret) {
  872. + dev_err(ipts->dev, "Failed to reset sensor: %d\n", ret);
  873. + return ret;
  874. + }
  875. +
  876. + ret = ipts_resources_free(&ipts->resources);
  877. + if (ret) {
  878. + dev_err(ipts->dev, "Failed to free resources: %d\n", ret);
  879. + return ret;
  880. + }
  881. +
  882. + return 0;
  883. +}
  884. +
  885. +int ipts_control_stop(struct ipts_context *ipts)
  886. +{
  887. + int ret = 0;
  888. +
  889. + ret = _ipts_control_stop(ipts);
  890. + if (ret)
  891. + return ret;
  892. +
  893. + ret = ipts_hid_free(ipts);
  894. + if (ret) {
  895. + dev_err(ipts->dev, "Failed to free HID device: %d\n", ret);
  896. + return ret;
  897. + }
  898. +
  899. + return 0;
  900. +}
  901. +
  902. +int ipts_control_restart(struct ipts_context *ipts)
  903. +{
  904. + int ret = 0;
  905. +
  906. + ret = _ipts_control_stop(ipts);
  907. + if (ret)
  908. + return ret;
  909. +
  910. + /*
  911. + * Give the sensor some time to come back from resetting
  912. + */
  913. + msleep(1000);
  914. +
  915. + ret = ipts_control_start(ipts);
  916. + if (ret)
  917. + return ret;
  918. +
  919. + return 0;
  920. +}
  921. diff --git a/drivers/hid/ipts/control.h b/drivers/hid/ipts/control.h
  922. new file mode 100644
  923. index 000000000000..744bb92d682a
  924. --- /dev/null
  925. +++ b/drivers/hid/ipts/control.h
  926. @@ -0,0 +1,127 @@
  927. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  928. +/*
  929. + * Copyright (c) 2016 Intel Corporation
  930. + * Copyright (c) 2020-2023 Dorian Stoll
  931. + *
  932. + * Linux driver for Intel Precise Touch & Stylus
  933. + */
  934. +
  935. +#ifndef IPTS_CONTROL_H
  936. +#define IPTS_CONTROL_H
  937. +
  938. +#include <linux/types.h>
  939. +
  940. +#include "context.h"
  941. +#include "spec-data.h"
  942. +#include "spec-device.h"
  943. +
  944. +/*
  945. + * ipts_control_request_flush() - Stop the data flow.
  946. + * @ipts: The IPTS driver context.
  947. + *
  948. + * Runs the command to stop the data flow on the device.
  949. + * All outstanding data needs to be acknowledged using feedback before the command will return.
  950. + *
  951. + * Returns: 0 on success, <0 on error.
  952. + */
  953. +int ipts_control_request_flush(struct ipts_context *ipts);
  954. +
  955. +/*
  956. + * ipts_control_wait_flush() - Wait until data flow has been stopped.
  957. + * @ipts: The IPTS driver context.
  958. + *
  959. + * Returns: 0 on success, <0 on error.
  960. + */
  961. +int ipts_control_wait_flush(struct ipts_context *ipts);
  962. +
  963. +/*
  964. + * ipts_control_wait_flush() - Notify the device that the driver can receive new data.
  965. + * @ipts: The IPTS driver context.
  966. + *
  967. + * Returns: 0 on success, <0 on error.
  968. + */
  969. +int ipts_control_request_data(struct ipts_context *ipts);
  970. +
  971. +/*
  972. + * ipts_control_wait_data() - Wait until new data is available.
  973. + * @ipts: The IPTS driver context.
  974. + * @block: Whether to block execution until data is available.
  975. + *
  976. + * In doorbell mode, this function will never return while the data flow is active. Instead,
  977. + * the doorbell will be incremented when new data is available.
  978. + *
  979. + * Returns: 0 on success, <0 on error, -EAGAIN if no data is available.
  980. + */
  981. +int ipts_control_wait_data(struct ipts_context *ipts, bool block);
  982. +
  983. +/*
  984. + * ipts_control_send_feedback() - Submits a feedback buffer to the device.
  985. + * @ipts: The IPTS driver context.
  986. + * @buffer: The ID of the buffer containing feedback data.
  987. + *
  988. + * Returns: 0 on success, <0 on error.
  989. + */
  990. +int ipts_control_send_feedback(struct ipts_context *ipts, u32 buffer);
  991. +
  992. +/*
  993. + * ipts_control_hid2me_feedback() - Sends HID2ME feedback, a special type of feedback.
  994. + * @ipts: The IPTS driver context.
  995. + * @cmd: The command that will be run on the device.
  996. + * @type: The type of the payload that is sent to the device.
  997. + * @data: The payload of the feedback command.
  998. + * @size: The size of the payload.
  999. + *
  1000. + * HID2ME feedback is a special type of feedback, because it allows interfacing with
  1001. + * the HID API of the device at any moment, without requiring a buffer that has to
  1002. + * be acknowledged.
  1003. + *
  1004. + * Returns: 0 on success, <0 on error.
  1005. + */
  1006. +int ipts_control_hid2me_feedback(struct ipts_context *ipts, enum ipts_feedback_cmd_type cmd,
  1007. + enum ipts_feedback_data_type type, void *data, size_t size);
  1008. +
  1009. +/*
  1010. + * ipts_control_refill_buffer() - Acknowledges that data in a buffer has been processed.
  1011. + * @ipts: The IPTS driver context.
  1012. + * @buffer: The buffer that has been processed and can be refilled.
  1013. + *
  1014. + * Returns: 0 on success, <0 on error.
  1015. + */
  1016. +static inline int ipts_control_refill_buffer(struct ipts_context *ipts, u32 buffer)
  1017. +{
  1018. + /*
  1019. + * IPTS expects structured data in the feedback buffer matching the buffer that will be
  1020. + * refilled. We don't know what that data looks like, so we just keep the buffer empty.
  1021. + * This results in an INVALID_PARAMS error, but the buffer gets refilled without an issue.
  1022. + * Sending a minimal structure with the buffer ID fixes the error, but breaks refilling
  1023. + * the buffers on some devices.
  1024. + */
  1025. +
  1026. + return ipts_control_send_feedback(ipts, buffer);
  1027. +}
  1028. +
  1029. +/*
  1030. + * ipts_control_start() - Initialized the device and starts the data flow.
  1031. + * @ipts: The IPTS driver context.
  1032. + *
  1033. + * Returns: 0 on success, <0 on error.
  1034. + */
  1035. +int ipts_control_start(struct ipts_context *ipts);
  1036. +
  1037. +/*
  1038. + * ipts_control_stop() - Stops the data flow and resets the device.
  1039. + * @ipts: The IPTS driver context.
  1040. + *
  1041. + * Returns: 0 on success, <0 on error.
  1042. + */
  1043. +int ipts_control_stop(struct ipts_context *ipts);
  1044. +
  1045. +/*
  1046. + * ipts_control_restart() - Stops the device and starts it again.
  1047. + * @ipts: The IPTS driver context.
  1048. + *
  1049. + * Returns: 0 on success, <0 on error.
  1050. + */
  1051. +int ipts_control_restart(struct ipts_context *ipts);
  1052. +
  1053. +#endif /* IPTS_CONTROL_H */
  1054. diff --git a/drivers/hid/ipts/desc.h b/drivers/hid/ipts/desc.h
  1055. new file mode 100644
  1056. index 000000000000..c058974a03a1
  1057. --- /dev/null
  1058. +++ b/drivers/hid/ipts/desc.h
  1059. @@ -0,0 +1,81 @@
  1060. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  1061. +/*
  1062. + * Copyright (c) 2016 Intel Corporation
  1063. + * Copyright (c) 2022-2023 Dorian Stoll
  1064. + *
  1065. + * Linux driver for Intel Precise Touch & Stylus
  1066. + */
  1067. +
  1068. +#ifndef IPTS_DESC_H
  1069. +#define IPTS_DESC_H
  1070. +
  1071. +#include <linux/types.h>
  1072. +
  1073. +#define IPTS_HID_REPORT_SINGLETOUCH 64
  1074. +#define IPTS_HID_REPORT_DATA 65
  1075. +#define IPTS_HID_REPORT_SET_MODE 66
  1076. +
  1077. +#define IPTS_HID_REPORT_DATA_SIZE 7485
  1078. +
  1079. +/*
  1080. + * HID descriptor for singletouch data.
  1081. + * This descriptor should be present on all IPTS devices.
  1082. + */
  1083. +static const u8 ipts_singletouch_descriptor[] = {
  1084. + 0x05, 0x0D, /* Usage Page (Digitizer), */
  1085. + 0x09, 0x04, /* Usage (Touchscreen), */
  1086. + 0xA1, 0x01, /* Collection (Application), */
  1087. + 0x85, 0x40, /* Report ID (64), */
  1088. + 0x09, 0x42, /* Usage (Tip Switch), */
  1089. + 0x15, 0x00, /* Logical Minimum (0), */
  1090. + 0x25, 0x01, /* Logical Maximum (1), */
  1091. + 0x75, 0x01, /* Report Size (1), */
  1092. + 0x95, 0x01, /* Report Count (1), */
  1093. + 0x81, 0x02, /* Input (Variable), */
  1094. + 0x95, 0x07, /* Report Count (7), */
  1095. + 0x81, 0x03, /* Input (Constant, Variable), */
  1096. + 0x05, 0x01, /* Usage Page (Desktop), */
  1097. + 0x09, 0x30, /* Usage (X), */
  1098. + 0x75, 0x10, /* Report Size (16), */
  1099. + 0x95, 0x01, /* Report Count (1), */
  1100. + 0xA4, /* Push, */
  1101. + 0x55, 0x0E, /* Unit Exponent (14), */
  1102. + 0x65, 0x11, /* Unit (Centimeter), */
  1103. + 0x46, 0x76, 0x0B, /* Physical Maximum (2934), */
  1104. + 0x26, 0xFF, 0x7F, /* Logical Maximum (32767), */
  1105. + 0x81, 0x02, /* Input (Variable), */
  1106. + 0x09, 0x31, /* Usage (Y), */
  1107. + 0x46, 0x74, 0x06, /* Physical Maximum (1652), */
  1108. + 0x26, 0xFF, 0x7F, /* Logical Maximum (32767), */
  1109. + 0x81, 0x02, /* Input (Variable), */
  1110. + 0xB4, /* Pop, */
  1111. + 0xC0, /* End Collection */
  1112. +};
  1113. +
  1114. +/*
  1115. + * Fallback HID descriptor for older devices that do not have
  1116. + * the ability to query their HID descriptor.
  1117. + */
  1118. +static const u8 ipts_fallback_descriptor[] = {
  1119. + 0x05, 0x0D, /* Usage Page (Digitizer), */
  1120. + 0x09, 0x0F, /* Usage (Capacitive Hm Digitizer), */
  1121. + 0xA1, 0x01, /* Collection (Application), */
  1122. + 0x85, 0x41, /* Report ID (65), */
  1123. + 0x09, 0x56, /* Usage (Scan Time), */
  1124. + 0x95, 0x01, /* Report Count (1), */
  1125. + 0x75, 0x10, /* Report Size (16), */
  1126. + 0x81, 0x02, /* Input (Variable), */
  1127. + 0x09, 0x61, /* Usage (Gesture Char Quality), */
  1128. + 0x75, 0x08, /* Report Size (8), */
  1129. + 0x96, 0x3D, 0x1D, /* Report Count (7485), */
  1130. + 0x81, 0x03, /* Input (Constant, Variable), */
  1131. + 0x85, 0x42, /* Report ID (66), */
  1132. + 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
  1133. + 0x09, 0xC8, /* Usage (C8h), */
  1134. + 0x75, 0x08, /* Report Size (8), */
  1135. + 0x95, 0x01, /* Report Count (1), */
  1136. + 0xB1, 0x02, /* Feature (Variable), */
  1137. + 0xC0, /* End Collection, */
  1138. +};
  1139. +
  1140. +#endif /* IPTS_DESC_H */
  1141. diff --git a/drivers/hid/ipts/hid.c b/drivers/hid/ipts/hid.c
  1142. new file mode 100644
  1143. index 000000000000..6782394e8dde
  1144. --- /dev/null
  1145. +++ b/drivers/hid/ipts/hid.c
  1146. @@ -0,0 +1,348 @@
  1147. +// SPDX-License-Identifier: GPL-2.0-or-later
  1148. +/*
  1149. + * Copyright (c) 2016 Intel Corporation
  1150. + * Copyright (c) 2022-2023 Dorian Stoll
  1151. + *
  1152. + * Linux driver for Intel Precise Touch & Stylus
  1153. + */
  1154. +
  1155. +#include <linux/completion.h>
  1156. +#include <linux/gfp.h>
  1157. +#include <linux/hid.h>
  1158. +#include <linux/mutex.h>
  1159. +#include <linux/slab.h>
  1160. +#include <linux/types.h>
  1161. +
  1162. +#include "context.h"
  1163. +#include "control.h"
  1164. +#include "desc.h"
  1165. +#include "hid.h"
  1166. +#include "spec-data.h"
  1167. +#include "spec-device.h"
  1168. +#include "spec-hid.h"
  1169. +
  1170. +static int ipts_hid_start(struct hid_device *hid)
  1171. +{
  1172. + return 0;
  1173. +}
  1174. +
  1175. +static void ipts_hid_stop(struct hid_device *hid)
  1176. +{
  1177. +}
  1178. +
  1179. +static int ipts_hid_switch_mode(struct ipts_context *ipts, enum ipts_mode mode)
  1180. +{
  1181. + if (!ipts)
  1182. + return -EFAULT;
  1183. +
  1184. + if (ipts->mode == mode)
  1185. + return 0;
  1186. +
  1187. + /*
  1188. + * This is only allowed on older devices.
  1189. + */
  1190. + if (ipts->info.intf_eds > 1)
  1191. + return 0;
  1192. +
  1193. + ipts->mode = mode;
  1194. + return ipts_control_restart(ipts);
  1195. +}
  1196. +
  1197. +static int ipts_hid_parse(struct hid_device *hid)
  1198. +{
  1199. + int ret = 0;
  1200. + struct ipts_context *ipts = NULL;
  1201. +
  1202. + bool has_native_descriptor = false;
  1203. +
  1204. + u8 *buffer = NULL;
  1205. + size_t size = 0;
  1206. +
  1207. + if (!hid)
  1208. + return -ENODEV;
  1209. +
  1210. + ipts = hid->driver_data;
  1211. +
  1212. + if (!ipts)
  1213. + return -EFAULT;
  1214. +
  1215. + size = sizeof(ipts_singletouch_descriptor);
  1216. + has_native_descriptor = ipts->descriptor.address && ipts->descriptor.size > 0;
  1217. +
  1218. + if (has_native_descriptor)
  1219. + size += ipts->descriptor.size;
  1220. + else
  1221. + size += sizeof(ipts_fallback_descriptor);
  1222. +
  1223. + buffer = kzalloc(size, GFP_KERNEL);
  1224. + if (!buffer)
  1225. + return -ENOMEM;
  1226. +
  1227. + memcpy(buffer, ipts_singletouch_descriptor, sizeof(ipts_singletouch_descriptor));
  1228. +
  1229. + if (has_native_descriptor) {
  1230. + memcpy(&buffer[sizeof(ipts_singletouch_descriptor)], ipts->descriptor.address,
  1231. + ipts->descriptor.size);
  1232. + } else {
  1233. + memcpy(&buffer[sizeof(ipts_singletouch_descriptor)], ipts_fallback_descriptor,
  1234. + sizeof(ipts_fallback_descriptor));
  1235. + }
  1236. +
  1237. + ret = hid_parse_report(hid, buffer, size);
  1238. + kfree(buffer);
  1239. +
  1240. + if (ret) {
  1241. + dev_err(ipts->dev, "Failed to parse HID descriptor: %d\n", ret);
  1242. + return ret;
  1243. + }
  1244. +
  1245. + return 0;
  1246. +}
  1247. +
  1248. +static int ipts_hid_get_feature(struct ipts_context *ipts, unsigned char reportnum, __u8 *buf,
  1249. + size_t size, enum ipts_feedback_data_type type)
  1250. +{
  1251. + int ret = 0;
  1252. +
  1253. + if (!ipts)
  1254. + return -EFAULT;
  1255. +
  1256. + if (!buf)
  1257. + return -EFAULT;
  1258. +
  1259. + mutex_lock(&ipts->feature_lock);
  1260. +
  1261. + memset(buf, 0, size);
  1262. + buf[0] = reportnum;
  1263. +
  1264. + memset(&ipts->feature_report, 0, sizeof(ipts->feature_report));
  1265. + reinit_completion(&ipts->feature_event);
  1266. +
  1267. + ret = ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE, type, buf, size);
  1268. + if (ret) {
  1269. + dev_err(ipts->dev, "Failed to send hid2me feedback: %d\n", ret);
  1270. + goto out;
  1271. + }
  1272. +
  1273. + ret = wait_for_completion_timeout(&ipts->feature_event, msecs_to_jiffies(5000));
  1274. + if (ret == 0) {
  1275. + dev_warn(ipts->dev, "GET_FEATURES timed out!\n");
  1276. + ret = -EIO;
  1277. + goto out;
  1278. + }
  1279. +
  1280. + if (!ipts->feature_report.address) {
  1281. + ret = -EFAULT;
  1282. + goto out;
  1283. + }
  1284. +
  1285. + if (ipts->feature_report.size > size) {
  1286. + ret = -ETOOSMALL;
  1287. + goto out;
  1288. + }
  1289. +
  1290. + ret = ipts->feature_report.size;
  1291. + memcpy(buf, ipts->feature_report.address, ipts->feature_report.size);
  1292. +
  1293. +out:
  1294. + mutex_unlock(&ipts->feature_lock);
  1295. + return ret;
  1296. +}
  1297. +
  1298. +static int ipts_hid_set_feature(struct ipts_context *ipts, unsigned char reportnum, __u8 *buf,
  1299. + size_t size, enum ipts_feedback_data_type type)
  1300. +{
  1301. + int ret = 0;
  1302. +
  1303. + if (!ipts)
  1304. + return -EFAULT;
  1305. +
  1306. + if (!buf)
  1307. + return -EFAULT;
  1308. +
  1309. + buf[0] = reportnum;
  1310. +
  1311. + ret = ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE, type, buf, size);
  1312. + if (ret)
  1313. + dev_err(ipts->dev, "Failed to send hid2me feedback: %d\n", ret);
  1314. +
  1315. + return ret;
  1316. +}
  1317. +
  1318. +static int ipts_hid_raw_request(struct hid_device *hid, unsigned char reportnum, __u8 *buf,
  1319. + size_t size, unsigned char rtype, int reqtype)
  1320. +{
  1321. + int ret = 0;
  1322. + struct ipts_context *ipts = NULL;
  1323. +
  1324. + enum ipts_feedback_data_type type = IPTS_FEEDBACK_DATA_TYPE_VENDOR;
  1325. +
  1326. + if (!hid)
  1327. + return -ENODEV;
  1328. +
  1329. + ipts = hid->driver_data;
  1330. +
  1331. + if (!ipts)
  1332. + return -EFAULT;
  1333. +
  1334. + if (!buf)
  1335. + return -EFAULT;
  1336. +
  1337. + if (rtype == HID_OUTPUT_REPORT && reqtype == HID_REQ_SET_REPORT)
  1338. + type = IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT;
  1339. + else if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_GET_REPORT)
  1340. + type = IPTS_FEEDBACK_DATA_TYPE_GET_FEATURES;
  1341. + else if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_SET_REPORT)
  1342. + type = IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES;
  1343. + else
  1344. + return -EIO;
  1345. +
  1346. + // Implemente mode switching report for older devices without native HID support
  1347. + if (type == IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES && reportnum == IPTS_HID_REPORT_SET_MODE) {
  1348. + ret = ipts_hid_switch_mode(ipts, buf[1]);
  1349. + if (ret) {
  1350. + dev_err(ipts->dev, "Failed to switch modes: %d\n", ret);
  1351. + return ret;
  1352. + }
  1353. + }
  1354. +
  1355. + if (reqtype == HID_REQ_GET_REPORT)
  1356. + return ipts_hid_get_feature(ipts, reportnum, buf, size, type);
  1357. + else
  1358. + return ipts_hid_set_feature(ipts, reportnum, buf, size, type);
  1359. +}
  1360. +
  1361. +static int ipts_hid_output_report(struct hid_device *hid, __u8 *data, size_t size)
  1362. +{
  1363. + struct ipts_context *ipts = NULL;
  1364. +
  1365. + if (!hid)
  1366. + return -ENODEV;
  1367. +
  1368. + ipts = hid->driver_data;
  1369. +
  1370. + return ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE,
  1371. + IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT, data, size);
  1372. +}
  1373. +
  1374. +static struct hid_ll_driver ipts_hid_driver = {
  1375. + .start = ipts_hid_start,
  1376. + .stop = ipts_hid_stop,
  1377. + .open = ipts_hid_start,
  1378. + .close = ipts_hid_stop,
  1379. + .parse = ipts_hid_parse,
  1380. + .raw_request = ipts_hid_raw_request,
  1381. + .output_report = ipts_hid_output_report,
  1382. +};
  1383. +
  1384. +int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer)
  1385. +{
  1386. + int ret = 0;
  1387. + u8 *temp = NULL;
  1388. + struct ipts_hid_header *frame = NULL;
  1389. + struct ipts_data_header *header = NULL;
  1390. +
  1391. + if (!ipts)
  1392. + return -EFAULT;
  1393. +
  1394. + if (!ipts->hid)
  1395. + return -ENODEV;
  1396. +
  1397. + header = (struct ipts_data_header *)ipts->resources.data[buffer].address;
  1398. +
  1399. + if (!header)
  1400. + return -EFAULT;
  1401. +
  1402. + if (header->size == 0)
  1403. + return 0;
  1404. +
  1405. + if (header->type == IPTS_DATA_TYPE_HID)
  1406. + return hid_input_report(ipts->hid, HID_INPUT_REPORT, header->data, header->size, 1);
  1407. +
  1408. + if (header->type == IPTS_DATA_TYPE_GET_FEATURES) {
  1409. + ipts->feature_report.address = header->data;
  1410. + ipts->feature_report.size = header->size;
  1411. +
  1412. + complete_all(&ipts->feature_event);
  1413. + return 0;
  1414. + }
  1415. +
  1416. + if (header->type != IPTS_DATA_TYPE_FRAME)
  1417. + return 0;
  1418. +
  1419. + if (header->size + 3 + sizeof(struct ipts_hid_header) > IPTS_HID_REPORT_DATA_SIZE)
  1420. + return -ERANGE;
  1421. +
  1422. + temp = kzalloc(IPTS_HID_REPORT_DATA_SIZE, GFP_KERNEL);
  1423. + if (!temp)
  1424. + return -ENOMEM;
  1425. +
  1426. + /*
  1427. + * Synthesize a HID report matching the devices that natively send HID reports
  1428. + */
  1429. + temp[0] = IPTS_HID_REPORT_DATA;
  1430. +
  1431. + frame = (struct ipts_hid_header *)&temp[3];
  1432. + frame->type = IPTS_HID_FRAME_TYPE_RAW;
  1433. + frame->size = header->size + sizeof(*frame);
  1434. +
  1435. + memcpy(frame->data, header->data, header->size);
  1436. +
  1437. + ret = hid_input_report(ipts->hid, HID_INPUT_REPORT, temp, IPTS_HID_REPORT_DATA_SIZE, 1);
  1438. + kfree(temp);
  1439. +
  1440. + return ret;
  1441. +}
  1442. +
  1443. +int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info)
  1444. +{
  1445. + int ret = 0;
  1446. +
  1447. + if (!ipts)
  1448. + return -EFAULT;
  1449. +
  1450. + if (ipts->hid)
  1451. + return 0;
  1452. +
  1453. + ipts->hid = hid_allocate_device();
  1454. + if (IS_ERR(ipts->hid)) {
  1455. + int err = PTR_ERR(ipts->hid);
  1456. +
  1457. + dev_err(ipts->dev, "Failed to allocate HID device: %d\n", err);
  1458. + return err;
  1459. + }
  1460. +
  1461. + ipts->hid->driver_data = ipts;
  1462. + ipts->hid->dev.parent = ipts->dev;
  1463. + ipts->hid->ll_driver = &ipts_hid_driver;
  1464. +
  1465. + ipts->hid->vendor = info.vendor;
  1466. + ipts->hid->product = info.product;
  1467. + ipts->hid->group = HID_GROUP_MULTITOUCH;
  1468. +
  1469. + snprintf(ipts->hid->name, sizeof(ipts->hid->name), "IPTS %04X:%04X", info.vendor,
  1470. + info.product);
  1471. +
  1472. + ret = hid_add_device(ipts->hid);
  1473. + if (ret) {
  1474. + dev_err(ipts->dev, "Failed to add HID device: %d\n", ret);
  1475. + ipts_hid_free(ipts);
  1476. + return ret;
  1477. + }
  1478. +
  1479. + return 0;
  1480. +}
  1481. +
  1482. +int ipts_hid_free(struct ipts_context *ipts)
  1483. +{
  1484. + if (!ipts)
  1485. + return -EFAULT;
  1486. +
  1487. + if (!ipts->hid)
  1488. + return 0;
  1489. +
  1490. + hid_destroy_device(ipts->hid);
  1491. + ipts->hid = NULL;
  1492. +
  1493. + return 0;
  1494. +}
  1495. diff --git a/drivers/hid/ipts/hid.h b/drivers/hid/ipts/hid.h
  1496. new file mode 100644
  1497. index 000000000000..62bf3cd48608
  1498. --- /dev/null
  1499. +++ b/drivers/hid/ipts/hid.h
  1500. @@ -0,0 +1,22 @@
  1501. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  1502. +/*
  1503. + * Copyright (c) 2016 Intel Corporation
  1504. + * Copyright (c) 2022-2023 Dorian Stoll
  1505. + *
  1506. + * Linux driver for Intel Precise Touch & Stylus
  1507. + */
  1508. +
  1509. +#ifndef IPTS_HID_H
  1510. +#define IPTS_HID_H
  1511. +
  1512. +#include <linux/types.h>
  1513. +
  1514. +#include "context.h"
  1515. +#include "spec-device.h"
  1516. +
  1517. +int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer);
  1518. +
  1519. +int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info);
  1520. +int ipts_hid_free(struct ipts_context *ipts);
  1521. +
  1522. +#endif /* IPTS_HID_H */
  1523. diff --git a/drivers/hid/ipts/main.c b/drivers/hid/ipts/main.c
  1524. new file mode 100644
  1525. index 000000000000..0f20c6c08c38
  1526. --- /dev/null
  1527. +++ b/drivers/hid/ipts/main.c
  1528. @@ -0,0 +1,127 @@
  1529. +// SPDX-License-Identifier: GPL-2.0-or-later
  1530. +/*
  1531. + * Copyright (c) 2016 Intel Corporation
  1532. + * Copyright (c) 2020-2023 Dorian Stoll
  1533. + *
  1534. + * Linux driver for Intel Precise Touch & Stylus
  1535. + */
  1536. +
  1537. +#include <linux/completion.h>
  1538. +#include <linux/delay.h>
  1539. +#include <linux/device.h>
  1540. +#include <linux/dma-mapping.h>
  1541. +#include <linux/mei_cl_bus.h>
  1542. +#include <linux/mod_devicetable.h>
  1543. +#include <linux/module.h>
  1544. +#include <linux/mutex.h>
  1545. +#include <linux/slab.h>
  1546. +#include <linux/stddef.h>
  1547. +#include <linux/types.h>
  1548. +
  1549. +#include "context.h"
  1550. +#include "control.h"
  1551. +#include "mei.h"
  1552. +#include "receiver.h"
  1553. +#include "spec-device.h"
  1554. +
  1555. +/*
  1556. + * The MEI client ID for IPTS functionality.
  1557. + */
  1558. +#define IPTS_ID UUID_LE(0x3e8d0870, 0x271a, 0x4208, 0x8e, 0xb5, 0x9a, 0xcb, 0x94, 0x02, 0xae, 0x04)
  1559. +
  1560. +static int ipts_set_dma_mask(struct mei_cl_device *cldev)
  1561. +{
  1562. + if (!cldev)
  1563. + return -EFAULT;
  1564. +
  1565. + if (!dma_coerce_mask_and_coherent(&cldev->dev, DMA_BIT_MASK(64)))
  1566. + return 0;
  1567. +
  1568. + return dma_coerce_mask_and_coherent(&cldev->dev, DMA_BIT_MASK(32));
  1569. +}
  1570. +
  1571. +static int ipts_probe(struct mei_cl_device *cldev, const struct mei_cl_device_id *id)
  1572. +{
  1573. + int ret = 0;
  1574. + struct ipts_context *ipts = NULL;
  1575. +
  1576. + if (!cldev)
  1577. + return -EFAULT;
  1578. +
  1579. + ret = ipts_set_dma_mask(cldev);
  1580. + if (ret) {
  1581. + dev_err(&cldev->dev, "Failed to set DMA mask for IPTS: %d\n", ret);
  1582. + return ret;
  1583. + }
  1584. +
  1585. + ret = mei_cldev_enable(cldev);
  1586. + if (ret) {
  1587. + dev_err(&cldev->dev, "Failed to enable MEI device: %d\n", ret);
  1588. + return ret;
  1589. + }
  1590. +
  1591. + ipts = devm_kzalloc(&cldev->dev, sizeof(*ipts), GFP_KERNEL);
  1592. + if (!ipts) {
  1593. + mei_cldev_disable(cldev);
  1594. + return -ENOMEM;
  1595. + }
  1596. +
  1597. + ret = ipts_mei_init(&ipts->mei, cldev);
  1598. + if (ret) {
  1599. + dev_err(&cldev->dev, "Failed to init MEI bus logic: %d\n", ret);
  1600. + return ret;
  1601. + }
  1602. +
  1603. + ipts->dev = &cldev->dev;
  1604. + ipts->mode = IPTS_MODE_EVENT;
  1605. +
  1606. + mutex_init(&ipts->feature_lock);
  1607. + init_completion(&ipts->feature_event);
  1608. +
  1609. + mei_cldev_set_drvdata(cldev, ipts);
  1610. +
  1611. + ret = ipts_control_start(ipts);
  1612. + if (ret) {
  1613. + dev_err(&cldev->dev, "Failed to start IPTS: %d\n", ret);
  1614. + return ret;
  1615. + }
  1616. +
  1617. + return 0;
  1618. +}
  1619. +
  1620. +static void ipts_remove(struct mei_cl_device *cldev)
  1621. +{
  1622. + int ret = 0;
  1623. + struct ipts_context *ipts = NULL;
  1624. +
  1625. + if (!cldev) {
  1626. + pr_err("MEI device is NULL!");
  1627. + return;
  1628. + }
  1629. +
  1630. + ipts = mei_cldev_get_drvdata(cldev);
  1631. +
  1632. + ret = ipts_control_stop(ipts);
  1633. + if (ret)
  1634. + dev_err(&cldev->dev, "Failed to stop IPTS: %d\n", ret);
  1635. +
  1636. + mei_cldev_disable(cldev);
  1637. +}
  1638. +
  1639. +static struct mei_cl_device_id ipts_device_id_table[] = {
  1640. + { .uuid = IPTS_ID, .version = MEI_CL_VERSION_ANY },
  1641. + {},
  1642. +};
  1643. +MODULE_DEVICE_TABLE(mei, ipts_device_id_table);
  1644. +
  1645. +static struct mei_cl_driver ipts_driver = {
  1646. + .id_table = ipts_device_id_table,
  1647. + .name = "ipts",
  1648. + .probe = ipts_probe,
  1649. + .remove = ipts_remove,
  1650. +};
  1651. +module_mei_cl_driver(ipts_driver);
  1652. +
  1653. +MODULE_DESCRIPTION("IPTS touchscreen driver");
  1654. +MODULE_AUTHOR("Dorian Stoll <dorian.stoll@tmsp.io>");
  1655. +MODULE_LICENSE("GPL");
  1656. diff --git a/drivers/hid/ipts/mei.c b/drivers/hid/ipts/mei.c
  1657. new file mode 100644
  1658. index 000000000000..26666fd99b0c
  1659. --- /dev/null
  1660. +++ b/drivers/hid/ipts/mei.c
  1661. @@ -0,0 +1,189 @@
  1662. +// SPDX-License-Identifier: GPL-2.0-or-later
  1663. +/*
  1664. + * Copyright (c) 2016 Intel Corporation
  1665. + * Copyright (c) 2023 Dorian Stoll
  1666. + *
  1667. + * Linux driver for Intel Precise Touch & Stylus
  1668. + */
  1669. +
  1670. +#include <linux/device.h>
  1671. +#include <linux/errno.h>
  1672. +#include <linux/jiffies.h>
  1673. +#include <linux/list.h>
  1674. +#include <linux/mei_cl_bus.h>
  1675. +#include <linux/printk.h>
  1676. +#include <linux/rwsem.h>
  1677. +#include <linux/types.h>
  1678. +#include <linux/wait.h>
  1679. +
  1680. +#include "context.h"
  1681. +#include "mei.h"
  1682. +
  1683. +static void locked_list_add(struct list_head *new, struct list_head *head,
  1684. + struct rw_semaphore *lock)
  1685. +{
  1686. + down_write(lock);
  1687. + list_add(new, head);
  1688. + up_write(lock);
  1689. +}
  1690. +
  1691. +static void locked_list_del(struct list_head *entry, struct rw_semaphore *lock)
  1692. +{
  1693. + down_write(lock);
  1694. + list_del(entry);
  1695. + up_write(lock);
  1696. +}
  1697. +
  1698. +static void ipts_mei_incoming(struct mei_cl_device *cldev)
  1699. +{
  1700. + ssize_t ret = 0;
  1701. + struct ipts_mei_message *entry = NULL;
  1702. + struct ipts_context *ipts = NULL;
  1703. +
  1704. + if (!cldev) {
  1705. + pr_err("MEI device is NULL!");
  1706. + return;
  1707. + }
  1708. +
  1709. + ipts = mei_cldev_get_drvdata(cldev);
  1710. + if (!ipts) {
  1711. + pr_err("IPTS driver context is NULL!");
  1712. + return;
  1713. + }
  1714. +
  1715. + entry = devm_kzalloc(ipts->dev, sizeof(*entry), GFP_KERNEL);
  1716. + if (!entry)
  1717. + return;
  1718. +
  1719. + INIT_LIST_HEAD(&entry->list);
  1720. +
  1721. + do {
  1722. + ret = mei_cldev_recv(cldev, (u8 *)&entry->rsp, sizeof(entry->rsp));
  1723. + } while (ret == -EINTR);
  1724. +
  1725. + if (ret < 0) {
  1726. + dev_err(ipts->dev, "Error while reading response: %ld\n", ret);
  1727. + return;
  1728. + }
  1729. +
  1730. + if (ret == 0) {
  1731. + dev_err(ipts->dev, "Received empty response\n");
  1732. + return;
  1733. + }
  1734. +
  1735. + locked_list_add(&entry->list, &ipts->mei.messages, &ipts->mei.message_lock);
  1736. + wake_up_all(&ipts->mei.message_queue);
  1737. +}
  1738. +
  1739. +static int ipts_mei_search(struct ipts_mei *mei, enum ipts_command_code code,
  1740. + struct ipts_response *rsp)
  1741. +{
  1742. + struct ipts_mei_message *entry = NULL;
  1743. +
  1744. + if (!mei)
  1745. + return -EFAULT;
  1746. +
  1747. + if (!rsp)
  1748. + return -EFAULT;
  1749. +
  1750. + down_read(&mei->message_lock);
  1751. +
  1752. + /*
  1753. + * Iterate over the list of received messages, and check if there is one
  1754. + * matching the requested command code.
  1755. + */
  1756. + list_for_each_entry(entry, &mei->messages, list) {
  1757. + if (entry->rsp.cmd == code)
  1758. + break;
  1759. + }
  1760. +
  1761. + up_read(&mei->message_lock);
  1762. +
  1763. + /*
  1764. + * If entry is not the list head, this means that the loop above has been stopped early,
  1765. + * and that we found a matching element. We drop the message from the list and return it.
  1766. + */
  1767. + if (!list_entry_is_head(entry, &mei->messages, list)) {
  1768. + locked_list_del(&entry->list, &mei->message_lock);
  1769. +
  1770. + *rsp = entry->rsp;
  1771. + devm_kfree(&mei->cldev->dev, entry);
  1772. +
  1773. + return 0;
  1774. + }
  1775. +
  1776. + return -EAGAIN;
  1777. +}
  1778. +
  1779. +int ipts_mei_recv(struct ipts_mei *mei, enum ipts_command_code code, struct ipts_response *rsp,
  1780. + u64 timeout)
  1781. +{
  1782. + int ret = 0;
  1783. +
  1784. + if (!mei)
  1785. + return -EFAULT;
  1786. +
  1787. + /*
  1788. + * A timeout of 0 means check and return immideately.
  1789. + */
  1790. + if (timeout == 0)
  1791. + return ipts_mei_search(mei, code, rsp);
  1792. +
  1793. + /*
  1794. + * A timeout of less than 0 means to wait forever.
  1795. + */
  1796. + if (timeout < 0) {
  1797. + wait_event(mei->message_queue, ipts_mei_search(mei, code, rsp) == 0);
  1798. + return 0;
  1799. + }
  1800. +
  1801. + ret = wait_event_timeout(mei->message_queue, ipts_mei_search(mei, code, rsp) == 0,
  1802. + msecs_to_jiffies(timeout));
  1803. +
  1804. + if (ret > 0)
  1805. + return 0;
  1806. +
  1807. + return -EAGAIN;
  1808. +}
  1809. +
  1810. +int ipts_mei_send(struct ipts_mei *mei, void *data, size_t length)
  1811. +{
  1812. + int ret = 0;
  1813. +
  1814. + if (!mei)
  1815. + return -EFAULT;
  1816. +
  1817. + if (!mei->cldev)
  1818. + return -EFAULT;
  1819. +
  1820. + if (!data)
  1821. + return -EFAULT;
  1822. +
  1823. + do {
  1824. + ret = mei_cldev_send(mei->cldev, (u8 *)data, length);
  1825. + } while (ret == -EINTR);
  1826. +
  1827. + if (ret < 0)
  1828. + return ret;
  1829. +
  1830. + return 0;
  1831. +}
  1832. +
  1833. +int ipts_mei_init(struct ipts_mei *mei, struct mei_cl_device *cldev)
  1834. +{
  1835. + if (!mei)
  1836. + return -EFAULT;
  1837. +
  1838. + if (!cldev)
  1839. + return -EFAULT;
  1840. +
  1841. + mei->cldev = cldev;
  1842. +
  1843. + INIT_LIST_HEAD(&mei->messages);
  1844. + init_waitqueue_head(&mei->message_queue);
  1845. + init_rwsem(&mei->message_lock);
  1846. +
  1847. + mei_cldev_register_rx_cb(cldev, ipts_mei_incoming);
  1848. +
  1849. + return 0;
  1850. +}
  1851. diff --git a/drivers/hid/ipts/mei.h b/drivers/hid/ipts/mei.h
  1852. new file mode 100644
  1853. index 000000000000..eadacae54c40
  1854. --- /dev/null
  1855. +++ b/drivers/hid/ipts/mei.h
  1856. @@ -0,0 +1,67 @@
  1857. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  1858. +/*
  1859. + * Copyright (c) 2016 Intel Corporation
  1860. + * Copyright (c) 2023 Dorian Stoll
  1861. + *
  1862. + * Linux driver for Intel Precise Touch & Stylus
  1863. + */
  1864. +
  1865. +#ifndef IPTS_MEI_H
  1866. +#define IPTS_MEI_H
  1867. +
  1868. +#include <linux/list.h>
  1869. +#include <linux/mei_cl_bus.h>
  1870. +#include <linux/rwsem.h>
  1871. +#include <linux/types.h>
  1872. +#include <linux/wait.h>
  1873. +
  1874. +#include "spec-device.h"
  1875. +
  1876. +struct ipts_mei_message {
  1877. + struct list_head list;
  1878. + struct ipts_response rsp;
  1879. +};
  1880. +
  1881. +struct ipts_mei {
  1882. + struct mei_cl_device *cldev;
  1883. +
  1884. + struct list_head messages;
  1885. +
  1886. + wait_queue_head_t message_queue;
  1887. + struct rw_semaphore message_lock;
  1888. +};
  1889. +
  1890. +/*
  1891. + * ipts_mei_recv() - Receive data from a MEI device.
  1892. + * @mei: The IPTS MEI device context.
  1893. + * @code: The IPTS command code to look for.
  1894. + * @rsp: The address that the received data will be copied to.
  1895. + * @timeout: How many milliseconds the function will wait at most.
  1896. + *
  1897. + * A negative timeout means to wait forever.
  1898. + *
  1899. + * Returns: 0 on success, <0 on error, -EAGAIN if no response has been received.
  1900. + */
  1901. +int ipts_mei_recv(struct ipts_mei *mei, enum ipts_command_code code, struct ipts_response *rsp,
  1902. + u64 timeout);
  1903. +
  1904. +/*
  1905. + * ipts_mei_send() - Send data to a MEI device.
  1906. + * @ipts: The IPTS MEI device context.
  1907. + * @data: The data to send.
  1908. + * @size: The size of the data.
  1909. + *
  1910. + * Returns: 0 on success, <0 on error.
  1911. + */
  1912. +int ipts_mei_send(struct ipts_mei *mei, void *data, size_t length);
  1913. +
  1914. +/*
  1915. + * ipts_mei_init() - Initialize the MEI device context.
  1916. + * @mei: The MEI device context to initialize.
  1917. + * @cldev: The MEI device the context will be bound to.
  1918. + *
  1919. + * Returns: 0 on success, <0 on error.
  1920. + */
  1921. +int ipts_mei_init(struct ipts_mei *mei, struct mei_cl_device *cldev);
  1922. +
  1923. +#endif /* IPTS_MEI_H */
  1924. diff --git a/drivers/hid/ipts/receiver.c b/drivers/hid/ipts/receiver.c
  1925. new file mode 100644
  1926. index 000000000000..77234f9e0e17
  1927. --- /dev/null
  1928. +++ b/drivers/hid/ipts/receiver.c
  1929. @@ -0,0 +1,249 @@
  1930. +// SPDX-License-Identifier: GPL-2.0-or-later
  1931. +/*
  1932. + * Copyright (c) 2016 Intel Corporation
  1933. + * Copyright (c) 2020-2023 Dorian Stoll
  1934. + *
  1935. + * Linux driver for Intel Precise Touch & Stylus
  1936. + */
  1937. +
  1938. +#include <linux/delay.h>
  1939. +#include <linux/err.h>
  1940. +#include <linux/kthread.h>
  1941. +#include <linux/time64.h>
  1942. +#include <linux/timekeeping.h>
  1943. +#include <linux/types.h>
  1944. +
  1945. +#include "cmd.h"
  1946. +#include "context.h"
  1947. +#include "control.h"
  1948. +#include "hid.h"
  1949. +#include "resources.h"
  1950. +#include "spec-device.h"
  1951. +#include "thread.h"
  1952. +
  1953. +static void ipts_receiver_next_doorbell(struct ipts_context *ipts)
  1954. +{
  1955. + u32 *doorbell = (u32 *)ipts->resources.doorbell.address;
  1956. + *doorbell = *doorbell + 1;
  1957. +}
  1958. +
  1959. +static u32 ipts_receiver_current_doorbell(struct ipts_context *ipts)
  1960. +{
  1961. + u32 *doorbell = (u32 *)ipts->resources.doorbell.address;
  1962. + return *doorbell;
  1963. +}
  1964. +
  1965. +static void ipts_receiver_backoff(time64_t last, u32 n)
  1966. +{
  1967. + /*
  1968. + * If the last change was less than n seconds ago,
  1969. + * sleep for a shorter period so that new data can be
  1970. + * processed quickly. If there was no change for more than
  1971. + * n seconds, sleep longer to avoid wasting CPU cycles.
  1972. + */
  1973. + if (last + n > ktime_get_seconds())
  1974. + msleep(20);
  1975. + else
  1976. + msleep(200);
  1977. +}
  1978. +
  1979. +static int ipts_receiver_event_loop(struct ipts_thread *thread)
  1980. +{
  1981. + int ret = 0;
  1982. + u32 buffer = 0;
  1983. +
  1984. + struct ipts_context *ipts = NULL;
  1985. + time64_t last = ktime_get_seconds();
  1986. +
  1987. + if (!thread)
  1988. + return -EFAULT;
  1989. +
  1990. + ipts = thread->data;
  1991. +
  1992. + if (!ipts)
  1993. + return -EFAULT;
  1994. +
  1995. + dev_info(ipts->dev, "IPTS running in event mode\n");
  1996. +
  1997. + while (!ipts_thread_should_stop(thread)) {
  1998. + for (int i = 0; i < IPTS_BUFFERS; i++) {
  1999. + ret = ipts_control_wait_data(ipts, false);
  2000. + if (ret == -EAGAIN)
  2001. + break;
  2002. +
  2003. + if (ret) {
  2004. + dev_err(ipts->dev, "Failed to wait for data: %d\n", ret);
  2005. + continue;
  2006. + }
  2007. +
  2008. + buffer = ipts_receiver_current_doorbell(ipts) % IPTS_BUFFERS;
  2009. + ipts_receiver_next_doorbell(ipts);
  2010. +
  2011. + ret = ipts_hid_input_data(ipts, buffer);
  2012. + if (ret)
  2013. + dev_err(ipts->dev, "Failed to process buffer: %d\n", ret);
  2014. +
  2015. + ret = ipts_control_refill_buffer(ipts, buffer);
  2016. + if (ret)
  2017. + dev_err(ipts->dev, "Failed to send feedback: %d\n", ret);
  2018. +
  2019. + ret = ipts_control_request_data(ipts);
  2020. + if (ret)
  2021. + dev_err(ipts->dev, "Failed to request data: %d\n", ret);
  2022. +
  2023. + last = ktime_get_seconds();
  2024. + }
  2025. +
  2026. + ipts_receiver_backoff(last, 5);
  2027. + }
  2028. +
  2029. + ret = ipts_control_request_flush(ipts);
  2030. + if (ret) {
  2031. + dev_err(ipts->dev, "Failed to request flush: %d\n", ret);
  2032. + return ret;
  2033. + }
  2034. +
  2035. + ret = ipts_control_wait_data(ipts, true);
  2036. + if (ret) {
  2037. + dev_err(ipts->dev, "Failed to wait for data: %d\n", ret);
  2038. +
  2039. + if (ret != -EAGAIN)
  2040. + return ret;
  2041. + else
  2042. + return 0;
  2043. + }
  2044. +
  2045. + ret = ipts_control_wait_flush(ipts);
  2046. + if (ret) {
  2047. + dev_err(ipts->dev, "Failed to wait for flush: %d\n", ret);
  2048. +
  2049. + if (ret != -EAGAIN)
  2050. + return ret;
  2051. + else
  2052. + return 0;
  2053. + }
  2054. +
  2055. + return 0;
  2056. +}
  2057. +
  2058. +static int ipts_receiver_doorbell_loop(struct ipts_thread *thread)
  2059. +{
  2060. + int ret = 0;
  2061. + u32 buffer = 0;
  2062. +
  2063. + u32 doorbell = 0;
  2064. + u32 lastdb = 0;
  2065. +
  2066. + struct ipts_context *ipts = NULL;
  2067. + time64_t last = ktime_get_seconds();
  2068. +
  2069. + if (!thread)
  2070. + return -EFAULT;
  2071. +
  2072. + ipts = thread->data;
  2073. +
  2074. + if (!ipts)
  2075. + return -EFAULT;
  2076. +
  2077. + dev_info(ipts->dev, "IPTS running in doorbell mode\n");
  2078. +
  2079. + while (true) {
  2080. + if (ipts_thread_should_stop(thread)) {
  2081. + ret = ipts_control_request_flush(ipts);
  2082. + if (ret) {
  2083. + dev_err(ipts->dev, "Failed to request flush: %d\n", ret);
  2084. + return ret;
  2085. + }
  2086. + }
  2087. +
  2088. + doorbell = ipts_receiver_current_doorbell(ipts);
  2089. +
  2090. + /*
  2091. + * After filling up one of the data buffers, IPTS will increment
  2092. + * the doorbell. The value of the doorbell stands for the *next*
  2093. + * buffer that IPTS is going to fill.
  2094. + */
  2095. + while (lastdb != doorbell) {
  2096. + buffer = lastdb % IPTS_BUFFERS;
  2097. +
  2098. + ret = ipts_hid_input_data(ipts, buffer);
  2099. + if (ret)
  2100. + dev_err(ipts->dev, "Failed to process buffer: %d\n", ret);
  2101. +
  2102. + ret = ipts_control_refill_buffer(ipts, buffer);
  2103. + if (ret)
  2104. + dev_err(ipts->dev, "Failed to send feedback: %d\n", ret);
  2105. +
  2106. + last = ktime_get_seconds();
  2107. + lastdb++;
  2108. + }
  2109. +
  2110. + if (ipts_thread_should_stop(thread))
  2111. + break;
  2112. +
  2113. + ipts_receiver_backoff(last, 5);
  2114. + }
  2115. +
  2116. + ret = ipts_control_wait_data(ipts, true);
  2117. + if (ret) {
  2118. + dev_err(ipts->dev, "Failed to wait for data: %d\n", ret);
  2119. +
  2120. + if (ret != -EAGAIN)
  2121. + return ret;
  2122. + else
  2123. + return 0;
  2124. + }
  2125. +
  2126. + ret = ipts_control_wait_flush(ipts);
  2127. + if (ret) {
  2128. + dev_err(ipts->dev, "Failed to wait for flush: %d\n", ret);
  2129. +
  2130. + if (ret != -EAGAIN)
  2131. + return ret;
  2132. + else
  2133. + return 0;
  2134. + }
  2135. +
  2136. + return 0;
  2137. +}
  2138. +
  2139. +int ipts_receiver_start(struct ipts_context *ipts)
  2140. +{
  2141. + int ret = 0;
  2142. +
  2143. + if (!ipts)
  2144. + return -EFAULT;
  2145. +
  2146. + if (ipts->mode == IPTS_MODE_EVENT) {
  2147. + ret = ipts_thread_start(&ipts->receiver_loop, ipts_receiver_event_loop, ipts,
  2148. + "ipts_event");
  2149. + } else if (ipts->mode == IPTS_MODE_DOORBELL) {
  2150. + ret = ipts_thread_start(&ipts->receiver_loop, ipts_receiver_doorbell_loop, ipts,
  2151. + "ipts_doorbell");
  2152. + } else {
  2153. + ret = -EINVAL;
  2154. + }
  2155. +
  2156. + if (ret) {
  2157. + dev_err(ipts->dev, "Failed to start receiver loop: %d\n", ret);
  2158. + return ret;
  2159. + }
  2160. +
  2161. + return 0;
  2162. +}
  2163. +
  2164. +int ipts_receiver_stop(struct ipts_context *ipts)
  2165. +{
  2166. + int ret = 0;
  2167. +
  2168. + if (!ipts)
  2169. + return -EFAULT;
  2170. +
  2171. + ret = ipts_thread_stop(&ipts->receiver_loop);
  2172. + if (ret) {
  2173. + dev_err(ipts->dev, "Failed to stop receiver loop: %d\n", ret);
  2174. + return ret;
  2175. + }
  2176. +
  2177. + return 0;
  2178. +}
  2179. diff --git a/drivers/hid/ipts/receiver.h b/drivers/hid/ipts/receiver.h
  2180. new file mode 100644
  2181. index 000000000000..96070f34fbca
  2182. --- /dev/null
  2183. +++ b/drivers/hid/ipts/receiver.h
  2184. @@ -0,0 +1,17 @@
  2185. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2186. +/*
  2187. + * Copyright (c) 2016 Intel Corporation
  2188. + * Copyright (c) 2020-2023 Dorian Stoll
  2189. + *
  2190. + * Linux driver for Intel Precise Touch & Stylus
  2191. + */
  2192. +
  2193. +#ifndef IPTS_RECEIVER_H
  2194. +#define IPTS_RECEIVER_H
  2195. +
  2196. +#include "context.h"
  2197. +
  2198. +int ipts_receiver_start(struct ipts_context *ipts);
  2199. +int ipts_receiver_stop(struct ipts_context *ipts);
  2200. +
  2201. +#endif /* IPTS_RECEIVER_H */
  2202. diff --git a/drivers/hid/ipts/resources.c b/drivers/hid/ipts/resources.c
  2203. new file mode 100644
  2204. index 000000000000..80ba5885bb55
  2205. --- /dev/null
  2206. +++ b/drivers/hid/ipts/resources.c
  2207. @@ -0,0 +1,108 @@
  2208. +// SPDX-License-Identifier: GPL-2.0-or-later
  2209. +/*
  2210. + * Copyright (c) 2016 Intel Corporation
  2211. + * Copyright (c) 2020-2023 Dorian Stoll
  2212. + *
  2213. + * Linux driver for Intel Precise Touch & Stylus
  2214. + */
  2215. +
  2216. +#include <linux/dma-mapping.h>
  2217. +#include <linux/types.h>
  2218. +
  2219. +#include "resources.h"
  2220. +#include "spec-device.h"
  2221. +
  2222. +static int ipts_resources_alloc_buffer(struct ipts_buffer *buffer, struct device *dev, size_t size)
  2223. +{
  2224. + if (!buffer)
  2225. + return -EFAULT;
  2226. +
  2227. + if (buffer->address)
  2228. + return 0;
  2229. +
  2230. + buffer->address = dma_alloc_coherent(dev, size, &buffer->dma_address, GFP_KERNEL);
  2231. +
  2232. + if (!buffer->address)
  2233. + return -ENOMEM;
  2234. +
  2235. + buffer->size = size;
  2236. + buffer->device = dev;
  2237. +
  2238. + return 0;
  2239. +}
  2240. +
  2241. +static void ipts_resources_free_buffer(struct ipts_buffer *buffer)
  2242. +{
  2243. + if (!buffer->address)
  2244. + return;
  2245. +
  2246. + dma_free_coherent(buffer->device, buffer->size, buffer->address, buffer->dma_address);
  2247. +
  2248. + buffer->address = NULL;
  2249. + buffer->size = 0;
  2250. +
  2251. + buffer->dma_address = 0;
  2252. + buffer->device = NULL;
  2253. +}
  2254. +
  2255. +int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t ds, size_t fs)
  2256. +{
  2257. + int ret = 0;
  2258. +
  2259. + if (!res)
  2260. + return -EFAULT;
  2261. +
  2262. + for (int i = 0; i < IPTS_BUFFERS; i++) {
  2263. + ret = ipts_resources_alloc_buffer(&res->data[i], dev, ds);
  2264. + if (ret)
  2265. + goto err;
  2266. + }
  2267. +
  2268. + for (int i = 0; i < IPTS_BUFFERS; i++) {
  2269. + ret = ipts_resources_alloc_buffer(&res->feedback[i], dev, fs);
  2270. + if (ret)
  2271. + goto err;
  2272. + }
  2273. +
  2274. + ret = ipts_resources_alloc_buffer(&res->doorbell, dev, sizeof(u32));
  2275. + if (ret)
  2276. + goto err;
  2277. +
  2278. + ret = ipts_resources_alloc_buffer(&res->workqueue, dev, sizeof(u32));
  2279. + if (ret)
  2280. + goto err;
  2281. +
  2282. + ret = ipts_resources_alloc_buffer(&res->hid2me, dev, fs);
  2283. + if (ret)
  2284. + goto err;
  2285. +
  2286. + ret = ipts_resources_alloc_buffer(&res->descriptor, dev, ds + 8);
  2287. + if (ret)
  2288. + goto err;
  2289. +
  2290. + return 0;
  2291. +
  2292. +err:
  2293. +
  2294. + ipts_resources_free(res);
  2295. + return ret;
  2296. +}
  2297. +
  2298. +int ipts_resources_free(struct ipts_resources *res)
  2299. +{
  2300. + if (!res)
  2301. + return -EFAULT;
  2302. +
  2303. + for (int i = 0; i < IPTS_BUFFERS; i++)
  2304. + ipts_resources_free_buffer(&res->data[i]);
  2305. +
  2306. + for (int i = 0; i < IPTS_BUFFERS; i++)
  2307. + ipts_resources_free_buffer(&res->feedback[i]);
  2308. +
  2309. + ipts_resources_free_buffer(&res->doorbell);
  2310. + ipts_resources_free_buffer(&res->workqueue);
  2311. + ipts_resources_free_buffer(&res->hid2me);
  2312. + ipts_resources_free_buffer(&res->descriptor);
  2313. +
  2314. + return 0;
  2315. +}
  2316. diff --git a/drivers/hid/ipts/resources.h b/drivers/hid/ipts/resources.h
  2317. new file mode 100644
  2318. index 000000000000..6cbb24a8a054
  2319. --- /dev/null
  2320. +++ b/drivers/hid/ipts/resources.h
  2321. @@ -0,0 +1,39 @@
  2322. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2323. +/*
  2324. + * Copyright (c) 2016 Intel Corporation
  2325. + * Copyright (c) 2020-2023 Dorian Stoll
  2326. + *
  2327. + * Linux driver for Intel Precise Touch & Stylus
  2328. + */
  2329. +
  2330. +#ifndef IPTS_RESOURCES_H
  2331. +#define IPTS_RESOURCES_H
  2332. +
  2333. +#include <linux/device.h>
  2334. +#include <linux/types.h>
  2335. +
  2336. +#include "spec-device.h"
  2337. +
  2338. +struct ipts_buffer {
  2339. + u8 *address;
  2340. + size_t size;
  2341. +
  2342. + dma_addr_t dma_address;
  2343. + struct device *device;
  2344. +};
  2345. +
  2346. +struct ipts_resources {
  2347. + struct ipts_buffer data[IPTS_BUFFERS];
  2348. + struct ipts_buffer feedback[IPTS_BUFFERS];
  2349. +
  2350. + struct ipts_buffer doorbell;
  2351. + struct ipts_buffer workqueue;
  2352. + struct ipts_buffer hid2me;
  2353. +
  2354. + struct ipts_buffer descriptor;
  2355. +};
  2356. +
  2357. +int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t ds, size_t fs);
  2358. +int ipts_resources_free(struct ipts_resources *res);
  2359. +
  2360. +#endif /* IPTS_RESOURCES_H */
  2361. diff --git a/drivers/hid/ipts/spec-data.h b/drivers/hid/ipts/spec-data.h
  2362. new file mode 100644
  2363. index 000000000000..e8dd98895a7e
  2364. --- /dev/null
  2365. +++ b/drivers/hid/ipts/spec-data.h
  2366. @@ -0,0 +1,100 @@
  2367. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2368. +/*
  2369. + * Copyright (c) 2016 Intel Corporation
  2370. + * Copyright (c) 2020-2023 Dorian Stoll
  2371. + *
  2372. + * Linux driver for Intel Precise Touch & Stylus
  2373. + */
  2374. +
  2375. +#ifndef IPTS_SPEC_DATA_H
  2376. +#define IPTS_SPEC_DATA_H
  2377. +
  2378. +#include <linux/build_bug.h>
  2379. +#include <linux/types.h>
  2380. +
  2381. +/**
  2382. + * enum ipts_feedback_cmd_type - Commands that can be executed on the sensor through feedback.
  2383. + */
  2384. +enum ipts_feedback_cmd_type {
  2385. + IPTS_FEEDBACK_CMD_TYPE_NONE = 0,
  2386. + IPTS_FEEDBACK_CMD_TYPE_SOFT_RESET = 1,
  2387. + IPTS_FEEDBACK_CMD_TYPE_GOTO_ARMED = 2,
  2388. + IPTS_FEEDBACK_CMD_TYPE_GOTO_SENSING = 3,
  2389. + IPTS_FEEDBACK_CMD_TYPE_GOTO_SLEEP = 4,
  2390. + IPTS_FEEDBACK_CMD_TYPE_GOTO_DOZE = 5,
  2391. + IPTS_FEEDBACK_CMD_TYPE_HARD_RESET = 6,
  2392. +};
  2393. +
  2394. +/**
  2395. + * enum ipts_feedback_data_type - Defines what data a feedback buffer contains.
  2396. + * @IPTS_FEEDBACK_DATA_TYPE_VENDOR: The buffer contains vendor specific feedback.
  2397. + * @IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES: The buffer contains a HID set features report.
  2398. + * @IPTS_FEEDBACK_DATA_TYPE_GET_FEATURES: The buffer contains a HID get features report.
  2399. + * @IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT: The buffer contains a HID output report.
  2400. + * @IPTS_FEEDBACK_DATA_TYPE_STORE_DATA: The buffer contains calibration data for the sensor.
  2401. + */
  2402. +enum ipts_feedback_data_type {
  2403. + IPTS_FEEDBACK_DATA_TYPE_VENDOR = 0,
  2404. + IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES = 1,
  2405. + IPTS_FEEDBACK_DATA_TYPE_GET_FEATURES = 2,
  2406. + IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT = 3,
  2407. + IPTS_FEEDBACK_DATA_TYPE_STORE_DATA = 4,
  2408. +};
  2409. +
  2410. +/**
  2411. + * struct ipts_feedback_header - Header that is prefixed to the data in a feedback buffer.
  2412. + * @cmd_type: A command that should be executed on the sensor.
  2413. + * @size: The size of the payload to be written.
  2414. + * @buffer: The ID of the buffer that contains this feedback data.
  2415. + * @protocol: The protocol version of the EDS.
  2416. + * @data_type: The type of data that the buffer contains.
  2417. + * @spi_offset: The offset at which to write the payload data to the sensor.
  2418. + * @payload: Payload for the feedback command, or 0 if no payload is sent.
  2419. + */
  2420. +struct ipts_feedback_header {
  2421. + enum ipts_feedback_cmd_type cmd_type;
  2422. + u32 size;
  2423. + u32 buffer;
  2424. + u32 protocol;
  2425. + enum ipts_feedback_data_type data_type;
  2426. + u32 spi_offset;
  2427. + u8 reserved[40];
  2428. + u8 payload[];
  2429. +} __packed;
  2430. +
  2431. +static_assert(sizeof(struct ipts_feedback_header) == 64);
  2432. +
  2433. +/**
  2434. + * enum ipts_data_type - Defines what type of data a buffer contains.
  2435. + * @IPTS_DATA_TYPE_FRAME: Raw data frame.
  2436. + * @IPTS_DATA_TYPE_ERROR: Error data.
  2437. + * @IPTS_DATA_TYPE_VENDOR: Vendor specific data.
  2438. + * @IPTS_DATA_TYPE_HID: A HID report.
  2439. + * @IPTS_DATA_TYPE_GET_FEATURES: The response to a GET_FEATURES HID2ME command.
  2440. + */
  2441. +enum ipts_data_type {
  2442. + IPTS_DATA_TYPE_FRAME = 0x00,
  2443. + IPTS_DATA_TYPE_ERROR = 0x01,
  2444. + IPTS_DATA_TYPE_VENDOR = 0x02,
  2445. + IPTS_DATA_TYPE_HID = 0x03,
  2446. + IPTS_DATA_TYPE_GET_FEATURES = 0x04,
  2447. + IPTS_DATA_TYPE_DESCRIPTOR = 0x05,
  2448. +};
  2449. +
  2450. +/**
  2451. + * struct ipts_data_header - Header that is prefixed to the data in a data buffer.
  2452. + * @type: What data the buffer contains.
  2453. + * @size: How much data the buffer contains.
  2454. + * @buffer: Which buffer the data is in.
  2455. + */
  2456. +struct ipts_data_header {
  2457. + enum ipts_data_type type;
  2458. + u32 size;
  2459. + u32 buffer;
  2460. + u8 reserved[52];
  2461. + u8 data[];
  2462. +} __packed;
  2463. +
  2464. +static_assert(sizeof(struct ipts_data_header) == 64);
  2465. +
  2466. +#endif /* IPTS_SPEC_DATA_H */
  2467. diff --git a/drivers/hid/ipts/spec-device.h b/drivers/hid/ipts/spec-device.h
  2468. new file mode 100644
  2469. index 000000000000..93f673d981f7
  2470. --- /dev/null
  2471. +++ b/drivers/hid/ipts/spec-device.h
  2472. @@ -0,0 +1,285 @@
  2473. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2474. +/*
  2475. + * Copyright (c) 2016 Intel Corporation
  2476. + * Copyright (c) 2020-2023 Dorian Stoll
  2477. + *
  2478. + * Linux driver for Intel Precise Touch & Stylus
  2479. + */
  2480. +
  2481. +#ifndef IPTS_SPEC_DEVICE_H
  2482. +#define IPTS_SPEC_DEVICE_H
  2483. +
  2484. +#include <linux/build_bug.h>
  2485. +#include <linux/types.h>
  2486. +
  2487. +/*
  2488. + * The amount of buffers that IPTS can use for data transfer.
  2489. + */
  2490. +#define IPTS_BUFFERS 16
  2491. +
  2492. +/*
  2493. + * The buffer ID that is used for HID2ME feedback
  2494. + */
  2495. +#define IPTS_HID2ME_BUFFER IPTS_BUFFERS
  2496. +
  2497. +/**
  2498. + * enum ipts_command - Commands that can be sent to the IPTS hardware.
  2499. + * @IPTS_CMD_GET_DEVICE_INFO: Retrieves vendor information from the device.
  2500. + * @IPTS_CMD_SET_MODE: Changes the mode that the device will operate in.
  2501. + * @IPTS_CMD_SET_MEM_WINDOW: Configures memory buffers for passing data between device and driver.
  2502. + * @IPTS_CMD_QUIESCE_IO: Stops the data flow from the device to the driver.
  2503. + * @IPTS_CMD_READY_FOR_DATA: Informs the device that the driver is ready to receive data.
  2504. + * @IPTS_CMD_FEEDBACK: Informs the device that a buffer was processed and can be refilled.
  2505. + * @IPTS_CMD_CLEAR_MEM_WINDOW: Stops the data flow and clears the buffer addresses on the device.
  2506. + * @IPTS_CMD_RESET_SENSOR: Resets the sensor to its default state.
  2507. + * @IPTS_CMD_GET_DESCRIPTOR: Retrieves the HID descriptor of the device.
  2508. + */
  2509. +enum ipts_command_code {
  2510. + IPTS_CMD_GET_DEVICE_INFO = 0x01,
  2511. + IPTS_CMD_SET_MODE = 0x02,
  2512. + IPTS_CMD_SET_MEM_WINDOW = 0x03,
  2513. + IPTS_CMD_QUIESCE_IO = 0x04,
  2514. + IPTS_CMD_READY_FOR_DATA = 0x05,
  2515. + IPTS_CMD_FEEDBACK = 0x06,
  2516. + IPTS_CMD_CLEAR_MEM_WINDOW = 0x07,
  2517. + IPTS_CMD_RESET_SENSOR = 0x0B,
  2518. + IPTS_CMD_GET_DESCRIPTOR = 0x0F,
  2519. +};
  2520. +
  2521. +/**
  2522. + * enum ipts_status - Possible status codes returned by the IPTS device.
  2523. + * @IPTS_STATUS_SUCCESS: Operation completed successfully.
  2524. + * @IPTS_STATUS_INVALID_PARAMS: Command contained an invalid payload.
  2525. + * @IPTS_STATUS_ACCESS_DENIED: ME could not validate a buffer address.
  2526. + * @IPTS_STATUS_CMD_SIZE_ERROR: Command contains an invalid payload.
  2527. + * @IPTS_STATUS_NOT_READY: Buffer addresses have not been set.
  2528. + * @IPTS_STATUS_REQUEST_OUTSTANDING: There is an outstanding command of the same type.
  2529. + * @IPTS_STATUS_NO_SENSOR_FOUND: No sensor could be found.
  2530. + * @IPTS_STATUS_OUT_OF_MEMORY: Not enough free memory for requested operation.
  2531. + * @IPTS_STATUS_INTERNAL_ERROR: An unexpected error occurred.
  2532. + * @IPTS_STATUS_SENSOR_DISABLED: The sensor has been disabled and must be reinitialized.
  2533. + * @IPTS_STATUS_COMPAT_CHECK_FAIL: Compatibility revision check between sensor and ME failed.
  2534. + * The host can ignore this error and attempt to continue.
  2535. + * @IPTS_STATUS_SENSOR_EXPECTED_RESET: The sensor went through a reset initiated by the driver.
  2536. + * @IPTS_STATUS_SENSOR_UNEXPECTED_RESET: The sensor went through an unexpected reset.
  2537. + * @IPTS_STATUS_RESET_FAILED: Requested sensor reset failed to complete.
  2538. + * @IPTS_STATUS_TIMEOUT: The operation timed out.
  2539. + * @IPTS_STATUS_TEST_MODE_FAIL: Test mode pattern did not match expected values.
  2540. + * @IPTS_STATUS_SENSOR_FAIL_FATAL: The sensor reported an error during reset sequence.
  2541. + * Further progress is not possible.
  2542. + * @IPTS_STATUS_SENSOR_FAIL_NONFATAL: The sensor reported an error during reset sequence.
  2543. + * The driver can attempt to continue.
  2544. + * @IPTS_STATUS_INVALID_DEVICE_CAPS: The device reported invalid capabilities.
  2545. + * @IPTS_STATUS_QUIESCE_IO_IN_PROGRESS: Command cannot be completed until Quiesce IO is done.
  2546. + */
  2547. +enum ipts_status {
  2548. + IPTS_STATUS_SUCCESS = 0x00,
  2549. + IPTS_STATUS_INVALID_PARAMS = 0x01,
  2550. + IPTS_STATUS_ACCESS_DENIED = 0x02,
  2551. + IPTS_STATUS_CMD_SIZE_ERROR = 0x03,
  2552. + IPTS_STATUS_NOT_READY = 0x04,
  2553. + IPTS_STATUS_REQUEST_OUTSTANDING = 0x05,
  2554. + IPTS_STATUS_NO_SENSOR_FOUND = 0x06,
  2555. + IPTS_STATUS_OUT_OF_MEMORY = 0x07,
  2556. + IPTS_STATUS_INTERNAL_ERROR = 0x08,
  2557. + IPTS_STATUS_SENSOR_DISABLED = 0x09,
  2558. + IPTS_STATUS_COMPAT_CHECK_FAIL = 0x0A,
  2559. + IPTS_STATUS_SENSOR_EXPECTED_RESET = 0x0B,
  2560. + IPTS_STATUS_SENSOR_UNEXPECTED_RESET = 0x0C,
  2561. + IPTS_STATUS_RESET_FAILED = 0x0D,
  2562. + IPTS_STATUS_TIMEOUT = 0x0E,
  2563. + IPTS_STATUS_TEST_MODE_FAIL = 0x0F,
  2564. + IPTS_STATUS_SENSOR_FAIL_FATAL = 0x10,
  2565. + IPTS_STATUS_SENSOR_FAIL_NONFATAL = 0x11,
  2566. + IPTS_STATUS_INVALID_DEVICE_CAPS = 0x12,
  2567. + IPTS_STATUS_QUIESCE_IO_IN_PROGRESS = 0x13,
  2568. +};
  2569. +
  2570. +/**
  2571. + * struct ipts_command - Message that is sent to the device for calling a command.
  2572. + * @cmd: The command that will be called.
  2573. + * @payload: Payload containing parameters for the called command.
  2574. + */
  2575. +struct ipts_command {
  2576. + enum ipts_command_code cmd;
  2577. + u8 payload[320];
  2578. +} __packed;
  2579. +
  2580. +static_assert(sizeof(struct ipts_command) == 324);
  2581. +
  2582. +/**
  2583. + * enum ipts_mode - Configures what data the device produces and how its sent.
  2584. + * @IPTS_MODE_EVENT: The device will send an event once a buffer was filled.
  2585. + * Older devices will return singletouch data in this mode.
  2586. + * @IPTS_MODE_DOORBELL: The device will notify the driver by incrementing the doorbell value.
  2587. + * Older devices will return multitouch data in this mode.
  2588. + */
  2589. +enum ipts_mode {
  2590. + IPTS_MODE_EVENT = 0x00,
  2591. + IPTS_MODE_DOORBELL = 0x01,
  2592. +};
  2593. +
  2594. +/**
  2595. + * struct ipts_set_mode - Payload for the SET_MODE command.
  2596. + * @mode: Changes the mode that IPTS will operate in.
  2597. + */
  2598. +struct ipts_set_mode {
  2599. + enum ipts_mode mode;
  2600. + u8 reserved[12];
  2601. +} __packed;
  2602. +
  2603. +static_assert(sizeof(struct ipts_set_mode) == 16);
  2604. +
  2605. +#define IPTS_WORKQUEUE_SIZE 8192
  2606. +#define IPTS_WORKQUEUE_ITEM_SIZE 16
  2607. +
  2608. +/**
  2609. + * struct ipts_mem_window - Payload for the SET_MEM_WINDOW command.
  2610. + * @data_addr_lower: Lower 32 bits of the data buffer addresses.
  2611. + * @data_addr_upper: Upper 32 bits of the data buffer addresses.
  2612. + * @workqueue_addr_lower: Lower 32 bits of the workqueue buffer address.
  2613. + * @workqueue_addr_upper: Upper 32 bits of the workqueue buffer address.
  2614. + * @doorbell_addr_lower: Lower 32 bits of the doorbell buffer address.
  2615. + * @doorbell_addr_upper: Upper 32 bits of the doorbell buffer address.
  2616. + * @feedbackaddr_lower: Lower 32 bits of the feedback buffer addresses.
  2617. + * @feedbackaddr_upper: Upper 32 bits of the feedback buffer addresses.
  2618. + * @hid2me_addr_lower: Lower 32 bits of the hid2me buffer address.
  2619. + * @hid2me_addr_upper: Upper 32 bits of the hid2me buffer address.
  2620. + * @hid2me_size: Size of the hid2me feedback buffer.
  2621. + * @workqueue_item_size: Magic value. Must be 16.
  2622. + * @workqueue_size: Magic value. Must be 8192.
  2623. + *
  2624. + * The workqueue related items in this struct are required for using
  2625. + * GuC submission with binary processing firmware. Since this driver does
  2626. + * not use GuC submission and instead exports raw data to userspace, these
  2627. + * items are not actually used, but they need to be allocated and passed
  2628. + * to the device, otherwise initialization will fail.
  2629. + */
  2630. +struct ipts_mem_window {
  2631. + u32 data_addr_lower[IPTS_BUFFERS];
  2632. + u32 data_addr_upper[IPTS_BUFFERS];
  2633. + u32 workqueue_addr_lower;
  2634. + u32 workqueue_addr_upper;
  2635. + u32 doorbell_addr_lower;
  2636. + u32 doorbell_addr_upper;
  2637. + u32 feedback_addr_lower[IPTS_BUFFERS];
  2638. + u32 feedback_addr_upper[IPTS_BUFFERS];
  2639. + u32 hid2me_addr_lower;
  2640. + u32 hid2me_addr_upper;
  2641. + u32 hid2me_size;
  2642. + u8 reserved1;
  2643. + u8 workqueue_item_size;
  2644. + u16 workqueue_size;
  2645. + u8 reserved[32];
  2646. +} __packed;
  2647. +
  2648. +static_assert(sizeof(struct ipts_mem_window) == 320);
  2649. +
  2650. +/**
  2651. + * struct ipts_quiesce_io - Payload for the QUIESCE_IO command.
  2652. + */
  2653. +struct ipts_quiesce_io {
  2654. + u8 reserved[12];
  2655. +} __packed;
  2656. +
  2657. +static_assert(sizeof(struct ipts_quiesce_io) == 12);
  2658. +
  2659. +/**
  2660. + * struct ipts_feedback - Payload for the FEEDBACK command.
  2661. + * @buffer: The buffer that the device should refill.
  2662. + */
  2663. +struct ipts_feedback {
  2664. + u32 buffer;
  2665. + u8 reserved[12];
  2666. +} __packed;
  2667. +
  2668. +static_assert(sizeof(struct ipts_feedback) == 16);
  2669. +
  2670. +/**
  2671. + * enum ipts_reset_type - Possible ways of resetting the device.
  2672. + * @IPTS_RESET_TYPE_HARD: Perform hardware reset using GPIO pin.
  2673. + * @IPTS_RESET_TYPE_SOFT: Perform software reset using SPI command.
  2674. + */
  2675. +enum ipts_reset_type {
  2676. + IPTS_RESET_TYPE_HARD = 0x00,
  2677. + IPTS_RESET_TYPE_SOFT = 0x01,
  2678. +};
  2679. +
  2680. +/**
  2681. + * struct ipts_reset - Payload for the RESET_SENSOR command.
  2682. + * @type: How the device should get reset.
  2683. + */
  2684. +struct ipts_reset_sensor {
  2685. + enum ipts_reset_type type;
  2686. + u8 reserved[4];
  2687. +} __packed;
  2688. +
  2689. +static_assert(sizeof(struct ipts_reset_sensor) == 8);
  2690. +
  2691. +/**
  2692. + * struct ipts_get_descriptor - Payload for the GET_DESCRIPTOR command.
  2693. + * @addr_lower: The lower 32 bits of the descriptor buffer address.
  2694. + * @addr_upper: The upper 32 bits of the descriptor buffer address.
  2695. + * @magic: A magic value. Must be 8.
  2696. + */
  2697. +struct ipts_get_descriptor {
  2698. + u32 addr_lower;
  2699. + u32 addr_upper;
  2700. + u32 magic;
  2701. + u8 reserved[12];
  2702. +} __packed;
  2703. +
  2704. +static_assert(sizeof(struct ipts_get_descriptor) == 24);
  2705. +
  2706. +/*
  2707. + * The type of a response is indicated by a
  2708. + * command code, with the most significant bit flipped to 1.
  2709. + */
  2710. +#define IPTS_RSP_BIT BIT(31)
  2711. +
  2712. +/**
  2713. + * struct ipts_response - Data returned from the device in response to a command.
  2714. + * @cmd: The command that this response answers (IPTS_RSP_BIT will be 1).
  2715. + * @status: The return code of the command.
  2716. + * @payload: The data that was produced by the command.
  2717. + */
  2718. +struct ipts_response {
  2719. + enum ipts_command_code cmd;
  2720. + enum ipts_status status;
  2721. + u8 payload[80];
  2722. +} __packed;
  2723. +
  2724. +static_assert(sizeof(struct ipts_response) == 88);
  2725. +
  2726. +/**
  2727. + * struct ipts_device_info - Vendor information of the IPTS device.
  2728. + * @vendor: Vendor ID of this device.
  2729. + * @product: Product ID of this device.
  2730. + * @hw_version: Hardware revision of this device.
  2731. + * @fw_version: Firmware revision of this device.
  2732. + * @data_size: Requested size for a data buffer.
  2733. + * @feedback_size: Requested size for a feedback buffer.
  2734. + * @mode: Mode that the device currently operates in.
  2735. + * @max_contacts: Maximum amount of concurrent touches the sensor can process.
  2736. + */
  2737. +struct ipts_device_info {
  2738. + u16 vendor;
  2739. + u16 product;
  2740. + u32 hw_version;
  2741. + u32 fw_version;
  2742. + u32 data_size;
  2743. + u32 feedback_size;
  2744. + enum ipts_mode mode;
  2745. + u8 max_contacts;
  2746. + u8 reserved1[3];
  2747. + u8 sensor_min_eds;
  2748. + u8 sensor_maj_eds;
  2749. + u8 me_min_eds;
  2750. + u8 me_maj_eds;
  2751. + u8 intf_eds;
  2752. + u8 reserved2[11];
  2753. +} __packed;
  2754. +
  2755. +static_assert(sizeof(struct ipts_device_info) == 44);
  2756. +
  2757. +#endif /* IPTS_SPEC_DEVICE_H */
  2758. diff --git a/drivers/hid/ipts/spec-hid.h b/drivers/hid/ipts/spec-hid.h
  2759. new file mode 100644
  2760. index 000000000000..ea70f29ff00c
  2761. --- /dev/null
  2762. +++ b/drivers/hid/ipts/spec-hid.h
  2763. @@ -0,0 +1,35 @@
  2764. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2765. +/*
  2766. + * Copyright (c) 2016 Intel Corporation
  2767. + * Copyright (c) 2020-2023 Dorian Stoll
  2768. + *
  2769. + * Linux driver for Intel Precise Touch & Stylus
  2770. + */
  2771. +
  2772. +#ifndef IPTS_SPEC_HID_H
  2773. +#define IPTS_SPEC_HID_H
  2774. +
  2775. +#include <linux/build_bug.h>
  2776. +#include <linux/types.h>
  2777. +
  2778. +/*
  2779. + * Made-up type for passing raw IPTS data in a HID report.
  2780. + */
  2781. +#define IPTS_HID_FRAME_TYPE_RAW 0xEE
  2782. +
  2783. +/**
  2784. + * struct ipts_hid_frame - Header that is prefixed to raw IPTS data wrapped in a HID report.
  2785. + * @size: Size of the data inside the report, including this header.
  2786. + * @type: What type of data does this report contain.
  2787. + */
  2788. +struct ipts_hid_header {
  2789. + u32 size;
  2790. + u8 reserved1;
  2791. + u8 type;
  2792. + u8 reserved2;
  2793. + u8 data[];
  2794. +} __packed;
  2795. +
  2796. +static_assert(sizeof(struct ipts_hid_header) == 7);
  2797. +
  2798. +#endif /* IPTS_SPEC_HID_H */
  2799. diff --git a/drivers/hid/ipts/thread.c b/drivers/hid/ipts/thread.c
  2800. new file mode 100644
  2801. index 000000000000..8b46f775c107
  2802. --- /dev/null
  2803. +++ b/drivers/hid/ipts/thread.c
  2804. @@ -0,0 +1,85 @@
  2805. +// SPDX-License-Identifier: GPL-2.0-or-later
  2806. +/*
  2807. + * Copyright (c) 2016 Intel Corporation
  2808. + * Copyright (c) 2023 Dorian Stoll
  2809. + *
  2810. + * Linux driver for Intel Precise Touch & Stylus
  2811. + */
  2812. +
  2813. +#include <linux/completion.h>
  2814. +#include <linux/err.h>
  2815. +#include <linux/kthread.h>
  2816. +#include <linux/mutex.h>
  2817. +
  2818. +#include "thread.h"
  2819. +
  2820. +bool ipts_thread_should_stop(struct ipts_thread *thread)
  2821. +{
  2822. + if (!thread)
  2823. + return false;
  2824. +
  2825. + return READ_ONCE(thread->should_stop);
  2826. +}
  2827. +
  2828. +static int ipts_thread_runner(void *data)
  2829. +{
  2830. + int ret = 0;
  2831. + struct ipts_thread *thread = data;
  2832. +
  2833. + if (!thread)
  2834. + return -EFAULT;
  2835. +
  2836. + if (!thread->threadfn)
  2837. + return -EFAULT;
  2838. +
  2839. + ret = thread->threadfn(thread);
  2840. + complete_all(&thread->done);
  2841. +
  2842. + return ret;
  2843. +}
  2844. +
  2845. +int ipts_thread_start(struct ipts_thread *thread, int (*threadfn)(struct ipts_thread *thread),
  2846. + void *data, const char *name)
  2847. +{
  2848. + if (!thread)
  2849. + return -EFAULT;
  2850. +
  2851. + if (!threadfn)
  2852. + return -EFAULT;
  2853. +
  2854. + init_completion(&thread->done);
  2855. +
  2856. + thread->data = data;
  2857. + thread->should_stop = false;
  2858. + thread->threadfn = threadfn;
  2859. +
  2860. + thread->thread = kthread_run(ipts_thread_runner, thread, name);
  2861. + return PTR_ERR_OR_ZERO(thread->thread);
  2862. +}
  2863. +
  2864. +int ipts_thread_stop(struct ipts_thread *thread)
  2865. +{
  2866. + int ret = 0;
  2867. +
  2868. + if (!thread)
  2869. + return -EFAULT;
  2870. +
  2871. + if (!thread->thread)
  2872. + return 0;
  2873. +
  2874. + WRITE_ONCE(thread->should_stop, true);
  2875. +
  2876. + /*
  2877. + * Make sure that the write has gone through before waiting.
  2878. + */
  2879. + wmb();
  2880. +
  2881. + wait_for_completion(&thread->done);
  2882. + ret = kthread_stop(thread->thread);
  2883. +
  2884. + thread->thread = NULL;
  2885. + thread->data = NULL;
  2886. + thread->threadfn = NULL;
  2887. +
  2888. + return ret;
  2889. +}
  2890. diff --git a/drivers/hid/ipts/thread.h b/drivers/hid/ipts/thread.h
  2891. new file mode 100644
  2892. index 000000000000..a314843599fc
  2893. --- /dev/null
  2894. +++ b/drivers/hid/ipts/thread.h
  2895. @@ -0,0 +1,60 @@
  2896. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2897. +/*
  2898. + * Copyright (c) 2016 Intel Corporation
  2899. + * Copyright (c) 2023 Dorian Stoll
  2900. + *
  2901. + * Linux driver for Intel Precise Touch & Stylus
  2902. + */
  2903. +
  2904. +#ifndef IPTS_THREAD_H
  2905. +#define IPTS_THREAD_H
  2906. +
  2907. +#include <linux/completion.h>
  2908. +#include <linux/mutex.h>
  2909. +#include <linux/sched.h>
  2910. +
  2911. +/*
  2912. + * This wrapper over kthread is necessary, because calling kthread_stop makes it impossible
  2913. + * to issue MEI commands from that thread while it shuts itself down. By using a custom
  2914. + * boolean variable and a completion object, we can call kthread_stop only when the thread
  2915. + * already finished all of its work and has returned.
  2916. + */
  2917. +struct ipts_thread {
  2918. + struct task_struct *thread;
  2919. +
  2920. + bool should_stop;
  2921. + struct completion done;
  2922. +
  2923. + void *data;
  2924. + int (*threadfn)(struct ipts_thread *thread);
  2925. +};
  2926. +
  2927. +/*
  2928. + * ipts_thread_should_stop() - Returns true if the thread is asked to terminate.
  2929. + * @thread: The current thread.
  2930. + *
  2931. + * Returns: true if the thread should stop, false if not.
  2932. + */
  2933. +bool ipts_thread_should_stop(struct ipts_thread *thread);
  2934. +
  2935. +/*
  2936. + * ipts_thread_start() - Starts an IPTS thread.
  2937. + * @thread: The thread to initialize and start.
  2938. + * @threadfn: The function to execute.
  2939. + * @data: An argument that will be passed to threadfn.
  2940. + * @name: The name of the new thread.
  2941. + *
  2942. + * Returns: 0 on success, <0 on error.
  2943. + */
  2944. +int ipts_thread_start(struct ipts_thread *thread, int (*threadfn)(struct ipts_thread *thread),
  2945. + void *data, const char name[]);
  2946. +
  2947. +/*
  2948. + * ipts_thread_stop() - Asks the thread to terminate and waits until it has finished.
  2949. + * @thread: The thread that should stop.
  2950. + *
  2951. + * Returns: The return value of the thread function.
  2952. + */
  2953. +int ipts_thread_stop(struct ipts_thread *thread);
  2954. +
  2955. +#endif /* IPTS_THREAD_H */
  2956. --
  2957. 2.41.0
  2958. From 29b1919585750c0a6ac5d017e1acc8ad6e1aa728 Mon Sep 17 00:00:00 2001
  2959. From: Dorian Stoll <dorian.stoll@tmsp.io>
  2960. Date: Fri, 28 Apr 2023 15:41:12 +0200
  2961. Subject: [PATCH] Update IPTS from module repo
  2962. Changes:
  2963. * Fix redefinition error on AOSP clang
  2964. * Increase the polling frequency to reduce latency
  2965. * Don't allocate a new buffer for every HID report
  2966. * Always use the generic HID driver instead of forcing hid-multitouch
  2967. Based on https://github.com/linux-surface/intel-precise-touch/commit/a2b675d72dbde80ebe36a5b6ceaebd596c030314
  2968. Signed-off-by: Dorian Stoll <dorian.stoll@tmsp.io>
  2969. Patchset: ipts
  2970. ---
  2971. drivers/hid/ipts/hid.c | 15 +++++----------
  2972. drivers/hid/ipts/receiver.c | 2 +-
  2973. drivers/hid/ipts/resources.c | 35 +++++++++++++++++++++++++++++++----
  2974. drivers/hid/ipts/resources.h | 3 +++
  2975. 4 files changed, 40 insertions(+), 15 deletions(-)
  2976. diff --git a/drivers/hid/ipts/hid.c b/drivers/hid/ipts/hid.c
  2977. index 6782394e8dde..a2471219615b 100644
  2978. --- a/drivers/hid/ipts/hid.c
  2979. +++ b/drivers/hid/ipts/hid.c
  2980. @@ -237,7 +237,6 @@ static struct hid_ll_driver ipts_hid_driver = {
  2981. int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer)
  2982. {
  2983. - int ret = 0;
  2984. u8 *temp = NULL;
  2985. struct ipts_hid_header *frame = NULL;
  2986. struct ipts_data_header *header = NULL;
  2987. @@ -250,6 +249,9 @@ int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer)
  2988. header = (struct ipts_data_header *)ipts->resources.data[buffer].address;
  2989. + temp = ipts->resources.report.address;
  2990. + memset(temp, 0, ipts->resources.report.size);
  2991. +
  2992. if (!header)
  2993. return -EFAULT;
  2994. @@ -273,10 +275,6 @@ int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer)
  2995. if (header->size + 3 + sizeof(struct ipts_hid_header) > IPTS_HID_REPORT_DATA_SIZE)
  2996. return -ERANGE;
  2997. - temp = kzalloc(IPTS_HID_REPORT_DATA_SIZE, GFP_KERNEL);
  2998. - if (!temp)
  2999. - return -ENOMEM;
  3000. -
  3001. /*
  3002. * Synthesize a HID report matching the devices that natively send HID reports
  3003. */
  3004. @@ -288,10 +286,7 @@ int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer)
  3005. memcpy(frame->data, header->data, header->size);
  3006. - ret = hid_input_report(ipts->hid, HID_INPUT_REPORT, temp, IPTS_HID_REPORT_DATA_SIZE, 1);
  3007. - kfree(temp);
  3008. -
  3009. - return ret;
  3010. + return hid_input_report(ipts->hid, HID_INPUT_REPORT, temp, IPTS_HID_REPORT_DATA_SIZE, 1);
  3011. }
  3012. int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info)
  3013. @@ -318,7 +313,7 @@ int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info)
  3014. ipts->hid->vendor = info.vendor;
  3015. ipts->hid->product = info.product;
  3016. - ipts->hid->group = HID_GROUP_MULTITOUCH;
  3017. + ipts->hid->group = HID_GROUP_GENERIC;
  3018. snprintf(ipts->hid->name, sizeof(ipts->hid->name), "IPTS %04X:%04X", info.vendor,
  3019. info.product);
  3020. diff --git a/drivers/hid/ipts/receiver.c b/drivers/hid/ipts/receiver.c
  3021. index 77234f9e0e17..f56e9ed32d57 100644
  3022. --- a/drivers/hid/ipts/receiver.c
  3023. +++ b/drivers/hid/ipts/receiver.c
  3024. @@ -42,7 +42,7 @@ static void ipts_receiver_backoff(time64_t last, u32 n)
  3025. * n seconds, sleep longer to avoid wasting CPU cycles.
  3026. */
  3027. if (last + n > ktime_get_seconds())
  3028. - msleep(20);
  3029. + usleep_range(1 * USEC_PER_MSEC, 5 * USEC_PER_MSEC);
  3030. else
  3031. msleep(200);
  3032. }
  3033. diff --git a/drivers/hid/ipts/resources.c b/drivers/hid/ipts/resources.c
  3034. index 80ba5885bb55..5e924d58c488 100644
  3035. --- a/drivers/hid/ipts/resources.c
  3036. +++ b/drivers/hid/ipts/resources.c
  3037. @@ -9,6 +9,7 @@
  3038. #include <linux/dma-mapping.h>
  3039. #include <linux/types.h>
  3040. +#include "desc.h"
  3041. #include "resources.h"
  3042. #include "spec-device.h"
  3043. @@ -49,16 +50,22 @@ int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t d
  3044. {
  3045. int ret = 0;
  3046. + /*
  3047. + * Some compilers (AOSP clang) complain about a redefined
  3048. + * variable when this is declared inside of the for loop.
  3049. + */
  3050. + int i = 0;
  3051. +
  3052. if (!res)
  3053. return -EFAULT;
  3054. - for (int i = 0; i < IPTS_BUFFERS; i++) {
  3055. + for (i = 0; i < IPTS_BUFFERS; i++) {
  3056. ret = ipts_resources_alloc_buffer(&res->data[i], dev, ds);
  3057. if (ret)
  3058. goto err;
  3059. }
  3060. - for (int i = 0; i < IPTS_BUFFERS; i++) {
  3061. + for (i = 0; i < IPTS_BUFFERS; i++) {
  3062. ret = ipts_resources_alloc_buffer(&res->feedback[i], dev, fs);
  3063. if (ret)
  3064. goto err;
  3065. @@ -80,6 +87,16 @@ int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t d
  3066. if (ret)
  3067. goto err;
  3068. + if (!res->report.address) {
  3069. + res->report.size = IPTS_HID_REPORT_DATA_SIZE;
  3070. + res->report.address = kzalloc(res->report.size, GFP_KERNEL);
  3071. +
  3072. + if (!res->report.address) {
  3073. + ret = -ENOMEM;
  3074. + goto err;
  3075. + }
  3076. + }
  3077. +
  3078. return 0;
  3079. err:
  3080. @@ -90,13 +107,19 @@ int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t d
  3081. int ipts_resources_free(struct ipts_resources *res)
  3082. {
  3083. + /*
  3084. + * Some compilers (AOSP clang) complain about a redefined
  3085. + * variable when this is declared inside of the for loop.
  3086. + */
  3087. + int i = 0;
  3088. +
  3089. if (!res)
  3090. return -EFAULT;
  3091. - for (int i = 0; i < IPTS_BUFFERS; i++)
  3092. + for (i = 0; i < IPTS_BUFFERS; i++)
  3093. ipts_resources_free_buffer(&res->data[i]);
  3094. - for (int i = 0; i < IPTS_BUFFERS; i++)
  3095. + for (i = 0; i < IPTS_BUFFERS; i++)
  3096. ipts_resources_free_buffer(&res->feedback[i]);
  3097. ipts_resources_free_buffer(&res->doorbell);
  3098. @@ -104,5 +127,9 @@ int ipts_resources_free(struct ipts_resources *res)
  3099. ipts_resources_free_buffer(&res->hid2me);
  3100. ipts_resources_free_buffer(&res->descriptor);
  3101. + kfree(res->report.address);
  3102. + res->report.address = NULL;
  3103. + res->report.size = 0;
  3104. +
  3105. return 0;
  3106. }
  3107. diff --git a/drivers/hid/ipts/resources.h b/drivers/hid/ipts/resources.h
  3108. index 6cbb24a8a054..e0c400f420b9 100644
  3109. --- a/drivers/hid/ipts/resources.h
  3110. +++ b/drivers/hid/ipts/resources.h
  3111. @@ -31,6 +31,9 @@ struct ipts_resources {
  3112. struct ipts_buffer hid2me;
  3113. struct ipts_buffer descriptor;
  3114. +
  3115. + // Buffer for synthesizing HID reports
  3116. + struct ipts_buffer report;
  3117. };
  3118. int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t ds, size_t fs);
  3119. --
  3120. 2.41.0
  3121. From fe0c021970641e80ebba3e2b6c123bef2689b39e Mon Sep 17 00:00:00 2001
  3122. From: Dorian Stoll <dorian.stoll@tmsp.io>
  3123. Date: Mon, 17 Jul 2023 18:10:43 +0200
  3124. Subject: [PATCH] Update IPTS from module repo
  3125. Changes:
  3126. * Remove usages of C11 for compatibility with older (Android) kernels
  3127. * Remove sensor reset during shutdown
  3128. * Fix compiling resources.c on Android-x86 kernel
  3129. * Split out HID behaviour for different EDS versions into different files
  3130. * Fix kernel-doc comments
  3131. * Add missing docs for members of the device info struct
  3132. * Drop Intel copyright
  3133. * Rename doorbell mode to poll mode
  3134. * Disable the HID interface when the hardware is shutting down.
  3135. Based on https://github.com/linux-surface/intel-precise-touch/commit/56765da3f87d87c5e5cf09e928abe0f579525e5e
  3136. Signed-off-by: Dorian Stoll <dorian.stoll@tmsp.io>
  3137. Patchset: ipts
  3138. ---
  3139. drivers/hid/ipts/Makefile | 2 +
  3140. drivers/hid/ipts/cmd.c | 1 -
  3141. drivers/hid/ipts/cmd.h | 7 +-
  3142. drivers/hid/ipts/context.h | 3 +-
  3143. drivers/hid/ipts/control.c | 25 ++---
  3144. drivers/hid/ipts/control.h | 25 +++--
  3145. drivers/hid/ipts/desc.h | 1 -
  3146. drivers/hid/ipts/eds1.c | 103 ++++++++++++++++++
  3147. drivers/hid/ipts/eds1.h | 35 +++++++
  3148. drivers/hid/ipts/eds2.c | 144 ++++++++++++++++++++++++++
  3149. drivers/hid/ipts/eds2.h | 35 +++++++
  3150. drivers/hid/ipts/hid.c | 184 ++++++---------------------------
  3151. drivers/hid/ipts/hid.h | 4 +-
  3152. drivers/hid/ipts/main.c | 1 -
  3153. drivers/hid/ipts/mei.c | 1 -
  3154. drivers/hid/ipts/mei.h | 7 +-
  3155. drivers/hid/ipts/receiver.c | 15 +--
  3156. drivers/hid/ipts/receiver.h | 1 -
  3157. drivers/hid/ipts/resources.c | 6 +-
  3158. drivers/hid/ipts/resources.h | 1 -
  3159. drivers/hid/ipts/spec-device.h | 31 +++---
  3160. drivers/hid/ipts/spec-hid.h | 1 -
  3161. drivers/hid/ipts/thread.c | 1 -
  3162. drivers/hid/ipts/thread.h | 7 +-
  3163. 24 files changed, 413 insertions(+), 228 deletions(-)
  3164. create mode 100644 drivers/hid/ipts/eds1.c
  3165. create mode 100644 drivers/hid/ipts/eds1.h
  3166. create mode 100644 drivers/hid/ipts/eds2.c
  3167. create mode 100644 drivers/hid/ipts/eds2.h
  3168. diff --git a/drivers/hid/ipts/Makefile b/drivers/hid/ipts/Makefile
  3169. index 0fe655bccdc0..883896f68e6a 100644
  3170. --- a/drivers/hid/ipts/Makefile
  3171. +++ b/drivers/hid/ipts/Makefile
  3172. @@ -6,6 +6,8 @@
  3173. obj-$(CONFIG_HID_IPTS) += ipts.o
  3174. ipts-objs := cmd.o
  3175. ipts-objs += control.o
  3176. +ipts-objs += eds1.o
  3177. +ipts-objs += eds2.o
  3178. ipts-objs += hid.o
  3179. ipts-objs += main.o
  3180. ipts-objs += mei.o
  3181. diff --git a/drivers/hid/ipts/cmd.c b/drivers/hid/ipts/cmd.c
  3182. index 7fd69271ccd5..63a4934bbc5f 100644
  3183. --- a/drivers/hid/ipts/cmd.c
  3184. +++ b/drivers/hid/ipts/cmd.c
  3185. @@ -1,6 +1,5 @@
  3186. // SPDX-License-Identifier: GPL-2.0-or-later
  3187. /*
  3188. - * Copyright (c) 2016 Intel Corporation
  3189. * Copyright (c) 2020-2023 Dorian Stoll
  3190. *
  3191. * Linux driver for Intel Precise Touch & Stylus
  3192. diff --git a/drivers/hid/ipts/cmd.h b/drivers/hid/ipts/cmd.h
  3193. index 924758ffee67..2b4079075b64 100644
  3194. --- a/drivers/hid/ipts/cmd.h
  3195. +++ b/drivers/hid/ipts/cmd.h
  3196. @@ -1,6 +1,5 @@
  3197. /* SPDX-License-Identifier: GPL-2.0-or-later */
  3198. /*
  3199. - * Copyright (c) 2016 Intel Corporation
  3200. * Copyright (c) 2020-2023 Dorian Stoll
  3201. *
  3202. * Linux driver for Intel Precise Touch & Stylus
  3203. @@ -19,7 +18,7 @@
  3204. */
  3205. #define IPTS_CMD_DEFAULT_TIMEOUT 1000
  3206. -/*
  3207. +/**
  3208. * ipts_cmd_recv_timeout() - Receives a response to a command.
  3209. * @ipts: The IPTS driver context.
  3210. * @code: The type of the command / response.
  3211. @@ -33,7 +32,7 @@
  3212. int ipts_cmd_recv_timeout(struct ipts_context *ipts, enum ipts_command_code code,
  3213. struct ipts_response *rsp, u64 timeout);
  3214. -/*
  3215. +/**
  3216. * ipts_cmd_recv() - Receives a response to a command.
  3217. * @ipts: The IPTS driver context.
  3218. * @code: The type of the command / response.
  3219. @@ -47,7 +46,7 @@ static inline int ipts_cmd_recv(struct ipts_context *ipts, enum ipts_command_cod
  3220. return ipts_cmd_recv_timeout(ipts, code, rsp, IPTS_CMD_DEFAULT_TIMEOUT);
  3221. }
  3222. -/*
  3223. +/**
  3224. * ipts_cmd_send() - Executes a command on the device.
  3225. * @ipts: The IPTS driver context.
  3226. * @code: The type of the command to execute.
  3227. diff --git a/drivers/hid/ipts/context.h b/drivers/hid/ipts/context.h
  3228. index 3450a95e66ee..ba33259f1f7c 100644
  3229. --- a/drivers/hid/ipts/context.h
  3230. +++ b/drivers/hid/ipts/context.h
  3231. @@ -1,6 +1,5 @@
  3232. /* SPDX-License-Identifier: GPL-2.0-or-later */
  3233. /*
  3234. - * Copyright (c) 2016 Intel Corporation
  3235. * Copyright (c) 2020-2023 Dorian Stoll
  3236. *
  3237. * Linux driver for Intel Precise Touch & Stylus
  3238. @@ -41,7 +40,9 @@ struct ipts_context {
  3239. struct ipts_buffer feature_report;
  3240. struct ipts_buffer descriptor;
  3241. + bool hid_active;
  3242. struct hid_device *hid;
  3243. +
  3244. struct ipts_device_info info;
  3245. struct ipts_resources resources;
  3246. diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c
  3247. index 2f61500b5119..5360842d260b 100644
  3248. --- a/drivers/hid/ipts/control.c
  3249. +++ b/drivers/hid/ipts/control.c
  3250. @@ -1,6 +1,5 @@
  3251. // SPDX-License-Identifier: GPL-2.0-or-later
  3252. /*
  3253. - * Copyright (c) 2016 Intel Corporation
  3254. * Copyright (c) 2020-2023 Dorian Stoll
  3255. *
  3256. * Linux driver for Intel Precise Touch & Stylus
  3257. @@ -88,6 +87,7 @@ static int ipts_control_set_mode(struct ipts_context *ipts, enum ipts_mode mode)
  3258. static int ipts_control_set_mem_window(struct ipts_context *ipts, struct ipts_resources *res)
  3259. {
  3260. + int i = 0;
  3261. int ret = 0;
  3262. struct ipts_mem_window cmd = { 0 };
  3263. struct ipts_response rsp = { 0 };
  3264. @@ -98,7 +98,7 @@ static int ipts_control_set_mem_window(struct ipts_context *ipts, struct ipts_re
  3265. if (!res)
  3266. return -EFAULT;
  3267. - for (int i = 0; i < IPTS_BUFFERS; i++) {
  3268. + for (i = 0; i < IPTS_BUFFERS; i++) {
  3269. cmd.data_addr_lower[i] = lower_32_bits(res->data[i].dma_address);
  3270. cmd.data_addr_upper[i] = upper_32_bits(res->data[i].dma_address);
  3271. cmd.feedback_addr_lower[i] = lower_32_bits(res->feedback[i].dma_address);
  3272. @@ -342,12 +342,6 @@ int ipts_control_hid2me_feedback(struct ipts_context *ipts, enum ipts_feedback_c
  3273. return ipts_control_send_feedback(ipts, IPTS_HID2ME_BUFFER);
  3274. }
  3275. -static inline int ipts_control_reset_sensor(struct ipts_context *ipts)
  3276. -{
  3277. - return ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_SOFT_RESET,
  3278. - IPTS_FEEDBACK_DATA_TYPE_VENDOR, NULL, 0);
  3279. -}
  3280. -
  3281. int ipts_control_start(struct ipts_context *ipts)
  3282. {
  3283. int ret = 0;
  3284. @@ -389,9 +383,9 @@ int ipts_control_start(struct ipts_context *ipts)
  3285. }
  3286. /*
  3287. - * Newer devices can be directly initialized in doorbell mode.
  3288. + * Newer devices can be directly initialized in polling mode.
  3289. */
  3290. - ipts->mode = IPTS_MODE_DOORBELL;
  3291. + ipts->mode = IPTS_MODE_POLL;
  3292. }
  3293. ret = ipts_control_set_mode(ipts, ipts->mode);
  3294. @@ -418,6 +412,8 @@ int ipts_control_start(struct ipts_context *ipts)
  3295. return ret;
  3296. }
  3297. + ipts_hid_enable(ipts);
  3298. +
  3299. ret = ipts_hid_init(ipts, info);
  3300. if (ret) {
  3301. dev_err(ipts->dev, "Failed to initialize HID device: %d\n", ret);
  3302. @@ -434,6 +430,7 @@ static int _ipts_control_stop(struct ipts_context *ipts)
  3303. if (!ipts)
  3304. return -EFAULT;
  3305. + ipts_hid_disable(ipts);
  3306. dev_info(ipts->dev, "Stopping IPTS\n");
  3307. ret = ipts_receiver_stop(ipts);
  3308. @@ -442,12 +439,6 @@ static int _ipts_control_stop(struct ipts_context *ipts)
  3309. return ret;
  3310. }
  3311. - ret = ipts_control_reset_sensor(ipts);
  3312. - if (ret) {
  3313. - dev_err(ipts->dev, "Failed to reset sensor: %d\n", ret);
  3314. - return ret;
  3315. - }
  3316. -
  3317. ret = ipts_resources_free(&ipts->resources);
  3318. if (ret) {
  3319. dev_err(ipts->dev, "Failed to free resources: %d\n", ret);
  3320. @@ -483,7 +474,7 @@ int ipts_control_restart(struct ipts_context *ipts)
  3321. return ret;
  3322. /*
  3323. - * Give the sensor some time to come back from resetting
  3324. + * Wait a second to give the sensor time to fully shut down.
  3325. */
  3326. msleep(1000);
  3327. diff --git a/drivers/hid/ipts/control.h b/drivers/hid/ipts/control.h
  3328. index 744bb92d682a..26629c5144ed 100644
  3329. --- a/drivers/hid/ipts/control.h
  3330. +++ b/drivers/hid/ipts/control.h
  3331. @@ -1,6 +1,5 @@
  3332. /* SPDX-License-Identifier: GPL-2.0-or-later */
  3333. /*
  3334. - * Copyright (c) 2016 Intel Corporation
  3335. * Copyright (c) 2020-2023 Dorian Stoll
  3336. *
  3337. * Linux driver for Intel Precise Touch & Stylus
  3338. @@ -15,7 +14,7 @@
  3339. #include "spec-data.h"
  3340. #include "spec-device.h"
  3341. -/*
  3342. +/**
  3343. * ipts_control_request_flush() - Stop the data flow.
  3344. * @ipts: The IPTS driver context.
  3345. *
  3346. @@ -26,7 +25,7 @@
  3347. */
  3348. int ipts_control_request_flush(struct ipts_context *ipts);
  3349. -/*
  3350. +/**
  3351. * ipts_control_wait_flush() - Wait until data flow has been stopped.
  3352. * @ipts: The IPTS driver context.
  3353. *
  3354. @@ -34,7 +33,7 @@ int ipts_control_request_flush(struct ipts_context *ipts);
  3355. */
  3356. int ipts_control_wait_flush(struct ipts_context *ipts);
  3357. -/*
  3358. +/**
  3359. * ipts_control_wait_flush() - Notify the device that the driver can receive new data.
  3360. * @ipts: The IPTS driver context.
  3361. *
  3362. @@ -42,19 +41,19 @@ int ipts_control_wait_flush(struct ipts_context *ipts);
  3363. */
  3364. int ipts_control_request_data(struct ipts_context *ipts);
  3365. -/*
  3366. +/**
  3367. * ipts_control_wait_data() - Wait until new data is available.
  3368. * @ipts: The IPTS driver context.
  3369. * @block: Whether to block execution until data is available.
  3370. *
  3371. - * In doorbell mode, this function will never return while the data flow is active. Instead,
  3372. - * the doorbell will be incremented when new data is available.
  3373. + * In poll mode, this function will never return while the data flow is active. Instead,
  3374. + * the poll will be incremented when new data is available.
  3375. *
  3376. * Returns: 0 on success, <0 on error, -EAGAIN if no data is available.
  3377. */
  3378. int ipts_control_wait_data(struct ipts_context *ipts, bool block);
  3379. -/*
  3380. +/**
  3381. * ipts_control_send_feedback() - Submits a feedback buffer to the device.
  3382. * @ipts: The IPTS driver context.
  3383. * @buffer: The ID of the buffer containing feedback data.
  3384. @@ -63,7 +62,7 @@ int ipts_control_wait_data(struct ipts_context *ipts, bool block);
  3385. */
  3386. int ipts_control_send_feedback(struct ipts_context *ipts, u32 buffer);
  3387. -/*
  3388. +/**
  3389. * ipts_control_hid2me_feedback() - Sends HID2ME feedback, a special type of feedback.
  3390. * @ipts: The IPTS driver context.
  3391. * @cmd: The command that will be run on the device.
  3392. @@ -80,7 +79,7 @@ int ipts_control_send_feedback(struct ipts_context *ipts, u32 buffer);
  3393. int ipts_control_hid2me_feedback(struct ipts_context *ipts, enum ipts_feedback_cmd_type cmd,
  3394. enum ipts_feedback_data_type type, void *data, size_t size);
  3395. -/*
  3396. +/**
  3397. * ipts_control_refill_buffer() - Acknowledges that data in a buffer has been processed.
  3398. * @ipts: The IPTS driver context.
  3399. * @buffer: The buffer that has been processed and can be refilled.
  3400. @@ -100,7 +99,7 @@ static inline int ipts_control_refill_buffer(struct ipts_context *ipts, u32 buff
  3401. return ipts_control_send_feedback(ipts, buffer);
  3402. }
  3403. -/*
  3404. +/**
  3405. * ipts_control_start() - Initialized the device and starts the data flow.
  3406. * @ipts: The IPTS driver context.
  3407. *
  3408. @@ -108,7 +107,7 @@ static inline int ipts_control_refill_buffer(struct ipts_context *ipts, u32 buff
  3409. */
  3410. int ipts_control_start(struct ipts_context *ipts);
  3411. -/*
  3412. +/**
  3413. * ipts_control_stop() - Stops the data flow and resets the device.
  3414. * @ipts: The IPTS driver context.
  3415. *
  3416. @@ -116,7 +115,7 @@ int ipts_control_start(struct ipts_context *ipts);
  3417. */
  3418. int ipts_control_stop(struct ipts_context *ipts);
  3419. -/*
  3420. +/**
  3421. * ipts_control_restart() - Stops the device and starts it again.
  3422. * @ipts: The IPTS driver context.
  3423. *
  3424. diff --git a/drivers/hid/ipts/desc.h b/drivers/hid/ipts/desc.h
  3425. index c058974a03a1..307438c7c80c 100644
  3426. --- a/drivers/hid/ipts/desc.h
  3427. +++ b/drivers/hid/ipts/desc.h
  3428. @@ -1,6 +1,5 @@
  3429. /* SPDX-License-Identifier: GPL-2.0-or-later */
  3430. /*
  3431. - * Copyright (c) 2016 Intel Corporation
  3432. * Copyright (c) 2022-2023 Dorian Stoll
  3433. *
  3434. * Linux driver for Intel Precise Touch & Stylus
  3435. diff --git a/drivers/hid/ipts/eds1.c b/drivers/hid/ipts/eds1.c
  3436. new file mode 100644
  3437. index 000000000000..ecbb3a8bdaf6
  3438. --- /dev/null
  3439. +++ b/drivers/hid/ipts/eds1.c
  3440. @@ -0,0 +1,103 @@
  3441. +// SPDX-License-Identifier: GPL-2.0-or-later
  3442. +/*
  3443. + * Copyright (c) 2023 Dorian Stoll
  3444. + *
  3445. + * Linux driver for Intel Precise Touch & Stylus
  3446. + */
  3447. +
  3448. +#include <linux/err.h>
  3449. +#include <linux/gfp.h>
  3450. +#include <linux/hid.h>
  3451. +#include <linux/slab.h>
  3452. +#include <linux/types.h>
  3453. +
  3454. +#include "context.h"
  3455. +#include "control.h"
  3456. +#include "desc.h"
  3457. +#include "spec-device.h"
  3458. +
  3459. +int ipts_eds1_get_descriptor(struct ipts_context *ipts, u8 **desc_buffer, size_t *desc_size)
  3460. +{
  3461. + size_t size = 0;
  3462. + u8 *buffer = NULL;
  3463. +
  3464. + if (!ipts)
  3465. + return -EFAULT;
  3466. +
  3467. + if (!desc_buffer)
  3468. + return -EFAULT;
  3469. +
  3470. + if (!desc_size)
  3471. + return -EFAULT;
  3472. +
  3473. + size = sizeof(ipts_singletouch_descriptor) + sizeof(ipts_fallback_descriptor);
  3474. +
  3475. + buffer = kzalloc(size, GFP_KERNEL);
  3476. + if (!buffer)
  3477. + return -ENOMEM;
  3478. +
  3479. + memcpy(buffer, ipts_singletouch_descriptor, sizeof(ipts_singletouch_descriptor));
  3480. + memcpy(&buffer[sizeof(ipts_singletouch_descriptor)], ipts_fallback_descriptor,
  3481. + sizeof(ipts_fallback_descriptor));
  3482. +
  3483. + *desc_size = size;
  3484. + *desc_buffer = buffer;
  3485. +
  3486. + return 0;
  3487. +}
  3488. +
  3489. +static int ipts_eds1_switch_mode(struct ipts_context *ipts, enum ipts_mode mode)
  3490. +{
  3491. + int ret = 0;
  3492. +
  3493. + if (!ipts)
  3494. + return -EFAULT;
  3495. +
  3496. + if (ipts->mode == mode)
  3497. + return 0;
  3498. +
  3499. + ipts->mode = mode;
  3500. +
  3501. + ret = ipts_control_restart(ipts);
  3502. + if (ret)
  3503. + dev_err(ipts->dev, "Failed to switch modes: %d\n", ret);
  3504. +
  3505. + return ret;
  3506. +}
  3507. +
  3508. +int ipts_eds1_raw_request(struct ipts_context *ipts, u8 *buffer, size_t size, u8 report_id,
  3509. + enum hid_report_type report_type, enum hid_class_request request_type)
  3510. +{
  3511. + int ret = 0;
  3512. +
  3513. + if (!ipts)
  3514. + return -EFAULT;
  3515. +
  3516. + if (!buffer)
  3517. + return -EFAULT;
  3518. +
  3519. + if (report_id != IPTS_HID_REPORT_SET_MODE)
  3520. + return -EIO;
  3521. +
  3522. + if (report_type != HID_FEATURE_REPORT)
  3523. + return -EIO;
  3524. +
  3525. + if (size != 2)
  3526. + return -EINVAL;
  3527. +
  3528. + /*
  3529. + * Implement mode switching report for older devices without native HID support.
  3530. + */
  3531. +
  3532. + if (request_type == HID_REQ_GET_REPORT) {
  3533. + memset(buffer, 0, size);
  3534. + buffer[0] = report_id;
  3535. + buffer[1] = ipts->mode;
  3536. + } else if (request_type == HID_REQ_SET_REPORT) {
  3537. + return ipts_eds1_switch_mode(ipts, buffer[1]);
  3538. + } else {
  3539. + return -EIO;
  3540. + }
  3541. +
  3542. + return ret;
  3543. +}
  3544. diff --git a/drivers/hid/ipts/eds1.h b/drivers/hid/ipts/eds1.h
  3545. new file mode 100644
  3546. index 000000000000..eeeb6575e3e8
  3547. --- /dev/null
  3548. +++ b/drivers/hid/ipts/eds1.h
  3549. @@ -0,0 +1,35 @@
  3550. +// SPDX-License-Identifier: GPL-2.0-or-later
  3551. +/*
  3552. + * Copyright (c) 2023 Dorian Stoll
  3553. + *
  3554. + * Linux driver for Intel Precise Touch & Stylus
  3555. + */
  3556. +
  3557. +#include <linux/hid.h>
  3558. +#include <linux/types.h>
  3559. +
  3560. +#include "context.h"
  3561. +
  3562. +/**
  3563. + * ipts_eds1_get_descriptor() - Assembles the HID descriptor of the device.
  3564. + * @ipts: The IPTS driver context.
  3565. + * @desc_buffer: A pointer to the location where the address of the allocated buffer is stored.
  3566. + * @desc_size: A pointer to the location where the size of the allocated buffer is stored.
  3567. + *
  3568. + * Returns: 0 on success, <0 on error.
  3569. + */
  3570. +int ipts_eds1_get_descriptor(struct ipts_context *ipts, u8 **desc_buffer, size_t *desc_size);
  3571. +
  3572. +/**
  3573. + * ipts_eds1_raw_request() - Executes an output or feature report on the device.
  3574. + * @ipts: The IPTS driver context.
  3575. + * @buffer: The buffer containing the report.
  3576. + * @size: The size of the buffer.
  3577. + * @report_id: The HID report ID.
  3578. + * @report_type: Whether this report is an output or a feature report.
  3579. + * @request_type: Whether this report requests or sends data.
  3580. + *
  3581. + * Returns: 0 on success, <0 on error.
  3582. + */
  3583. +int ipts_eds1_raw_request(struct ipts_context *ipts, u8 *buffer, size_t size, u8 report_id,
  3584. + enum hid_report_type report_type, enum hid_class_request request_type);
  3585. diff --git a/drivers/hid/ipts/eds2.c b/drivers/hid/ipts/eds2.c
  3586. new file mode 100644
  3587. index 000000000000..e835b460aa79
  3588. --- /dev/null
  3589. +++ b/drivers/hid/ipts/eds2.c
  3590. @@ -0,0 +1,144 @@
  3591. +// SPDX-License-Identifier: GPL-2.0-or-later
  3592. +/*
  3593. + * Copyright (c) 2023 Dorian Stoll
  3594. + *
  3595. + * Linux driver for Intel Precise Touch & Stylus
  3596. + */
  3597. +
  3598. +#include <linux/completion.h>
  3599. +#include <linux/err.h>
  3600. +#include <linux/gfp.h>
  3601. +#include <linux/mutex.h>
  3602. +#include <linux/slab.h>
  3603. +#include <linux/types.h>
  3604. +
  3605. +#include "context.h"
  3606. +#include "control.h"
  3607. +#include "desc.h"
  3608. +#include "spec-data.h"
  3609. +
  3610. +int ipts_eds2_get_descriptor(struct ipts_context *ipts, u8 **desc_buffer, size_t *desc_size)
  3611. +{
  3612. + size_t size = 0;
  3613. + u8 *buffer = NULL;
  3614. +
  3615. + if (!ipts)
  3616. + return -EFAULT;
  3617. +
  3618. + if (!desc_buffer)
  3619. + return -EFAULT;
  3620. +
  3621. + if (!desc_size)
  3622. + return -EFAULT;
  3623. +
  3624. + size = sizeof(ipts_singletouch_descriptor) + ipts->descriptor.size;
  3625. +
  3626. + buffer = kzalloc(size, GFP_KERNEL);
  3627. + if (!*buffer)
  3628. + return -ENOMEM;
  3629. +
  3630. + memcpy(buffer, ipts_singletouch_descriptor, sizeof(ipts_singletouch_descriptor));
  3631. + memcpy(&buffer[sizeof(ipts_singletouch_descriptor)], ipts->descriptor.address,
  3632. + ipts->descriptor.size);
  3633. +
  3634. + *desc_size = size;
  3635. + *desc_buffer = buffer;
  3636. +
  3637. + return 0;
  3638. +}
  3639. +
  3640. +static int ipts_eds2_get_feature(struct ipts_context *ipts, u8 *buffer, size_t size, u8 report_id,
  3641. + enum ipts_feedback_data_type type)
  3642. +{
  3643. + int ret = 0;
  3644. +
  3645. + if (!ipts)
  3646. + return -EFAULT;
  3647. +
  3648. + if (!buffer)
  3649. + return -EFAULT;
  3650. +
  3651. + mutex_lock(&ipts->feature_lock);
  3652. +
  3653. + memset(buffer, 0, size);
  3654. + buffer[0] = report_id;
  3655. +
  3656. + memset(&ipts->feature_report, 0, sizeof(ipts->feature_report));
  3657. + reinit_completion(&ipts->feature_event);
  3658. +
  3659. + ret = ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE, type, buffer, size);
  3660. + if (ret) {
  3661. + dev_err(ipts->dev, "Failed to send hid2me feedback: %d\n", ret);
  3662. + goto out;
  3663. + }
  3664. +
  3665. + ret = wait_for_completion_timeout(&ipts->feature_event, msecs_to_jiffies(5000));
  3666. + if (ret == 0) {
  3667. + dev_warn(ipts->dev, "GET_FEATURES timed out!\n");
  3668. + ret = -EIO;
  3669. + goto out;
  3670. + }
  3671. +
  3672. + if (!ipts->feature_report.address) {
  3673. + ret = -EFAULT;
  3674. + goto out;
  3675. + }
  3676. +
  3677. + if (ipts->feature_report.size > size) {
  3678. + ret = -ETOOSMALL;
  3679. + goto out;
  3680. + }
  3681. +
  3682. + ret = ipts->feature_report.size;
  3683. + memcpy(buffer, ipts->feature_report.address, ipts->feature_report.size);
  3684. +
  3685. +out:
  3686. + mutex_unlock(&ipts->feature_lock);
  3687. + return ret;
  3688. +}
  3689. +
  3690. +static int ipts_eds2_set_feature(struct ipts_context *ipts, u8 *buffer, size_t size, u8 report_id,
  3691. + enum ipts_feedback_data_type type)
  3692. +{
  3693. + int ret = 0;
  3694. +
  3695. + if (!ipts)
  3696. + return -EFAULT;
  3697. +
  3698. + if (!buffer)
  3699. + return -EFAULT;
  3700. +
  3701. + buffer[0] = report_id;
  3702. +
  3703. + ret = ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE, type, buffer, size);
  3704. + if (ret)
  3705. + dev_err(ipts->dev, "Failed to send hid2me feedback: %d\n", ret);
  3706. +
  3707. + return ret;
  3708. +}
  3709. +
  3710. +int ipts_eds2_raw_request(struct ipts_context *ipts, u8 *buffer, size_t size, u8 report_id,
  3711. + enum hid_report_type report_type, enum hid_class_request request_type)
  3712. +{
  3713. + enum ipts_feedback_data_type feedback_type = IPTS_FEEDBACK_DATA_TYPE_VENDOR;
  3714. +
  3715. + if (!ipts)
  3716. + return -EFAULT;
  3717. +
  3718. + if (!buffer)
  3719. + return -EFAULT;
  3720. +
  3721. + if (report_type == HID_OUTPUT_REPORT && request_type == HID_REQ_SET_REPORT)
  3722. + feedback_type = IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT;
  3723. + else if (report_type == HID_FEATURE_REPORT && request_type == HID_REQ_GET_REPORT)
  3724. + feedback_type = IPTS_FEEDBACK_DATA_TYPE_GET_FEATURES;
  3725. + else if (report_type == HID_FEATURE_REPORT && request_type == HID_REQ_SET_REPORT)
  3726. + feedback_type = IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES;
  3727. + else
  3728. + return -EIO;
  3729. +
  3730. + if (request_type == HID_REQ_GET_REPORT)
  3731. + return ipts_eds2_get_feature(ipts, buffer, report_id, size, feedback_type);
  3732. + else
  3733. + return ipts_eds2_set_feature(ipts, buffer, report_id, size, feedback_type);
  3734. +}
  3735. diff --git a/drivers/hid/ipts/eds2.h b/drivers/hid/ipts/eds2.h
  3736. new file mode 100644
  3737. index 000000000000..064e3716907a
  3738. --- /dev/null
  3739. +++ b/drivers/hid/ipts/eds2.h
  3740. @@ -0,0 +1,35 @@
  3741. +// SPDX-License-Identifier: GPL-2.0-or-later
  3742. +/*
  3743. + * Copyright (c) 2023 Dorian Stoll
  3744. + *
  3745. + * Linux driver for Intel Precise Touch & Stylus
  3746. + */
  3747. +
  3748. +#include <linux/hid.h>
  3749. +#include <linux/types.h>
  3750. +
  3751. +#include "context.h"
  3752. +
  3753. +/**
  3754. + * ipts_eds2_get_descriptor() - Assembles the HID descriptor of the device.
  3755. + * @ipts: The IPTS driver context.
  3756. + * @desc_buffer: A pointer to the location where the address of the allocated buffer is stored.
  3757. + * @desc_size: A pointer to the location where the size of the allocated buffer is stored.
  3758. + *
  3759. + * Returns: 0 on success, <0 on error.
  3760. + */
  3761. +int ipts_eds2_get_descriptor(struct ipts_context *ipts, u8 **desc_buffer, size_t *desc_size);
  3762. +
  3763. +/**
  3764. + * ipts_eds2_raw_request() - Executes an output or feature report on the device.
  3765. + * @ipts: The IPTS driver context.
  3766. + * @buffer: The buffer containing the report.
  3767. + * @size: The size of the buffer.
  3768. + * @report_id: The HID report ID.
  3769. + * @report_type: Whether this report is an output or a feature report.
  3770. + * @request_type: Whether this report requests or sends data.
  3771. + *
  3772. + * Returns: 0 on success, <0 on error.
  3773. + */
  3774. +int ipts_eds2_raw_request(struct ipts_context *ipts, u8 *buffer, size_t size, u8 report_id,
  3775. + enum hid_report_type report_type, enum hid_class_request request_type);
  3776. diff --git a/drivers/hid/ipts/hid.c b/drivers/hid/ipts/hid.c
  3777. index a2471219615b..e34a1a4f9fa7 100644
  3778. --- a/drivers/hid/ipts/hid.c
  3779. +++ b/drivers/hid/ipts/hid.c
  3780. @@ -1,12 +1,12 @@
  3781. // SPDX-License-Identifier: GPL-2.0-or-later
  3782. /*
  3783. - * Copyright (c) 2016 Intel Corporation
  3784. * Copyright (c) 2022-2023 Dorian Stoll
  3785. *
  3786. * Linux driver for Intel Precise Touch & Stylus
  3787. */
  3788. #include <linux/completion.h>
  3789. +#include <linux/err.h>
  3790. #include <linux/gfp.h>
  3791. #include <linux/hid.h>
  3792. #include <linux/mutex.h>
  3793. @@ -14,38 +14,30 @@
  3794. #include <linux/types.h>
  3795. #include "context.h"
  3796. -#include "control.h"
  3797. #include "desc.h"
  3798. +#include "eds1.h"
  3799. +#include "eds2.h"
  3800. #include "hid.h"
  3801. #include "spec-data.h"
  3802. -#include "spec-device.h"
  3803. #include "spec-hid.h"
  3804. -static int ipts_hid_start(struct hid_device *hid)
  3805. +void ipts_hid_enable(struct ipts_context *ipts)
  3806. {
  3807. - return 0;
  3808. + WRITE_ONCE(ipts->hid_active, true);
  3809. }
  3810. -static void ipts_hid_stop(struct hid_device *hid)
  3811. +void ipts_hid_disable(struct ipts_context *ipts)
  3812. {
  3813. + WRITE_ONCE(ipts->hid_active, false);
  3814. }
  3815. -static int ipts_hid_switch_mode(struct ipts_context *ipts, enum ipts_mode mode)
  3816. +static int ipts_hid_start(struct hid_device *hid)
  3817. {
  3818. - if (!ipts)
  3819. - return -EFAULT;
  3820. -
  3821. - if (ipts->mode == mode)
  3822. - return 0;
  3823. -
  3824. - /*
  3825. - * This is only allowed on older devices.
  3826. - */
  3827. - if (ipts->info.intf_eds > 1)
  3828. - return 0;
  3829. + return 0;
  3830. +}
  3831. - ipts->mode = mode;
  3832. - return ipts_control_restart(ipts);
  3833. +static void ipts_hid_stop(struct hid_device *hid)
  3834. +{
  3835. }
  3836. static int ipts_hid_parse(struct hid_device *hid)
  3837. @@ -53,8 +45,6 @@ static int ipts_hid_parse(struct hid_device *hid)
  3838. int ret = 0;
  3839. struct ipts_context *ipts = NULL;
  3840. - bool has_native_descriptor = false;
  3841. -
  3842. u8 *buffer = NULL;
  3843. size_t size = 0;
  3844. @@ -66,26 +56,17 @@ static int ipts_hid_parse(struct hid_device *hid)
  3845. if (!ipts)
  3846. return -EFAULT;
  3847. - size = sizeof(ipts_singletouch_descriptor);
  3848. - has_native_descriptor = ipts->descriptor.address && ipts->descriptor.size > 0;
  3849. + if (!READ_ONCE(ipts->hid_active))
  3850. + return -ENODEV;
  3851. - if (has_native_descriptor)
  3852. - size += ipts->descriptor.size;
  3853. + if (ipts->info.intf_eds == 1)
  3854. + ret = ipts_eds1_get_descriptor(ipts, &buffer, &size);
  3855. else
  3856. - size += sizeof(ipts_fallback_descriptor);
  3857. -
  3858. - buffer = kzalloc(size, GFP_KERNEL);
  3859. - if (!buffer)
  3860. - return -ENOMEM;
  3861. -
  3862. - memcpy(buffer, ipts_singletouch_descriptor, sizeof(ipts_singletouch_descriptor));
  3863. + ret = ipts_eds2_get_descriptor(ipts, &buffer, &size);
  3864. - if (has_native_descriptor) {
  3865. - memcpy(&buffer[sizeof(ipts_singletouch_descriptor)], ipts->descriptor.address,
  3866. - ipts->descriptor.size);
  3867. - } else {
  3868. - memcpy(&buffer[sizeof(ipts_singletouch_descriptor)], ipts_fallback_descriptor,
  3869. - sizeof(ipts_fallback_descriptor));
  3870. + if (ret) {
  3871. + dev_err(ipts->dev, "Failed to allocate HID descriptor: %d\n", ret);
  3872. + return ret;
  3873. }
  3874. ret = hid_parse_report(hid, buffer, size);
  3875. @@ -99,84 +80,11 @@ static int ipts_hid_parse(struct hid_device *hid)
  3876. return 0;
  3877. }
  3878. -static int ipts_hid_get_feature(struct ipts_context *ipts, unsigned char reportnum, __u8 *buf,
  3879. - size_t size, enum ipts_feedback_data_type type)
  3880. -{
  3881. - int ret = 0;
  3882. -
  3883. - if (!ipts)
  3884. - return -EFAULT;
  3885. -
  3886. - if (!buf)
  3887. - return -EFAULT;
  3888. -
  3889. - mutex_lock(&ipts->feature_lock);
  3890. -
  3891. - memset(buf, 0, size);
  3892. - buf[0] = reportnum;
  3893. -
  3894. - memset(&ipts->feature_report, 0, sizeof(ipts->feature_report));
  3895. - reinit_completion(&ipts->feature_event);
  3896. -
  3897. - ret = ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE, type, buf, size);
  3898. - if (ret) {
  3899. - dev_err(ipts->dev, "Failed to send hid2me feedback: %d\n", ret);
  3900. - goto out;
  3901. - }
  3902. -
  3903. - ret = wait_for_completion_timeout(&ipts->feature_event, msecs_to_jiffies(5000));
  3904. - if (ret == 0) {
  3905. - dev_warn(ipts->dev, "GET_FEATURES timed out!\n");
  3906. - ret = -EIO;
  3907. - goto out;
  3908. - }
  3909. -
  3910. - if (!ipts->feature_report.address) {
  3911. - ret = -EFAULT;
  3912. - goto out;
  3913. - }
  3914. -
  3915. - if (ipts->feature_report.size > size) {
  3916. - ret = -ETOOSMALL;
  3917. - goto out;
  3918. - }
  3919. -
  3920. - ret = ipts->feature_report.size;
  3921. - memcpy(buf, ipts->feature_report.address, ipts->feature_report.size);
  3922. -
  3923. -out:
  3924. - mutex_unlock(&ipts->feature_lock);
  3925. - return ret;
  3926. -}
  3927. -
  3928. -static int ipts_hid_set_feature(struct ipts_context *ipts, unsigned char reportnum, __u8 *buf,
  3929. - size_t size, enum ipts_feedback_data_type type)
  3930. -{
  3931. - int ret = 0;
  3932. -
  3933. - if (!ipts)
  3934. - return -EFAULT;
  3935. -
  3936. - if (!buf)
  3937. - return -EFAULT;
  3938. -
  3939. - buf[0] = reportnum;
  3940. -
  3941. - ret = ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE, type, buf, size);
  3942. - if (ret)
  3943. - dev_err(ipts->dev, "Failed to send hid2me feedback: %d\n", ret);
  3944. -
  3945. - return ret;
  3946. -}
  3947. -
  3948. -static int ipts_hid_raw_request(struct hid_device *hid, unsigned char reportnum, __u8 *buf,
  3949. - size_t size, unsigned char rtype, int reqtype)
  3950. +static int ipts_hid_raw_request(struct hid_device *hid, unsigned char report_id, __u8 *buffer,
  3951. + size_t size, unsigned char report_type, int request_type)
  3952. {
  3953. - int ret = 0;
  3954. struct ipts_context *ipts = NULL;
  3955. - enum ipts_feedback_data_type type = IPTS_FEEDBACK_DATA_TYPE_VENDOR;
  3956. -
  3957. if (!hid)
  3958. return -ENODEV;
  3959. @@ -185,44 +93,16 @@ static int ipts_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
  3960. if (!ipts)
  3961. return -EFAULT;
  3962. - if (!buf)
  3963. - return -EFAULT;
  3964. -
  3965. - if (rtype == HID_OUTPUT_REPORT && reqtype == HID_REQ_SET_REPORT)
  3966. - type = IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT;
  3967. - else if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_GET_REPORT)
  3968. - type = IPTS_FEEDBACK_DATA_TYPE_GET_FEATURES;
  3969. - else if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_SET_REPORT)
  3970. - type = IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES;
  3971. - else
  3972. - return -EIO;
  3973. -
  3974. - // Implemente mode switching report for older devices without native HID support
  3975. - if (type == IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES && reportnum == IPTS_HID_REPORT_SET_MODE) {
  3976. - ret = ipts_hid_switch_mode(ipts, buf[1]);
  3977. - if (ret) {
  3978. - dev_err(ipts->dev, "Failed to switch modes: %d\n", ret);
  3979. - return ret;
  3980. - }
  3981. - }
  3982. -
  3983. - if (reqtype == HID_REQ_GET_REPORT)
  3984. - return ipts_hid_get_feature(ipts, reportnum, buf, size, type);
  3985. - else
  3986. - return ipts_hid_set_feature(ipts, reportnum, buf, size, type);
  3987. -}
  3988. -
  3989. -static int ipts_hid_output_report(struct hid_device *hid, __u8 *data, size_t size)
  3990. -{
  3991. - struct ipts_context *ipts = NULL;
  3992. -
  3993. - if (!hid)
  3994. + if (!READ_ONCE(ipts->hid_active))
  3995. return -ENODEV;
  3996. - ipts = hid->driver_data;
  3997. -
  3998. - return ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE,
  3999. - IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT, data, size);
  4000. + if (ipts->info.intf_eds == 1) {
  4001. + return ipts_eds1_raw_request(ipts, buffer, size, report_id, report_type,
  4002. + request_type);
  4003. + } else {
  4004. + return ipts_eds2_raw_request(ipts, buffer, size, report_id, report_type,
  4005. + request_type);
  4006. + }
  4007. }
  4008. static struct hid_ll_driver ipts_hid_driver = {
  4009. @@ -232,7 +112,6 @@ static struct hid_ll_driver ipts_hid_driver = {
  4010. .close = ipts_hid_stop,
  4011. .parse = ipts_hid_parse,
  4012. .raw_request = ipts_hid_raw_request,
  4013. - .output_report = ipts_hid_output_report,
  4014. };
  4015. int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer)
  4016. @@ -247,6 +126,9 @@ int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer)
  4017. if (!ipts->hid)
  4018. return -ENODEV;
  4019. + if (!READ_ONCE(ipts->hid_active))
  4020. + return -ENODEV;
  4021. +
  4022. header = (struct ipts_data_header *)ipts->resources.data[buffer].address;
  4023. temp = ipts->resources.report.address;
  4024. diff --git a/drivers/hid/ipts/hid.h b/drivers/hid/ipts/hid.h
  4025. index 62bf3cd48608..1ebe77447903 100644
  4026. --- a/drivers/hid/ipts/hid.h
  4027. +++ b/drivers/hid/ipts/hid.h
  4028. @@ -1,6 +1,5 @@
  4029. /* SPDX-License-Identifier: GPL-2.0-or-later */
  4030. /*
  4031. - * Copyright (c) 2016 Intel Corporation
  4032. * Copyright (c) 2022-2023 Dorian Stoll
  4033. *
  4034. * Linux driver for Intel Precise Touch & Stylus
  4035. @@ -14,6 +13,9 @@
  4036. #include "context.h"
  4037. #include "spec-device.h"
  4038. +void ipts_hid_enable(struct ipts_context *ipts);
  4039. +void ipts_hid_disable(struct ipts_context *ipts);
  4040. +
  4041. int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer);
  4042. int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info);
  4043. diff --git a/drivers/hid/ipts/main.c b/drivers/hid/ipts/main.c
  4044. index 0f20c6c08c38..fb5b5c13ee3e 100644
  4045. --- a/drivers/hid/ipts/main.c
  4046. +++ b/drivers/hid/ipts/main.c
  4047. @@ -1,6 +1,5 @@
  4048. // SPDX-License-Identifier: GPL-2.0-or-later
  4049. /*
  4050. - * Copyright (c) 2016 Intel Corporation
  4051. * Copyright (c) 2020-2023 Dorian Stoll
  4052. *
  4053. * Linux driver for Intel Precise Touch & Stylus
  4054. diff --git a/drivers/hid/ipts/mei.c b/drivers/hid/ipts/mei.c
  4055. index 26666fd99b0c..1e0395ceae4a 100644
  4056. --- a/drivers/hid/ipts/mei.c
  4057. +++ b/drivers/hid/ipts/mei.c
  4058. @@ -1,6 +1,5 @@
  4059. // SPDX-License-Identifier: GPL-2.0-or-later
  4060. /*
  4061. - * Copyright (c) 2016 Intel Corporation
  4062. * Copyright (c) 2023 Dorian Stoll
  4063. *
  4064. * Linux driver for Intel Precise Touch & Stylus
  4065. diff --git a/drivers/hid/ipts/mei.h b/drivers/hid/ipts/mei.h
  4066. index eadacae54c40..973bade6b0fd 100644
  4067. --- a/drivers/hid/ipts/mei.h
  4068. +++ b/drivers/hid/ipts/mei.h
  4069. @@ -1,6 +1,5 @@
  4070. /* SPDX-License-Identifier: GPL-2.0-or-later */
  4071. /*
  4072. - * Copyright (c) 2016 Intel Corporation
  4073. * Copyright (c) 2023 Dorian Stoll
  4074. *
  4075. * Linux driver for Intel Precise Touch & Stylus
  4076. @@ -31,7 +30,7 @@ struct ipts_mei {
  4077. struct rw_semaphore message_lock;
  4078. };
  4079. -/*
  4080. +/**
  4081. * ipts_mei_recv() - Receive data from a MEI device.
  4082. * @mei: The IPTS MEI device context.
  4083. * @code: The IPTS command code to look for.
  4084. @@ -45,7 +44,7 @@ struct ipts_mei {
  4085. int ipts_mei_recv(struct ipts_mei *mei, enum ipts_command_code code, struct ipts_response *rsp,
  4086. u64 timeout);
  4087. -/*
  4088. +/**
  4089. * ipts_mei_send() - Send data to a MEI device.
  4090. * @ipts: The IPTS MEI device context.
  4091. * @data: The data to send.
  4092. @@ -55,7 +54,7 @@ int ipts_mei_recv(struct ipts_mei *mei, enum ipts_command_code code, struct ipts
  4093. */
  4094. int ipts_mei_send(struct ipts_mei *mei, void *data, size_t length);
  4095. -/*
  4096. +/**
  4097. * ipts_mei_init() - Initialize the MEI device context.
  4098. * @mei: The MEI device context to initialize.
  4099. * @cldev: The MEI device the context will be bound to.
  4100. diff --git a/drivers/hid/ipts/receiver.c b/drivers/hid/ipts/receiver.c
  4101. index f56e9ed32d57..ef66c3c9db80 100644
  4102. --- a/drivers/hid/ipts/receiver.c
  4103. +++ b/drivers/hid/ipts/receiver.c
  4104. @@ -1,6 +1,5 @@
  4105. // SPDX-License-Identifier: GPL-2.0-or-later
  4106. /*
  4107. - * Copyright (c) 2016 Intel Corporation
  4108. * Copyright (c) 2020-2023 Dorian Stoll
  4109. *
  4110. * Linux driver for Intel Precise Touch & Stylus
  4111. @@ -66,7 +65,9 @@ static int ipts_receiver_event_loop(struct ipts_thread *thread)
  4112. dev_info(ipts->dev, "IPTS running in event mode\n");
  4113. while (!ipts_thread_should_stop(thread)) {
  4114. - for (int i = 0; i < IPTS_BUFFERS; i++) {
  4115. + int i = 0;
  4116. +
  4117. + for (i = 0; i < IPTS_BUFFERS; i++) {
  4118. ret = ipts_control_wait_data(ipts, false);
  4119. if (ret == -EAGAIN)
  4120. break;
  4121. @@ -126,7 +127,7 @@ static int ipts_receiver_event_loop(struct ipts_thread *thread)
  4122. return 0;
  4123. }
  4124. -static int ipts_receiver_doorbell_loop(struct ipts_thread *thread)
  4125. +static int ipts_receiver_poll_loop(struct ipts_thread *thread)
  4126. {
  4127. int ret = 0;
  4128. u32 buffer = 0;
  4129. @@ -145,7 +146,7 @@ static int ipts_receiver_doorbell_loop(struct ipts_thread *thread)
  4130. if (!ipts)
  4131. return -EFAULT;
  4132. - dev_info(ipts->dev, "IPTS running in doorbell mode\n");
  4133. + dev_info(ipts->dev, "IPTS running in poll mode\n");
  4134. while (true) {
  4135. if (ipts_thread_should_stop(thread)) {
  4136. @@ -217,9 +218,9 @@ int ipts_receiver_start(struct ipts_context *ipts)
  4137. if (ipts->mode == IPTS_MODE_EVENT) {
  4138. ret = ipts_thread_start(&ipts->receiver_loop, ipts_receiver_event_loop, ipts,
  4139. "ipts_event");
  4140. - } else if (ipts->mode == IPTS_MODE_DOORBELL) {
  4141. - ret = ipts_thread_start(&ipts->receiver_loop, ipts_receiver_doorbell_loop, ipts,
  4142. - "ipts_doorbell");
  4143. + } else if (ipts->mode == IPTS_MODE_POLL) {
  4144. + ret = ipts_thread_start(&ipts->receiver_loop, ipts_receiver_poll_loop, ipts,
  4145. + "ipts_poll");
  4146. } else {
  4147. ret = -EINVAL;
  4148. }
  4149. diff --git a/drivers/hid/ipts/receiver.h b/drivers/hid/ipts/receiver.h
  4150. index 96070f34fbca..3de7da62d40c 100644
  4151. --- a/drivers/hid/ipts/receiver.h
  4152. +++ b/drivers/hid/ipts/receiver.h
  4153. @@ -1,6 +1,5 @@
  4154. /* SPDX-License-Identifier: GPL-2.0-or-later */
  4155. /*
  4156. - * Copyright (c) 2016 Intel Corporation
  4157. * Copyright (c) 2020-2023 Dorian Stoll
  4158. *
  4159. * Linux driver for Intel Precise Touch & Stylus
  4160. diff --git a/drivers/hid/ipts/resources.c b/drivers/hid/ipts/resources.c
  4161. index 5e924d58c488..cc14653b2a9f 100644
  4162. --- a/drivers/hid/ipts/resources.c
  4163. +++ b/drivers/hid/ipts/resources.c
  4164. @@ -1,12 +1,12 @@
  4165. // SPDX-License-Identifier: GPL-2.0-or-later
  4166. /*
  4167. - * Copyright (c) 2016 Intel Corporation
  4168. * Copyright (c) 2020-2023 Dorian Stoll
  4169. *
  4170. * Linux driver for Intel Precise Touch & Stylus
  4171. */
  4172. #include <linux/dma-mapping.h>
  4173. +#include <linux/slab.h>
  4174. #include <linux/types.h>
  4175. #include "desc.h"
  4176. @@ -107,10 +107,6 @@ int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t d
  4177. int ipts_resources_free(struct ipts_resources *res)
  4178. {
  4179. - /*
  4180. - * Some compilers (AOSP clang) complain about a redefined
  4181. - * variable when this is declared inside of the for loop.
  4182. - */
  4183. int i = 0;
  4184. if (!res)
  4185. diff --git a/drivers/hid/ipts/resources.h b/drivers/hid/ipts/resources.h
  4186. index e0c400f420b9..2068e13285f0 100644
  4187. --- a/drivers/hid/ipts/resources.h
  4188. +++ b/drivers/hid/ipts/resources.h
  4189. @@ -1,6 +1,5 @@
  4190. /* SPDX-License-Identifier: GPL-2.0-or-later */
  4191. /*
  4192. - * Copyright (c) 2016 Intel Corporation
  4193. * Copyright (c) 2020-2023 Dorian Stoll
  4194. *
  4195. * Linux driver for Intel Precise Touch & Stylus
  4196. diff --git a/drivers/hid/ipts/spec-device.h b/drivers/hid/ipts/spec-device.h
  4197. index 93f673d981f7..41845f9d9025 100644
  4198. --- a/drivers/hid/ipts/spec-device.h
  4199. +++ b/drivers/hid/ipts/spec-device.h
  4200. @@ -109,14 +109,14 @@ static_assert(sizeof(struct ipts_command) == 324);
  4201. /**
  4202. * enum ipts_mode - Configures what data the device produces and how its sent.
  4203. - * @IPTS_MODE_EVENT: The device will send an event once a buffer was filled.
  4204. - * Older devices will return singletouch data in this mode.
  4205. - * @IPTS_MODE_DOORBELL: The device will notify the driver by incrementing the doorbell value.
  4206. - * Older devices will return multitouch data in this mode.
  4207. + * @IPTS_MODE_EVENT: The device will send an event once a buffer was filled.
  4208. + * Older devices will return singletouch data in this mode.
  4209. + * @IPTS_MODE_POLL: The device will notify the driver by incrementing the doorbell value.
  4210. + * Older devices will return multitouch data in this mode.
  4211. */
  4212. enum ipts_mode {
  4213. IPTS_MODE_EVENT = 0x00,
  4214. - IPTS_MODE_DOORBELL = 0x01,
  4215. + IPTS_MODE_POLL = 0x01,
  4216. };
  4217. /**
  4218. @@ -253,14 +253,19 @@ static_assert(sizeof(struct ipts_response) == 88);
  4219. /**
  4220. * struct ipts_device_info - Vendor information of the IPTS device.
  4221. - * @vendor: Vendor ID of this device.
  4222. - * @product: Product ID of this device.
  4223. - * @hw_version: Hardware revision of this device.
  4224. - * @fw_version: Firmware revision of this device.
  4225. - * @data_size: Requested size for a data buffer.
  4226. - * @feedback_size: Requested size for a feedback buffer.
  4227. - * @mode: Mode that the device currently operates in.
  4228. - * @max_contacts: Maximum amount of concurrent touches the sensor can process.
  4229. + * @vendor: Vendor ID of this device.
  4230. + * @product: Product ID of this device.
  4231. + * @hw_version: Hardware revision of this device.
  4232. + * @fw_version: Firmware revision of this device.
  4233. + * @data_size: Requested size for a data buffer.
  4234. + * @feedback_size: Requested size for a feedback buffer.
  4235. + * @mode: Mode that the device currently operates in.
  4236. + * @max_contacts: Maximum amount of concurrent touches the sensor can process.
  4237. + * @sensor_min_eds: The minimum EDS version supported by the sensor.
  4238. + * @sensor_max_eds: The maximum EDS version supported by the sensor.
  4239. + * @me_min_eds: The minimum EDS version supported by the ME for communicating with the sensor.
  4240. + * @me_max_eds: The maximum EDS version supported by the ME for communicating with the sensor.
  4241. + * @intf_eds: The EDS version implemented by the interface between ME and host.
  4242. */
  4243. struct ipts_device_info {
  4244. u16 vendor;
  4245. diff --git a/drivers/hid/ipts/spec-hid.h b/drivers/hid/ipts/spec-hid.h
  4246. index ea70f29ff00c..5a58d4a0a610 100644
  4247. --- a/drivers/hid/ipts/spec-hid.h
  4248. +++ b/drivers/hid/ipts/spec-hid.h
  4249. @@ -1,6 +1,5 @@
  4250. /* SPDX-License-Identifier: GPL-2.0-or-later */
  4251. /*
  4252. - * Copyright (c) 2016 Intel Corporation
  4253. * Copyright (c) 2020-2023 Dorian Stoll
  4254. *
  4255. * Linux driver for Intel Precise Touch & Stylus
  4256. diff --git a/drivers/hid/ipts/thread.c b/drivers/hid/ipts/thread.c
  4257. index 8b46f775c107..355e92bea26f 100644
  4258. --- a/drivers/hid/ipts/thread.c
  4259. +++ b/drivers/hid/ipts/thread.c
  4260. @@ -1,6 +1,5 @@
  4261. // SPDX-License-Identifier: GPL-2.0-or-later
  4262. /*
  4263. - * Copyright (c) 2016 Intel Corporation
  4264. * Copyright (c) 2023 Dorian Stoll
  4265. *
  4266. * Linux driver for Intel Precise Touch & Stylus
  4267. diff --git a/drivers/hid/ipts/thread.h b/drivers/hid/ipts/thread.h
  4268. index a314843599fc..1f966b8b32c4 100644
  4269. --- a/drivers/hid/ipts/thread.h
  4270. +++ b/drivers/hid/ipts/thread.h
  4271. @@ -1,6 +1,5 @@
  4272. /* SPDX-License-Identifier: GPL-2.0-or-later */
  4273. /*
  4274. - * Copyright (c) 2016 Intel Corporation
  4275. * Copyright (c) 2023 Dorian Stoll
  4276. *
  4277. * Linux driver for Intel Precise Touch & Stylus
  4278. @@ -29,7 +28,7 @@ struct ipts_thread {
  4279. int (*threadfn)(struct ipts_thread *thread);
  4280. };
  4281. -/*
  4282. +/**
  4283. * ipts_thread_should_stop() - Returns true if the thread is asked to terminate.
  4284. * @thread: The current thread.
  4285. *
  4286. @@ -37,7 +36,7 @@ struct ipts_thread {
  4287. */
  4288. bool ipts_thread_should_stop(struct ipts_thread *thread);
  4289. -/*
  4290. +/**
  4291. * ipts_thread_start() - Starts an IPTS thread.
  4292. * @thread: The thread to initialize and start.
  4293. * @threadfn: The function to execute.
  4294. @@ -49,7 +48,7 @@ bool ipts_thread_should_stop(struct ipts_thread *thread);
  4295. int ipts_thread_start(struct ipts_thread *thread, int (*threadfn)(struct ipts_thread *thread),
  4296. void *data, const char name[]);
  4297. -/*
  4298. +/**
  4299. * ipts_thread_stop() - Asks the thread to terminate and waits until it has finished.
  4300. * @thread: The thread that should stop.
  4301. *
  4302. --
  4303. 2.41.0
  4304. From c4dd408973703bf9dbe7a0f2cd8687376caf7152 Mon Sep 17 00:00:00 2001
  4305. From: Dorian Stoll <dorian.stoll@tmsp.io>
  4306. Date: Sat, 22 Jul 2023 17:08:56 +0200
  4307. Subject: [PATCH] Update IPTS from module repo
  4308. Changes:
  4309. * Fix allocating the HID descriptor on EDS v2
  4310. Idk why I wrote that, for EDS v1 I got it right ...
  4311. Based on https://github.com/linux-surface/intel-precise-touch/commit/32622a37f27113067a6c9aff21627859caab43f9
  4312. Signed-off-by: Dorian Stoll <dorian.stoll@tmsp.io>
  4313. Patchset: ipts
  4314. ---
  4315. drivers/hid/ipts/eds2.c | 2 +-
  4316. 1 file changed, 1 insertion(+), 1 deletion(-)
  4317. diff --git a/drivers/hid/ipts/eds2.c b/drivers/hid/ipts/eds2.c
  4318. index e835b460aa79..7c5cbafdb279 100644
  4319. --- a/drivers/hid/ipts/eds2.c
  4320. +++ b/drivers/hid/ipts/eds2.c
  4321. @@ -34,7 +34,7 @@ int ipts_eds2_get_descriptor(struct ipts_context *ipts, u8 **desc_buffer, size_t
  4322. size = sizeof(ipts_singletouch_descriptor) + ipts->descriptor.size;
  4323. buffer = kzalloc(size, GFP_KERNEL);
  4324. - if (!*buffer)
  4325. + if (!buffer)
  4326. return -ENOMEM;
  4327. memcpy(buffer, ipts_singletouch_descriptor, sizeof(ipts_singletouch_descriptor));
  4328. --
  4329. 2.41.0
  4330. From c79400bddf06027fc6a4639d2c3c0a5e0c3acf1d Mon Sep 17 00:00:00 2001
  4331. From: Dorian Stoll <dorian.stoll@tmsp.io>
  4332. Date: Sun, 23 Jul 2023 17:55:15 +0200
  4333. Subject: [PATCH] Update IPTS from module repo
  4334. Changes:
  4335. * Fix swapped parameters in EDS v2
  4336. Based on https://github.com/linux-surface/intel-precise-touch/commit/4b5c013d0140c47e4a009adf9a053a45bb0532a3
  4337. Signed-off-by: Dorian Stoll <dorian.stoll@tmsp.io>
  4338. Patchset: ipts
  4339. ---
  4340. drivers/hid/ipts/eds2.c | 4 ++--
  4341. 1 file changed, 2 insertions(+), 2 deletions(-)
  4342. diff --git a/drivers/hid/ipts/eds2.c b/drivers/hid/ipts/eds2.c
  4343. index 7c5cbafdb279..198dc65d7887 100644
  4344. --- a/drivers/hid/ipts/eds2.c
  4345. +++ b/drivers/hid/ipts/eds2.c
  4346. @@ -138,7 +138,7 @@ int ipts_eds2_raw_request(struct ipts_context *ipts, u8 *buffer, size_t size, u8
  4347. return -EIO;
  4348. if (request_type == HID_REQ_GET_REPORT)
  4349. - return ipts_eds2_get_feature(ipts, buffer, report_id, size, feedback_type);
  4350. + return ipts_eds2_get_feature(ipts, buffer, size, report_id, feedback_type);
  4351. else
  4352. - return ipts_eds2_set_feature(ipts, buffer, report_id, size, feedback_type);
  4353. + return ipts_eds2_set_feature(ipts, buffer, size, report_id, feedback_type);
  4354. }
  4355. --
  4356. 2.41.0