mei_phy.c 3.7 KB

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