Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

NFC: st21nfca: Adding support for secure element

st21nfca has 1 physical SWP line and can support up to 2 secure elements
(UICC & eSE) thanks to an external switch managed with a gpio.

The platform integrator needs to specify thanks to 2 initialization
properties, uicc-present and ese-present, if it is suppose to have uicc
and/or ese. Of course if the platform does not have an external switch,
only one kind of secure element can be supported. Those parameters are
under platform integrator responsibilities.

During initialization, the white_list will be set according to those
parameters.

The discovery_se function will assume a secure element is physically
present according to uicc-present and ese-present values and will add it
to the secure element list. On ese activation, the atr is retrieved to
calculate a command exchange timeout based on the first atr(TB) value.

The se_io will allow to transfer data over SWP. 2 kind of events may appear
after a data is sent over:
- ST21NFCA_EVT_TRANSMIT_DATA when receiving an apdu answer
- ST21NFCA_EVT_WTX_REQUEST when the secure element needs more time than
expected to compute a command. If this timeout expired, a first recovery
tentative consist to send a simple software reset proprietary command.
If this tentative still fail, a second recovery tentative consist to send
a hardware reset proprietary command.
This function is only relevant for eSE like secure element.

This patch also change the way a pipe is referenced. There can be
different pipe connected to the same gate with different host destination
(ex: CONNECTIVITY). In order to keep host information every pipe are
reference with a tuple (gate, host). In order to reduce changes, we are
keeping unchanged the way a gate is addressed on the Terminal Host.
However, this is working because we consider the apdu reader gate is only
present on the eSE slot also the connectivity gate cannot give a reliable
value; it will give the latest stored pipe value.

Signed-off-by: Christophe Ricard <christophe-h.ricard@st.com>
Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>

authored by

Christophe Ricard and committed by
Samuel Ortiz
2130fb97 ec14b6c9

