1
2
3
4
5
6
7#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
9#include <linux/crc-ccitt.h>
10#include <linux/module.h>
11#include <linux/i2c.h>
12#include <linux/gpio/consumer.h>
13#include <linux/of_irq.h>
14#include <linux/of_gpio.h>
15#include <linux/acpi.h>
16#include <linux/interrupt.h>
17#include <linux/delay.h>
18#include <linux/nfc.h>
19#include <linux/firmware.h>
20
21#include <net/nfc/hci.h>
22#include <net/nfc/llc.h>
23#include <net/nfc/nfc.h>
24
25#include "st21nfca.h"
26
27
28
29
30
31
32
33
34
35
36#define ST21NFCA_SOF_EOF 0x7e
37#define ST21NFCA_BYTE_STUFFING_MASK 0x20
38#define ST21NFCA_ESCAPE_BYTE_STUFFING 0x7d
39
40
41#define ST21NFCA_FRAME_HEADROOM 2
42
43
44#define ST21NFCA_FRAME_TAILROOM 3
45#define IS_START_OF_FRAME(buf) (buf[0] == ST21NFCA_SOF_EOF && \
46 buf[1] == 0)
47
48#define ST21NFCA_HCI_DRIVER_NAME "st21nfca_hci"
49#define ST21NFCA_HCI_I2C_DRIVER_NAME "st21nfca_hci_i2c"
50
51struct st21nfca_i2c_phy {
52 struct i2c_client *i2c_dev;
53 struct nfc_hci_dev *hdev;
54
55 struct gpio_desc *gpiod_ena;
56 struct st21nfca_se_status se_status;
57
58 struct sk_buff *pending_skb;
59 int current_read_len;
60
61
62
63
64 int crc_trials;
65
66 int powered;
67 int run_mode;
68
69
70
71
72
73 int hard_fault;
74 struct mutex phy_lock;
75};
76
77static const u8 len_seq[] = { 16, 24, 12, 29 };
78static const u16 wait_tab[] = { 2, 3, 5, 15, 20, 40};
79
80#define I2C_DUMP_SKB(info, skb) \
81do { \
82 pr_debug("%s:\n", info); \
83 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
84 16, 1, (skb)->data, (skb)->len, 0); \
85} while (0)
86
87
88
89
90
91
92
93static int st21nfca_hci_platform_init(struct st21nfca_i2c_phy *phy)
94{
95 u16 wait_reboot[] = { 50, 300, 1000 };
96 char reboot_cmd[] = { 0x7E, 0x66, 0x48, 0xF6, 0x7E };
97 u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE];
98 int i, r = -1;
99
100 for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
101 r = i2c_master_send(phy->i2c_dev, reboot_cmd,
102 sizeof(reboot_cmd));
103 if (r < 0)
104 msleep(wait_reboot[i]);
105 }
106 if (r < 0)
107 return r;
108
109
110 msleep(20);
111 r = -1;
112 for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
113 r = i2c_master_recv(phy->i2c_dev, tmp,
114 ST21NFCA_HCI_LLC_MAX_SIZE);
115 if (r < 0)
116 msleep(wait_reboot[i]);
117 }
118 if (r < 0)
119 return r;
120
121 for (i = 0; i < ST21NFCA_HCI_LLC_MAX_SIZE &&
122 tmp[i] == ST21NFCA_SOF_EOF; i++)
123 ;
124
125 if (r != ST21NFCA_HCI_LLC_MAX_SIZE)
126 return -ENODEV;
127
128 usleep_range(1000, 1500);
129 return 0;
130}
131
132static int st21nfca_hci_i2c_enable(void *phy_id)
133{
134 struct st21nfca_i2c_phy *phy = phy_id;
135
136 gpiod_set_value(phy->gpiod_ena, 1);
137 phy->powered = 1;
138 phy->run_mode = ST21NFCA_HCI_MODE;
139
140 usleep_range(10000, 15000);
141
142 return 0;
143}
144
145static void st21nfca_hci_i2c_disable(void *phy_id)
146{
147 struct st21nfca_i2c_phy *phy = phy_id;
148
149 gpiod_set_value(phy->gpiod_ena, 0);
150
151 phy->powered = 0;
152}
153
154static void st21nfca_hci_add_len_crc(struct sk_buff *skb)
155{
156 u16 crc;
157 u8 tmp;
158
159 *(u8 *)skb_push(skb, 1) = 0;
160
161 crc = crc_ccitt(0xffff, skb->data, skb->len);
162 crc = ~crc;
163
164 tmp = crc & 0x00ff;
165 skb_put_u8(skb, tmp);
166
167 tmp = (crc >> 8) & 0x00ff;
168 skb_put_u8(skb, tmp);
169}
170
171static void st21nfca_hci_remove_len_crc(struct sk_buff *skb)
172{
173 skb_pull(skb, ST21NFCA_FRAME_HEADROOM);
174 skb_trim(skb, skb->len - ST21NFCA_FRAME_TAILROOM);
175}
176
177
178
179
180
181
182static int st21nfca_hci_i2c_write(void *phy_id, struct sk_buff *skb)
183{
184 int r = -1, i, j;
185 struct st21nfca_i2c_phy *phy = phy_id;
186 struct i2c_client *client = phy->i2c_dev;
187 u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE * 2];
188
189 I2C_DUMP_SKB("st21nfca_hci_i2c_write", skb);
190
191 if (phy->hard_fault != 0)
192 return phy->hard_fault;
193
194
195
196
197
198
199 st21nfca_hci_add_len_crc(skb);
200
201
202 skb_put_u8(skb, ST21NFCA_SOF_EOF);
203
204 *(u8 *)skb_push(skb, 1) = ST21NFCA_SOF_EOF;
205
206
207
208
209
210
211
212 tmp[0] = skb->data[0];
213 for (i = 1, j = 1; i < skb->len - 1; i++, j++) {
214 if (skb->data[i] == ST21NFCA_SOF_EOF
215 || skb->data[i] == ST21NFCA_ESCAPE_BYTE_STUFFING) {
216 tmp[j] = ST21NFCA_ESCAPE_BYTE_STUFFING;
217 j++;
218 tmp[j] = skb->data[i] ^ ST21NFCA_BYTE_STUFFING_MASK;
219 } else {
220 tmp[j] = skb->data[i];
221 }
222 }
223 tmp[j] = skb->data[i];
224 j++;
225
226
227
228
229
230 mutex_lock(&phy->phy_lock);
231 for (i = 0; i < ARRAY_SIZE(wait_tab) && r < 0; i++) {
232 r = i2c_master_send(client, tmp, j);
233 if (r < 0)
234 msleep(wait_tab[i]);
235 }
236 mutex_unlock(&phy->phy_lock);
237
238 if (r >= 0) {
239 if (r != j)
240 r = -EREMOTEIO;
241 else
242 r = 0;
243 }
244
245 st21nfca_hci_remove_len_crc(skb);
246
247 return r;
248}
249
250static int get_frame_size(u8 *buf, int buflen)
251{
252 int len = 0;
253
254 if (buf[len + 1] == ST21NFCA_SOF_EOF)
255 return 0;
256
257 for (len = 1; len < buflen && buf[len] != ST21NFCA_SOF_EOF; len++)
258 ;
259
260 return len;
261}
262
263static int check_crc(u8 *buf, int buflen)
264{
265 u16 crc;
266
267 crc = crc_ccitt(0xffff, buf, buflen - 2);
268 crc = ~crc;
269
270 if (buf[buflen - 2] != (crc & 0xff) || buf[buflen - 1] != (crc >> 8)) {
271 pr_err(ST21NFCA_HCI_DRIVER_NAME
272 ": CRC error 0x%x != 0x%x 0x%x\n", crc, buf[buflen - 1],
273 buf[buflen - 2]);
274
275 pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__);
276 print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
277 16, 2, buf, buflen, false);
278 return -EPERM;
279 }
280 return 0;
281}
282
283
284
285
286
287
288
289
290
291static int st21nfca_hci_i2c_repack(struct sk_buff *skb)
292{
293 int i, j, r, size;
294
295 if (skb->len < 1 || (skb->len > 1 && skb->data[1] != 0))
296 return -EBADMSG;
297
298 size = get_frame_size(skb->data, skb->len);
299 if (size > 0) {
300 skb_trim(skb, size);
301
302 for (i = 1, j = 0; i < skb->len; i++) {
303 if (skb->data[i + j] ==
304 (u8) ST21NFCA_ESCAPE_BYTE_STUFFING) {
305 skb->data[i] = skb->data[i + j + 1]
306 | ST21NFCA_BYTE_STUFFING_MASK;
307 i++;
308 j++;
309 }
310 skb->data[i] = skb->data[i + j];
311 }
312
313 skb_trim(skb, i - j);
314
315 skb_pull(skb, 1);
316
317 r = check_crc(skb->data, skb->len);
318 if (r != 0) {
319 i = 0;
320 return -EBADMSG;
321 }
322
323
324 skb_pull(skb, 1);
325
326 skb_trim(skb, skb->len - 2);
327 return skb->len;
328 }
329 return 0;
330}
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347static int st21nfca_hci_i2c_read(struct st21nfca_i2c_phy *phy,
348 struct sk_buff *skb)
349{
350 int r, i;
351 u8 len;
352 u8 buf[ST21NFCA_HCI_LLC_MAX_PAYLOAD];
353 struct i2c_client *client = phy->i2c_dev;
354
355 if (phy->current_read_len < ARRAY_SIZE(len_seq)) {
356 len = len_seq[phy->current_read_len];
357
358
359
360
361
362
363 r = 0;
364 mutex_lock(&phy->phy_lock);
365 for (i = 0; i < ARRAY_SIZE(wait_tab) && r <= 0; i++) {
366 r = i2c_master_recv(client, buf, len);
367 if (r < 0)
368 msleep(wait_tab[i]);
369 }
370 mutex_unlock(&phy->phy_lock);
371
372 if (r != len) {
373 phy->current_read_len = 0;
374 return -EREMOTEIO;
375 }
376
377
378
379
380
381 if (!phy->current_read_len && !IS_START_OF_FRAME(buf)) {
382 skb_trim(skb, 0);
383 phy->current_read_len = 0;
384 return -EIO;
385 } else if (phy->current_read_len && IS_START_OF_FRAME(buf)) {
386
387
388
389
390
391 skb_trim(skb, 0);
392 phy->current_read_len = 0;
393 }
394
395 skb_put_data(skb, buf, len);
396
397 if (skb->data[skb->len - 1] == ST21NFCA_SOF_EOF) {
398 phy->current_read_len = 0;
399 return st21nfca_hci_i2c_repack(skb);
400 }
401 phy->current_read_len++;
402 return -EAGAIN;
403 }
404 return -EIO;
405}
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421static irqreturn_t st21nfca_hci_irq_thread_fn(int irq, void *phy_id)
422{
423 struct st21nfca_i2c_phy *phy = phy_id;
424 struct i2c_client *client;
425
426 int r;
427
428 if (!phy || irq != phy->i2c_dev->irq) {
429 WARN_ON_ONCE(1);
430 return IRQ_NONE;
431 }
432
433 client = phy->i2c_dev;
434 dev_dbg(&client->dev, "IRQ\n");
435
436 if (phy->hard_fault != 0)
437 return IRQ_HANDLED;
438
439 r = st21nfca_hci_i2c_read(phy, phy->pending_skb);
440 if (r == -EREMOTEIO) {
441 phy->hard_fault = r;
442
443 nfc_hci_recv_frame(phy->hdev, NULL);
444
445 return IRQ_HANDLED;
446 } else if (r == -EAGAIN || r == -EIO) {
447 return IRQ_HANDLED;
448 } else if (r == -EBADMSG && phy->crc_trials < ARRAY_SIZE(wait_tab)) {
449
450
451
452
453
454
455
456
457
458 msleep(wait_tab[phy->crc_trials]);
459 phy->crc_trials++;
460 phy->current_read_len = 0;
461 kfree_skb(phy->pending_skb);
462 } else if (r > 0) {
463
464
465
466
467
468 nfc_hci_recv_frame(phy->hdev, phy->pending_skb);
469 phy->crc_trials = 0;
470 } else {
471 kfree_skb(phy->pending_skb);
472 }
473
474 phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
475 if (phy->pending_skb == NULL) {
476 phy->hard_fault = -ENOMEM;
477 nfc_hci_recv_frame(phy->hdev, NULL);
478 }
479
480 return IRQ_HANDLED;
481}
482
483static const struct nfc_phy_ops i2c_phy_ops = {
484 .write = st21nfca_hci_i2c_write,
485 .enable = st21nfca_hci_i2c_enable,
486 .disable = st21nfca_hci_i2c_disable,
487};
488
489static const struct acpi_gpio_params enable_gpios = { 1, 0, false };
490
491static const struct acpi_gpio_mapping acpi_st21nfca_gpios[] = {
492 { "enable-gpios", &enable_gpios, 1 },
493 {},
494};
495
496static int st21nfca_hci_i2c_probe(struct i2c_client *client,
497 const struct i2c_device_id *id)
498{
499 struct device *dev = &client->dev;
500 struct st21nfca_i2c_phy *phy;
501 int r;
502
503 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
504 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
505 return -ENODEV;
506 }
507
508 phy = devm_kzalloc(&client->dev, sizeof(struct st21nfca_i2c_phy),
509 GFP_KERNEL);
510 if (!phy)
511 return -ENOMEM;
512
513 phy->i2c_dev = client;
514 phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
515 if (phy->pending_skb == NULL)
516 return -ENOMEM;
517
518 phy->current_read_len = 0;
519 phy->crc_trials = 0;
520 mutex_init(&phy->phy_lock);
521 i2c_set_clientdata(client, phy);
522
523 r = devm_acpi_dev_add_driver_gpios(dev, acpi_st21nfca_gpios);
524 if (r)
525 dev_dbg(dev, "Unable to add GPIO mapping table\n");
526
527
528 phy->gpiod_ena = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
529 if (IS_ERR(phy->gpiod_ena)) {
530 nfc_err(dev, "Unable to get ENABLE GPIO\n");
531 return PTR_ERR(phy->gpiod_ena);
532 }
533
534 phy->se_status.is_ese_present =
535 device_property_read_bool(&client->dev, "ese-present");
536 phy->se_status.is_uicc_present =
537 device_property_read_bool(&client->dev, "uicc-present");
538
539 r = st21nfca_hci_platform_init(phy);
540 if (r < 0) {
541 nfc_err(&client->dev, "Unable to reboot st21nfca\n");
542 return r;
543 }
544
545 r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
546 st21nfca_hci_irq_thread_fn,
547 IRQF_ONESHOT,
548 ST21NFCA_HCI_DRIVER_NAME, phy);
549 if (r < 0) {
550 nfc_err(&client->dev, "Unable to register IRQ handler\n");
551 return r;
552 }
553
554 return st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
555 ST21NFCA_FRAME_HEADROOM,
556 ST21NFCA_FRAME_TAILROOM,
557 ST21NFCA_HCI_LLC_MAX_PAYLOAD,
558 &phy->hdev,
559 &phy->se_status);
560}
561
562static int st21nfca_hci_i2c_remove(struct i2c_client *client)
563{
564 struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
565
566 st21nfca_hci_remove(phy->hdev);
567
568 if (phy->powered)
569 st21nfca_hci_i2c_disable(phy);
570
571 return 0;
572}
573
574static const struct i2c_device_id st21nfca_hci_i2c_id_table[] = {
575 {ST21NFCA_HCI_DRIVER_NAME, 0},
576 {}
577};
578MODULE_DEVICE_TABLE(i2c, st21nfca_hci_i2c_id_table);
579
580static const struct acpi_device_id st21nfca_hci_i2c_acpi_match[] __maybe_unused = {
581 {"SMO2100", 0},
582 {}
583};
584MODULE_DEVICE_TABLE(acpi, st21nfca_hci_i2c_acpi_match);
585
586static const struct of_device_id of_st21nfca_i2c_match[] __maybe_unused = {
587 { .compatible = "st,st21nfca-i2c", },
588 { .compatible = "st,st21nfca_i2c", },
589 {}
590};
591MODULE_DEVICE_TABLE(of, of_st21nfca_i2c_match);
592
593static struct i2c_driver st21nfca_hci_i2c_driver = {
594 .driver = {
595 .name = ST21NFCA_HCI_I2C_DRIVER_NAME,
596 .of_match_table = of_match_ptr(of_st21nfca_i2c_match),
597 .acpi_match_table = ACPI_PTR(st21nfca_hci_i2c_acpi_match),
598 },
599 .probe = st21nfca_hci_i2c_probe,
600 .id_table = st21nfca_hci_i2c_id_table,
601 .remove = st21nfca_hci_i2c_remove,
602};
603module_i2c_driver(st21nfca_hci_i2c_driver);
604
605MODULE_LICENSE("GPL");
606MODULE_DESCRIPTION(DRIVER_DESC);
607