2 * HCI based Driver for NXP PN544 NFC Chip
4 * Copyright (C) 2012 Intel Corporation. All rights reserved.
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.
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.
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.
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>
30 #include <linux/nfc.h>
31 #include <net/nfc/hci.h>
32 #include <net/nfc/shdlc.h>
34 #include <linux/nfc/pn544.h>
36 #define DRIVER_DESC "HCI NFC driver for PN544"
38 #define PN544_HCI_DRIVER_NAME "pn544_hci"
40 /* Timing restrictions (ms) */
41 #define PN544_HCI_RESETVEN_TIME 30
43 static struct i2c_device_id pn544_hci_id_table
[] = {
48 MODULE_DEVICE_TABLE(i2c
, pn544_hci_id_table
);
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)
68 #define FULL_VERSION_LEN 11
70 /* Proprietary commands */
71 #define PN544_WRITE 0x3f
73 /* Proprietary gates, events, commands and registers */
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
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
84 /* NFC_HCI_ID_MGMT_GATE additional registers */
85 #define PN544_ID_MGMT_FULL_VERSION_SW 0x10
87 #define PN544_RF_READER_ISO15693_GATE 0x12
89 #define PN544_RF_READER_F_GATE 0x14
90 #define PN544_FELICA_ID 0x04
91 #define PN544_FELICA_RAW 0x20
93 #define PN544_RF_READER_JEWEL_GATE 0x15
94 #define PN544_JEWEL_RAW_CMD 0x23
96 #define PN544_RF_READER_NFCIP1_INITIATOR_GATE 0x30
97 #define PN544_RF_READER_NFCIP1_TARGET_GATE 0x31
99 #define PN544_SYS_MGMT_GATE 0x90
100 #define PN544_SYS_MGMT_INFO_NOTIFICATION 0x02
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
107 #define PN544_SWP_MGMT_GATE 0xA0
109 #define PN544_NFC_WI_MGMT_GATE 0xA1
111 static 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
}
129 /* Largest headroom needed for outgoing custom commands */
130 #define PN544_CMDS_HEADROOM 2
131 #define PN544_FRAME_HEADROOM 1
132 #define PN544_FRAME_TAILROOM 2
134 struct pn544_hci_info
{
135 struct i2c_client
*i2c_dev
;
136 struct nfc_shdlc
*shdlc
;
138 enum pn544_state state
;
140 struct mutex info_lock
;
142 unsigned int gpio_en
;
143 unsigned int gpio_irq
;
144 unsigned int gpio_fw
;
145 unsigned int en_polarity
;
148 * < 0 if hardware error occured (e.g. i2c err)
149 * and prevents normal operation.
153 static void pn544_hci_platform_init(struct pn544_hci_info
*info
)
155 int polarity
, retry
, ret
;
156 char rset_cmd
[] = { 0x05, 0xF9, 0x04, 0x00, 0xC3, 0xE5 };
157 int count
= sizeof(rset_cmd
);
159 pr_info(DRIVER_DESC
": %s\n", __func__
);
160 dev_info(&info
->i2c_dev
->dev
, "Detecting nfc_en polarity\n");
162 /* Disable fw download */
163 gpio_set_value(info
->gpio_fw
, 0);
165 for (polarity
= 0; polarity
< 2; polarity
++) {
166 info
->en_polarity
= polarity
;
170 gpio_set_value(info
->gpio_en
, !info
->en_polarity
);
171 usleep_range(10000, 15000);
174 gpio_set_value(info
->gpio_en
, info
->en_polarity
);
175 usleep_range(10000, 15000);
178 dev_dbg(&info
->i2c_dev
->dev
, "Sending reset cmd\n");
179 ret
= i2c_master_send(info
->i2c_dev
, rset_cmd
, count
);
181 dev_info(&info
->i2c_dev
->dev
,
182 "nfc_en polarity : active %s\n",
183 (polarity
== 0 ? "low" : "high"));
189 dev_err(&info
->i2c_dev
->dev
,
190 "Could not detect nfc_en polarity, fallback to active high\n");
193 gpio_set_value(info
->gpio_en
, !info
->en_polarity
);
196 static int pn544_hci_enable(struct pn544_hci_info
*info
, int mode
)
198 pr_info(DRIVER_DESC
": %s\n", __func__
);
200 gpio_set_value(info
->gpio_fw
, 0);
201 gpio_set_value(info
->gpio_en
, info
->en_polarity
);
202 usleep_range(10000, 15000);
207 static void pn544_hci_disable(struct pn544_hci_info
*info
)
209 pr_info(DRIVER_DESC
": %s\n", __func__
);
211 gpio_set_value(info
->gpio_fw
, 0);
212 gpio_set_value(info
->gpio_en
, !info
->en_polarity
);
213 usleep_range(10000, 15000);
215 gpio_set_value(info
->gpio_en
, info
->en_polarity
);
216 usleep_range(10000, 15000);
218 gpio_set_value(info
->gpio_en
, !info
->en_polarity
);
219 usleep_range(10000, 15000);
222 static int pn544_hci_i2c_write(struct i2c_client
*client
, u8
*buf
, int len
)
226 usleep_range(3000, 6000);
228 r
= i2c_master_send(client
, buf
, len
);
230 if (r
== -EREMOTEIO
) { /* Retry, chip was in standby */
231 usleep_range(6000, 10000);
232 r
= i2c_master_send(client
, buf
, len
);
235 if (r
>= 0 && r
!= len
)
241 static int check_crc(u8
*buf
, int buflen
)
247 crc
= crc_ccitt(0xffff, buf
, len
- 2);
250 if (buf
[len
- 2] != (crc
& 0xff) || buf
[len
- 1] != (crc
>> 8)) {
251 pr_err(PN544_HCI_DRIVER_NAME
": CRC error 0x%x != 0x%x 0x%x\n",
252 crc
, buf
[len
- 1], buf
[len
- 2]);
254 pr_info(DRIVER_DESC
": %s : BAD CRC\n", __func__
);
255 print_hex_dump(KERN_DEBUG
, "crc: ", DUMP_PREFIX_NONE
,
256 16, 2, buf
, buflen
, false);
263 * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
264 * that i2c bus will be flushed and that next read will start on a new frame.
265 * returned skb contains only LLC header and payload.
267 * -EREMOTEIO : i2c read error (fatal)
268 * -EBADMSG : frame was incorrect and discarded
269 * -ENOMEM : cannot allocate skb, frame dropped
271 static int pn544_hci_i2c_read(struct i2c_client
*client
, struct sk_buff
**skb
)
275 u8 tmp
[PN544_HCI_LLC_MAX_SIZE
- 1];
277 r
= i2c_master_recv(client
, &len
, 1);
279 dev_err(&client
->dev
, "cannot read len byte\n");
283 if ((len
< (PN544_HCI_LLC_MIN_SIZE
- 1)) ||
284 (len
> (PN544_HCI_LLC_MAX_SIZE
- 1))) {
285 dev_err(&client
->dev
, "invalid len byte\n");
290 *skb
= alloc_skb(1 + len
, GFP_KERNEL
);
296 *skb_put(*skb
, 1) = len
;
298 r
= i2c_master_recv(client
, skb_put(*skb
, len
), len
);
304 r
= check_crc((*skb
)->data
, (*skb
)->len
);
312 skb_trim(*skb
, (*skb
)->len
- 2);
314 usleep_range(3000, 6000);
319 if (i2c_master_recv(client
, tmp
, sizeof(tmp
)) < 0)
322 usleep_range(3000, 6000);
328 * Reads an shdlc frame from the chip. This is not as straightforward as it
329 * seems. There are cases where we could loose the frame start synchronization.
330 * The frame format is len-data-crc, and corruption can occur anywhere while
331 * transiting on i2c bus, such that we could read an invalid len.
332 * In order to recover synchronization with the next frame, we must be sure
333 * to read the real amount of data without using the len byte. We do this by
334 * assuming the following:
335 * - the chip will always present only one single complete frame on the bus
336 * before triggering the interrupt
337 * - the chip will not present a new frame until we have completely read
338 * the previous one (or until we have handled the interrupt).
339 * The tricky case is when we read a corrupted len that is less than the real
340 * len. We must detect this here in order to determine that we need to flush
341 * the bus. This is the reason why we check the crc here.
343 static irqreturn_t
pn544_hci_irq_thread_fn(int irq
, void *dev_id
)
345 struct pn544_hci_info
*info
= dev_id
;
346 struct i2c_client
*client
= info
->i2c_dev
;
347 struct sk_buff
*skb
= NULL
;
351 BUG_ON(irq
!= info
->i2c_dev
->irq
);
353 dev_dbg(&client
->dev
, "IRQ\n");
355 if (info
->hard_fault
!= 0)
358 r
= pn544_hci_i2c_read(client
, &skb
);
359 if (r
== -EREMOTEIO
) {
360 info
->hard_fault
= r
;
362 nfc_shdlc_recv_frame(info
->shdlc
, NULL
);
365 } else if ((r
== -ENOMEM
) || (r
== -EBADMSG
)) {
369 nfc_shdlc_recv_frame(info
->shdlc
, skb
);
374 static int pn544_hci_open(struct nfc_shdlc
*shdlc
)
376 struct pn544_hci_info
*info
= nfc_shdlc_get_clientdata(shdlc
);
379 mutex_lock(&info
->info_lock
);
381 if (info
->state
!= PN544_ST_COLD
) {
386 r
= pn544_hci_enable(info
, HCI_MODE
);
389 info
->state
= PN544_ST_READY
;
392 mutex_unlock(&info
->info_lock
);
396 static void pn544_hci_close(struct nfc_shdlc
*shdlc
)
398 struct pn544_hci_info
*info
= nfc_shdlc_get_clientdata(shdlc
);
400 mutex_lock(&info
->info_lock
);
402 if (info
->state
== PN544_ST_COLD
)
405 pn544_hci_disable(info
);
407 info
->state
= PN544_ST_COLD
;
410 mutex_unlock(&info
->info_lock
);
413 static int pn544_hci_ready(struct nfc_shdlc
*shdlc
)
415 struct nfc_hci_dev
*hdev
= nfc_shdlc_get_hci_dev(shdlc
);
417 static struct hw_config
{
421 {{0x9f, 0x9a}, 0x00},
423 {{0x98, 0x10}, 0xbc},
425 {{0x9e, 0x71}, 0x00},
427 {{0x98, 0x09}, 0x00},
429 {{0x9e, 0xb4}, 0x00},
431 {{0x9e, 0xd9}, 0xff},
432 {{0x9e, 0xda}, 0xff},
433 {{0x9e, 0xdb}, 0x23},
434 {{0x9e, 0xdc}, 0x21},
435 {{0x9e, 0xdd}, 0x22},
436 {{0x9e, 0xde}, 0x24},
438 {{0x9c, 0x01}, 0x08},
440 {{0x9e, 0xaa}, 0x01},
442 {{0x9b, 0xd1}, 0x0d},
443 {{0x9b, 0xd2}, 0x24},
444 {{0x9b, 0xd3}, 0x0a},
445 {{0x9b, 0xd4}, 0x22},
446 {{0x9b, 0xd5}, 0x08},
447 {{0x9b, 0xd6}, 0x1e},
448 {{0x9b, 0xdd}, 0x1c},
450 {{0x9b, 0x84}, 0x13},
451 {{0x99, 0x81}, 0x7f},
452 {{0x99, 0x31}, 0x70},
454 {{0x98, 0x00}, 0x3f},
456 {{0x9f, 0x09}, 0x00},
458 {{0x9f, 0x0a}, 0x05},
460 {{0x9e, 0xd1}, 0xa1},
461 {{0x99, 0x23}, 0x00},
463 {{0x9e, 0x74}, 0x80},
465 {{0x9f, 0x28}, 0x10},
467 {{0x9f, 0x35}, 0x14},
469 {{0x9f, 0x36}, 0x60},
471 {{0x9c, 0x31}, 0x00},
473 {{0x9c, 0x32}, 0xc8},
475 {{0x9c, 0x19}, 0x40},
477 {{0x9c, 0x1a}, 0x40},
479 {{0x9c, 0x0c}, 0x00},
481 {{0x9c, 0x0d}, 0x00},
483 {{0x9c, 0x12}, 0x00},
485 {{0x9c, 0x13}, 0x00},
487 {{0x98, 0xa2}, 0x0e},
489 {{0x98, 0x93}, 0x40},
491 {{0x98, 0x7d}, 0x02},
492 {{0x98, 0x7e}, 0x00},
493 {{0x9f, 0xc8}, 0x01},
495 struct hw_config
*p
= hw_config
;
496 int count
= ARRAY_SIZE(hw_config
);
497 struct sk_buff
*res_skb
;
503 param
[1] = p
->adr
[0];
504 param
[2] = p
->adr
[1];
507 r
= nfc_hci_send_cmd(hdev
, PN544_SYS_MGMT_GATE
, PN544_WRITE
,
512 if (res_skb
->len
!= 1) {
517 if (res_skb
->data
[0] != p
->value
) {
527 param
[0] = NFC_HCI_UICC_HOST_ID
;
528 r
= nfc_hci_set_param(hdev
, NFC_HCI_ADMIN_GATE
,
529 NFC_HCI_ADMIN_WHITELIST
, param
, 1);
534 r
= nfc_hci_set_param(hdev
, PN544_SYS_MGMT_GATE
,
535 PN544_SYS_MGMT_INFO_NOTIFICATION
, param
, 1);
540 r
= nfc_hci_set_param(hdev
, NFC_HCI_RF_READER_A_GATE
,
541 PN544_RF_READER_A_AUTO_ACTIVATION
, param
, 1);
545 r
= nfc_hci_send_event(hdev
, NFC_HCI_RF_READER_A_GATE
,
546 NFC_HCI_EVT_END_OPERATION
, NULL
, 0);
551 r
= nfc_hci_set_param(hdev
, PN544_POLLING_LOOP_MGMT_GATE
,
552 PN544_PL_NFCT_DEACTIVATED
, param
, 1);
557 r
= nfc_hci_set_param(hdev
, PN544_POLLING_LOOP_MGMT_GATE
,
558 PN544_PL_RDPHASES
, param
, 1);
562 r
= nfc_hci_get_param(hdev
, NFC_HCI_ID_MGMT_GATE
,
563 PN544_ID_MGMT_FULL_VERSION_SW
, &skb
);
567 if (skb
->len
!= FULL_VERSION_LEN
) {
572 print_hex_dump(KERN_DEBUG
, "FULL VERSION SOFTWARE INFO: ",
573 DUMP_PREFIX_NONE
, 16, 1,
574 skb
->data
, FULL_VERSION_LEN
, false);
581 static void pn544_hci_add_len_crc(struct sk_buff
*skb
)
587 *skb_push(skb
, 1) = len
;
589 crc
= crc_ccitt(0xffff, skb
->data
, skb
->len
);
591 *skb_put(skb
, 1) = crc
& 0xff;
592 *skb_put(skb
, 1) = crc
>> 8;
595 static void pn544_hci_remove_len_crc(struct sk_buff
*skb
)
597 skb_pull(skb
, PN544_FRAME_HEADROOM
);
598 skb_trim(skb
, PN544_FRAME_TAILROOM
);
601 static int pn544_hci_xmit(struct nfc_shdlc
*shdlc
, struct sk_buff
*skb
)
603 struct pn544_hci_info
*info
= nfc_shdlc_get_clientdata(shdlc
);
604 struct i2c_client
*client
= info
->i2c_dev
;
607 if (info
->hard_fault
!= 0)
608 return info
->hard_fault
;
610 pn544_hci_add_len_crc(skb
);
611 r
= pn544_hci_i2c_write(client
, skb
->data
, skb
->len
);
612 pn544_hci_remove_len_crc(skb
);
617 static int pn544_hci_start_poll(struct nfc_shdlc
*shdlc
,
618 u32 im_protocols
, u32 tm_protocols
)
620 struct nfc_hci_dev
*hdev
= nfc_shdlc_get_hci_dev(shdlc
);
626 pr_info(DRIVER_DESC
": %s protocols 0x%x 0x%x\n",
627 __func__
, im_protocols
, tm_protocols
);
629 r
= nfc_hci_send_event(hdev
, NFC_HCI_RF_READER_A_GATE
,
630 NFC_HCI_EVT_END_OPERATION
, NULL
, 0);
636 r
= nfc_hci_set_param(hdev
, PN544_POLLING_LOOP_MGMT_GATE
,
637 PN544_PL_EMULATION
, duration
, 2);
642 r
= nfc_hci_set_param(hdev
, PN544_POLLING_LOOP_MGMT_GATE
,
643 PN544_PL_NFCT_DEACTIVATED
, &activated
, 1);
647 if (im_protocols
& (NFC_PROTO_ISO14443_MASK
| NFC_PROTO_MIFARE_MASK
|
648 NFC_PROTO_JEWEL_MASK
))
649 phases
|= 1; /* Type A */
650 if (im_protocols
& NFC_PROTO_FELICA_MASK
) {
651 phases
|= (1 << 2); /* Type F 212 */
652 phases
|= (1 << 3); /* Type F 424 */
655 phases
|= (1 << 5); /* NFC active */
657 r
= nfc_hci_set_param(hdev
, PN544_POLLING_LOOP_MGMT_GATE
,
658 PN544_PL_RDPHASES
, &phases
, 1);
662 r
= nfc_hci_send_event(hdev
, NFC_HCI_RF_READER_A_GATE
,
663 NFC_HCI_EVT_READER_REQUESTED
, NULL
, 0);
665 nfc_hci_send_event(hdev
, NFC_HCI_RF_READER_A_GATE
,
666 NFC_HCI_EVT_END_OPERATION
, NULL
, 0);
671 static int pn544_hci_target_from_gate(struct nfc_shdlc
*shdlc
, u8 gate
,
672 struct nfc_target
*target
)
675 case PN544_RF_READER_F_GATE
:
676 target
->supported_protocols
= NFC_PROTO_FELICA_MASK
;
678 case PN544_RF_READER_JEWEL_GATE
:
679 target
->supported_protocols
= NFC_PROTO_JEWEL_MASK
;
680 target
->sens_res
= 0x0c00;
689 static int pn544_hci_complete_target_discovered(struct nfc_shdlc
*shdlc
,
691 struct nfc_target
*target
)
693 struct nfc_hci_dev
*hdev
= nfc_shdlc_get_hci_dev(shdlc
);
694 struct sk_buff
*uid_skb
;
697 if (target
->supported_protocols
& NFC_PROTO_MIFARE_MASK
) {
698 if (target
->nfcid1_len
!= 4 && target
->nfcid1_len
!= 7 &&
699 target
->nfcid1_len
!= 10)
702 r
= nfc_hci_send_cmd(hdev
, NFC_HCI_RF_READER_A_GATE
,
703 PN544_RF_READER_CMD_ACTIVATE_NEXT
,
704 target
->nfcid1
, target
->nfcid1_len
, NULL
);
705 } else if (target
->supported_protocols
& NFC_PROTO_FELICA_MASK
) {
706 r
= nfc_hci_get_param(hdev
, PN544_RF_READER_F_GATE
,
707 PN544_FELICA_ID
, &uid_skb
);
711 if (uid_skb
->len
!= 8) {
716 r
= nfc_hci_send_cmd(hdev
, PN544_RF_READER_F_GATE
,
717 PN544_RF_READER_CMD_ACTIVATE_NEXT
,
718 uid_skb
->data
, uid_skb
->len
, NULL
);
720 } else if (target
->supported_protocols
& NFC_PROTO_ISO14443_MASK
) {
722 * TODO: maybe other ISO 14443 require some kind of continue
723 * activation, but for now we've seen only this one below.
725 if (target
->sens_res
== 0x4403) /* Type 4 Mifare DESFire */
726 r
= nfc_hci_send_cmd(hdev
, NFC_HCI_RF_READER_A_GATE
,
727 PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION
,
734 #define MIFARE_CMD_AUTH_KEY_A 0x60
735 #define MIFARE_CMD_AUTH_KEY_B 0x61
736 #define MIFARE_CMD_HEADER 2
737 #define MIFARE_UID_LEN 4
738 #define MIFARE_KEY_LEN 6
739 #define MIFARE_CMD_LEN 12
742 * <= 0: driver handled the data exchange
743 * 1: driver doesn't especially handle, please do standard processing
745 static int pn544_hci_data_exchange(struct nfc_shdlc
*shdlc
,
746 struct nfc_target
*target
,
748 struct sk_buff
**res_skb
)
750 struct nfc_hci_dev
*hdev
= nfc_shdlc_get_hci_dev(shdlc
);
753 pr_info(DRIVER_DESC
": %s for gate=%d\n", __func__
,
754 target
->hci_reader_gate
);
756 switch (target
->hci_reader_gate
) {
757 case NFC_HCI_RF_READER_A_GATE
:
758 if (target
->supported_protocols
& NFC_PROTO_MIFARE_MASK
) {
760 * It seems that pn544 is inverting key and UID for
761 * MIFARE authentication commands.
763 if (skb
->len
== MIFARE_CMD_LEN
&&
764 (skb
->data
[0] == MIFARE_CMD_AUTH_KEY_A
||
765 skb
->data
[0] == MIFARE_CMD_AUTH_KEY_B
)) {
766 u8 uid
[MIFARE_UID_LEN
];
767 u8
*data
= skb
->data
+ MIFARE_CMD_HEADER
;
769 memcpy(uid
, data
+ MIFARE_KEY_LEN
,
771 memmove(data
+ MIFARE_UID_LEN
, data
,
773 memcpy(data
, uid
, MIFARE_UID_LEN
);
776 return nfc_hci_send_cmd(hdev
, target
->hci_reader_gate
,
778 skb
->data
, skb
->len
, res_skb
);
781 case PN544_RF_READER_F_GATE
:
782 *skb_push(skb
, 1) = 0;
783 *skb_push(skb
, 1) = 0;
785 r
= nfc_hci_send_cmd(hdev
, target
->hci_reader_gate
,
787 skb
->data
, skb
->len
, res_skb
);
789 skb_pull(*res_skb
, 1);
791 case PN544_RF_READER_JEWEL_GATE
:
792 return nfc_hci_send_cmd(hdev
, target
->hci_reader_gate
,
794 skb
->data
, skb
->len
, res_skb
);
800 static int pn544_hci_check_presence(struct nfc_shdlc
*shdlc
,
801 struct nfc_target
*target
)
803 struct nfc_hci_dev
*hdev
= nfc_shdlc_get_hci_dev(shdlc
);
805 return nfc_hci_send_cmd(hdev
, target
->hci_reader_gate
,
806 PN544_RF_READER_CMD_PRESENCE_CHECK
,
810 static struct nfc_shdlc_ops pn544_shdlc_ops
= {
811 .open
= pn544_hci_open
,
812 .close
= pn544_hci_close
,
813 .hci_ready
= pn544_hci_ready
,
814 .xmit
= pn544_hci_xmit
,
815 .start_poll
= pn544_hci_start_poll
,
816 .target_from_gate
= pn544_hci_target_from_gate
,
817 .complete_target_discovered
= pn544_hci_complete_target_discovered
,
818 .data_exchange
= pn544_hci_data_exchange
,
819 .check_presence
= pn544_hci_check_presence
,
822 static int __devinit
pn544_hci_probe(struct i2c_client
*client
,
823 const struct i2c_device_id
*id
)
825 struct pn544_hci_info
*info
;
826 struct pn544_nfc_platform_data
*pdata
;
829 struct nfc_hci_init_data init_data
;
831 dev_dbg(&client
->dev
, "%s\n", __func__
);
832 dev_dbg(&client
->dev
, "IRQ: %d\n", client
->irq
);
834 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
835 dev_err(&client
->dev
, "Need I2C_FUNC_I2C\n");
839 info
= kzalloc(sizeof(struct pn544_hci_info
), GFP_KERNEL
);
841 dev_err(&client
->dev
,
842 "Cannot allocate memory for pn544_hci_info.\n");
847 info
->i2c_dev
= client
;
848 info
->state
= PN544_ST_COLD
;
849 mutex_init(&info
->info_lock
);
850 i2c_set_clientdata(client
, info
);
852 pdata
= client
->dev
.platform_data
;
854 dev_err(&client
->dev
, "No platform data\n");
859 if (pdata
->request_resources
== NULL
) {
860 dev_err(&client
->dev
, "request_resources() missing\n");
865 r
= pdata
->request_resources(client
);
867 dev_err(&client
->dev
, "Cannot get platform resources\n");
871 info
->gpio_en
= pdata
->get_gpio(NFC_GPIO_ENABLE
);
872 info
->gpio_fw
= pdata
->get_gpio(NFC_GPIO_FW_RESET
);
873 info
->gpio_irq
= pdata
->get_gpio(NFC_GPIO_IRQ
);
875 pn544_hci_platform_init(info
);
877 r
= request_threaded_irq(client
->irq
, NULL
, pn544_hci_irq_thread_fn
,
878 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
879 PN544_HCI_DRIVER_NAME
, info
);
881 dev_err(&client
->dev
, "Unable to register IRQ handler\n");
885 init_data
.gate_count
= ARRAY_SIZE(pn544_gates
);
887 memcpy(init_data
.gates
, pn544_gates
, sizeof(pn544_gates
));
890 * TODO: Session id must include the driver name + some bus addr
891 * persistent info to discriminate 2 identical chips
893 strcpy(init_data
.session_id
, "ID544HCI");
895 protocols
= NFC_PROTO_JEWEL_MASK
|
896 NFC_PROTO_MIFARE_MASK
|
897 NFC_PROTO_FELICA_MASK
|
898 NFC_PROTO_ISO14443_MASK
|
899 NFC_PROTO_ISO14443_B_MASK
|
900 NFC_PROTO_NFC_DEP_MASK
;
902 info
->shdlc
= nfc_shdlc_allocate(&pn544_shdlc_ops
,
903 &init_data
, protocols
,
904 PN544_FRAME_HEADROOM
+ PN544_CMDS_HEADROOM
,
905 PN544_FRAME_TAILROOM
,
906 PN544_HCI_LLC_MAX_PAYLOAD
,
907 dev_name(&client
->dev
));
909 dev_err(&client
->dev
, "Cannot allocate nfc shdlc.\n");
914 nfc_shdlc_set_clientdata(info
->shdlc
, info
);
919 free_irq(client
->irq
, info
);
922 if (pdata
->free_resources
!= NULL
)
923 pdata
->free_resources();
932 static __devexit
int pn544_hci_remove(struct i2c_client
*client
)
934 struct pn544_hci_info
*info
= i2c_get_clientdata(client
);
935 struct pn544_nfc_platform_data
*pdata
= client
->dev
.platform_data
;
937 dev_dbg(&client
->dev
, "%s\n", __func__
);
939 nfc_shdlc_free(info
->shdlc
);
941 if (info
->state
!= PN544_ST_COLD
) {
946 free_irq(client
->irq
, info
);
947 if (pdata
->free_resources
)
948 pdata
->free_resources();
955 static struct i2c_driver pn544_hci_driver
= {
957 .name
= PN544_HCI_DRIVER_NAME
,
959 .probe
= pn544_hci_probe
,
960 .id_table
= pn544_hci_id_table
,
961 .remove
= __devexit_p(pn544_hci_remove
),
964 static int __init
pn544_hci_init(void)
968 pr_debug(DRIVER_DESC
": %s\n", __func__
);
970 r
= i2c_add_driver(&pn544_hci_driver
);
972 pr_err(PN544_HCI_DRIVER_NAME
": driver registration failed\n");
979 static void __exit
pn544_hci_exit(void)
981 i2c_del_driver(&pn544_hci_driver
);
984 module_init(pn544_hci_init
);
985 module_exit(pn544_hci_exit
);
987 MODULE_LICENSE("GPL");
988 MODULE_DESCRIPTION(DRIVER_DESC
);