blob: ca33ae193935bc3c3e75ffa809aa0573a5e24692 [file] [log] [blame]
Samuel Ortize0af11f2013-01-14 20:35:22 +01001/*
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
21#include <linux/module.h>
22#include <linux/slab.h>
23#include <linux/interrupt.h>
24#include <linux/gpio.h>
Samuel Ortiz9593b0b2013-03-28 10:39:28 +010025#include <linux/mei_cl_bus.h>
Samuel Ortize0af11f2013-01-14 20:35:22 +010026
27#include <linux/nfc.h>
28#include <net/nfc/hci.h>
29#include <net/nfc/llc.h>
30
31#include "microread.h"
32
33#define MICROREAD_DRIVER_NAME "microread"
34
Samuel Ortize0af11f2013-01-14 20:35:22 +010035struct mei_nfc_hdr {
36 u8 cmd;
37 u8 status;
38 u16 req_id;
39 u32 reserved;
40 u16 data_size;
41} __attribute__((packed));
42
43#define MEI_NFC_HEADER_SIZE 10
44#define MEI_NFC_MAX_HCI_PAYLOAD 300
45#define MEI_NFC_MAX_READ (MEI_NFC_HEADER_SIZE + MEI_NFC_MAX_HCI_PAYLOAD)
46
47struct microread_mei_phy {
Samuel Ortiz9593b0b2013-03-28 10:39:28 +010048 struct mei_cl_device *device;
Samuel Ortize0af11f2013-01-14 20:35:22 +010049 struct nfc_hci_dev *hdev;
50
51 int powered;
52
53 int hard_fault; /*
54 * < 0 if hardware error occured (e.g. i2c err)
55 * and prevents normal operation.
56 */
57};
58
59#define MEI_DUMP_SKB_IN(info, skb) \
60do { \
61 pr_debug("%s:\n", info); \
62 print_hex_dump(KERN_DEBUG, "mei in : ", DUMP_PREFIX_OFFSET, \
63 16, 1, (skb)->data, (skb)->len, 0); \
64} while (0)
65
66#define MEI_DUMP_SKB_OUT(info, skb) \
67do { \
68 pr_debug("%s:\n", info); \
69 print_hex_dump(KERN_DEBUG, "mei out: ", DUMP_PREFIX_OFFSET, \
70 16, 1, (skb)->data, (skb)->len, 0); \
71} while (0)
72
73static int microread_mei_enable(void *phy_id)
74{
75 struct microread_mei_phy *phy = phy_id;
76
77 pr_info(DRIVER_DESC ": %s\n", __func__);
78
79 phy->powered = 1;
80
81 return 0;
82}
83
84static void microread_mei_disable(void *phy_id)
85{
86 struct microread_mei_phy *phy = phy_id;
87
88 pr_info(DRIVER_DESC ": %s\n", __func__);
89
90 phy->powered = 0;
91}
92
93/*
94 * Writing a frame must not return the number of written bytes.
95 * It must return either zero for success, or <0 for error.
96 * In addition, it must not alter the skb
97 */
98static int microread_mei_write(void *phy_id, struct sk_buff *skb)
99{
100 struct microread_mei_phy *phy = phy_id;
101 int r;
102
103 MEI_DUMP_SKB_OUT("mei frame sent", skb);
104
Samuel Ortiz9593b0b2013-03-28 10:39:28 +0100105 r = mei_cl_send(phy->device, skb->data, skb->len);
Samuel Ortize0af11f2013-01-14 20:35:22 +0100106 if (r > 0)
107 r = 0;
108
109 return r;
110}
111
Samuel Ortiz9593b0b2013-03-28 10:39:28 +0100112static void microread_event_cb(struct mei_cl_device *device, u32 events,
Samuel Ortize0af11f2013-01-14 20:35:22 +0100113 void *context)
114{
115 struct microread_mei_phy *phy = context;
116
117 if (phy->hard_fault != 0)
118 return;
119
Samuel Ortiz9593b0b2013-03-28 10:39:28 +0100120 if (events & BIT(MEI_CL_EVENT_RX)) {
Samuel Ortize0af11f2013-01-14 20:35:22 +0100121 struct sk_buff *skb;
122 int reply_size;
123
124 skb = alloc_skb(MEI_NFC_MAX_READ, GFP_KERNEL);
125 if (!skb)
126 return;
127
Samuel Ortiz9593b0b2013-03-28 10:39:28 +0100128 reply_size = mei_cl_recv(device, skb->data, MEI_NFC_MAX_READ);
Samuel Ortize0af11f2013-01-14 20:35:22 +0100129 if (reply_size < MEI_NFC_HEADER_SIZE) {
130 kfree(skb);
131 return;
132 }
133
134 skb_put(skb, reply_size);
135 skb_pull(skb, MEI_NFC_HEADER_SIZE);
136
137 MEI_DUMP_SKB_IN("mei frame read", skb);
138
139 nfc_hci_recv_frame(phy->hdev, skb);
140 }
141}
142
143static struct nfc_phy_ops mei_phy_ops = {
144 .write = microread_mei_write,
145 .enable = microread_mei_enable,
146 .disable = microread_mei_disable,
147};
148
Samuel Ortiz9593b0b2013-03-28 10:39:28 +0100149static int microread_mei_probe(struct mei_cl_device *device,
150 const struct mei_cl_device_id *id)
Samuel Ortize0af11f2013-01-14 20:35:22 +0100151{
152 struct microread_mei_phy *phy;
153 int r;
154
155 pr_info("Probing NFC microread\n");
156
157 phy = kzalloc(sizeof(struct microread_mei_phy), GFP_KERNEL);
158 if (!phy) {
159 pr_err("Cannot allocate memory for microread mei phy.\n");
160 return -ENOMEM;
161 }
162
Samuel Ortizcd48d8b2013-02-11 10:30:04 +0100163 phy->device = device;
Samuel Ortiz9593b0b2013-03-28 10:39:28 +0100164 mei_cl_set_drvdata(device, phy);
Samuel Ortize0af11f2013-01-14 20:35:22 +0100165
Samuel Ortiz9593b0b2013-03-28 10:39:28 +0100166 r = mei_cl_register_event_cb(device, microread_event_cb, phy);
Samuel Ortize0af11f2013-01-14 20:35:22 +0100167 if (r) {
168 pr_err(MICROREAD_DRIVER_NAME ": event cb registration failed\n");
169 goto err_out;
170 }
171
172 r = microread_probe(phy, &mei_phy_ops, LLC_NOP_NAME,
173 MEI_NFC_HEADER_SIZE, 0, MEI_NFC_MAX_HCI_PAYLOAD,
174 &phy->hdev);
175 if (r < 0)
176 goto err_out;
177
178 return 0;
179
180err_out:
181 kfree(phy);
182
183 return r;
184}
185
Samuel Ortiz9593b0b2013-03-28 10:39:28 +0100186static int microread_mei_remove(struct mei_cl_device *device)
Samuel Ortize0af11f2013-01-14 20:35:22 +0100187{
Samuel Ortiz9593b0b2013-03-28 10:39:28 +0100188 struct microread_mei_phy *phy = mei_cl_get_drvdata(device);
Samuel Ortize0af11f2013-01-14 20:35:22 +0100189
190 pr_info("Removing microread\n");
191
192 microread_remove(phy->hdev);
193
194 if (phy->powered)
195 microread_mei_disable(phy);
196
197 kfree(phy);
198
199 return 0;
200}
201
Samuel Ortiz9593b0b2013-03-28 10:39:28 +0100202static struct mei_cl_device_id microread_mei_tbl[] = {
203 { MICROREAD_DRIVER_NAME },
Samuel Ortizcd48d8b2013-02-11 10:30:04 +0100204
205 /* required last entry */
206 { }
207};
Samuel Ortizcd48d8b2013-02-11 10:30:04 +0100208MODULE_DEVICE_TABLE(mei, microread_mei_tbl);
209
Samuel Ortiz9593b0b2013-03-28 10:39:28 +0100210static struct mei_cl_driver microread_driver = {
Samuel Ortizcd48d8b2013-02-11 10:30:04 +0100211 .id_table = microread_mei_tbl,
212 .name = MICROREAD_DRIVER_NAME,
Samuel Ortize0af11f2013-01-14 20:35:22 +0100213
214 .probe = microread_mei_probe,
215 .remove = microread_mei_remove,
216};
217
218static int microread_mei_init(void)
219{
220 int r;
221
222 pr_debug(DRIVER_DESC ": %s\n", __func__);
223
Samuel Ortiz9593b0b2013-03-28 10:39:28 +0100224 r = mei_cl_driver_register(&microread_driver);
Samuel Ortize0af11f2013-01-14 20:35:22 +0100225 if (r) {
226 pr_err(MICROREAD_DRIVER_NAME ": driver registration failed\n");
227 return r;
228 }
229
230 return 0;
231}
232
233static void microread_mei_exit(void)
234{
Samuel Ortiz9593b0b2013-03-28 10:39:28 +0100235 mei_cl_driver_unregister(&microread_driver);
Samuel Ortize0af11f2013-01-14 20:35:22 +0100236}
237
238module_init(microread_mei_init);
239module_exit(microread_mei_exit);
240
241MODULE_LICENSE("GPL");
242MODULE_DESCRIPTION(DRIVER_DESC);