mei_phy.c 3.8 KB

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