mei_phy.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #define MEI_NFC_MAX_READ (MEI_NFC_HEADER_SIZE + MEI_NFC_MAX_HCI_PAYLOAD)
  31. #define MEI_DUMP_SKB_IN(info, skb) \
  32. do { \
  33. pr_debug("%s:\n", info); \
  34. print_hex_dump_debug("mei in : ", DUMP_PREFIX_OFFSET, \
  35. 16, 1, (skb)->data, (skb)->len, false); \
  36. } while (0)
  37. #define MEI_DUMP_SKB_OUT(info, skb) \
  38. do { \
  39. pr_debug("%s:\n", info); \
  40. print_hex_dump_debug("mei out: ", DUMP_PREFIX_OFFSET, \
  41. 16, 1, (skb)->data, (skb)->len, false); \
  42. } while (0)
  43. int nfc_mei_phy_enable(void *phy_id)
  44. {
  45. int r;
  46. struct nfc_mei_phy *phy = phy_id;
  47. pr_info("%s\n", __func__);
  48. if (phy->powered == 1)
  49. return 0;
  50. r = mei_cl_enable_device(phy->device);
  51. if (r < 0) {
  52. pr_err("Could not enable device\n");
  53. return r;
  54. }
  55. r = mei_cl_register_event_cb(phy->device, nfc_mei_event_cb, phy);
  56. if (r) {
  57. pr_err("Event cb registration failed\n");
  58. mei_cl_disable_device(phy->device);
  59. phy->powered = 0;
  60. return r;
  61. }
  62. phy->powered = 1;
  63. return 0;
  64. }
  65. EXPORT_SYMBOL_GPL(nfc_mei_phy_enable);
  66. void nfc_mei_phy_disable(void *phy_id)
  67. {
  68. struct nfc_mei_phy *phy = phy_id;
  69. pr_info("%s\n", __func__);
  70. mei_cl_disable_device(phy->device);
  71. phy->powered = 0;
  72. }
  73. EXPORT_SYMBOL_GPL(nfc_mei_phy_disable);
  74. /*
  75. * Writing a frame must not return the number of written bytes.
  76. * It must return either zero for success, or <0 for error.
  77. * In addition, it must not alter the skb
  78. */
  79. static int nfc_mei_phy_write(void *phy_id, struct sk_buff *skb)
  80. {
  81. struct nfc_mei_phy *phy = phy_id;
  82. int r;
  83. MEI_DUMP_SKB_OUT("mei frame sent", skb);
  84. r = mei_cl_send(phy->device, skb->data, skb->len);
  85. if (r > 0)
  86. r = 0;
  87. return r;
  88. }
  89. void nfc_mei_event_cb(struct mei_cl_device *device, u32 events, void *context)
  90. {
  91. struct nfc_mei_phy *phy = context;
  92. if (phy->hard_fault != 0)
  93. return;
  94. if (events & BIT(MEI_CL_EVENT_RX)) {
  95. struct sk_buff *skb;
  96. int reply_size;
  97. skb = alloc_skb(MEI_NFC_MAX_READ, GFP_KERNEL);
  98. if (!skb)
  99. return;
  100. reply_size = mei_cl_recv(device, skb->data, MEI_NFC_MAX_READ);
  101. if (reply_size < MEI_NFC_HEADER_SIZE) {
  102. kfree_skb(skb);
  103. return;
  104. }
  105. skb_put(skb, reply_size);
  106. skb_pull(skb, MEI_NFC_HEADER_SIZE);
  107. MEI_DUMP_SKB_IN("mei frame read", skb);
  108. nfc_hci_recv_frame(phy->hdev, skb);
  109. }
  110. }
  111. EXPORT_SYMBOL_GPL(nfc_mei_event_cb);
  112. struct nfc_phy_ops mei_phy_ops = {
  113. .write = nfc_mei_phy_write,
  114. .enable = nfc_mei_phy_enable,
  115. .disable = nfc_mei_phy_disable,
  116. };
  117. EXPORT_SYMBOL_GPL(mei_phy_ops);
  118. struct nfc_mei_phy *nfc_mei_phy_alloc(struct mei_cl_device *device)
  119. {
  120. struct nfc_mei_phy *phy;
  121. phy = kzalloc(sizeof(struct nfc_mei_phy), GFP_KERNEL);
  122. if (!phy)
  123. return NULL;
  124. phy->device = device;
  125. mei_cl_set_drvdata(device, phy);
  126. return phy;
  127. }
  128. EXPORT_SYMBOL_GPL(nfc_mei_phy_alloc);
  129. void nfc_mei_phy_free(struct nfc_mei_phy *phy)
  130. {
  131. kfree(phy);
  132. }
  133. EXPORT_SYMBOL_GPL(nfc_mei_phy_free);
  134. MODULE_LICENSE("GPL");
  135. MODULE_DESCRIPTION("mei bus NFC device interface");