nfc.c 11 KB

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