0003-ipts.patch 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. From 91c1d0698afc3c81905651232ca1d52ea92d9542 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 14be76d4c2e6..6cfdbedd9b13 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_JSP_N 0x4DE0 /* Jasper Lake Point N */
  19. diff --git a/drivers/misc/mei/pci-me.c b/drivers/misc/mei/pci-me.c
  20. index a7e179626b63..fdcc0eedc49f 100644
  21. --- a/drivers/misc/mei/pci-me.c
  22. +++ b/drivers/misc/mei/pci-me.c
  23. @@ -96,6 +96,7 @@ static const struct pci_device_id mei_me_pci_tbl[] = {
  24. {MEI_PCI_DEVICE(MEI_DEV_ID_CMP_H_3, MEI_ME_PCH8_ITOUCH_CFG)},
  25. {MEI_PCI_DEVICE(MEI_DEV_ID_ICP_LP, MEI_ME_PCH12_CFG)},
  26. + {MEI_PCI_DEVICE(MEI_DEV_ID_ICP_LP_3, MEI_ME_PCH12_CFG)},
  27. {MEI_PCI_DEVICE(MEI_DEV_ID_TGP_LP, MEI_ME_PCH15_CFG)},
  28. {MEI_PCI_DEVICE(MEI_DEV_ID_TGP_H, MEI_ME_PCH15_SPS_CFG)},
  29. --
  30. 2.31.0
  31. From eb288ff77b396a6833237a950f40e41dd40fe011 Mon Sep 17 00:00:00 2001
  32. From: Dorian Stoll <dorian.stoll@tmsp.io>
  33. Date: Thu, 25 Feb 2021 09:37:47 +0100
  34. Subject: [PATCH] misc: mei: Remove client devices before shutting down bus
  35. Patchset: ipts
  36. ---
  37. drivers/misc/mei/init.c | 2 +-
  38. 1 file changed, 1 insertion(+), 1 deletion(-)
  39. diff --git a/drivers/misc/mei/init.c b/drivers/misc/mei/init.c
  40. index bcee77768b91..21ed765003e1 100644
  41. --- a/drivers/misc/mei/init.c
  42. +++ b/drivers/misc/mei/init.c
  43. @@ -302,10 +302,10 @@ void mei_stop(struct mei_device *dev)
  44. {
  45. dev_dbg(dev->dev, "stopping the device.\n");
  46. + mei_cl_bus_remove_devices(dev);
  47. mutex_lock(&dev->device_lock);
  48. mei_set_devstate(dev, MEI_DEV_POWER_DOWN);
  49. mutex_unlock(&dev->device_lock);
  50. - mei_cl_bus_remove_devices(dev);
  51. mei_cancel_work(dev);
  52. --
  53. 2.31.0
  54. From f39f0f0c81d66f27fcbee988266e84300c9a5ab6 Mon Sep 17 00:00:00 2001
  55. From: Dorian Stoll <dorian.stoll@tmsp.io>
  56. Date: Thu, 6 Aug 2020 11:20:41 +0200
  57. Subject: [PATCH] misc: Add support for Intel Precise Touch & Stylus
  58. Based on linux-surface/intel-precise-touch@3f362c
  59. Signed-off-by: Dorian Stoll <dorian.stoll@tmsp.io>
  60. Patchset: ipts
  61. ---
  62. drivers/misc/Kconfig | 1 +
  63. drivers/misc/Makefile | 1 +
  64. drivers/misc/ipts/Kconfig | 17 ++
  65. drivers/misc/ipts/Makefile | 12 ++
  66. drivers/misc/ipts/context.h | 47 +++++
  67. drivers/misc/ipts/control.c | 113 +++++++++++
  68. drivers/misc/ipts/control.h | 24 +++
  69. drivers/misc/ipts/mei.c | 127 +++++++++++++
  70. drivers/misc/ipts/protocol.h | 347 ++++++++++++++++++++++++++++++++++
  71. drivers/misc/ipts/receiver.c | 224 ++++++++++++++++++++++
  72. drivers/misc/ipts/receiver.h | 16 ++
  73. drivers/misc/ipts/resources.c | 128 +++++++++++++
  74. drivers/misc/ipts/resources.h | 17 ++
  75. drivers/misc/ipts/uapi.c | 208 ++++++++++++++++++++
  76. drivers/misc/ipts/uapi.h | 47 +++++
  77. 15 files changed, 1329 insertions(+)
  78. create mode 100644 drivers/misc/ipts/Kconfig
  79. create mode 100644 drivers/misc/ipts/Makefile
  80. create mode 100644 drivers/misc/ipts/context.h
  81. create mode 100644 drivers/misc/ipts/control.c
  82. create mode 100644 drivers/misc/ipts/control.h
  83. create mode 100644 drivers/misc/ipts/mei.c
  84. create mode 100644 drivers/misc/ipts/protocol.h
  85. create mode 100644 drivers/misc/ipts/receiver.c
  86. create mode 100644 drivers/misc/ipts/receiver.h
  87. create mode 100644 drivers/misc/ipts/resources.c
  88. create mode 100644 drivers/misc/ipts/resources.h
  89. create mode 100644 drivers/misc/ipts/uapi.c
  90. create mode 100644 drivers/misc/ipts/uapi.h
  91. diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
  92. index fafa8b0d8099..c795c56e8d42 100644
  93. --- a/drivers/misc/Kconfig
  94. +++ b/drivers/misc/Kconfig
  95. @@ -481,4 +481,5 @@ source "drivers/misc/ocxl/Kconfig"
  96. source "drivers/misc/cardreader/Kconfig"
  97. source "drivers/misc/habanalabs/Kconfig"
  98. source "drivers/misc/uacce/Kconfig"
  99. +source "drivers/misc/ipts/Kconfig"
  100. endmenu
  101. diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
  102. index d23231e73330..9e6e3e2f2ea9 100644
  103. --- a/drivers/misc/Makefile
  104. +++ b/drivers/misc/Makefile
  105. @@ -57,3 +57,4 @@ obj-$(CONFIG_HABANA_AI) += habanalabs/
  106. obj-$(CONFIG_UACCE) += uacce/
  107. obj-$(CONFIG_XILINX_SDFEC) += xilinx_sdfec.o
  108. obj-$(CONFIG_HISI_HIKEY_USB) += hisi_hikey_usb.o
  109. +obj-$(CONFIG_MISC_IPTS) += ipts/
  110. diff --git a/drivers/misc/ipts/Kconfig b/drivers/misc/ipts/Kconfig
  111. new file mode 100644
  112. index 000000000000..83e2a930c396
  113. --- /dev/null
  114. +++ b/drivers/misc/ipts/Kconfig
  115. @@ -0,0 +1,17 @@
  116. +# SPDX-License-Identifier: GPL-2.0-or-later
  117. +
  118. +config MISC_IPTS
  119. + tristate "Intel Precise Touch & Stylus"
  120. + depends on INTEL_MEI
  121. + help
  122. + Say Y here if your system has a touchscreen using Intels
  123. + Precise Touch & Stylus (IPTS) technology.
  124. +
  125. + If unsure say N.
  126. +
  127. + To compile this driver as a module, choose M here: the
  128. + module will be called ipts.
  129. +
  130. + Building this driver alone will not give you a working touchscreen.
  131. + It only exposed a userspace API that can be used by a daemon to
  132. + receive and process data from the touchscreen hardware.
  133. diff --git a/drivers/misc/ipts/Makefile b/drivers/misc/ipts/Makefile
  134. new file mode 100644
  135. index 000000000000..8f58b9adbc94
  136. --- /dev/null
  137. +++ b/drivers/misc/ipts/Makefile
  138. @@ -0,0 +1,12 @@
  139. +# SPDX-License-Identifier: GPL-2.0-or-later
  140. +#
  141. +# Makefile for the IPTS touchscreen driver
  142. +#
  143. +
  144. +obj-$(CONFIG_MISC_IPTS) += ipts.o
  145. +ipts-objs := control.o
  146. +ipts-objs += mei.o
  147. +ipts-objs += receiver.o
  148. +ipts-objs += resources.o
  149. +ipts-objs += uapi.o
  150. +
  151. diff --git a/drivers/misc/ipts/context.h b/drivers/misc/ipts/context.h
  152. new file mode 100644
  153. index 000000000000..f4b06a2d3f72
  154. --- /dev/null
  155. +++ b/drivers/misc/ipts/context.h
  156. @@ -0,0 +1,47 @@
  157. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  158. +/*
  159. + * Copyright (c) 2016 Intel Corporation
  160. + * Copyright (c) 2020 Dorian Stoll
  161. + *
  162. + * Linux driver for Intel Precise Touch & Stylus
  163. + */
  164. +
  165. +#ifndef _IPTS_CONTEXT_H_
  166. +#define _IPTS_CONTEXT_H_
  167. +
  168. +#include <linux/cdev.h>
  169. +#include <linux/device.h>
  170. +#include <linux/mei_cl_bus.h>
  171. +#include <linux/types.h>
  172. +
  173. +#include "protocol.h"
  174. +
  175. +enum ipts_host_status {
  176. + IPTS_HOST_STATUS_STARTING,
  177. + IPTS_HOST_STATUS_STARTED,
  178. + IPTS_HOST_STATUS_STOPPING,
  179. + IPTS_HOST_STATUS_STOPPED,
  180. +};
  181. +
  182. +struct ipts_buffer_info {
  183. + u8 *address;
  184. + dma_addr_t dma_address;
  185. +};
  186. +
  187. +struct ipts_context {
  188. + struct mei_cl_device *cldev;
  189. + struct device *dev;
  190. +
  191. + bool restart;
  192. + enum ipts_host_status status;
  193. + struct ipts_get_device_info_rsp device_info;
  194. +
  195. + struct ipts_buffer_info data[IPTS_BUFFERS];
  196. + struct ipts_buffer_info doorbell;
  197. +
  198. + struct ipts_buffer_info feedback[IPTS_BUFFERS];
  199. + struct ipts_buffer_info workqueue;
  200. + struct ipts_buffer_info host2me;
  201. +};
  202. +
  203. +#endif /* _IPTS_CONTEXT_H_ */
  204. diff --git a/drivers/misc/ipts/control.c b/drivers/misc/ipts/control.c
  205. new file mode 100644
  206. index 000000000000..a1d1f97a13d7
  207. --- /dev/null
  208. +++ b/drivers/misc/ipts/control.c
  209. @@ -0,0 +1,113 @@
  210. +// SPDX-License-Identifier: GPL-2.0-or-later
  211. +/*
  212. + * Copyright (c) 2016 Intel Corporation
  213. + * Copyright (c) 2020 Dorian Stoll
  214. + *
  215. + * Linux driver for Intel Precise Touch & Stylus
  216. + */
  217. +
  218. +#include <linux/mei_cl_bus.h>
  219. +
  220. +#include "context.h"
  221. +#include "protocol.h"
  222. +#include "resources.h"
  223. +#include "uapi.h"
  224. +
  225. +int ipts_control_send(struct ipts_context *ipts, u32 code, void *payload,
  226. + size_t size)
  227. +{
  228. + int ret;
  229. + struct ipts_command cmd;
  230. +
  231. + memset(&cmd, 0, sizeof(struct ipts_command));
  232. + cmd.code = code;
  233. +
  234. + if (payload && size > 0)
  235. + memcpy(&cmd.payload, payload, size);
  236. +
  237. + ret = mei_cldev_send(ipts->cldev, (u8 *)&cmd, sizeof(cmd.code) + size);
  238. + if (ret >= 0)
  239. + return 0;
  240. +
  241. + /*
  242. + * During shutdown the device might get pulled away from below our feet.
  243. + * Dont log an error in this case, because it will confuse people.
  244. + */
  245. + if (ret != -ENODEV || ipts->status != IPTS_HOST_STATUS_STOPPING)
  246. + dev_err(ipts->dev, "Error while sending: 0x%X:%d\n", code, ret);
  247. +
  248. + return ret;
  249. +}
  250. +
  251. +int ipts_control_send_feedback(struct ipts_context *ipts, u32 buffer)
  252. +{
  253. + struct ipts_feedback_cmd cmd;
  254. +
  255. + memset(&cmd, 0, sizeof(struct ipts_feedback_cmd));
  256. + cmd.buffer = buffer;
  257. +
  258. + return ipts_control_send(ipts, IPTS_CMD_FEEDBACK, &cmd,
  259. + sizeof(struct ipts_feedback_cmd));
  260. +}
  261. +
  262. +int ipts_control_set_feature(struct ipts_context *ipts, u8 report, u8 value)
  263. +{
  264. + struct ipts_feedback_buffer *feedback;
  265. +
  266. + memset(ipts->host2me.address, 0, ipts->device_info.feedback_size);
  267. + feedback = (struct ipts_feedback_buffer *)ipts->host2me.address;
  268. +
  269. + feedback->cmd_type = IPTS_FEEDBACK_CMD_TYPE_NONE;
  270. + feedback->data_type = IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES;
  271. + feedback->buffer = IPTS_HOST2ME_BUFFER;
  272. + feedback->size = 2;
  273. + feedback->payload[0] = report;
  274. + feedback->payload[1] = value;
  275. +
  276. + return ipts_control_send_feedback(ipts, IPTS_HOST2ME_BUFFER);
  277. +}
  278. +
  279. +int ipts_control_start(struct ipts_context *ipts)
  280. +{
  281. + if (ipts->status != IPTS_HOST_STATUS_STOPPED)
  282. + return -EBUSY;
  283. +
  284. + dev_info(ipts->dev, "Starting IPTS\n");
  285. + ipts->status = IPTS_HOST_STATUS_STARTING;
  286. + ipts->restart = false;
  287. +
  288. + ipts_uapi_link(ipts);
  289. + return ipts_control_send(ipts, IPTS_CMD_GET_DEVICE_INFO, NULL, 0);
  290. +}
  291. +
  292. +int ipts_control_stop(struct ipts_context *ipts)
  293. +{
  294. + int ret;
  295. +
  296. + if (ipts->status == IPTS_HOST_STATUS_STOPPING)
  297. + return -EBUSY;
  298. +
  299. + if (ipts->status == IPTS_HOST_STATUS_STOPPED)
  300. + return -EBUSY;
  301. +
  302. + dev_info(ipts->dev, "Stopping IPTS\n");
  303. + ipts->status = IPTS_HOST_STATUS_STOPPING;
  304. +
  305. + ipts_uapi_unlink();
  306. + ipts_resources_free(ipts);
  307. +
  308. + ret = ipts_control_send_feedback(ipts, 0);
  309. + if (ret == -ENODEV)
  310. + ipts->status = IPTS_HOST_STATUS_STOPPED;
  311. +
  312. + return ret;
  313. +}
  314. +
  315. +int ipts_control_restart(struct ipts_context *ipts)
  316. +{
  317. + if (ipts->restart)
  318. + return -EBUSY;
  319. +
  320. + ipts->restart = true;
  321. + return ipts_control_stop(ipts);
  322. +}
  323. diff --git a/drivers/misc/ipts/control.h b/drivers/misc/ipts/control.h
  324. new file mode 100644
  325. index 000000000000..2c44e9e0e99f
  326. --- /dev/null
  327. +++ b/drivers/misc/ipts/control.h
  328. @@ -0,0 +1,24 @@
  329. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  330. +/*
  331. + * Copyright (c) 2016 Intel Corporation
  332. + * Copyright (c) 2020 Dorian Stoll
  333. + *
  334. + * Linux driver for Intel Precise Touch & Stylus
  335. + */
  336. +
  337. +#ifndef _IPTS_CONTROL_H_
  338. +#define _IPTS_CONTROL_H_
  339. +
  340. +#include <linux/types.h>
  341. +
  342. +#include "context.h"
  343. +
  344. +int ipts_control_send(struct ipts_context *ipts, u32 cmd, void *payload,
  345. + size_t size);
  346. +int ipts_control_send_feedback(struct ipts_context *ipts, u32 buffer);
  347. +int ipts_control_set_feature(struct ipts_context *ipts, u8 report, u8 value);
  348. +int ipts_control_start(struct ipts_context *ipts);
  349. +int ipts_control_restart(struct ipts_context *ipts);
  350. +int ipts_control_stop(struct ipts_context *ipts);
  351. +
  352. +#endif /* _IPTS_CONTROL_H_ */
  353. diff --git a/drivers/misc/ipts/mei.c b/drivers/misc/ipts/mei.c
  354. new file mode 100644
  355. index 000000000000..b3805a91383d
  356. --- /dev/null
  357. +++ b/drivers/misc/ipts/mei.c
  358. @@ -0,0 +1,127 @@
  359. +// SPDX-License-Identifier: GPL-2.0-or-later
  360. +/*
  361. + * Copyright (c) 2016 Intel Corporation
  362. + * Copyright (c) 2020 Dorian Stoll
  363. + *
  364. + * Linux driver for Intel Precise Touch & Stylus
  365. + */
  366. +
  367. +#include <linux/delay.h>
  368. +#include <linux/dma-mapping.h>
  369. +#include <linux/mei_cl_bus.h>
  370. +#include <linux/mod_devicetable.h>
  371. +#include <linux/module.h>
  372. +#include <linux/slab.h>
  373. +
  374. +#include "context.h"
  375. +#include "control.h"
  376. +#include "protocol.h"
  377. +#include "receiver.h"
  378. +#include "uapi.h"
  379. +
  380. +static int ipts_mei_set_dma_mask(struct mei_cl_device *cldev)
  381. +{
  382. + int ret;
  383. +
  384. + ret = dma_coerce_mask_and_coherent(&cldev->dev, DMA_BIT_MASK(64));
  385. + if (!ret)
  386. + return 0;
  387. +
  388. + return dma_coerce_mask_and_coherent(&cldev->dev, DMA_BIT_MASK(32));
  389. +}
  390. +
  391. +static int ipts_mei_probe(struct mei_cl_device *cldev,
  392. + const struct mei_cl_device_id *id)
  393. +{
  394. + int ret;
  395. + struct ipts_context *ipts;
  396. +
  397. + if (ipts_mei_set_dma_mask(cldev)) {
  398. + dev_err(&cldev->dev, "Failed to set DMA mask for IPTS\n");
  399. + return -EFAULT;
  400. + }
  401. +
  402. + ret = mei_cldev_enable(cldev);
  403. + if (ret) {
  404. + dev_err(&cldev->dev, "Failed to enable MEI device: %d\n", ret);
  405. + return ret;
  406. + }
  407. +
  408. + ipts = kzalloc(sizeof(*ipts), GFP_KERNEL);
  409. + if (!ipts) {
  410. + mei_cldev_disable(cldev);
  411. + return -ENOMEM;
  412. + }
  413. +
  414. + ipts->cldev = cldev;
  415. + ipts->dev = &cldev->dev;
  416. + ipts->status = IPTS_HOST_STATUS_STOPPED;
  417. +
  418. + mei_cldev_set_drvdata(cldev, ipts);
  419. + mei_cldev_register_rx_cb(cldev, ipts_receiver_callback);
  420. +
  421. + return ipts_control_start(ipts);
  422. +}
  423. +
  424. +static int ipts_mei_remove(struct mei_cl_device *cldev)
  425. +{
  426. + int i;
  427. + struct ipts_context *ipts = mei_cldev_get_drvdata(cldev);
  428. +
  429. + ipts_control_stop(ipts);
  430. +
  431. + for (i = 0; i < 20; i++) {
  432. + if (ipts->status == IPTS_HOST_STATUS_STOPPED)
  433. + break;
  434. +
  435. + msleep(25);
  436. + }
  437. +
  438. + mei_cldev_disable(cldev);
  439. + kfree(ipts);
  440. +
  441. + return 0;
  442. +}
  443. +
  444. +static struct mei_cl_device_id ipts_mei_device_id_table[] = {
  445. + { "", IPTS_MEI_UUID, MEI_CL_VERSION_ANY },
  446. + {},
  447. +};
  448. +MODULE_DEVICE_TABLE(mei, ipts_mei_device_id_table);
  449. +
  450. +static struct mei_cl_driver ipts_mei_driver = {
  451. + .id_table = ipts_mei_device_id_table,
  452. + .name = "ipts",
  453. + .probe = ipts_mei_probe,
  454. + .remove = ipts_mei_remove,
  455. +};
  456. +
  457. +static int __init ipts_mei_init(void)
  458. +{
  459. + int ret;
  460. +
  461. + ret = ipts_uapi_init();
  462. + if (ret)
  463. + return ret;
  464. +
  465. + ret = mei_cldev_driver_register(&ipts_mei_driver);
  466. + if (ret) {
  467. + ipts_uapi_free();
  468. + return ret;
  469. + }
  470. +
  471. + return 0;
  472. +}
  473. +
  474. +static void __exit ipts_mei_exit(void)
  475. +{
  476. + mei_cldev_driver_unregister(&ipts_mei_driver);
  477. + ipts_uapi_free();
  478. +}
  479. +
  480. +MODULE_DESCRIPTION("IPTS touchscreen driver");
  481. +MODULE_AUTHOR("Dorian Stoll <dorian.stoll@tmsp.io>");
  482. +MODULE_LICENSE("GPL");
  483. +
  484. +module_init(ipts_mei_init);
  485. +module_exit(ipts_mei_exit);
  486. diff --git a/drivers/misc/ipts/protocol.h b/drivers/misc/ipts/protocol.h
  487. new file mode 100644
  488. index 000000000000..c3458904a94d
  489. --- /dev/null
  490. +++ b/drivers/misc/ipts/protocol.h
  491. @@ -0,0 +1,347 @@
  492. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  493. +/*
  494. + * Copyright (c) 2016 Intel Corporation
  495. + * Copyright (c) 2020 Dorian Stoll
  496. + *
  497. + * Linux driver for Intel Precise Touch & Stylus
  498. + */
  499. +
  500. +#ifndef _IPTS_PROTOCOL_H_
  501. +#define _IPTS_PROTOCOL_H_
  502. +
  503. +#include <linux/types.h>
  504. +
  505. +/*
  506. + * The MEI client ID for IPTS functionality.
  507. + */
  508. +#define IPTS_MEI_UUID \
  509. + UUID_LE(0x3e8d0870, 0x271a, 0x4208, 0x8e, 0xb5, 0x9a, 0xcb, 0x94, \
  510. + 0x02, 0xae, 0x04)
  511. +
  512. +/*
  513. + * Queries the device for vendor specific information.
  514. + *
  515. + * The command must not contain any payload.
  516. + * The response will contain struct ipts_get_device_info_rsp as payload.
  517. + */
  518. +#define IPTS_CMD_GET_DEVICE_INFO 0x00000001
  519. +#define IPTS_RSP_GET_DEVICE_INFO 0x80000001
  520. +
  521. +/*
  522. + * Sets the mode that IPTS will operate in.
  523. + *
  524. + * The command must contain struct ipts_set_mode_cmd as payload.
  525. + * The response will not contain any payload.
  526. + */
  527. +#define IPTS_CMD_SET_MODE 0x00000002
  528. +#define IPTS_RSP_SET_MODE 0x80000002
  529. +
  530. +/*
  531. + * Configures the memory buffers that the ME will use
  532. + * for passing data to the host.
  533. + *
  534. + * The command must contain struct ipts_set_mem_window_cmd as payload.
  535. + * The response will not contain any payload.
  536. + */
  537. +#define IPTS_CMD_SET_MEM_WINDOW 0x00000003
  538. +#define IPTS_RSP_SET_MEM_WINDOW 0x80000003
  539. +
  540. +/*
  541. + * Signals that the host is ready to receive data to the ME.
  542. + *
  543. + * The command must not contain any payload.
  544. + * The response will not contain any payload.
  545. + */
  546. +#define IPTS_CMD_READY_FOR_DATA 0x00000005
  547. +#define IPTS_RSP_READY_FOR_DATA 0x80000005
  548. +
  549. +/*
  550. + * Signals that a buffer can be refilled to the ME.
  551. + *
  552. + * The command must contain struct ipts_feedback_cmd as payload.
  553. + * The response will not contain any payload.
  554. + */
  555. +#define IPTS_CMD_FEEDBACK 0x00000006
  556. +#define IPTS_RSP_FEEDBACK 0x80000006
  557. +
  558. +/*
  559. + * Resets the data flow from the ME to the hosts and
  560. + * clears the buffers that were set with SET_MEM_WINDOW.
  561. + *
  562. + * The command must not contain any payload.
  563. + * The response will not contain any payload.
  564. + */
  565. +#define IPTS_CMD_CLEAR_MEM_WINDOW 0x00000007
  566. +#define IPTS_RSP_CLEAR_MEM_WINDOW 0x80000007
  567. +
  568. +/*
  569. + * Instructs the ME to reset the touch sensor.
  570. + *
  571. + * The command must contain struct ipts_reset_sensor_cmd as payload.
  572. + * The response will not contain any payload.
  573. + */
  574. +#define IPTS_CMD_RESET_SENSOR 0x0000000B
  575. +#define IPTS_RSP_RESET_SENSOR 0x8000000B
  576. +
  577. +/**
  578. + * enum ipts_status - Possible status codes returned by IPTS commands.
  579. + * @IPTS_STATUS_SUCCESS: Operation completed successfully.
  580. + * @IPTS_STATUS_INVALID_PARAMS: Command contained a payload with invalid parameters.
  581. + * @IPTS_STATUS_ACCESS_DENIED: ME could not validate buffer addresses supplied by host.
  582. + * @IPTS_STATUS_CMD_SIZE_ERROR: Command contains an invalid payload.
  583. + * @IPTS_STATUS_NOT_READY: Buffer addresses have not been set.
  584. + * @IPTS_STATUS_REQUEST_OUTSTANDING: There is an outstanding command of the same type.
  585. + * The host must wait for a response before sending another
  586. + * command of the same type.
  587. + * @IPTS_STATUS_NO_SENSOR_FOUND: No sensor could be found. Either no sensor is connected, it
  588. + * has not been initialized yet, or the system is improperly
  589. + * configured.
  590. + * @IPTS_STATUS_OUT_OF_MEMORY: Not enough free memory for requested operation.
  591. + * @IPTS_STATUS_INTERNAL_ERROR: An unexpected error occurred.
  592. + * @IPTS_STATUS_SENSOR_DISABLED: The sensor has been disabled and must be reinitialized.
  593. + * @IPTS_STATUS_COMPAT_CHECK_FAIL: Compatibility revision check between sensor and ME failed.
  594. + * The host can ignore this error and attempt to continue.
  595. + * @IPTS_STATUS_SENSOR_EXPECTED_RESET: The sensor went through a reset initiated by ME or host.
  596. + * @IPTS_STATUS_SENSOR_UNEXPECTED_RESET: The sensor went through an unexpected reset.
  597. + * @IPTS_STATUS_RESET_FAILED: Requested sensor reset failed to complete.
  598. + * @IPTS_STATUS_TIMEOUT: The operation timed out.
  599. + * @IPTS_STATUS_TEST_MODE_FAIL: Test mode pattern did not match expected values.
  600. + * @IPTS_STATUS_SENSOR_FAIL_FATAL: The sensor reported a fatal error during reset sequence.
  601. + * Further progress is not possible.
  602. + * @IPTS_STATUS_SENSOR_FAIL_NONFATAL: The sensor reported a fatal error during reset sequence.
  603. + * The host can attempt to continue.
  604. + * @IPTS_STATUS_INVALID_DEVICE_CAPS: The device reported invalid capabilities.
  605. + * @IPTS_STATUS_QUIESCE_IO_IN_PROGRESS: Command cannot be completed until Quiesce IO is done.
  606. + */
  607. +enum ipts_status {
  608. + IPTS_STATUS_SUCCESS = 0,
  609. + IPTS_STATUS_INVALID_PARAMS = 1,
  610. + IPTS_STATUS_ACCESS_DENIED = 2,
  611. + IPTS_STATUS_CMD_SIZE_ERROR = 3,
  612. + IPTS_STATUS_NOT_READY = 4,
  613. + IPTS_STATUS_REQUEST_OUTSTANDING = 5,
  614. + IPTS_STATUS_NO_SENSOR_FOUND = 6,
  615. + IPTS_STATUS_OUT_OF_MEMORY = 7,
  616. + IPTS_STATUS_INTERNAL_ERROR = 8,
  617. + IPTS_STATUS_SENSOR_DISABLED = 9,
  618. + IPTS_STATUS_COMPAT_CHECK_FAIL = 10,
  619. + IPTS_STATUS_SENSOR_EXPECTED_RESET = 11,
  620. + IPTS_STATUS_SENSOR_UNEXPECTED_RESET = 12,
  621. + IPTS_STATUS_RESET_FAILED = 13,
  622. + IPTS_STATUS_TIMEOUT = 14,
  623. + IPTS_STATUS_TEST_MODE_FAIL = 15,
  624. + IPTS_STATUS_SENSOR_FAIL_FATAL = 16,
  625. + IPTS_STATUS_SENSOR_FAIL_NONFATAL = 17,
  626. + IPTS_STATUS_INVALID_DEVICE_CAPS = 18,
  627. + IPTS_STATUS_QUIESCE_IO_IN_PROGRESS = 19,
  628. +};
  629. +
  630. +/*
  631. + * The amount of buffers that is used for IPTS
  632. + */
  633. +#define IPTS_BUFFERS 16
  634. +
  635. +/*
  636. + * The special buffer ID that is used for direct host2me feedback.
  637. + */
  638. +#define IPTS_HOST2ME_BUFFER IPTS_BUFFERS
  639. +
  640. +/**
  641. + * enum ipts_mode - Operation mode for IPTS hardware
  642. + * @IPTS_MODE_SINGLETOUCH: Fallback that supports only one finger and no stylus.
  643. + * The data is received as a HID report with ID 64.
  644. + * @IPTS_MODE_MULTITOUCH: The "proper" operation mode for IPTS. It will return
  645. + * stylus data as well as capacitive heatmap touch data.
  646. + * This data needs to be processed in userspace.
  647. + */
  648. +enum ipts_mode {
  649. + IPTS_MODE_SINGLETOUCH = 0,
  650. + IPTS_MODE_MULTITOUCH = 1,
  651. +};
  652. +
  653. +/**
  654. + * struct ipts_set_mode_cmd - Payload for the SET_MODE command.
  655. + * @mode: The mode that IPTS should operate in.
  656. + */
  657. +struct ipts_set_mode_cmd {
  658. + enum ipts_mode mode;
  659. + u8 reserved[12];
  660. +} __packed;
  661. +
  662. +#define IPTS_WORKQUEUE_SIZE 8192
  663. +#define IPTS_WORKQUEUE_ITEM_SIZE 16
  664. +
  665. +/**
  666. + * struct ipts_set_mem_window_cmd - Payload for the SET_MEM_WINDOW command.
  667. + * @data_buffer_addr_lower: Lower 32 bits of the data buffer addresses.
  668. + * @data_buffer_addr_upper: Upper 32 bits of the data buffer addresses.
  669. + * @workqueue_addr_lower: Lower 32 bits of the workqueue buffer address.
  670. + * @workqueue_addr_upper: Upper 32 bits of the workqueue buffer address.
  671. + * @doorbell_addr_lower: Lower 32 bits of the doorbell buffer address.
  672. + * @doorbell_addr_upper: Upper 32 bits of the doorbell buffer address.
  673. + * @feedback_buffer_addr_lower: Lower 32 bits of the feedback buffer addresses.
  674. + * @feedback_buffer_addr_upper: Upper 32 bits of the feedback buffer addresses.
  675. + * @host2me_addr_lower: Lower 32 bits of the host2me buffer address.
  676. + * @host2me_addr_upper: Upper 32 bits of the host2me buffer address.
  677. + * @workqueue_item_size: Magic value. (IPTS_WORKQUEUE_ITEM_SIZE)
  678. + * @workqueue_size: Magic value. (IPTS_WORKQUEUE_SIZE)
  679. + *
  680. + * The data buffers are buffers that get filled with touch data by the ME.
  681. + * The doorbell buffer is a u32 that gets incremented by the ME once a data
  682. + * buffer has been filled with new data.
  683. + *
  684. + * The other buffers are required for using GuC submission with binary
  685. + * firmware. Since support for GuC submission has been dropped from i915,
  686. + * they are not used anymore, but they need to be allocated and passed,
  687. + * otherwise the hardware will refuse to start.
  688. + */
  689. +struct ipts_set_mem_window_cmd {
  690. + u32 data_buffer_addr_lower[IPTS_BUFFERS];
  691. + u32 data_buffer_addr_upper[IPTS_BUFFERS];
  692. + u32 workqueue_addr_lower;
  693. + u32 workqueue_addr_upper;
  694. + u32 doorbell_addr_lower;
  695. + u32 doorbell_addr_upper;
  696. + u32 feedback_buffer_addr_lower[IPTS_BUFFERS];
  697. + u32 feedback_buffer_addr_upper[IPTS_BUFFERS];
  698. + u32 host2me_addr_lower;
  699. + u32 host2me_addr_upper;
  700. + u32 host2me_size;
  701. + u8 reserved1;
  702. + u8 workqueue_item_size;
  703. + u16 workqueue_size;
  704. + u8 reserved[32];
  705. +} __packed;
  706. +
  707. +/**
  708. + * struct ipts_feedback_cmd - Payload for the FEEDBACK command.
  709. + * @buffer: The buffer that the ME should refill.
  710. + */
  711. +struct ipts_feedback_cmd {
  712. + u32 buffer;
  713. + u8 reserved[12];
  714. +} __packed;
  715. +
  716. +/**
  717. + * enum ipts_feedback_cmd_type - Commands that can be executed on the sensor through feedback.
  718. + */
  719. +enum ipts_feedback_cmd_type {
  720. + IPTS_FEEDBACK_CMD_TYPE_NONE = 0,
  721. + IPTS_FEEDBACK_CMD_TYPE_SOFT_RESET = 1,
  722. + IPTS_FEEDBACK_CMD_TYPE_GOTO_ARMED = 2,
  723. + IPTS_FEEDBACK_CMD_TYPE_GOTO_SENSING = 3,
  724. + IPTS_FEEDBACK_CMD_TYPE_GOTO_SLEEP = 4,
  725. + IPTS_FEEDBACK_CMD_TYPE_GOTO_DOZE = 5,
  726. + IPTS_FEEDBACK_CMD_TYPE_HARD_RESET = 6,
  727. +};
  728. +
  729. +/**
  730. + * enum ipts_feedback_data_type - Describes the data that a feedback buffer contains.
  731. + * @IPTS_FEEDBACK_DATA_TYPE_VENDOR: The buffer contains vendor specific feedback.
  732. + * @IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES: The buffer contains a HID set features command.
  733. + * @IPTS_FEEDBACK_DATA_TYPE_GET_FEATURES: The buffer contains a HID get features command.
  734. + * @IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT: The buffer contains a HID output report.
  735. + * @IPTS_FEEDBACK_DATA_TYPE_STORE_DATA: The buffer contains calibration data for the sensor.
  736. + */
  737. +enum ipts_feedback_data_type {
  738. + IPTS_FEEDBACK_DATA_TYPE_VENDOR = 0,
  739. + IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES = 1,
  740. + IPTS_FEEDBACK_DATA_TYPE_GET_FEATURES = 2,
  741. + IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT = 3,
  742. + IPTS_FEEDBACK_DATA_TYPE_STORE_DATA = 4,
  743. +};
  744. +
  745. +/**
  746. + * struct ipts_feedback_buffer - The contents of an IPTS feedback buffer.
  747. + * @cmd_type: A command that should be executed on the sensor.
  748. + * @size: The size of the payload to be written.
  749. + * @buffer: The ID of the buffer that contains this feedback data.
  750. + * @protocol: The protocol version of the EDS.
  751. + * @data_type: The type of payload that the buffer contains.
  752. + * @spi_offset: The offset at which to write the payload data.
  753. + * @payload: Payload for the feedback command, or 0 if no payload is sent.
  754. + */
  755. +struct ipts_feedback_buffer {
  756. + enum ipts_feedback_cmd_type cmd_type;
  757. + u32 size;
  758. + u32 buffer;
  759. + u32 protocol;
  760. + enum ipts_feedback_data_type data_type;
  761. + u32 spi_offset;
  762. + u8 reserved[40];
  763. + u8 payload[];
  764. +} __packed;
  765. +
  766. +/**
  767. + * enum ipts_reset_type - Possible ways of resetting the touch sensor
  768. + * @IPTS_RESET_TYPE_HARD: Perform hardware reset using GPIO pin.
  769. + * @IPTS_RESET_TYPE_SOFT: Perform software reset using SPI interface.
  770. + */
  771. +enum ipts_reset_type {
  772. + IPTS_RESET_TYPE_HARD = 0,
  773. + IPTS_RESET_TYPE_SOFT = 1,
  774. +};
  775. +
  776. +/**
  777. + * struct ipts_reset_sensor_cmd - Payload for the RESET_SENSOR command.
  778. + * @type: What type of reset should be performed.
  779. + */
  780. +struct ipts_reset_sensor_cmd {
  781. + enum ipts_reset_type type;
  782. + u8 reserved[4];
  783. +} __packed;
  784. +
  785. +/**
  786. + * struct ipts_command - A message sent from the host to the ME.
  787. + * @code: The message code describing the command. (see IPTS_CMD_*)
  788. + * @payload: Payload for the command, or 0 if no payload is required.
  789. + */
  790. +struct ipts_command {
  791. + u32 code;
  792. + u8 payload[320];
  793. +} __packed;
  794. +
  795. +/**
  796. + * struct ipts_device_info - Payload for the GET_DEVICE_INFO response.
  797. + * @vendor_id: Vendor ID of the touch sensor.
  798. + * @device_id: Device ID of the touch sensor.
  799. + * @hw_rev: Hardware revision of the touch sensor.
  800. + * @fw_rev: Firmware revision of the touch sensor.
  801. + * @data_size: Required size of one data buffer.
  802. + * @feedback_size: Required size of one feedback buffer.
  803. + * @mode: Current operation mode of IPTS.
  804. + * @max_contacts: The amount of concurrent touches supported by the sensor.
  805. + */
  806. +struct ipts_get_device_info_rsp {
  807. + u16 vendor_id;
  808. + u16 device_id;
  809. + u32 hw_rev;
  810. + u32 fw_rev;
  811. + u32 data_size;
  812. + u32 feedback_size;
  813. + enum ipts_mode mode;
  814. + u8 max_contacts;
  815. + u8 reserved[19];
  816. +} __packed;
  817. +
  818. +/**
  819. + * struct ipts_feedback_rsp - Payload for the FEEDBACK response.
  820. + * @buffer: The buffer that has received feedback.
  821. + */
  822. +struct ipts_feedback_rsp {
  823. + u32 buffer;
  824. +} __packed;
  825. +
  826. +/**
  827. + * struct ipts_response - A message sent from the ME to the host.
  828. + * @code: The message code describing the response. (see IPTS_RSP_*)
  829. + * @status: The status code returned by the command.
  830. + * @payload: Payload returned by the command.
  831. + */
  832. +struct ipts_response {
  833. + u32 code;
  834. + enum ipts_status status;
  835. + u8 payload[80];
  836. +} __packed;
  837. +
  838. +#endif /* _IPTS_PROTOCOL_H_ */
  839. diff --git a/drivers/misc/ipts/receiver.c b/drivers/misc/ipts/receiver.c
  840. new file mode 100644
  841. index 000000000000..23dca13c2139
  842. --- /dev/null
  843. +++ b/drivers/misc/ipts/receiver.c
  844. @@ -0,0 +1,224 @@
  845. +// SPDX-License-Identifier: GPL-2.0-or-later
  846. +/*
  847. + * Copyright (c) 2016 Intel Corporation
  848. + * Copyright (c) 2020 Dorian Stoll
  849. + *
  850. + * Linux driver for Intel Precise Touch & Stylus
  851. + */
  852. +
  853. +#include <linux/mei_cl_bus.h>
  854. +#include <linux/moduleparam.h>
  855. +#include <linux/types.h>
  856. +
  857. +#include "context.h"
  858. +#include "control.h"
  859. +#include "protocol.h"
  860. +#include "resources.h"
  861. +
  862. +/*
  863. + * Temporary parameter to guard gen7 multitouch mode.
  864. + * Remove once gen7 has stable iptsd support.
  865. + */
  866. +static bool gen7mt;
  867. +module_param(gen7mt, bool, 0644);
  868. +
  869. +static int ipts_receiver_handle_get_device_info(struct ipts_context *ipts,
  870. + struct ipts_response *rsp)
  871. +{
  872. + struct ipts_set_mode_cmd cmd;
  873. +
  874. + memcpy(&ipts->device_info, rsp->payload,
  875. + sizeof(struct ipts_get_device_info_rsp));
  876. +
  877. + memset(&cmd, 0, sizeof(struct ipts_set_mode_cmd));
  878. + cmd.mode = IPTS_MODE_MULTITOUCH;
  879. +
  880. + return ipts_control_send(ipts, IPTS_CMD_SET_MODE, &cmd,
  881. + sizeof(struct ipts_set_mode_cmd));
  882. +}
  883. +
  884. +static int ipts_receiver_handle_set_mode(struct ipts_context *ipts)
  885. +{
  886. + int i, ret;
  887. + struct ipts_set_mem_window_cmd cmd;
  888. +
  889. + ret = ipts_resources_alloc(ipts);
  890. + if (ret) {
  891. + dev_err(ipts->dev, "Failed to allocate resources\n");
  892. + return ret;
  893. + }
  894. +
  895. + memset(&cmd, 0, sizeof(struct ipts_set_mem_window_cmd));
  896. +
  897. + for (i = 0; i < IPTS_BUFFERS; i++) {
  898. + cmd.data_buffer_addr_lower[i] =
  899. + lower_32_bits(ipts->data[i].dma_address);
  900. +
  901. + cmd.data_buffer_addr_upper[i] =
  902. + upper_32_bits(ipts->data[i].dma_address);
  903. +
  904. + cmd.feedback_buffer_addr_lower[i] =
  905. + lower_32_bits(ipts->feedback[i].dma_address);
  906. +
  907. + cmd.feedback_buffer_addr_upper[i] =
  908. + upper_32_bits(ipts->feedback[i].dma_address);
  909. + }
  910. +
  911. + cmd.workqueue_addr_lower = lower_32_bits(ipts->workqueue.dma_address);
  912. + cmd.workqueue_addr_upper = upper_32_bits(ipts->workqueue.dma_address);
  913. +
  914. + cmd.doorbell_addr_lower = lower_32_bits(ipts->doorbell.dma_address);
  915. + cmd.doorbell_addr_upper = upper_32_bits(ipts->doorbell.dma_address);
  916. +
  917. + cmd.host2me_addr_lower = lower_32_bits(ipts->host2me.dma_address);
  918. + cmd.host2me_addr_upper = upper_32_bits(ipts->host2me.dma_address);
  919. +
  920. + cmd.workqueue_size = IPTS_WORKQUEUE_SIZE;
  921. + cmd.workqueue_item_size = IPTS_WORKQUEUE_ITEM_SIZE;
  922. +
  923. + return ipts_control_send(ipts, IPTS_CMD_SET_MEM_WINDOW, &cmd,
  924. + sizeof(struct ipts_set_mem_window_cmd));
  925. +}
  926. +
  927. +static int ipts_receiver_handle_set_mem_window(struct ipts_context *ipts)
  928. +{
  929. + int ret;
  930. +
  931. + dev_info(ipts->dev, "Device %04hX:%04hX ready\n",
  932. + ipts->device_info.vendor_id, ipts->device_info.device_id);
  933. + ipts->status = IPTS_HOST_STATUS_STARTED;
  934. +
  935. + ret = ipts_control_send(ipts, IPTS_CMD_READY_FOR_DATA, NULL, 0);
  936. + if (ret)
  937. + return ret;
  938. +
  939. + if (!gen7mt)
  940. + return 0;
  941. +
  942. + return ipts_control_set_feature(ipts, 0x5, 0x1);
  943. +}
  944. +
  945. +static int ipts_receiver_handle_feedback(struct ipts_context *ipts,
  946. + struct ipts_response *rsp)
  947. +{
  948. + struct ipts_feedback_rsp feedback;
  949. +
  950. + if (ipts->status != IPTS_HOST_STATUS_STOPPING)
  951. + return 0;
  952. +
  953. + memcpy(&feedback, rsp->payload, sizeof(feedback));
  954. +
  955. + if (feedback.buffer < IPTS_BUFFERS - 1)
  956. + return ipts_control_send_feedback(ipts, feedback.buffer + 1);
  957. +
  958. + return ipts_control_send(ipts, IPTS_CMD_CLEAR_MEM_WINDOW, NULL, 0);
  959. +}
  960. +
  961. +static int ipts_receiver_handle_clear_mem_window(struct ipts_context *ipts)
  962. +{
  963. + ipts->status = IPTS_HOST_STATUS_STOPPED;
  964. +
  965. + if (ipts->restart)
  966. + return ipts_control_start(ipts);
  967. +
  968. + return 0;
  969. +}
  970. +
  971. +static bool ipts_receiver_sensor_was_reset(u32 status)
  972. +{
  973. + return status == IPTS_STATUS_SENSOR_EXPECTED_RESET ||
  974. + status == IPTS_STATUS_SENSOR_UNEXPECTED_RESET;
  975. +}
  976. +
  977. +static bool ipts_receiver_handle_error(struct ipts_context *ipts,
  978. + struct ipts_response *rsp)
  979. +{
  980. + bool error;
  981. +
  982. + switch (rsp->status) {
  983. + case IPTS_STATUS_SUCCESS:
  984. + case IPTS_STATUS_COMPAT_CHECK_FAIL:
  985. + error = false;
  986. + break;
  987. + case IPTS_STATUS_INVALID_PARAMS:
  988. + error = rsp->code != IPTS_RSP_FEEDBACK;
  989. + break;
  990. + case IPTS_STATUS_SENSOR_DISABLED:
  991. + error = ipts->status != IPTS_HOST_STATUS_STOPPING;
  992. + break;
  993. + default:
  994. + error = true;
  995. + break;
  996. + }
  997. +
  998. + if (!error)
  999. + return false;
  1000. +
  1001. + dev_err(ipts->dev, "Command 0x%08x failed: %d\n", rsp->code,
  1002. + rsp->status);
  1003. +
  1004. + if (ipts_receiver_sensor_was_reset(rsp->status)) {
  1005. + dev_err(ipts->dev, "Sensor was reset\n");
  1006. +
  1007. + if (ipts_control_restart(ipts))
  1008. + dev_err(ipts->dev, "Failed to restart IPTS\n");
  1009. + }
  1010. +
  1011. + return true;
  1012. +}
  1013. +
  1014. +static void ipts_receiver_handle_response(struct ipts_context *ipts,
  1015. + struct ipts_response *rsp)
  1016. +{
  1017. + int ret;
  1018. +
  1019. + if (ipts_receiver_handle_error(ipts, rsp))
  1020. + return;
  1021. +
  1022. + switch (rsp->code) {
  1023. + case IPTS_RSP_GET_DEVICE_INFO:
  1024. + ret = ipts_receiver_handle_get_device_info(ipts, rsp);
  1025. + break;
  1026. + case IPTS_RSP_SET_MODE:
  1027. + ret = ipts_receiver_handle_set_mode(ipts);
  1028. + break;
  1029. + case IPTS_RSP_SET_MEM_WINDOW:
  1030. + ret = ipts_receiver_handle_set_mem_window(ipts);
  1031. + break;
  1032. + case IPTS_RSP_FEEDBACK:
  1033. + ret = ipts_receiver_handle_feedback(ipts, rsp);
  1034. + break;
  1035. + case IPTS_RSP_CLEAR_MEM_WINDOW:
  1036. + ret = ipts_receiver_handle_clear_mem_window(ipts);
  1037. + break;
  1038. + default:
  1039. + ret = 0;
  1040. + break;
  1041. + }
  1042. +
  1043. + if (!ret)
  1044. + return;
  1045. +
  1046. + dev_err(ipts->dev, "Error while handling response 0x%08x: %d\n",
  1047. + rsp->code, ret);
  1048. +
  1049. + if (ipts_control_stop(ipts))
  1050. + dev_err(ipts->dev, "Failed to stop IPTS\n");
  1051. +}
  1052. +
  1053. +void ipts_receiver_callback(struct mei_cl_device *cldev)
  1054. +{
  1055. + int ret;
  1056. + struct ipts_response rsp;
  1057. + struct ipts_context *ipts;
  1058. +
  1059. + ipts = mei_cldev_get_drvdata(cldev);
  1060. +
  1061. + ret = mei_cldev_recv(cldev, (u8 *)&rsp, sizeof(struct ipts_response));
  1062. + if (ret <= 0) {
  1063. + dev_err(ipts->dev, "Error while reading response: %d\n", ret);
  1064. + return;
  1065. + }
  1066. +
  1067. + ipts_receiver_handle_response(ipts, &rsp);
  1068. +}
  1069. diff --git a/drivers/misc/ipts/receiver.h b/drivers/misc/ipts/receiver.h
  1070. new file mode 100644
  1071. index 000000000000..7f075afa7ef8
  1072. --- /dev/null
  1073. +++ b/drivers/misc/ipts/receiver.h
  1074. @@ -0,0 +1,16 @@
  1075. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  1076. +/*
  1077. + * Copyright (c) 2016 Intel Corporation
  1078. + * Copyright (c) 2020 Dorian Stoll
  1079. + *
  1080. + * Linux driver for Intel Precise Touch & Stylus
  1081. + */
  1082. +
  1083. +#ifndef _IPTS_RECEIVER_H_
  1084. +#define _IPTS_RECEIVER_H_
  1085. +
  1086. +#include <linux/mei_cl_bus.h>
  1087. +
  1088. +void ipts_receiver_callback(struct mei_cl_device *cldev);
  1089. +
  1090. +#endif /* _IPTS_RECEIVER_H_ */
  1091. diff --git a/drivers/misc/ipts/resources.c b/drivers/misc/ipts/resources.c
  1092. new file mode 100644
  1093. index 000000000000..8e3a2409e438
  1094. --- /dev/null
  1095. +++ b/drivers/misc/ipts/resources.c
  1096. @@ -0,0 +1,128 @@
  1097. +// SPDX-License-Identifier: GPL-2.0-or-later
  1098. +/*
  1099. + * Copyright (c) 2016 Intel Corporation
  1100. + * Copyright (c) 2020 Dorian Stoll
  1101. + *
  1102. + * Linux driver for Intel Precise Touch & Stylus
  1103. + */
  1104. +
  1105. +#include <linux/dma-mapping.h>
  1106. +
  1107. +#include "context.h"
  1108. +
  1109. +void ipts_resources_free(struct ipts_context *ipts)
  1110. +{
  1111. + int i;
  1112. + struct ipts_buffer_info *buffers;
  1113. +
  1114. + u32 data_buffer_size = ipts->device_info.data_size;
  1115. + u32 feedback_buffer_size = ipts->device_info.feedback_size;
  1116. +
  1117. + buffers = ipts->data;
  1118. + for (i = 0; i < IPTS_BUFFERS; i++) {
  1119. + if (!buffers[i].address)
  1120. + continue;
  1121. +
  1122. + dma_free_coherent(ipts->dev, data_buffer_size,
  1123. + buffers[i].address, buffers[i].dma_address);
  1124. +
  1125. + buffers[i].address = NULL;
  1126. + buffers[i].dma_address = 0;
  1127. + }
  1128. +
  1129. + buffers = ipts->feedback;
  1130. + for (i = 0; i < IPTS_BUFFERS; i++) {
  1131. + if (!buffers[i].address)
  1132. + continue;
  1133. +
  1134. + dma_free_coherent(ipts->dev, feedback_buffer_size,
  1135. + buffers[i].address, buffers[i].dma_address);
  1136. +
  1137. + buffers[i].address = NULL;
  1138. + buffers[i].dma_address = 0;
  1139. + }
  1140. +
  1141. + if (ipts->doorbell.address) {
  1142. + dma_free_coherent(ipts->dev, sizeof(u32),
  1143. + ipts->doorbell.address,
  1144. + ipts->doorbell.dma_address);
  1145. +
  1146. + ipts->doorbell.address = NULL;
  1147. + ipts->doorbell.dma_address = 0;
  1148. + }
  1149. +
  1150. + if (ipts->workqueue.address) {
  1151. + dma_free_coherent(ipts->dev, sizeof(u32),
  1152. + ipts->workqueue.address,
  1153. + ipts->workqueue.dma_address);
  1154. +
  1155. + ipts->workqueue.address = NULL;
  1156. + ipts->workqueue.dma_address = 0;
  1157. + }
  1158. +
  1159. + if (ipts->host2me.address) {
  1160. + dma_free_coherent(ipts->dev, feedback_buffer_size,
  1161. + ipts->host2me.address,
  1162. + ipts->host2me.dma_address);
  1163. +
  1164. + ipts->host2me.address = NULL;
  1165. + ipts->host2me.dma_address = 0;
  1166. + }
  1167. +}
  1168. +
  1169. +int ipts_resources_alloc(struct ipts_context *ipts)
  1170. +{
  1171. + int i;
  1172. + struct ipts_buffer_info *buffers;
  1173. +
  1174. + u32 data_buffer_size = ipts->device_info.data_size;
  1175. + u32 feedback_buffer_size = ipts->device_info.feedback_size;
  1176. +
  1177. + buffers = ipts->data;
  1178. + for (i = 0; i < IPTS_BUFFERS; i++) {
  1179. + buffers[i].address =
  1180. + dma_alloc_coherent(ipts->dev, data_buffer_size,
  1181. + &buffers[i].dma_address, GFP_KERNEL);
  1182. +
  1183. + if (!buffers[i].address)
  1184. + goto release_resources;
  1185. + }
  1186. +
  1187. + buffers = ipts->feedback;
  1188. + for (i = 0; i < IPTS_BUFFERS; i++) {
  1189. + buffers[i].address =
  1190. + dma_alloc_coherent(ipts->dev, feedback_buffer_size,
  1191. + &buffers[i].dma_address, GFP_KERNEL);
  1192. +
  1193. + if (!buffers[i].address)
  1194. + goto release_resources;
  1195. + }
  1196. +
  1197. + ipts->doorbell.address =
  1198. + dma_alloc_coherent(ipts->dev, sizeof(u32),
  1199. + &ipts->doorbell.dma_address, GFP_KERNEL);
  1200. +
  1201. + if (!ipts->doorbell.address)
  1202. + goto release_resources;
  1203. +
  1204. + ipts->workqueue.address =
  1205. + dma_alloc_coherent(ipts->dev, sizeof(u32),
  1206. + &ipts->workqueue.dma_address, GFP_KERNEL);
  1207. +
  1208. + if (!ipts->workqueue.address)
  1209. + goto release_resources;
  1210. +
  1211. + ipts->host2me.address =
  1212. + dma_alloc_coherent(ipts->dev, feedback_buffer_size,
  1213. + &ipts->host2me.dma_address, GFP_KERNEL);
  1214. +
  1215. + if (!ipts->workqueue.address)
  1216. + goto release_resources;
  1217. +
  1218. + return 0;
  1219. +
  1220. +release_resources:
  1221. +
  1222. + ipts_resources_free(ipts);
  1223. + return -ENOMEM;
  1224. +}
  1225. diff --git a/drivers/misc/ipts/resources.h b/drivers/misc/ipts/resources.h
  1226. new file mode 100644
  1227. index 000000000000..fdac0eee9156
  1228. --- /dev/null
  1229. +++ b/drivers/misc/ipts/resources.h
  1230. @@ -0,0 +1,17 @@
  1231. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  1232. +/*
  1233. + * Copyright (c) 2016 Intel Corporation
  1234. + * Copyright (c) 2020 Dorian Stoll
  1235. + *
  1236. + * Linux driver for Intel Precise Touch & Stylus
  1237. + */
  1238. +
  1239. +#ifndef _IPTS_RESOURCES_H_
  1240. +#define _IPTS_RESOURCES_H_
  1241. +
  1242. +#include "context.h"
  1243. +
  1244. +int ipts_resources_alloc(struct ipts_context *ipts);
  1245. +void ipts_resources_free(struct ipts_context *ipts);
  1246. +
  1247. +#endif /* _IPTS_RESOURCES_H_ */
  1248. diff --git a/drivers/misc/ipts/uapi.c b/drivers/misc/ipts/uapi.c
  1249. new file mode 100644
  1250. index 000000000000..598f0710ad64
  1251. --- /dev/null
  1252. +++ b/drivers/misc/ipts/uapi.c
  1253. @@ -0,0 +1,208 @@
  1254. +// SPDX-License-Identifier: GPL-2.0-or-later
  1255. +/*
  1256. + * Copyright (c) 2016 Intel Corporation
  1257. + * Copyright (c) 2020 Dorian Stoll
  1258. + *
  1259. + * Linux driver for Intel Precise Touch & Stylus
  1260. + */
  1261. +
  1262. +#include <linux/cdev.h>
  1263. +#include <linux/delay.h>
  1264. +#include <linux/device.h>
  1265. +#include <linux/fs.h>
  1266. +#include <linux/types.h>
  1267. +#include <linux/uaccess.h>
  1268. +
  1269. +#include "context.h"
  1270. +#include "control.h"
  1271. +#include "protocol.h"
  1272. +#include "uapi.h"
  1273. +
  1274. +struct ipts_uapi uapi;
  1275. +
  1276. +static ssize_t ipts_uapi_read(struct file *file, char __user *buf, size_t count,
  1277. + loff_t *offset)
  1278. +{
  1279. + int buffer;
  1280. + int maxbytes;
  1281. + struct ipts_context *ipts = uapi.ipts;
  1282. +
  1283. + buffer = MINOR(file->f_path.dentry->d_inode->i_rdev);
  1284. +
  1285. + if (!ipts || ipts->status != IPTS_HOST_STATUS_STARTED)
  1286. + return -ENODEV;
  1287. +
  1288. + maxbytes = ipts->device_info.data_size - *offset;
  1289. + if (maxbytes <= 0 || count > maxbytes)
  1290. + return -EINVAL;
  1291. +
  1292. + if (copy_to_user(buf, ipts->data[buffer].address + *offset, count))
  1293. + return -EFAULT;
  1294. +
  1295. + return count;
  1296. +}
  1297. +
  1298. +static long ipts_uapi_ioctl_get_device_ready(struct ipts_context *ipts,
  1299. + unsigned long arg)
  1300. +{
  1301. + void __user *buffer = (void __user *)arg;
  1302. + u8 ready = 0;
  1303. +
  1304. + if (ipts)
  1305. + ready = ipts->status == IPTS_HOST_STATUS_STARTED;
  1306. +
  1307. + if (copy_to_user(buffer, &ready, sizeof(u8)))
  1308. + return -EFAULT;
  1309. +
  1310. + return 0;
  1311. +}
  1312. +
  1313. +static long ipts_uapi_ioctl_get_device_info(struct ipts_context *ipts,
  1314. + unsigned long arg)
  1315. +{
  1316. + struct ipts_device_info info;
  1317. + void __user *buffer = (void __user *)arg;
  1318. +
  1319. + if (!ipts || ipts->status != IPTS_HOST_STATUS_STARTED)
  1320. + return -ENODEV;
  1321. +
  1322. + info.vendor = ipts->device_info.vendor_id;
  1323. + info.product = ipts->device_info.device_id;
  1324. + info.version = ipts->device_info.fw_rev;
  1325. + info.buffer_size = ipts->device_info.data_size;
  1326. + info.max_contacts = ipts->device_info.max_contacts;
  1327. +
  1328. + if (copy_to_user(buffer, &info, sizeof(struct ipts_device_info)))
  1329. + return -EFAULT;
  1330. +
  1331. + return 0;
  1332. +}
  1333. +
  1334. +static long ipts_uapi_ioctl_get_doorbell(struct ipts_context *ipts,
  1335. + unsigned long arg)
  1336. +{
  1337. + void __user *buffer = (void __user *)arg;
  1338. +
  1339. + if (!ipts || ipts->status != IPTS_HOST_STATUS_STARTED)
  1340. + return -ENODEV;
  1341. +
  1342. + if (copy_to_user(buffer, ipts->doorbell.address, sizeof(u32)))
  1343. + return -EFAULT;
  1344. +
  1345. + return 0;
  1346. +}
  1347. +
  1348. +static long ipts_uapi_ioctl_send_feedback(struct ipts_context *ipts,
  1349. + struct file *file)
  1350. +{
  1351. + int ret;
  1352. + u32 buffer;
  1353. +
  1354. + if (!ipts || ipts->status != IPTS_HOST_STATUS_STARTED)
  1355. + return -ENODEV;
  1356. +
  1357. + buffer = MINOR(file->f_path.dentry->d_inode->i_rdev);
  1358. +
  1359. + ret = ipts_control_send_feedback(ipts, buffer);
  1360. + if (ret)
  1361. + return -EFAULT;
  1362. +
  1363. + return 0;
  1364. +}
  1365. +
  1366. +static long ipts_uapi_ioctl_send_reset(struct ipts_context *ipts)
  1367. +{
  1368. + int ret;
  1369. + struct ipts_reset_sensor_cmd cmd;
  1370. +
  1371. + if (!ipts || ipts->status != IPTS_HOST_STATUS_STARTED)
  1372. + return -ENODEV;
  1373. +
  1374. + memset(&cmd, 0, sizeof(struct ipts_reset_sensor_cmd));
  1375. + cmd.type = IPTS_RESET_TYPE_SOFT;
  1376. +
  1377. + ret = ipts_control_send(ipts, IPTS_CMD_RESET_SENSOR, &cmd,
  1378. + sizeof(struct ipts_reset_sensor_cmd));
  1379. +
  1380. + if (ret)
  1381. + return -EFAULT;
  1382. +
  1383. + return 0;
  1384. +}
  1385. +
  1386. +static long ipts_uapi_ioctl(struct file *file, unsigned int cmd,
  1387. + unsigned long arg)
  1388. +{
  1389. + struct ipts_context *ipts = uapi.ipts;
  1390. +
  1391. + switch (cmd) {
  1392. + case IPTS_IOCTL_GET_DEVICE_READY:
  1393. + return ipts_uapi_ioctl_get_device_ready(ipts, arg);
  1394. + case IPTS_IOCTL_GET_DEVICE_INFO:
  1395. + return ipts_uapi_ioctl_get_device_info(ipts, arg);
  1396. + case IPTS_IOCTL_GET_DOORBELL:
  1397. + return ipts_uapi_ioctl_get_doorbell(ipts, arg);
  1398. + case IPTS_IOCTL_SEND_FEEDBACK:
  1399. + return ipts_uapi_ioctl_send_feedback(ipts, file);
  1400. + case IPTS_IOCTL_SEND_RESET:
  1401. + return ipts_uapi_ioctl_send_reset(ipts);
  1402. + default:
  1403. + return -ENOTTY;
  1404. + }
  1405. +}
  1406. +
  1407. +static const struct file_operations ipts_uapi_fops = {
  1408. + .owner = THIS_MODULE,
  1409. + .read = ipts_uapi_read,
  1410. + .unlocked_ioctl = ipts_uapi_ioctl,
  1411. +#ifdef CONFIG_COMPAT
  1412. + .compat_ioctl = ipts_uapi_ioctl,
  1413. +#endif
  1414. +};
  1415. +
  1416. +void ipts_uapi_link(struct ipts_context *ipts)
  1417. +{
  1418. + uapi.ipts = ipts;
  1419. +}
  1420. +
  1421. +void ipts_uapi_unlink(void)
  1422. +{
  1423. + uapi.ipts = NULL;
  1424. +}
  1425. +
  1426. +int ipts_uapi_init(void)
  1427. +{
  1428. + int i, major;
  1429. +
  1430. + alloc_chrdev_region(&uapi.dev, 0, IPTS_BUFFERS, "ipts");
  1431. + uapi.class = class_create(THIS_MODULE, "ipts");
  1432. +
  1433. + major = MAJOR(uapi.dev);
  1434. +
  1435. + cdev_init(&uapi.cdev, &ipts_uapi_fops);
  1436. + uapi.cdev.owner = THIS_MODULE;
  1437. + cdev_add(&uapi.cdev, MKDEV(major, 0), IPTS_BUFFERS);
  1438. +
  1439. + for (i = 0; i < IPTS_BUFFERS; i++) {
  1440. + device_create(uapi.class, NULL, MKDEV(major, i), NULL,
  1441. + "ipts/%d", i);
  1442. + }
  1443. +
  1444. + return 0;
  1445. +}
  1446. +
  1447. +void ipts_uapi_free(void)
  1448. +{
  1449. + int i;
  1450. + int major;
  1451. +
  1452. + major = MAJOR(uapi.dev);
  1453. +
  1454. + for (i = 0; i < IPTS_BUFFERS; i++)
  1455. + device_destroy(uapi.class, MKDEV(major, i));
  1456. +
  1457. + cdev_del(&uapi.cdev);
  1458. +
  1459. + unregister_chrdev_region(MKDEV(major, 0), MINORMASK);
  1460. + class_destroy(uapi.class);
  1461. +}
  1462. diff --git a/drivers/misc/ipts/uapi.h b/drivers/misc/ipts/uapi.h
  1463. new file mode 100644
  1464. index 000000000000..53fb86a88f97
  1465. --- /dev/null
  1466. +++ b/drivers/misc/ipts/uapi.h
  1467. @@ -0,0 +1,47 @@
  1468. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  1469. +/*
  1470. + * Copyright (c) 2016 Intel Corporation
  1471. + * Copyright (c) 2020 Dorian Stoll
  1472. + *
  1473. + * Linux driver for Intel Precise Touch & Stylus
  1474. + */
  1475. +
  1476. +#ifndef _IPTS_UAPI_H_
  1477. +#define _IPTS_UAPI_H_
  1478. +
  1479. +#include <linux/types.h>
  1480. +
  1481. +#include "context.h"
  1482. +
  1483. +struct ipts_uapi {
  1484. + dev_t dev;
  1485. + struct class *class;
  1486. + struct cdev cdev;
  1487. +
  1488. + struct ipts_context *ipts;
  1489. +};
  1490. +
  1491. +struct ipts_device_info {
  1492. + __u16 vendor;
  1493. + __u16 product;
  1494. + __u32 version;
  1495. + __u32 buffer_size;
  1496. + __u8 max_contacts;
  1497. +
  1498. + /* For future expansion */
  1499. + __u8 reserved[19];
  1500. +};
  1501. +
  1502. +#define IPTS_IOCTL_GET_DEVICE_READY _IOR(0x86, 0x01, __u8)
  1503. +#define IPTS_IOCTL_GET_DEVICE_INFO _IOR(0x86, 0x02, struct ipts_device_info)
  1504. +#define IPTS_IOCTL_GET_DOORBELL _IOR(0x86, 0x03, __u32)
  1505. +#define IPTS_IOCTL_SEND_FEEDBACK _IO(0x86, 0x04)
  1506. +#define IPTS_IOCTL_SEND_RESET _IO(0x86, 0x05)
  1507. +
  1508. +void ipts_uapi_link(struct ipts_context *ipts);
  1509. +void ipts_uapi_unlink(void);
  1510. +
  1511. +int ipts_uapi_init(void);
  1512. +void ipts_uapi_free(void);
  1513. +
  1514. +#endif /* _IPTS_UAPI_H_ */
  1515. --
  1516. 2.31.0