nfc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/device.h>
  20. #include <linux/pci.h>
  21. #include <linux/mei_cl_bus.h>
  22. #include "mei_dev.h"
  23. #include "client.h"
  24. struct mei_nfc_cmd {
  25. u8 command;
  26. u8 status;
  27. u16 req_id;
  28. u32 reserved;
  29. u16 data_size;
  30. u8 sub_command;
  31. u8 data[];
  32. } __packed;
  33. struct mei_nfc_reply {
  34. u8 command;
  35. u8 status;
  36. u16 req_id;
  37. u32 reserved;
  38. u16 data_size;
  39. u8 sub_command;
  40. u8 reply_status;
  41. u8 data[];
  42. } __packed;
  43. struct mei_nfc_if_version {
  44. u8 radio_version_sw[3];
  45. u8 reserved[3];
  46. u8 radio_version_hw[3];
  47. u8 i2c_addr;
  48. u8 fw_ivn;
  49. u8 vendor_id;
  50. u8 radio_type;
  51. } __packed;
  52. struct mei_nfc_connect {
  53. u8 fw_ivn;
  54. u8 vendor_id;
  55. } __packed;
  56. struct mei_nfc_connect_resp {
  57. u8 fw_ivn;
  58. u8 vendor_id;
  59. u16 me_major;
  60. u16 me_minor;
  61. u16 me_hotfix;
  62. u16 me_build;
  63. } __packed;
  64. struct mei_nfc_hci_hdr {
  65. u8 cmd;
  66. u8 status;
  67. u16 req_id;
  68. u32 reserved;
  69. u16 data_size;
  70. } __packed;
  71. #define MEI_NFC_CMD_MAINTENANCE 0x00
  72. #define MEI_NFC_CMD_HCI_SEND 0x01
  73. #define MEI_NFC_CMD_HCI_RECV 0x02
  74. #define MEI_NFC_SUBCMD_CONNECT 0x00
  75. #define MEI_NFC_SUBCMD_IF_VERSION 0x01
  76. #define MEI_NFC_HEADER_SIZE 10
  77. /** mei_nfc_dev - NFC mei device
  78. *
  79. * @cl: NFC host client
  80. * @cl_info: NFC info host client
  81. * @init_work: perform connection to the info client
  82. * @fw_ivn: NFC Intervace Version Number
  83. * @vendor_id: NFC manufacturer ID
  84. * @radio_type: NFC radio type
  85. */
  86. struct mei_nfc_dev {
  87. struct mei_cl *cl;
  88. struct mei_cl *cl_info;
  89. struct work_struct init_work;
  90. u8 fw_ivn;
  91. u8 vendor_id;
  92. u8 radio_type;
  93. char *bus_name;
  94. };
  95. static struct mei_nfc_dev nfc_dev;
  96. /* UUIDs for NFC F/W clients */
  97. const uuid_le mei_nfc_guid = UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50,
  98. 0x94, 0xd4, 0x50, 0x26,
  99. 0x67, 0x23, 0x77, 0x5c);
  100. static const uuid_le mei_nfc_info_guid = UUID_LE(0xd2de1625, 0x382d, 0x417d,
  101. 0x48, 0xa4, 0xef, 0xab,
  102. 0xba, 0x8a, 0x12, 0x06);
  103. /* Vendors */
  104. #define MEI_NFC_VENDOR_INSIDE 0x00
  105. #define MEI_NFC_VENDOR_NXP 0x01
  106. /* Radio types */
  107. #define MEI_NFC_VENDOR_INSIDE_UREAD 0x00
  108. #define MEI_NFC_VENDOR_NXP_PN544 0x01
  109. static void mei_nfc_free(struct mei_nfc_dev *ndev)
  110. {
  111. if (ndev->cl) {
  112. list_del(&ndev->cl->device_link);
  113. mei_cl_unlink(ndev->cl);
  114. kfree(ndev->cl);
  115. }
  116. if (ndev->cl_info) {
  117. list_del(&ndev->cl_info->device_link);
  118. mei_cl_unlink(ndev->cl_info);
  119. kfree(ndev->cl_info);
  120. }
  121. }
  122. static int mei_nfc_build_bus_name(struct mei_nfc_dev *ndev)
  123. {
  124. struct mei_device *dev;
  125. if (!ndev->cl)
  126. return -ENODEV;
  127. dev = ndev->cl->dev;
  128. switch (ndev->vendor_id) {
  129. case MEI_NFC_VENDOR_INSIDE:
  130. switch (ndev->radio_type) {
  131. case MEI_NFC_VENDOR_INSIDE_UREAD:
  132. ndev->bus_name = "microread";
  133. return 0;
  134. default:
  135. dev_err(&dev->pdev->dev, "Unknow radio type 0x%x\n",
  136. ndev->radio_type);
  137. return -EINVAL;
  138. }
  139. case MEI_NFC_VENDOR_NXP:
  140. switch (ndev->radio_type) {
  141. case MEI_NFC_VENDOR_NXP_PN544:
  142. ndev->bus_name = "pn544";
  143. return 0;
  144. default:
  145. dev_err(&dev->pdev->dev, "Unknow radio type 0x%x\n",
  146. ndev->radio_type);
  147. return -EINVAL;
  148. }
  149. default:
  150. dev_err(&dev->pdev->dev, "Unknow vendor ID 0x%x\n",
  151. ndev->vendor_id);
  152. return -EINVAL;
  153. }
  154. return 0;
  155. }
  156. static int mei_nfc_if_version(struct mei_nfc_dev *ndev)
  157. {
  158. struct mei_device *dev;
  159. struct mei_cl *cl;
  160. struct mei_nfc_cmd cmd;
  161. struct mei_nfc_reply *reply = NULL;
  162. struct mei_nfc_if_version *version;
  163. size_t if_version_length;
  164. int bytes_recv, ret;
  165. cl = ndev->cl_info;
  166. dev = cl->dev;
  167. memset(&cmd, 0, sizeof(struct mei_nfc_cmd));
  168. cmd.command = MEI_NFC_CMD_MAINTENANCE;
  169. cmd.data_size = 1;
  170. cmd.sub_command = MEI_NFC_SUBCMD_IF_VERSION;
  171. ret = __mei_cl_send(cl, (u8 *)&cmd, sizeof(struct mei_nfc_cmd));
  172. if (ret < 0) {
  173. dev_err(&dev->pdev->dev, "Could not send IF version cmd\n");
  174. return ret;
  175. }
  176. /* to be sure on the stack we alloc memory */
  177. if_version_length = sizeof(struct mei_nfc_reply) +
  178. sizeof(struct mei_nfc_if_version);
  179. reply = kzalloc(if_version_length, GFP_KERNEL);
  180. if (!reply)
  181. return -ENOMEM;
  182. bytes_recv = __mei_cl_recv(cl, (u8 *)reply, if_version_length);
  183. if (bytes_recv < 0 || bytes_recv < sizeof(struct mei_nfc_reply)) {
  184. dev_err(&dev->pdev->dev, "Could not read IF version\n");
  185. ret = -EIO;
  186. goto err;
  187. }
  188. version = (struct mei_nfc_if_version *)reply->data;
  189. ndev->fw_ivn = version->fw_ivn;
  190. ndev->vendor_id = version->vendor_id;
  191. ndev->radio_type = version->radio_type;
  192. err:
  193. kfree(reply);
  194. return ret;
  195. }
  196. static void mei_nfc_init(struct work_struct *work)
  197. {
  198. struct mei_device *dev;
  199. struct mei_cl_device *cldev;
  200. struct mei_nfc_dev *ndev;
  201. struct mei_cl *cl_info;
  202. ndev = container_of(work, struct mei_nfc_dev, init_work);
  203. cl_info = ndev->cl_info;
  204. dev = cl_info->dev;
  205. mutex_lock(&dev->device_lock);
  206. if (mei_cl_connect(cl_info, NULL) < 0) {
  207. mutex_unlock(&dev->device_lock);
  208. dev_err(&dev->pdev->dev,
  209. "Could not connect to the NFC INFO ME client");
  210. goto err;
  211. }
  212. mutex_unlock(&dev->device_lock);
  213. if (mei_nfc_if_version(ndev) < 0) {
  214. dev_err(&dev->pdev->dev, "Could not get the NFC interfave version");
  215. goto err;
  216. }
  217. dev_info(&dev->pdev->dev,
  218. "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n",
  219. ndev->fw_ivn, ndev->vendor_id, ndev->radio_type);
  220. mutex_lock(&dev->device_lock);
  221. if (mei_cl_disconnect(cl_info) < 0) {
  222. mutex_unlock(&dev->device_lock);
  223. dev_err(&dev->pdev->dev,
  224. "Could not disconnect the NFC INFO ME client");
  225. goto err;
  226. }
  227. mutex_unlock(&dev->device_lock);
  228. if (mei_nfc_build_bus_name(ndev) < 0) {
  229. dev_err(&dev->pdev->dev,
  230. "Could not build the bus ID name\n");
  231. return;
  232. }
  233. cldev = mei_cl_add_device(dev, mei_nfc_guid, ndev->bus_name, NULL);
  234. if (!cldev) {
  235. dev_err(&dev->pdev->dev,
  236. "Could not add the NFC device to the MEI bus\n");
  237. goto err;
  238. }
  239. cldev->priv_data = ndev;
  240. return;
  241. err:
  242. mei_nfc_free(ndev);
  243. return;
  244. }
  245. int mei_nfc_host_init(struct mei_device *dev)
  246. {
  247. struct mei_nfc_dev *ndev = &nfc_dev;
  248. struct mei_cl *cl_info, *cl = NULL;
  249. int i, ret;
  250. /* already initialzed */
  251. if (ndev->cl_info)
  252. return 0;
  253. cl_info = mei_cl_allocate(dev);
  254. cl = mei_cl_allocate(dev);
  255. if (!cl || !cl_info) {
  256. ret = -ENOMEM;
  257. goto err;
  258. }
  259. /* check for valid client id */
  260. i = mei_me_cl_by_uuid(dev, &mei_nfc_info_guid);
  261. if (i < 0) {
  262. dev_info(&dev->pdev->dev, "nfc: failed to find the client\n");
  263. ret = -ENOENT;
  264. goto err;
  265. }
  266. cl_info->me_client_id = dev->me_clients[i].client_id;
  267. ret = mei_cl_link(cl_info, MEI_HOST_CLIENT_ID_ANY);
  268. if (ret)
  269. goto err;
  270. cl_info->device_uuid = mei_nfc_info_guid;
  271. list_add_tail(&cl_info->device_link, &dev->device_list);
  272. /* check for valid client id */
  273. i = mei_me_cl_by_uuid(dev, &mei_nfc_guid);
  274. if (i < 0) {
  275. dev_info(&dev->pdev->dev, "nfc: failed to find the client\n");
  276. ret = -ENOENT;
  277. goto err;
  278. }
  279. cl->me_client_id = dev->me_clients[i].client_id;
  280. ret = mei_cl_link(cl, MEI_HOST_CLIENT_ID_ANY);
  281. if (ret)
  282. goto err;
  283. cl->device_uuid = mei_nfc_guid;
  284. list_add_tail(&cl->device_link, &dev->device_list);
  285. ndev->cl_info = cl_info;
  286. ndev->cl = cl;
  287. INIT_WORK(&ndev->init_work, mei_nfc_init);
  288. schedule_work(&ndev->init_work);
  289. return 0;
  290. err:
  291. mei_nfc_free(ndev);
  292. return ret;
  293. }
  294. void mei_nfc_host_exit(void)
  295. {
  296. struct mei_nfc_dev *ndev = &nfc_dev;
  297. if (ndev->cl && ndev->cl->device)
  298. mei_cl_remove_device(ndev->cl->device);
  299. mei_nfc_free(ndev);
  300. }