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*/
80 #define ST21NFCA_EVT_FIELD_ON 0x11
81 #define ST21NFCA_EVT_CARD_DEACTIVATED 0x12
82 #define ST21NFCA_EVT_CARD_ACTIVATED 0x13
83 #define ST21NFCA_EVT_FIELD_OFF 0x14
85 static DECLARE_BITMAP(dev_mask, ST21NFCA_NUM_DEVICES);
87 static struct nfc_hci_gate st21nfca_gates[] = {
88 {NFC_HCI_ADMIN_GATE, NFC_HCI_ADMIN_PIPE},
89 {NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
90 {NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
91 {NFC_HCI_LINK_MGMT_GATE, NFC_HCI_LINK_MGMT_PIPE},
92 {NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
93 {NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
94 {ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE},
95 {ST21NFCA_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
96 {ST21NFCA_RF_READER_14443_3_A_GATE, NFC_HCI_INVALID_PIPE},
97 {ST21NFCA_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE},
98 {ST21NFCA_RF_CARD_F_GATE, NFC_HCI_INVALID_PIPE},
101 struct st21nfca_pipe_info {
109 /* Largest headroom needed for outgoing custom commands */
110 #define ST21NFCA_CMDS_HEADROOM 7
112 static int st21nfca_hci_load_session(struct nfc_hci_dev *hdev)
115 struct sk_buff *skb_pipe_list, *skb_pipe_info;
116 struct st21nfca_pipe_info *info;
118 u8 pipe_list[] = { ST21NFCA_DM_GETINFO_PIPE_LIST,
119 NFC_HCI_TERMINAL_HOST_ID
121 u8 pipe_info[] = { ST21NFCA_DM_GETINFO_PIPE_INFO,
122 NFC_HCI_TERMINAL_HOST_ID, 0
125 skb_pipe_list = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL);
126 if (!skb_pipe_list) {
131 skb_pipe_info = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL);
132 if (!skb_pipe_info) {
137 /* On ST21NFCA device pipes number are dynamics
138 * A maximum of 16 pipes can be created at the same time
139 * If pipes are already created, hci_dev_up will fail.
140 * Doing a clear all pipe is a bad idea because:
141 * - It does useless EEPROM cycling
142 * - It might cause issue for secure elements support
143 * (such as removing connectivity or APDU reader pipe)
144 * A better approach on ST21NFCA is to:
145 * - get a pipe list for each host.
146 * (eg: NFC_HCI_HOST_CONTROLLER_ID for now).
147 * (TODO Later on UICC HOST and eSE HOST)
148 * - get pipe information
149 * - match retrieved pipe list in st21nfca_gates
150 * ST21NFCA_DEVICE_MGNT_GATE is a proprietary gate
151 * with ST21NFCA_DEVICE_MGNT_PIPE.
152 * Pipe can be closed and need to be open.
154 r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
155 ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE);
160 r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
161 ST21NFCA_DM_GETINFO, pipe_list, sizeof(pipe_list),
166 /* Complete the existing gate_pipe table */
167 for (i = 0; i < skb_pipe_list->len; i++) {
168 pipe_info[2] = skb_pipe_list->data[i];
169 r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
170 ST21NFCA_DM_GETINFO, pipe_info,
171 sizeof(pipe_info), &skb_pipe_info);
177 * Match pipe ID and gate ID
178 * Output format from ST21NFC_DM_GETINFO is:
179 * - pipe state (1byte)
180 * - source hid (1byte)
181 * - source gid (1byte)
182 * - destination hid (1byte)
183 * - destination gid (1byte)
185 info = (struct st21nfca_pipe_info *) skb_pipe_info->data;
186 for (j = 0; (j < ARRAY_SIZE(st21nfca_gates)) &&
187 (st21nfca_gates[j].gate != info->dst_gate_id);
191 if (j < ARRAY_SIZE(st21nfca_gates) &&
192 st21nfca_gates[j].gate == info->dst_gate_id &&
193 ST21NFCA_DM_IS_PIPE_OPEN(info->pipe_state)) {
194 st21nfca_gates[j].pipe = pipe_info[2];
195 hdev->gate2pipe[st21nfca_gates[j].gate] =
196 st21nfca_gates[j].pipe;
201 * 3 gates have a well known pipe ID.
202 * They will never appear in the pipe list
204 if (skb_pipe_list->len + 3 < ARRAY_SIZE(st21nfca_gates)) {
205 for (i = skb_pipe_list->len + 3;
206 i < ARRAY_SIZE(st21nfca_gates); i++) {
207 r = nfc_hci_connect_gate(hdev,
208 NFC_HCI_HOST_CONTROLLER_ID,
209 st21nfca_gates[i].gate,
210 st21nfca_gates[i].pipe);
216 memcpy(hdev->init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
218 kfree_skb(skb_pipe_info);
220 kfree_skb(skb_pipe_list);
224 static int st21nfca_hci_open(struct nfc_hci_dev *hdev)
226 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
229 mutex_lock(&info->info_lock);
231 if (info->state != ST21NFCA_ST_COLD) {
236 r = info->phy_ops->enable(info->phy_id);
239 info->state = ST21NFCA_ST_READY;
242 mutex_unlock(&info->info_lock);
246 static void st21nfca_hci_close(struct nfc_hci_dev *hdev)
248 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
250 mutex_lock(&info->info_lock);
252 if (info->state == ST21NFCA_ST_COLD)
255 info->phy_ops->disable(info->phy_id);
256 info->state = ST21NFCA_ST_COLD;
259 mutex_unlock(&info->info_lock);
262 static int st21nfca_hci_ready(struct nfc_hci_dev *hdev)
269 param = NFC_HCI_UICC_HOST_ID;
270 r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
271 NFC_HCI_ADMIN_WHITELIST, ¶m, 1);
275 /* Set NFC_MODE in device management gate to enable */
276 r = nfc_hci_get_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
277 ST21NFCA_NFC_MODE, &skb);
281 if (skb->data[0] == 0) {
285 r = nfc_hci_set_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
286 ST21NFCA_NFC_MODE, ¶m, 1);
291 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
292 NFC_HCI_EVT_END_OPERATION, NULL, 0);
296 r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
297 NFC_HCI_ID_MGMT_VERSION_SW, &skb);
301 if (skb->len != FULL_VERSION_LEN) {
306 print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
307 DUMP_PREFIX_NONE, 16, 1,
308 skb->data, FULL_VERSION_LEN, false);
315 static int st21nfca_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
317 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
319 return info->phy_ops->write(info->phy_id, skb);
322 static int st21nfca_hci_start_poll(struct nfc_hci_dev *hdev,
323 u32 im_protocols, u32 tm_protocols)
328 struct sk_buff *datarate_skb;
330 pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
331 __func__, im_protocols, tm_protocols);
333 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
334 NFC_HCI_EVT_END_OPERATION, NULL, 0);
339 * enable polling according to im_protocols & tm_protocols
340 * - CLOSE pipe according to im_protocols & tm_protocols
342 if ((NFC_HCI_RF_READER_B_GATE & im_protocols) == 0) {
343 r = nfc_hci_disconnect_gate(hdev,
344 NFC_HCI_RF_READER_B_GATE);
349 if ((NFC_HCI_RF_READER_A_GATE & im_protocols) == 0) {
350 r = nfc_hci_disconnect_gate(hdev,
351 NFC_HCI_RF_READER_A_GATE);
356 if ((ST21NFCA_RF_READER_F_GATE & im_protocols) == 0) {
357 r = nfc_hci_disconnect_gate(hdev,
358 ST21NFCA_RF_READER_F_GATE);
362 hdev->gb = nfc_get_local_general_bytes(hdev->ndev,
365 if (hdev->gb == NULL || hdev->gb_len == 0) {
366 im_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
367 tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
370 param[0] = ST21NFCA_RF_READER_F_DATARATE_106 |
371 ST21NFCA_RF_READER_F_DATARATE_212 |
372 ST21NFCA_RF_READER_F_DATARATE_424;
373 r = nfc_hci_set_param(hdev, ST21NFCA_RF_READER_F_GATE,
374 ST21NFCA_RF_READER_F_DATARATE,
379 pol_req = be32_to_cpu((__force __be32)
380 ST21NFCA_RF_READER_F_POL_REQ_DEFAULT);
381 r = nfc_hci_set_param(hdev, ST21NFCA_RF_READER_F_GATE,
382 ST21NFCA_RF_READER_F_POL_REQ,
388 if ((ST21NFCA_RF_READER_14443_3_A_GATE & im_protocols) == 0) {
389 r = nfc_hci_disconnect_gate(hdev,
390 ST21NFCA_RF_READER_14443_3_A_GATE);
395 if ((ST21NFCA_RF_READER_ISO15693_GATE & im_protocols) == 0) {
396 r = nfc_hci_disconnect_gate(hdev,
397 ST21NFCA_RF_READER_ISO15693_GATE);
402 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
403 NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
405 nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
406 NFC_HCI_EVT_END_OPERATION, NULL, 0);
409 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
410 r = nfc_hci_get_param(hdev, ST21NFCA_RF_CARD_F_GATE,
411 ST21NFCA_RF_CARD_F_DATARATE,
416 /* Configure the maximum supported datarate to 424Kbps */
417 if (datarate_skb->len > 0 &&
418 datarate_skb->data[0] !=
419 ST21NFCA_RF_CARD_F_DATARATE_212_424) {
420 param[0] = ST21NFCA_RF_CARD_F_DATARATE_212_424;
421 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
422 ST21NFCA_RF_CARD_F_DATARATE,
431 * NFC Forum Digital Spec Table 7:
432 * NFCID1 size: triple (10 bytes)
436 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
437 ST21NFCA_RF_CARD_F_SENS_RES, param, 2);
444 * NFC Forum Digistal Spec Table 17:
445 * b3 set to 0b (value b7-b6):
446 * - 10b: Configured for NFC-DEP Protocol
449 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
450 ST21NFCA_RF_CARD_F_SEL_RES, param, 1);
454 /* Configure NFCID1 Random uid */
455 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
456 ST21NFCA_RF_CARD_F_NFCID1, NULL, 0);
460 /* Configure NFCID2_LIST */
473 /* 8 byte Pad bytes used for polling respone frame */
476 * Configuration byte:
477 * - bit 0: define the default NFCID2 entry used when the
478 * system code is equal to 'FFFF'
479 * - bit 1: use a random value for lowest 6 bytes of
481 * - bit 2: ignore polling request frame if request code
483 * - Other bits are RFU
486 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
487 ST21NFCA_RF_CARD_F_NFCID2_LIST, param,
493 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
494 ST21NFCA_RF_CARD_F_MODE, param, 1);
500 static void st21nfca_hci_stop_poll(struct nfc_hci_dev *hdev)
502 nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
503 ST21NFCA_DM_DISCONNECT, NULL, 0, NULL);
506 static int st21nfca_get_iso14443_3_atqa(struct nfc_hci_dev *hdev, u16 *atqa)
509 struct sk_buff *atqa_skb = NULL;
511 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
512 ST21NFCA_RF_READER_14443_3_A_ATQA, &atqa_skb);
516 if (atqa_skb->len != 2) {
521 *atqa = be16_to_cpu(*(__be16 *) atqa_skb->data);
528 static int st21nfca_get_iso14443_3_sak(struct nfc_hci_dev *hdev, u8 *sak)
531 struct sk_buff *sak_skb = NULL;
533 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
534 ST21NFCA_RF_READER_14443_3_A_SAK, &sak_skb);
538 if (sak_skb->len != 1) {
543 *sak = sak_skb->data[0];
550 static int st21nfca_get_iso14443_3_uid(struct nfc_hci_dev *hdev, u8 *gate,
554 struct sk_buff *uid_skb = NULL;
556 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
557 ST21NFCA_RF_READER_14443_3_A_UID, &uid_skb);
561 if (uid_skb->len == 0 || uid_skb->len > NFC_NFCID1_MAXSIZE) {
566 gate = uid_skb->data;
573 static int st21nfca_get_iso15693_inventory(struct nfc_hci_dev *hdev,
574 struct nfc_target *target)
577 struct sk_buff *inventory_skb = NULL;
579 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_ISO15693_GATE,
580 ST21NFCA_RF_READER_ISO15693_INVENTORY,
585 skb_pull(inventory_skb, 2);
587 if (inventory_skb->len == 0 ||
588 inventory_skb->len > NFC_ISO15693_UID_MAXSIZE) {
593 memcpy(target->iso15693_uid, inventory_skb->data, inventory_skb->len);
594 target->iso15693_dsfid = inventory_skb->data[1];
595 target->is_iso15693 = 1;
597 kfree_skb(inventory_skb);
601 static int st21nfca_hci_dep_link_up(struct nfc_hci_dev *hdev,
602 struct nfc_target *target, u8 comm_mode,
603 u8 *gb, size_t gb_len)
605 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
607 info->dep_info.idx = target->idx;
608 return st21nfca_im_send_atr_req(hdev, gb, gb_len);
611 static int st21nfca_hci_dep_link_down(struct nfc_hci_dev *hdev)
613 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
615 info->state = ST21NFCA_ST_READY;
617 return nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
618 ST21NFCA_DM_DISCONNECT, NULL, 0, NULL);
621 static int st21nfca_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
622 struct nfc_target *target)
627 u8 uid[NFC_NFCID1_MAXSIZE];
630 case ST21NFCA_RF_READER_F_GATE:
631 target->supported_protocols = NFC_PROTO_FELICA_MASK;
633 case ST21NFCA_RF_READER_14443_3_A_GATE:
634 /* ISO14443-3 type 1 or 2 tags */
635 r = st21nfca_get_iso14443_3_atqa(hdev, &atqa);
638 if (atqa == 0x000c) {
639 target->supported_protocols = NFC_PROTO_JEWEL_MASK;
640 target->sens_res = 0x0c00;
642 r = st21nfca_get_iso14443_3_sak(hdev, &sak);
646 r = st21nfca_get_iso14443_3_uid(hdev, uid, &len);
650 target->supported_protocols =
651 nfc_hci_sak_to_protocol(sak);
652 if (target->supported_protocols == 0xffffffff)
655 target->sens_res = atqa;
656 target->sel_res = sak;
657 memcpy(target->nfcid1, uid, len);
658 target->nfcid1_len = len;
662 case ST21NFCA_RF_READER_ISO15693_GATE:
663 target->supported_protocols = NFC_PROTO_ISO15693_MASK;
664 r = st21nfca_get_iso15693_inventory(hdev, target);
675 static int st21nfca_hci_complete_target_discovered(struct nfc_hci_dev *hdev,
677 struct nfc_target *target)
680 struct sk_buff *nfcid2_skb = NULL, *nfcid1_skb;
682 if (gate == ST21NFCA_RF_READER_F_GATE) {
683 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE,
684 ST21NFCA_RF_READER_F_NFCID2, &nfcid2_skb);
688 if (nfcid2_skb->len > NFC_SENSF_RES_MAXSIZE) {
694 * - After the recepton of polling response for type F frame
695 * at 212 or 424 Kbit/s, NFCID2 registry parameters will be
697 * - After the reception of SEL_RES with NFCIP-1 compliant bit
698 * set for type A frame NFCID1 will be updated
700 if (nfcid2_skb->len > 0) {
702 memcpy(target->sensf_res, nfcid2_skb->data,
704 target->sensf_res_len = nfcid2_skb->len;
705 /* NFC Forum Digital Protocol Table 44 */
706 if (target->sensf_res[0] == 0x01 &&
707 target->sensf_res[1] == 0xfe)
708 target->supported_protocols =
709 NFC_PROTO_NFC_DEP_MASK;
711 target->supported_protocols =
712 NFC_PROTO_FELICA_MASK;
715 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE,
716 ST21NFCA_RF_READER_F_NFCID1,
721 if (nfcid1_skb->len > NFC_NFCID1_MAXSIZE) {
725 memcpy(target->sensf_res, nfcid1_skb->data,
727 target->sensf_res_len = nfcid1_skb->len;
728 target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
730 target->hci_reader_gate = ST21NFCA_RF_READER_F_GATE;
734 kfree_skb(nfcid2_skb);
738 #define ST21NFCA_CB_TYPE_READER_ISO15693 1
739 static void st21nfca_hci_data_exchange_cb(void *context, struct sk_buff *skb,
742 struct st21nfca_hci_info *info = context;
744 switch (info->async_cb_type) {
745 case ST21NFCA_CB_TYPE_READER_ISO15693:
747 skb_trim(skb, skb->len - 1);
748 info->async_cb(info->async_cb_context, skb, err);
759 * <= 0: driver handled the data exchange
760 * 1: driver doesn't especially handle, please do standard processing
762 static int st21nfca_hci_im_transceive(struct nfc_hci_dev *hdev,
763 struct nfc_target *target,
765 data_exchange_cb_t cb, void *cb_context)
767 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
769 pr_info(DRIVER_DESC ": %s for gate=%d len=%d\n", __func__,
770 target->hci_reader_gate, skb->len);
772 switch (target->hci_reader_gate) {
773 case ST21NFCA_RF_READER_F_GATE:
774 if (target->supported_protocols == NFC_PROTO_NFC_DEP_MASK)
775 return st21nfca_im_send_dep_req(hdev, skb);
777 *skb_push(skb, 1) = 0x1a;
778 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
779 ST21NFCA_WR_XCHG_DATA, skb->data,
780 skb->len, cb, cb_context);
781 case ST21NFCA_RF_READER_14443_3_A_GATE:
782 *skb_push(skb, 1) = 0x1a; /* CTR, see spec:10.2.2.1 */
784 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
785 ST21NFCA_WR_XCHG_DATA, skb->data,
786 skb->len, cb, cb_context);
787 case ST21NFCA_RF_READER_ISO15693_GATE:
788 info->async_cb_type = ST21NFCA_CB_TYPE_READER_ISO15693;
790 info->async_cb_context = cb_context;
792 *skb_push(skb, 1) = 0x17;
794 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
795 ST21NFCA_WR_XCHG_DATA, skb->data,
797 st21nfca_hci_data_exchange_cb,
805 static int st21nfca_hci_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
807 return st21nfca_tm_send_dep_res(hdev, skb);
810 static int st21nfca_hci_check_presence(struct nfc_hci_dev *hdev,
811 struct nfc_target *target)
815 switch (target->hci_reader_gate) {
816 case NFC_HCI_RF_READER_A_GATE:
817 case NFC_HCI_RF_READER_B_GATE:
819 * PRESENCE_CHECK on those gates is available
820 * However, the answer to this command is taking 3 * fwi
821 * if the card is no present.
822 * Instead, we send an empty I-Frame with a very short
823 * configurable fwi ~604µs.
825 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
826 ST21NFCA_WR_XCHG_DATA, &fwi, 1, NULL);
827 case ST21NFCA_RF_READER_14443_3_A_GATE:
828 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
829 ST21NFCA_RF_READER_CMD_PRESENCE_CHECK,
838 * <= 0: driver handled the event, skb consumed
839 * 1: driver does not handle the event, please do standard processing
841 static int st21nfca_hci_event_received(struct nfc_hci_dev *hdev, u8 gate,
842 u8 event, struct sk_buff *skb)
845 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
847 pr_debug("hci event: %d\n", event);
850 case ST21NFCA_EVT_CARD_ACTIVATED:
851 if (gate == ST21NFCA_RF_CARD_F_GATE)
852 info->dep_info.curr_nfc_dep_pni = 0;
854 case ST21NFCA_EVT_CARD_DEACTIVATED:
856 case ST21NFCA_EVT_FIELD_ON:
858 case ST21NFCA_EVT_FIELD_OFF:
860 case ST21NFCA_EVT_SEND_DATA:
861 if (gate == ST21NFCA_RF_CARD_F_GATE) {
862 r = st21nfca_tm_event_send_data(hdev, skb, gate);
867 info->dep_info.curr_nfc_dep_pni = 0;
876 static struct nfc_hci_ops st21nfca_hci_ops = {
877 .open = st21nfca_hci_open,
878 .close = st21nfca_hci_close,
879 .load_session = st21nfca_hci_load_session,
880 .hci_ready = st21nfca_hci_ready,
881 .xmit = st21nfca_hci_xmit,
882 .start_poll = st21nfca_hci_start_poll,
883 .stop_poll = st21nfca_hci_stop_poll,
884 .dep_link_up = st21nfca_hci_dep_link_up,
885 .dep_link_down = st21nfca_hci_dep_link_down,
886 .target_from_gate = st21nfca_hci_target_from_gate,
887 .complete_target_discovered = st21nfca_hci_complete_target_discovered,
888 .im_transceive = st21nfca_hci_im_transceive,
889 .tm_send = st21nfca_hci_tm_send,
890 .check_presence = st21nfca_hci_check_presence,
891 .event_received = st21nfca_hci_event_received,
894 int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
895 char *llc_name, int phy_headroom, int phy_tailroom,
896 int phy_payload, struct nfc_hci_dev **hdev)
898 struct st21nfca_hci_info *info;
902 struct nfc_hci_init_data init_data;
903 unsigned long quirks = 0;
905 info = kzalloc(sizeof(struct st21nfca_hci_info), GFP_KERNEL);
911 info->phy_ops = phy_ops;
912 info->phy_id = phy_id;
913 info->state = ST21NFCA_ST_COLD;
914 mutex_init(&info->info_lock);
916 init_data.gate_count = ARRAY_SIZE(st21nfca_gates);
918 memcpy(init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
921 * Session id must include the driver name + i2c bus addr
922 * persistent info to discriminate 2 identical chips
924 dev_num = find_first_zero_bit(dev_mask, ST21NFCA_NUM_DEVICES);
926 if (dev_num >= ST21NFCA_NUM_DEVICES)
929 set_bit(dev_num, dev_mask);
931 scnprintf(init_data.session_id, sizeof(init_data.session_id), "%s%2x",
934 protocols = NFC_PROTO_JEWEL_MASK |
935 NFC_PROTO_MIFARE_MASK |
936 NFC_PROTO_FELICA_MASK |
937 NFC_PROTO_ISO14443_MASK |
938 NFC_PROTO_ISO14443_B_MASK |
939 NFC_PROTO_ISO15693_MASK |
940 NFC_PROTO_NFC_DEP_MASK;
942 set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks);
945 nfc_hci_allocate_device(&st21nfca_hci_ops, &init_data, quirks,
947 phy_headroom + ST21NFCA_CMDS_HEADROOM,
948 phy_tailroom, phy_payload);
951 pr_err("Cannot allocate nfc hdev.\n");
956 nfc_hci_set_clientdata(info->hdev, info);
958 r = nfc_hci_register_device(info->hdev);
963 st21nfca_dep_init(info->hdev);
968 nfc_hci_free_device(info->hdev);
975 EXPORT_SYMBOL(st21nfca_hci_probe);
977 void st21nfca_hci_remove(struct nfc_hci_dev *hdev)
979 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
981 st21nfca_dep_deinit(hdev);
982 nfc_hci_unregister_device(hdev);
983 nfc_hci_free_device(hdev);
986 EXPORT_SYMBOL(st21nfca_hci_remove);
988 MODULE_LICENSE("GPL");
989 MODULE_DESCRIPTION(DRIVER_DESC);