1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
6 #include <net/nfc/hci.h>
10 #define ST21NFCA_NFCIP1_INITIATOR 0x00
11 #define ST21NFCA_NFCIP1_REQ 0xd4
12 #define ST21NFCA_NFCIP1_RES 0xd5
13 #define ST21NFCA_NFCIP1_ATR_REQ 0x00
14 #define ST21NFCA_NFCIP1_ATR_RES 0x01
15 #define ST21NFCA_NFCIP1_PSL_REQ 0x04
16 #define ST21NFCA_NFCIP1_PSL_RES 0x05
17 #define ST21NFCA_NFCIP1_DEP_REQ 0x06
18 #define ST21NFCA_NFCIP1_DEP_RES 0x07
20 #define ST21NFCA_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
21 #define ST21NFCA_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
22 #define ST21NFCA_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
23 ((pfb) & ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT)
24 #define ST21NFCA_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & 0x04)
25 #define ST21NFCA_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
26 #define ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT 0x10
28 #define ST21NFCA_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
29 ((pfb) & ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT)
31 #define ST21NFCA_NFC_DEP_PFB_I_PDU 0x00
32 #define ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU 0x40
33 #define ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
35 #define ST21NFCA_ATR_REQ_MIN_SIZE 17
36 #define ST21NFCA_ATR_REQ_MAX_SIZE 65
37 #define ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B 0x30
38 #define ST21NFCA_GB_BIT 0x02
40 #define ST21NFCA_EVT_SEND_DATA 0x10
41 #define ST21NFCA_EVT_FIELD_ON 0x11
42 #define ST21NFCA_EVT_CARD_DEACTIVATED 0x12
43 #define ST21NFCA_EVT_CARD_ACTIVATED 0x13
44 #define ST21NFCA_EVT_FIELD_OFF 0x14
46 #define ST21NFCA_EVT_CARD_F_BITRATE 0x16
47 #define ST21NFCA_EVT_READER_F_BITRATE 0x13
48 #define ST21NFCA_PSL_REQ_SEND_SPEED(brs) (brs & 0x38)
49 #define ST21NFCA_PSL_REQ_RECV_SPEED(brs) (brs & 0x07)
50 #define ST21NFCA_PP2LRI(pp) ((pp & 0x30) >> 4)
51 #define ST21NFCA_CARD_BITRATE_212 0x01
52 #define ST21NFCA_CARD_BITRATE_424 0x02
54 #define ST21NFCA_DEFAULT_TIMEOUT 0x0a
57 #define PROTOCOL_ERR(req) pr_err("%d: ST21NFCA Protocol error: %s\n", \
60 struct st21nfca_atr_req
{
64 u8 nfcid3
[NFC_NFCID3_MAXSIZE
];
72 struct st21nfca_atr_res
{
76 u8 nfcid3
[NFC_NFCID3_MAXSIZE
];
85 struct st21nfca_psl_req
{
94 struct st21nfca_psl_res
{
101 struct st21nfca_dep_req_res
{
110 static void st21nfca_tx_work(struct work_struct
*work
)
112 struct st21nfca_hci_info
*info
= container_of(work
,
113 struct st21nfca_hci_info
,
120 dev
= info
->hdev
->ndev
;
121 skb
= info
->dep_info
.tx_pending
;
123 device_lock(&dev
->dev
);
125 nfc_hci_send_cmd_async(info
->hdev
, ST21NFCA_RF_READER_F_GATE
,
126 ST21NFCA_WR_XCHG_DATA
, skb
->data
, skb
->len
,
127 info
->async_cb
, info
);
128 device_unlock(&dev
->dev
);
133 static void st21nfca_im_send_pdu(struct st21nfca_hci_info
*info
,
136 info
->dep_info
.tx_pending
= skb
;
137 schedule_work(&info
->dep_info
.tx_work
);
140 static int st21nfca_tm_send_atr_res(struct nfc_hci_dev
*hdev
,
141 struct st21nfca_atr_req
*atr_req
)
143 struct st21nfca_atr_res
*atr_res
;
147 struct st21nfca_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
149 gb_len
= atr_req
->length
- sizeof(struct st21nfca_atr_req
);
150 skb
= alloc_skb(atr_req
->length
+ 1, GFP_KERNEL
);
154 skb_put(skb
, sizeof(struct st21nfca_atr_res
));
156 atr_res
= (struct st21nfca_atr_res
*)skb
->data
;
157 memset(atr_res
, 0, sizeof(struct st21nfca_atr_res
));
159 atr_res
->length
= atr_req
->length
+ 1;
160 atr_res
->cmd0
= ST21NFCA_NFCIP1_RES
;
161 atr_res
->cmd1
= ST21NFCA_NFCIP1_ATR_RES
;
163 memcpy(atr_res
->nfcid3
, atr_req
->nfcid3
, 6);
166 atr_res
->to
= ST21NFCA_DEFAULT_TIMEOUT
;
167 atr_res
->ppi
= ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B
;
170 skb_put(skb
, gb_len
);
172 atr_res
->ppi
|= ST21NFCA_GB_BIT
;
173 memcpy(atr_res
->gbi
, atr_req
->gbi
, gb_len
);
174 r
= nfc_set_remote_general_bytes(hdev
->ndev
, atr_res
->gbi
,
182 info
->dep_info
.curr_nfc_dep_pni
= 0;
184 r
= nfc_hci_send_event(hdev
, ST21NFCA_RF_CARD_F_GATE
,
185 ST21NFCA_EVT_SEND_DATA
, skb
->data
, skb
->len
);
190 static int st21nfca_tm_recv_atr_req(struct nfc_hci_dev
*hdev
,
193 struct st21nfca_atr_req
*atr_req
;
197 skb_trim(skb
, skb
->len
- 1);
204 if (skb
->len
< ST21NFCA_ATR_REQ_MIN_SIZE
) {
209 atr_req
= (struct st21nfca_atr_req
*)skb
->data
;
211 if (atr_req
->length
< sizeof(struct st21nfca_atr_req
)) {
216 r
= st21nfca_tm_send_atr_res(hdev
, atr_req
);
220 gb_len
= skb
->len
- sizeof(struct st21nfca_atr_req
);
222 r
= nfc_tm_activated(hdev
->ndev
, NFC_PROTO_NFC_DEP_MASK
,
223 NFC_COMM_PASSIVE
, atr_req
->gbi
, gb_len
);
233 static int st21nfca_tm_send_psl_res(struct nfc_hci_dev
*hdev
,
234 struct st21nfca_psl_req
*psl_req
)
236 struct st21nfca_psl_res
*psl_res
;
238 u8 bitrate
[2] = {0, 0};
241 skb
= alloc_skb(sizeof(struct st21nfca_psl_res
), GFP_KERNEL
);
244 skb_put(skb
, sizeof(struct st21nfca_psl_res
));
246 psl_res
= (struct st21nfca_psl_res
*)skb
->data
;
248 psl_res
->length
= sizeof(struct st21nfca_psl_res
);
249 psl_res
->cmd0
= ST21NFCA_NFCIP1_RES
;
250 psl_res
->cmd1
= ST21NFCA_NFCIP1_PSL_RES
;
251 psl_res
->did
= psl_req
->did
;
253 r
= nfc_hci_send_event(hdev
, ST21NFCA_RF_CARD_F_GATE
,
254 ST21NFCA_EVT_SEND_DATA
, skb
->data
, skb
->len
);
259 * ST21NFCA only support P2P passive.
260 * PSL_REQ BRS value != 0 has only a meaning to
261 * change technology to type F.
262 * We change to BITRATE 424Kbits.
263 * In other case switch to BITRATE 106Kbits.
265 if (ST21NFCA_PSL_REQ_SEND_SPEED(psl_req
->brs
) &&
266 ST21NFCA_PSL_REQ_RECV_SPEED(psl_req
->brs
)) {
267 bitrate
[0] = ST21NFCA_CARD_BITRATE_424
;
268 bitrate
[1] = ST21NFCA_CARD_BITRATE_424
;
271 /* Send an event to change bitrate change event to card f */
272 r
= nfc_hci_send_event(hdev
, ST21NFCA_RF_CARD_F_GATE
,
273 ST21NFCA_EVT_CARD_F_BITRATE
, bitrate
, 2);
279 static int st21nfca_tm_recv_psl_req(struct nfc_hci_dev
*hdev
,
282 struct st21nfca_psl_req
*psl_req
;
285 skb_trim(skb
, skb
->len
- 1);
292 psl_req
= (struct st21nfca_psl_req
*)skb
->data
;
294 if (skb
->len
< sizeof(struct st21nfca_psl_req
)) {
299 r
= st21nfca_tm_send_psl_res(hdev
, psl_req
);
304 int st21nfca_tm_send_dep_res(struct nfc_hci_dev
*hdev
, struct sk_buff
*skb
)
307 struct st21nfca_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
309 *(u8
*)skb_push(skb
, 1) = info
->dep_info
.curr_nfc_dep_pni
;
310 *(u8
*)skb_push(skb
, 1) = ST21NFCA_NFCIP1_DEP_RES
;
311 *(u8
*)skb_push(skb
, 1) = ST21NFCA_NFCIP1_RES
;
312 *(u8
*)skb_push(skb
, 1) = skb
->len
;
314 r
= nfc_hci_send_event(hdev
, ST21NFCA_RF_CARD_F_GATE
,
315 ST21NFCA_EVT_SEND_DATA
, skb
->data
, skb
->len
);
320 EXPORT_SYMBOL(st21nfca_tm_send_dep_res
);
322 static int st21nfca_tm_recv_dep_req(struct nfc_hci_dev
*hdev
,
325 struct st21nfca_dep_req_res
*dep_req
;
328 struct st21nfca_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
330 skb_trim(skb
, skb
->len
- 1);
334 dep_req
= (struct st21nfca_dep_req_res
*)skb
->data
;
335 if (skb
->len
< size
) {
340 if (ST21NFCA_NFC_DEP_DID_BIT_SET(dep_req
->pfb
))
342 if (ST21NFCA_NFC_DEP_NAD_BIT_SET(dep_req
->pfb
))
345 if (skb
->len
< size
) {
350 /* Receiving DEP_REQ - Decoding */
351 switch (ST21NFCA_NFC_DEP_PFB_TYPE(dep_req
->pfb
)) {
352 case ST21NFCA_NFC_DEP_PFB_I_PDU
:
353 info
->dep_info
.curr_nfc_dep_pni
=
354 ST21NFCA_NFC_DEP_PFB_PNI(dep_req
->pfb
);
356 case ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU
:
357 pr_err("Received a ACK/NACK PDU\n");
359 case ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU
:
360 pr_err("Received a SUPERVISOR PDU\n");
366 return nfc_tm_data_received(hdev
->ndev
, skb
);
371 static int st21nfca_tm_event_send_data(struct nfc_hci_dev
*hdev
,
379 case ST21NFCA_NFCIP1_REQ
:
382 case ST21NFCA_NFCIP1_ATR_REQ
:
383 r
= st21nfca_tm_recv_atr_req(hdev
, skb
);
385 case ST21NFCA_NFCIP1_PSL_REQ
:
386 r
= st21nfca_tm_recv_psl_req(hdev
, skb
);
388 case ST21NFCA_NFCIP1_DEP_REQ
:
389 r
= st21nfca_tm_recv_dep_req(hdev
, skb
);
403 * <= 0: driver handled the event, skb consumed
404 * 1: driver does not handle the event, please do standard processing
406 int st21nfca_dep_event_received(struct nfc_hci_dev
*hdev
,
407 u8 event
, struct sk_buff
*skb
)
410 struct st21nfca_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
412 pr_debug("dep event: %d\n", event
);
415 case ST21NFCA_EVT_CARD_ACTIVATED
:
416 info
->dep_info
.curr_nfc_dep_pni
= 0;
418 case ST21NFCA_EVT_CARD_DEACTIVATED
:
420 case ST21NFCA_EVT_FIELD_ON
:
422 case ST21NFCA_EVT_FIELD_OFF
:
424 case ST21NFCA_EVT_SEND_DATA
:
425 r
= st21nfca_tm_event_send_data(hdev
, skb
);
430 nfc_err(&hdev
->ndev
->dev
, "Unexpected event on card f gate\n");
436 EXPORT_SYMBOL(st21nfca_dep_event_received
);
438 static void st21nfca_im_send_psl_req(struct nfc_hci_dev
*hdev
, u8 did
, u8 bsi
,
442 struct st21nfca_psl_req
*psl_req
;
443 struct st21nfca_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
446 alloc_skb(sizeof(struct st21nfca_psl_req
) + 1, GFP_KERNEL
);
451 skb_put(skb
, sizeof(struct st21nfca_psl_req
));
452 psl_req
= (struct st21nfca_psl_req
*) skb
->data
;
454 psl_req
->length
= sizeof(struct st21nfca_psl_req
);
455 psl_req
->cmd0
= ST21NFCA_NFCIP1_REQ
;
456 psl_req
->cmd1
= ST21NFCA_NFCIP1_PSL_REQ
;
458 psl_req
->brs
= (0x30 & bsi
<< 4) | (bri
& 0x03);
461 *(u8
*)skb_push(skb
, 1) = info
->dep_info
.to
| 0x10;
463 st21nfca_im_send_pdu(info
, skb
);
466 #define ST21NFCA_CB_TYPE_READER_F 1
467 static void st21nfca_im_recv_atr_res_cb(void *context
, struct sk_buff
*skb
,
470 struct st21nfca_hci_info
*info
= context
;
471 struct st21nfca_atr_res
*atr_res
;
480 switch (info
->async_cb_type
) {
481 case ST21NFCA_CB_TYPE_READER_F
:
482 skb_trim(skb
, skb
->len
- 1);
483 atr_res
= (struct st21nfca_atr_res
*)skb
->data
;
484 r
= nfc_set_remote_general_bytes(info
->hdev
->ndev
,
486 skb
->len
- sizeof(struct st21nfca_atr_res
));
490 if (atr_res
->to
>= 0x0e)
491 info
->dep_info
.to
= 0x0e;
493 info
->dep_info
.to
= atr_res
->to
+ 1;
495 info
->dep_info
.to
|= 0x10;
497 r
= nfc_dep_link_is_up(info
->hdev
->ndev
, info
->dep_info
.idx
,
498 NFC_COMM_PASSIVE
, NFC_RF_INITIATOR
);
502 info
->dep_info
.curr_nfc_dep_pni
= 0;
503 if (ST21NFCA_PP2LRI(atr_res
->ppi
) != info
->dep_info
.lri
)
504 st21nfca_im_send_psl_req(info
->hdev
, atr_res
->did
,
505 atr_res
->bsi
, atr_res
->bri
,
506 ST21NFCA_PP2LRI(atr_res
->ppi
));
514 int st21nfca_im_send_atr_req(struct nfc_hci_dev
*hdev
, u8
*gb
, size_t gb_len
)
517 struct st21nfca_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
518 struct st21nfca_atr_req
*atr_req
;
519 struct nfc_target
*target
;
522 info
->dep_info
.to
= ST21NFCA_DEFAULT_TIMEOUT
;
523 size
= ST21NFCA_ATR_REQ_MIN_SIZE
+ gb_len
;
524 if (size
> ST21NFCA_ATR_REQ_MAX_SIZE
) {
525 PROTOCOL_ERR("14.6.1.1");
530 alloc_skb(sizeof(struct st21nfca_atr_req
) + gb_len
+ 1, GFP_KERNEL
);
536 skb_put(skb
, sizeof(struct st21nfca_atr_req
));
538 atr_req
= (struct st21nfca_atr_req
*)skb
->data
;
539 memset(atr_req
, 0, sizeof(struct st21nfca_atr_req
));
541 atr_req
->cmd0
= ST21NFCA_NFCIP1_REQ
;
542 atr_req
->cmd1
= ST21NFCA_NFCIP1_ATR_REQ
;
543 memset(atr_req
->nfcid3
, 0, NFC_NFCID3_MAXSIZE
);
544 target
= hdev
->ndev
->targets
;
546 if (target
->sensf_res_len
> 0)
547 memcpy(atr_req
->nfcid3
, target
->sensf_res
,
548 target
->sensf_res_len
);
550 get_random_bytes(atr_req
->nfcid3
, NFC_NFCID3_MAXSIZE
);
556 atr_req
->ppi
= ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B
;
558 atr_req
->ppi
|= ST21NFCA_GB_BIT
;
559 skb_put_data(skb
, gb
, gb_len
);
561 atr_req
->length
= sizeof(struct st21nfca_atr_req
) + hdev
->gb_len
;
563 *(u8
*)skb_push(skb
, 1) = info
->dep_info
.to
| 0x10; /* timeout */
565 info
->async_cb_type
= ST21NFCA_CB_TYPE_READER_F
;
566 info
->async_cb_context
= info
;
567 info
->async_cb
= st21nfca_im_recv_atr_res_cb
;
568 info
->dep_info
.bri
= atr_req
->bri
;
569 info
->dep_info
.bsi
= atr_req
->bsi
;
570 info
->dep_info
.lri
= ST21NFCA_PP2LRI(atr_req
->ppi
);
572 return nfc_hci_send_cmd_async(hdev
, ST21NFCA_RF_READER_F_GATE
,
573 ST21NFCA_WR_XCHG_DATA
, skb
->data
,
574 skb
->len
, info
->async_cb
, info
);
576 EXPORT_SYMBOL(st21nfca_im_send_atr_req
);
578 static void st21nfca_im_recv_dep_res_cb(void *context
, struct sk_buff
*skb
,
581 struct st21nfca_hci_info
*info
= context
;
582 struct st21nfca_dep_req_res
*dep_res
;
592 switch (info
->async_cb_type
) {
593 case ST21NFCA_CB_TYPE_READER_F
:
594 dep_res
= (struct st21nfca_dep_req_res
*)skb
->data
;
600 if (ST21NFCA_NFC_DEP_DID_BIT_SET(dep_res
->pfb
))
602 if (ST21NFCA_NFC_DEP_NAD_BIT_SET(dep_res
->pfb
))
608 skb_trim(skb
, skb
->len
- 1);
610 /* Receiving DEP_REQ - Decoding */
611 switch (ST21NFCA_NFC_DEP_PFB_TYPE(dep_res
->pfb
)) {
612 case ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU
:
613 pr_err("Received a ACK/NACK PDU\n");
615 case ST21NFCA_NFC_DEP_PFB_I_PDU
:
616 info
->dep_info
.curr_nfc_dep_pni
=
617 ST21NFCA_NFC_DEP_PFB_PNI(dep_res
->pfb
+ 1);
620 nfc_tm_data_received(info
->hdev
->ndev
, skb
);
622 case ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU
:
623 pr_err("Received a SUPERVISOR PDU\n");
625 *(u8
*)skb_push(skb
, 1) = ST21NFCA_NFCIP1_DEP_REQ
;
626 *(u8
*)skb_push(skb
, 1) = ST21NFCA_NFCIP1_REQ
;
627 *(u8
*)skb_push(skb
, 1) = skb
->len
;
628 *(u8
*)skb_push(skb
, 1) = info
->dep_info
.to
| 0x10;
630 st21nfca_im_send_pdu(info
, skb
);
643 int st21nfca_im_send_dep_req(struct nfc_hci_dev
*hdev
, struct sk_buff
*skb
)
645 struct st21nfca_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
647 info
->async_cb_type
= ST21NFCA_CB_TYPE_READER_F
;
648 info
->async_cb_context
= info
;
649 info
->async_cb
= st21nfca_im_recv_dep_res_cb
;
651 *(u8
*)skb_push(skb
, 1) = info
->dep_info
.curr_nfc_dep_pni
;
652 *(u8
*)skb_push(skb
, 1) = ST21NFCA_NFCIP1_DEP_REQ
;
653 *(u8
*)skb_push(skb
, 1) = ST21NFCA_NFCIP1_REQ
;
654 *(u8
*)skb_push(skb
, 1) = skb
->len
;
656 *(u8
*)skb_push(skb
, 1) = info
->dep_info
.to
| 0x10;
658 return nfc_hci_send_cmd_async(hdev
, ST21NFCA_RF_READER_F_GATE
,
659 ST21NFCA_WR_XCHG_DATA
,
661 info
->async_cb
, info
);
663 EXPORT_SYMBOL(st21nfca_im_send_dep_req
);
665 void st21nfca_dep_init(struct nfc_hci_dev
*hdev
)
667 struct st21nfca_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
669 INIT_WORK(&info
->dep_info
.tx_work
, st21nfca_tx_work
);
670 info
->dep_info
.curr_nfc_dep_pni
= 0;
671 info
->dep_info
.idx
= 0;
672 info
->dep_info
.to
= ST21NFCA_DEFAULT_TIMEOUT
;
674 EXPORT_SYMBOL(st21nfca_dep_init
);
676 void st21nfca_dep_deinit(struct nfc_hci_dev
*hdev
)
678 struct st21nfca_hci_info
*info
= nfc_hci_get_clientdata(hdev
);
680 cancel_work_sync(&info
->dep_info
.tx_work
);
682 EXPORT_SYMBOL(st21nfca_dep_deinit
);