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_RF_READER_F_DATARATE 0x01
49 #define ST21NFCA_RF_READER_F_DATARATE_106 0x01
50 #define ST21NFCA_RF_READER_F_DATARATE_212 0x02
51 #define ST21NFCA_RF_READER_F_DATARATE_424 0x04
52 #define ST21NFCA_RF_READER_F_POL_REQ 0x02
53 #define ST21NFCA_RF_READER_F_POL_REQ_DEFAULT 0xffff0000
54 #define ST21NFCA_RF_READER_F_NFCID2 0x03
55 #define ST21NFCA_RF_READER_F_NFCID1 0x04
57 #define ST21NFCA_RF_CARD_F_MODE 0x01
58 #define ST21NFCA_RF_CARD_F_NFCID2_LIST 0x04
59 #define ST21NFCA_RF_CARD_F_NFCID1 0x05
60 #define ST21NFCA_RF_CARD_F_SENS_RES 0x06
61 #define ST21NFCA_RF_CARD_F_SEL_RES 0x07
62 #define ST21NFCA_RF_CARD_F_DATARATE 0x08
63 #define ST21NFCA_RF_CARD_F_DATARATE_212_424 0x01
65 #define ST21NFCA_DEVICE_MGNT_GATE 0x01
66 #define ST21NFCA_DEVICE_MGNT_PIPE 0x02
68 #define ST21NFCA_DM_GETINFO 0x13
69 #define ST21NFCA_DM_GETINFO_PIPE_LIST 0x02
70 #define ST21NFCA_DM_GETINFO_PIPE_INFO 0x01
71 #define ST21NFCA_DM_PIPE_CREATED 0x02
72 #define ST21NFCA_DM_PIPE_OPEN 0x04
73 #define ST21NFCA_DM_RF_ACTIVE 0x80
74 #define ST21NFCA_DM_DISCONNECT 0x30
76 #define ST21NFCA_DM_IS_PIPE_OPEN(p) \
77 ((p & 0x0f) == (ST21NFCA_DM_PIPE_CREATED | ST21NFCA_DM_PIPE_OPEN))
79 #define ST21NFCA_NFC_MODE 0x03 /* NFC_MODE parameter*/
81 static DECLARE_BITMAP(dev_mask, ST21NFCA_NUM_DEVICES);
83 static struct nfc_hci_gate st21nfca_gates[] = {
84 {NFC_HCI_ADMIN_GATE, NFC_HCI_ADMIN_PIPE},
85 {NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
86 {NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
87 {NFC_HCI_LINK_MGMT_GATE, NFC_HCI_LINK_MGMT_PIPE},
88 {NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
89 {NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
90 {ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE},
91 {ST21NFCA_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
92 {ST21NFCA_RF_READER_14443_3_A_GATE, NFC_HCI_INVALID_PIPE},
93 {ST21NFCA_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE},
94 {ST21NFCA_RF_CARD_F_GATE, NFC_HCI_INVALID_PIPE},
97 struct st21nfca_pipe_info {
105 /* Largest headroom needed for outgoing custom commands */
106 #define ST21NFCA_CMDS_HEADROOM 7
108 static int st21nfca_hci_load_session(struct nfc_hci_dev *hdev)
111 struct sk_buff *skb_pipe_list, *skb_pipe_info;
112 struct st21nfca_pipe_info *info;
114 u8 pipe_list[] = { ST21NFCA_DM_GETINFO_PIPE_LIST,
115 NFC_HCI_TERMINAL_HOST_ID
117 u8 pipe_info[] = { ST21NFCA_DM_GETINFO_PIPE_INFO,
118 NFC_HCI_TERMINAL_HOST_ID, 0
121 skb_pipe_list = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL);
122 if (!skb_pipe_list) {
127 skb_pipe_info = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL);
128 if (!skb_pipe_info) {
133 /* On ST21NFCA device pipes number are dynamics
134 * A maximum of 16 pipes can be created at the same time
135 * If pipes are already created, hci_dev_up will fail.
136 * Doing a clear all pipe is a bad idea because:
137 * - It does useless EEPROM cycling
138 * - It might cause issue for secure elements support
139 * (such as removing connectivity or APDU reader pipe)
140 * A better approach on ST21NFCA is to:
141 * - get a pipe list for each host.
142 * (eg: NFC_HCI_HOST_CONTROLLER_ID for now).
143 * (TODO Later on UICC HOST and eSE HOST)
144 * - get pipe information
145 * - match retrieved pipe list in st21nfca_gates
146 * ST21NFCA_DEVICE_MGNT_GATE is a proprietary gate
147 * with ST21NFCA_DEVICE_MGNT_PIPE.
148 * Pipe can be closed and need to be open.
150 r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
151 ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE);
156 r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
157 ST21NFCA_DM_GETINFO, pipe_list, sizeof(pipe_list),
162 /* Complete the existing gate_pipe table */
163 for (i = 0; i < skb_pipe_list->len; i++) {
164 pipe_info[2] = skb_pipe_list->data[i];
165 r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
166 ST21NFCA_DM_GETINFO, pipe_info,
167 sizeof(pipe_info), &skb_pipe_info);
173 * Match pipe ID and gate ID
174 * Output format from ST21NFC_DM_GETINFO is:
175 * - pipe state (1byte)
176 * - source hid (1byte)
177 * - source gid (1byte)
178 * - destination hid (1byte)
179 * - destination gid (1byte)
181 info = (struct st21nfca_pipe_info *) skb_pipe_info->data;
182 for (j = 0; (j < ARRAY_SIZE(st21nfca_gates)) &&
183 (st21nfca_gates[j].gate != info->dst_gate_id);
187 if (j < ARRAY_SIZE(st21nfca_gates) &&
188 st21nfca_gates[j].gate == info->dst_gate_id &&
189 ST21NFCA_DM_IS_PIPE_OPEN(info->pipe_state)) {
190 st21nfca_gates[j].pipe = pipe_info[2];
191 hdev->gate2pipe[st21nfca_gates[j].gate] =
192 st21nfca_gates[j].pipe;
197 * 3 gates have a well known pipe ID.
198 * They will never appear in the pipe list
200 if (skb_pipe_list->len + 3 < ARRAY_SIZE(st21nfca_gates)) {
201 for (i = skb_pipe_list->len + 3;
202 i < ARRAY_SIZE(st21nfca_gates); i++) {
203 r = nfc_hci_connect_gate(hdev,
204 NFC_HCI_HOST_CONTROLLER_ID,
205 st21nfca_gates[i].gate,
206 st21nfca_gates[i].pipe);
212 memcpy(hdev->init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
214 kfree_skb(skb_pipe_info);
216 kfree_skb(skb_pipe_list);
220 static int st21nfca_hci_open(struct nfc_hci_dev *hdev)
222 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
225 mutex_lock(&info->info_lock);
227 if (info->state != ST21NFCA_ST_COLD) {
232 r = info->phy_ops->enable(info->phy_id);
235 info->state = ST21NFCA_ST_READY;
238 mutex_unlock(&info->info_lock);
242 static void st21nfca_hci_close(struct nfc_hci_dev *hdev)
244 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
246 mutex_lock(&info->info_lock);
248 if (info->state == ST21NFCA_ST_COLD)
251 info->phy_ops->disable(info->phy_id);
252 info->state = ST21NFCA_ST_COLD;
255 mutex_unlock(&info->info_lock);
258 static int st21nfca_hci_ready(struct nfc_hci_dev *hdev)
265 param = NFC_HCI_UICC_HOST_ID;
266 r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
267 NFC_HCI_ADMIN_WHITELIST, ¶m, 1);
271 /* Set NFC_MODE in device management gate to enable */
272 r = nfc_hci_get_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
273 ST21NFCA_NFC_MODE, &skb);
277 if (skb->data[0] == 0) {
281 r = nfc_hci_set_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
282 ST21NFCA_NFC_MODE, ¶m, 1);
287 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
288 NFC_HCI_EVT_END_OPERATION, NULL, 0);
292 r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
293 NFC_HCI_ID_MGMT_VERSION_SW, &skb);
297 if (skb->len != FULL_VERSION_LEN) {
302 print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
303 DUMP_PREFIX_NONE, 16, 1,
304 skb->data, FULL_VERSION_LEN, false);
311 static int st21nfca_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
313 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
315 return info->phy_ops->write(info->phy_id, skb);
318 static int st21nfca_hci_start_poll(struct nfc_hci_dev *hdev,
319 u32 im_protocols, u32 tm_protocols)
324 struct sk_buff *datarate_skb;
326 pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
327 __func__, im_protocols, tm_protocols);
329 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
330 NFC_HCI_EVT_END_OPERATION, NULL, 0);
335 * enable polling according to im_protocols & tm_protocols
336 * - CLOSE pipe according to im_protocols & tm_protocols
338 if ((NFC_HCI_RF_READER_B_GATE & im_protocols) == 0) {
339 r = nfc_hci_disconnect_gate(hdev,
340 NFC_HCI_RF_READER_B_GATE);
345 if ((NFC_HCI_RF_READER_A_GATE & im_protocols) == 0) {
346 r = nfc_hci_disconnect_gate(hdev,
347 NFC_HCI_RF_READER_A_GATE);
352 if ((ST21NFCA_RF_READER_F_GATE & im_protocols) == 0) {
353 r = nfc_hci_disconnect_gate(hdev,
354 ST21NFCA_RF_READER_F_GATE);
358 hdev->gb = nfc_get_local_general_bytes(hdev->ndev,
361 if (hdev->gb == NULL || hdev->gb_len == 0) {
362 im_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
363 tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
366 param[0] = ST21NFCA_RF_READER_F_DATARATE_106 |
367 ST21NFCA_RF_READER_F_DATARATE_212 |
368 ST21NFCA_RF_READER_F_DATARATE_424;
369 r = nfc_hci_set_param(hdev, ST21NFCA_RF_READER_F_GATE,
370 ST21NFCA_RF_READER_F_DATARATE,
375 pol_req = be32_to_cpu((__force __be32)
376 ST21NFCA_RF_READER_F_POL_REQ_DEFAULT);
377 r = nfc_hci_set_param(hdev, ST21NFCA_RF_READER_F_GATE,
378 ST21NFCA_RF_READER_F_POL_REQ,
384 if ((ST21NFCA_RF_READER_14443_3_A_GATE & im_protocols) == 0) {
385 r = nfc_hci_disconnect_gate(hdev,
386 ST21NFCA_RF_READER_14443_3_A_GATE);
391 if ((ST21NFCA_RF_READER_ISO15693_GATE & im_protocols) == 0) {
392 r = nfc_hci_disconnect_gate(hdev,
393 ST21NFCA_RF_READER_ISO15693_GATE);
398 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
399 NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
401 nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
402 NFC_HCI_EVT_END_OPERATION, NULL, 0);
405 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
406 r = nfc_hci_get_param(hdev, ST21NFCA_RF_CARD_F_GATE,
407 ST21NFCA_RF_CARD_F_DATARATE,
412 /* Configure the maximum supported datarate to 424Kbps */
413 if (datarate_skb->len > 0 &&
414 datarate_skb->data[0] !=
415 ST21NFCA_RF_CARD_F_DATARATE_212_424) {
416 param[0] = ST21NFCA_RF_CARD_F_DATARATE_212_424;
417 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
418 ST21NFCA_RF_CARD_F_DATARATE,
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 *gate,
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 gate = uid_skb->data;
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 *nfcid2_skb = NULL, *nfcid1_skb;
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, &nfcid2_skb);
684 if (nfcid2_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 (nfcid2_skb->len > 0) {
698 memcpy(target->sensf_res, nfcid2_skb->data,
700 target->sensf_res_len = nfcid2_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;
711 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE,
712 ST21NFCA_RF_READER_F_NFCID1,
717 if (nfcid1_skb->len > NFC_NFCID1_MAXSIZE) {
721 memcpy(target->sensf_res, nfcid1_skb->data,
723 target->sensf_res_len = nfcid1_skb->len;
724 target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
726 target->hci_reader_gate = ST21NFCA_RF_READER_F_GATE;
730 kfree_skb(nfcid2_skb);
734 #define ST21NFCA_CB_TYPE_READER_ISO15693 1
735 static void st21nfca_hci_data_exchange_cb(void *context, struct sk_buff *skb,
738 struct st21nfca_hci_info *info = context;
740 switch (info->async_cb_type) {
741 case ST21NFCA_CB_TYPE_READER_ISO15693:
743 skb_trim(skb, skb->len - 1);
744 info->async_cb(info->async_cb_context, skb, err);
755 * <= 0: driver handled the data exchange
756 * 1: driver doesn't especially handle, please do standard processing
758 static int st21nfca_hci_im_transceive(struct nfc_hci_dev *hdev,
759 struct nfc_target *target,
761 data_exchange_cb_t cb, void *cb_context)
763 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
765 pr_info(DRIVER_DESC ": %s for gate=%d len=%d\n", __func__,
766 target->hci_reader_gate, skb->len);
768 switch (target->hci_reader_gate) {
769 case ST21NFCA_RF_READER_F_GATE:
770 if (target->supported_protocols == NFC_PROTO_NFC_DEP_MASK)
771 return st21nfca_im_send_dep_req(hdev, skb);
773 *skb_push(skb, 1) = 0x1a;
774 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
775 ST21NFCA_WR_XCHG_DATA, skb->data,
776 skb->len, cb, cb_context);
777 case ST21NFCA_RF_READER_14443_3_A_GATE:
778 *skb_push(skb, 1) = 0x1a; /* CTR, see spec:10.2.2.1 */
780 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
781 ST21NFCA_WR_XCHG_DATA, skb->data,
782 skb->len, cb, cb_context);
783 case ST21NFCA_RF_READER_ISO15693_GATE:
784 info->async_cb_type = ST21NFCA_CB_TYPE_READER_ISO15693;
786 info->async_cb_context = cb_context;
788 *skb_push(skb, 1) = 0x17;
790 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
791 ST21NFCA_WR_XCHG_DATA, skb->data,
793 st21nfca_hci_data_exchange_cb,
801 static int st21nfca_hci_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
803 return st21nfca_tm_send_dep_res(hdev, skb);
806 static int st21nfca_hci_check_presence(struct nfc_hci_dev *hdev,
807 struct nfc_target *target)
811 switch (target->hci_reader_gate) {
812 case NFC_HCI_RF_READER_A_GATE:
813 case NFC_HCI_RF_READER_B_GATE:
815 * PRESENCE_CHECK on those gates is available
816 * However, the answer to this command is taking 3 * fwi
817 * if the card is no present.
818 * Instead, we send an empty I-Frame with a very short
819 * configurable fwi ~604µs.
821 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
822 ST21NFCA_WR_XCHG_DATA, &fwi, 1, NULL);
823 case ST21NFCA_RF_READER_14443_3_A_GATE:
824 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
825 ST21NFCA_RF_READER_CMD_PRESENCE_CHECK,
834 * <= 0: driver handled the event, skb consumed
835 * 1: driver does not handle the event, please do standard processing
837 static int st21nfca_hci_event_received(struct nfc_hci_dev *hdev, u8 gate,
838 u8 event, struct sk_buff *skb)
840 pr_debug("hci event: %d gate: %x\n", event, gate);
843 case ST21NFCA_RF_CARD_F_GATE:
844 return st21nfca_dep_event_received(hdev, event, skb);
852 static struct nfc_hci_ops st21nfca_hci_ops = {
853 .open = st21nfca_hci_open,
854 .close = st21nfca_hci_close,
855 .load_session = st21nfca_hci_load_session,
856 .hci_ready = st21nfca_hci_ready,
857 .xmit = st21nfca_hci_xmit,
858 .start_poll = st21nfca_hci_start_poll,
859 .stop_poll = st21nfca_hci_stop_poll,
860 .dep_link_up = st21nfca_hci_dep_link_up,
861 .dep_link_down = st21nfca_hci_dep_link_down,
862 .target_from_gate = st21nfca_hci_target_from_gate,
863 .complete_target_discovered = st21nfca_hci_complete_target_discovered,
864 .im_transceive = st21nfca_hci_im_transceive,
865 .tm_send = st21nfca_hci_tm_send,
866 .check_presence = st21nfca_hci_check_presence,
867 .event_received = st21nfca_hci_event_received,
870 int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
871 char *llc_name, int phy_headroom, int phy_tailroom,
872 int phy_payload, struct nfc_hci_dev **hdev)
874 struct st21nfca_hci_info *info;
878 struct nfc_hci_init_data init_data;
879 unsigned long quirks = 0;
881 info = kzalloc(sizeof(struct st21nfca_hci_info), GFP_KERNEL);
887 info->phy_ops = phy_ops;
888 info->phy_id = phy_id;
889 info->state = ST21NFCA_ST_COLD;
890 mutex_init(&info->info_lock);
892 init_data.gate_count = ARRAY_SIZE(st21nfca_gates);
894 memcpy(init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
897 * Session id must include the driver name + i2c bus addr
898 * persistent info to discriminate 2 identical chips
900 dev_num = find_first_zero_bit(dev_mask, ST21NFCA_NUM_DEVICES);
902 if (dev_num >= ST21NFCA_NUM_DEVICES)
905 set_bit(dev_num, dev_mask);
907 scnprintf(init_data.session_id, sizeof(init_data.session_id), "%s%2x",
910 protocols = NFC_PROTO_JEWEL_MASK |
911 NFC_PROTO_MIFARE_MASK |
912 NFC_PROTO_FELICA_MASK |
913 NFC_PROTO_ISO14443_MASK |
914 NFC_PROTO_ISO14443_B_MASK |
915 NFC_PROTO_ISO15693_MASK |
916 NFC_PROTO_NFC_DEP_MASK;
918 set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks);
921 nfc_hci_allocate_device(&st21nfca_hci_ops, &init_data, quirks,
923 phy_headroom + ST21NFCA_CMDS_HEADROOM,
924 phy_tailroom, phy_payload);
927 pr_err("Cannot allocate nfc hdev.\n");
932 nfc_hci_set_clientdata(info->hdev, info);
934 r = nfc_hci_register_device(info->hdev);
939 st21nfca_dep_init(info->hdev);
944 nfc_hci_free_device(info->hdev);
951 EXPORT_SYMBOL(st21nfca_hci_probe);
953 void st21nfca_hci_remove(struct nfc_hci_dev *hdev)
955 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
957 st21nfca_dep_deinit(hdev);
958 nfc_hci_unregister_device(hdev);
959 nfc_hci_free_device(hdev);
962 EXPORT_SYMBOL(st21nfca_hci_remove);
964 MODULE_LICENSE("GPL");
965 MODULE_DESCRIPTION(DRIVER_DESC);