nfc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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/pci.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. /** mei_nfc_dev - NFC mei device
  79. *
  80. * @cl: NFC host client
  81. * @cl_info: NFC info host client
  82. * @init_work: perform connection to the info client
  83. * @fw_ivn: NFC Intervace Version Number
  84. * @vendor_id: NFC manufacturer ID
  85. * @radio_type: NFC radio type
  86. */
  87. struct mei_nfc_dev {
  88. struct mei_cl *cl;
  89. struct mei_cl *cl_info;
  90. struct work_struct init_work;
  91. wait_queue_head_t send_wq;
  92. u8 fw_ivn;
  93. u8 vendor_id;
  94. u8 radio_type;
  95. char *bus_name;
  96. u16 req_id;
  97. u16 recv_req_id;
  98. };
  99. static struct mei_nfc_dev nfc_dev;
  100. /* UUIDs for NFC F/W clients */
  101. const uuid_le mei_nfc_guid = UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50,
  102. 0x94, 0xd4, 0x50, 0x26,
  103. 0x67, 0x23, 0x77, 0x5c);
  104. static const uuid_le mei_nfc_info_guid = UUID_LE(0xd2de1625, 0x382d, 0x417d,
  105. 0x48, 0xa4, 0xef, 0xab,
  106. 0xba, 0x8a, 0x12, 0x06);
  107. /* Vendors */
  108. #define MEI_NFC_VENDOR_INSIDE 0x00
  109. #define MEI_NFC_VENDOR_NXP 0x01
  110. /* Radio types */
  111. #define MEI_NFC_VENDOR_INSIDE_UREAD 0x00
  112. #define MEI_NFC_VENDOR_NXP_PN544 0x01
  113. static void mei_nfc_free(struct mei_nfc_dev *ndev)
  114. {
  115. if (ndev->cl) {
  116. list_del(&ndev->cl->device_link);
  117. mei_cl_unlink(ndev->cl);
  118. kfree(ndev->cl);
  119. }
  120. if (ndev->cl_info) {
  121. list_del(&ndev->cl_info->device_link);
  122. mei_cl_unlink(ndev->cl_info);
  123. kfree(ndev->cl_info);
  124. }
  125. }
  126. static int mei_nfc_build_bus_name(struct mei_nfc_dev *ndev)
  127. {
  128. struct mei_device *dev;
  129. if (!ndev->cl)
  130. return -ENODEV;
  131. dev = ndev->cl->dev;
  132. switch (ndev->vendor_id) {
  133. case MEI_NFC_VENDOR_INSIDE:
  134. switch (ndev->radio_type) {
  135. case MEI_NFC_VENDOR_INSIDE_UREAD:
  136. ndev->bus_name = "microread";
  137. return 0;
  138. default:
  139. dev_err(&dev->pdev->dev, "Unknow radio type 0x%x\n",
  140. ndev->radio_type);
  141. return -EINVAL;
  142. }
  143. case MEI_NFC_VENDOR_NXP:
  144. switch (ndev->radio_type) {
  145. case MEI_NFC_VENDOR_NXP_PN544:
  146. ndev->bus_name = "pn544";
  147. return 0;
  148. default:
  149. dev_err(&dev->pdev->dev, "Unknow radio type 0x%x\n",
  150. ndev->radio_type);
  151. return -EINVAL;
  152. }
  153. default:
  154. dev_err(&dev->pdev->dev, "Unknow vendor ID 0x%x\n",
  155. ndev->vendor_id);
  156. return -EINVAL;
  157. }
  158. return 0;
  159. }
  160. static int mei_nfc_connect(struct mei_nfc_dev *ndev)
  161. {
  162. struct mei_device *dev;
  163. struct mei_cl *cl;
  164. struct mei_nfc_cmd *cmd, *reply;
  165. struct mei_nfc_connect *connect;
  166. struct mei_nfc_connect_resp *connect_resp;
  167. size_t connect_length, connect_resp_length;
  168. int bytes_recv, ret;
  169. cl = ndev->cl;
  170. dev = cl->dev;
  171. connect_length = sizeof(struct mei_nfc_cmd) +
  172. sizeof(struct mei_nfc_connect);
  173. connect_resp_length = sizeof(struct mei_nfc_cmd) +
  174. sizeof(struct mei_nfc_connect_resp);
  175. cmd = kzalloc(connect_length, GFP_KERNEL);
  176. if (!cmd)
  177. return -ENOMEM;
  178. connect = (struct mei_nfc_connect *)cmd->data;
  179. reply = kzalloc(connect_resp_length, GFP_KERNEL);
  180. if (!reply) {
  181. kfree(cmd);
  182. return -ENOMEM;
  183. }
  184. connect_resp = (struct mei_nfc_connect_resp *)reply->data;
  185. cmd->command = MEI_NFC_CMD_MAINTENANCE;
  186. cmd->data_size = 3;
  187. cmd->sub_command = MEI_NFC_SUBCMD_CONNECT;
  188. connect->fw_ivn = ndev->fw_ivn;
  189. connect->vendor_id = ndev->vendor_id;
  190. ret = __mei_cl_send(cl, (u8 *)cmd, connect_length);
  191. if (ret < 0) {
  192. dev_err(&dev->pdev->dev, "Could not send connect cmd\n");
  193. goto err;
  194. }
  195. bytes_recv = __mei_cl_recv(cl, (u8 *)reply, connect_resp_length);
  196. if (bytes_recv < 0) {
  197. dev_err(&dev->pdev->dev, "Could not read connect response\n");
  198. ret = bytes_recv;
  199. goto err;
  200. }
  201. dev_info(&dev->pdev->dev, "IVN 0x%x Vendor ID 0x%x\n",
  202. connect_resp->fw_ivn, connect_resp->vendor_id);
  203. dev_info(&dev->pdev->dev, "ME FW %d.%d.%d.%d\n",
  204. connect_resp->me_major, connect_resp->me_minor,
  205. connect_resp->me_hotfix, connect_resp->me_build);
  206. ret = 0;
  207. err:
  208. kfree(reply);
  209. kfree(cmd);
  210. return ret;
  211. }
  212. static int mei_nfc_if_version(struct mei_nfc_dev *ndev)
  213. {
  214. struct mei_device *dev;
  215. struct mei_cl *cl;
  216. struct mei_nfc_cmd cmd;
  217. struct mei_nfc_reply *reply = NULL;
  218. struct mei_nfc_if_version *version;
  219. size_t if_version_length;
  220. int bytes_recv, ret;
  221. cl = ndev->cl_info;
  222. dev = cl->dev;
  223. memset(&cmd, 0, sizeof(struct mei_nfc_cmd));
  224. cmd.command = MEI_NFC_CMD_MAINTENANCE;
  225. cmd.data_size = 1;
  226. cmd.sub_command = MEI_NFC_SUBCMD_IF_VERSION;
  227. ret = __mei_cl_send(cl, (u8 *)&cmd, sizeof(struct mei_nfc_cmd));
  228. if (ret < 0) {
  229. dev_err(&dev->pdev->dev, "Could not send IF version cmd\n");
  230. return ret;
  231. }
  232. /* to be sure on the stack we alloc memory */
  233. if_version_length = sizeof(struct mei_nfc_reply) +
  234. sizeof(struct mei_nfc_if_version);
  235. reply = kzalloc(if_version_length, GFP_KERNEL);
  236. if (!reply)
  237. return -ENOMEM;
  238. bytes_recv = __mei_cl_recv(cl, (u8 *)reply, if_version_length);
  239. if (bytes_recv < 0 || bytes_recv < sizeof(struct mei_nfc_reply)) {
  240. dev_err(&dev->pdev->dev, "Could not read IF version\n");
  241. ret = -EIO;
  242. goto err;
  243. }
  244. version = (struct mei_nfc_if_version *)reply->data;
  245. ndev->fw_ivn = version->fw_ivn;
  246. ndev->vendor_id = version->vendor_id;
  247. ndev->radio_type = version->radio_type;
  248. err:
  249. kfree(reply);
  250. return ret;
  251. }
  252. static int mei_nfc_enable(struct mei_cl_device *cldev)
  253. {
  254. struct mei_device *dev;
  255. struct mei_nfc_dev *ndev = &nfc_dev;
  256. int ret;
  257. dev = ndev->cl->dev;
  258. ret = mei_nfc_connect(ndev);
  259. if (ret < 0) {
  260. dev_err(&dev->pdev->dev, "Could not connect to NFC");
  261. return ret;
  262. }
  263. return 0;
  264. }
  265. static int mei_nfc_disable(struct mei_cl_device *cldev)
  266. {
  267. return 0;
  268. }
  269. static int mei_nfc_send(struct mei_cl_device *cldev, u8 *buf, size_t length)
  270. {
  271. struct mei_device *dev;
  272. struct mei_nfc_dev *ndev;
  273. struct mei_nfc_hci_hdr *hdr;
  274. u8 *mei_buf;
  275. int err;
  276. ndev = (struct mei_nfc_dev *) cldev->priv_data;
  277. dev = ndev->cl->dev;
  278. mei_buf = kzalloc(length + MEI_NFC_HEADER_SIZE, GFP_KERNEL);
  279. if (!mei_buf)
  280. return -ENOMEM;
  281. hdr = (struct mei_nfc_hci_hdr *) mei_buf;
  282. hdr->cmd = MEI_NFC_CMD_HCI_SEND;
  283. hdr->status = 0;
  284. hdr->req_id = ndev->req_id;
  285. hdr->reserved = 0;
  286. hdr->data_size = length;
  287. memcpy(mei_buf + MEI_NFC_HEADER_SIZE, buf, length);
  288. err = __mei_cl_send(ndev->cl, mei_buf, length + MEI_NFC_HEADER_SIZE);
  289. if (err < 0)
  290. return err;
  291. kfree(mei_buf);
  292. if (!wait_event_interruptible_timeout(ndev->send_wq,
  293. ndev->recv_req_id == ndev->req_id, HZ)) {
  294. dev_err(&dev->pdev->dev, "NFC MEI command timeout\n");
  295. err = -ETIMEDOUT;
  296. } else {
  297. ndev->req_id++;
  298. }
  299. return err;
  300. }
  301. static int mei_nfc_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
  302. {
  303. struct mei_nfc_dev *ndev;
  304. struct mei_nfc_hci_hdr *hci_hdr;
  305. int received_length;
  306. ndev = (struct mei_nfc_dev *)cldev->priv_data;
  307. received_length = __mei_cl_recv(ndev->cl, buf, length);
  308. if (received_length < 0)
  309. return received_length;
  310. hci_hdr = (struct mei_nfc_hci_hdr *) buf;
  311. if (hci_hdr->cmd == MEI_NFC_CMD_HCI_SEND) {
  312. ndev->recv_req_id = hci_hdr->req_id;
  313. wake_up(&ndev->send_wq);
  314. return 0;
  315. }
  316. return received_length;
  317. }
  318. static struct mei_cl_ops nfc_ops = {
  319. .enable = mei_nfc_enable,
  320. .disable = mei_nfc_disable,
  321. .send = mei_nfc_send,
  322. .recv = mei_nfc_recv,
  323. };
  324. static void mei_nfc_init(struct work_struct *work)
  325. {
  326. struct mei_device *dev;
  327. struct mei_cl_device *cldev;
  328. struct mei_nfc_dev *ndev;
  329. struct mei_cl *cl_info;
  330. ndev = container_of(work, struct mei_nfc_dev, init_work);
  331. cl_info = ndev->cl_info;
  332. dev = cl_info->dev;
  333. mutex_lock(&dev->device_lock);
  334. if (mei_cl_connect(cl_info, NULL) < 0) {
  335. mutex_unlock(&dev->device_lock);
  336. dev_err(&dev->pdev->dev,
  337. "Could not connect to the NFC INFO ME client");
  338. goto err;
  339. }
  340. mutex_unlock(&dev->device_lock);
  341. if (mei_nfc_if_version(ndev) < 0) {
  342. dev_err(&dev->pdev->dev, "Could not get the NFC interfave version");
  343. goto err;
  344. }
  345. dev_info(&dev->pdev->dev,
  346. "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n",
  347. ndev->fw_ivn, ndev->vendor_id, ndev->radio_type);
  348. mutex_lock(&dev->device_lock);
  349. if (mei_cl_disconnect(cl_info) < 0) {
  350. mutex_unlock(&dev->device_lock);
  351. dev_err(&dev->pdev->dev,
  352. "Could not disconnect the NFC INFO ME client");
  353. goto err;
  354. }
  355. mutex_unlock(&dev->device_lock);
  356. if (mei_nfc_build_bus_name(ndev) < 0) {
  357. dev_err(&dev->pdev->dev,
  358. "Could not build the bus ID name\n");
  359. return;
  360. }
  361. cldev = mei_cl_add_device(dev, mei_nfc_guid, ndev->bus_name, &nfc_ops);
  362. if (!cldev) {
  363. dev_err(&dev->pdev->dev,
  364. "Could not add the NFC device to the MEI bus\n");
  365. goto err;
  366. }
  367. cldev->priv_data = ndev;
  368. return;
  369. err:
  370. mei_nfc_free(ndev);
  371. return;
  372. }
  373. int mei_nfc_host_init(struct mei_device *dev)
  374. {
  375. struct mei_nfc_dev *ndev = &nfc_dev;
  376. struct mei_cl *cl_info, *cl = NULL;
  377. int i, ret;
  378. /* already initialzed */
  379. if (ndev->cl_info)
  380. return 0;
  381. cl_info = mei_cl_allocate(dev);
  382. cl = mei_cl_allocate(dev);
  383. if (!cl || !cl_info) {
  384. ret = -ENOMEM;
  385. goto err;
  386. }
  387. /* check for valid client id */
  388. i = mei_me_cl_by_uuid(dev, &mei_nfc_info_guid);
  389. if (i < 0) {
  390. dev_info(&dev->pdev->dev, "nfc: failed to find the client\n");
  391. ret = -ENOENT;
  392. goto err;
  393. }
  394. cl_info->me_client_id = dev->me_clients[i].client_id;
  395. ret = mei_cl_link(cl_info, MEI_HOST_CLIENT_ID_ANY);
  396. if (ret)
  397. goto err;
  398. cl_info->device_uuid = mei_nfc_info_guid;
  399. list_add_tail(&cl_info->device_link, &dev->device_list);
  400. /* check for valid client id */
  401. i = mei_me_cl_by_uuid(dev, &mei_nfc_guid);
  402. if (i < 0) {
  403. dev_info(&dev->pdev->dev, "nfc: failed to find the client\n");
  404. ret = -ENOENT;
  405. goto err;
  406. }
  407. cl->me_client_id = dev->me_clients[i].client_id;
  408. ret = mei_cl_link(cl, MEI_HOST_CLIENT_ID_ANY);
  409. if (ret)
  410. goto err;
  411. cl->device_uuid = mei_nfc_guid;
  412. list_add_tail(&cl->device_link, &dev->device_list);
  413. ndev->cl_info = cl_info;
  414. ndev->cl = cl;
  415. ndev->req_id = 1;
  416. INIT_WORK(&ndev->init_work, mei_nfc_init);
  417. init_waitqueue_head(&ndev->send_wq);
  418. schedule_work(&ndev->init_work);
  419. return 0;
  420. err:
  421. mei_nfc_free(ndev);
  422. return ret;
  423. }
  424. void mei_nfc_host_exit(void)
  425. {
  426. struct mei_nfc_dev *ndev = &nfc_dev;
  427. if (ndev->cl && ndev->cl->device)
  428. mei_cl_remove_device(ndev->cl->device);
  429. mei_nfc_free(ndev);
  430. }