]>
Commit | Line | Data |
---|---|---|
71054c7d SO |
1 | /* |
2 | * HCI based Driver for Inside Secure microread NFC Chip - i2c layer | |
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 | |
98b32dec | 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
71054c7d SO |
17 | */ |
18 | ||
17936b43 JP |
19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
20 | ||
71054c7d SO |
21 | #include <linux/module.h> |
22 | #include <linux/i2c.h> | |
23 | #include <linux/delay.h> | |
24 | #include <linux/slab.h> | |
25 | #include <linux/interrupt.h> | |
26 | #include <linux/gpio.h> | |
27 | ||
28 | #include <linux/nfc.h> | |
29 | #include <net/nfc/hci.h> | |
30 | #include <net/nfc/llc.h> | |
31 | ||
32 | #include "microread.h" | |
33 | ||
34 | #define MICROREAD_I2C_DRIVER_NAME "microread" | |
35 | ||
36 | #define MICROREAD_I2C_FRAME_HEADROOM 1 | |
37 | #define MICROREAD_I2C_FRAME_TAILROOM 1 | |
38 | ||
39 | /* framing in HCI mode */ | |
40 | #define MICROREAD_I2C_LLC_LEN 1 | |
41 | #define MICROREAD_I2C_LLC_CRC 1 | |
42 | #define MICROREAD_I2C_LLC_LEN_CRC (MICROREAD_I2C_LLC_LEN + \ | |
43 | MICROREAD_I2C_LLC_CRC) | |
44 | #define MICROREAD_I2C_LLC_MIN_SIZE (1 + MICROREAD_I2C_LLC_LEN_CRC) | |
45 | #define MICROREAD_I2C_LLC_MAX_PAYLOAD 29 | |
46 | #define MICROREAD_I2C_LLC_MAX_SIZE (MICROREAD_I2C_LLC_LEN_CRC + 1 + \ | |
47 | MICROREAD_I2C_LLC_MAX_PAYLOAD) | |
48 | ||
49 | struct microread_i2c_phy { | |
50 | struct i2c_client *i2c_dev; | |
51 | struct nfc_hci_dev *hdev; | |
52 | ||
71054c7d SO |
53 | int hard_fault; /* |
54 | * < 0 if hardware error occured (e.g. i2c err) | |
55 | * and prevents normal operation. | |
56 | */ | |
57 | }; | |
58 | ||
59 | #define I2C_DUMP_SKB(info, skb) \ | |
60 | do { \ | |
61 | pr_debug("%s:\n", info); \ | |
62 | print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \ | |
63 | 16, 1, (skb)->data, (skb)->len, 0); \ | |
64 | } while (0) | |
65 | ||
66 | static void microread_i2c_add_len_crc(struct sk_buff *skb) | |
67 | { | |
68 | int i; | |
69 | u8 crc = 0; | |
70 | int len; | |
71 | ||
72 | len = skb->len; | |
73 | *skb_push(skb, 1) = len; | |
74 | ||
75 | for (i = 0; i < skb->len; i++) | |
76 | crc = crc ^ skb->data[i]; | |
77 | ||
78 | *skb_put(skb, 1) = crc; | |
79 | } | |
80 | ||
81 | static void microread_i2c_remove_len_crc(struct sk_buff *skb) | |
82 | { | |
83 | skb_pull(skb, MICROREAD_I2C_FRAME_HEADROOM); | |
84 | skb_trim(skb, MICROREAD_I2C_FRAME_TAILROOM); | |
85 | } | |
86 | ||
87 | static int check_crc(struct sk_buff *skb) | |
88 | { | |
89 | int i; | |
90 | u8 crc = 0; | |
91 | ||
92 | for (i = 0; i < skb->len - 1; i++) | |
93 | crc = crc ^ skb->data[i]; | |
94 | ||
95 | if (crc != skb->data[skb->len-1]) { | |
17936b43 JP |
96 | pr_err("CRC error 0x%x != 0x%x\n", crc, skb->data[skb->len-1]); |
97 | pr_info("%s: BAD CRC\n", __func__); | |
71054c7d SO |
98 | return -EPERM; |
99 | } | |
100 | ||
101 | return 0; | |
102 | } | |
103 | ||
104 | static int microread_i2c_enable(void *phy_id) | |
105 | { | |
106 | return 0; | |
107 | } | |
108 | ||
109 | static void microread_i2c_disable(void *phy_id) | |
110 | { | |
111 | return; | |
112 | } | |
113 | ||
114 | static int microread_i2c_write(void *phy_id, struct sk_buff *skb) | |
115 | { | |
116 | int r; | |
117 | struct microread_i2c_phy *phy = phy_id; | |
118 | struct i2c_client *client = phy->i2c_dev; | |
119 | ||
120 | if (phy->hard_fault != 0) | |
121 | return phy->hard_fault; | |
122 | ||
123 | usleep_range(3000, 6000); | |
124 | ||
125 | microread_i2c_add_len_crc(skb); | |
126 | ||
127 | I2C_DUMP_SKB("i2c frame written", skb); | |
128 | ||
129 | r = i2c_master_send(client, skb->data, skb->len); | |
130 | ||
131 | if (r == -EREMOTEIO) { /* Retry, chip was in standby */ | |
132 | usleep_range(6000, 10000); | |
133 | r = i2c_master_send(client, skb->data, skb->len); | |
134 | } | |
135 | ||
136 | if (r >= 0) { | |
137 | if (r != skb->len) | |
138 | r = -EREMOTEIO; | |
139 | else | |
140 | r = 0; | |
141 | } | |
142 | ||
143 | microread_i2c_remove_len_crc(skb); | |
144 | ||
145 | return r; | |
146 | } | |
147 | ||
148 | ||
149 | static int microread_i2c_read(struct microread_i2c_phy *phy, | |
150 | struct sk_buff **skb) | |
151 | { | |
152 | int r; | |
153 | u8 len; | |
154 | u8 tmp[MICROREAD_I2C_LLC_MAX_SIZE - 1]; | |
155 | struct i2c_client *client = phy->i2c_dev; | |
156 | ||
71054c7d SO |
157 | r = i2c_master_recv(client, &len, 1); |
158 | if (r != 1) { | |
17936b43 | 159 | nfc_err(&client->dev, "cannot read len byte\n"); |
71054c7d SO |
160 | return -EREMOTEIO; |
161 | } | |
162 | ||
163 | if ((len < MICROREAD_I2C_LLC_MIN_SIZE) || | |
164 | (len > MICROREAD_I2C_LLC_MAX_SIZE)) { | |
17936b43 | 165 | nfc_err(&client->dev, "invalid len byte\n"); |
71054c7d SO |
166 | r = -EBADMSG; |
167 | goto flush; | |
168 | } | |
169 | ||
170 | *skb = alloc_skb(1 + len, GFP_KERNEL); | |
171 | if (*skb == NULL) { | |
172 | r = -ENOMEM; | |
173 | goto flush; | |
174 | } | |
175 | ||
176 | *skb_put(*skb, 1) = len; | |
177 | ||
178 | r = i2c_master_recv(client, skb_put(*skb, len), len); | |
179 | if (r != len) { | |
180 | kfree_skb(*skb); | |
181 | return -EREMOTEIO; | |
182 | } | |
183 | ||
184 | I2C_DUMP_SKB("cc frame read", *skb); | |
185 | ||
186 | r = check_crc(*skb); | |
187 | if (r != 0) { | |
188 | kfree_skb(*skb); | |
189 | r = -EBADMSG; | |
190 | goto flush; | |
191 | } | |
192 | ||
193 | skb_pull(*skb, 1); | |
194 | skb_trim(*skb, (*skb)->len - MICROREAD_I2C_FRAME_TAILROOM); | |
195 | ||
196 | usleep_range(3000, 6000); | |
197 | ||
198 | return 0; | |
199 | ||
200 | flush: | |
201 | if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0) | |
202 | r = -EREMOTEIO; | |
203 | ||
204 | usleep_range(3000, 6000); | |
205 | ||
206 | return r; | |
207 | } | |
208 | ||
209 | static irqreturn_t microread_i2c_irq_thread_fn(int irq, void *phy_id) | |
210 | { | |
211 | struct microread_i2c_phy *phy = phy_id; | |
71054c7d SO |
212 | struct sk_buff *skb = NULL; |
213 | int r; | |
214 | ||
215 | if (!phy || irq != phy->i2c_dev->irq) { | |
216 | WARN_ON_ONCE(1); | |
217 | return IRQ_NONE; | |
218 | } | |
219 | ||
71054c7d SO |
220 | if (phy->hard_fault != 0) |
221 | return IRQ_HANDLED; | |
222 | ||
223 | r = microread_i2c_read(phy, &skb); | |
224 | if (r == -EREMOTEIO) { | |
225 | phy->hard_fault = r; | |
226 | ||
227 | nfc_hci_recv_frame(phy->hdev, NULL); | |
228 | ||
229 | return IRQ_HANDLED; | |
230 | } else if ((r == -ENOMEM) || (r == -EBADMSG)) { | |
231 | return IRQ_HANDLED; | |
232 | } | |
233 | ||
234 | nfc_hci_recv_frame(phy->hdev, skb); | |
235 | ||
236 | return IRQ_HANDLED; | |
237 | } | |
238 | ||
239 | static struct nfc_phy_ops i2c_phy_ops = { | |
240 | .write = microread_i2c_write, | |
241 | .enable = microread_i2c_enable, | |
242 | .disable = microread_i2c_disable, | |
243 | }; | |
244 | ||
245 | static int microread_i2c_probe(struct i2c_client *client, | |
246 | const struct i2c_device_id *id) | |
247 | { | |
248 | struct microread_i2c_phy *phy; | |
249 | struct microread_nfc_platform_data *pdata = | |
250 | dev_get_platdata(&client->dev); | |
251 | int r; | |
252 | ||
17936b43 | 253 | dev_dbg(&client->dev, "client %p\n", client); |
71054c7d SO |
254 | |
255 | if (!pdata) { | |
17936b43 | 256 | nfc_err(&client->dev, "client %p: missing platform data\n", |
71054c7d SO |
257 | client); |
258 | return -EINVAL; | |
259 | } | |
260 | ||
261 | phy = devm_kzalloc(&client->dev, sizeof(struct microread_i2c_phy), | |
262 | GFP_KERNEL); | |
17936b43 | 263 | if (!phy) |
71054c7d | 264 | return -ENOMEM; |
71054c7d SO |
265 | |
266 | i2c_set_clientdata(client, phy); | |
267 | phy->i2c_dev = client; | |
268 | ||
269 | r = request_threaded_irq(client->irq, NULL, microread_i2c_irq_thread_fn, | |
270 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, | |
271 | MICROREAD_I2C_DRIVER_NAME, phy); | |
272 | if (r) { | |
17936b43 | 273 | nfc_err(&client->dev, "Unable to register IRQ handler\n"); |
71054c7d SO |
274 | return r; |
275 | } | |
276 | ||
277 | r = microread_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME, | |
278 | MICROREAD_I2C_FRAME_HEADROOM, | |
279 | MICROREAD_I2C_FRAME_TAILROOM, | |
280 | MICROREAD_I2C_LLC_MAX_PAYLOAD, &phy->hdev); | |
281 | if (r < 0) | |
282 | goto err_irq; | |
283 | ||
3590ebc0 | 284 | nfc_info(&client->dev, "Probed\n"); |
71054c7d SO |
285 | |
286 | return 0; | |
287 | ||
288 | err_irq: | |
289 | free_irq(client->irq, phy); | |
290 | ||
291 | return r; | |
292 | } | |
293 | ||
294 | static int microread_i2c_remove(struct i2c_client *client) | |
295 | { | |
296 | struct microread_i2c_phy *phy = i2c_get_clientdata(client); | |
297 | ||
71054c7d SO |
298 | microread_remove(phy->hdev); |
299 | ||
300 | free_irq(client->irq, phy); | |
301 | ||
302 | return 0; | |
303 | } | |
304 | ||
305 | static struct i2c_device_id microread_i2c_id[] = { | |
306 | { MICROREAD_I2C_DRIVER_NAME, 0}, | |
307 | { } | |
308 | }; | |
309 | MODULE_DEVICE_TABLE(i2c, microread_i2c_id); | |
310 | ||
311 | static struct i2c_driver microread_i2c_driver = { | |
312 | .driver = { | |
313 | .name = MICROREAD_I2C_DRIVER_NAME, | |
314 | }, | |
315 | .probe = microread_i2c_probe, | |
316 | .remove = microread_i2c_remove, | |
317 | .id_table = microread_i2c_id, | |
318 | }; | |
319 | ||
320 | module_i2c_driver(microread_i2c_driver); | |
321 | ||
322 | MODULE_LICENSE("GPL"); | |
323 | MODULE_DESCRIPTION(DRIVER_DESC); |