2 * HCI based Driver for Inside Secure microread NFC Chip
4 * Copyright (C) 2013 Intel Corporation. All rights reserved.
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.
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.
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.
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/gpio.h>
25 #include <linux/mei_bus.h>
27 #include <linux/nfc.h>
28 #include <net/nfc/hci.h>
29 #include <net/nfc/llc.h>
31 #include "microread.h"
33 #define MICROREAD_DRIVER_NAME "microread"
35 #define MICROREAD_UUID UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50, 0x94, \
36 0xd4, 0x50, 0x26, 0x67, 0x23, 0x77, 0x5c)
44 } __attribute__((packed));
46 #define MEI_NFC_HEADER_SIZE 10
47 #define MEI_NFC_MAX_HCI_PAYLOAD 300
48 #define MEI_NFC_MAX_READ (MEI_NFC_HEADER_SIZE + MEI_NFC_MAX_HCI_PAYLOAD)
50 struct microread_mei_phy {
51 struct mei_device *mei_device;
52 struct nfc_hci_dev *hdev;
57 * < 0 if hardware error occured (e.g. i2c err)
58 * and prevents normal operation.
62 #define MEI_DUMP_SKB_IN(info, skb) \
64 pr_debug("%s:\n", info); \
65 print_hex_dump(KERN_DEBUG, "mei in : ", DUMP_PREFIX_OFFSET, \
66 16, 1, (skb)->data, (skb)->len, 0); \
69 #define MEI_DUMP_SKB_OUT(info, skb) \
71 pr_debug("%s:\n", info); \
72 print_hex_dump(KERN_DEBUG, "mei out: ", DUMP_PREFIX_OFFSET, \
73 16, 1, (skb)->data, (skb)->len, 0); \
76 static int microread_mei_enable(void *phy_id)
78 struct microread_mei_phy *phy = phy_id;
80 pr_info(DRIVER_DESC ": %s\n", __func__);
87 static void microread_mei_disable(void *phy_id)
89 struct microread_mei_phy *phy = phy_id;
91 pr_info(DRIVER_DESC ": %s\n", __func__);
97 * Writing a frame must not return the number of written bytes.
98 * It must return either zero for success, or <0 for error.
99 * In addition, it must not alter the skb
101 static int microread_mei_write(void *phy_id, struct sk_buff *skb)
103 struct microread_mei_phy *phy = phy_id;
106 MEI_DUMP_SKB_OUT("mei frame sent", skb);
108 r = mei_send(phy->device, skb->data, skb->len);
115 static void microread_event_cb(struct mei_device *device, u32 events,
118 struct microread_mei_phy *phy = context;
120 if (phy->hard_fault != 0)
123 if (events & BIT(MEI_EVENT_RX)) {
127 skb = alloc_skb(MEI_NFC_MAX_READ, GFP_KERNEL);
131 reply_size = mei_recv(device, skb->data, MEI_NFC_MAX_READ);
132 if (reply_size < MEI_NFC_HEADER_SIZE) {
137 skb_put(skb, reply_size);
138 skb_pull(skb, MEI_NFC_HEADER_SIZE);
140 MEI_DUMP_SKB_IN("mei frame read", skb);
142 nfc_hci_recv_frame(phy->hdev, skb);
146 static struct nfc_phy_ops mei_phy_ops = {
147 .write = microread_mei_write,
148 .enable = microread_mei_enable,
149 .disable = microread_mei_disable,
152 static int microread_mei_probe(struct mei_device *device,
153 const struct mei_id *id)
155 struct microread_mei_phy *phy;
158 pr_info("Probing NFC microread\n");
160 phy = kzalloc(sizeof(struct microread_mei_phy), GFP_KERNEL);
162 pr_err("Cannot allocate memory for microread mei phy.\n");
166 phy->device = device;
167 mei_set_clientdata(device, phy);
169 r = mei_register_event_cb(device, microread_event_cb, phy);
171 pr_err(MICROREAD_DRIVER_NAME ": event cb registration failed\n");
175 r = microread_probe(phy, &mei_phy_ops, LLC_NOP_NAME,
176 MEI_NFC_HEADER_SIZE, 0, MEI_NFC_MAX_HCI_PAYLOAD,
189 static int microread_mei_remove(struct mei_device *device)
191 struct microread_mei_phy *phy = mei_get_clientdata(device);
193 pr_info("Removing microread\n");
195 microread_remove(phy->hdev);
198 microread_mei_disable(phy);
205 static struct mei_id microread_mei_tbl[] = {
206 { MICROREAD_DRIVER_NAME, MICROREAD_UUID },
208 /* required last entry */
212 MODULE_DEVICE_TABLE(mei, microread_mei_tbl);
214 static struct mei_driver microread_driver = {
215 .id_table = microread_mei_tbl,
216 .name = MICROREAD_DRIVER_NAME,
218 .probe = microread_mei_probe,
219 .remove = microread_mei_remove,
222 static int microread_mei_init(void)
226 pr_debug(DRIVER_DESC ": %s\n", __func__);
228 r = mei_driver_register(µread_driver);
230 pr_err(MICROREAD_DRIVER_NAME ": driver registration failed\n");
237 static void microread_mei_exit(void)
239 mei_driver_unregister(µread_driver);
242 module_init(microread_mei_init);
243 module_exit(microread_mei_exit);
245 MODULE_LICENSE("GPL");
246 MODULE_DESCRIPTION(DRIVER_DESC);