mei.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * HCI based Driver for Inside Secure microread NFC Chip
  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/interrupt.h>
  23. #include <linux/gpio.h>
  24. #include <linux/mei_bus.h>
  25. #include <linux/nfc.h>
  26. #include <net/nfc/hci.h>
  27. #include <net/nfc/llc.h>
  28. #include "microread.h"
  29. #define MICROREAD_DRIVER_NAME "microread"
  30. #define MICROREAD_UUID UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50, 0x94, \
  31. 0xd4, 0x50, 0x26, 0x67, 0x23, 0x77, 0x5c)
  32. struct mei_nfc_hdr {
  33. u8 cmd;
  34. u8 status;
  35. u16 req_id;
  36. u32 reserved;
  37. u16 data_size;
  38. } __attribute__((packed));
  39. #define MEI_NFC_HEADER_SIZE 10
  40. #define MEI_NFC_MAX_HCI_PAYLOAD 300
  41. #define MEI_NFC_MAX_READ (MEI_NFC_HEADER_SIZE + MEI_NFC_MAX_HCI_PAYLOAD)
  42. struct microread_mei_phy {
  43. struct mei_bus_client *client;
  44. struct nfc_hci_dev *hdev;
  45. int powered;
  46. int hard_fault; /*
  47. * < 0 if hardware error occured (e.g. i2c err)
  48. * and prevents normal operation.
  49. */
  50. };
  51. #define MEI_DUMP_SKB_IN(info, skb) \
  52. do { \
  53. pr_debug("%s:\n", info); \
  54. print_hex_dump(KERN_DEBUG, "mei in : ", DUMP_PREFIX_OFFSET, \
  55. 16, 1, (skb)->data, (skb)->len, 0); \
  56. } while (0)
  57. #define MEI_DUMP_SKB_OUT(info, skb) \
  58. do { \
  59. pr_debug("%s:\n", info); \
  60. print_hex_dump(KERN_DEBUG, "mei out: ", DUMP_PREFIX_OFFSET, \
  61. 16, 1, (skb)->data, (skb)->len, 0); \
  62. } while (0)
  63. static int microread_mei_enable(void *phy_id)
  64. {
  65. struct microread_mei_phy *phy = phy_id;
  66. pr_info(DRIVER_DESC ": %s\n", __func__);
  67. phy->powered = 1;
  68. return 0;
  69. }
  70. static void microread_mei_disable(void *phy_id)
  71. {
  72. struct microread_mei_phy *phy = phy_id;
  73. pr_info(DRIVER_DESC ": %s\n", __func__);
  74. phy->powered = 0;
  75. }
  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 microread_mei_write(void *phy_id, struct sk_buff *skb)
  82. {
  83. struct microread_mei_phy *phy = phy_id;
  84. int r;
  85. MEI_DUMP_SKB_OUT("mei frame sent", skb);
  86. r = mei_bus_send(phy->client, skb->data, skb->len);
  87. if (r > 0)
  88. r = 0;
  89. return r;
  90. }
  91. static void microread_event_cb(struct mei_bus_client *client, u32 events,
  92. void *context)
  93. {
  94. struct microread_mei_phy *phy = context;
  95. if (phy->hard_fault != 0)
  96. return;
  97. if (events & BIT(MEI_BUS_EVENT_RX)) {
  98. struct sk_buff *skb;
  99. int reply_size;
  100. skb = alloc_skb(MEI_NFC_MAX_READ, GFP_KERNEL);
  101. if (!skb)
  102. return;
  103. reply_size = mei_bus_recv(client, skb->data, MEI_NFC_MAX_READ);
  104. if (reply_size < MEI_NFC_HEADER_SIZE) {
  105. kfree(skb);
  106. return;
  107. }
  108. skb_put(skb, reply_size);
  109. skb_pull(skb, MEI_NFC_HEADER_SIZE);
  110. MEI_DUMP_SKB_IN("mei frame read", skb);
  111. nfc_hci_recv_frame(phy->hdev, skb);
  112. }
  113. }
  114. static struct nfc_phy_ops mei_phy_ops = {
  115. .write = microread_mei_write,
  116. .enable = microread_mei_enable,
  117. .disable = microread_mei_disable,
  118. };
  119. static int microread_mei_probe(struct mei_bus_client *client)
  120. {
  121. struct microread_mei_phy *phy;
  122. int r;
  123. pr_info("Probing NFC microread\n");
  124. phy = kzalloc(sizeof(struct microread_mei_phy), GFP_KERNEL);
  125. if (!phy) {
  126. pr_err("Cannot allocate memory for microread mei phy.\n");
  127. return -ENOMEM;
  128. }
  129. phy->client = client;
  130. mei_bus_set_clientdata(client, phy);
  131. r = mei_bus_register_event_cb(client, microread_event_cb, phy);
  132. if (r) {
  133. pr_err(MICROREAD_DRIVER_NAME ": event cb registration failed\n");
  134. goto err_out;
  135. }
  136. r = microread_probe(phy, &mei_phy_ops, LLC_NOP_NAME,
  137. MEI_NFC_HEADER_SIZE, 0, MEI_NFC_MAX_HCI_PAYLOAD,
  138. &phy->hdev);
  139. if (r < 0)
  140. goto err_out;
  141. return 0;
  142. err_out:
  143. kfree(phy);
  144. return r;
  145. }
  146. static int microread_mei_remove(struct mei_bus_client *client)
  147. {
  148. struct microread_mei_phy *phy = mei_bus_get_clientdata(client);
  149. pr_info("Removing microread\n");
  150. microread_remove(phy->hdev);
  151. if (phy->powered)
  152. microread_mei_disable(phy);
  153. kfree(phy);
  154. return 0;
  155. }
  156. static struct mei_bus_driver microread_driver = {
  157. .driver = {
  158. .name = MICROREAD_DRIVER_NAME,
  159. },
  160. .id = {
  161. .name = MICROREAD_DRIVER_NAME,
  162. .uuid = MICROREAD_UUID,
  163. },
  164. .probe = microread_mei_probe,
  165. .remove = microread_mei_remove,
  166. };
  167. static int microread_mei_init(void)
  168. {
  169. int r;
  170. pr_debug(DRIVER_DESC ": %s\n", __func__);
  171. r = mei_driver_register(&microread_driver);
  172. if (r) {
  173. pr_err(MICROREAD_DRIVER_NAME ": driver registration failed\n");
  174. return r;
  175. }
  176. return 0;
  177. }
  178. static void microread_mei_exit(void)
  179. {
  180. mei_driver_unregister(&microread_driver);
  181. }
  182. module_init(microread_mei_init);
  183. module_exit(microread_mei_exit);
  184. MODULE_LICENSE("GPL");
  185. MODULE_DESCRIPTION(DRIVER_DESC);