+608 -21
+1 -1
drivers/nfc/st21nfca/Makefile
··· 2 2 # Makefile for ST21NFCA HCI based NFC driver 3 3 # 4 4 5 - st21nfca_hci-objs = st21nfca.o st21nfca_dep.o 5 + st21nfca_hci-objs = st21nfca.o st21nfca_dep.o st21nfca_se.o 6 6 obj-$(CONFIG_NFC_ST21NFCA) += st21nfca_hci.o 7 7 8 8 st21nfca_i2c-objs = i2c.o
+15 -2
drivers/nfc/st21nfca/i2c.c
··· 74 74 unsigned int gpio_ena; 75 75 unsigned int irq_polarity; 76 76 77 + struct st21nfca_se_status se_status; 78 + 77 79 struct sk_buff *pending_skb; 78 80 int current_read_len; 79 81 /* ··· 539 537 540 538 phy->irq_polarity = irq_get_trigger_type(client->irq); 541 539 540 + phy->se_status.is_ese_present = 541 + of_property_read_bool(pp, "ese-present"); 542 + phy->se_status.is_uicc_present = 543 + of_property_read_bool(pp, "uicc-present"); 544 + 542 545 return 0; 543 546 } 544 547 #else ··· 577 570 return r; 578 571 } 579 572 } 573 + 574 + phy->se_status.is_ese_present = pdata->is_ese_present; 575 + phy->se_status.is_uicc_present = pdata->is_uicc_present; 580 576 581 577 return 0; 582 578 } ··· 648 638 } 649 639 650 640 return st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME, 651 - ST21NFCA_FRAME_HEADROOM, ST21NFCA_FRAME_TAILROOM, 652 - ST21NFCA_HCI_LLC_MAX_PAYLOAD, &phy->hdev); 641 + ST21NFCA_FRAME_HEADROOM, 642 + ST21NFCA_FRAME_TAILROOM, 643 + ST21NFCA_HCI_LLC_MAX_PAYLOAD, 644 + &phy->hdev, 645 + &phy->se_status); 653 646 } 654 647 655 648 static int st21nfca_hci_i2c_remove(struct i2c_client *client)
+121 -13
drivers/nfc/st21nfca/st21nfca.c
··· 23 23 24 24 #include "st21nfca.h" 25 25 #include "st21nfca_dep.h" 26 + #include "st21nfca_se.h" 26 27 27 28 #define DRIVER_DESC "HCI NFC driver for ST21NFCA" 28 29 ··· 63 62 #define ST21NFCA_RF_CARD_F_DATARATE 0x08 64 63 #define ST21NFCA_RF_CARD_F_DATARATE_212_424 0x01 65 64 66 - #define ST21NFCA_DEVICE_MGNT_GATE 0x01 67 65 #define ST21NFCA_DEVICE_MGNT_PIPE 0x02 68 66 69 67 #define ST21NFCA_DM_GETINFO 0x13 ··· 78 78 79 79 #define ST21NFCA_NFC_MODE 0x03 /* NFC_MODE parameter*/ 80 80 81 + #define ST21NFCA_EVT_HOT_PLUG 0x03 82 + #define ST21NFCA_EVT_HOT_PLUG_IS_INHIBITED(x) (x->data[0] & 0x80) 83 + 84 + #define ST21NFCA_SE_TO_PIPES 2000 85 + 81 86 static DECLARE_BITMAP(dev_mask, ST21NFCA_NUM_DEVICES); 82 87 83 88 static struct nfc_hci_gate st21nfca_gates[] = { ··· 97 92 {ST21NFCA_RF_READER_14443_3_A_GATE, NFC_HCI_INVALID_PIPE}, 98 93 {ST21NFCA_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE}, 99 94 {ST21NFCA_RF_CARD_F_GATE, NFC_HCI_INVALID_PIPE}, 95 + 96 + /* Secure element pipes are created by secure element host */ 97 + {ST21NFCA_CONNECTIVITY_GATE, NFC_HCI_DO_NOT_CREATE_PIPE}, 98 + {ST21NFCA_APDU_READER_GATE, NFC_HCI_DO_NOT_CREATE_PIPE}, 100 99 }; 101 100 102 101 struct st21nfca_pipe_info { ··· 145 136 * Pipe can be closed and need to be open. 146 137 */ 147 138 r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID, 148 - ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE); 139 + ST21NFCA_DEVICE_MGNT_GATE, 140 + ST21NFCA_DEVICE_MGNT_PIPE); 149 141 if (r < 0) 150 142 goto free_info; 151 143 ··· 177 167 * - destination gid (1byte) 178 168 */ 179 169 info = (struct st21nfca_pipe_info *) skb_pipe_info->data; 170 + if (info->dst_gate_id == ST21NFCA_APDU_READER_GATE && 171 + info->src_host_id != ST21NFCA_ESE_HOST_ID) { 172 + pr_err("Unexpected apdu_reader pipe on host %x\n", 173 + info->src_host_id); 174 + continue; 175 + } 176 + 180 177 for (j = 0; (j < ARRAY_SIZE(st21nfca_gates)) && 181 - (st21nfca_gates[j].gate != info->dst_gate_id); 182 - j++) 178 + (st21nfca_gates[j].gate != info->dst_gate_id) ; j++) 183 179 ; 184 180 185 181 if (j < ARRAY_SIZE(st21nfca_gates) && 186 182 st21nfca_gates[j].gate == info->dst_gate_id && 187 183 ST21NFCA_DM_IS_PIPE_OPEN(info->pipe_state)) { 188 184 st21nfca_gates[j].pipe = pipe_info[2]; 185 + 189 186 hdev->gate2pipe[st21nfca_gates[j].gate] = 190 - st21nfca_gates[j].pipe; 187 + st21nfca_gates[j].pipe; 188 + hdev->pipes[st21nfca_gates[j].pipe].gate = 189 + st21nfca_gates[j].gate; 190 + hdev->pipes[st21nfca_gates[j].pipe].dest_host = 191 + info->src_host_id; 191 192 } 192 193 } 193 194 ··· 208 187 */ 209 188 if (skb_pipe_list->len + 3 < ARRAY_SIZE(st21nfca_gates)) { 210 189 for (i = skb_pipe_list->len + 3; 211 - i < ARRAY_SIZE(st21nfca_gates); i++) { 190 + i < ARRAY_SIZE(st21nfca_gates) - 2; i++) { 212 191 r = nfc_hci_connect_gate(hdev, 213 192 NFC_HCI_HOST_CONTROLLER_ID, 214 193 st21nfca_gates[i].gate, ··· 265 244 266 245 static int st21nfca_hci_ready(struct nfc_hci_dev *hdev) 267 246 { 247 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 268 248 struct sk_buff *skb; 269 249 270 250 u8 param; 251 + u8 white_list[2]; 252 + int wl_size = 0; 271 253 int r; 272 254 273 - param = NFC_HCI_UICC_HOST_ID; 274 - r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE, 275 - NFC_HCI_ADMIN_WHITELIST, &param, 1); 276 - if (r < 0) 277 - return r; 255 + if (info->se_status->is_ese_present && 256 + info->se_status->is_uicc_present) { 257 + white_list[wl_size++] = NFC_HCI_UICC_HOST_ID; 258 + white_list[wl_size++] = ST21NFCA_ESE_HOST_ID; 259 + } else if (!info->se_status->is_ese_present && 260 + info->se_status->is_uicc_present) { 261 + white_list[wl_size++] = NFC_HCI_UICC_HOST_ID; 262 + } else if (info->se_status->is_ese_present && 263 + !info->se_status->is_uicc_present) { 264 + white_list[wl_size++] = ST21NFCA_ESE_HOST_ID; 265 + } 266 + 267 + if (wl_size) { 268 + r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE, 269 + NFC_HCI_ADMIN_WHITELIST, 270 + (u8 *) &white_list, wl_size); 271 + if (r < 0) 272 + return r; 273 + } 278 274 279 275 /* Set NFC_MODE in device management gate to enable */ 280 276 r = nfc_hci_get_param(hdev, ST21NFCA_DEVICE_MGNT_GATE, ··· 859 821 } 860 822 } 861 823 824 + static void st21nfca_hci_cmd_received(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd, 825 + struct sk_buff *skb) 826 + { 827 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 828 + u8 gate = hdev->pipes[pipe].gate; 829 + 830 + pr_debug("cmd: %x\n", cmd); 831 + 832 + switch (cmd) { 833 + case NFC_HCI_ANY_OPEN_PIPE: 834 + if (gate != ST21NFCA_APDU_READER_GATE && 835 + hdev->pipes[pipe].dest_host != NFC_HCI_UICC_HOST_ID) 836 + info->se_info.count_pipes++; 837 + 838 + if (info->se_info.count_pipes == info->se_info.expected_pipes) { 839 + del_timer_sync(&info->se_info.se_active_timer); 840 + info->se_info.se_active = false; 841 + info->se_info.count_pipes = 0; 842 + complete(&info->se_info.req_completion); 843 + } 844 + break; 845 + } 846 + } 847 + 848 + static int st21nfca_admin_event_received(struct nfc_hci_dev *hdev, u8 event, 849 + struct sk_buff *skb) 850 + { 851 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 852 + 853 + pr_debug("admin event: %x\n", event); 854 + 855 + switch (event) { 856 + case ST21NFCA_EVT_HOT_PLUG: 857 + if (info->se_info.se_active) { 858 + if (!ST21NFCA_EVT_HOT_PLUG_IS_INHIBITED(skb)) { 859 + del_timer_sync(&info->se_info.se_active_timer); 860 + info->se_info.se_active = false; 861 + complete(&info->se_info.req_completion); 862 + } else { 863 + mod_timer(&info->se_info.se_active_timer, 864 + jiffies + 865 + msecs_to_jiffies(ST21NFCA_SE_TO_PIPES)); 866 + } 867 + } 868 + break; 869 + } 870 + kfree_skb(skb); 871 + return 0; 872 + } 873 + 862 874 /* 863 875 * Returns: 864 876 * <= 0: driver handled the event, skb consumed 865 877 * 1: driver does not handle the event, please do standard processing 866 878 */ 867 - static int st21nfca_hci_event_received(struct nfc_hci_dev *hdev, u8 gate, 879 + static int st21nfca_hci_event_received(struct nfc_hci_dev *hdev, u8 pipe, 868 880 u8 event, struct sk_buff *skb) 869 881 { 882 + u8 gate = hdev->pipes[pipe].gate; 883 + u8 host = hdev->pipes[pipe].dest_host; 884 + 870 885 pr_debug("hci event: %d gate: %x\n", event, gate); 871 886 872 887 switch (gate) { 888 + case NFC_HCI_ADMIN_GATE: 889 + return st21nfca_admin_event_received(hdev, event, skb); 873 890 case ST21NFCA_RF_CARD_F_GATE: 874 891 return st21nfca_dep_event_received(hdev, event, skb); 892 + case ST21NFCA_CONNECTIVITY_GATE: 893 + return st21nfca_connectivity_event_received(hdev, host, 894 + event, skb); 895 + case ST21NFCA_APDU_READER_GATE: 896 + return st21nfca_apdu_reader_event_received(hdev, event, skb); 875 897 default: 876 898 return 1; 877 899 } ··· 953 855 .tm_send = st21nfca_hci_tm_send, 954 856 .check_presence = st21nfca_hci_check_presence, 955 857 .event_received = st21nfca_hci_event_received, 858 + .cmd_received = st21nfca_hci_cmd_received, 859 + .discover_se = st21nfca_hci_discover_se, 860 + .enable_se = st21nfca_hci_enable_se, 861 + .disable_se = st21nfca_hci_disable_se, 862 + .se_io = st21nfca_hci_se_io, 956 863 }; 957 864 958 865 int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, 959 866 char *llc_name, int phy_headroom, int phy_tailroom, 960 - int phy_payload, struct nfc_hci_dev **hdev) 867 + int phy_payload, struct nfc_hci_dev **hdev, 868 + struct st21nfca_se_status *se_status) 961 869 { 962 870 struct st21nfca_hci_info *info; 963 871 int r = 0; ··· 1023 919 goto err_alloc_hdev; 1024 920 } 1025 921 922 + info->se_status = se_status; 923 + 1026 924 nfc_hci_set_clientdata(info->hdev, info); 1027 925 1028 926 r = nfc_hci_register_device(info->hdev); ··· 1033 927 1034 928 *hdev = info->hdev; 1035 929 st21nfca_dep_init(info->hdev); 930 + st21nfca_se_init(info->hdev); 1036 931 1037 932 return 0; 1038 933 ··· 1052 945 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 1053 946 1054 947 st21nfca_dep_deinit(hdev); 948 + st21nfca_se_deinit(hdev); 1055 949 nfc_hci_unregister_device(hdev); 1056 950 nfc_hci_free_device(hdev); 1057 951 kfree(info);
+16 -5
drivers/nfc/st21nfca/st21nfca.h
··· 20 20 #include <net/nfc/hci.h> 21 21 22 22 #include "st21nfca_dep.h" 23 + #include "st21nfca_se.h" 23 24 24 25 #define HCI_MODE 0 25 26 ··· 52 51 53 52 #define ST21NFCA_NUM_DEVICES 256 54 53 54 + struct st21nfca_se_status { 55 + bool is_ese_present; 56 + bool is_uicc_present; 57 + }; 58 + 55 59 int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, 56 60 char *llc_name, int phy_headroom, int phy_tailroom, 57 - int phy_payload, struct nfc_hci_dev **hdev); 61 + int phy_payload, struct nfc_hci_dev **hdev, 62 + struct st21nfca_se_status *se_status); 58 63 void st21nfca_hci_remove(struct nfc_hci_dev *hdev); 59 64 60 65 enum st21nfca_state { ··· 73 66 void *phy_id; 74 67 75 68 struct nfc_hci_dev *hdev; 69 + struct st21nfca_se_status *se_status; 76 70 77 71 enum st21nfca_state state; 78 72 ··· 84 76 void *async_cb_context; 85 77 86 78 struct st21nfca_dep_info dep_info; 79 + struct st21nfca_se_info se_info; 87 80 }; 88 81 89 82 /* Reader RF commands */ 90 - #define ST21NFCA_WR_XCHG_DATA 0x10 83 + #define ST21NFCA_WR_XCHG_DATA 0x10 91 84 92 - #define ST21NFCA_RF_READER_F_GATE 0x14 93 - 94 - #define ST21NFCA_RF_CARD_F_GATE 0x24 85 + #define ST21NFCA_DEVICE_MGNT_GATE 0x01 86 + #define ST21NFCA_RF_READER_F_GATE 0x14 87 + #define ST21NFCA_RF_CARD_F_GATE 0x24 88 + #define ST21NFCA_APDU_READER_GATE 0xf0 89 + #define ST21NFCA_CONNECTIVITY_GATE 0x41 95 90 96 91 #endif /* __LOCAL_ST21NFCA_H_ */
+390
drivers/nfc/st21nfca/st21nfca_se.c
··· 1 + /* 2 + * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved. 3 + * 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. 7 + * 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. 12 + * 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/>. 15 + */ 16 + 17 + #include <net/nfc/hci.h> 18 + 19 + #include "st21nfca.h" 20 + #include "st21nfca_se.h" 21 + 22 + #define ST21NFCA_EVT_UICC_ACTIVATE 0x10 23 + #define ST21NFCA_EVT_UICC_DEACTIVATE 0x13 24 + #define ST21NFCA_EVT_SE_HARD_RESET 0x20 25 + #define ST21NFCA_EVT_SE_SOFT_RESET 0x11 26 + #define ST21NFCA_EVT_SE_END_OF_APDU_TRANSFER 0x21 27 + #define ST21NFCA_EVT_SE_ACTIVATE 0x22 28 + #define ST21NFCA_EVT_SE_DEACTIVATE 0x23 29 + 30 + #define ST21NFCA_EVT_TRANSMIT_DATA 0x10 31 + #define ST21NFCA_EVT_WTX_REQUEST 0x11 32 + 33 + #define ST21NFCA_EVT_CONNECTIVITY 0x10 34 + #define ST21NFCA_EVT_TRANSACTION 0x12 35 + 36 + #define ST21NFCA_ESE_HOST_ID 0xc0 37 + 38 + #define ST21NFCA_SE_TO_HOT_PLUG 1000 39 + /* Connectivity pipe only */ 40 + #define ST21NFCA_SE_COUNT_PIPE_UICC 0x01 41 + /* Connectivity + APDU Reader pipe */ 42 + #define ST21NFCA_SE_COUNT_PIPE_EMBEDDED 0x02 43 + 44 + #define ST21NFCA_SE_MODE_OFF 0x00 45 + #define ST21NFCA_SE_MODE_ON 0x01 46 + 47 + #define ST21NFCA_PARAM_ATR 0x01 48 + #define ST21NFCA_ATR_DEFAULT_BWI 0x04 49 + 50 + /* 51 + * WT = 2^BWI/10[s], convert into msecs and add a secure 52 + * room by increasing by 2 this timeout 53 + */ 54 + #define ST21NFCA_BWI_TO_TIMEOUT(x) ((1 << x) * 200) 55 + #define ST21NFCA_ATR_GET_Y_FROM_TD(x) (x >> 4) 56 + 57 + /* If TA is present bit 0 is set */ 58 + #define ST21NFCA_ATR_TA_PRESENT(x) (x & 0x01) 59 + /* If TB is present bit 1 is set */ 60 + #define ST21NFCA_ATR_TB_PRESENT(x) (x & 0x02) 61 + 62 + static u8 st21nfca_se_get_bwi(struct nfc_hci_dev *hdev) 63 + { 64 + int i; 65 + u8 td; 66 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 67 + 68 + /* Bits 8 to 5 of the first TB for T=1 encode BWI from zero to nine */ 69 + for (i = 1; i < ST21NFCA_ESE_MAX_LENGTH; i++) { 70 + td = ST21NFCA_ATR_GET_Y_FROM_TD(info->se_info.atr[i]); 71 + if (ST21NFCA_ATR_TA_PRESENT(td)) 72 + i++; 73 + if (ST21NFCA_ATR_TB_PRESENT(td)) { 74 + i++; 75 + return info->se_info.atr[i] >> 4; 76 + } 77 + } 78 + return ST21NFCA_ATR_DEFAULT_BWI; 79 + } 80 + 81 + static void st21nfca_se_get_atr(struct nfc_hci_dev *hdev) 82 + { 83 + int r; 84 + struct sk_buff *skb; 85 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 86 + 87 + r = nfc_hci_get_param(hdev, ST21NFCA_APDU_READER_GATE, 88 + ST21NFCA_PARAM_ATR, &skb); 89 + if (r < 0) 90 + return; 91 + 92 + if (skb->len <= ST21NFCA_ESE_MAX_LENGTH) { 93 + memcpy(info->se_info.atr, skb->data, skb->len); 94 + info->se_info.wt_timeout = 95 + ST21NFCA_BWI_TO_TIMEOUT(st21nfca_se_get_bwi(hdev)); 96 + } 97 + kfree_skb(skb); 98 + } 99 + 100 + static int st21nfca_hci_control_se(struct nfc_hci_dev *hdev, u32 se_idx, 101 + u8 state) 102 + { 103 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 104 + int r; 105 + struct sk_buff *sk_host_list; 106 + u8 se_event, host_id; 107 + 108 + switch (se_idx) { 109 + case NFC_HCI_UICC_HOST_ID: 110 + se_event = (state == ST21NFCA_SE_MODE_ON ? 111 + ST21NFCA_EVT_UICC_ACTIVATE : 112 + ST21NFCA_EVT_UICC_DEACTIVATE); 113 + 114 + info->se_info.count_pipes = 0; 115 + info->se_info.expected_pipes = ST21NFCA_SE_COUNT_PIPE_UICC; 116 + break; 117 + case ST21NFCA_ESE_HOST_ID: 118 + se_event = (state == ST21NFCA_SE_MODE_ON ? 119 + ST21NFCA_EVT_SE_ACTIVATE : 120 + ST21NFCA_EVT_SE_DEACTIVATE); 121 + 122 + info->se_info.count_pipes = 0; 123 + info->se_info.expected_pipes = ST21NFCA_SE_COUNT_PIPE_EMBEDDED; 124 + break; 125 + default: 126 + return -EINVAL; 127 + } 128 + 129 + /* 130 + * Wait for an EVT_HOT_PLUG in order to 131 + * retrieve a relevant host list. 132 + */ 133 + reinit_completion(&info->se_info.req_completion); 134 + r = nfc_hci_send_event(hdev, ST21NFCA_DEVICE_MGNT_GATE, se_event, 135 + NULL, 0); 136 + if (r < 0) 137 + return r; 138 + 139 + mod_timer(&info->se_info.se_active_timer, jiffies + 140 + msecs_to_jiffies(ST21NFCA_SE_TO_HOT_PLUG)); 141 + info->se_info.se_active = true; 142 + 143 + /* Ignore return value and check in any case the host_list */ 144 + wait_for_completion_interruptible(&info->se_info.req_completion); 145 + 146 + r = nfc_hci_get_param(hdev, NFC_HCI_ADMIN_GATE, 147 + NFC_HCI_ADMIN_HOST_LIST, 148 + &sk_host_list); 149 + if (r < 0) 150 + return r; 151 + 152 + host_id = sk_host_list->data[sk_host_list->len - 1]; 153 + kfree_skb(sk_host_list); 154 + 155 + if (state == ST21NFCA_SE_MODE_ON && host_id == se_idx) 156 + return se_idx; 157 + else if (state == ST21NFCA_SE_MODE_OFF && host_id != se_idx) 158 + return se_idx; 159 + 160 + return -1; 161 + } 162 + 163 + int st21nfca_hci_discover_se(struct nfc_hci_dev *hdev) 164 + { 165 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 166 + int se_count = 0; 167 + 168 + if (info->se_status->is_uicc_present) { 169 + nfc_add_se(hdev->ndev, NFC_HCI_UICC_HOST_ID, NFC_SE_UICC); 170 + se_count++; 171 + } 172 + 173 + if (info->se_status->is_ese_present) { 174 + nfc_add_se(hdev->ndev, ST21NFCA_ESE_HOST_ID, NFC_SE_EMBEDDED); 175 + se_count++; 176 + } 177 + 178 + return !se_count; 179 + } 180 + EXPORT_SYMBOL(st21nfca_hci_discover_se); 181 + 182 + int st21nfca_hci_enable_se(struct nfc_hci_dev *hdev, u32 se_idx) 183 + { 184 + int r; 185 + 186 + /* 187 + * According to upper layer, se_idx == NFC_SE_UICC when 188 + * info->se_status->is_uicc_enable is true should never happen. 189 + * Same for eSE. 190 + */ 191 + r = st21nfca_hci_control_se(hdev, se_idx, ST21NFCA_SE_MODE_ON); 192 + 193 + if (r == ST21NFCA_ESE_HOST_ID) { 194 + st21nfca_se_get_atr(hdev); 195 + r = nfc_hci_send_event(hdev, ST21NFCA_APDU_READER_GATE, 196 + ST21NFCA_EVT_SE_SOFT_RESET, NULL, 0); 197 + if (r < 0) 198 + return r; 199 + } else if (r < 0) { 200 + /* 201 + * The activation tentative failed, the secure element 202 + * is not connected. Remove from the list. 203 + */ 204 + nfc_remove_se(hdev->ndev, se_idx); 205 + return r; 206 + } 207 + 208 + return 0; 209 + } 210 + EXPORT_SYMBOL(st21nfca_hci_enable_se); 211 + 212 + int st21nfca_hci_disable_se(struct nfc_hci_dev *hdev, u32 se_idx) 213 + { 214 + int r; 215 + 216 + /* 217 + * According to upper layer, se_idx == NFC_SE_UICC when 218 + * info->se_status->is_uicc_enable is true should never happen 219 + * Same for eSE. 220 + */ 221 + r = st21nfca_hci_control_se(hdev, se_idx, ST21NFCA_SE_MODE_OFF); 222 + if (r < 0) 223 + return r; 224 + 225 + return 0; 226 + } 227 + EXPORT_SYMBOL(st21nfca_hci_disable_se); 228 + 229 + int st21nfca_hci_se_io(struct nfc_hci_dev *hdev, u32 se_idx, 230 + u8 *apdu, size_t apdu_length, 231 + se_io_cb_t cb, void *cb_context) 232 + { 233 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 234 + 235 + pr_debug("se_io %x\n", se_idx); 236 + 237 + switch (se_idx) { 238 + case ST21NFCA_ESE_HOST_ID: 239 + info->se_info.cb = cb; 240 + info->se_info.cb_context = cb_context; 241 + mod_timer(&info->se_info.bwi_timer, jiffies + 242 + msecs_to_jiffies(info->se_info.wt_timeout)); 243 + info->se_info.bwi_active = true; 244 + return nfc_hci_send_event(hdev, ST21NFCA_APDU_READER_GATE, 245 + ST21NFCA_EVT_TRANSMIT_DATA, 246 + apdu, apdu_length); 247 + default: 248 + return -ENODEV; 249 + } 250 + } 251 + EXPORT_SYMBOL(st21nfca_hci_se_io); 252 + 253 + static void st21nfca_se_wt_timeout(unsigned long data) 254 + { 255 + /* 256 + * No answer from the secure element 257 + * within the defined timeout. 258 + * Let's send a reset request as recovery procedure. 259 + * According to the situation, we first try to send a software reset 260 + * to the secure element. If the next command is still not 261 + * answering in time, we send to the CLF a secure element hardware 262 + * reset request. 263 + */ 264 + /* hardware reset managed through VCC_UICC_OUT power supply */ 265 + u8 param = 0x01; 266 + struct st21nfca_hci_info *info = (struct st21nfca_hci_info *) data; 267 + 268 + pr_debug("\n"); 269 + 270 + info->se_info.bwi_active = false; 271 + 272 + if (!info->se_info.xch_error) { 273 + info->se_info.xch_error = true; 274 + nfc_hci_send_event(info->hdev, ST21NFCA_APDU_READER_GATE, 275 + ST21NFCA_EVT_SE_SOFT_RESET, NULL, 0); 276 + } else { 277 + info->se_info.xch_error = false; 278 + nfc_hci_send_event(info->hdev, ST21NFCA_DEVICE_MGNT_GATE, 279 + ST21NFCA_EVT_SE_HARD_RESET, &param, 1); 280 + } 281 + info->se_info.cb(info->se_info.cb_context, NULL, 0, -ETIME); 282 + } 283 + 284 + static void st21nfca_se_activation_timeout(unsigned long data) 285 + { 286 + struct st21nfca_hci_info *info = (struct st21nfca_hci_info *) data; 287 + 288 + pr_debug("\n"); 289 + 290 + info->se_info.se_active = false; 291 + 292 + complete(&info->se_info.req_completion); 293 + } 294 + 295 + /* 296 + * Returns: 297 + * <= 0: driver handled the event, skb consumed 298 + * 1: driver does not handle the event, please do standard processing 299 + */ 300 + int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host, 301 + u8 event, struct sk_buff *skb) 302 + { 303 + int r = 0; 304 + 305 + pr_debug("connectivity gate event: %x\n", event); 306 + 307 + switch (event) { 308 + case ST21NFCA_EVT_CONNECTIVITY: 309 + break; 310 + case ST21NFCA_EVT_TRANSACTION: 311 + break; 312 + default: 313 + return 1; 314 + } 315 + kfree_skb(skb); 316 + return r; 317 + } 318 + EXPORT_SYMBOL(st21nfca_connectivity_event_received); 319 + 320 + int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev, 321 + u8 event, struct sk_buff *skb) 322 + { 323 + int r = 0; 324 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 325 + 326 + pr_debug("apdu reader gate event: %x\n", event); 327 + 328 + switch (event) { 329 + case ST21NFCA_EVT_TRANSMIT_DATA: 330 + del_timer_sync(&info->se_info.bwi_timer); 331 + info->se_info.bwi_active = false; 332 + r = nfc_hci_send_event(hdev, ST21NFCA_DEVICE_MGNT_GATE, 333 + ST21NFCA_EVT_SE_END_OF_APDU_TRANSFER, NULL, 0); 334 + if (r < 0) 335 + goto exit; 336 + 337 + info->se_info.cb(info->se_info.cb_context, 338 + skb->data, skb->len, 0); 339 + break; 340 + case ST21NFCA_EVT_WTX_REQUEST: 341 + mod_timer(&info->se_info.bwi_timer, jiffies + 342 + msecs_to_jiffies(info->se_info.wt_timeout)); 343 + break; 344 + } 345 + 346 + exit: 347 + kfree_skb(skb); 348 + return r; 349 + } 350 + EXPORT_SYMBOL(st21nfca_apdu_reader_event_received); 351 + 352 + void st21nfca_se_init(struct nfc_hci_dev *hdev) 353 + { 354 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 355 + 356 + init_completion(&info->se_info.req_completion); 357 + /* initialize timers */ 358 + init_timer(&info->se_info.bwi_timer); 359 + info->se_info.bwi_timer.data = (unsigned long)info; 360 + info->se_info.bwi_timer.function = st21nfca_se_wt_timeout; 361 + info->se_info.bwi_active = false; 362 + 363 + init_timer(&info->se_info.se_active_timer); 364 + info->se_info.se_active_timer.data = (unsigned long)info; 365 + info->se_info.se_active_timer.function = st21nfca_se_activation_timeout; 366 + info->se_info.se_active = false; 367 + 368 + info->se_info.count_pipes = 0; 369 + info->se_info.expected_pipes = 0; 370 + 371 + info->se_info.xch_error = false; 372 + 373 + info->se_info.wt_timeout = 374 + ST21NFCA_BWI_TO_TIMEOUT(ST21NFCA_ATR_DEFAULT_BWI); 375 + } 376 + EXPORT_SYMBOL(st21nfca_se_init); 377 + 378 + void st21nfca_se_deinit(struct nfc_hci_dev *hdev) 379 + { 380 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 381 + 382 + if (info->se_info.bwi_active) 383 + del_timer_sync(&info->se_info.bwi_timer); 384 + if (info->se_info.se_active) 385 + del_timer_sync(&info->se_info.se_active_timer); 386 + 387 + info->se_info.bwi_active = false; 388 + info->se_info.se_active = false; 389 + } 390 + EXPORT_SYMBOL(st21nfca_se_deinit);
+63
drivers/nfc/st21nfca/st21nfca_se.h
··· 1 + /* 2 + * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved. 3 + * 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. 7 + * 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. 12 + * 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/>. 15 + */ 16 + 17 + #ifndef __ST21NFCA_SE_H 18 + #define __ST21NFCA_SE_H 19 + 20 + #include <linux/skbuff.h> 21 + #include <linux/workqueue.h> 22 + 23 + /* 24 + * ref ISO7816-3 chap 8.1. the initial character TS is followed by a 25 + * sequence of at most 32 characters. 26 + */ 27 + #define ST21NFCA_ESE_MAX_LENGTH 33 28 + #define ST21NFCA_ESE_HOST_ID 0xc0 29 + 30 + struct st21nfca_se_info { 31 + u8 atr[ST21NFCA_ESE_MAX_LENGTH]; 32 + struct completion req_completion; 33 + 34 + struct timer_list bwi_timer; 35 + int wt_timeout; /* in msecs */ 36 + bool bwi_active; 37 + 38 + struct timer_list se_active_timer; 39 + bool se_active; 40 + int expected_pipes; 41 + int count_pipes; 42 + 43 + bool xch_error; 44 + 45 + se_io_cb_t cb; 46 + void *cb_context; 47 + }; 48 + 49 + int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host, 50 + u8 event, struct sk_buff *skb); 51 + int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev, 52 + u8 event, struct sk_buff *skb); 53 + 54 + int st21nfca_hci_discover_se(struct nfc_hci_dev *hdev); 55 + int st21nfca_hci_enable_se(struct nfc_hci_dev *hdev, u32 se_idx); 56 + int st21nfca_hci_disable_se(struct nfc_hci_dev *hdev, u32 se_idx); 57 + int st21nfca_hci_se_io(struct nfc_hci_dev *hdev, u32 se_idx, 58 + u8 *apdu, size_t apdu_length, 59 + se_io_cb_t cb, void *cb_context); 60 + 61 + void st21nfca_se_init(struct nfc_hci_dev *hdev); 62 + void st21nfca_se_deinit(struct nfc_hci_dev *hdev); 63 + #endif /* __ST21NFCA_SE_H */
+2
include/linux/platform_data/st21nfca.h
··· 26 26 struct st21nfca_nfc_platform_data { 27 27 unsigned int gpio_ena; 28 28 unsigned int irq_polarity; 29 + bool is_ese_present; 30 + bool is_uicc_present; 29 31 }; 30 32 31 33 #endif /* _ST21NFCA_HCI_H_ */