nfc.c 8.3 KB

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