xref: /linux/drivers/nfc/microread/mei.c (revision e0af11fa0df30bc645e6abb4d3ddc7ed05af0451)
1*e0af11faSSamuel Ortiz /*
2*e0af11faSSamuel Ortiz  * HCI based Driver for Inside Secure microread NFC Chip
3*e0af11faSSamuel Ortiz  *
4*e0af11faSSamuel Ortiz  * Copyright (C) 2013  Intel Corporation. All rights reserved.
5*e0af11faSSamuel Ortiz  *
6*e0af11faSSamuel Ortiz  * This program is free software; you can redistribute it and/or modify it
7*e0af11faSSamuel Ortiz  * under the terms and conditions of the GNU General Public License,
8*e0af11faSSamuel Ortiz  * version 2, as published by the Free Software Foundation.
9*e0af11faSSamuel Ortiz  *
10*e0af11faSSamuel Ortiz  * This program is distributed in the hope that it will be useful,
11*e0af11faSSamuel Ortiz  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*e0af11faSSamuel Ortiz  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*e0af11faSSamuel Ortiz  * GNU General Public License for more details.
14*e0af11faSSamuel Ortiz  *
15*e0af11faSSamuel Ortiz  * You should have received a copy of the GNU General Public License
16*e0af11faSSamuel Ortiz  * along with this program; if not, write to the
17*e0af11faSSamuel Ortiz  * Free Software Foundation, Inc.,
18*e0af11faSSamuel Ortiz  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19*e0af11faSSamuel Ortiz  */
20*e0af11faSSamuel Ortiz 
21*e0af11faSSamuel Ortiz #include <linux/module.h>
22*e0af11faSSamuel Ortiz #include <linux/slab.h>
23*e0af11faSSamuel Ortiz #include <linux/interrupt.h>
24*e0af11faSSamuel Ortiz #include <linux/gpio.h>
25*e0af11faSSamuel Ortiz #include <linux/mei_bus.h>
26*e0af11faSSamuel Ortiz 
27*e0af11faSSamuel Ortiz #include <linux/nfc.h>
28*e0af11faSSamuel Ortiz #include <net/nfc/hci.h>
29*e0af11faSSamuel Ortiz #include <net/nfc/llc.h>
30*e0af11faSSamuel Ortiz 
31*e0af11faSSamuel Ortiz #include "microread.h"
32*e0af11faSSamuel Ortiz 
33*e0af11faSSamuel Ortiz #define MICROREAD_DRIVER_NAME "microread"
34*e0af11faSSamuel Ortiz 
35*e0af11faSSamuel Ortiz #define MICROREAD_UUID UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50, 0x94, \
36*e0af11faSSamuel Ortiz 			       0xd4, 0x50, 0x26, 0x67, 0x23, 0x77, 0x5c)
37*e0af11faSSamuel Ortiz 
38*e0af11faSSamuel Ortiz struct mei_nfc_hdr {
39*e0af11faSSamuel Ortiz 	u8 cmd;
40*e0af11faSSamuel Ortiz 	u8 status;
41*e0af11faSSamuel Ortiz 	u16 req_id;
42*e0af11faSSamuel Ortiz 	u32 reserved;
43*e0af11faSSamuel Ortiz 	u16 data_size;
44*e0af11faSSamuel Ortiz } __attribute__((packed));
45*e0af11faSSamuel Ortiz 
46*e0af11faSSamuel Ortiz #define MEI_NFC_HEADER_SIZE 10
47*e0af11faSSamuel Ortiz #define MEI_NFC_MAX_HCI_PAYLOAD 300
48*e0af11faSSamuel Ortiz #define MEI_NFC_MAX_READ (MEI_NFC_HEADER_SIZE + MEI_NFC_MAX_HCI_PAYLOAD)
49*e0af11faSSamuel Ortiz 
50*e0af11faSSamuel Ortiz struct microread_mei_phy {
51*e0af11faSSamuel Ortiz 	struct mei_bus_client *client;
52*e0af11faSSamuel Ortiz 	struct nfc_hci_dev *hdev;
53*e0af11faSSamuel Ortiz 
54*e0af11faSSamuel Ortiz 	int powered;
55*e0af11faSSamuel Ortiz 
56*e0af11faSSamuel Ortiz 	int hard_fault;		/*
57*e0af11faSSamuel Ortiz 				 * < 0 if hardware error occured (e.g. i2c err)
58*e0af11faSSamuel Ortiz 				 * and prevents normal operation.
59*e0af11faSSamuel Ortiz 				 */
60*e0af11faSSamuel Ortiz };
61*e0af11faSSamuel Ortiz 
62*e0af11faSSamuel Ortiz #define MEI_DUMP_SKB_IN(info, skb)					\
63*e0af11faSSamuel Ortiz do {								\
64*e0af11faSSamuel Ortiz 	pr_debug("%s:\n", info);				\
65*e0af11faSSamuel Ortiz 	print_hex_dump(KERN_DEBUG, "mei in : ", DUMP_PREFIX_OFFSET,	\
66*e0af11faSSamuel Ortiz 		       16, 1, (skb)->data, (skb)->len, 0);	\
67*e0af11faSSamuel Ortiz } while (0)
68*e0af11faSSamuel Ortiz 
69*e0af11faSSamuel Ortiz #define MEI_DUMP_SKB_OUT(info, skb)					\
70*e0af11faSSamuel Ortiz do {								\
71*e0af11faSSamuel Ortiz 	pr_debug("%s:\n", info);				\
72*e0af11faSSamuel Ortiz 	print_hex_dump(KERN_DEBUG, "mei out: ", DUMP_PREFIX_OFFSET,	\
73*e0af11faSSamuel Ortiz 		       16, 1, (skb)->data, (skb)->len, 0);	\
74*e0af11faSSamuel Ortiz } while (0)
75*e0af11faSSamuel Ortiz 
76*e0af11faSSamuel Ortiz static int microread_mei_enable(void *phy_id)
77*e0af11faSSamuel Ortiz {
78*e0af11faSSamuel Ortiz 	struct microread_mei_phy *phy = phy_id;
79*e0af11faSSamuel Ortiz 
80*e0af11faSSamuel Ortiz 	pr_info(DRIVER_DESC ": %s\n", __func__);
81*e0af11faSSamuel Ortiz 
82*e0af11faSSamuel Ortiz 	phy->powered = 1;
83*e0af11faSSamuel Ortiz 
84*e0af11faSSamuel Ortiz 	return 0;
85*e0af11faSSamuel Ortiz }
86*e0af11faSSamuel Ortiz 
87*e0af11faSSamuel Ortiz static void microread_mei_disable(void *phy_id)
88*e0af11faSSamuel Ortiz {
89*e0af11faSSamuel Ortiz 	struct microread_mei_phy *phy = phy_id;
90*e0af11faSSamuel Ortiz 
91*e0af11faSSamuel Ortiz 	pr_info(DRIVER_DESC ": %s\n", __func__);
92*e0af11faSSamuel Ortiz 
93*e0af11faSSamuel Ortiz 	phy->powered = 0;
94*e0af11faSSamuel Ortiz }
95*e0af11faSSamuel Ortiz 
96*e0af11faSSamuel Ortiz /*
97*e0af11faSSamuel Ortiz  * Writing a frame must not return the number of written bytes.
98*e0af11faSSamuel Ortiz  * It must return either zero for success, or <0 for error.
99*e0af11faSSamuel Ortiz  * In addition, it must not alter the skb
100*e0af11faSSamuel Ortiz  */
101*e0af11faSSamuel Ortiz static int microread_mei_write(void *phy_id, struct sk_buff *skb)
102*e0af11faSSamuel Ortiz {
103*e0af11faSSamuel Ortiz 	struct microread_mei_phy *phy = phy_id;
104*e0af11faSSamuel Ortiz 	int r;
105*e0af11faSSamuel Ortiz 
106*e0af11faSSamuel Ortiz 	MEI_DUMP_SKB_OUT("mei frame sent", skb);
107*e0af11faSSamuel Ortiz 
108*e0af11faSSamuel Ortiz 	r = mei_bus_send(phy->client, skb->data, skb->len);
109*e0af11faSSamuel Ortiz 	if (r > 0)
110*e0af11faSSamuel Ortiz 		r = 0;
111*e0af11faSSamuel Ortiz 
112*e0af11faSSamuel Ortiz 	return r;
113*e0af11faSSamuel Ortiz }
114*e0af11faSSamuel Ortiz 
115*e0af11faSSamuel Ortiz static void microread_event_cb(struct mei_bus_client *client, u32 events,
116*e0af11faSSamuel Ortiz 			       void *context)
117*e0af11faSSamuel Ortiz {
118*e0af11faSSamuel Ortiz 	struct microread_mei_phy *phy = context;
119*e0af11faSSamuel Ortiz 
120*e0af11faSSamuel Ortiz 	if (phy->hard_fault != 0)
121*e0af11faSSamuel Ortiz 		return;
122*e0af11faSSamuel Ortiz 
123*e0af11faSSamuel Ortiz 	if (events & BIT(MEI_BUS_EVENT_RX)) {
124*e0af11faSSamuel Ortiz 		struct sk_buff *skb;
125*e0af11faSSamuel Ortiz 		int reply_size;
126*e0af11faSSamuel Ortiz 
127*e0af11faSSamuel Ortiz 		skb = alloc_skb(MEI_NFC_MAX_READ, GFP_KERNEL);
128*e0af11faSSamuel Ortiz 		if (!skb)
129*e0af11faSSamuel Ortiz 			return;
130*e0af11faSSamuel Ortiz 
131*e0af11faSSamuel Ortiz 		reply_size = mei_bus_recv(client, skb->data, MEI_NFC_MAX_READ);
132*e0af11faSSamuel Ortiz 		if (reply_size < MEI_NFC_HEADER_SIZE) {
133*e0af11faSSamuel Ortiz 			kfree(skb);
134*e0af11faSSamuel Ortiz 			return;
135*e0af11faSSamuel Ortiz 		}
136*e0af11faSSamuel Ortiz 
137*e0af11faSSamuel Ortiz 		skb_put(skb, reply_size);
138*e0af11faSSamuel Ortiz 		skb_pull(skb, MEI_NFC_HEADER_SIZE);
139*e0af11faSSamuel Ortiz 
140*e0af11faSSamuel Ortiz 		MEI_DUMP_SKB_IN("mei frame read", skb);
141*e0af11faSSamuel Ortiz 
142*e0af11faSSamuel Ortiz 		nfc_hci_recv_frame(phy->hdev, skb);
143*e0af11faSSamuel Ortiz 	}
144*e0af11faSSamuel Ortiz }
145*e0af11faSSamuel Ortiz 
146*e0af11faSSamuel Ortiz static struct nfc_phy_ops mei_phy_ops = {
147*e0af11faSSamuel Ortiz 	.write = microread_mei_write,
148*e0af11faSSamuel Ortiz 	.enable = microread_mei_enable,
149*e0af11faSSamuel Ortiz 	.disable = microread_mei_disable,
150*e0af11faSSamuel Ortiz };
151*e0af11faSSamuel Ortiz 
152*e0af11faSSamuel Ortiz static int microread_mei_probe(struct mei_bus_client *client)
153*e0af11faSSamuel Ortiz {
154*e0af11faSSamuel Ortiz 	struct microread_mei_phy *phy;
155*e0af11faSSamuel Ortiz 	int r;
156*e0af11faSSamuel Ortiz 
157*e0af11faSSamuel Ortiz 	pr_info("Probing NFC microread\n");
158*e0af11faSSamuel Ortiz 
159*e0af11faSSamuel Ortiz 	phy = kzalloc(sizeof(struct microread_mei_phy), GFP_KERNEL);
160*e0af11faSSamuel Ortiz 	if (!phy) {
161*e0af11faSSamuel Ortiz 		pr_err("Cannot allocate memory for microread mei phy.\n");
162*e0af11faSSamuel Ortiz 		return -ENOMEM;
163*e0af11faSSamuel Ortiz 	}
164*e0af11faSSamuel Ortiz 
165*e0af11faSSamuel Ortiz 	phy->client = client;
166*e0af11faSSamuel Ortiz 	mei_bus_set_clientdata(client, phy);
167*e0af11faSSamuel Ortiz 
168*e0af11faSSamuel Ortiz 	r = mei_bus_register_event_cb(client, microread_event_cb, phy);
169*e0af11faSSamuel Ortiz 	if (r) {
170*e0af11faSSamuel Ortiz 		pr_err(MICROREAD_DRIVER_NAME ": event cb registration failed\n");
171*e0af11faSSamuel Ortiz 		goto err_out;
172*e0af11faSSamuel Ortiz 	}
173*e0af11faSSamuel Ortiz 
174*e0af11faSSamuel Ortiz 	r = microread_probe(phy, &mei_phy_ops, LLC_NOP_NAME,
175*e0af11faSSamuel Ortiz 			    MEI_NFC_HEADER_SIZE, 0, MEI_NFC_MAX_HCI_PAYLOAD,
176*e0af11faSSamuel Ortiz 			    &phy->hdev);
177*e0af11faSSamuel Ortiz 	if (r < 0)
178*e0af11faSSamuel Ortiz 		goto err_out;
179*e0af11faSSamuel Ortiz 
180*e0af11faSSamuel Ortiz 	return 0;
181*e0af11faSSamuel Ortiz 
182*e0af11faSSamuel Ortiz err_out:
183*e0af11faSSamuel Ortiz 	kfree(phy);
184*e0af11faSSamuel Ortiz 
185*e0af11faSSamuel Ortiz 	return r;
186*e0af11faSSamuel Ortiz }
187*e0af11faSSamuel Ortiz 
188*e0af11faSSamuel Ortiz static int microread_mei_remove(struct mei_bus_client *client)
189*e0af11faSSamuel Ortiz {
190*e0af11faSSamuel Ortiz 	struct microread_mei_phy *phy = mei_bus_get_clientdata(client);
191*e0af11faSSamuel Ortiz 
192*e0af11faSSamuel Ortiz 	pr_info("Removing microread\n");
193*e0af11faSSamuel Ortiz 
194*e0af11faSSamuel Ortiz 	microread_remove(phy->hdev);
195*e0af11faSSamuel Ortiz 
196*e0af11faSSamuel Ortiz 	if (phy->powered)
197*e0af11faSSamuel Ortiz 		microread_mei_disable(phy);
198*e0af11faSSamuel Ortiz 
199*e0af11faSSamuel Ortiz 	kfree(phy);
200*e0af11faSSamuel Ortiz 
201*e0af11faSSamuel Ortiz 	return 0;
202*e0af11faSSamuel Ortiz }
203*e0af11faSSamuel Ortiz 
204*e0af11faSSamuel Ortiz static struct mei_bus_driver microread_driver = {
205*e0af11faSSamuel Ortiz 	.driver = {
206*e0af11faSSamuel Ortiz 		   .name = MICROREAD_DRIVER_NAME,
207*e0af11faSSamuel Ortiz 		  },
208*e0af11faSSamuel Ortiz 	.id = {
209*e0af11faSSamuel Ortiz 		.name = MICROREAD_DRIVER_NAME,
210*e0af11faSSamuel Ortiz 		.uuid = MICROREAD_UUID,
211*e0af11faSSamuel Ortiz 	},
212*e0af11faSSamuel Ortiz 
213*e0af11faSSamuel Ortiz 	.probe = microread_mei_probe,
214*e0af11faSSamuel Ortiz 	.remove = microread_mei_remove,
215*e0af11faSSamuel Ortiz };
216*e0af11faSSamuel Ortiz 
217*e0af11faSSamuel Ortiz static int microread_mei_init(void)
218*e0af11faSSamuel Ortiz {
219*e0af11faSSamuel Ortiz 	int r;
220*e0af11faSSamuel Ortiz 
221*e0af11faSSamuel Ortiz 	pr_debug(DRIVER_DESC ": %s\n", __func__);
222*e0af11faSSamuel Ortiz 
223*e0af11faSSamuel Ortiz 	r = mei_driver_register(&microread_driver);
224*e0af11faSSamuel Ortiz 	if (r) {
225*e0af11faSSamuel Ortiz 		pr_err(MICROREAD_DRIVER_NAME ": driver registration failed\n");
226*e0af11faSSamuel Ortiz 		return r;
227*e0af11faSSamuel Ortiz 	}
228*e0af11faSSamuel Ortiz 
229*e0af11faSSamuel Ortiz 	return 0;
230*e0af11faSSamuel Ortiz }
231*e0af11faSSamuel Ortiz 
232*e0af11faSSamuel Ortiz static void microread_mei_exit(void)
233*e0af11faSSamuel Ortiz {
234*e0af11faSSamuel Ortiz 	mei_driver_unregister(&microread_driver);
235*e0af11faSSamuel Ortiz }
236*e0af11faSSamuel Ortiz 
237*e0af11faSSamuel Ortiz module_init(microread_mei_init);
238*e0af11faSSamuel Ortiz module_exit(microread_mei_exit);
239*e0af11faSSamuel Ortiz 
240*e0af11faSSamuel Ortiz MODULE_LICENSE("GPL");
241*e0af11faSSamuel Ortiz MODULE_DESCRIPTION(DRIVER_DESC);
242