2 * HCI based Driver for STMicroelectronics NFC Chip
4 * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include <linux/module.h>
20 #include <linux/nfc.h>
21 #include <net/nfc/hci.h>
22 #include <net/nfc/llc.h>
25 #include "st21nfca_dep.h"
27 #define DRIVER_DESC "HCI NFC driver for ST21NFCA"
29 #define FULL_VERSION_LEN 3
31 /* Proprietary gates, events, commands and registers */
33 /* Commands that apply to all RF readers */
34 #define ST21NFCA_RF_READER_CMD_PRESENCE_CHECK 0x30
36 #define ST21NFCA_RF_READER_ISO15693_GATE 0x12
37 #define ST21NFCA_RF_READER_ISO15693_INVENTORY 0x01
40 * Reader gate for communication with contact-less cards using Type A
41 * protocol ISO14443-3 but not compliant with ISO14443-4
43 #define ST21NFCA_RF_READER_14443_3_A_GATE 0x15
44 #define ST21NFCA_RF_READER_14443_3_A_UID 0x02
45 #define ST21NFCA_RF_READER_14443_3_A_ATQA 0x03
46 #define ST21NFCA_RF_READER_14443_3_A_SAK 0x04
48 #define ST21NFCA_DEVICE_MGNT_GATE 0x01
49 #define ST21NFCA_DEVICE_MGNT_PIPE 0x02
51 #define ST21NFCA_DM_GETINFO 0x13
52 #define ST21NFCA_DM_GETINFO_PIPE_LIST 0x02
53 #define ST21NFCA_DM_GETINFO_PIPE_INFO 0x01
54 #define ST21NFCA_DM_PIPE_CREATED 0x02
55 #define ST21NFCA_DM_PIPE_OPEN 0x04
56 #define ST21NFCA_DM_RF_ACTIVE 0x80
57 #define ST21NFCA_DM_DISCONNECT 0x30
59 #define ST21NFCA_DM_IS_PIPE_OPEN(p) \
60 ((p & 0x0f) == (ST21NFCA_DM_PIPE_CREATED | ST21NFCA_DM_PIPE_OPEN))
62 #define ST21NFCA_NFC_MODE 0x03 /* NFC_MODE parameter*/
64 static DECLARE_BITMAP(dev_mask, ST21NFCA_NUM_DEVICES);
66 static struct nfc_hci_gate st21nfca_gates[] = {
67 {NFC_HCI_ADMIN_GATE, NFC_HCI_ADMIN_PIPE},
68 {NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
69 {NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
70 {NFC_HCI_LINK_MGMT_GATE, NFC_HCI_LINK_MGMT_PIPE},
71 {NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
72 {NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
73 {ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE},
74 {ST21NFCA_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
75 {ST21NFCA_RF_READER_14443_3_A_GATE, NFC_HCI_INVALID_PIPE},
76 {ST21NFCA_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE},
77 {ST21NFCA_RF_CARD_F_GATE, NFC_HCI_INVALID_PIPE},
80 struct st21nfca_pipe_info {
88 /* Largest headroom needed for outgoing custom commands */
89 #define ST21NFCA_CMDS_HEADROOM 7
91 static int st21nfca_hci_load_session(struct nfc_hci_dev *hdev)
94 struct sk_buff *skb_pipe_list, *skb_pipe_info;
95 struct st21nfca_pipe_info *info;
97 u8 pipe_list[] = { ST21NFCA_DM_GETINFO_PIPE_LIST,
98 NFC_HCI_TERMINAL_HOST_ID
100 u8 pipe_info[] = { ST21NFCA_DM_GETINFO_PIPE_INFO,
101 NFC_HCI_TERMINAL_HOST_ID, 0
104 skb_pipe_list = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL);
105 if (!skb_pipe_list) {
110 skb_pipe_info = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL);
111 if (!skb_pipe_info) {
116 /* On ST21NFCA device pipes number are dynamics
117 * A maximum of 16 pipes can be created at the same time
118 * If pipes are already created, hci_dev_up will fail.
119 * Doing a clear all pipe is a bad idea because:
120 * - It does useless EEPROM cycling
121 * - It might cause issue for secure elements support
122 * (such as removing connectivity or APDU reader pipe)
123 * A better approach on ST21NFCA is to:
124 * - get a pipe list for each host.
125 * (eg: NFC_HCI_HOST_CONTROLLER_ID for now).
126 * (TODO Later on UICC HOST and eSE HOST)
127 * - get pipe information
128 * - match retrieved pipe list in st21nfca_gates
129 * ST21NFCA_DEVICE_MGNT_GATE is a proprietary gate
130 * with ST21NFCA_DEVICE_MGNT_PIPE.
131 * Pipe can be closed and need to be open.
133 r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
134 ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE);
139 r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
140 ST21NFCA_DM_GETINFO, pipe_list, sizeof(pipe_list),
145 /* Complete the existing gate_pipe table */
146 for (i = 0; i < skb_pipe_list->len; i++) {
147 pipe_info[2] = skb_pipe_list->data[i];
148 r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
149 ST21NFCA_DM_GETINFO, pipe_info,
150 sizeof(pipe_info), &skb_pipe_info);
156 * Match pipe ID and gate ID
157 * Output format from ST21NFC_DM_GETINFO is:
158 * - pipe state (1byte)
159 * - source hid (1byte)
160 * - source gid (1byte)
161 * - destination hid (1byte)
162 * - destination gid (1byte)
164 info = (struct st21nfca_pipe_info *) skb_pipe_info->data;
165 for (j = 0; (j < ARRAY_SIZE(st21nfca_gates)) &&
166 (st21nfca_gates[j].gate != info->dst_gate_id);
170 if (j < ARRAY_SIZE(st21nfca_gates) &&
171 st21nfca_gates[j].gate == info->dst_gate_id &&
172 ST21NFCA_DM_IS_PIPE_OPEN(info->pipe_state)) {
173 st21nfca_gates[j].pipe = pipe_info[2];
174 hdev->gate2pipe[st21nfca_gates[j].gate] =
175 st21nfca_gates[j].pipe;
180 * 3 gates have a well known pipe ID.
181 * They will never appear in the pipe list
183 if (skb_pipe_list->len + 3 < ARRAY_SIZE(st21nfca_gates)) {
184 for (i = skb_pipe_list->len + 3;
185 i < ARRAY_SIZE(st21nfca_gates); i++) {
186 r = nfc_hci_connect_gate(hdev,
187 NFC_HCI_HOST_CONTROLLER_ID,
188 st21nfca_gates[i].gate,
189 st21nfca_gates[i].pipe);
195 memcpy(hdev->init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
197 kfree_skb(skb_pipe_info);
199 kfree_skb(skb_pipe_list);
203 static int st21nfca_hci_open(struct nfc_hci_dev *hdev)
205 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
208 mutex_lock(&info->info_lock);
210 if (info->state != ST21NFCA_ST_COLD) {
215 r = info->phy_ops->enable(info->phy_id);
218 info->state = ST21NFCA_ST_READY;
221 mutex_unlock(&info->info_lock);
225 static void st21nfca_hci_close(struct nfc_hci_dev *hdev)
227 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
229 mutex_lock(&info->info_lock);
231 if (info->state == ST21NFCA_ST_COLD)
234 info->phy_ops->disable(info->phy_id);
235 info->state = ST21NFCA_ST_COLD;
238 mutex_unlock(&info->info_lock);
241 static int st21nfca_hci_ready(struct nfc_hci_dev *hdev)
248 param = NFC_HCI_UICC_HOST_ID;
249 r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
250 NFC_HCI_ADMIN_WHITELIST, ¶m, 1);
254 /* Set NFC_MODE in device management gate to enable */
255 r = nfc_hci_get_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
256 ST21NFCA_NFC_MODE, &skb);
260 if (skb->data[0] == 0) {
264 r = nfc_hci_set_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
265 ST21NFCA_NFC_MODE, ¶m, 1);
270 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
271 NFC_HCI_EVT_END_OPERATION, NULL, 0);
275 r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
276 NFC_HCI_ID_MGMT_VERSION_SW, &skb);
280 if (skb->len != FULL_VERSION_LEN) {
285 print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
286 DUMP_PREFIX_NONE, 16, 1,
287 skb->data, FULL_VERSION_LEN, false);
294 static int st21nfca_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
296 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
298 return info->phy_ops->write(info->phy_id, skb);
301 static int st21nfca_hci_start_poll(struct nfc_hci_dev *hdev,
302 u32 im_protocols, u32 tm_protocols)
307 struct sk_buff *datarate_skb;
309 pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
310 __func__, im_protocols, tm_protocols);
312 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
313 NFC_HCI_EVT_END_OPERATION, NULL, 0);
318 * enable polling according to im_protocols & tm_protocols
319 * - CLOSE pipe according to im_protocols & tm_protocols
321 if ((NFC_HCI_RF_READER_B_GATE & im_protocols) == 0) {
322 r = nfc_hci_disconnect_gate(hdev,
323 NFC_HCI_RF_READER_B_GATE);
328 if ((NFC_HCI_RF_READER_A_GATE & im_protocols) == 0) {
329 r = nfc_hci_disconnect_gate(hdev,
330 NFC_HCI_RF_READER_A_GATE);
335 if ((ST21NFCA_RF_READER_F_GATE & im_protocols) == 0) {
336 r = nfc_hci_disconnect_gate(hdev,
337 ST21NFCA_RF_READER_F_GATE);
341 hdev->gb = nfc_get_local_general_bytes(hdev->ndev,
344 if (hdev->gb == NULL || hdev->gb_len == 0) {
345 im_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
346 tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
349 param[0] = ST21NFCA_RF_READER_F_DATARATE_106 |
350 ST21NFCA_RF_READER_F_DATARATE_212 |
351 ST21NFCA_RF_READER_F_DATARATE_424;
352 r = nfc_hci_set_param(hdev, ST21NFCA_RF_READER_F_GATE,
353 ST21NFCA_RF_READER_F_DATARATE,
359 be32_to_cpu(ST21NFCA_RF_READER_F_POL_REQ_DEFAULT);
360 r = nfc_hci_set_param(hdev, ST21NFCA_RF_READER_F_GATE,
361 ST21NFCA_RF_READER_F_POL_REQ,
367 if ((ST21NFCA_RF_READER_14443_3_A_GATE & im_protocols) == 0) {
368 r = nfc_hci_disconnect_gate(hdev,
369 ST21NFCA_RF_READER_14443_3_A_GATE);
374 if ((ST21NFCA_RF_READER_ISO15693_GATE & im_protocols) == 0) {
375 r = nfc_hci_disconnect_gate(hdev,
376 ST21NFCA_RF_READER_ISO15693_GATE);
381 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
382 NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
384 nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
385 NFC_HCI_EVT_END_OPERATION, NULL, 0);
388 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
389 r = nfc_hci_get_param(hdev, ST21NFCA_RF_CARD_F_GATE,
390 ST21NFCA_RF_CARD_F_DATARATE,
395 /* Configure the maximum supported datarate to 424Kbps */
396 if (datarate_skb->len > 0 &&
397 datarate_skb->data[0] !=
398 ST21NFCA_RF_CARD_F_DATARATE_212_424) {
399 param[0] = ST21NFCA_RF_CARD_F_DATARATE_212_424;
400 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
401 ST21NFCA_RF_CARD_F_DATARATE,
410 * NFC Forum Digital Spec Table 7:
411 * NFCID1 size: triple (10 bytes)
415 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
416 ST21NFCA_RF_CARD_F_SENS_RES, param, 2);
423 * NFC Forum Digistal Spec Table 17:
424 * b3 set to 0b (value b7-b6):
425 * - 10b: Configured for NFC-DEP Protocol
428 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
429 ST21NFCA_RF_CARD_F_SEL_RES, param, 1);
433 /* Configure NFCID1 Random uid */
434 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
435 ST21NFCA_RF_CARD_F_NFCID1, NULL, 0);
439 /* Configure NFCID2_LIST */
452 /* 8 byte Pad bytes used for polling respone frame */
455 * Configuration byte:
456 * - bit 0: define the default NFCID2 entry used when the
457 * system code is equal to 'FFFF'
458 * - bit 1: use a random value for lowest 6 bytes of
460 * - bit 2: ignore polling request frame if request code
462 * - Other bits are RFU
465 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
466 ST21NFCA_RF_CARD_F_NFCID2_LIST, param,
472 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
473 ST21NFCA_RF_CARD_F_MODE, param, 1);
479 static void st21nfca_hci_stop_poll(struct nfc_hci_dev *hdev)
481 nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
482 ST21NFCA_DM_DISCONNECT, NULL, 0, NULL);
485 static int st21nfca_get_iso14443_3_atqa(struct nfc_hci_dev *hdev, u16 *atqa)
488 struct sk_buff *atqa_skb = NULL;
490 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
491 ST21NFCA_RF_READER_14443_3_A_ATQA, &atqa_skb);
495 if (atqa_skb->len != 2) {
500 *atqa = be16_to_cpu(*(__be16 *) atqa_skb->data);
507 static int st21nfca_get_iso14443_3_sak(struct nfc_hci_dev *hdev, u8 *sak)
510 struct sk_buff *sak_skb = NULL;
512 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
513 ST21NFCA_RF_READER_14443_3_A_SAK, &sak_skb);
517 if (sak_skb->len != 1) {
522 *sak = sak_skb->data[0];
529 static int st21nfca_get_iso14443_3_uid(struct nfc_hci_dev *hdev, u8 *gate,
533 struct sk_buff *uid_skb = NULL;
535 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
536 ST21NFCA_RF_READER_14443_3_A_UID, &uid_skb);
540 if (uid_skb->len == 0 || uid_skb->len > NFC_NFCID1_MAXSIZE) {
545 gate = uid_skb->data;
552 static int st21nfca_get_iso15693_inventory(struct nfc_hci_dev *hdev,
553 struct nfc_target *target)
556 struct sk_buff *inventory_skb = NULL;
558 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_ISO15693_GATE,
559 ST21NFCA_RF_READER_ISO15693_INVENTORY,
564 skb_pull(inventory_skb, 2);
566 if (inventory_skb->len == 0 ||
567 inventory_skb->len > NFC_ISO15693_UID_MAXSIZE) {
572 memcpy(target->iso15693_uid, inventory_skb->data, inventory_skb->len);
573 target->iso15693_dsfid = inventory_skb->data[1];
574 target->is_iso15693 = 1;
576 kfree_skb(inventory_skb);
580 static int st21nfca_hci_dep_link_up(struct nfc_hci_dev *hdev,
581 struct nfc_target *target, u8 comm_mode,
582 u8 *gb, size_t gb_len)
584 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
586 info->dep_info.idx = target->idx;
587 return st21nfca_im_send_atr_req(hdev, gb, gb_len);
590 static int st21nfca_hci_dep_link_down(struct nfc_hci_dev *hdev)
592 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
594 info->state = ST21NFCA_ST_READY;
596 return nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
597 ST21NFCA_DM_DISCONNECT, NULL, 0, NULL);
600 static int st21nfca_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
601 struct nfc_target *target)
606 u8 uid[NFC_NFCID1_MAXSIZE];
609 case ST21NFCA_RF_READER_F_GATE:
610 target->supported_protocols = NFC_PROTO_FELICA_MASK;
612 case ST21NFCA_RF_READER_14443_3_A_GATE:
613 /* ISO14443-3 type 1 or 2 tags */
614 r = st21nfca_get_iso14443_3_atqa(hdev, &atqa);
617 if (atqa == 0x000c) {
618 target->supported_protocols = NFC_PROTO_JEWEL_MASK;
619 target->sens_res = 0x0c00;
621 r = st21nfca_get_iso14443_3_sak(hdev, &sak);
625 r = st21nfca_get_iso14443_3_uid(hdev, uid, &len);
629 target->supported_protocols =
630 nfc_hci_sak_to_protocol(sak);
631 if (target->supported_protocols == 0xffffffff)
634 target->sens_res = atqa;
635 target->sel_res = sak;
636 memcpy(target->nfcid1, uid, len);
637 target->nfcid1_len = len;
641 case ST21NFCA_RF_READER_ISO15693_GATE:
642 target->supported_protocols = NFC_PROTO_ISO15693_MASK;
643 r = st21nfca_get_iso15693_inventory(hdev, target);
654 static int st21nfca_hci_complete_target_discovered(struct nfc_hci_dev *hdev,
656 struct nfc_target *target)
659 struct sk_buff *nfcid2_skb = NULL, *nfcid1_skb;
661 if (gate == ST21NFCA_RF_READER_F_GATE) {
662 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE,
663 ST21NFCA_RF_READER_F_NFCID2, &nfcid2_skb);
667 if (nfcid2_skb->len > NFC_SENSF_RES_MAXSIZE) {
673 * - After the recepton of polling response for type F frame
674 * at 212 or 424 Kbit/s, NFCID2 registry parameters will be
676 * - After the reception of SEL_RES with NFCIP-1 compliant bit
677 * set for type A frame NFCID1 will be updated
679 if (nfcid2_skb->len > 0) {
681 memcpy(target->sensf_res, nfcid2_skb->data,
683 target->sensf_res_len = nfcid2_skb->len;
684 /* NFC Forum Digital Protocol Table 44 */
685 if (target->sensf_res[0] == 0x01 &&
686 target->sensf_res[1] == 0xfe)
687 target->supported_protocols =
688 NFC_PROTO_NFC_DEP_MASK;
690 target->supported_protocols =
691 NFC_PROTO_FELICA_MASK;
694 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE,
695 ST21NFCA_RF_READER_F_NFCID1,
700 if (nfcid1_skb->len > NFC_NFCID1_MAXSIZE) {
704 memcpy(target->sensf_res, nfcid1_skb->data,
706 target->sensf_res_len = nfcid1_skb->len;
707 target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
709 target->hci_reader_gate = ST21NFCA_RF_READER_F_GATE;
713 kfree_skb(nfcid2_skb);
717 #define ST21NFCA_CB_TYPE_READER_ISO15693 1
718 static void st21nfca_hci_data_exchange_cb(void *context, struct sk_buff *skb,
721 struct st21nfca_hci_info *info = context;
723 switch (info->async_cb_type) {
724 case ST21NFCA_CB_TYPE_READER_ISO15693:
726 skb_trim(skb, skb->len - 1);
727 info->async_cb(info->async_cb_context, skb, err);
738 * <= 0: driver handled the data exchange
739 * 1: driver doesn't especially handle, please do standard processing
741 static int st21nfca_hci_im_transceive(struct nfc_hci_dev *hdev,
742 struct nfc_target *target,
744 data_exchange_cb_t cb, void *cb_context)
746 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
748 pr_info(DRIVER_DESC ": %s for gate=%d len=%d\n", __func__,
749 target->hci_reader_gate, skb->len);
751 switch (target->hci_reader_gate) {
752 case ST21NFCA_RF_READER_F_GATE:
753 if (target->supported_protocols == NFC_PROTO_NFC_DEP_MASK)
754 return st21nfca_im_send_dep_req(hdev, skb);
756 *skb_push(skb, 1) = 0x1a;
757 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
758 ST21NFCA_WR_XCHG_DATA, skb->data,
759 skb->len, cb, cb_context);
760 case ST21NFCA_RF_READER_14443_3_A_GATE:
761 *skb_push(skb, 1) = 0x1a; /* CTR, see spec:10.2.2.1 */
763 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
764 ST21NFCA_WR_XCHG_DATA, skb->data,
765 skb->len, cb, cb_context);
766 case ST21NFCA_RF_READER_ISO15693_GATE:
767 info->async_cb_type = ST21NFCA_CB_TYPE_READER_ISO15693;
769 info->async_cb_context = cb_context;
771 *skb_push(skb, 1) = 0x17;
773 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
774 ST21NFCA_WR_XCHG_DATA, skb->data,
776 st21nfca_hci_data_exchange_cb,
784 static int st21nfca_hci_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
786 return st21nfca_tm_send_dep_res(hdev, skb);
789 static int st21nfca_hci_check_presence(struct nfc_hci_dev *hdev,
790 struct nfc_target *target)
793 switch (target->hci_reader_gate) {
794 case NFC_HCI_RF_READER_A_GATE:
795 case NFC_HCI_RF_READER_B_GATE:
797 * PRESENCE_CHECK on those gates is available
798 * However, the answer to this command is taking 3 * fwi
799 * if the card is no present.
800 * Instead, we send an empty I-Frame with a very short
801 * configurable fwi ~604µs.
803 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
804 ST21NFCA_WR_XCHG_DATA, &fwi, 1, NULL);
805 case ST21NFCA_RF_READER_14443_3_A_GATE:
806 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
807 ST21NFCA_RF_READER_CMD_PRESENCE_CHECK,
816 * <= 0: driver handled the event, skb consumed
817 * 1: driver does not handle the event, please do standard processing
819 static int st21nfca_hci_event_received(struct nfc_hci_dev *hdev, u8 gate,
820 u8 event, struct sk_buff *skb)
823 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
825 pr_debug("hci event: %d\n", event);
828 case ST21NFCA_EVT_CARD_ACTIVATED:
829 if (gate == ST21NFCA_RF_CARD_F_GATE)
830 info->dep_info.curr_nfc_dep_pni = 0;
832 case ST21NFCA_EVT_CARD_DEACTIVATED:
834 case ST21NFCA_EVT_FIELD_ON:
836 case ST21NFCA_EVT_FIELD_OFF:
838 case ST21NFCA_EVT_SEND_DATA:
839 if (gate == ST21NFCA_RF_CARD_F_GATE) {
840 r = st21nfca_tm_event_send_data(hdev, skb, gate);
845 info->dep_info.curr_nfc_dep_pni = 0;
858 static struct nfc_hci_ops st21nfca_hci_ops = {
859 .open = st21nfca_hci_open,
860 .close = st21nfca_hci_close,
861 .load_session = st21nfca_hci_load_session,
862 .hci_ready = st21nfca_hci_ready,
863 .xmit = st21nfca_hci_xmit,
864 .start_poll = st21nfca_hci_start_poll,
865 .stop_poll = st21nfca_hci_stop_poll,
866 .dep_link_up = st21nfca_hci_dep_link_up,
867 .dep_link_down = st21nfca_hci_dep_link_down,
868 .target_from_gate = st21nfca_hci_target_from_gate,
869 .complete_target_discovered = st21nfca_hci_complete_target_discovered,
870 .im_transceive = st21nfca_hci_im_transceive,
871 .tm_send = st21nfca_hci_tm_send,
872 .check_presence = st21nfca_hci_check_presence,
873 .event_received = st21nfca_hci_event_received,
876 int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
877 char *llc_name, int phy_headroom, int phy_tailroom,
878 int phy_payload, struct nfc_hci_dev **hdev)
880 struct st21nfca_hci_info *info;
884 struct nfc_hci_init_data init_data;
885 unsigned long quirks = 0;
887 info = kzalloc(sizeof(struct st21nfca_hci_info), GFP_KERNEL);
893 info->phy_ops = phy_ops;
894 info->phy_id = phy_id;
895 info->state = ST21NFCA_ST_COLD;
896 mutex_init(&info->info_lock);
898 init_data.gate_count = ARRAY_SIZE(st21nfca_gates);
900 memcpy(init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
903 * Session id must include the driver name + i2c bus addr
904 * persistent info to discriminate 2 identical chips
906 dev_num = find_first_zero_bit(dev_mask, ST21NFCA_NUM_DEVICES);
907 if (dev_num >= ST21NFCA_NUM_DEVICES)
910 scnprintf(init_data.session_id, sizeof(init_data.session_id), "%s%2x",
913 protocols = NFC_PROTO_JEWEL_MASK |
914 NFC_PROTO_MIFARE_MASK |
915 NFC_PROTO_FELICA_MASK |
916 NFC_PROTO_ISO14443_MASK |
917 NFC_PROTO_ISO14443_B_MASK |
918 NFC_PROTO_ISO15693_MASK |
919 NFC_PROTO_NFC_DEP_MASK;
921 set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks);
924 nfc_hci_allocate_device(&st21nfca_hci_ops, &init_data, quirks,
926 phy_headroom + ST21NFCA_CMDS_HEADROOM,
927 phy_tailroom, phy_payload);
930 pr_err("Cannot allocate nfc hdev.\n");
935 nfc_hci_set_clientdata(info->hdev, info);
937 r = nfc_hci_register_device(info->hdev);
942 st21nfca_dep_init(info->hdev);
947 nfc_hci_free_device(info->hdev);
954 EXPORT_SYMBOL(st21nfca_hci_probe);
956 void st21nfca_hci_remove(struct nfc_hci_dev *hdev)
958 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
960 st21nfca_dep_deinit(hdev);
961 nfc_hci_unregister_device(hdev);
962 nfc_hci_free_device(hdev);
965 EXPORT_SYMBOL(st21nfca_hci_remove);
967 MODULE_LICENSE("GPL");
968 MODULE_DESCRIPTION(DRIVER_DESC);