2 * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 #include <net/nfc/hci.h>
21 #define ST21NFCA_NFCIP1_INITIATOR 0x00
22 #define ST21NFCA_NFCIP1_REQ 0xd4
23 #define ST21NFCA_NFCIP1_RES 0xd5
24 #define ST21NFCA_NFCIP1_ATR_REQ 0x00
25 #define ST21NFCA_NFCIP1_ATR_RES 0x01
26 #define ST21NFCA_NFCIP1_PSL_REQ 0x04
27 #define ST21NFCA_NFCIP1_PSL_RES 0x05
28 #define ST21NFCA_NFCIP1_DEP_REQ 0x06
29 #define ST21NFCA_NFCIP1_DEP_RES 0x07
31 #define ST21NFCA_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
32 #define ST21NFCA_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
33 #define ST21NFCA_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
34 ((pfb) & ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT)
35 #define ST21NFCA_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & 0x04)
36 #define ST21NFCA_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
37 #define ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT 0x10
39 #define ST21NFCA_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
40 ((pfb) & ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT)
42 #define ST21NFCA_NFC_DEP_PFB_I_PDU 0x00
43 #define ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU 0x40
44 #define ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
46 #define ST21NFCA_ATR_REQ_MIN_SIZE 17
47 #define ST21NFCA_ATR_REQ_MAX_SIZE 65
48 #define ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B 0x30
49 #define ST21NFCA_GB_BIT 0x02
51 #define ST21NFCA_EVT_SEND_DATA 0x10
52 #define ST21NFCA_EVT_FIELD_ON 0x11
53 #define ST21NFCA_EVT_CARD_DEACTIVATED 0x12
54 #define ST21NFCA_EVT_CARD_ACTIVATED 0x13
55 #define ST21NFCA_EVT_FIELD_OFF 0x14
57 #define ST21NFCA_EVT_CARD_F_BITRATE 0x16
58 #define ST21NFCA_EVT_READER_F_BITRATE 0x13
59 #define ST21NFCA_PSL_REQ_SEND_SPEED(brs) (brs & 0x38)
60 #define ST21NFCA_PSL_REQ_RECV_SPEED(brs) (brs & 0x07)
61 #define ST21NFCA_PP2LRI(pp) ((pp & 0x30) >> 4)
62 #define ST21NFCA_CARD_BITRATE_212 0x01
63 #define ST21NFCA_CARD_BITRATE_424 0x02
65 #define ST21NFCA_DEFAULT_TIMEOUT 0x0a
68 #define PROTOCOL_ERR(req) pr_err("%d: ST21NFCA Protocol error: %s\n", \
71 struct st21nfca_atr_req {
75 u8 nfcid3[NFC_NFCID3_MAXSIZE];
83 struct st21nfca_atr_res {
87 u8 nfcid3[NFC_NFCID3_MAXSIZE];
96 struct st21nfca_psl_req {
105 struct st21nfca_psl_res {
112 struct st21nfca_dep_req_res {
121 static void st21nfca_tx_work(struct work_struct *work)
123 struct st21nfca_hci_info *info = container_of(work,
124 struct st21nfca_hci_info,
131 dev = info->hdev->ndev;
132 skb = info->dep_info.tx_pending;
134 device_lock(&dev->dev);
136 nfc_hci_send_cmd_async(info->hdev, ST21NFCA_RF_READER_F_GATE,
137 ST21NFCA_WR_XCHG_DATA, skb->data, skb->len,
138 info->async_cb, info);
139 device_unlock(&dev->dev);
144 static void st21nfca_im_send_pdu(struct st21nfca_hci_info *info,
147 info->dep_info.tx_pending = skb;
148 schedule_work(&info->dep_info.tx_work);
151 static int st21nfca_tm_send_atr_res(struct nfc_hci_dev *hdev,
152 struct st21nfca_atr_req *atr_req)
154 struct st21nfca_atr_res *atr_res;
158 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
160 gb_len = atr_req->length - sizeof(struct st21nfca_atr_req);
161 skb = alloc_skb(atr_req->length + 1, GFP_KERNEL);
165 skb_put(skb, sizeof(struct st21nfca_atr_res));
167 atr_res = (struct st21nfca_atr_res *)skb->data;
168 memset(atr_res, 0, sizeof(struct st21nfca_atr_res));
170 atr_res->length = atr_req->length + 1;
171 atr_res->cmd0 = ST21NFCA_NFCIP1_RES;
172 atr_res->cmd1 = ST21NFCA_NFCIP1_ATR_RES;
174 memcpy(atr_res->nfcid3, atr_req->nfcid3, 6);
177 atr_res->to = ST21NFCA_DEFAULT_TIMEOUT;
178 atr_res->ppi = ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B;
181 skb_put(skb, gb_len);
183 atr_res->ppi |= ST21NFCA_GB_BIT;
184 memcpy(atr_res->gbi, atr_req->gbi, gb_len);
185 r = nfc_set_remote_general_bytes(hdev->ndev, atr_res->gbi,
193 info->dep_info.curr_nfc_dep_pni = 0;
195 r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
196 ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
201 static int st21nfca_tm_recv_atr_req(struct nfc_hci_dev *hdev,
204 struct st21nfca_atr_req *atr_req;
208 skb_trim(skb, skb->len - 1);
215 if (skb->len < ST21NFCA_ATR_REQ_MIN_SIZE) {
220 atr_req = (struct st21nfca_atr_req *)skb->data;
222 if (atr_req->length < sizeof(struct st21nfca_atr_req)) {
227 r = st21nfca_tm_send_atr_res(hdev, atr_req);
231 gb_len = skb->len - sizeof(struct st21nfca_atr_req);
233 r = nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK,
234 NFC_COMM_PASSIVE, atr_req->gbi, gb_len);
244 static int st21nfca_tm_send_psl_res(struct nfc_hci_dev *hdev,
245 struct st21nfca_psl_req *psl_req)
247 struct st21nfca_psl_res *psl_res;
249 u8 bitrate[2] = {0, 0};
252 skb = alloc_skb(sizeof(struct st21nfca_psl_res), GFP_KERNEL);
255 skb_put(skb, sizeof(struct st21nfca_psl_res));
257 psl_res = (struct st21nfca_psl_res *)skb->data;
259 psl_res->length = sizeof(struct st21nfca_psl_res);
260 psl_res->cmd0 = ST21NFCA_NFCIP1_RES;
261 psl_res->cmd1 = ST21NFCA_NFCIP1_PSL_RES;
262 psl_res->did = psl_req->did;
264 r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
265 ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
270 * ST21NFCA only support P2P passive.
271 * PSL_REQ BRS value != 0 has only a meaning to
272 * change technology to type F.
273 * We change to BITRATE 424Kbits.
274 * In other case switch to BITRATE 106Kbits.
276 if (ST21NFCA_PSL_REQ_SEND_SPEED(psl_req->brs) &&
277 ST21NFCA_PSL_REQ_RECV_SPEED(psl_req->brs)) {
278 bitrate[0] = ST21NFCA_CARD_BITRATE_424;
279 bitrate[1] = ST21NFCA_CARD_BITRATE_424;
282 /* Send an event to change bitrate change event to card f */
283 r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
284 ST21NFCA_EVT_CARD_F_BITRATE, bitrate, 2);
290 static int st21nfca_tm_recv_psl_req(struct nfc_hci_dev *hdev,
293 struct st21nfca_psl_req *psl_req;
296 skb_trim(skb, skb->len - 1);
303 psl_req = (struct st21nfca_psl_req *)skb->data;
305 if (skb->len < sizeof(struct st21nfca_psl_req)) {
310 r = st21nfca_tm_send_psl_res(hdev, psl_req);
315 int st21nfca_tm_send_dep_res(struct nfc_hci_dev *hdev, struct sk_buff *skb)
318 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
320 *(u8 *)skb_push(skb, 1) = info->dep_info.curr_nfc_dep_pni;
321 *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_RES;
322 *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_RES;
323 *(u8 *)skb_push(skb, 1) = skb->len;
325 r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
326 ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
331 EXPORT_SYMBOL(st21nfca_tm_send_dep_res);
333 static int st21nfca_tm_recv_dep_req(struct nfc_hci_dev *hdev,
336 struct st21nfca_dep_req_res *dep_req;
339 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
341 skb_trim(skb, skb->len - 1);
345 dep_req = (struct st21nfca_dep_req_res *)skb->data;
346 if (skb->len < size) {
351 if (ST21NFCA_NFC_DEP_DID_BIT_SET(dep_req->pfb))
353 if (ST21NFCA_NFC_DEP_NAD_BIT_SET(dep_req->pfb))
356 if (skb->len < size) {
361 /* Receiving DEP_REQ - Decoding */
362 switch (ST21NFCA_NFC_DEP_PFB_TYPE(dep_req->pfb)) {
363 case ST21NFCA_NFC_DEP_PFB_I_PDU:
364 info->dep_info.curr_nfc_dep_pni =
365 ST21NFCA_NFC_DEP_PFB_PNI(dep_req->pfb);
367 case ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU:
368 pr_err("Received a ACK/NACK PDU\n");
370 case ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU:
371 pr_err("Received a SUPERVISOR PDU\n");
377 return nfc_tm_data_received(hdev->ndev, skb);
382 static int st21nfca_tm_event_send_data(struct nfc_hci_dev *hdev,
390 case ST21NFCA_NFCIP1_REQ:
393 case ST21NFCA_NFCIP1_ATR_REQ:
394 r = st21nfca_tm_recv_atr_req(hdev, skb);
396 case ST21NFCA_NFCIP1_PSL_REQ:
397 r = st21nfca_tm_recv_psl_req(hdev, skb);
399 case ST21NFCA_NFCIP1_DEP_REQ:
400 r = st21nfca_tm_recv_dep_req(hdev, skb);
413 * <= 0: driver handled the event, skb consumed
414 * 1: driver does not handle the event, please do standard processing
416 int st21nfca_dep_event_received(struct nfc_hci_dev *hdev,
417 u8 event, struct sk_buff *skb)
420 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
422 pr_debug("dep event: %d\n", event);
425 case ST21NFCA_EVT_CARD_ACTIVATED:
426 info->dep_info.curr_nfc_dep_pni = 0;
428 case ST21NFCA_EVT_CARD_DEACTIVATED:
430 case ST21NFCA_EVT_FIELD_ON:
432 case ST21NFCA_EVT_FIELD_OFF:
434 case ST21NFCA_EVT_SEND_DATA:
435 r = st21nfca_tm_event_send_data(hdev, skb);
440 nfc_err(&hdev->ndev->dev, "Unexpected event on card f gate\n");
446 EXPORT_SYMBOL(st21nfca_dep_event_received);
448 static void st21nfca_im_send_psl_req(struct nfc_hci_dev *hdev, u8 did, u8 bsi,
452 struct st21nfca_psl_req *psl_req;
453 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
456 alloc_skb(sizeof(struct st21nfca_psl_req) + 1, GFP_KERNEL);
461 skb_put(skb, sizeof(struct st21nfca_psl_req));
462 psl_req = (struct st21nfca_psl_req *) skb->data;
464 psl_req->length = sizeof(struct st21nfca_psl_req);
465 psl_req->cmd0 = ST21NFCA_NFCIP1_REQ;
466 psl_req->cmd1 = ST21NFCA_NFCIP1_PSL_REQ;
468 psl_req->brs = (0x30 & bsi << 4) | (bri & 0x03);
471 *(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10;
473 st21nfca_im_send_pdu(info, skb);
476 #define ST21NFCA_CB_TYPE_READER_F 1
477 static void st21nfca_im_recv_atr_res_cb(void *context, struct sk_buff *skb,
480 struct st21nfca_hci_info *info = context;
481 struct st21nfca_atr_res *atr_res;
490 switch (info->async_cb_type) {
491 case ST21NFCA_CB_TYPE_READER_F:
492 skb_trim(skb, skb->len - 1);
493 atr_res = (struct st21nfca_atr_res *)skb->data;
494 r = nfc_set_remote_general_bytes(info->hdev->ndev,
496 skb->len - sizeof(struct st21nfca_atr_res));
500 if (atr_res->to >= 0x0e)
501 info->dep_info.to = 0x0e;
503 info->dep_info.to = atr_res->to + 1;
505 info->dep_info.to |= 0x10;
507 r = nfc_dep_link_is_up(info->hdev->ndev, info->dep_info.idx,
508 NFC_COMM_PASSIVE, NFC_RF_INITIATOR);
512 info->dep_info.curr_nfc_dep_pni = 0;
513 if (ST21NFCA_PP2LRI(atr_res->ppi) != info->dep_info.lri)
514 st21nfca_im_send_psl_req(info->hdev, atr_res->did,
515 atr_res->bsi, atr_res->bri,
516 ST21NFCA_PP2LRI(atr_res->ppi));
524 int st21nfca_im_send_atr_req(struct nfc_hci_dev *hdev, u8 *gb, size_t gb_len)
527 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
528 struct st21nfca_atr_req *atr_req;
529 struct nfc_target *target;
532 info->dep_info.to = ST21NFCA_DEFAULT_TIMEOUT;
533 size = ST21NFCA_ATR_REQ_MIN_SIZE + gb_len;
534 if (size > ST21NFCA_ATR_REQ_MAX_SIZE) {
535 PROTOCOL_ERR("14.6.1.1");
540 alloc_skb(sizeof(struct st21nfca_atr_req) + gb_len + 1, GFP_KERNEL);
546 skb_put(skb, sizeof(struct st21nfca_atr_req));
548 atr_req = (struct st21nfca_atr_req *)skb->data;
549 memset(atr_req, 0, sizeof(struct st21nfca_atr_req));
551 atr_req->cmd0 = ST21NFCA_NFCIP1_REQ;
552 atr_req->cmd1 = ST21NFCA_NFCIP1_ATR_REQ;
553 memset(atr_req->nfcid3, 0, NFC_NFCID3_MAXSIZE);
554 target = hdev->ndev->targets;
556 if (target->sensf_res_len > 0)
557 memcpy(atr_req->nfcid3, target->sensf_res,
558 target->sensf_res_len);
560 get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
566 atr_req->ppi = ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B;
568 atr_req->ppi |= ST21NFCA_GB_BIT;
569 skb_put_data(skb, gb, gb_len);
571 atr_req->length = sizeof(struct st21nfca_atr_req) + hdev->gb_len;
573 *(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10; /* timeout */
575 info->async_cb_type = ST21NFCA_CB_TYPE_READER_F;
576 info->async_cb_context = info;
577 info->async_cb = st21nfca_im_recv_atr_res_cb;
578 info->dep_info.bri = atr_req->bri;
579 info->dep_info.bsi = atr_req->bsi;
580 info->dep_info.lri = ST21NFCA_PP2LRI(atr_req->ppi);
582 return nfc_hci_send_cmd_async(hdev, ST21NFCA_RF_READER_F_GATE,
583 ST21NFCA_WR_XCHG_DATA, skb->data,
584 skb->len, info->async_cb, info);
586 EXPORT_SYMBOL(st21nfca_im_send_atr_req);
588 static void st21nfca_im_recv_dep_res_cb(void *context, struct sk_buff *skb,
591 struct st21nfca_hci_info *info = context;
592 struct st21nfca_dep_req_res *dep_res;
602 switch (info->async_cb_type) {
603 case ST21NFCA_CB_TYPE_READER_F:
604 dep_res = (struct st21nfca_dep_req_res *)skb->data;
610 if (ST21NFCA_NFC_DEP_DID_BIT_SET(dep_res->pfb))
612 if (ST21NFCA_NFC_DEP_NAD_BIT_SET(dep_res->pfb))
618 skb_trim(skb, skb->len - 1);
620 /* Receiving DEP_REQ - Decoding */
621 switch (ST21NFCA_NFC_DEP_PFB_TYPE(dep_res->pfb)) {
622 case ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU:
623 pr_err("Received a ACK/NACK PDU\n");
624 case ST21NFCA_NFC_DEP_PFB_I_PDU:
625 info->dep_info.curr_nfc_dep_pni =
626 ST21NFCA_NFC_DEP_PFB_PNI(dep_res->pfb + 1);
629 nfc_tm_data_received(info->hdev->ndev, skb);
631 case ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU:
632 pr_err("Received a SUPERVISOR PDU\n");
634 *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_REQ;
635 *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_REQ;
636 *(u8 *)skb_push(skb, 1) = skb->len;
637 *(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10;
639 st21nfca_im_send_pdu(info, skb);
652 int st21nfca_im_send_dep_req(struct nfc_hci_dev *hdev, struct sk_buff *skb)
654 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
656 info->async_cb_type = ST21NFCA_CB_TYPE_READER_F;
657 info->async_cb_context = info;
658 info->async_cb = st21nfca_im_recv_dep_res_cb;
660 *(u8 *)skb_push(skb, 1) = info->dep_info.curr_nfc_dep_pni;
661 *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_REQ;
662 *(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_REQ;
663 *(u8 *)skb_push(skb, 1) = skb->len;
665 *(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10;
667 return nfc_hci_send_cmd_async(hdev, ST21NFCA_RF_READER_F_GATE,
668 ST21NFCA_WR_XCHG_DATA,
670 info->async_cb, info);
672 EXPORT_SYMBOL(st21nfca_im_send_dep_req);
674 void st21nfca_dep_init(struct nfc_hci_dev *hdev)
676 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
678 INIT_WORK(&info->dep_info.tx_work, st21nfca_tx_work);
679 info->dep_info.curr_nfc_dep_pni = 0;
680 info->dep_info.idx = 0;
681 info->dep_info.to = ST21NFCA_DEFAULT_TIMEOUT;
683 EXPORT_SYMBOL(st21nfca_dep_init);
685 void st21nfca_dep_deinit(struct nfc_hci_dev *hdev)
687 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
689 cancel_work_sync(&info->dep_info.tx_work);
691 EXPORT_SYMBOL(st21nfca_dep_deinit);