nfc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. };
  94. static struct mei_nfc_dev nfc_dev;
  95. /* UUIDs for NFC F/W clients */
  96. const uuid_le mei_nfc_guid = UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50,
  97. 0x94, 0xd4, 0x50, 0x26,
  98. 0x67, 0x23, 0x77, 0x5c);
  99. static const uuid_le mei_nfc_info_guid = UUID_LE(0xd2de1625, 0x382d, 0x417d,
  100. 0x48, 0xa4, 0xef, 0xab,
  101. 0xba, 0x8a, 0x12, 0x06);
  102. static void mei_nfc_free(struct mei_nfc_dev *ndev)
  103. {
  104. if (ndev->cl) {
  105. list_del(&ndev->cl->device_link);
  106. mei_cl_unlink(ndev->cl);
  107. kfree(ndev->cl);
  108. }
  109. if (ndev->cl_info) {
  110. list_del(&ndev->cl_info->device_link);
  111. mei_cl_unlink(ndev->cl_info);
  112. kfree(ndev->cl_info);
  113. }
  114. }
  115. static int mei_nfc_if_version(struct mei_nfc_dev *ndev)
  116. {
  117. struct mei_device *dev;
  118. struct mei_cl *cl;
  119. struct mei_nfc_cmd cmd;
  120. struct mei_nfc_reply *reply = NULL;
  121. struct mei_nfc_if_version *version;
  122. size_t if_version_length;
  123. int bytes_recv, ret;
  124. cl = ndev->cl_info;
  125. dev = cl->dev;
  126. memset(&cmd, 0, sizeof(struct mei_nfc_cmd));
  127. cmd.command = MEI_NFC_CMD_MAINTENANCE;
  128. cmd.data_size = 1;
  129. cmd.sub_command = MEI_NFC_SUBCMD_IF_VERSION;
  130. ret = __mei_cl_send(cl, (u8 *)&cmd, sizeof(struct mei_nfc_cmd));
  131. if (ret < 0) {
  132. dev_err(&dev->pdev->dev, "Could not send IF version cmd\n");
  133. return ret;
  134. }
  135. /* to be sure on the stack we alloc memory */
  136. if_version_length = sizeof(struct mei_nfc_reply) +
  137. sizeof(struct mei_nfc_if_version);
  138. reply = kzalloc(if_version_length, GFP_KERNEL);
  139. if (!reply)
  140. return -ENOMEM;
  141. bytes_recv = __mei_cl_recv(cl, (u8 *)reply, if_version_length);
  142. if (bytes_recv < 0 || bytes_recv < sizeof(struct mei_nfc_reply)) {
  143. dev_err(&dev->pdev->dev, "Could not read IF version\n");
  144. ret = -EIO;
  145. goto err;
  146. }
  147. version = (struct mei_nfc_if_version *)reply->data;
  148. ndev->fw_ivn = version->fw_ivn;
  149. ndev->vendor_id = version->vendor_id;
  150. ndev->radio_type = version->radio_type;
  151. err:
  152. kfree(reply);
  153. return ret;
  154. }
  155. static void mei_nfc_init(struct work_struct *work)
  156. {
  157. struct mei_device *dev;
  158. struct mei_nfc_dev *ndev;
  159. struct mei_cl *cl_info;
  160. ndev = container_of(work, struct mei_nfc_dev, init_work);
  161. cl_info = ndev->cl_info;
  162. dev = cl_info->dev;
  163. mutex_lock(&dev->device_lock);
  164. if (mei_cl_connect(cl_info, NULL) < 0) {
  165. mutex_unlock(&dev->device_lock);
  166. dev_err(&dev->pdev->dev,
  167. "Could not connect to the NFC INFO ME client");
  168. goto err;
  169. }
  170. mutex_unlock(&dev->device_lock);
  171. if (mei_nfc_if_version(ndev) < 0) {
  172. dev_err(&dev->pdev->dev, "Could not get the NFC interfave version");
  173. goto err;
  174. }
  175. dev_info(&dev->pdev->dev,
  176. "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n",
  177. ndev->fw_ivn, ndev->vendor_id, ndev->radio_type);
  178. mutex_lock(&dev->device_lock);
  179. if (mei_cl_disconnect(cl_info) < 0) {
  180. mutex_unlock(&dev->device_lock);
  181. dev_err(&dev->pdev->dev,
  182. "Could not disconnect the NFC INFO ME client");
  183. goto err;
  184. }
  185. mutex_unlock(&dev->device_lock);
  186. return;
  187. err:
  188. mei_nfc_free(ndev);
  189. return;
  190. }
  191. int mei_nfc_host_init(struct mei_device *dev)
  192. {
  193. struct mei_nfc_dev *ndev = &nfc_dev;
  194. struct mei_cl *cl_info, *cl = NULL;
  195. int i, ret;
  196. /* already initialzed */
  197. if (ndev->cl_info)
  198. return 0;
  199. cl_info = mei_cl_allocate(dev);
  200. cl = mei_cl_allocate(dev);
  201. if (!cl || !cl_info) {
  202. ret = -ENOMEM;
  203. goto err;
  204. }
  205. /* check for valid client id */
  206. i = mei_me_cl_by_uuid(dev, &mei_nfc_info_guid);
  207. if (i < 0) {
  208. dev_info(&dev->pdev->dev, "nfc: failed to find the client\n");
  209. ret = -ENOENT;
  210. goto err;
  211. }
  212. cl_info->me_client_id = dev->me_clients[i].client_id;
  213. ret = mei_cl_link(cl_info, MEI_HOST_CLIENT_ID_ANY);
  214. if (ret)
  215. goto err;
  216. cl_info->device_uuid = mei_nfc_info_guid;
  217. list_add_tail(&cl_info->device_link, &dev->device_list);
  218. /* check for valid client id */
  219. i = mei_me_cl_by_uuid(dev, &mei_nfc_guid);
  220. if (i < 0) {
  221. dev_info(&dev->pdev->dev, "nfc: failed to find the client\n");
  222. ret = -ENOENT;
  223. goto err;
  224. }
  225. cl->me_client_id = dev->me_clients[i].client_id;
  226. ret = mei_cl_link(cl, MEI_HOST_CLIENT_ID_ANY);
  227. if (ret)
  228. goto err;
  229. cl->device_uuid = mei_nfc_guid;
  230. list_add_tail(&cl->device_link, &dev->device_list);
  231. ndev->cl_info = cl_info;
  232. ndev->cl = cl;
  233. INIT_WORK(&ndev->init_work, mei_nfc_init);
  234. schedule_work(&ndev->init_work);
  235. return 0;
  236. err:
  237. mei_nfc_free(ndev);
  238. return ret;
  239. }
  240. void mei_nfc_host_exit(void)
  241. {
  242. struct mei_nfc_dev *ndev = &nfc_dev;
  243. mei_nfc_free(ndev);
  244. }