mei.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_device *mei_device;
  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_send(phy->device, skb->data, skb->len);
  87. if (r > 0)
  88. r = 0;
  89. return r;
  90. }
  91. static void microread_event_cb(struct mei_device *device, 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_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_recv(device, 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_device *device,
  120. const struct mei_id *id)
  121. {
  122. struct microread_mei_phy *phy;
  123. int r;
  124. pr_info("Probing NFC microread\n");
  125. phy = kzalloc(sizeof(struct microread_mei_phy), GFP_KERNEL);
  126. if (!phy) {
  127. pr_err("Cannot allocate memory for microread mei phy.\n");
  128. return -ENOMEM;
  129. }
  130. phy->device = device;
  131. mei_set_clientdata(device, phy);
  132. r = mei_register_event_cb(device, microread_event_cb, phy);
  133. if (r) {
  134. pr_err(MICROREAD_DRIVER_NAME ": event cb registration failed\n");
  135. goto err_out;
  136. }
  137. r = microread_probe(phy, &mei_phy_ops, LLC_NOP_NAME,
  138. MEI_NFC_HEADER_SIZE, 0, MEI_NFC_MAX_HCI_PAYLOAD,
  139. &phy->hdev);
  140. if (r < 0)
  141. goto err_out;
  142. return 0;
  143. err_out:
  144. kfree(phy);
  145. return r;
  146. }
  147. static int microread_mei_remove(struct mei_device *device)
  148. {
  149. struct microread_mei_phy *phy = mei_get_clientdata(device);
  150. pr_info("Removing microread\n");
  151. microread_remove(phy->hdev);
  152. if (phy->powered)
  153. microread_mei_disable(phy);
  154. kfree(phy);
  155. return 0;
  156. }
  157. static struct mei_id microread_mei_tbl[] = {
  158. { MICROREAD_DRIVER_NAME, MICROREAD_UUID },
  159. /* required last entry */
  160. { }
  161. };
  162. MODULE_DEVICE_TABLE(mei, microread_mei_tbl);
  163. static struct mei_driver microread_driver = {
  164. .id_table = microread_mei_tbl,
  165. .name = MICROREAD_DRIVER_NAME,
  166. .probe = microread_mei_probe,
  167. .remove = microread_mei_remove,
  168. };
  169. static int microread_mei_init(void)
  170. {
  171. int r;
  172. pr_debug(DRIVER_DESC ": %s\n", __func__);
  173. r = mei_driver_register(&microread_driver);
  174. if (r) {
  175. pr_err(MICROREAD_DRIVER_NAME ": driver registration failed\n");
  176. return r;
  177. }
  178. return 0;
  179. }
  180. static void microread_mei_exit(void)
  181. {
  182. mei_driver_unregister(&microread_driver);
  183. }
  184. module_init(microread_mei_init);
  185. module_exit(microread_mei_exit);
  186. MODULE_LICENSE("GPL");
  187. MODULE_DESCRIPTION(DRIVER_DESC);