bus-fixup.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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_ANY NULL_UUID_LE
  32. /**
  33. * number_of_connections - determine whether an client be on the bus
  34. * according number of connections
  35. * We support only clients:
  36. * 1. with single connection
  37. * 2. and fixed clients (max_number_of_connections == 0)
  38. *
  39. * @cldev: me clients device
  40. */
  41. static void number_of_connections(struct mei_cl_device *cldev)
  42. {
  43. dev_dbg(&cldev->dev, "running hook %s on %pUl\n",
  44. __func__, mei_me_cl_uuid(cldev->me_cl));
  45. if (cldev->me_cl->props.max_number_of_connections > 1)
  46. cldev->do_match = 0;
  47. }
  48. /**
  49. * blacklist - blacklist a client from the bus
  50. *
  51. * @cldev: me clients device
  52. */
  53. static void blacklist(struct mei_cl_device *cldev)
  54. {
  55. dev_dbg(&cldev->dev, "running hook %s on %pUl\n",
  56. __func__, mei_me_cl_uuid(cldev->me_cl));
  57. cldev->do_match = 0;
  58. }
  59. struct mei_nfc_cmd {
  60. u8 command;
  61. u8 status;
  62. u16 req_id;
  63. u32 reserved;
  64. u16 data_size;
  65. u8 sub_command;
  66. u8 data[];
  67. } __packed;
  68. struct mei_nfc_reply {
  69. u8 command;
  70. u8 status;
  71. u16 req_id;
  72. u32 reserved;
  73. u16 data_size;
  74. u8 sub_command;
  75. u8 reply_status;
  76. u8 data[];
  77. } __packed;
  78. struct mei_nfc_if_version {
  79. u8 radio_version_sw[3];
  80. u8 reserved[3];
  81. u8 radio_version_hw[3];
  82. u8 i2c_addr;
  83. u8 fw_ivn;
  84. u8 vendor_id;
  85. u8 radio_type;
  86. } __packed;
  87. #define MEI_NFC_CMD_MAINTENANCE 0x00
  88. #define MEI_NFC_SUBCMD_IF_VERSION 0x01
  89. /* Vendors */
  90. #define MEI_NFC_VENDOR_INSIDE 0x00
  91. #define MEI_NFC_VENDOR_NXP 0x01
  92. /* Radio types */
  93. #define MEI_NFC_VENDOR_INSIDE_UREAD 0x00
  94. #define MEI_NFC_VENDOR_NXP_PN544 0x01
  95. /**
  96. * mei_nfc_if_version - get NFC interface version
  97. *
  98. * @cl: host client (nfc info)
  99. * @ver: NFC interface version to be filled in
  100. *
  101. * Return: 0 on success; < 0 otherwise
  102. */
  103. static int mei_nfc_if_version(struct mei_cl *cl,
  104. struct mei_nfc_if_version *ver)
  105. {
  106. struct mei_device *bus;
  107. struct mei_nfc_cmd cmd = {
  108. .command = MEI_NFC_CMD_MAINTENANCE,
  109. .data_size = 1,
  110. .sub_command = MEI_NFC_SUBCMD_IF_VERSION,
  111. };
  112. struct mei_nfc_reply *reply = NULL;
  113. size_t if_version_length;
  114. int bytes_recv, ret;
  115. bus = cl->dev;
  116. WARN_ON(mutex_is_locked(&bus->device_lock));
  117. ret = __mei_cl_send(cl, (u8 *)&cmd, sizeof(struct mei_nfc_cmd), 1);
  118. if (ret < 0) {
  119. dev_err(bus->dev, "Could not send IF version cmd\n");
  120. return ret;
  121. }
  122. /* to be sure on the stack we alloc memory */
  123. if_version_length = sizeof(struct mei_nfc_reply) +
  124. sizeof(struct mei_nfc_if_version);
  125. reply = kzalloc(if_version_length, GFP_KERNEL);
  126. if (!reply)
  127. return -ENOMEM;
  128. ret = 0;
  129. bytes_recv = __mei_cl_recv(cl, (u8 *)reply, if_version_length);
  130. if (bytes_recv < 0 || bytes_recv < if_version_length) {
  131. dev_err(bus->dev, "Could not read IF version\n");
  132. ret = -EIO;
  133. goto err;
  134. }
  135. memcpy(ver, reply->data, sizeof(struct mei_nfc_if_version));
  136. dev_info(bus->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n",
  137. ver->fw_ivn, ver->vendor_id, ver->radio_type);
  138. err:
  139. kfree(reply);
  140. return ret;
  141. }
  142. /**
  143. * mei_nfc_radio_name - derive nfc radio name from the interface version
  144. *
  145. * @ver: NFC radio version
  146. *
  147. * Return: radio name string
  148. */
  149. static const char *mei_nfc_radio_name(struct mei_nfc_if_version *ver)
  150. {
  151. if (ver->vendor_id == MEI_NFC_VENDOR_INSIDE) {
  152. if (ver->radio_type == MEI_NFC_VENDOR_INSIDE_UREAD)
  153. return "microread";
  154. }
  155. if (ver->vendor_id == MEI_NFC_VENDOR_NXP) {
  156. if (ver->radio_type == MEI_NFC_VENDOR_NXP_PN544)
  157. return "pn544";
  158. }
  159. return NULL;
  160. }
  161. /**
  162. * mei_nfc - The nfc fixup function. The function retrieves nfc radio
  163. * name and set is as device attribute so we can load
  164. * the proper device driver for it
  165. *
  166. * @cldev: me client device (nfc)
  167. */
  168. static void mei_nfc(struct mei_cl_device *cldev)
  169. {
  170. struct mei_device *bus;
  171. struct mei_cl *cl;
  172. struct mei_me_client *me_cl = NULL;
  173. struct mei_nfc_if_version ver;
  174. const char *radio_name = NULL;
  175. int ret;
  176. bus = cldev->bus;
  177. dev_dbg(bus->dev, "running hook %s: %pUl match=%d\n",
  178. __func__, mei_me_cl_uuid(cldev->me_cl), cldev->do_match);
  179. mutex_lock(&bus->device_lock);
  180. /* we need to connect to INFO GUID */
  181. cl = mei_cl_alloc_linked(bus, MEI_HOST_CLIENT_ID_ANY);
  182. if (IS_ERR(cl)) {
  183. ret = PTR_ERR(cl);
  184. cl = NULL;
  185. dev_err(bus->dev, "nfc hook alloc failed %d\n", ret);
  186. goto out;
  187. }
  188. me_cl = mei_me_cl_by_uuid(bus, &mei_nfc_info_guid);
  189. if (!me_cl) {
  190. ret = -ENOTTY;
  191. dev_err(bus->dev, "Cannot find nfc info %d\n", ret);
  192. goto out;
  193. }
  194. ret = mei_cl_connect(cl, me_cl, NULL);
  195. if (ret < 0) {
  196. dev_err(&cldev->dev, "Can't connect to the NFC INFO ME ret = %d\n",
  197. ret);
  198. goto out;
  199. }
  200. mutex_unlock(&bus->device_lock);
  201. ret = mei_nfc_if_version(cl, &ver);
  202. if (ret)
  203. goto disconnect;
  204. radio_name = mei_nfc_radio_name(&ver);
  205. if (!radio_name) {
  206. ret = -ENOENT;
  207. dev_err(&cldev->dev, "Can't get the NFC interface version ret = %d\n",
  208. ret);
  209. goto disconnect;
  210. }
  211. dev_dbg(bus->dev, "nfc radio %s\n", radio_name);
  212. strlcpy(cldev->name, radio_name, sizeof(cldev->name));
  213. disconnect:
  214. mutex_lock(&bus->device_lock);
  215. if (mei_cl_disconnect(cl) < 0)
  216. dev_err(bus->dev, "Can't disconnect the NFC INFO ME\n");
  217. mei_cl_flush_queues(cl, NULL);
  218. out:
  219. mei_cl_unlink(cl);
  220. mutex_unlock(&bus->device_lock);
  221. mei_me_cl_put(me_cl);
  222. kfree(cl);
  223. if (ret)
  224. cldev->do_match = 0;
  225. dev_dbg(bus->dev, "end of fixup match = %d\n", cldev->do_match);
  226. }
  227. #define MEI_FIXUP(_uuid, _hook) { _uuid, _hook }
  228. static struct mei_fixup {
  229. const uuid_le uuid;
  230. void (*hook)(struct mei_cl_device *cldev);
  231. } mei_fixups[] = {
  232. MEI_FIXUP(MEI_UUID_ANY, number_of_connections),
  233. MEI_FIXUP(MEI_UUID_NFC_INFO, blacklist),
  234. MEI_FIXUP(MEI_UUID_NFC_HCI, mei_nfc),
  235. };
  236. /**
  237. * mei_cldev_fixup - run fixup handlers
  238. *
  239. * @cldev: me client device
  240. */
  241. void mei_cl_bus_dev_fixup(struct mei_cl_device *cldev)
  242. {
  243. struct mei_fixup *f;
  244. const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
  245. int i;
  246. for (i = 0; i < ARRAY_SIZE(mei_fixups); i++) {
  247. f = &mei_fixups[i];
  248. if (uuid_le_cmp(f->uuid, MEI_UUID_ANY) == 0 ||
  249. uuid_le_cmp(f->uuid, *uuid) == 0)
  250. f->hook(cldev);
  251. }
  252. }