blob: a176d1f7bbf3e5a89da7e97fe8104e11d2cd846a [file] [log] [blame]
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001/*
2 * HCI based Driver for NXP PN544 NFC Chip
3 *
4 * Copyright (C) 2012 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/crc-ccitt.h>
22#include <linux/module.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/miscdevice.h>
26#include <linux/interrupt.h>
27#include <linux/gpio.h>
28#include <linux/i2c.h>
29
30#include <linux/nfc.h>
31#include <net/nfc/hci.h>
32#include <net/nfc/shdlc.h>
33
34#include <linux/nfc/pn544.h>
35
36#define DRIVER_DESC "HCI NFC driver for PN544"
37
38#define PN544_HCI_DRIVER_NAME "pn544_hci"
39
40/* Timing restrictions (ms) */
41#define PN544_HCI_RESETVEN_TIME 30
42
43static struct i2c_device_id pn544_hci_id_table[] = {
44 {"pn544", 0},
45 {}
46};
47
48MODULE_DEVICE_TABLE(i2c, pn544_hci_id_table);
49
50#define HCI_MODE 0
51#define FW_MODE 1
52
53/* framing in HCI mode */
54#define PN544_HCI_LLC_LEN 1
55#define PN544_HCI_LLC_CRC 2
56#define PN544_HCI_LLC_LEN_CRC (PN544_HCI_LLC_LEN + PN544_HCI_LLC_CRC)
57#define PN544_HCI_LLC_MIN_SIZE (1 + PN544_HCI_LLC_LEN_CRC)
58#define PN544_HCI_LLC_MAX_PAYLOAD 29
59#define PN544_HCI_LLC_MAX_SIZE (PN544_HCI_LLC_LEN_CRC + 1 + \
60 PN544_HCI_LLC_MAX_PAYLOAD)
61
62enum pn544_state {
63 PN544_ST_COLD,
64 PN544_ST_FW_READY,
65 PN544_ST_READY,
66};
67
68#define FULL_VERSION_LEN 11
69
70/* Proprietary commands */
71#define PN544_WRITE 0x3f
72
73/* Proprietary gates, events, commands and registers */
74
75/* NFC_HCI_RF_READER_A_GATE additional registers and commands */
76#define PN544_RF_READER_A_AUTO_ACTIVATION 0x10
77#define PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION 0x12
78#define PN544_MIFARE_CMD 0x21
79
80/* Commands that apply to all RF readers */
81#define PN544_RF_READER_CMD_PRESENCE_CHECK 0x30
82#define PN544_RF_READER_CMD_ACTIVATE_NEXT 0x32
83
84/* NFC_HCI_ID_MGMT_GATE additional registers */
85#define PN544_ID_MGMT_FULL_VERSION_SW 0x10
86
87#define PN544_RF_READER_ISO15693_GATE 0x12
88
89#define PN544_RF_READER_F_GATE 0x14
90#define PN544_FELICA_ID 0x04
91#define PN544_FELICA_RAW 0x20
92
93#define PN544_RF_READER_JEWEL_GATE 0x15
94#define PN544_JEWEL_RAW_CMD 0x23
95
96#define PN544_RF_READER_NFCIP1_INITIATOR_GATE 0x30
97#define PN544_RF_READER_NFCIP1_TARGET_GATE 0x31
98
99#define PN544_SYS_MGMT_GATE 0x90
100#define PN544_SYS_MGMT_INFO_NOTIFICATION 0x02
101
102#define PN544_POLLING_LOOP_MGMT_GATE 0x94
103#define PN544_PL_RDPHASES 0x06
104#define PN544_PL_EMULATION 0x07
105#define PN544_PL_NFCT_DEACTIVATED 0x09
106
107#define PN544_SWP_MGMT_GATE 0xA0
108
109#define PN544_NFC_WI_MGMT_GATE 0xA1
110
Eric Lapuyadea10d5952012-06-05 14:42:11 +0200111static struct nfc_hci_gate pn544_gates[] = {
112 {NFC_HCI_ADMIN_GATE, NFC_HCI_INVALID_PIPE},
113 {NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
114 {NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
115 {NFC_HCI_LINK_MGMT_GATE, NFC_HCI_INVALID_PIPE},
116 {NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
117 {NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
118 {PN544_SYS_MGMT_GATE, NFC_HCI_INVALID_PIPE},
119 {PN544_SWP_MGMT_GATE, NFC_HCI_INVALID_PIPE},
120 {PN544_POLLING_LOOP_MGMT_GATE, NFC_HCI_INVALID_PIPE},
121 {PN544_NFC_WI_MGMT_GATE, NFC_HCI_INVALID_PIPE},
122 {PN544_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
123 {PN544_RF_READER_JEWEL_GATE, NFC_HCI_INVALID_PIPE},
124 {PN544_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE},
125 {PN544_RF_READER_NFCIP1_INITIATOR_GATE, NFC_HCI_INVALID_PIPE},
126 {PN544_RF_READER_NFCIP1_TARGET_GATE, NFC_HCI_INVALID_PIPE}
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200127};
128
129/* Largest headroom needed for outgoing custom commands */
130#define PN544_CMDS_HEADROOM 2
Waldemar Rymarkiewiczade672082012-09-07 11:08:29 +0200131#define PN544_FRAME_HEADROOM 1
132#define PN544_FRAME_TAILROOM 2
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200133
134struct pn544_hci_info {
135 struct i2c_client *i2c_dev;
136 struct nfc_shdlc *shdlc;
137
138 enum pn544_state state;
139
140 struct mutex info_lock;
141
142 unsigned int gpio_en;
143 unsigned int gpio_irq;
144 unsigned int gpio_fw;
145 unsigned int en_polarity;
146
147 int hard_fault; /*
148 * < 0 if hardware error occured (e.g. i2c err)
149 * and prevents normal operation.
150 */
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200151 int async_cb_type;
152 data_exchange_cb_t async_cb;
153 void *async_cb_context;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200154};
155
156static void pn544_hci_platform_init(struct pn544_hci_info *info)
157{
158 int polarity, retry, ret;
159 char rset_cmd[] = { 0x05, 0xF9, 0x04, 0x00, 0xC3, 0xE5 };
160 int count = sizeof(rset_cmd);
161
162 pr_info(DRIVER_DESC ": %s\n", __func__);
163 dev_info(&info->i2c_dev->dev, "Detecting nfc_en polarity\n");
164
165 /* Disable fw download */
166 gpio_set_value(info->gpio_fw, 0);
167
168 for (polarity = 0; polarity < 2; polarity++) {
169 info->en_polarity = polarity;
170 retry = 3;
171 while (retry--) {
172 /* power off */
173 gpio_set_value(info->gpio_en, !info->en_polarity);
174 usleep_range(10000, 15000);
175
176 /* power on */
177 gpio_set_value(info->gpio_en, info->en_polarity);
178 usleep_range(10000, 15000);
179
180 /* send reset */
181 dev_dbg(&info->i2c_dev->dev, "Sending reset cmd\n");
182 ret = i2c_master_send(info->i2c_dev, rset_cmd, count);
183 if (ret == count) {
184 dev_info(&info->i2c_dev->dev,
185 "nfc_en polarity : active %s\n",
186 (polarity == 0 ? "low" : "high"));
187 goto out;
188 }
189 }
190 }
191
192 dev_err(&info->i2c_dev->dev,
193 "Could not detect nfc_en polarity, fallback to active high\n");
194
195out:
196 gpio_set_value(info->gpio_en, !info->en_polarity);
197}
198
199static int pn544_hci_enable(struct pn544_hci_info *info, int mode)
200{
201 pr_info(DRIVER_DESC ": %s\n", __func__);
202
203 gpio_set_value(info->gpio_fw, 0);
204 gpio_set_value(info->gpio_en, info->en_polarity);
205 usleep_range(10000, 15000);
206
207 return 0;
208}
209
210static void pn544_hci_disable(struct pn544_hci_info *info)
211{
212 pr_info(DRIVER_DESC ": %s\n", __func__);
213
214 gpio_set_value(info->gpio_fw, 0);
215 gpio_set_value(info->gpio_en, !info->en_polarity);
216 usleep_range(10000, 15000);
217
218 gpio_set_value(info->gpio_en, info->en_polarity);
219 usleep_range(10000, 15000);
220
221 gpio_set_value(info->gpio_en, !info->en_polarity);
222 usleep_range(10000, 15000);
223}
224
225static int pn544_hci_i2c_write(struct i2c_client *client, u8 *buf, int len)
226{
227 int r;
228
229 usleep_range(3000, 6000);
230
231 r = i2c_master_send(client, buf, len);
232
233 if (r == -EREMOTEIO) { /* Retry, chip was in standby */
234 usleep_range(6000, 10000);
235 r = i2c_master_send(client, buf, len);
236 }
237
238 if (r >= 0 && r != len)
239 r = -EREMOTEIO;
240
241 return r;
242}
243
244static int check_crc(u8 *buf, int buflen)
245{
Dan Carpenter885ba1d2012-05-18 10:36:47 +0300246 int len;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200247 u16 crc;
248
249 len = buf[0] + 1;
250 crc = crc_ccitt(0xffff, buf, len - 2);
251 crc = ~crc;
252
253 if (buf[len - 2] != (crc & 0xff) || buf[len - 1] != (crc >> 8)) {
254 pr_err(PN544_HCI_DRIVER_NAME ": CRC error 0x%x != 0x%x 0x%x\n",
255 crc, buf[len - 1], buf[len - 2]);
256
257 pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__);
258 print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
259 16, 2, buf, buflen, false);
260 return -EPERM;
261 }
262 return 0;
263}
264
265/*
266 * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
267 * that i2c bus will be flushed and that next read will start on a new frame.
268 * returned skb contains only LLC header and payload.
269 * returns:
270 * -EREMOTEIO : i2c read error (fatal)
271 * -EBADMSG : frame was incorrect and discarded
272 * -ENOMEM : cannot allocate skb, frame dropped
273 */
274static int pn544_hci_i2c_read(struct i2c_client *client, struct sk_buff **skb)
275{
276 int r;
277 u8 len;
278 u8 tmp[PN544_HCI_LLC_MAX_SIZE - 1];
279
280 r = i2c_master_recv(client, &len, 1);
281 if (r != 1) {
282 dev_err(&client->dev, "cannot read len byte\n");
283 return -EREMOTEIO;
284 }
285
286 if ((len < (PN544_HCI_LLC_MIN_SIZE - 1)) ||
287 (len > (PN544_HCI_LLC_MAX_SIZE - 1))) {
288 dev_err(&client->dev, "invalid len byte\n");
289 r = -EBADMSG;
290 goto flush;
291 }
292
293 *skb = alloc_skb(1 + len, GFP_KERNEL);
294 if (*skb == NULL) {
295 r = -ENOMEM;
296 goto flush;
297 }
298
299 *skb_put(*skb, 1) = len;
300
301 r = i2c_master_recv(client, skb_put(*skb, len), len);
302 if (r != len) {
303 kfree_skb(*skb);
304 return -EREMOTEIO;
305 }
306
307 r = check_crc((*skb)->data, (*skb)->len);
308 if (r != 0) {
309 kfree_skb(*skb);
310 r = -EBADMSG;
311 goto flush;
312 }
313
314 skb_pull(*skb, 1);
315 skb_trim(*skb, (*skb)->len - 2);
316
317 usleep_range(3000, 6000);
318
319 return 0;
320
321flush:
322 if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
323 r = -EREMOTEIO;
324
325 usleep_range(3000, 6000);
326
327 return r;
328}
329
330/*
331 * Reads an shdlc frame from the chip. This is not as straightforward as it
332 * seems. There are cases where we could loose the frame start synchronization.
333 * The frame format is len-data-crc, and corruption can occur anywhere while
334 * transiting on i2c bus, such that we could read an invalid len.
335 * In order to recover synchronization with the next frame, we must be sure
336 * to read the real amount of data without using the len byte. We do this by
337 * assuming the following:
338 * - the chip will always present only one single complete frame on the bus
339 * before triggering the interrupt
340 * - the chip will not present a new frame until we have completely read
341 * the previous one (or until we have handled the interrupt).
342 * The tricky case is when we read a corrupted len that is less than the real
343 * len. We must detect this here in order to determine that we need to flush
344 * the bus. This is the reason why we check the crc here.
345 */
346static irqreturn_t pn544_hci_irq_thread_fn(int irq, void *dev_id)
347{
348 struct pn544_hci_info *info = dev_id;
349 struct i2c_client *client = info->i2c_dev;
350 struct sk_buff *skb = NULL;
351 int r;
352
353 BUG_ON(!info);
354 BUG_ON(irq != info->i2c_dev->irq);
355
356 dev_dbg(&client->dev, "IRQ\n");
357
358 if (info->hard_fault != 0)
359 return IRQ_HANDLED;
360
361 r = pn544_hci_i2c_read(client, &skb);
362 if (r == -EREMOTEIO) {
363 info->hard_fault = r;
364
365 nfc_shdlc_recv_frame(info->shdlc, NULL);
366
367 return IRQ_HANDLED;
368 } else if ((r == -ENOMEM) || (r == -EBADMSG)) {
369 return IRQ_HANDLED;
370 }
371
372 nfc_shdlc_recv_frame(info->shdlc, skb);
373
374 return IRQ_HANDLED;
375}
376
377static int pn544_hci_open(struct nfc_shdlc *shdlc)
378{
379 struct pn544_hci_info *info = nfc_shdlc_get_clientdata(shdlc);
380 int r = 0;
381
382 mutex_lock(&info->info_lock);
383
384 if (info->state != PN544_ST_COLD) {
385 r = -EBUSY;
386 goto out;
387 }
388
389 r = pn544_hci_enable(info, HCI_MODE);
390
Eric Lapuyadeeae202a2012-05-30 18:13:06 +0200391 if (r == 0)
392 info->state = PN544_ST_READY;
393
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200394out:
395 mutex_unlock(&info->info_lock);
396 return r;
397}
398
399static void pn544_hci_close(struct nfc_shdlc *shdlc)
400{
401 struct pn544_hci_info *info = nfc_shdlc_get_clientdata(shdlc);
402
403 mutex_lock(&info->info_lock);
404
405 if (info->state == PN544_ST_COLD)
406 goto out;
407
408 pn544_hci_disable(info);
409
Eric Lapuyadeeae202a2012-05-30 18:13:06 +0200410 info->state = PN544_ST_COLD;
411
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200412out:
413 mutex_unlock(&info->info_lock);
414}
415
416static int pn544_hci_ready(struct nfc_shdlc *shdlc)
417{
418 struct nfc_hci_dev *hdev = nfc_shdlc_get_hci_dev(shdlc);
419 struct sk_buff *skb;
420 static struct hw_config {
421 u8 adr[2];
422 u8 value;
423 } hw_config[] = {
424 {{0x9f, 0x9a}, 0x00},
425
426 {{0x98, 0x10}, 0xbc},
427
428 {{0x9e, 0x71}, 0x00},
429
430 {{0x98, 0x09}, 0x00},
431
432 {{0x9e, 0xb4}, 0x00},
433
434 {{0x9e, 0xd9}, 0xff},
435 {{0x9e, 0xda}, 0xff},
436 {{0x9e, 0xdb}, 0x23},
437 {{0x9e, 0xdc}, 0x21},
438 {{0x9e, 0xdd}, 0x22},
439 {{0x9e, 0xde}, 0x24},
440
441 {{0x9c, 0x01}, 0x08},
442
443 {{0x9e, 0xaa}, 0x01},
444
445 {{0x9b, 0xd1}, 0x0d},
446 {{0x9b, 0xd2}, 0x24},
447 {{0x9b, 0xd3}, 0x0a},
448 {{0x9b, 0xd4}, 0x22},
449 {{0x9b, 0xd5}, 0x08},
450 {{0x9b, 0xd6}, 0x1e},
451 {{0x9b, 0xdd}, 0x1c},
452
453 {{0x9b, 0x84}, 0x13},
454 {{0x99, 0x81}, 0x7f},
455 {{0x99, 0x31}, 0x70},
456
457 {{0x98, 0x00}, 0x3f},
458
459 {{0x9f, 0x09}, 0x00},
460
461 {{0x9f, 0x0a}, 0x05},
462
463 {{0x9e, 0xd1}, 0xa1},
464 {{0x99, 0x23}, 0x00},
465
466 {{0x9e, 0x74}, 0x80},
467
468 {{0x9f, 0x28}, 0x10},
469
470 {{0x9f, 0x35}, 0x14},
471
472 {{0x9f, 0x36}, 0x60},
473
474 {{0x9c, 0x31}, 0x00},
475
476 {{0x9c, 0x32}, 0xc8},
477
478 {{0x9c, 0x19}, 0x40},
479
480 {{0x9c, 0x1a}, 0x40},
481
482 {{0x9c, 0x0c}, 0x00},
483
484 {{0x9c, 0x0d}, 0x00},
485
486 {{0x9c, 0x12}, 0x00},
487
488 {{0x9c, 0x13}, 0x00},
489
490 {{0x98, 0xa2}, 0x0e},
491
492 {{0x98, 0x93}, 0x40},
493
494 {{0x98, 0x7d}, 0x02},
495 {{0x98, 0x7e}, 0x00},
496 {{0x9f, 0xc8}, 0x01},
497 };
498 struct hw_config *p = hw_config;
499 int count = ARRAY_SIZE(hw_config);
500 struct sk_buff *res_skb;
501 u8 param[4];
502 int r;
503
504 param[0] = 0;
505 while (count--) {
506 param[1] = p->adr[0];
507 param[2] = p->adr[1];
508 param[3] = p->value;
509
510 r = nfc_hci_send_cmd(hdev, PN544_SYS_MGMT_GATE, PN544_WRITE,
511 param, 4, &res_skb);
512 if (r < 0)
513 return r;
514
515 if (res_skb->len != 1) {
516 kfree_skb(res_skb);
517 return -EPROTO;
518 }
519
520 if (res_skb->data[0] != p->value) {
521 kfree_skb(res_skb);
522 return -EIO;
523 }
524
525 kfree_skb(res_skb);
526
527 p++;
528 }
529
530 param[0] = NFC_HCI_UICC_HOST_ID;
531 r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
532 NFC_HCI_ADMIN_WHITELIST, param, 1);
533 if (r < 0)
534 return r;
535
536 param[0] = 0x3d;
537 r = nfc_hci_set_param(hdev, PN544_SYS_MGMT_GATE,
538 PN544_SYS_MGMT_INFO_NOTIFICATION, param, 1);
539 if (r < 0)
540 return r;
541
542 param[0] = 0x0;
543 r = nfc_hci_set_param(hdev, NFC_HCI_RF_READER_A_GATE,
544 PN544_RF_READER_A_AUTO_ACTIVATION, param, 1);
545 if (r < 0)
546 return r;
547
548 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
549 NFC_HCI_EVT_END_OPERATION, NULL, 0);
550 if (r < 0)
551 return r;
552
553 param[0] = 0x1;
554 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
555 PN544_PL_NFCT_DEACTIVATED, param, 1);
556 if (r < 0)
557 return r;
558
559 param[0] = 0x0;
560 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
561 PN544_PL_RDPHASES, param, 1);
562 if (r < 0)
563 return r;
564
565 r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
566 PN544_ID_MGMT_FULL_VERSION_SW, &skb);
567 if (r < 0)
568 return r;
569
570 if (skb->len != FULL_VERSION_LEN) {
571 kfree_skb(skb);
572 return -EINVAL;
573 }
574
575 print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
576 DUMP_PREFIX_NONE, 16, 1,
577 skb->data, FULL_VERSION_LEN, false);
578
579 kfree_skb(skb);
580
581 return 0;
582}
583
Waldemar Rymarkiewiczade672082012-09-07 11:08:29 +0200584static void pn544_hci_add_len_crc(struct sk_buff *skb)
585{
586 u16 crc;
587 int len;
588
589 len = skb->len + 2;
590 *skb_push(skb, 1) = len;
591
592 crc = crc_ccitt(0xffff, skb->data, skb->len);
593 crc = ~crc;
594 *skb_put(skb, 1) = crc & 0xff;
595 *skb_put(skb, 1) = crc >> 8;
596}
597
598static void pn544_hci_remove_len_crc(struct sk_buff *skb)
599{
600 skb_pull(skb, PN544_FRAME_HEADROOM);
601 skb_trim(skb, PN544_FRAME_TAILROOM);
602}
603
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200604static int pn544_hci_xmit(struct nfc_shdlc *shdlc, struct sk_buff *skb)
605{
606 struct pn544_hci_info *info = nfc_shdlc_get_clientdata(shdlc);
607 struct i2c_client *client = info->i2c_dev;
Waldemar Rymarkiewiczade672082012-09-07 11:08:29 +0200608 int r;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200609
610 if (info->hard_fault != 0)
611 return info->hard_fault;
612
Waldemar Rymarkiewiczade672082012-09-07 11:08:29 +0200613 pn544_hci_add_len_crc(skb);
614 r = pn544_hci_i2c_write(client, skb->data, skb->len);
615 pn544_hci_remove_len_crc(skb);
616
617 return r;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200618}
619
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200620static int pn544_hci_start_poll(struct nfc_shdlc *shdlc,
621 u32 im_protocols, u32 tm_protocols)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200622{
623 struct nfc_hci_dev *hdev = nfc_shdlc_get_hci_dev(shdlc);
624 u8 phases = 0;
625 int r;
626 u8 duration[2];
627 u8 activated;
628
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200629 pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
630 __func__, im_protocols, tm_protocols);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200631
632 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
633 NFC_HCI_EVT_END_OPERATION, NULL, 0);
634 if (r < 0)
635 return r;
636
637 duration[0] = 0x18;
638 duration[1] = 0x6a;
639 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
640 PN544_PL_EMULATION, duration, 2);
641 if (r < 0)
642 return r;
643
644 activated = 0;
645 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
646 PN544_PL_NFCT_DEACTIVATED, &activated, 1);
647 if (r < 0)
648 return r;
649
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200650 if (im_protocols & (NFC_PROTO_ISO14443_MASK | NFC_PROTO_MIFARE_MASK |
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200651 NFC_PROTO_JEWEL_MASK))
652 phases |= 1; /* Type A */
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200653 if (im_protocols & NFC_PROTO_FELICA_MASK) {
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200654 phases |= (1 << 2); /* Type F 212 */
655 phases |= (1 << 3); /* Type F 424 */
656 }
657
658 phases |= (1 << 5); /* NFC active */
659
660 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
661 PN544_PL_RDPHASES, &phases, 1);
662 if (r < 0)
663 return r;
664
665 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
666 NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
667 if (r < 0)
668 nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
669 NFC_HCI_EVT_END_OPERATION, NULL, 0);
670
671 return r;
672}
673
674static int pn544_hci_target_from_gate(struct nfc_shdlc *shdlc, u8 gate,
675 struct nfc_target *target)
676{
677 switch (gate) {
678 case PN544_RF_READER_F_GATE:
679 target->supported_protocols = NFC_PROTO_FELICA_MASK;
680 break;
681 case PN544_RF_READER_JEWEL_GATE:
682 target->supported_protocols = NFC_PROTO_JEWEL_MASK;
683 target->sens_res = 0x0c00;
684 break;
685 default:
686 return -EPROTO;
687 }
688
689 return 0;
690}
691
692static int pn544_hci_complete_target_discovered(struct nfc_shdlc *shdlc,
693 u8 gate,
694 struct nfc_target *target)
695{
696 struct nfc_hci_dev *hdev = nfc_shdlc_get_hci_dev(shdlc);
697 struct sk_buff *uid_skb;
698 int r = 0;
699
700 if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
701 if (target->nfcid1_len != 4 && target->nfcid1_len != 7 &&
702 target->nfcid1_len != 10)
703 return -EPROTO;
704
705 r = nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
706 PN544_RF_READER_CMD_ACTIVATE_NEXT,
707 target->nfcid1, target->nfcid1_len, NULL);
708 } else if (target->supported_protocols & NFC_PROTO_FELICA_MASK) {
709 r = nfc_hci_get_param(hdev, PN544_RF_READER_F_GATE,
710 PN544_FELICA_ID, &uid_skb);
711 if (r < 0)
712 return r;
713
714 if (uid_skb->len != 8) {
715 kfree_skb(uid_skb);
716 return -EPROTO;
717 }
718
719 r = nfc_hci_send_cmd(hdev, PN544_RF_READER_F_GATE,
720 PN544_RF_READER_CMD_ACTIVATE_NEXT,
721 uid_skb->data, uid_skb->len, NULL);
722 kfree_skb(uid_skb);
723 } else if (target->supported_protocols & NFC_PROTO_ISO14443_MASK) {
724 /*
725 * TODO: maybe other ISO 14443 require some kind of continue
726 * activation, but for now we've seen only this one below.
727 */
728 if (target->sens_res == 0x4403) /* Type 4 Mifare DESFire */
729 r = nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
730 PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION,
731 NULL, 0, NULL);
732 }
733
734 return r;
735}
736
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200737#define PN544_CB_TYPE_READER_F 1
738
739static void pn544_hci_data_exchange_cb(void *context, struct sk_buff *skb,
740 int err)
741{
742 struct pn544_hci_info *info = context;
743
744 switch (info->async_cb_type) {
745 case PN544_CB_TYPE_READER_F:
746 if (err == 0)
747 skb_pull(skb, 1);
748 info->async_cb(info->async_cb_context, skb, err);
749 break;
750 default:
751 if (err == 0)
752 kfree_skb(skb);
753 break;
754 }
755}
756
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200757#define MIFARE_CMD_AUTH_KEY_A 0x60
758#define MIFARE_CMD_AUTH_KEY_B 0x61
759#define MIFARE_CMD_HEADER 2
760#define MIFARE_UID_LEN 4
761#define MIFARE_KEY_LEN 6
762#define MIFARE_CMD_LEN 12
763/*
764 * Returns:
765 * <= 0: driver handled the data exchange
766 * 1: driver doesn't especially handle, please do standard processing
767 */
768static int pn544_hci_data_exchange(struct nfc_shdlc *shdlc,
769 struct nfc_target *target,
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200770 struct sk_buff *skb, data_exchange_cb_t cb,
771 void *cb_context)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200772{
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200773 struct pn544_hci_info *info = nfc_shdlc_get_clientdata(shdlc);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200774 struct nfc_hci_dev *hdev = nfc_shdlc_get_hci_dev(shdlc);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200775
776 pr_info(DRIVER_DESC ": %s for gate=%d\n", __func__,
777 target->hci_reader_gate);
778
779 switch (target->hci_reader_gate) {
780 case NFC_HCI_RF_READER_A_GATE:
781 if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
782 /*
783 * It seems that pn544 is inverting key and UID for
784 * MIFARE authentication commands.
785 */
786 if (skb->len == MIFARE_CMD_LEN &&
787 (skb->data[0] == MIFARE_CMD_AUTH_KEY_A ||
788 skb->data[0] == MIFARE_CMD_AUTH_KEY_B)) {
789 u8 uid[MIFARE_UID_LEN];
790 u8 *data = skb->data + MIFARE_CMD_HEADER;
791
792 memcpy(uid, data + MIFARE_KEY_LEN,
793 MIFARE_UID_LEN);
794 memmove(data + MIFARE_UID_LEN, data,
795 MIFARE_KEY_LEN);
796 memcpy(data, uid, MIFARE_UID_LEN);
797 }
798
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200799 return nfc_hci_send_cmd_async(hdev,
800 target->hci_reader_gate,
801 PN544_MIFARE_CMD,
802 skb->data, skb->len,
803 cb, cb_context);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200804 } else
805 return 1;
806 case PN544_RF_READER_F_GATE:
807 *skb_push(skb, 1) = 0;
808 *skb_push(skb, 1) = 0;
809
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200810 info->async_cb_type = PN544_CB_TYPE_READER_F;
811 info->async_cb = cb;
812 info->async_cb_context = cb_context;
813
814 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
815 PN544_FELICA_RAW, skb->data,
816 skb->len,
817 pn544_hci_data_exchange_cb, info);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200818 case PN544_RF_READER_JEWEL_GATE:
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200819 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
820 PN544_JEWEL_RAW_CMD, skb->data,
821 skb->len, cb, cb_context);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200822 default:
823 return 1;
824 }
825}
826
827static int pn544_hci_check_presence(struct nfc_shdlc *shdlc,
828 struct nfc_target *target)
829{
830 struct nfc_hci_dev *hdev = nfc_shdlc_get_hci_dev(shdlc);
831
832 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
833 PN544_RF_READER_CMD_PRESENCE_CHECK,
834 NULL, 0, NULL);
835}
836
837static struct nfc_shdlc_ops pn544_shdlc_ops = {
838 .open = pn544_hci_open,
839 .close = pn544_hci_close,
840 .hci_ready = pn544_hci_ready,
841 .xmit = pn544_hci_xmit,
842 .start_poll = pn544_hci_start_poll,
843 .target_from_gate = pn544_hci_target_from_gate,
844 .complete_target_discovered = pn544_hci_complete_target_discovered,
845 .data_exchange = pn544_hci_data_exchange,
846 .check_presence = pn544_hci_check_presence,
847};
848
849static int __devinit pn544_hci_probe(struct i2c_client *client,
850 const struct i2c_device_id *id)
851{
852 struct pn544_hci_info *info;
853 struct pn544_nfc_platform_data *pdata;
854 int r = 0;
855 u32 protocols;
856 struct nfc_hci_init_data init_data;
857
858 dev_dbg(&client->dev, "%s\n", __func__);
859 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
860
861 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
862 dev_err(&client->dev, "Need I2C_FUNC_I2C\n");
863 return -ENODEV;
864 }
865
866 info = kzalloc(sizeof(struct pn544_hci_info), GFP_KERNEL);
867 if (!info) {
868 dev_err(&client->dev,
869 "Cannot allocate memory for pn544_hci_info.\n");
870 r = -ENOMEM;
871 goto err_info_alloc;
872 }
873
874 info->i2c_dev = client;
875 info->state = PN544_ST_COLD;
876 mutex_init(&info->info_lock);
877 i2c_set_clientdata(client, info);
878
879 pdata = client->dev.platform_data;
880 if (pdata == NULL) {
881 dev_err(&client->dev, "No platform data\n");
882 r = -EINVAL;
883 goto err_pdata;
884 }
885
886 if (pdata->request_resources == NULL) {
887 dev_err(&client->dev, "request_resources() missing\n");
888 r = -EINVAL;
889 goto err_pdata;
890 }
891
892 r = pdata->request_resources(client);
893 if (r) {
894 dev_err(&client->dev, "Cannot get platform resources\n");
895 goto err_pdata;
896 }
897
898 info->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE);
899 info->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET);
900 info->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ);
901
902 pn544_hci_platform_init(info);
903
904 r = request_threaded_irq(client->irq, NULL, pn544_hci_irq_thread_fn,
Samuel Ortizf2ce3982012-08-25 00:40:16 +0200905 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
906 PN544_HCI_DRIVER_NAME, info);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200907 if (r < 0) {
908 dev_err(&client->dev, "Unable to register IRQ handler\n");
909 goto err_rti;
910 }
911
Eric Lapuyadea10d5952012-06-05 14:42:11 +0200912 init_data.gate_count = ARRAY_SIZE(pn544_gates);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200913
Eric Lapuyadea10d5952012-06-05 14:42:11 +0200914 memcpy(init_data.gates, pn544_gates, sizeof(pn544_gates));
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200915
916 /*
917 * TODO: Session id must include the driver name + some bus addr
918 * persistent info to discriminate 2 identical chips
919 */
920 strcpy(init_data.session_id, "ID544HCI");
921
922 protocols = NFC_PROTO_JEWEL_MASK |
923 NFC_PROTO_MIFARE_MASK |
924 NFC_PROTO_FELICA_MASK |
925 NFC_PROTO_ISO14443_MASK |
Samuel Ortiz01d719a2012-07-04 00:14:04 +0200926 NFC_PROTO_ISO14443_B_MASK |
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200927 NFC_PROTO_NFC_DEP_MASK;
928
929 info->shdlc = nfc_shdlc_allocate(&pn544_shdlc_ops,
930 &init_data, protocols,
Waldemar Rymarkiewiczade672082012-09-07 11:08:29 +0200931 PN544_FRAME_HEADROOM + PN544_CMDS_HEADROOM,
932 PN544_FRAME_TAILROOM,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200933 PN544_HCI_LLC_MAX_PAYLOAD,
934 dev_name(&client->dev));
935 if (!info->shdlc) {
936 dev_err(&client->dev, "Cannot allocate nfc shdlc.\n");
937 r = -ENOMEM;
938 goto err_allocshdlc;
939 }
940
941 nfc_shdlc_set_clientdata(info->shdlc, info);
942
943 return 0;
944
945err_allocshdlc:
946 free_irq(client->irq, info);
947
948err_rti:
949 if (pdata->free_resources != NULL)
950 pdata->free_resources();
951
952err_pdata:
953 kfree(info);
954
955err_info_alloc:
956 return r;
957}
958
959static __devexit int pn544_hci_remove(struct i2c_client *client)
960{
961 struct pn544_hci_info *info = i2c_get_clientdata(client);
962 struct pn544_nfc_platform_data *pdata = client->dev.platform_data;
963
964 dev_dbg(&client->dev, "%s\n", __func__);
965
966 nfc_shdlc_free(info->shdlc);
967
968 if (info->state != PN544_ST_COLD) {
969 if (pdata->disable)
970 pdata->disable();
971 }
972
973 free_irq(client->irq, info);
974 if (pdata->free_resources)
975 pdata->free_resources();
976
977 kfree(info);
978
979 return 0;
980}
981
982static struct i2c_driver pn544_hci_driver = {
983 .driver = {
984 .name = PN544_HCI_DRIVER_NAME,
985 },
986 .probe = pn544_hci_probe,
987 .id_table = pn544_hci_id_table,
988 .remove = __devexit_p(pn544_hci_remove),
989};
990
991static int __init pn544_hci_init(void)
992{
993 int r;
994
995 pr_debug(DRIVER_DESC ": %s\n", __func__);
996
997 r = i2c_add_driver(&pn544_hci_driver);
998 if (r) {
999 pr_err(PN544_HCI_DRIVER_NAME ": driver registration failed\n");
1000 return r;
1001 }
1002
1003 return 0;
1004}
1005
1006static void __exit pn544_hci_exit(void)
1007{
1008 i2c_del_driver(&pn544_hci_driver);
1009}
1010
1011module_init(pn544_hci_init);
1012module_exit(pn544_hci_exit);
1013
1014MODULE_LICENSE("GPL");
1015MODULE_DESCRIPTION(DRIVER_DESC);