mei_phy.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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_cl_send(phy->device, (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_cl_recv(phy->device, (u8 *)reply, if_version_length);
  112. if (bytes_recv < 0 || bytes_recv < sizeof(struct mei_nfc_reply)) {
  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_cl_send(phy->device, (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_cl_recv(phy->device, (u8 *)reply, connect_resp_length);
  159. if (bytes_recv < 0) {
  160. r = bytes_recv;
  161. pr_err("Could not read connect response %d\n", r);
  162. goto err;
  163. }
  164. MEI_DUMP_NFC_HDR("connect reply", &reply->hdr);
  165. pr_info("IVN 0x%x Vendor ID 0x%x\n",
  166. connect_resp->fw_ivn, connect_resp->vendor_id);
  167. pr_info("ME FW %d.%d.%d.%d\n",
  168. connect_resp->me_major, connect_resp->me_minor,
  169. connect_resp->me_hotfix, connect_resp->me_build);
  170. r = 0;
  171. err:
  172. kfree(reply);
  173. kfree(cmd);
  174. return r;
  175. }
  176. static int mei_nfc_send(struct nfc_mei_phy *phy, u8 *buf, size_t length)
  177. {
  178. struct mei_nfc_hdr *hdr;
  179. u8 *mei_buf;
  180. int err;
  181. err = -ENOMEM;
  182. mei_buf = kzalloc(length + MEI_NFC_HEADER_SIZE, GFP_KERNEL);
  183. if (!mei_buf)
  184. goto out;
  185. hdr = (struct mei_nfc_hdr *)mei_buf;
  186. hdr->cmd = MEI_NFC_CMD_HCI_SEND;
  187. hdr->status = 0;
  188. hdr->req_id = phy->req_id;
  189. hdr->reserved = 0;
  190. hdr->data_size = length;
  191. MEI_DUMP_NFC_HDR("send", hdr);
  192. memcpy(mei_buf + MEI_NFC_HEADER_SIZE, buf, length);
  193. err = mei_cl_send(phy->device, mei_buf, length + MEI_NFC_HEADER_SIZE);
  194. if (err < 0)
  195. goto out;
  196. if (!wait_event_interruptible_timeout(phy->send_wq,
  197. phy->recv_req_id == phy->req_id, HZ)) {
  198. pr_err("NFC MEI command timeout\n");
  199. err = -ETIME;
  200. } else {
  201. phy->req_id++;
  202. }
  203. out:
  204. kfree(mei_buf);
  205. return err;
  206. }
  207. /*
  208. * Writing a frame must not return the number of written bytes.
  209. * It must return either zero for success, or <0 for error.
  210. * In addition, it must not alter the skb
  211. */
  212. static int nfc_mei_phy_write(void *phy_id, struct sk_buff *skb)
  213. {
  214. struct nfc_mei_phy *phy = phy_id;
  215. int r;
  216. MEI_DUMP_SKB_OUT("mei frame sent", skb);
  217. r = mei_nfc_send(phy, skb->data, skb->len);
  218. if (r > 0)
  219. r = 0;
  220. return r;
  221. }
  222. static int mei_nfc_recv(struct nfc_mei_phy *phy, u8 *buf, size_t length)
  223. {
  224. struct mei_nfc_hdr *hdr;
  225. int received_length;
  226. received_length = mei_cl_recv(phy->device, buf, length);
  227. if (received_length < 0)
  228. return received_length;
  229. hdr = (struct mei_nfc_hdr *) buf;
  230. MEI_DUMP_NFC_HDR("receive", hdr);
  231. if (hdr->cmd == MEI_NFC_CMD_HCI_SEND) {
  232. phy->recv_req_id = hdr->req_id;
  233. wake_up(&phy->send_wq);
  234. return 0;
  235. }
  236. return received_length;
  237. }
  238. static void nfc_mei_event_cb(struct mei_cl_device *device, u32 events,
  239. void *context)
  240. {
  241. struct nfc_mei_phy *phy = context;
  242. if (phy->hard_fault != 0)
  243. return;
  244. if (events & BIT(MEI_CL_EVENT_RX)) {
  245. struct sk_buff *skb;
  246. int reply_size;
  247. skb = alloc_skb(MEI_NFC_MAX_READ, GFP_KERNEL);
  248. if (!skb)
  249. return;
  250. reply_size = mei_nfc_recv(phy, skb->data, MEI_NFC_MAX_READ);
  251. if (reply_size < MEI_NFC_HEADER_SIZE) {
  252. kfree_skb(skb);
  253. return;
  254. }
  255. skb_put(skb, reply_size);
  256. skb_pull(skb, MEI_NFC_HEADER_SIZE);
  257. MEI_DUMP_SKB_IN("mei frame read", skb);
  258. nfc_hci_recv_frame(phy->hdev, skb);
  259. }
  260. }
  261. static int nfc_mei_phy_enable(void *phy_id)
  262. {
  263. int r;
  264. struct nfc_mei_phy *phy = phy_id;
  265. pr_info("%s\n", __func__);
  266. if (phy->powered == 1)
  267. return 0;
  268. r = mei_cl_enable_device(phy->device);
  269. if (r < 0) {
  270. pr_err("Could not enable device %d\n", r);
  271. return r;
  272. }
  273. r = mei_nfc_if_version(phy);
  274. if (r < 0) {
  275. pr_err("Could not enable device %d\n", r);
  276. goto err;
  277. }
  278. r = mei_nfc_connect(phy);
  279. if (r < 0) {
  280. pr_err("Could not connect to device %d\n", r);
  281. goto err;
  282. }
  283. r = mei_cl_register_event_cb(phy->device, nfc_mei_event_cb, phy);
  284. if (r) {
  285. pr_err("Event cb registration failed %d\n", r);
  286. goto err;
  287. }
  288. phy->powered = 1;
  289. return 0;
  290. err:
  291. phy->powered = 0;
  292. mei_cl_disable_device(phy->device);
  293. return r;
  294. }
  295. static void nfc_mei_phy_disable(void *phy_id)
  296. {
  297. struct nfc_mei_phy *phy = phy_id;
  298. pr_info("%s\n", __func__);
  299. mei_cl_disable_device(phy->device);
  300. phy->powered = 0;
  301. }
  302. struct nfc_phy_ops mei_phy_ops = {
  303. .write = nfc_mei_phy_write,
  304. .enable = nfc_mei_phy_enable,
  305. .disable = nfc_mei_phy_disable,
  306. };
  307. EXPORT_SYMBOL_GPL(mei_phy_ops);
  308. struct nfc_mei_phy *nfc_mei_phy_alloc(struct mei_cl_device *device)
  309. {
  310. struct nfc_mei_phy *phy;
  311. phy = kzalloc(sizeof(struct nfc_mei_phy), GFP_KERNEL);
  312. if (!phy)
  313. return NULL;
  314. phy->device = device;
  315. init_waitqueue_head(&phy->send_wq);
  316. mei_cl_set_drvdata(device, phy);
  317. return phy;
  318. }
  319. EXPORT_SYMBOL_GPL(nfc_mei_phy_alloc);
  320. void nfc_mei_phy_free(struct nfc_mei_phy *phy)
  321. {
  322. mei_cl_disable_device(phy->device);
  323. kfree(phy);
  324. }
  325. EXPORT_SYMBOL_GPL(nfc_mei_phy_free);
  326. MODULE_LICENSE("GPL");
  327. MODULE_DESCRIPTION("mei bus NFC device interface");