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