mei_phy.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * MEI Library for mei bus nfc device access
  3. *
  4. * Copyright (C) 2013 Intel Corporation. All rights reserved.
  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 that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/nfc.h>
  22. #include "mei_phy.h"
  23. struct mei_nfc_hdr {
  24. u8 cmd;
  25. u8 status;
  26. u16 req_id;
  27. u32 reserved;
  28. u16 data_size;
  29. } __packed;
  30. struct mei_nfc_cmd {
  31. struct mei_nfc_hdr hdr;
  32. u8 sub_command;
  33. u8 data[];
  34. } __packed;
  35. struct mei_nfc_reply {
  36. struct mei_nfc_hdr hdr;
  37. u8 sub_command;
  38. u8 reply_status;
  39. u8 data[];
  40. } __packed;
  41. struct mei_nfc_if_version {
  42. u8 radio_version_sw[3];
  43. u8 reserved[3];
  44. u8 radio_version_hw[3];
  45. u8 i2c_addr;
  46. u8 fw_ivn;
  47. u8 vendor_id;
  48. u8 radio_type;
  49. } __packed;
  50. struct mei_nfc_connect {
  51. u8 fw_ivn;
  52. u8 vendor_id;
  53. } __packed;
  54. struct mei_nfc_connect_resp {
  55. u8 fw_ivn;
  56. u8 vendor_id;
  57. u16 me_major;
  58. u16 me_minor;
  59. u16 me_hotfix;
  60. u16 me_build;
  61. } __packed;
  62. #define MEI_NFC_CMD_MAINTENANCE 0x00
  63. #define MEI_NFC_CMD_HCI_SEND 0x01
  64. #define MEI_NFC_CMD_HCI_RECV 0x02
  65. #define MEI_NFC_SUBCMD_CONNECT 0x00
  66. #define MEI_NFC_SUBCMD_IF_VERSION 0x01
  67. #define MEI_NFC_MAX_READ (MEI_NFC_HEADER_SIZE + MEI_NFC_MAX_HCI_PAYLOAD)
  68. #define MEI_DUMP_SKB_IN(info, skb) \
  69. do { \
  70. pr_debug("%s:\n", info); \
  71. print_hex_dump_debug("mei in : ", DUMP_PREFIX_OFFSET, \
  72. 16, 1, (skb)->data, (skb)->len, false); \
  73. } while (0)
  74. #define MEI_DUMP_SKB_OUT(info, skb) \
  75. do { \
  76. pr_debug("%s:\n", info); \
  77. print_hex_dump_debug("mei out: ", DUMP_PREFIX_OFFSET, \
  78. 16, 1, (skb)->data, (skb)->len, false); \
  79. } while (0)
  80. #define MEI_DUMP_NFC_HDR(info, _hdr) \
  81. do { \
  82. pr_debug("%s:\n", info); \
  83. pr_debug("cmd=%02d status=%d req_id=%d rsvd=%d size=%d\n", \
  84. (_hdr)->cmd, (_hdr)->status, (_hdr)->req_id, \
  85. (_hdr)->reserved, (_hdr)->data_size); \
  86. } while (0)
  87. static int mei_nfc_if_version(struct nfc_mei_phy *phy)
  88. {
  89. struct mei_nfc_cmd cmd;
  90. struct mei_nfc_reply *reply = NULL;
  91. struct mei_nfc_if_version *version;
  92. size_t if_version_length;
  93. int bytes_recv, r;
  94. pr_info("%s\n", __func__);
  95. memset(&cmd, 0, sizeof(struct mei_nfc_cmd));
  96. cmd.hdr.cmd = MEI_NFC_CMD_MAINTENANCE;
  97. cmd.hdr.data_size = 1;
  98. cmd.sub_command = MEI_NFC_SUBCMD_IF_VERSION;
  99. MEI_DUMP_NFC_HDR("version", &cmd.hdr);
  100. r = mei_cldev_send(phy->cldev, (u8 *)&cmd, sizeof(struct mei_nfc_cmd));
  101. if (r < 0) {
  102. pr_err("Could not send IF version cmd\n");
  103. return r;
  104. }
  105. /* to be sure on the stack we alloc memory */
  106. if_version_length = sizeof(struct mei_nfc_reply) +
  107. sizeof(struct mei_nfc_if_version);
  108. reply = kzalloc(if_version_length, GFP_KERNEL);
  109. if (!reply)
  110. return -ENOMEM;
  111. bytes_recv = mei_cldev_recv(phy->cldev, (u8 *)reply, if_version_length);
  112. if (bytes_recv < 0 || bytes_recv < if_version_length) {
  113. pr_err("Could not read IF version\n");
  114. r = -EIO;
  115. goto err;
  116. }
  117. version = (struct mei_nfc_if_version *)reply->data;
  118. phy->fw_ivn = version->fw_ivn;
  119. phy->vendor_id = version->vendor_id;
  120. phy->radio_type = version->radio_type;
  121. err:
  122. kfree(reply);
  123. return r;
  124. }
  125. static int mei_nfc_connect(struct nfc_mei_phy *phy)
  126. {
  127. struct mei_nfc_cmd *cmd, *reply;
  128. struct mei_nfc_connect *connect;
  129. struct mei_nfc_connect_resp *connect_resp;
  130. size_t connect_length, connect_resp_length;
  131. int bytes_recv, r;
  132. pr_info("%s\n", __func__);
  133. connect_length = sizeof(struct mei_nfc_cmd) +
  134. sizeof(struct mei_nfc_connect);
  135. connect_resp_length = sizeof(struct mei_nfc_cmd) +
  136. sizeof(struct mei_nfc_connect_resp);
  137. cmd = kzalloc(connect_length, GFP_KERNEL);
  138. if (!cmd)
  139. return -ENOMEM;
  140. connect = (struct mei_nfc_connect *)cmd->data;
  141. reply = kzalloc(connect_resp_length, GFP_KERNEL);
  142. if (!reply) {
  143. kfree(cmd);
  144. return -ENOMEM;
  145. }
  146. connect_resp = (struct mei_nfc_connect_resp *)reply->data;
  147. cmd->hdr.cmd = MEI_NFC_CMD_MAINTENANCE;
  148. cmd->hdr.data_size = 3;
  149. cmd->sub_command = MEI_NFC_SUBCMD_CONNECT;
  150. connect->fw_ivn = phy->fw_ivn;
  151. connect->vendor_id = phy->vendor_id;
  152. MEI_DUMP_NFC_HDR("connect request", &cmd->hdr);
  153. r = mei_cldev_send(phy->cldev, (u8 *)cmd, connect_length);
  154. if (r < 0) {
  155. pr_err("Could not send connect cmd %d\n", r);
  156. goto err;
  157. }
  158. bytes_recv = mei_cldev_recv(phy->cldev, (u8 *)reply,
  159. connect_resp_length);
  160. if (bytes_recv < 0) {
  161. r = bytes_recv;
  162. pr_err("Could not read connect response %d\n", r);
  163. goto err;
  164. }
  165. MEI_DUMP_NFC_HDR("connect reply", &reply->hdr);
  166. pr_info("IVN 0x%x Vendor ID 0x%x\n",
  167. connect_resp->fw_ivn, connect_resp->vendor_id);
  168. pr_info("ME FW %d.%d.%d.%d\n",
  169. connect_resp->me_major, connect_resp->me_minor,
  170. connect_resp->me_hotfix, connect_resp->me_build);
  171. r = 0;
  172. err:
  173. kfree(reply);
  174. kfree(cmd);
  175. return r;
  176. }
  177. static int mei_nfc_send(struct nfc_mei_phy *phy, u8 *buf, size_t length)
  178. {
  179. struct mei_nfc_hdr *hdr;
  180. u8 *mei_buf;
  181. int err;
  182. err = -ENOMEM;
  183. mei_buf = kzalloc(length + MEI_NFC_HEADER_SIZE, GFP_KERNEL);
  184. if (!mei_buf)
  185. goto out;
  186. hdr = (struct mei_nfc_hdr *)mei_buf;
  187. hdr->cmd = MEI_NFC_CMD_HCI_SEND;
  188. hdr->status = 0;
  189. hdr->req_id = phy->req_id;
  190. hdr->reserved = 0;
  191. hdr->data_size = length;
  192. MEI_DUMP_NFC_HDR("send", hdr);
  193. memcpy(mei_buf + MEI_NFC_HEADER_SIZE, buf, length);
  194. err = mei_cldev_send(phy->cldev, mei_buf, length + MEI_NFC_HEADER_SIZE);
  195. if (err < 0)
  196. goto out;
  197. if (!wait_event_interruptible_timeout(phy->send_wq,
  198. phy->recv_req_id == phy->req_id, HZ)) {
  199. pr_err("NFC MEI command timeout\n");
  200. err = -ETIME;
  201. } else {
  202. phy->req_id++;
  203. }
  204. out:
  205. kfree(mei_buf);
  206. return err;
  207. }
  208. /*
  209. * Writing a frame must not return the number of written bytes.
  210. * It must return either zero for success, or <0 for error.
  211. * In addition, it must not alter the skb
  212. */
  213. static int nfc_mei_phy_write(void *phy_id, struct sk_buff *skb)
  214. {
  215. struct nfc_mei_phy *phy = phy_id;
  216. int r;
  217. MEI_DUMP_SKB_OUT("mei frame sent", skb);
  218. r = mei_nfc_send(phy, skb->data, skb->len);
  219. if (r > 0)
  220. r = 0;
  221. return r;
  222. }
  223. static int mei_nfc_recv(struct nfc_mei_phy *phy, u8 *buf, size_t length)
  224. {
  225. struct mei_nfc_hdr *hdr;
  226. int received_length;
  227. received_length = mei_cldev_recv(phy->cldev, buf, length);
  228. if (received_length < 0)
  229. return received_length;
  230. hdr = (struct mei_nfc_hdr *) buf;
  231. MEI_DUMP_NFC_HDR("receive", hdr);
  232. if (hdr->cmd == MEI_NFC_CMD_HCI_SEND) {
  233. phy->recv_req_id = hdr->req_id;
  234. wake_up(&phy->send_wq);
  235. return 0;
  236. }
  237. return received_length;
  238. }
  239. static void nfc_mei_event_cb(struct mei_cl_device *cldev, u32 events,
  240. void *context)
  241. {
  242. struct nfc_mei_phy *phy = context;
  243. if (phy->hard_fault != 0)
  244. return;
  245. if (events & BIT(MEI_CL_EVENT_RX)) {
  246. struct sk_buff *skb;
  247. int reply_size;
  248. skb = alloc_skb(MEI_NFC_MAX_READ, GFP_KERNEL);
  249. if (!skb)
  250. return;
  251. reply_size = mei_nfc_recv(phy, skb->data, MEI_NFC_MAX_READ);
  252. if (reply_size < MEI_NFC_HEADER_SIZE) {
  253. kfree_skb(skb);
  254. return;
  255. }
  256. skb_put(skb, reply_size);
  257. skb_pull(skb, MEI_NFC_HEADER_SIZE);
  258. MEI_DUMP_SKB_IN("mei frame read", skb);
  259. nfc_hci_recv_frame(phy->hdev, skb);
  260. }
  261. }
  262. static int nfc_mei_phy_enable(void *phy_id)
  263. {
  264. int r;
  265. struct nfc_mei_phy *phy = phy_id;
  266. pr_info("%s\n", __func__);
  267. if (phy->powered == 1)
  268. return 0;
  269. r = mei_cldev_enable(phy->cldev);
  270. if (r < 0) {
  271. pr_err("Could not enable device %d\n", r);
  272. return r;
  273. }
  274. r = mei_nfc_if_version(phy);
  275. if (r < 0) {
  276. pr_err("Could not enable device %d\n", r);
  277. goto err;
  278. }
  279. r = mei_nfc_connect(phy);
  280. if (r < 0) {
  281. pr_err("Could not connect to device %d\n", r);
  282. goto err;
  283. }
  284. r = mei_cldev_register_event_cb(phy->cldev, BIT(MEI_CL_EVENT_RX),
  285. nfc_mei_event_cb, phy);
  286. if (r) {
  287. pr_err("Event cb registration failed %d\n", r);
  288. goto err;
  289. }
  290. phy->powered = 1;
  291. return 0;
  292. err:
  293. phy->powered = 0;
  294. mei_cldev_disable(phy->cldev);
  295. return r;
  296. }
  297. static void nfc_mei_phy_disable(void *phy_id)
  298. {
  299. struct nfc_mei_phy *phy = phy_id;
  300. pr_info("%s\n", __func__);
  301. mei_cldev_disable(phy->cldev);
  302. phy->powered = 0;
  303. }
  304. struct nfc_phy_ops mei_phy_ops = {
  305. .write = nfc_mei_phy_write,
  306. .enable = nfc_mei_phy_enable,
  307. .disable = nfc_mei_phy_disable,
  308. };
  309. EXPORT_SYMBOL_GPL(mei_phy_ops);
  310. struct nfc_mei_phy *nfc_mei_phy_alloc(struct mei_cl_device *cldev)
  311. {
  312. struct nfc_mei_phy *phy;
  313. phy = kzalloc(sizeof(struct nfc_mei_phy), GFP_KERNEL);
  314. if (!phy)
  315. return NULL;
  316. phy->cldev = cldev;
  317. init_waitqueue_head(&phy->send_wq);
  318. mei_cldev_set_drvdata(cldev, phy);
  319. return phy;
  320. }
  321. EXPORT_SYMBOL_GPL(nfc_mei_phy_alloc);
  322. void nfc_mei_phy_free(struct nfc_mei_phy *phy)
  323. {
  324. mei_cldev_disable(phy->cldev);
  325. kfree(phy);
  326. }
  327. EXPORT_SYMBOL_GPL(nfc_mei_phy_free);
  328. MODULE_LICENSE("GPL");
  329. MODULE_DESCRIPTION("mei bus NFC device interface");