nfc.c 11 KB

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