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,
180 info->dep_info.curr_nfc_dep_pni = 0;
182 r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
183 ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
188 static int st21nfca_tm_recv_atr_req(struct nfc_hci_dev *hdev,
191 struct st21nfca_atr_req *atr_req;
195 skb_trim(skb, skb->len - 1);
202 if (skb->len < ST21NFCA_ATR_REQ_MIN_SIZE) {
207 atr_req = (struct st21nfca_atr_req *)skb->data;
209 if (atr_req->length < sizeof(struct st21nfca_atr_req)) {
214 r = st21nfca_tm_send_atr_res(hdev, atr_req);
218 gb_len = skb->len - sizeof(struct st21nfca_atr_req);
220 r = nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK,
221 NFC_COMM_PASSIVE, atr_req->gbi, gb_len);
231 static int st21nfca_tm_send_psl_res(struct nfc_hci_dev *hdev,
232 struct st21nfca_psl_req *psl_req)
234 struct st21nfca_psl_res *psl_res;
236 u8 bitrate[2] = {0, 0};
239 skb = alloc_skb(sizeof(struct st21nfca_psl_res), GFP_KERNEL);
242 skb_put(skb, sizeof(struct st21nfca_psl_res));
244 psl_res = (struct st21nfca_psl_res *)skb->data;
246 psl_res->length = sizeof(struct st21nfca_psl_res);
247 psl_res->cmd0 = ST21NFCA_NFCIP1_RES;
248 psl_res->cmd1 = ST21NFCA_NFCIP1_PSL_RES;
249 psl_res->did = psl_req->did;
251 r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
252 ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
257 * ST21NFCA only support P2P passive.
258 * PSL_REQ BRS value != 0 has only a meaning to
259 * change technology to type F.
260 * We change to BITRATE 424Kbits.
261 * In other case switch to BITRATE 106Kbits.
263 if (ST21NFCA_PSL_REQ_SEND_SPEED(psl_req->brs) &&
264 ST21NFCA_PSL_REQ_RECV_SPEED(psl_req->brs)) {
265 bitrate[0] = ST21NFCA_CARD_BITRATE_424;
266 bitrate[1] = ST21NFCA_CARD_BITRATE_424;
269 /* Send an event to change bitrate change event to card f */
270 r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
271 ST21NFCA_EVT_CARD_F_BITRATE, bitrate, 2);
277 static int st21nfca_tm_recv_psl_req(struct nfc_hci_dev *hdev,
280 struct st21nfca_psl_req *psl_req;
283 skb_trim(skb, skb->len - 1);
290 psl_req = (struct st21nfca_psl_req *)skb->data;
292 if (skb->len < sizeof(struct st21nfca_psl_req)) {
297 r = st21nfca_tm_send_psl_res(hdev, psl_req);
302 int st21nfca_tm_send_dep_res(struct nfc_hci_dev *hdev, struct sk_buff *skb)
305 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
307 *(u8 *)skb_push(skb, 1) = info->dep_info.curr_nfc_dep_pni;
308 *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_RES;
309 *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_RES;
310 *(u8 *)skb_push(skb, 1) = skb->len;
312 r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
313 ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
318 EXPORT_SYMBOL(st21nfca_tm_send_dep_res);
320 static int st21nfca_tm_recv_dep_req(struct nfc_hci_dev *hdev,
323 struct st21nfca_dep_req_res *dep_req;
326 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
328 skb_trim(skb, skb->len - 1);
332 dep_req = (struct st21nfca_dep_req_res *)skb->data;
333 if (skb->len < size) {
338 if (ST21NFCA_NFC_DEP_DID_BIT_SET(dep_req->pfb))
340 if (ST21NFCA_NFC_DEP_NAD_BIT_SET(dep_req->pfb))
343 if (skb->len < size) {
348 /* Receiving DEP_REQ - Decoding */
349 switch (ST21NFCA_NFC_DEP_PFB_TYPE(dep_req->pfb)) {
350 case ST21NFCA_NFC_DEP_PFB_I_PDU:
351 info->dep_info.curr_nfc_dep_pni =
352 ST21NFCA_NFC_DEP_PFB_PNI(dep_req->pfb);
354 case ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU:
355 pr_err("Received a ACK/NACK PDU\n");
357 case ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU:
358 pr_err("Received a SUPERVISOR PDU\n");
364 return nfc_tm_data_received(hdev->ndev, skb);
369 static int st21nfca_tm_event_send_data(struct nfc_hci_dev *hdev,
377 case ST21NFCA_NFCIP1_REQ:
380 case ST21NFCA_NFCIP1_ATR_REQ:
381 r = st21nfca_tm_recv_atr_req(hdev, skb);
383 case ST21NFCA_NFCIP1_PSL_REQ:
384 r = st21nfca_tm_recv_psl_req(hdev, skb);
386 case ST21NFCA_NFCIP1_DEP_REQ:
387 r = st21nfca_tm_recv_dep_req(hdev, skb);
401 * <= 0: driver handled the event, skb consumed
402 * 1: driver does not handle the event, please do standard processing
404 int st21nfca_dep_event_received(struct nfc_hci_dev *hdev,
405 u8 event, struct sk_buff *skb)
408 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
410 pr_debug("dep event: %d\n", event);
413 case ST21NFCA_EVT_CARD_ACTIVATED:
414 info->dep_info.curr_nfc_dep_pni = 0;
416 case ST21NFCA_EVT_CARD_DEACTIVATED:
418 case ST21NFCA_EVT_FIELD_ON:
420 case ST21NFCA_EVT_FIELD_OFF:
422 case ST21NFCA_EVT_SEND_DATA:
423 r = st21nfca_tm_event_send_data(hdev, skb);
428 nfc_err(&hdev->ndev->dev, "Unexpected event on card f gate\n");
434 EXPORT_SYMBOL(st21nfca_dep_event_received);
436 static void st21nfca_im_send_psl_req(struct nfc_hci_dev *hdev, u8 did, u8 bsi,
440 struct st21nfca_psl_req *psl_req;
441 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
444 alloc_skb(sizeof(struct st21nfca_psl_req) + 1, GFP_KERNEL);
449 skb_put(skb, sizeof(struct st21nfca_psl_req));
450 psl_req = (struct st21nfca_psl_req *) skb->data;
452 psl_req->length = sizeof(struct st21nfca_psl_req);
453 psl_req->cmd0 = ST21NFCA_NFCIP1_REQ;
454 psl_req->cmd1 = ST21NFCA_NFCIP1_PSL_REQ;
456 psl_req->brs = (0x30 & bsi << 4) | (bri & 0x03);
459 *(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10;
461 st21nfca_im_send_pdu(info, skb);
464 #define ST21NFCA_CB_TYPE_READER_F 1
465 static void st21nfca_im_recv_atr_res_cb(void *context, struct sk_buff *skb,
468 struct st21nfca_hci_info *info = context;
469 struct st21nfca_atr_res *atr_res;
478 switch (info->async_cb_type) {
479 case ST21NFCA_CB_TYPE_READER_F:
480 skb_trim(skb, skb->len - 1);
481 atr_res = (struct st21nfca_atr_res *)skb->data;
482 r = nfc_set_remote_general_bytes(info->hdev->ndev,
484 skb->len - sizeof(struct st21nfca_atr_res));
488 if (atr_res->to >= 0x0e)
489 info->dep_info.to = 0x0e;
491 info->dep_info.to = atr_res->to + 1;
493 info->dep_info.to |= 0x10;
495 r = nfc_dep_link_is_up(info->hdev->ndev, info->dep_info.idx,
496 NFC_COMM_PASSIVE, NFC_RF_INITIATOR);
500 info->dep_info.curr_nfc_dep_pni = 0;
501 if (ST21NFCA_PP2LRI(atr_res->ppi) != info->dep_info.lri)
502 st21nfca_im_send_psl_req(info->hdev, atr_res->did,
503 atr_res->bsi, atr_res->bri,
504 ST21NFCA_PP2LRI(atr_res->ppi));
512 int st21nfca_im_send_atr_req(struct nfc_hci_dev *hdev, u8 *gb, size_t gb_len)
515 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
516 struct st21nfca_atr_req *atr_req;
517 struct nfc_target *target;
520 info->dep_info.to = ST21NFCA_DEFAULT_TIMEOUT;
521 size = ST21NFCA_ATR_REQ_MIN_SIZE + gb_len;
522 if (size > ST21NFCA_ATR_REQ_MAX_SIZE) {
523 PROTOCOL_ERR("14.6.1.1");
528 alloc_skb(sizeof(struct st21nfca_atr_req) + gb_len + 1, GFP_KERNEL);
534 skb_put(skb, sizeof(struct st21nfca_atr_req));
536 atr_req = (struct st21nfca_atr_req *)skb->data;
537 memset(atr_req, 0, sizeof(struct st21nfca_atr_req));
539 atr_req->cmd0 = ST21NFCA_NFCIP1_REQ;
540 atr_req->cmd1 = ST21NFCA_NFCIP1_ATR_REQ;
541 memset(atr_req->nfcid3, 0, NFC_NFCID3_MAXSIZE);
542 target = hdev->ndev->targets;
544 if (target->sensf_res_len > 0)
545 memcpy(atr_req->nfcid3, target->sensf_res,
546 target->sensf_res_len);
548 get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
554 atr_req->ppi = ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B;
556 atr_req->ppi |= ST21NFCA_GB_BIT;
557 skb_put_data(skb, gb, gb_len);
559 atr_req->length = sizeof(struct st21nfca_atr_req) + hdev->gb_len;
561 *(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10; /* timeout */
563 info->async_cb_type = ST21NFCA_CB_TYPE_READER_F;
564 info->async_cb_context = info;
565 info->async_cb = st21nfca_im_recv_atr_res_cb;
566 info->dep_info.bri = atr_req->bri;
567 info->dep_info.bsi = atr_req->bsi;
568 info->dep_info.lri = ST21NFCA_PP2LRI(atr_req->ppi);
570 return nfc_hci_send_cmd_async(hdev, ST21NFCA_RF_READER_F_GATE,
571 ST21NFCA_WR_XCHG_DATA, skb->data,
572 skb->len, info->async_cb, info);
574 EXPORT_SYMBOL(st21nfca_im_send_atr_req);
576 static void st21nfca_im_recv_dep_res_cb(void *context, struct sk_buff *skb,
579 struct st21nfca_hci_info *info = context;
580 struct st21nfca_dep_req_res *dep_res;
590 switch (info->async_cb_type) {
591 case ST21NFCA_CB_TYPE_READER_F:
592 dep_res = (struct st21nfca_dep_req_res *)skb->data;
598 if (ST21NFCA_NFC_DEP_DID_BIT_SET(dep_res->pfb))
600 if (ST21NFCA_NFC_DEP_NAD_BIT_SET(dep_res->pfb))
606 skb_trim(skb, skb->len - 1);
608 /* Receiving DEP_REQ - Decoding */
609 switch (ST21NFCA_NFC_DEP_PFB_TYPE(dep_res->pfb)) {
610 case ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU:
611 pr_err("Received a ACK/NACK PDU\n");
613 case ST21NFCA_NFC_DEP_PFB_I_PDU:
614 info->dep_info.curr_nfc_dep_pni =
615 ST21NFCA_NFC_DEP_PFB_PNI(dep_res->pfb + 1);
618 nfc_tm_data_received(info->hdev->ndev, skb);
620 case ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU:
621 pr_err("Received a SUPERVISOR PDU\n");
623 *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_REQ;
624 *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_REQ;
625 *(u8 *)skb_push(skb, 1) = skb->len;
626 *(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10;
628 st21nfca_im_send_pdu(info, skb);
641 int st21nfca_im_send_dep_req(struct nfc_hci_dev *hdev, struct sk_buff *skb)
643 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
645 info->async_cb_type = ST21NFCA_CB_TYPE_READER_F;
646 info->async_cb_context = info;
647 info->async_cb = st21nfca_im_recv_dep_res_cb;
649 *(u8 *)skb_push(skb, 1) = info->dep_info.curr_nfc_dep_pni;
650 *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_REQ;
651 *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_REQ;
652 *(u8 *)skb_push(skb, 1) = skb->len;
654 *(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10;
656 return nfc_hci_send_cmd_async(hdev, ST21NFCA_RF_READER_F_GATE,
657 ST21NFCA_WR_XCHG_DATA,
659 info->async_cb, info);
661 EXPORT_SYMBOL(st21nfca_im_send_dep_req);
663 void st21nfca_dep_init(struct nfc_hci_dev *hdev)
665 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
667 INIT_WORK(&info->dep_info.tx_work, st21nfca_tx_work);
668 info->dep_info.curr_nfc_dep_pni = 0;
669 info->dep_info.idx = 0;
670 info->dep_info.to = ST21NFCA_DEFAULT_TIMEOUT;
672 EXPORT_SYMBOL(st21nfca_dep_init);
674 void st21nfca_dep_deinit(struct nfc_hci_dev *hdev)
676 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
678 cancel_work_sync(&info->dep_info.tx_work);
680 EXPORT_SYMBOL(st21nfca_dep_deinit);