1 // SPDX-License-Identifier: GPL-2.0-only
3 * HCI based Driver for NXP PN544 NFC Chip
5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/delay.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
14 #include <linux/nfc.h>
15 #include <net/nfc/hci.h>
19 /* Timing restrictions (ms) */
20 #define PN544_HCI_RESETVEN_TIME 30
28 #define FULL_VERSION_LEN 11
30 /* Proprietary commands */
31 #define PN544_WRITE 0x3f
32 #define PN544_TEST_SWP 0x21
34 /* Proprietary gates, events, commands and registers */
36 /* NFC_HCI_RF_READER_A_GATE additional registers and commands */
37 #define PN544_RF_READER_A_AUTO_ACTIVATION 0x10
38 #define PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION 0x12
39 #define PN544_MIFARE_CMD 0x21
41 /* Commands that apply to all RF readers */
42 #define PN544_RF_READER_CMD_PRESENCE_CHECK 0x30
43 #define PN544_RF_READER_CMD_ACTIVATE_NEXT 0x32
45 /* NFC_HCI_ID_MGMT_GATE additional registers */
46 #define PN544_ID_MGMT_FULL_VERSION_SW 0x10
48 #define PN544_RF_READER_ISO15693_GATE 0x12
50 #define PN544_RF_READER_F_GATE 0x14
51 #define PN544_FELICA_ID 0x04
52 #define PN544_FELICA_RAW 0x20
54 #define PN544_RF_READER_JEWEL_GATE 0x15
55 #define PN544_JEWEL_RAW_CMD 0x23
57 #define PN544_RF_READER_NFCIP1_INITIATOR_GATE 0x30
58 #define PN544_RF_READER_NFCIP1_TARGET_GATE 0x31
60 #define PN544_SYS_MGMT_GATE 0x90
61 #define PN544_SYS_MGMT_INFO_NOTIFICATION 0x02
63 #define PN544_POLLING_LOOP_MGMT_GATE 0x94
64 #define PN544_DEP_MODE 0x01
65 #define PN544_DEP_ATR_REQ 0x02
66 #define PN544_DEP_ATR_RES 0x03
67 #define PN544_DEP_MERGE 0x0D
68 #define PN544_PL_RDPHASES 0x06
69 #define PN544_PL_EMULATION 0x07
70 #define PN544_PL_NFCT_DEACTIVATED 0x09
72 #define PN544_SWP_MGMT_GATE 0xA0
73 #define PN544_SWP_DEFAULT_MODE 0x01
75 #define PN544_NFC_WI_MGMT_GATE 0xA1
76 #define PN544_NFC_ESE_DEFAULT_MODE 0x01
78 #define PN544_HCI_EVT_SND_DATA 0x01
79 #define PN544_HCI_EVT_ACTIVATED 0x02
80 #define PN544_HCI_EVT_DEACTIVATED 0x03
81 #define PN544_HCI_EVT_RCV_DATA 0x04
82 #define PN544_HCI_EVT_CONTINUE_MI 0x05
83 #define PN544_HCI_EVT_SWITCH_MODE 0x03
85 #define PN544_HCI_CMD_ATTREQUEST 0x12
86 #define PN544_HCI_CMD_CONTINUE_ACTIVATION 0x13
88 static const struct nfc_hci_gate pn544_gates
[] = {
89 {NFC_HCI_ADMIN_GATE
, NFC_HCI_INVALID_PIPE
},
90 {NFC_HCI_LOOPBACK_GATE
, NFC_HCI_INVALID_PIPE
},
91 {NFC_HCI_ID_MGMT_GATE
, NFC_HCI_INVALID_PIPE
},
92 {NFC_HCI_LINK_MGMT_GATE
, NFC_HCI_INVALID_PIPE
},
93 {NFC_HCI_RF_READER_B_GATE
, NFC_HCI_INVALID_PIPE
},
94 {NFC_HCI_RF_READER_A_GATE
, NFC_HCI_INVALID_PIPE
},
95 {PN544_SYS_MGMT_GATE
, NFC_HCI_INVALID_PIPE
},
96 {PN544_SWP_MGMT_GATE
, NFC_HCI_INVALID_PIPE
},
97 {PN544_POLLING_LOOP_MGMT_GATE
, NFC_HCI_INVALID_PIPE
},
98 {PN544_NFC_WI_MGMT_GATE
, NFC_HCI_INVALID_PIPE
},
99 {PN544_RF_READER_F_GATE
, NFC_HCI_INVALID_PIPE
},
100 {PN544_RF_READER_JEWEL_GATE
, NFC_HCI_INVALID_PIPE
},
101 {PN544_RF_READER_ISO15693_GATE
, NFC_HCI_INVALID_PIPE
},
102 {PN544_RF_READER_NFCIP1_INITIATOR_GATE
, NFC_HCI_INVALID_PIPE
},
103 {PN544_RF_READER_NFCIP1_TARGET_GATE
, NFC_HCI_INVALID_PIPE
}
106 /* Largest headroom needed for outgoing custom commands */
107 #define PN544_CMDS_HEADROOM 2
109 struct pn544_hci_info
{
110 const struct nfc_phy_ops
*phy_ops
;
113 struct nfc_hci_dev
*hdev
;
115 enum pn544_state state
;
117 struct mutex info_lock
;
120 data_exchange_cb_t async_cb
;
121 void *async_cb_context
;
123 fw_download_t fw_download
;
126 static int pn544_hci_open(struct nfc_hci_dev
*hdev
)
128 struct pn544_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
131 mutex_lock(&info
->info_lock
);
133 if (info
->state
!= PN544_ST_COLD
) {
138 r
= info
->phy_ops
->enable(info
->phy_id
);
141 info
->state
= PN544_ST_READY
;
144 mutex_unlock(&info
->info_lock
);
148 static void pn544_hci_close(struct nfc_hci_dev
*hdev
)
150 struct pn544_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
152 mutex_lock(&info
->info_lock
);
154 if (info
->state
== PN544_ST_COLD
)
157 info
->phy_ops
->disable(info
->phy_id
);
159 info
->state
= PN544_ST_COLD
;
162 mutex_unlock(&info
->info_lock
);
165 static int pn544_hci_ready(struct nfc_hci_dev
*hdev
)
168 static struct hw_config
{
172 {{0x9f, 0x9a}, 0x00},
174 {{0x98, 0x10}, 0xbc},
176 {{0x9e, 0x71}, 0x00},
178 {{0x98, 0x09}, 0x00},
180 {{0x9e, 0xb4}, 0x00},
182 {{0x9c, 0x01}, 0x08},
184 {{0x9e, 0xaa}, 0x01},
186 {{0x9b, 0xd1}, 0x17},
187 {{0x9b, 0xd2}, 0x58},
188 {{0x9b, 0xd3}, 0x10},
189 {{0x9b, 0xd4}, 0x47},
190 {{0x9b, 0xd5}, 0x0c},
191 {{0x9b, 0xd6}, 0x37},
192 {{0x9b, 0xdd}, 0x33},
194 {{0x9b, 0x84}, 0x00},
195 {{0x99, 0x81}, 0x79},
196 {{0x99, 0x31}, 0x79},
198 {{0x98, 0x00}, 0x3f},
200 {{0x9f, 0x09}, 0x02},
202 {{0x9f, 0x0a}, 0x05},
204 {{0x9e, 0xd1}, 0xa1},
205 {{0x99, 0x23}, 0x01},
207 {{0x9e, 0x74}, 0x00},
208 {{0x9e, 0x90}, 0x00},
209 {{0x9f, 0x28}, 0x10},
211 {{0x9f, 0x35}, 0x04},
213 {{0x9f, 0x36}, 0x11},
215 {{0x9c, 0x31}, 0x00},
217 {{0x9c, 0x32}, 0x00},
219 {{0x9c, 0x19}, 0x0a},
221 {{0x9c, 0x1a}, 0x0a},
223 {{0x9c, 0x0c}, 0x00},
225 {{0x9c, 0x0d}, 0x00},
227 {{0x9c, 0x12}, 0x00},
229 {{0x9c, 0x13}, 0x00},
231 {{0x98, 0xa2}, 0x09},
233 {{0x98, 0x93}, 0x00},
235 {{0x98, 0x7d}, 0x08},
236 {{0x98, 0x7e}, 0x00},
237 {{0x9f, 0xc8}, 0x00},
239 struct hw_config
*p
= hw_config
;
240 int count
= ARRAY_SIZE(hw_config
);
241 struct sk_buff
*res_skb
;
247 param
[1] = p
->adr
[0];
248 param
[2] = p
->adr
[1];
251 r
= nfc_hci_send_cmd(hdev
, PN544_SYS_MGMT_GATE
, PN544_WRITE
,
256 if (res_skb
->len
!= 1) {
261 if (res_skb
->data
[0] != p
->value
) {
271 param
[0] = NFC_HCI_UICC_HOST_ID
;
272 r
= nfc_hci_set_param(hdev
, NFC_HCI_ADMIN_GATE
,
273 NFC_HCI_ADMIN_WHITELIST
, param
, 1);
278 r
= nfc_hci_set_param(hdev
, PN544_SYS_MGMT_GATE
,
279 PN544_SYS_MGMT_INFO_NOTIFICATION
, param
, 1);
284 r
= nfc_hci_set_param(hdev
, NFC_HCI_RF_READER_A_GATE
,
285 PN544_RF_READER_A_AUTO_ACTIVATION
, param
, 1);
289 r
= nfc_hci_send_event(hdev
, NFC_HCI_RF_READER_A_GATE
,
290 NFC_HCI_EVT_END_OPERATION
, NULL
, 0);
295 r
= nfc_hci_set_param(hdev
, PN544_POLLING_LOOP_MGMT_GATE
,
296 PN544_PL_NFCT_DEACTIVATED
, param
, 1);
301 r
= nfc_hci_set_param(hdev
, PN544_POLLING_LOOP_MGMT_GATE
,
302 PN544_PL_RDPHASES
, param
, 1);
306 r
= nfc_hci_get_param(hdev
, NFC_HCI_ID_MGMT_GATE
,
307 PN544_ID_MGMT_FULL_VERSION_SW
, &skb
);
311 if (skb
->len
!= FULL_VERSION_LEN
) {
316 print_hex_dump(KERN_DEBUG
, "FULL VERSION SOFTWARE INFO: ",
317 DUMP_PREFIX_NONE
, 16, 1,
318 skb
->data
, FULL_VERSION_LEN
, false);
325 static int pn544_hci_xmit(struct nfc_hci_dev
*hdev
, struct sk_buff
*skb
)
327 struct pn544_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
329 return info
->phy_ops
->write(info
->phy_id
, skb
);
332 static int pn544_hci_start_poll(struct nfc_hci_dev
*hdev
,
333 u32 im_protocols
, u32 tm_protocols
)
339 u8 i_mode
= 0x3f; /* Enable all supported modes */
341 u8 t_merge
= 0x01; /* Enable merge by default */
343 pr_info(DRIVER_DESC
": %s protocols 0x%x 0x%x\n",
344 __func__
, im_protocols
, tm_protocols
);
346 r
= nfc_hci_send_event(hdev
, NFC_HCI_RF_READER_A_GATE
,
347 NFC_HCI_EVT_END_OPERATION
, NULL
, 0);
353 r
= nfc_hci_set_param(hdev
, PN544_POLLING_LOOP_MGMT_GATE
,
354 PN544_PL_EMULATION
, duration
, 2);
359 r
= nfc_hci_set_param(hdev
, PN544_POLLING_LOOP_MGMT_GATE
,
360 PN544_PL_NFCT_DEACTIVATED
, &activated
, 1);
364 if (im_protocols
& (NFC_PROTO_ISO14443_MASK
| NFC_PROTO_MIFARE_MASK
|
365 NFC_PROTO_JEWEL_MASK
))
366 phases
|= 1; /* Type A */
367 if (im_protocols
& NFC_PROTO_FELICA_MASK
) {
368 phases
|= (1 << 2); /* Type F 212 */
369 phases
|= (1 << 3); /* Type F 424 */
372 phases
|= (1 << 5); /* NFC active */
374 r
= nfc_hci_set_param(hdev
, PN544_POLLING_LOOP_MGMT_GATE
,
375 PN544_PL_RDPHASES
, &phases
, 1);
379 if ((im_protocols
| tm_protocols
) & NFC_PROTO_NFC_DEP_MASK
) {
380 hdev
->gb
= nfc_get_local_general_bytes(hdev
->ndev
,
382 pr_debug("generate local bytes %p\n", hdev
->gb
);
383 if (hdev
->gb
== NULL
|| hdev
->gb_len
== 0) {
384 im_protocols
&= ~NFC_PROTO_NFC_DEP_MASK
;
385 tm_protocols
&= ~NFC_PROTO_NFC_DEP_MASK
;
389 if (im_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
390 r
= nfc_hci_send_event(hdev
,
391 PN544_RF_READER_NFCIP1_INITIATOR_GATE
,
392 NFC_HCI_EVT_END_OPERATION
, NULL
, 0);
396 r
= nfc_hci_set_param(hdev
,
397 PN544_RF_READER_NFCIP1_INITIATOR_GATE
,
398 PN544_DEP_MODE
, &i_mode
, 1);
402 r
= nfc_hci_set_param(hdev
,
403 PN544_RF_READER_NFCIP1_INITIATOR_GATE
,
404 PN544_DEP_ATR_REQ
, hdev
->gb
, hdev
->gb_len
);
408 r
= nfc_hci_send_event(hdev
,
409 PN544_RF_READER_NFCIP1_INITIATOR_GATE
,
410 NFC_HCI_EVT_READER_REQUESTED
, NULL
, 0);
412 nfc_hci_send_event(hdev
,
413 PN544_RF_READER_NFCIP1_INITIATOR_GATE
,
414 NFC_HCI_EVT_END_OPERATION
, NULL
, 0);
417 if (tm_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
418 r
= nfc_hci_set_param(hdev
, PN544_RF_READER_NFCIP1_TARGET_GATE
,
419 PN544_DEP_MODE
, &t_mode
, 1);
423 r
= nfc_hci_set_param(hdev
, PN544_RF_READER_NFCIP1_TARGET_GATE
,
424 PN544_DEP_ATR_RES
, hdev
->gb
, hdev
->gb_len
);
428 r
= nfc_hci_set_param(hdev
, PN544_RF_READER_NFCIP1_TARGET_GATE
,
429 PN544_DEP_MERGE
, &t_merge
, 1);
434 r
= nfc_hci_send_event(hdev
, NFC_HCI_RF_READER_A_GATE
,
435 NFC_HCI_EVT_READER_REQUESTED
, NULL
, 0);
437 nfc_hci_send_event(hdev
, NFC_HCI_RF_READER_A_GATE
,
438 NFC_HCI_EVT_END_OPERATION
, NULL
, 0);
443 static int pn544_hci_dep_link_up(struct nfc_hci_dev
*hdev
,
444 struct nfc_target
*target
, u8 comm_mode
,
445 u8
*gb
, size_t gb_len
)
447 struct sk_buff
*rgb_skb
= NULL
;
450 r
= nfc_hci_get_param(hdev
, target
->hci_reader_gate
,
451 PN544_DEP_ATR_RES
, &rgb_skb
);
455 if (rgb_skb
->len
== 0 || rgb_skb
->len
> NFC_GB_MAXSIZE
) {
459 print_hex_dump(KERN_DEBUG
, "remote gb: ", DUMP_PREFIX_OFFSET
,
460 16, 1, rgb_skb
->data
, rgb_skb
->len
, true);
462 r
= nfc_set_remote_general_bytes(hdev
->ndev
, rgb_skb
->data
,
466 r
= nfc_dep_link_is_up(hdev
->ndev
, target
->idx
, comm_mode
,
473 static int pn544_hci_dep_link_down(struct nfc_hci_dev
*hdev
)
476 return nfc_hci_send_event(hdev
, PN544_RF_READER_NFCIP1_INITIATOR_GATE
,
477 NFC_HCI_EVT_END_OPERATION
, NULL
, 0);
480 static int pn544_hci_target_from_gate(struct nfc_hci_dev
*hdev
, u8 gate
,
481 struct nfc_target
*target
)
484 case PN544_RF_READER_F_GATE
:
485 target
->supported_protocols
= NFC_PROTO_FELICA_MASK
;
487 case PN544_RF_READER_JEWEL_GATE
:
488 target
->supported_protocols
= NFC_PROTO_JEWEL_MASK
;
489 target
->sens_res
= 0x0c00;
491 case PN544_RF_READER_NFCIP1_INITIATOR_GATE
:
492 target
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
501 static int pn544_hci_complete_target_discovered(struct nfc_hci_dev
*hdev
,
503 struct nfc_target
*target
)
505 struct sk_buff
*uid_skb
;
508 if (gate
== PN544_RF_READER_NFCIP1_INITIATOR_GATE
)
511 if (target
->supported_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
512 r
= nfc_hci_send_cmd(hdev
,
513 PN544_RF_READER_NFCIP1_INITIATOR_GATE
,
514 PN544_HCI_CMD_CONTINUE_ACTIVATION
, NULL
, 0, NULL
);
518 target
->hci_reader_gate
= PN544_RF_READER_NFCIP1_INITIATOR_GATE
;
519 } else if (target
->supported_protocols
& NFC_PROTO_MIFARE_MASK
) {
520 if (target
->nfcid1_len
!= 4 && target
->nfcid1_len
!= 7 &&
521 target
->nfcid1_len
!= 10)
524 r
= nfc_hci_send_cmd(hdev
, NFC_HCI_RF_READER_A_GATE
,
525 PN544_RF_READER_CMD_ACTIVATE_NEXT
,
526 target
->nfcid1
, target
->nfcid1_len
, NULL
);
527 } else if (target
->supported_protocols
& NFC_PROTO_FELICA_MASK
) {
528 r
= nfc_hci_get_param(hdev
, PN544_RF_READER_F_GATE
,
529 PN544_FELICA_ID
, &uid_skb
);
533 if (uid_skb
->len
!= 8) {
538 /* Type F NFC-DEP IDm has prefix 0x01FE */
539 if ((uid_skb
->data
[0] == 0x01) && (uid_skb
->data
[1] == 0xfe)) {
541 r
= nfc_hci_send_cmd(hdev
,
542 PN544_RF_READER_NFCIP1_INITIATOR_GATE
,
543 PN544_HCI_CMD_CONTINUE_ACTIVATION
,
548 target
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
549 target
->hci_reader_gate
=
550 PN544_RF_READER_NFCIP1_INITIATOR_GATE
;
552 r
= nfc_hci_send_cmd(hdev
, PN544_RF_READER_F_GATE
,
553 PN544_RF_READER_CMD_ACTIVATE_NEXT
,
554 uid_skb
->data
, uid_skb
->len
, NULL
);
557 } else if (target
->supported_protocols
& NFC_PROTO_ISO14443_MASK
) {
559 * TODO: maybe other ISO 14443 require some kind of continue
560 * activation, but for now we've seen only this one below.
562 if (target
->sens_res
== 0x4403) /* Type 4 Mifare DESFire */
563 r
= nfc_hci_send_cmd(hdev
, NFC_HCI_RF_READER_A_GATE
,
564 PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION
,
571 #define PN544_CB_TYPE_READER_F 1
573 static void pn544_hci_data_exchange_cb(void *context
, struct sk_buff
*skb
,
576 struct pn544_hci_info
*info
= context
;
578 switch (info
->async_cb_type
) {
579 case PN544_CB_TYPE_READER_F
:
582 info
->async_cb(info
->async_cb_context
, skb
, err
);
591 #define MIFARE_CMD_AUTH_KEY_A 0x60
592 #define MIFARE_CMD_AUTH_KEY_B 0x61
593 #define MIFARE_CMD_HEADER 2
594 #define MIFARE_UID_LEN 4
595 #define MIFARE_KEY_LEN 6
596 #define MIFARE_CMD_LEN 12
599 * <= 0: driver handled the data exchange
600 * 1: driver doesn't especially handle, please do standard processing
602 static int pn544_hci_im_transceive(struct nfc_hci_dev
*hdev
,
603 struct nfc_target
*target
,
604 struct sk_buff
*skb
, data_exchange_cb_t cb
,
607 struct pn544_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
609 pr_info(DRIVER_DESC
": %s for gate=%d\n", __func__
,
610 target
->hci_reader_gate
);
612 switch (target
->hci_reader_gate
) {
613 case NFC_HCI_RF_READER_A_GATE
:
614 if (target
->supported_protocols
& NFC_PROTO_MIFARE_MASK
) {
616 * It seems that pn544 is inverting key and UID for
617 * MIFARE authentication commands.
619 if (skb
->len
== MIFARE_CMD_LEN
&&
620 (skb
->data
[0] == MIFARE_CMD_AUTH_KEY_A
||
621 skb
->data
[0] == MIFARE_CMD_AUTH_KEY_B
)) {
622 u8 uid
[MIFARE_UID_LEN
];
623 u8
*data
= skb
->data
+ MIFARE_CMD_HEADER
;
625 memcpy(uid
, data
+ MIFARE_KEY_LEN
,
627 memmove(data
+ MIFARE_UID_LEN
, data
,
629 memcpy(data
, uid
, MIFARE_UID_LEN
);
632 return nfc_hci_send_cmd_async(hdev
,
633 target
->hci_reader_gate
,
639 case PN544_RF_READER_F_GATE
:
640 *(u8
*)skb_push(skb
, 1) = 0;
641 *(u8
*)skb_push(skb
, 1) = 0;
643 info
->async_cb_type
= PN544_CB_TYPE_READER_F
;
645 info
->async_cb_context
= cb_context
;
647 return nfc_hci_send_cmd_async(hdev
, target
->hci_reader_gate
,
648 PN544_FELICA_RAW
, skb
->data
,
650 pn544_hci_data_exchange_cb
, info
);
651 case PN544_RF_READER_JEWEL_GATE
:
652 return nfc_hci_send_cmd_async(hdev
, target
->hci_reader_gate
,
653 PN544_JEWEL_RAW_CMD
, skb
->data
,
654 skb
->len
, cb
, cb_context
);
655 case PN544_RF_READER_NFCIP1_INITIATOR_GATE
:
656 *(u8
*)skb_push(skb
, 1) = 0;
658 return nfc_hci_send_event(hdev
, target
->hci_reader_gate
,
659 PN544_HCI_EVT_SND_DATA
, skb
->data
,
666 static int pn544_hci_tm_send(struct nfc_hci_dev
*hdev
, struct sk_buff
*skb
)
670 /* Set default false for multiple information chaining */
671 *(u8
*)skb_push(skb
, 1) = 0;
673 r
= nfc_hci_send_event(hdev
, PN544_RF_READER_NFCIP1_TARGET_GATE
,
674 PN544_HCI_EVT_SND_DATA
, skb
->data
, skb
->len
);
681 static int pn544_hci_check_presence(struct nfc_hci_dev
*hdev
,
682 struct nfc_target
*target
)
684 pr_debug("supported protocol %d\n", target
->supported_protocols
);
685 if (target
->supported_protocols
& (NFC_PROTO_ISO14443_MASK
|
686 NFC_PROTO_ISO14443_B_MASK
)) {
687 return nfc_hci_send_cmd(hdev
, target
->hci_reader_gate
,
688 PN544_RF_READER_CMD_PRESENCE_CHECK
,
690 } else if (target
->supported_protocols
& NFC_PROTO_MIFARE_MASK
) {
691 if (target
->nfcid1_len
!= 4 && target
->nfcid1_len
!= 7 &&
692 target
->nfcid1_len
!= 10)
695 return nfc_hci_send_cmd(hdev
, NFC_HCI_RF_READER_A_GATE
,
696 PN544_RF_READER_CMD_ACTIVATE_NEXT
,
697 target
->nfcid1
, target
->nfcid1_len
, NULL
);
698 } else if (target
->supported_protocols
& (NFC_PROTO_JEWEL_MASK
|
699 NFC_PROTO_FELICA_MASK
)) {
701 } else if (target
->supported_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
702 return nfc_hci_send_cmd(hdev
, target
->hci_reader_gate
,
703 PN544_HCI_CMD_ATTREQUEST
,
712 * <= 0: driver handled the event, skb consumed
713 * 1: driver does not handle the event, please do standard processing
715 static int pn544_hci_event_received(struct nfc_hci_dev
*hdev
, u8 pipe
, u8 event
,
718 struct sk_buff
*rgb_skb
= NULL
;
719 u8 gate
= hdev
->pipes
[pipe
].gate
;
722 pr_debug("hci event %d\n", event
);
724 case PN544_HCI_EVT_ACTIVATED
:
725 if (gate
== PN544_RF_READER_NFCIP1_INITIATOR_GATE
) {
726 r
= nfc_hci_target_discovered(hdev
, gate
);
727 } else if (gate
== PN544_RF_READER_NFCIP1_TARGET_GATE
) {
728 r
= nfc_hci_get_param(hdev
, gate
, PN544_DEP_ATR_REQ
,
733 r
= nfc_tm_activated(hdev
->ndev
, NFC_PROTO_NFC_DEP_MASK
,
734 NFC_COMM_PASSIVE
, rgb_skb
->data
,
742 case PN544_HCI_EVT_DEACTIVATED
:
743 r
= nfc_hci_send_event(hdev
, gate
, NFC_HCI_EVT_END_OPERATION
,
746 case PN544_HCI_EVT_RCV_DATA
:
752 if (skb
->data
[0] != 0) {
753 pr_debug("data0 %d\n", skb
->data
[0]);
759 return nfc_tm_data_received(hdev
->ndev
, skb
);
770 static int pn544_hci_fw_download(struct nfc_hci_dev
*hdev
,
771 const char *firmware_name
)
773 struct pn544_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
775 if (info
->fw_download
== NULL
)
778 return info
->fw_download(info
->phy_id
, firmware_name
, hdev
->sw_romlib
);
781 static int pn544_hci_discover_se(struct nfc_hci_dev
*hdev
)
784 u8 ese_mode
= 0x01; /* Default mode */
785 struct sk_buff
*res_skb
;
788 r
= nfc_hci_send_cmd(hdev
, PN544_SYS_MGMT_GATE
, PN544_TEST_SWP
,
792 if (res_skb
->len
== 2 && res_skb
->data
[0] == 0x00)
793 nfc_add_se(hdev
->ndev
, se_idx
++, NFC_SE_UICC
);
798 r
= nfc_hci_send_event(hdev
, PN544_NFC_WI_MGMT_GATE
,
799 PN544_HCI_EVT_SWITCH_MODE
,
802 nfc_add_se(hdev
->ndev
, se_idx
++, NFC_SE_EMBEDDED
);
807 #define PN544_SE_MODE_OFF 0x00
808 #define PN544_SE_MODE_ON 0x01
809 static int pn544_hci_enable_se(struct nfc_hci_dev
*hdev
, u32 se_idx
)
811 const struct nfc_se
*se
;
812 u8 enable
= PN544_SE_MODE_ON
;
813 static struct uicc_gatelist
{
817 } uicc_gatelist
[] = {
818 {0x00, {0x9e, 0xd9}, 0x23},
819 {0x00, {0x9e, 0xda}, 0x21},
820 {0x00, {0x9e, 0xdb}, 0x22},
821 {0x00, {0x9e, 0xdc}, 0x24},
823 struct uicc_gatelist
*p
= uicc_gatelist
;
824 int count
= ARRAY_SIZE(uicc_gatelist
);
825 struct sk_buff
*res_skb
;
828 se
= nfc_find_se(hdev
->ndev
, se_idx
);
833 r
= nfc_hci_send_cmd(hdev
, PN544_SYS_MGMT_GATE
,
834 PN544_WRITE
, (u8
*)p
, 4, &res_skb
);
838 if (res_skb
->len
!= 1) {
843 if (res_skb
->data
[0] != p
->value
) {
853 return nfc_hci_set_param(hdev
, PN544_SWP_MGMT_GATE
,
854 PN544_SWP_DEFAULT_MODE
, &enable
, 1);
855 case NFC_SE_EMBEDDED
:
856 return nfc_hci_set_param(hdev
, PN544_NFC_WI_MGMT_GATE
,
857 PN544_NFC_ESE_DEFAULT_MODE
, &enable
, 1);
864 static int pn544_hci_disable_se(struct nfc_hci_dev
*hdev
, u32 se_idx
)
866 const struct nfc_se
*se
;
867 u8 disable
= PN544_SE_MODE_OFF
;
869 se
= nfc_find_se(hdev
->ndev
, se_idx
);
873 return nfc_hci_set_param(hdev
, PN544_SWP_MGMT_GATE
,
874 PN544_SWP_DEFAULT_MODE
, &disable
, 1);
875 case NFC_SE_EMBEDDED
:
876 return nfc_hci_set_param(hdev
, PN544_NFC_WI_MGMT_GATE
,
877 PN544_NFC_ESE_DEFAULT_MODE
, &disable
, 1);
883 static const struct nfc_hci_ops pn544_hci_ops
= {
884 .open
= pn544_hci_open
,
885 .close
= pn544_hci_close
,
886 .hci_ready
= pn544_hci_ready
,
887 .xmit
= pn544_hci_xmit
,
888 .start_poll
= pn544_hci_start_poll
,
889 .dep_link_up
= pn544_hci_dep_link_up
,
890 .dep_link_down
= pn544_hci_dep_link_down
,
891 .target_from_gate
= pn544_hci_target_from_gate
,
892 .complete_target_discovered
= pn544_hci_complete_target_discovered
,
893 .im_transceive
= pn544_hci_im_transceive
,
894 .tm_send
= pn544_hci_tm_send
,
895 .check_presence
= pn544_hci_check_presence
,
896 .event_received
= pn544_hci_event_received
,
897 .fw_download
= pn544_hci_fw_download
,
898 .discover_se
= pn544_hci_discover_se
,
899 .enable_se
= pn544_hci_enable_se
,
900 .disable_se
= pn544_hci_disable_se
,
903 int pn544_hci_probe(void *phy_id
, const struct nfc_phy_ops
*phy_ops
,
904 char *llc_name
, int phy_headroom
, int phy_tailroom
,
905 int phy_payload
, fw_download_t fw_download
,
906 struct nfc_hci_dev
**hdev
)
908 struct pn544_hci_info
*info
;
910 struct nfc_hci_init_data init_data
;
913 info
= kzalloc(sizeof(struct pn544_hci_info
), GFP_KERNEL
);
919 info
->phy_ops
= phy_ops
;
920 info
->phy_id
= phy_id
;
921 info
->fw_download
= fw_download
;
922 info
->state
= PN544_ST_COLD
;
923 mutex_init(&info
->info_lock
);
925 init_data
.gate_count
= ARRAY_SIZE(pn544_gates
);
927 memcpy(init_data
.gates
, pn544_gates
, sizeof(pn544_gates
));
930 * TODO: Session id must include the driver name + some bus addr
931 * persistent info to discriminate 2 identical chips
933 strcpy(init_data
.session_id
, "ID544HCI");
935 protocols
= NFC_PROTO_JEWEL_MASK
|
936 NFC_PROTO_MIFARE_MASK
|
937 NFC_PROTO_FELICA_MASK
|
938 NFC_PROTO_ISO14443_MASK
|
939 NFC_PROTO_ISO14443_B_MASK
|
940 NFC_PROTO_NFC_DEP_MASK
;
942 info
->hdev
= nfc_hci_allocate_device(&pn544_hci_ops
, &init_data
, 0,
944 phy_headroom
+ PN544_CMDS_HEADROOM
,
945 phy_tailroom
, phy_payload
);
947 pr_err("Cannot allocate nfc hdev\n");
952 nfc_hci_set_clientdata(info
->hdev
, info
);
954 r
= nfc_hci_register_device(info
->hdev
);
963 nfc_hci_free_device(info
->hdev
);
971 EXPORT_SYMBOL(pn544_hci_probe
);
973 void pn544_hci_remove(struct nfc_hci_dev
*hdev
)
975 struct pn544_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
977 nfc_hci_unregister_device(hdev
);
978 nfc_hci_free_device(hdev
);
981 EXPORT_SYMBOL(pn544_hci_remove
);
983 MODULE_LICENSE("GPL");
984 MODULE_DESCRIPTION(DRIVER_DESC
);