1 // SPDX-License-Identifier: GPL-2.0-only
3 * HCI based Driver for STMicroelectronics NFC Chip
5 * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
8 #include <linux/module.h>
10 #include <net/nfc/hci.h>
11 #include <net/nfc/llc.h>
15 #define DRIVER_DESC "HCI NFC driver for ST21NFCA"
17 #define FULL_VERSION_LEN 3
19 /* Proprietary gates, events, commands and registers */
21 /* Commands that apply to all RF readers */
22 #define ST21NFCA_RF_READER_CMD_PRESENCE_CHECK 0x30
24 #define ST21NFCA_RF_READER_ISO15693_GATE 0x12
25 #define ST21NFCA_RF_READER_ISO15693_INVENTORY 0x01
28 * Reader gate for communication with contact-less cards using Type A
29 * protocol ISO14443-3 but not compliant with ISO14443-4
31 #define ST21NFCA_RF_READER_14443_3_A_GATE 0x15
32 #define ST21NFCA_RF_READER_14443_3_A_UID 0x02
33 #define ST21NFCA_RF_READER_14443_3_A_ATQA 0x03
34 #define ST21NFCA_RF_READER_14443_3_A_SAK 0x04
36 #define ST21NFCA_RF_READER_F_DATARATE 0x01
37 #define ST21NFCA_RF_READER_F_DATARATE_106 0x01
38 #define ST21NFCA_RF_READER_F_DATARATE_212 0x02
39 #define ST21NFCA_RF_READER_F_DATARATE_424 0x04
40 #define ST21NFCA_RF_READER_F_POL_REQ 0x02
41 #define ST21NFCA_RF_READER_F_POL_REQ_DEFAULT 0xffff0000
42 #define ST21NFCA_RF_READER_F_NFCID2 0x03
43 #define ST21NFCA_RF_READER_F_NFCID1 0x04
45 #define ST21NFCA_RF_CARD_F_MODE 0x01
46 #define ST21NFCA_RF_CARD_F_NFCID2_LIST 0x04
47 #define ST21NFCA_RF_CARD_F_NFCID1 0x05
48 #define ST21NFCA_RF_CARD_F_SENS_RES 0x06
49 #define ST21NFCA_RF_CARD_F_SEL_RES 0x07
50 #define ST21NFCA_RF_CARD_F_DATARATE 0x08
51 #define ST21NFCA_RF_CARD_F_DATARATE_212_424 0x01
53 #define ST21NFCA_DEVICE_MGNT_PIPE 0x02
55 #define ST21NFCA_DM_GETINFO 0x13
56 #define ST21NFCA_DM_GETINFO_PIPE_LIST 0x02
57 #define ST21NFCA_DM_GETINFO_PIPE_INFO 0x01
58 #define ST21NFCA_DM_PIPE_CREATED 0x02
59 #define ST21NFCA_DM_PIPE_OPEN 0x04
60 #define ST21NFCA_DM_RF_ACTIVE 0x80
61 #define ST21NFCA_DM_DISCONNECT 0x30
63 #define ST21NFCA_DM_IS_PIPE_OPEN(p) \
64 ((p & 0x0f) == (ST21NFCA_DM_PIPE_CREATED | ST21NFCA_DM_PIPE_OPEN))
66 #define ST21NFCA_NFC_MODE 0x03 /* NFC_MODE parameter*/
68 #define ST21NFCA_EVT_HOT_PLUG 0x03
69 #define ST21NFCA_EVT_HOT_PLUG_IS_INHIBITED(x) (x->data[0] & 0x80)
71 #define ST21NFCA_SE_TO_PIPES 2000
73 static DECLARE_BITMAP(dev_mask
, ST21NFCA_NUM_DEVICES
);
75 static struct nfc_hci_gate st21nfca_gates
[] = {
76 {NFC_HCI_ADMIN_GATE
, NFC_HCI_ADMIN_PIPE
},
77 {NFC_HCI_LINK_MGMT_GATE
, NFC_HCI_LINK_MGMT_PIPE
},
78 {ST21NFCA_DEVICE_MGNT_GATE
, ST21NFCA_DEVICE_MGNT_PIPE
},
80 {NFC_HCI_LOOPBACK_GATE
, NFC_HCI_INVALID_PIPE
},
81 {NFC_HCI_ID_MGMT_GATE
, NFC_HCI_INVALID_PIPE
},
82 {NFC_HCI_RF_READER_B_GATE
, NFC_HCI_INVALID_PIPE
},
83 {NFC_HCI_RF_READER_A_GATE
, NFC_HCI_INVALID_PIPE
},
84 {ST21NFCA_RF_READER_F_GATE
, NFC_HCI_INVALID_PIPE
},
85 {ST21NFCA_RF_READER_14443_3_A_GATE
, NFC_HCI_INVALID_PIPE
},
86 {ST21NFCA_RF_READER_ISO15693_GATE
, NFC_HCI_INVALID_PIPE
},
87 {ST21NFCA_RF_CARD_F_GATE
, NFC_HCI_INVALID_PIPE
},
89 /* Secure element pipes are created by secure element host */
90 {ST21NFCA_CONNECTIVITY_GATE
, NFC_HCI_DO_NOT_CREATE_PIPE
},
91 {ST21NFCA_APDU_READER_GATE
, NFC_HCI_DO_NOT_CREATE_PIPE
},
94 struct st21nfca_pipe_info
{
102 /* Largest headroom needed for outgoing custom commands */
103 #define ST21NFCA_CMDS_HEADROOM 7
105 static int st21nfca_hci_load_session(struct nfc_hci_dev
*hdev
)
108 struct sk_buff
*skb_pipe_list
, *skb_pipe_info
;
109 struct st21nfca_pipe_info
*info
;
111 u8 pipe_list
[] = { ST21NFCA_DM_GETINFO_PIPE_LIST
,
112 NFC_HCI_TERMINAL_HOST_ID
114 u8 pipe_info
[] = { ST21NFCA_DM_GETINFO_PIPE_INFO
,
115 NFC_HCI_TERMINAL_HOST_ID
, 0
118 /* On ST21NFCA device pipes number are dynamics
119 * A maximum of 16 pipes can be created at the same time
120 * If pipes are already created, hci_dev_up will fail.
121 * Doing a clear all pipe is a bad idea because:
122 * - It does useless EEPROM cycling
123 * - It might cause issue for secure elements support
124 * (such as removing connectivity or APDU reader pipe)
125 * A better approach on ST21NFCA is to:
126 * - get a pipe list for each host.
127 * (eg: NFC_HCI_HOST_CONTROLLER_ID for now).
128 * (TODO Later on UICC HOST and eSE HOST)
129 * - get pipe information
130 * - match retrieved pipe list in st21nfca_gates
131 * ST21NFCA_DEVICE_MGNT_GATE is a proprietary gate
132 * with ST21NFCA_DEVICE_MGNT_PIPE.
133 * Pipe can be closed and need to be open.
135 r
= nfc_hci_connect_gate(hdev
, NFC_HCI_HOST_CONTROLLER_ID
,
136 ST21NFCA_DEVICE_MGNT_GATE
,
137 ST21NFCA_DEVICE_MGNT_PIPE
);
142 r
= nfc_hci_send_cmd(hdev
, ST21NFCA_DEVICE_MGNT_GATE
,
143 ST21NFCA_DM_GETINFO
, pipe_list
, sizeof(pipe_list
),
148 /* Complete the existing gate_pipe table */
149 for (i
= 0; i
< skb_pipe_list
->len
; i
++) {
150 pipe_info
[2] = skb_pipe_list
->data
[i
];
151 r
= nfc_hci_send_cmd(hdev
, ST21NFCA_DEVICE_MGNT_GATE
,
152 ST21NFCA_DM_GETINFO
, pipe_info
,
153 sizeof(pipe_info
), &skb_pipe_info
);
158 * Match pipe ID and gate ID
159 * Output format from ST21NFC_DM_GETINFO is:
160 * - pipe state (1byte)
161 * - source hid (1byte)
162 * - source gid (1byte)
163 * - destination hid (1byte)
164 * - destination gid (1byte)
166 info
= (struct st21nfca_pipe_info
*) skb_pipe_info
->data
;
167 if (info
->dst_gate_id
== ST21NFCA_APDU_READER_GATE
&&
168 info
->src_host_id
== NFC_HCI_UICC_HOST_ID
) {
169 pr_err("Unexpected apdu_reader pipe on host %x\n",
171 kfree_skb(skb_pipe_info
);
175 for (j
= 3; (j
< ARRAY_SIZE(st21nfca_gates
)) &&
176 (st21nfca_gates
[j
].gate
!= info
->dst_gate_id
) ; j
++)
179 if (j
< ARRAY_SIZE(st21nfca_gates
) &&
180 st21nfca_gates
[j
].gate
== info
->dst_gate_id
&&
181 ST21NFCA_DM_IS_PIPE_OPEN(info
->pipe_state
)) {
182 hdev
->init_data
.gates
[j
].pipe
= pipe_info
[2];
184 hdev
->gate2pipe
[st21nfca_gates
[j
].gate
] =
186 hdev
->pipes
[pipe_info
[2]].gate
=
187 st21nfca_gates
[j
].gate
;
188 hdev
->pipes
[pipe_info
[2]].dest_host
=
191 kfree_skb(skb_pipe_info
);
195 * 3 gates have a well known pipe ID. Only NFC_HCI_LINK_MGMT_GATE
196 * is not yet open at this stage.
198 r
= nfc_hci_connect_gate(hdev
, NFC_HCI_HOST_CONTROLLER_ID
,
199 NFC_HCI_LINK_MGMT_GATE
,
200 NFC_HCI_LINK_MGMT_PIPE
);
202 kfree_skb(skb_pipe_list
);
206 static int st21nfca_hci_open(struct nfc_hci_dev
*hdev
)
208 struct st21nfca_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
211 mutex_lock(&info
->info_lock
);
213 if (info
->state
!= ST21NFCA_ST_COLD
) {
218 r
= info
->phy_ops
->enable(info
->phy_id
);
221 info
->state
= ST21NFCA_ST_READY
;
224 mutex_unlock(&info
->info_lock
);
228 static void st21nfca_hci_close(struct nfc_hci_dev
*hdev
)
230 struct st21nfca_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
232 mutex_lock(&info
->info_lock
);
234 if (info
->state
== ST21NFCA_ST_COLD
)
237 info
->phy_ops
->disable(info
->phy_id
);
238 info
->state
= ST21NFCA_ST_COLD
;
241 mutex_unlock(&info
->info_lock
);
244 static int st21nfca_hci_ready(struct nfc_hci_dev
*hdev
)
246 struct st21nfca_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
254 if (info
->se_status
->is_uicc_present
)
255 white_list
[wl_size
++] = NFC_HCI_UICC_HOST_ID
;
256 if (info
->se_status
->is_ese_present
)
257 white_list
[wl_size
++] = ST21NFCA_ESE_HOST_ID
;
260 r
= nfc_hci_set_param(hdev
, NFC_HCI_ADMIN_GATE
,
261 NFC_HCI_ADMIN_WHITELIST
,
262 (u8
*) &white_list
, wl_size
);
267 /* Set NFC_MODE in device management gate to enable */
268 r
= nfc_hci_get_param(hdev
, ST21NFCA_DEVICE_MGNT_GATE
,
269 ST21NFCA_NFC_MODE
, &skb
);
273 param
= skb
->data
[0];
278 r
= nfc_hci_set_param(hdev
, ST21NFCA_DEVICE_MGNT_GATE
,
279 ST21NFCA_NFC_MODE
, ¶m
, 1);
284 r
= nfc_hci_send_event(hdev
, NFC_HCI_RF_READER_A_GATE
,
285 NFC_HCI_EVT_END_OPERATION
, NULL
, 0);
289 r
= nfc_hci_get_param(hdev
, NFC_HCI_ID_MGMT_GATE
,
290 NFC_HCI_ID_MGMT_VERSION_SW
, &skb
);
294 if (skb
->len
!= FULL_VERSION_LEN
) {
299 print_hex_dump(KERN_DEBUG
, "FULL VERSION SOFTWARE INFO: ",
300 DUMP_PREFIX_NONE
, 16, 1,
301 skb
->data
, FULL_VERSION_LEN
, false);
308 static int st21nfca_hci_xmit(struct nfc_hci_dev
*hdev
, struct sk_buff
*skb
)
310 struct st21nfca_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
312 return info
->phy_ops
->write(info
->phy_id
, skb
);
315 static int st21nfca_hci_start_poll(struct nfc_hci_dev
*hdev
,
316 u32 im_protocols
, u32 tm_protocols
)
321 struct sk_buff
*datarate_skb
;
323 pr_info(DRIVER_DESC
": %s protocols 0x%x 0x%x\n",
324 __func__
, im_protocols
, tm_protocols
);
326 r
= nfc_hci_send_event(hdev
, NFC_HCI_RF_READER_A_GATE
,
327 NFC_HCI_EVT_END_OPERATION
, NULL
, 0);
332 * enable polling according to im_protocols & tm_protocols
333 * - CLOSE pipe according to im_protocols & tm_protocols
335 if ((NFC_HCI_RF_READER_B_GATE
& im_protocols
) == 0) {
336 r
= nfc_hci_disconnect_gate(hdev
,
337 NFC_HCI_RF_READER_B_GATE
);
342 if ((NFC_HCI_RF_READER_A_GATE
& im_protocols
) == 0) {
343 r
= nfc_hci_disconnect_gate(hdev
,
344 NFC_HCI_RF_READER_A_GATE
);
349 if ((ST21NFCA_RF_READER_F_GATE
& im_protocols
) == 0) {
350 r
= nfc_hci_disconnect_gate(hdev
,
351 ST21NFCA_RF_READER_F_GATE
);
355 hdev
->gb
= nfc_get_local_general_bytes(hdev
->ndev
,
358 if (hdev
->gb
== NULL
|| hdev
->gb_len
== 0) {
359 im_protocols
&= ~NFC_PROTO_NFC_DEP_MASK
;
360 tm_protocols
&= ~NFC_PROTO_NFC_DEP_MASK
;
363 param
[0] = ST21NFCA_RF_READER_F_DATARATE_106
|
364 ST21NFCA_RF_READER_F_DATARATE_212
|
365 ST21NFCA_RF_READER_F_DATARATE_424
;
366 r
= nfc_hci_set_param(hdev
, ST21NFCA_RF_READER_F_GATE
,
367 ST21NFCA_RF_READER_F_DATARATE
,
372 pol_req
= be32_to_cpu((__force __be32
)
373 ST21NFCA_RF_READER_F_POL_REQ_DEFAULT
);
374 r
= nfc_hci_set_param(hdev
, ST21NFCA_RF_READER_F_GATE
,
375 ST21NFCA_RF_READER_F_POL_REQ
,
381 if ((ST21NFCA_RF_READER_14443_3_A_GATE
& im_protocols
) == 0) {
382 r
= nfc_hci_disconnect_gate(hdev
,
383 ST21NFCA_RF_READER_14443_3_A_GATE
);
388 if ((ST21NFCA_RF_READER_ISO15693_GATE
& im_protocols
) == 0) {
389 r
= nfc_hci_disconnect_gate(hdev
,
390 ST21NFCA_RF_READER_ISO15693_GATE
);
395 r
= nfc_hci_send_event(hdev
, NFC_HCI_RF_READER_A_GATE
,
396 NFC_HCI_EVT_READER_REQUESTED
, NULL
, 0);
398 nfc_hci_send_event(hdev
, NFC_HCI_RF_READER_A_GATE
,
399 NFC_HCI_EVT_END_OPERATION
, NULL
, 0);
402 if (tm_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
403 r
= nfc_hci_get_param(hdev
, ST21NFCA_RF_CARD_F_GATE
,
404 ST21NFCA_RF_CARD_F_DATARATE
,
409 /* Configure the maximum supported datarate to 424Kbps */
410 if (datarate_skb
->len
> 0 &&
411 datarate_skb
->data
[0] !=
412 ST21NFCA_RF_CARD_F_DATARATE_212_424
) {
413 param
[0] = ST21NFCA_RF_CARD_F_DATARATE_212_424
;
414 r
= nfc_hci_set_param(hdev
, ST21NFCA_RF_CARD_F_GATE
,
415 ST21NFCA_RF_CARD_F_DATARATE
,
418 kfree_skb(datarate_skb
);
422 kfree_skb(datarate_skb
);
427 * NFC Forum Digital Spec Table 7:
428 * NFCID1 size: triple (10 bytes)
432 r
= nfc_hci_set_param(hdev
, ST21NFCA_RF_CARD_F_GATE
,
433 ST21NFCA_RF_CARD_F_SENS_RES
, param
, 2);
440 * NFC Forum Digistal Spec Table 17:
441 * b3 set to 0b (value b7-b6):
442 * - 10b: Configured for NFC-DEP Protocol
445 r
= nfc_hci_set_param(hdev
, ST21NFCA_RF_CARD_F_GATE
,
446 ST21NFCA_RF_CARD_F_SEL_RES
, param
, 1);
450 /* Configure NFCID1 Random uid */
451 r
= nfc_hci_set_param(hdev
, ST21NFCA_RF_CARD_F_GATE
,
452 ST21NFCA_RF_CARD_F_NFCID1
, NULL
, 0);
456 /* Configure NFCID2_LIST */
469 /* 8 byte Pad bytes used for polling respone frame */
472 * Configuration byte:
473 * - bit 0: define the default NFCID2 entry used when the
474 * system code is equal to 'FFFF'
475 * - bit 1: use a random value for lowest 6 bytes of
477 * - bit 2: ignore polling request frame if request code
479 * - Other bits are RFU
482 r
= nfc_hci_set_param(hdev
, ST21NFCA_RF_CARD_F_GATE
,
483 ST21NFCA_RF_CARD_F_NFCID2_LIST
, param
,
489 r
= nfc_hci_set_param(hdev
, ST21NFCA_RF_CARD_F_GATE
,
490 ST21NFCA_RF_CARD_F_MODE
, param
, 1);
496 static void st21nfca_hci_stop_poll(struct nfc_hci_dev
*hdev
)
498 nfc_hci_send_cmd(hdev
, ST21NFCA_DEVICE_MGNT_GATE
,
499 ST21NFCA_DM_DISCONNECT
, NULL
, 0, NULL
);
502 static int st21nfca_get_iso14443_3_atqa(struct nfc_hci_dev
*hdev
, u16
*atqa
)
505 struct sk_buff
*atqa_skb
= NULL
;
507 r
= nfc_hci_get_param(hdev
, ST21NFCA_RF_READER_14443_3_A_GATE
,
508 ST21NFCA_RF_READER_14443_3_A_ATQA
, &atqa_skb
);
512 if (atqa_skb
->len
!= 2) {
517 *atqa
= be16_to_cpu(*(__be16
*) atqa_skb
->data
);
524 static int st21nfca_get_iso14443_3_sak(struct nfc_hci_dev
*hdev
, u8
*sak
)
527 struct sk_buff
*sak_skb
= NULL
;
529 r
= nfc_hci_get_param(hdev
, ST21NFCA_RF_READER_14443_3_A_GATE
,
530 ST21NFCA_RF_READER_14443_3_A_SAK
, &sak_skb
);
534 if (sak_skb
->len
!= 1) {
539 *sak
= sak_skb
->data
[0];
546 static int st21nfca_get_iso14443_3_uid(struct nfc_hci_dev
*hdev
, u8
*uid
,
550 struct sk_buff
*uid_skb
= NULL
;
552 r
= nfc_hci_get_param(hdev
, ST21NFCA_RF_READER_14443_3_A_GATE
,
553 ST21NFCA_RF_READER_14443_3_A_UID
, &uid_skb
);
557 if (uid_skb
->len
== 0 || uid_skb
->len
> NFC_NFCID1_MAXSIZE
) {
562 memcpy(uid
, uid_skb
->data
, uid_skb
->len
);
569 static int st21nfca_get_iso15693_inventory(struct nfc_hci_dev
*hdev
,
570 struct nfc_target
*target
)
573 struct sk_buff
*inventory_skb
= NULL
;
575 r
= nfc_hci_get_param(hdev
, ST21NFCA_RF_READER_ISO15693_GATE
,
576 ST21NFCA_RF_READER_ISO15693_INVENTORY
,
581 skb_pull(inventory_skb
, 2);
583 if (inventory_skb
->len
== 0 ||
584 inventory_skb
->len
> NFC_ISO15693_UID_MAXSIZE
) {
589 memcpy(target
->iso15693_uid
, inventory_skb
->data
, inventory_skb
->len
);
590 target
->iso15693_dsfid
= inventory_skb
->data
[1];
591 target
->is_iso15693
= 1;
593 kfree_skb(inventory_skb
);
597 static int st21nfca_hci_dep_link_up(struct nfc_hci_dev
*hdev
,
598 struct nfc_target
*target
, u8 comm_mode
,
599 u8
*gb
, size_t gb_len
)
601 struct st21nfca_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
603 info
->dep_info
.idx
= target
->idx
;
604 return st21nfca_im_send_atr_req(hdev
, gb
, gb_len
);
607 static int st21nfca_hci_dep_link_down(struct nfc_hci_dev
*hdev
)
609 struct st21nfca_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
611 info
->state
= ST21NFCA_ST_READY
;
613 return nfc_hci_send_cmd(hdev
, ST21NFCA_DEVICE_MGNT_GATE
,
614 ST21NFCA_DM_DISCONNECT
, NULL
, 0, NULL
);
617 static int st21nfca_hci_target_from_gate(struct nfc_hci_dev
*hdev
, u8 gate
,
618 struct nfc_target
*target
)
623 u8 uid
[NFC_NFCID1_MAXSIZE
];
626 case ST21NFCA_RF_READER_F_GATE
:
627 target
->supported_protocols
= NFC_PROTO_FELICA_MASK
;
629 case ST21NFCA_RF_READER_14443_3_A_GATE
:
630 /* ISO14443-3 type 1 or 2 tags */
631 r
= st21nfca_get_iso14443_3_atqa(hdev
, &atqa
);
634 if (atqa
== 0x000c) {
635 target
->supported_protocols
= NFC_PROTO_JEWEL_MASK
;
636 target
->sens_res
= 0x0c00;
638 r
= st21nfca_get_iso14443_3_sak(hdev
, &sak
);
642 r
= st21nfca_get_iso14443_3_uid(hdev
, uid
, &len
);
646 target
->supported_protocols
=
647 nfc_hci_sak_to_protocol(sak
);
648 if (target
->supported_protocols
== 0xffffffff)
651 target
->sens_res
= atqa
;
652 target
->sel_res
= sak
;
653 memcpy(target
->nfcid1
, uid
, len
);
654 target
->nfcid1_len
= len
;
658 case ST21NFCA_RF_READER_ISO15693_GATE
:
659 target
->supported_protocols
= NFC_PROTO_ISO15693_MASK
;
660 r
= st21nfca_get_iso15693_inventory(hdev
, target
);
671 static int st21nfca_hci_complete_target_discovered(struct nfc_hci_dev
*hdev
,
673 struct nfc_target
*target
)
676 struct sk_buff
*nfcid_skb
= NULL
;
678 if (gate
== ST21NFCA_RF_READER_F_GATE
) {
679 r
= nfc_hci_get_param(hdev
, ST21NFCA_RF_READER_F_GATE
,
680 ST21NFCA_RF_READER_F_NFCID2
, &nfcid_skb
);
684 if (nfcid_skb
->len
> NFC_SENSF_RES_MAXSIZE
) {
690 * - After the recepton of polling response for type F frame
691 * at 212 or 424 Kbit/s, NFCID2 registry parameters will be
693 * - After the reception of SEL_RES with NFCIP-1 compliant bit
694 * set for type A frame NFCID1 will be updated
696 if (nfcid_skb
->len
> 0) {
698 memcpy(target
->sensf_res
, nfcid_skb
->data
,
700 target
->sensf_res_len
= nfcid_skb
->len
;
701 /* NFC Forum Digital Protocol Table 44 */
702 if (target
->sensf_res
[0] == 0x01 &&
703 target
->sensf_res
[1] == 0xfe)
704 target
->supported_protocols
=
705 NFC_PROTO_NFC_DEP_MASK
;
707 target
->supported_protocols
=
708 NFC_PROTO_FELICA_MASK
;
710 kfree_skb(nfcid_skb
);
713 r
= nfc_hci_get_param(hdev
, ST21NFCA_RF_READER_F_GATE
,
714 ST21NFCA_RF_READER_F_NFCID1
,
719 if (nfcid_skb
->len
> NFC_NFCID1_MAXSIZE
) {
723 memcpy(target
->sensf_res
, nfcid_skb
->data
,
725 target
->sensf_res_len
= nfcid_skb
->len
;
726 target
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
728 target
->hci_reader_gate
= ST21NFCA_RF_READER_F_GATE
;
732 kfree_skb(nfcid_skb
);
736 #define ST21NFCA_CB_TYPE_READER_ISO15693 1
737 static void st21nfca_hci_data_exchange_cb(void *context
, struct sk_buff
*skb
,
740 struct st21nfca_hci_info
*info
= context
;
742 switch (info
->async_cb_type
) {
743 case ST21NFCA_CB_TYPE_READER_ISO15693
:
745 skb_trim(skb
, skb
->len
- 1);
746 info
->async_cb(info
->async_cb_context
, skb
, err
);
757 * <= 0: driver handled the data exchange
758 * 1: driver doesn't especially handle, please do standard processing
760 static int st21nfca_hci_im_transceive(struct nfc_hci_dev
*hdev
,
761 struct nfc_target
*target
,
763 data_exchange_cb_t cb
, void *cb_context
)
765 struct st21nfca_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
767 pr_info(DRIVER_DESC
": %s for gate=%d len=%d\n", __func__
,
768 target
->hci_reader_gate
, skb
->len
);
770 switch (target
->hci_reader_gate
) {
771 case ST21NFCA_RF_READER_F_GATE
:
772 if (target
->supported_protocols
== NFC_PROTO_NFC_DEP_MASK
)
773 return st21nfca_im_send_dep_req(hdev
, skb
);
775 *(u8
*)skb_push(skb
, 1) = 0x1a;
776 return nfc_hci_send_cmd_async(hdev
, target
->hci_reader_gate
,
777 ST21NFCA_WR_XCHG_DATA
, skb
->data
,
778 skb
->len
, cb
, cb_context
);
779 case ST21NFCA_RF_READER_14443_3_A_GATE
:
780 *(u8
*)skb_push(skb
, 1) = 0x1a; /* CTR, see spec:10.2.2.1 */
782 return nfc_hci_send_cmd_async(hdev
, target
->hci_reader_gate
,
783 ST21NFCA_WR_XCHG_DATA
, skb
->data
,
784 skb
->len
, cb
, cb_context
);
785 case ST21NFCA_RF_READER_ISO15693_GATE
:
786 info
->async_cb_type
= ST21NFCA_CB_TYPE_READER_ISO15693
;
788 info
->async_cb_context
= cb_context
;
790 *(u8
*)skb_push(skb
, 1) = 0x17;
792 return nfc_hci_send_cmd_async(hdev
, target
->hci_reader_gate
,
793 ST21NFCA_WR_XCHG_DATA
, skb
->data
,
795 st21nfca_hci_data_exchange_cb
,
803 static int st21nfca_hci_tm_send(struct nfc_hci_dev
*hdev
, struct sk_buff
*skb
)
805 return st21nfca_tm_send_dep_res(hdev
, skb
);
808 static int st21nfca_hci_check_presence(struct nfc_hci_dev
*hdev
,
809 struct nfc_target
*target
)
813 switch (target
->hci_reader_gate
) {
814 case NFC_HCI_RF_READER_A_GATE
:
815 case NFC_HCI_RF_READER_B_GATE
:
817 * PRESENCE_CHECK on those gates is available
818 * However, the answer to this command is taking 3 * fwi
819 * if the card is no present.
820 * Instead, we send an empty I-Frame with a very short
821 * configurable fwi ~604µs.
823 return nfc_hci_send_cmd(hdev
, target
->hci_reader_gate
,
824 ST21NFCA_WR_XCHG_DATA
, &fwi
, 1, NULL
);
825 case ST21NFCA_RF_READER_14443_3_A_GATE
:
826 return nfc_hci_send_cmd(hdev
, target
->hci_reader_gate
,
827 ST21NFCA_RF_READER_CMD_PRESENCE_CHECK
,
834 static void st21nfca_hci_cmd_received(struct nfc_hci_dev
*hdev
, u8 pipe
, u8 cmd
,
837 struct st21nfca_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
838 u8 gate
= hdev
->pipes
[pipe
].gate
;
840 pr_debug("cmd: %x\n", cmd
);
843 case NFC_HCI_ANY_OPEN_PIPE
:
844 if (gate
!= ST21NFCA_APDU_READER_GATE
&&
845 hdev
->pipes
[pipe
].dest_host
!= NFC_HCI_UICC_HOST_ID
)
846 info
->se_info
.count_pipes
++;
848 if (info
->se_info
.count_pipes
== info
->se_info
.expected_pipes
) {
849 del_timer_sync(&info
->se_info
.se_active_timer
);
850 info
->se_info
.se_active
= false;
851 info
->se_info
.count_pipes
= 0;
852 complete(&info
->se_info
.req_completion
);
858 static int st21nfca_admin_event_received(struct nfc_hci_dev
*hdev
, u8 event
,
861 struct st21nfca_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
863 pr_debug("admin event: %x\n", event
);
866 case ST21NFCA_EVT_HOT_PLUG
:
867 if (info
->se_info
.se_active
) {
868 if (!ST21NFCA_EVT_HOT_PLUG_IS_INHIBITED(skb
)) {
869 del_timer_sync(&info
->se_info
.se_active_timer
);
870 info
->se_info
.se_active
= false;
871 complete(&info
->se_info
.req_completion
);
873 mod_timer(&info
->se_info
.se_active_timer
,
875 msecs_to_jiffies(ST21NFCA_SE_TO_PIPES
));
880 nfc_err(&hdev
->ndev
->dev
, "Unexpected event on admin gate\n");
888 * <= 0: driver handled the event, skb consumed
889 * 1: driver does not handle the event, please do standard processing
891 static int st21nfca_hci_event_received(struct nfc_hci_dev
*hdev
, u8 pipe
,
892 u8 event
, struct sk_buff
*skb
)
894 u8 gate
= hdev
->pipes
[pipe
].gate
;
895 u8 host
= hdev
->pipes
[pipe
].dest_host
;
897 pr_debug("hci event: %d gate: %x\n", event
, gate
);
900 case NFC_HCI_ADMIN_GATE
:
901 return st21nfca_admin_event_received(hdev
, event
, skb
);
902 case ST21NFCA_RF_CARD_F_GATE
:
903 return st21nfca_dep_event_received(hdev
, event
, skb
);
904 case ST21NFCA_CONNECTIVITY_GATE
:
905 return st21nfca_connectivity_event_received(hdev
, host
,
907 case ST21NFCA_APDU_READER_GATE
:
908 return st21nfca_apdu_reader_event_received(hdev
, event
, skb
);
909 case NFC_HCI_LOOPBACK_GATE
:
910 return st21nfca_hci_loopback_event_received(hdev
, event
, skb
);
916 static struct nfc_hci_ops st21nfca_hci_ops
= {
917 .open
= st21nfca_hci_open
,
918 .close
= st21nfca_hci_close
,
919 .load_session
= st21nfca_hci_load_session
,
920 .hci_ready
= st21nfca_hci_ready
,
921 .xmit
= st21nfca_hci_xmit
,
922 .start_poll
= st21nfca_hci_start_poll
,
923 .stop_poll
= st21nfca_hci_stop_poll
,
924 .dep_link_up
= st21nfca_hci_dep_link_up
,
925 .dep_link_down
= st21nfca_hci_dep_link_down
,
926 .target_from_gate
= st21nfca_hci_target_from_gate
,
927 .complete_target_discovered
= st21nfca_hci_complete_target_discovered
,
928 .im_transceive
= st21nfca_hci_im_transceive
,
929 .tm_send
= st21nfca_hci_tm_send
,
930 .check_presence
= st21nfca_hci_check_presence
,
931 .event_received
= st21nfca_hci_event_received
,
932 .cmd_received
= st21nfca_hci_cmd_received
,
933 .discover_se
= st21nfca_hci_discover_se
,
934 .enable_se
= st21nfca_hci_enable_se
,
935 .disable_se
= st21nfca_hci_disable_se
,
936 .se_io
= st21nfca_hci_se_io
,
939 int st21nfca_hci_probe(void *phy_id
, struct nfc_phy_ops
*phy_ops
,
940 char *llc_name
, int phy_headroom
, int phy_tailroom
,
941 int phy_payload
, struct nfc_hci_dev
**hdev
,
942 struct st21nfca_se_status
*se_status
)
944 struct st21nfca_hci_info
*info
;
948 struct nfc_hci_init_data init_data
;
949 unsigned long quirks
= 0;
951 info
= kzalloc(sizeof(struct st21nfca_hci_info
), GFP_KERNEL
);
955 info
->phy_ops
= phy_ops
;
956 info
->phy_id
= phy_id
;
957 info
->state
= ST21NFCA_ST_COLD
;
958 mutex_init(&info
->info_lock
);
960 init_data
.gate_count
= ARRAY_SIZE(st21nfca_gates
);
962 memcpy(init_data
.gates
, st21nfca_gates
, sizeof(st21nfca_gates
));
965 * Session id must include the driver name + i2c bus addr
966 * persistent info to discriminate 2 identical chips
968 dev_num
= find_first_zero_bit(dev_mask
, ST21NFCA_NUM_DEVICES
);
969 if (dev_num
>= ST21NFCA_NUM_DEVICES
) {
974 set_bit(dev_num
, dev_mask
);
976 scnprintf(init_data
.session_id
, sizeof(init_data
.session_id
), "%s%2x",
979 protocols
= NFC_PROTO_JEWEL_MASK
|
980 NFC_PROTO_MIFARE_MASK
|
981 NFC_PROTO_FELICA_MASK
|
982 NFC_PROTO_ISO14443_MASK
|
983 NFC_PROTO_ISO14443_B_MASK
|
984 NFC_PROTO_ISO15693_MASK
|
985 NFC_PROTO_NFC_DEP_MASK
;
987 set_bit(NFC_HCI_QUIRK_SHORT_CLEAR
, &quirks
);
990 nfc_hci_allocate_device(&st21nfca_hci_ops
, &init_data
, quirks
,
992 phy_headroom
+ ST21NFCA_CMDS_HEADROOM
,
993 phy_tailroom
, phy_payload
);
996 pr_err("Cannot allocate nfc hdev.\n");
1001 info
->se_status
= se_status
;
1003 nfc_hci_set_clientdata(info
->hdev
, info
);
1005 r
= nfc_hci_register_device(info
->hdev
);
1010 st21nfca_dep_init(info
->hdev
);
1011 st21nfca_se_init(info
->hdev
);
1012 st21nfca_vendor_cmds_init(info
->hdev
);
1017 nfc_hci_free_device(info
->hdev
);
1024 EXPORT_SYMBOL(st21nfca_hci_probe
);
1026 void st21nfca_hci_remove(struct nfc_hci_dev
*hdev
)
1028 struct st21nfca_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
1030 st21nfca_dep_deinit(hdev
);
1031 st21nfca_se_deinit(hdev
);
1032 nfc_hci_unregister_device(hdev
);
1033 nfc_hci_free_device(hdev
);
1036 EXPORT_SYMBOL(st21nfca_hci_remove
);
1038 MODULE_LICENSE("GPL");
1039 MODULE_DESCRIPTION(DRIVER_DESC
);