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