bus-fixup.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. *
  3. * Intel Management Engine Interface (Intel MEI) Linux driver
  4. * Copyright (c) 2003-2013, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/device.h>
  21. #include <linux/slab.h>
  22. #include <linux/uuid.h>
  23. #include <linux/mei_cl_bus.h>
  24. #include "mei_dev.h"
  25. #include "client.h"
  26. #define MEI_UUID_NFC_INFO UUID_LE(0xd2de1625, 0x382d, 0x417d, \
  27. 0x48, 0xa4, 0xef, 0xab, 0xba, 0x8a, 0x12, 0x06)
  28. static const uuid_le mei_nfc_info_guid = MEI_UUID_NFC_INFO;
  29. #define MEI_UUID_NFC_HCI UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50, \
  30. 0x94, 0xd4, 0x50, 0x26, 0x67, 0x23, 0x77, 0x5c)
  31. #define MEI_UUID_WD UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, \
  32. 0x89, 0x9D, 0xA9, 0x15, 0x14, 0xCB, 0x32, 0xAB)
  33. #define MEI_UUID_ANY NULL_UUID_LE
  34. /**
  35. * number_of_connections - determine whether an client be on the bus
  36. * according number of connections
  37. * We support only clients:
  38. * 1. with single connection
  39. * 2. and fixed clients (max_number_of_connections == 0)
  40. *
  41. * @cldev: me clients device
  42. */
  43. static void number_of_connections(struct mei_cl_device *cldev)
  44. {
  45. dev_dbg(&cldev->dev, "running hook %s\n", __func__);
  46. if (cldev->me_cl->props.max_number_of_connections > 1)
  47. cldev->do_match = 0;
  48. }
  49. /**
  50. * blacklist - blacklist a client from the bus
  51. *
  52. * @cldev: me clients device
  53. */
  54. static void blacklist(struct mei_cl_device *cldev)
  55. {
  56. dev_dbg(&cldev->dev, "running hook %s\n", __func__);
  57. cldev->do_match = 0;
  58. }
  59. /**
  60. * mei_wd - wd client on the bus, change protocol version
  61. * as the API has changed.
  62. *
  63. * @cldev: me clients device
  64. */
  65. #if IS_ENABLED(CONFIG_INTEL_MEI_ME)
  66. #include <linux/pci.h>
  67. #include "hw-me-regs.h"
  68. static void mei_wd(struct mei_cl_device *cldev)
  69. {
  70. struct pci_dev *pdev = to_pci_dev(cldev->dev.parent);
  71. dev_dbg(&cldev->dev, "running hook %s\n", __func__);
  72. if (pdev->device == MEI_DEV_ID_WPT_LP ||
  73. pdev->device == MEI_DEV_ID_SPT ||
  74. pdev->device == MEI_DEV_ID_SPT_H)
  75. cldev->me_cl->props.protocol_version = 0x2;
  76. cldev->do_match = 1;
  77. }
  78. #else
  79. static inline void mei_wd(struct mei_cl_device *cldev) {}
  80. #endif /* CONFIG_INTEL_MEI_ME */
  81. struct mei_nfc_cmd {
  82. u8 command;
  83. u8 status;
  84. u16 req_id;
  85. u32 reserved;
  86. u16 data_size;
  87. u8 sub_command;
  88. u8 data[];
  89. } __packed;
  90. struct mei_nfc_reply {
  91. u8 command;
  92. u8 status;
  93. u16 req_id;
  94. u32 reserved;
  95. u16 data_size;
  96. u8 sub_command;
  97. u8 reply_status;
  98. u8 data[];
  99. } __packed;
  100. struct mei_nfc_if_version {
  101. u8 radio_version_sw[3];
  102. u8 reserved[3];
  103. u8 radio_version_hw[3];
  104. u8 i2c_addr;
  105. u8 fw_ivn;
  106. u8 vendor_id;
  107. u8 radio_type;
  108. } __packed;
  109. #define MEI_NFC_CMD_MAINTENANCE 0x00
  110. #define MEI_NFC_SUBCMD_IF_VERSION 0x01
  111. /* Vendors */
  112. #define MEI_NFC_VENDOR_INSIDE 0x00
  113. #define MEI_NFC_VENDOR_NXP 0x01
  114. /* Radio types */
  115. #define MEI_NFC_VENDOR_INSIDE_UREAD 0x00
  116. #define MEI_NFC_VENDOR_NXP_PN544 0x01
  117. /**
  118. * mei_nfc_if_version - get NFC interface version
  119. *
  120. * @cl: host client (nfc info)
  121. * @ver: NFC interface version to be filled in
  122. *
  123. * Return: 0 on success; < 0 otherwise
  124. */
  125. static int mei_nfc_if_version(struct mei_cl *cl,
  126. struct mei_nfc_if_version *ver)
  127. {
  128. struct mei_device *bus;
  129. struct mei_nfc_cmd cmd = {
  130. .command = MEI_NFC_CMD_MAINTENANCE,
  131. .data_size = 1,
  132. .sub_command = MEI_NFC_SUBCMD_IF_VERSION,
  133. };
  134. struct mei_nfc_reply *reply = NULL;
  135. size_t if_version_length;
  136. int bytes_recv, ret;
  137. bus = cl->dev;
  138. WARN_ON(mutex_is_locked(&bus->device_lock));
  139. ret = __mei_cl_send(cl, (u8 *)&cmd, sizeof(struct mei_nfc_cmd), 1);
  140. if (ret < 0) {
  141. dev_err(bus->dev, "Could not send IF version cmd\n");
  142. return ret;
  143. }
  144. /* to be sure on the stack we alloc memory */
  145. if_version_length = sizeof(struct mei_nfc_reply) +
  146. sizeof(struct mei_nfc_if_version);
  147. reply = kzalloc(if_version_length, GFP_KERNEL);
  148. if (!reply)
  149. return -ENOMEM;
  150. ret = 0;
  151. bytes_recv = __mei_cl_recv(cl, (u8 *)reply, if_version_length);
  152. if (bytes_recv < if_version_length) {
  153. dev_err(bus->dev, "Could not read IF version\n");
  154. ret = -EIO;
  155. goto err;
  156. }
  157. memcpy(ver, reply->data, sizeof(struct mei_nfc_if_version));
  158. dev_info(bus->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n",
  159. ver->fw_ivn, ver->vendor_id, ver->radio_type);
  160. err:
  161. kfree(reply);
  162. return ret;
  163. }
  164. /**
  165. * mei_nfc_radio_name - derive nfc radio name from the interface version
  166. *
  167. * @ver: NFC radio version
  168. *
  169. * Return: radio name string
  170. */
  171. static const char *mei_nfc_radio_name(struct mei_nfc_if_version *ver)
  172. {
  173. if (ver->vendor_id == MEI_NFC_VENDOR_INSIDE) {
  174. if (ver->radio_type == MEI_NFC_VENDOR_INSIDE_UREAD)
  175. return "microread";
  176. }
  177. if (ver->vendor_id == MEI_NFC_VENDOR_NXP) {
  178. if (ver->radio_type == MEI_NFC_VENDOR_NXP_PN544)
  179. return "pn544";
  180. }
  181. return NULL;
  182. }
  183. /**
  184. * mei_nfc - The nfc fixup function. The function retrieves nfc radio
  185. * name and set is as device attribute so we can load
  186. * the proper device driver for it
  187. *
  188. * @cldev: me client device (nfc)
  189. */
  190. static void mei_nfc(struct mei_cl_device *cldev)
  191. {
  192. struct mei_device *bus;
  193. struct mei_cl *cl;
  194. struct mei_me_client *me_cl = NULL;
  195. struct mei_nfc_if_version ver;
  196. const char *radio_name = NULL;
  197. int ret;
  198. bus = cldev->bus;
  199. dev_dbg(&cldev->dev, "running hook %s\n", __func__);
  200. mutex_lock(&bus->device_lock);
  201. /* we need to connect to INFO GUID */
  202. cl = mei_cl_alloc_linked(bus);
  203. if (IS_ERR(cl)) {
  204. ret = PTR_ERR(cl);
  205. cl = NULL;
  206. dev_err(bus->dev, "nfc hook alloc failed %d\n", ret);
  207. goto out;
  208. }
  209. me_cl = mei_me_cl_by_uuid(bus, &mei_nfc_info_guid);
  210. if (!me_cl) {
  211. ret = -ENOTTY;
  212. dev_err(bus->dev, "Cannot find nfc info %d\n", ret);
  213. goto out;
  214. }
  215. ret = mei_cl_connect(cl, me_cl, NULL);
  216. if (ret < 0) {
  217. dev_err(&cldev->dev, "Can't connect to the NFC INFO ME ret = %d\n",
  218. ret);
  219. goto out;
  220. }
  221. mutex_unlock(&bus->device_lock);
  222. ret = mei_nfc_if_version(cl, &ver);
  223. if (ret)
  224. goto disconnect;
  225. radio_name = mei_nfc_radio_name(&ver);
  226. if (!radio_name) {
  227. ret = -ENOENT;
  228. dev_err(&cldev->dev, "Can't get the NFC interface version ret = %d\n",
  229. ret);
  230. goto disconnect;
  231. }
  232. dev_dbg(bus->dev, "nfc radio %s\n", radio_name);
  233. strlcpy(cldev->name, radio_name, sizeof(cldev->name));
  234. disconnect:
  235. mutex_lock(&bus->device_lock);
  236. if (mei_cl_disconnect(cl) < 0)
  237. dev_err(bus->dev, "Can't disconnect the NFC INFO ME\n");
  238. mei_cl_flush_queues(cl, NULL);
  239. out:
  240. mei_cl_unlink(cl);
  241. mutex_unlock(&bus->device_lock);
  242. mei_me_cl_put(me_cl);
  243. kfree(cl);
  244. if (ret)
  245. cldev->do_match = 0;
  246. dev_dbg(bus->dev, "end of fixup match = %d\n", cldev->do_match);
  247. }
  248. #define MEI_FIXUP(_uuid, _hook) { _uuid, _hook }
  249. static struct mei_fixup {
  250. const uuid_le uuid;
  251. void (*hook)(struct mei_cl_device *cldev);
  252. } mei_fixups[] = {
  253. MEI_FIXUP(MEI_UUID_ANY, number_of_connections),
  254. MEI_FIXUP(MEI_UUID_NFC_INFO, blacklist),
  255. MEI_FIXUP(MEI_UUID_NFC_HCI, mei_nfc),
  256. MEI_FIXUP(MEI_UUID_WD, mei_wd),
  257. };
  258. /**
  259. * mei_cldev_fixup - run fixup handlers
  260. *
  261. * @cldev: me client device
  262. */
  263. void mei_cl_bus_dev_fixup(struct mei_cl_device *cldev)
  264. {
  265. struct mei_fixup *f;
  266. const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
  267. int i;
  268. for (i = 0; i < ARRAY_SIZE(mei_fixups); i++) {
  269. f = &mei_fixups[i];
  270. if (uuid_le_cmp(f->uuid, MEI_UUID_ANY) == 0 ||
  271. uuid_le_cmp(f->uuid, *uuid) == 0)
  272. f->hook(cldev);
  273. }
  274. }