Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Secure Element driver for STMicroelectronics NFC NCI chip |
| 3 | * |
| 4 | * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved. |
| 5 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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/>. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/nfc.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <net/nfc/nci.h> |
| 23 | #include <net/nfc/nci_core.h> |
| 24 | |
| 25 | #include "st-nci.h" |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 26 | |
| 27 | struct st_nci_pipe_info { |
| 28 | u8 pipe_state; |
| 29 | u8 src_host_id; |
| 30 | u8 src_gate_id; |
| 31 | u8 dst_host_id; |
| 32 | u8 dst_gate_id; |
| 33 | } __packed; |
| 34 | |
| 35 | /* Hosts */ |
| 36 | #define ST_NCI_HOST_CONTROLLER_ID 0x00 |
| 37 | #define ST_NCI_TERMINAL_HOST_ID 0x01 |
| 38 | #define ST_NCI_UICC_HOST_ID 0x02 |
| 39 | #define ST_NCI_ESE_HOST_ID 0xc0 |
| 40 | |
| 41 | /* Gates */ |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 42 | #define ST_NCI_APDU_READER_GATE 0xf0 |
| 43 | #define ST_NCI_CONNECTIVITY_GATE 0x41 |
| 44 | |
| 45 | /* Pipes */ |
| 46 | #define ST_NCI_DEVICE_MGNT_PIPE 0x02 |
| 47 | |
| 48 | /* Connectivity pipe only */ |
| 49 | #define ST_NCI_SE_COUNT_PIPE_UICC 0x01 |
| 50 | /* Connectivity + APDU Reader pipe */ |
| 51 | #define ST_NCI_SE_COUNT_PIPE_EMBEDDED 0x02 |
| 52 | |
| 53 | #define ST_NCI_SE_TO_HOT_PLUG 1000 /* msecs */ |
| 54 | #define ST_NCI_SE_TO_PIPES 2000 |
| 55 | |
| 56 | #define ST_NCI_EVT_HOT_PLUG_IS_INHIBITED(x) (x->data[0] & 0x80) |
| 57 | |
| 58 | #define NCI_HCI_APDU_PARAM_ATR 0x01 |
| 59 | #define NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY 0x01 |
| 60 | #define NCI_HCI_ADMIN_PARAM_WHITELIST 0x03 |
| 61 | #define NCI_HCI_ADMIN_PARAM_HOST_LIST 0x04 |
| 62 | |
| 63 | #define ST_NCI_EVT_SE_HARD_RESET 0x20 |
| 64 | #define ST_NCI_EVT_TRANSMIT_DATA 0x10 |
Christophe Ricard | 064d004 | 2015-10-25 22:54:44 +0100 | [diff] [blame] | 65 | #define ST_NCI_EVT_WTX_REQUEST 0x11 |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 66 | #define ST_NCI_EVT_SE_SOFT_RESET 0x11 |
| 67 | #define ST_NCI_EVT_SE_END_OF_APDU_TRANSFER 0x21 |
| 68 | #define ST_NCI_EVT_HOT_PLUG 0x03 |
| 69 | |
| 70 | #define ST_NCI_SE_MODE_OFF 0x00 |
| 71 | #define ST_NCI_SE_MODE_ON 0x01 |
| 72 | |
| 73 | #define ST_NCI_EVT_CONNECTIVITY 0x10 |
| 74 | #define ST_NCI_EVT_TRANSACTION 0x12 |
| 75 | |
| 76 | #define ST_NCI_DM_GETINFO 0x13 |
| 77 | #define ST_NCI_DM_GETINFO_PIPE_LIST 0x02 |
| 78 | #define ST_NCI_DM_GETINFO_PIPE_INFO 0x01 |
| 79 | #define ST_NCI_DM_PIPE_CREATED 0x02 |
| 80 | #define ST_NCI_DM_PIPE_OPEN 0x04 |
| 81 | #define ST_NCI_DM_RF_ACTIVE 0x80 |
| 82 | #define ST_NCI_DM_DISCONNECT 0x30 |
| 83 | |
| 84 | #define ST_NCI_DM_IS_PIPE_OPEN(p) \ |
| 85 | ((p & 0x0f) == (ST_NCI_DM_PIPE_CREATED | ST_NCI_DM_PIPE_OPEN)) |
| 86 | |
| 87 | #define ST_NCI_ATR_DEFAULT_BWI 0x04 |
| 88 | |
| 89 | /* |
| 90 | * WT = 2^BWI/10[s], convert into msecs and add a secure |
| 91 | * room by increasing by 2 this timeout |
| 92 | */ |
| 93 | #define ST_NCI_BWI_TO_TIMEOUT(x) ((1 << x) * 200) |
| 94 | #define ST_NCI_ATR_GET_Y_FROM_TD(x) (x >> 4) |
| 95 | |
| 96 | /* If TA is present bit 0 is set */ |
| 97 | #define ST_NCI_ATR_TA_PRESENT(x) (x & 0x01) |
| 98 | /* If TB is present bit 1 is set */ |
| 99 | #define ST_NCI_ATR_TB_PRESENT(x) (x & 0x02) |
| 100 | |
| 101 | #define ST_NCI_NUM_DEVICES 256 |
| 102 | |
| 103 | static DECLARE_BITMAP(dev_mask, ST_NCI_NUM_DEVICES); |
| 104 | |
| 105 | /* Here are the mandatory pipe for st_nci */ |
| 106 | static struct nci_hci_gate st_nci_gates[] = { |
| 107 | {NCI_HCI_ADMIN_GATE, NCI_HCI_ADMIN_PIPE, |
| 108 | ST_NCI_HOST_CONTROLLER_ID}, |
| 109 | {NCI_HCI_LINK_MGMT_GATE, NCI_HCI_LINK_MGMT_PIPE, |
| 110 | ST_NCI_HOST_CONTROLLER_ID}, |
| 111 | {ST_NCI_DEVICE_MGNT_GATE, ST_NCI_DEVICE_MGNT_PIPE, |
| 112 | ST_NCI_HOST_CONTROLLER_ID}, |
| 113 | |
Christophe Ricard | 7e35740 | 2015-10-25 22:54:33 +0100 | [diff] [blame] | 114 | {NCI_HCI_IDENTITY_MGMT_GATE, NCI_HCI_INVALID_PIPE, |
| 115 | ST_NCI_HOST_CONTROLLER_ID}, |
| 116 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 117 | /* Secure element pipes are created by secure element host */ |
| 118 | {ST_NCI_CONNECTIVITY_GATE, NCI_HCI_DO_NOT_OPEN_PIPE, |
| 119 | ST_NCI_HOST_CONTROLLER_ID}, |
| 120 | {ST_NCI_APDU_READER_GATE, NCI_HCI_DO_NOT_OPEN_PIPE, |
| 121 | ST_NCI_HOST_CONTROLLER_ID}, |
| 122 | }; |
| 123 | |
| 124 | static u8 st_nci_se_get_bwi(struct nci_dev *ndev) |
| 125 | { |
| 126 | int i; |
| 127 | u8 td; |
| 128 | struct st_nci_info *info = nci_get_drvdata(ndev); |
| 129 | |
| 130 | /* Bits 8 to 5 of the first TB for T=1 encode BWI from zero to nine */ |
| 131 | for (i = 1; i < ST_NCI_ESE_MAX_LENGTH; i++) { |
| 132 | td = ST_NCI_ATR_GET_Y_FROM_TD(info->se_info.atr[i]); |
| 133 | if (ST_NCI_ATR_TA_PRESENT(td)) |
| 134 | i++; |
| 135 | if (ST_NCI_ATR_TB_PRESENT(td)) { |
| 136 | i++; |
| 137 | return info->se_info.atr[i] >> 4; |
| 138 | } |
| 139 | } |
| 140 | return ST_NCI_ATR_DEFAULT_BWI; |
| 141 | } |
| 142 | |
| 143 | static void st_nci_se_get_atr(struct nci_dev *ndev) |
| 144 | { |
| 145 | struct st_nci_info *info = nci_get_drvdata(ndev); |
| 146 | int r; |
| 147 | struct sk_buff *skb; |
| 148 | |
| 149 | r = nci_hci_get_param(ndev, ST_NCI_APDU_READER_GATE, |
| 150 | NCI_HCI_APDU_PARAM_ATR, &skb); |
| 151 | if (r < 0) |
| 152 | return; |
| 153 | |
| 154 | if (skb->len <= ST_NCI_ESE_MAX_LENGTH) { |
| 155 | memcpy(info->se_info.atr, skb->data, skb->len); |
| 156 | |
| 157 | info->se_info.wt_timeout = |
| 158 | ST_NCI_BWI_TO_TIMEOUT(st_nci_se_get_bwi(ndev)); |
| 159 | } |
| 160 | kfree_skb(skb); |
| 161 | } |
| 162 | |
| 163 | int st_nci_hci_load_session(struct nci_dev *ndev) |
| 164 | { |
| 165 | int i, j, r; |
| 166 | struct sk_buff *skb_pipe_list, *skb_pipe_info; |
| 167 | struct st_nci_pipe_info *dm_pipe_info; |
| 168 | u8 pipe_list[] = { ST_NCI_DM_GETINFO_PIPE_LIST, |
| 169 | ST_NCI_TERMINAL_HOST_ID}; |
| 170 | u8 pipe_info[] = { ST_NCI_DM_GETINFO_PIPE_INFO, |
| 171 | ST_NCI_TERMINAL_HOST_ID, 0}; |
| 172 | |
| 173 | /* On ST_NCI device pipes number are dynamics |
| 174 | * If pipes are already created, hci_dev_up will fail. |
| 175 | * Doing a clear all pipe is a bad idea because: |
| 176 | * - It does useless EEPROM cycling |
| 177 | * - It might cause issue for secure elements support |
| 178 | * (such as removing connectivity or APDU reader pipe) |
| 179 | * A better approach on ST_NCI is to: |
| 180 | * - get a pipe list for each host. |
| 181 | * (eg: ST_NCI_HOST_CONTROLLER_ID for now). |
| 182 | * (TODO Later on UICC HOST and eSE HOST) |
| 183 | * - get pipe information |
| 184 | * - match retrieved pipe list in st_nci_gates |
| 185 | * ST_NCI_DEVICE_MGNT_GATE is a proprietary gate |
| 186 | * with ST_NCI_DEVICE_MGNT_PIPE. |
| 187 | * Pipe can be closed and need to be open. |
| 188 | */ |
| 189 | r = nci_hci_connect_gate(ndev, ST_NCI_HOST_CONTROLLER_ID, |
| 190 | ST_NCI_DEVICE_MGNT_GATE, |
| 191 | ST_NCI_DEVICE_MGNT_PIPE); |
| 192 | if (r < 0) |
Christophe Ricard | daaf1e1 | 2015-08-14 22:33:34 +0200 | [diff] [blame] | 193 | return r; |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 194 | |
| 195 | /* Get pipe list */ |
| 196 | r = nci_hci_send_cmd(ndev, ST_NCI_DEVICE_MGNT_GATE, |
| 197 | ST_NCI_DM_GETINFO, pipe_list, sizeof(pipe_list), |
| 198 | &skb_pipe_list); |
| 199 | if (r < 0) |
Christophe Ricard | daaf1e1 | 2015-08-14 22:33:34 +0200 | [diff] [blame] | 200 | return r; |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 201 | |
| 202 | /* Complete the existing gate_pipe table */ |
| 203 | for (i = 0; i < skb_pipe_list->len; i++) { |
| 204 | pipe_info[2] = skb_pipe_list->data[i]; |
| 205 | r = nci_hci_send_cmd(ndev, ST_NCI_DEVICE_MGNT_GATE, |
| 206 | ST_NCI_DM_GETINFO, pipe_info, |
| 207 | sizeof(pipe_info), &skb_pipe_info); |
| 208 | |
| 209 | if (r) |
| 210 | continue; |
| 211 | |
| 212 | /* |
| 213 | * Match pipe ID and gate ID |
| 214 | * Output format from ST21NFC_DM_GETINFO is: |
| 215 | * - pipe state (1byte) |
| 216 | * - source hid (1byte) |
| 217 | * - source gid (1byte) |
| 218 | * - destination hid (1byte) |
| 219 | * - destination gid (1byte) |
| 220 | */ |
| 221 | dm_pipe_info = (struct st_nci_pipe_info *)skb_pipe_info->data; |
| 222 | if (dm_pipe_info->dst_gate_id == ST_NCI_APDU_READER_GATE && |
Christophe Ricard | 0209e79 | 2016-04-30 09:12:42 +0200 | [diff] [blame] | 223 | dm_pipe_info->src_host_id == ST_NCI_UICC_HOST_ID) { |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 224 | pr_err("Unexpected apdu_reader pipe on host %x\n", |
| 225 | dm_pipe_info->src_host_id); |
Christophe Ricard | daaf1e1 | 2015-08-14 22:33:34 +0200 | [diff] [blame] | 226 | kfree_skb(skb_pipe_info); |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 227 | continue; |
| 228 | } |
| 229 | |
Christophe Ricard | d3f13c5 | 2015-10-25 22:54:34 +0100 | [diff] [blame] | 230 | for (j = 3; (j < ARRAY_SIZE(st_nci_gates)) && |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 231 | (st_nci_gates[j].gate != dm_pipe_info->dst_gate_id); j++) |
| 232 | ; |
| 233 | |
| 234 | if (j < ARRAY_SIZE(st_nci_gates) && |
| 235 | st_nci_gates[j].gate == dm_pipe_info->dst_gate_id && |
| 236 | ST_NCI_DM_IS_PIPE_OPEN(dm_pipe_info->pipe_state)) { |
Christophe Ricard | 22c84c5 | 2015-10-25 22:54:30 +0100 | [diff] [blame] | 237 | ndev->hci_dev->init_data.gates[j].pipe = pipe_info[2]; |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 238 | |
| 239 | ndev->hci_dev->gate2pipe[st_nci_gates[j].gate] = |
Christophe Ricard | 22c84c5 | 2015-10-25 22:54:30 +0100 | [diff] [blame] | 240 | pipe_info[2]; |
| 241 | ndev->hci_dev->pipes[pipe_info[2]].gate = |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 242 | st_nci_gates[j].gate; |
Christophe Ricard | 22c84c5 | 2015-10-25 22:54:30 +0100 | [diff] [blame] | 243 | ndev->hci_dev->pipes[pipe_info[2]].host = |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 244 | dm_pipe_info->src_host_id; |
| 245 | } |
Christophe Ricard | daaf1e1 | 2015-08-14 22:33:34 +0200 | [diff] [blame] | 246 | kfree_skb(skb_pipe_info); |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 247 | } |
| 248 | |
Christophe Ricard | 9dfe29f | 2015-10-25 22:54:28 +0100 | [diff] [blame] | 249 | /* |
| 250 | * 3 gates have a well known pipe ID. Only NCI_HCI_LINK_MGMT_GATE |
| 251 | * is not yet open at this stage. |
| 252 | */ |
| 253 | r = nci_hci_connect_gate(ndev, ST_NCI_HOST_CONTROLLER_ID, |
| 254 | NCI_HCI_LINK_MGMT_GATE, |
| 255 | NCI_HCI_LINK_MGMT_PIPE); |
| 256 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 257 | kfree_skb(skb_pipe_list); |
| 258 | return r; |
| 259 | } |
| 260 | EXPORT_SYMBOL_GPL(st_nci_hci_load_session); |
| 261 | |
| 262 | static void st_nci_hci_admin_event_received(struct nci_dev *ndev, |
| 263 | u8 event, struct sk_buff *skb) |
| 264 | { |
| 265 | struct st_nci_info *info = nci_get_drvdata(ndev); |
| 266 | |
| 267 | switch (event) { |
| 268 | case ST_NCI_EVT_HOT_PLUG: |
| 269 | if (info->se_info.se_active) { |
| 270 | if (!ST_NCI_EVT_HOT_PLUG_IS_INHIBITED(skb)) { |
| 271 | del_timer_sync(&info->se_info.se_active_timer); |
| 272 | info->se_info.se_active = false; |
| 273 | complete(&info->se_info.req_completion); |
| 274 | } else { |
| 275 | mod_timer(&info->se_info.se_active_timer, |
| 276 | jiffies + |
| 277 | msecs_to_jiffies(ST_NCI_SE_TO_PIPES)); |
| 278 | } |
| 279 | } |
| 280 | break; |
Christophe Ricard | 2b5dbe0 | 2015-10-25 22:54:37 +0100 | [diff] [blame] | 281 | default: |
| 282 | nfc_err(&ndev->nfc_dev->dev, "Unexpected event on admin gate\n"); |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | static int st_nci_hci_apdu_reader_event_received(struct nci_dev *ndev, |
| 287 | u8 event, |
| 288 | struct sk_buff *skb) |
| 289 | { |
| 290 | int r = 0; |
| 291 | struct st_nci_info *info = nci_get_drvdata(ndev); |
| 292 | |
| 293 | pr_debug("apdu reader gate event: %x\n", event); |
| 294 | |
| 295 | switch (event) { |
| 296 | case ST_NCI_EVT_TRANSMIT_DATA: |
| 297 | del_timer_sync(&info->se_info.bwi_timer); |
| 298 | info->se_info.bwi_active = false; |
| 299 | info->se_info.cb(info->se_info.cb_context, |
| 300 | skb->data, skb->len, 0); |
| 301 | break; |
| 302 | case ST_NCI_EVT_WTX_REQUEST: |
| 303 | mod_timer(&info->se_info.bwi_timer, jiffies + |
| 304 | msecs_to_jiffies(info->se_info.wt_timeout)); |
| 305 | break; |
Christophe Ricard | 2b5dbe0 | 2015-10-25 22:54:37 +0100 | [diff] [blame] | 306 | default: |
| 307 | nfc_err(&ndev->nfc_dev->dev, "Unexpected event on apdu reader gate\n"); |
| 308 | return 1; |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | kfree_skb(skb); |
| 312 | return r; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Returns: |
| 317 | * <= 0: driver handled the event, skb consumed |
| 318 | * 1: driver does not handle the event, please do standard processing |
| 319 | */ |
| 320 | static int st_nci_hci_connectivity_event_received(struct nci_dev *ndev, |
| 321 | u8 host, u8 event, |
| 322 | struct sk_buff *skb) |
| 323 | { |
| 324 | int r = 0; |
| 325 | struct device *dev = &ndev->nfc_dev->dev; |
| 326 | struct nfc_evt_transaction *transaction; |
| 327 | |
| 328 | pr_debug("connectivity gate event: %x\n", event); |
| 329 | |
| 330 | switch (event) { |
| 331 | case ST_NCI_EVT_CONNECTIVITY: |
Christophe Ricard | 25960c2 | 2015-12-23 23:45:19 +0100 | [diff] [blame] | 332 | r = nfc_se_connectivity(ndev->nfc_dev, host); |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 333 | break; |
| 334 | case ST_NCI_EVT_TRANSACTION: |
| 335 | /* According to specification etsi 102 622 |
| 336 | * 11.2.2.4 EVT_TRANSACTION Table 52 |
| 337 | * Description Tag Length |
| 338 | * AID 81 5 to 16 |
| 339 | * PARAMETERS 82 0 to 255 |
| 340 | */ |
| 341 | if (skb->len < NFC_MIN_AID_LENGTH + 2 && |
| 342 | skb->data[0] != NFC_EVT_TRANSACTION_AID_TAG) |
| 343 | return -EPROTO; |
| 344 | |
| 345 | transaction = (struct nfc_evt_transaction *)devm_kzalloc(dev, |
| 346 | skb->len - 2, GFP_KERNEL); |
Navid Emamdoost | 65d02df | 2019-07-23 17:11:51 -0500 | [diff] [blame] | 347 | if (!transaction) |
| 348 | return -ENOMEM; |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 349 | |
| 350 | transaction->aid_len = skb->data[1]; |
| 351 | memcpy(transaction->aid, &skb->data[2], transaction->aid_len); |
| 352 | |
| 353 | /* Check next byte is PARAMETERS tag (82) */ |
| 354 | if (skb->data[transaction->aid_len + 2] != |
| 355 | NFC_EVT_TRANSACTION_PARAMS_TAG) |
| 356 | return -EPROTO; |
| 357 | |
| 358 | transaction->params_len = skb->data[transaction->aid_len + 3]; |
| 359 | memcpy(transaction->params, skb->data + |
| 360 | transaction->aid_len + 4, transaction->params_len); |
| 361 | |
| 362 | r = nfc_se_transaction(ndev->nfc_dev, host, transaction); |
| 363 | break; |
| 364 | default: |
Christophe Ricard | 2b5dbe0 | 2015-10-25 22:54:37 +0100 | [diff] [blame] | 365 | nfc_err(&ndev->nfc_dev->dev, "Unexpected event on connectivity gate\n"); |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 366 | return 1; |
| 367 | } |
| 368 | kfree_skb(skb); |
| 369 | return r; |
| 370 | } |
| 371 | |
| 372 | void st_nci_hci_event_received(struct nci_dev *ndev, u8 pipe, |
| 373 | u8 event, struct sk_buff *skb) |
| 374 | { |
| 375 | u8 gate = ndev->hci_dev->pipes[pipe].gate; |
| 376 | u8 host = ndev->hci_dev->pipes[pipe].host; |
| 377 | |
| 378 | switch (gate) { |
| 379 | case NCI_HCI_ADMIN_GATE: |
| 380 | st_nci_hci_admin_event_received(ndev, event, skb); |
| 381 | break; |
| 382 | case ST_NCI_APDU_READER_GATE: |
| 383 | st_nci_hci_apdu_reader_event_received(ndev, event, skb); |
| 384 | break; |
| 385 | case ST_NCI_CONNECTIVITY_GATE: |
Christophe Ricard | b1fa4dc | 2015-10-25 22:54:36 +0100 | [diff] [blame] | 386 | st_nci_hci_connectivity_event_received(ndev, host, event, skb); |
| 387 | break; |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | EXPORT_SYMBOL_GPL(st_nci_hci_event_received); |
| 391 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 392 | void st_nci_hci_cmd_received(struct nci_dev *ndev, u8 pipe, u8 cmd, |
| 393 | struct sk_buff *skb) |
| 394 | { |
| 395 | struct st_nci_info *info = nci_get_drvdata(ndev); |
| 396 | u8 gate = ndev->hci_dev->pipes[pipe].gate; |
| 397 | |
| 398 | pr_debug("cmd: %x\n", cmd); |
| 399 | |
| 400 | switch (cmd) { |
| 401 | case NCI_HCI_ANY_OPEN_PIPE: |
| 402 | if (gate != ST_NCI_APDU_READER_GATE && |
| 403 | ndev->hci_dev->pipes[pipe].host != ST_NCI_UICC_HOST_ID) |
| 404 | ndev->hci_dev->count_pipes++; |
| 405 | |
| 406 | if (ndev->hci_dev->count_pipes == |
| 407 | ndev->hci_dev->expected_pipes) { |
| 408 | del_timer_sync(&info->se_info.se_active_timer); |
| 409 | info->se_info.se_active = false; |
| 410 | ndev->hci_dev->count_pipes = 0; |
| 411 | complete(&info->se_info.req_completion); |
| 412 | } |
| 413 | break; |
| 414 | } |
| 415 | } |
| 416 | EXPORT_SYMBOL_GPL(st_nci_hci_cmd_received); |
| 417 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 418 | static int st_nci_control_se(struct nci_dev *ndev, u8 se_idx, |
Christophe Ricard | 3648dc6 | 2015-10-25 22:54:39 +0100 | [diff] [blame] | 419 | u8 state) |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 420 | { |
| 421 | struct st_nci_info *info = nci_get_drvdata(ndev); |
Christophe Ricard | 4e932ac | 2015-10-25 22:54:41 +0100 | [diff] [blame] | 422 | int r, i; |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 423 | struct sk_buff *sk_host_list; |
| 424 | u8 host_id; |
| 425 | |
| 426 | switch (se_idx) { |
| 427 | case ST_NCI_UICC_HOST_ID: |
| 428 | ndev->hci_dev->count_pipes = 0; |
| 429 | ndev->hci_dev->expected_pipes = ST_NCI_SE_COUNT_PIPE_UICC; |
| 430 | break; |
| 431 | case ST_NCI_ESE_HOST_ID: |
| 432 | ndev->hci_dev->count_pipes = 0; |
| 433 | ndev->hci_dev->expected_pipes = ST_NCI_SE_COUNT_PIPE_EMBEDDED; |
| 434 | break; |
| 435 | default: |
| 436 | return -EINVAL; |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * Wait for an EVT_HOT_PLUG in order to |
| 441 | * retrieve a relevant host list. |
| 442 | */ |
| 443 | reinit_completion(&info->se_info.req_completion); |
Christophe Ricard | 3648dc6 | 2015-10-25 22:54:39 +0100 | [diff] [blame] | 444 | r = nci_nfcee_mode_set(ndev, se_idx, state); |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 445 | if (r != NCI_STATUS_OK) |
| 446 | return r; |
| 447 | |
| 448 | mod_timer(&info->se_info.se_active_timer, jiffies + |
| 449 | msecs_to_jiffies(ST_NCI_SE_TO_HOT_PLUG)); |
| 450 | info->se_info.se_active = true; |
| 451 | |
| 452 | /* Ignore return value and check in any case the host_list */ |
| 453 | wait_for_completion_interruptible(&info->se_info.req_completion); |
| 454 | |
| 455 | /* There might be some "collision" after receiving a HOT_PLUG event |
| 456 | * This may cause the CLF to not answer to the next hci command. |
| 457 | * There is no possible synchronization to prevent this. |
| 458 | * Adding a small delay is the only way to solve the issue. |
| 459 | */ |
Christophe Ricard | 3648dc6 | 2015-10-25 22:54:39 +0100 | [diff] [blame] | 460 | if (info->se_info.se_status->is_ese_present && |
| 461 | info->se_info.se_status->is_uicc_present) |
Christophe Ricard | 0652105 | 2015-10-25 22:54:40 +0100 | [diff] [blame] | 462 | usleep_range(15000, 20000); |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 463 | |
| 464 | r = nci_hci_get_param(ndev, NCI_HCI_ADMIN_GATE, |
| 465 | NCI_HCI_ADMIN_PARAM_HOST_LIST, &sk_host_list); |
| 466 | if (r != NCI_HCI_ANY_OK) |
| 467 | return r; |
| 468 | |
Christophe Ricard | 4e932ac | 2015-10-25 22:54:41 +0100 | [diff] [blame] | 469 | for (i = 0; i < sk_host_list->len && |
| 470 | sk_host_list->data[i] != se_idx; i++) |
| 471 | ; |
| 472 | host_id = sk_host_list->data[i]; |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 473 | kfree_skb(sk_host_list); |
| 474 | if (state == ST_NCI_SE_MODE_ON && host_id == se_idx) |
| 475 | return se_idx; |
| 476 | else if (state == ST_NCI_SE_MODE_OFF && host_id != se_idx) |
| 477 | return se_idx; |
| 478 | |
| 479 | return -1; |
| 480 | } |
| 481 | |
| 482 | int st_nci_disable_se(struct nci_dev *ndev, u32 se_idx) |
| 483 | { |
| 484 | int r; |
| 485 | |
| 486 | pr_debug("st_nci_disable_se\n"); |
| 487 | |
Christophe Ricard | 3648dc6 | 2015-10-25 22:54:39 +0100 | [diff] [blame] | 488 | /* |
| 489 | * According to upper layer, se_idx == NFC_SE_UICC when |
| 490 | * info->se_info.se_status->is_uicc_enable is true should never happen |
| 491 | * Same for eSE. |
| 492 | */ |
| 493 | r = st_nci_control_se(ndev, se_idx, ST_NCI_SE_MODE_OFF); |
| 494 | if (r < 0) { |
| 495 | /* Do best effort to release SWP */ |
| 496 | if (se_idx == NFC_SE_EMBEDDED) { |
| 497 | r = nci_hci_send_event(ndev, ST_NCI_APDU_READER_GATE, |
| 498 | ST_NCI_EVT_SE_END_OF_APDU_TRANSFER, |
| 499 | NULL, 0); |
| 500 | } |
| 501 | return r; |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | EXPORT_SYMBOL_GPL(st_nci_disable_se); |
| 507 | |
| 508 | int st_nci_enable_se(struct nci_dev *ndev, u32 se_idx) |
| 509 | { |
| 510 | int r; |
| 511 | |
| 512 | pr_debug("st_nci_enable_se\n"); |
| 513 | |
Christophe Ricard | 3648dc6 | 2015-10-25 22:54:39 +0100 | [diff] [blame] | 514 | /* |
| 515 | * According to upper layer, se_idx == NFC_SE_UICC when |
| 516 | * info->se_info.se_status->is_uicc_enable is true should never happen. |
| 517 | * Same for eSE. |
| 518 | */ |
| 519 | r = st_nci_control_se(ndev, se_idx, ST_NCI_SE_MODE_ON); |
Christophe Ricard | c50e8fe | 2016-04-30 09:12:47 +0200 | [diff] [blame] | 520 | if (r == ST_NCI_ESE_HOST_ID) { |
Christophe Ricard | 3648dc6 | 2015-10-25 22:54:39 +0100 | [diff] [blame] | 521 | st_nci_se_get_atr(ndev); |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 522 | r = nci_hci_send_event(ndev, ST_NCI_APDU_READER_GATE, |
| 523 | ST_NCI_EVT_SE_SOFT_RESET, NULL, 0); |
Christophe Ricard | 3648dc6 | 2015-10-25 22:54:39 +0100 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | if (r < 0) { |
| 527 | /* |
| 528 | * The activation procedure failed, the secure element |
| 529 | * is not connected. Remove from the list. |
| 530 | */ |
| 531 | nfc_remove_se(ndev->nfc_dev, se_idx); |
| 532 | return r; |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | return 0; |
| 536 | } |
| 537 | EXPORT_SYMBOL_GPL(st_nci_enable_se); |
| 538 | |
| 539 | static int st_nci_hci_network_init(struct nci_dev *ndev) |
| 540 | { |
Christophe Ricard | b1fa4dc | 2015-10-25 22:54:36 +0100 | [diff] [blame] | 541 | struct st_nci_info *info = nci_get_drvdata(ndev); |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 542 | struct core_conn_create_dest_spec_params *dest_params; |
| 543 | struct dest_spec_params spec_params; |
| 544 | struct nci_conn_info *conn_info; |
| 545 | int r, dev_num; |
| 546 | |
| 547 | dest_params = |
| 548 | kzalloc(sizeof(struct core_conn_create_dest_spec_params) + |
| 549 | sizeof(struct dest_spec_params), GFP_KERNEL); |
| 550 | if (dest_params == NULL) { |
| 551 | r = -ENOMEM; |
| 552 | goto exit; |
| 553 | } |
| 554 | |
| 555 | dest_params->type = NCI_DESTINATION_SPECIFIC_PARAM_NFCEE_TYPE; |
| 556 | dest_params->length = sizeof(struct dest_spec_params); |
| 557 | spec_params.id = ndev->hci_dev->nfcee_id; |
| 558 | spec_params.protocol = NCI_NFCEE_INTERFACE_HCI_ACCESS; |
| 559 | memcpy(dest_params->value, &spec_params, |
| 560 | sizeof(struct dest_spec_params)); |
| 561 | r = nci_core_conn_create(ndev, NCI_DESTINATION_NFCEE, 1, |
| 562 | sizeof(struct core_conn_create_dest_spec_params) + |
| 563 | sizeof(struct dest_spec_params), |
| 564 | dest_params); |
| 565 | if (r != NCI_STATUS_OK) |
| 566 | goto free_dest_params; |
| 567 | |
| 568 | conn_info = ndev->hci_dev->conn_info; |
| 569 | if (!conn_info) |
| 570 | goto free_dest_params; |
| 571 | |
Christophe Ricard | 404b3e5 | 2015-10-25 22:54:32 +0100 | [diff] [blame] | 572 | ndev->hci_dev->init_data.gate_count = ARRAY_SIZE(st_nci_gates); |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 573 | memcpy(ndev->hci_dev->init_data.gates, st_nci_gates, |
| 574 | sizeof(st_nci_gates)); |
| 575 | |
| 576 | /* |
| 577 | * Session id must include the driver name + i2c bus addr |
| 578 | * persistent info to discriminate 2 identical chips |
| 579 | */ |
| 580 | dev_num = find_first_zero_bit(dev_mask, ST_NCI_NUM_DEVICES); |
| 581 | if (dev_num >= ST_NCI_NUM_DEVICES) { |
| 582 | r = -ENODEV; |
| 583 | goto free_dest_params; |
| 584 | } |
| 585 | |
| 586 | scnprintf(ndev->hci_dev->init_data.session_id, |
| 587 | sizeof(ndev->hci_dev->init_data.session_id), |
| 588 | "%s%2x", "ST21BH", dev_num); |
| 589 | |
| 590 | r = nci_hci_dev_session_init(ndev); |
| 591 | if (r != NCI_HCI_ANY_OK) |
| 592 | goto free_dest_params; |
| 593 | |
Christophe Ricard | b1fa4dc | 2015-10-25 22:54:36 +0100 | [diff] [blame] | 594 | /* |
| 595 | * In factory mode, we prevent secure elements activation |
| 596 | * by disabling nfcee on the current HCI connection id. |
| 597 | * HCI will be used here only for proprietary commands. |
| 598 | */ |
| 599 | if (test_bit(ST_NCI_FACTORY_MODE, &info->flags)) |
Christophe Ricard | 9b8d1a4 | 2016-04-30 09:12:51 +0200 | [diff] [blame] | 600 | r = nci_nfcee_mode_set(ndev, |
| 601 | ndev->hci_dev->conn_info->dest_params->id, |
Christophe Ricard | b1fa4dc | 2015-10-25 22:54:36 +0100 | [diff] [blame] | 602 | NCI_NFCEE_DISABLE); |
| 603 | else |
Christophe Ricard | 9b8d1a4 | 2016-04-30 09:12:51 +0200 | [diff] [blame] | 604 | r = nci_nfcee_mode_set(ndev, |
| 605 | ndev->hci_dev->conn_info->dest_params->id, |
Christophe Ricard | b1fa4dc | 2015-10-25 22:54:36 +0100 | [diff] [blame] | 606 | NCI_NFCEE_ENABLE); |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 607 | |
| 608 | free_dest_params: |
| 609 | kfree(dest_params); |
| 610 | |
| 611 | exit: |
| 612 | return r; |
| 613 | } |
| 614 | |
| 615 | int st_nci_discover_se(struct nci_dev *ndev) |
| 616 | { |
Christophe Ricard | 3648dc6 | 2015-10-25 22:54:39 +0100 | [diff] [blame] | 617 | u8 white_list[2]; |
| 618 | int r, wl_size = 0; |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 619 | int se_count = 0; |
Christophe Ricard | b1fa4dc | 2015-10-25 22:54:36 +0100 | [diff] [blame] | 620 | struct st_nci_info *info = nci_get_drvdata(ndev); |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 621 | |
| 622 | pr_debug("st_nci_discover_se\n"); |
| 623 | |
| 624 | r = st_nci_hci_network_init(ndev); |
| 625 | if (r != 0) |
| 626 | return r; |
| 627 | |
Christophe Ricard | b1fa4dc | 2015-10-25 22:54:36 +0100 | [diff] [blame] | 628 | if (test_bit(ST_NCI_FACTORY_MODE, &info->flags)) |
| 629 | return 0; |
| 630 | |
Christophe Ricard | cde4856 | 2016-04-30 09:12:41 +0200 | [diff] [blame] | 631 | if (info->se_info.se_status->is_uicc_present) |
Christophe Ricard | 3648dc6 | 2015-10-25 22:54:39 +0100 | [diff] [blame] | 632 | white_list[wl_size++] = ST_NCI_UICC_HOST_ID; |
Christophe Ricard | cde4856 | 2016-04-30 09:12:41 +0200 | [diff] [blame] | 633 | if (info->se_info.se_status->is_ese_present) |
Christophe Ricard | 3648dc6 | 2015-10-25 22:54:39 +0100 | [diff] [blame] | 634 | white_list[wl_size++] = ST_NCI_ESE_HOST_ID; |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 635 | |
Christophe Ricard | 3648dc6 | 2015-10-25 22:54:39 +0100 | [diff] [blame] | 636 | if (wl_size) { |
| 637 | r = nci_hci_set_param(ndev, NCI_HCI_ADMIN_GATE, |
| 638 | NCI_HCI_ADMIN_PARAM_WHITELIST, |
| 639 | white_list, wl_size); |
| 640 | if (r != NCI_HCI_ANY_OK) |
| 641 | return r; |
| 642 | } |
| 643 | |
| 644 | if (info->se_info.se_status->is_uicc_present) { |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 645 | nfc_add_se(ndev->nfc_dev, ST_NCI_UICC_HOST_ID, NFC_SE_UICC); |
| 646 | se_count++; |
| 647 | } |
| 648 | |
Christophe Ricard | 3648dc6 | 2015-10-25 22:54:39 +0100 | [diff] [blame] | 649 | if (info->se_info.se_status->is_ese_present) { |
| 650 | nfc_add_se(ndev->nfc_dev, ST_NCI_ESE_HOST_ID, NFC_SE_EMBEDDED); |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 651 | se_count++; |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | return !se_count; |
| 655 | } |
| 656 | EXPORT_SYMBOL_GPL(st_nci_discover_se); |
| 657 | |
| 658 | int st_nci_se_io(struct nci_dev *ndev, u32 se_idx, |
| 659 | u8 *apdu, size_t apdu_length, |
| 660 | se_io_cb_t cb, void *cb_context) |
| 661 | { |
| 662 | struct st_nci_info *info = nci_get_drvdata(ndev); |
| 663 | |
| 664 | pr_debug("\n"); |
| 665 | |
| 666 | switch (se_idx) { |
Christophe Ricard | c50e8fe | 2016-04-30 09:12:47 +0200 | [diff] [blame] | 667 | case ST_NCI_ESE_HOST_ID: |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 668 | info->se_info.cb = cb; |
| 669 | info->se_info.cb_context = cb_context; |
| 670 | mod_timer(&info->se_info.bwi_timer, jiffies + |
| 671 | msecs_to_jiffies(info->se_info.wt_timeout)); |
| 672 | info->se_info.bwi_active = true; |
| 673 | return nci_hci_send_event(ndev, ST_NCI_APDU_READER_GATE, |
| 674 | ST_NCI_EVT_TRANSMIT_DATA, apdu, |
| 675 | apdu_length); |
| 676 | default: |
| 677 | return -ENODEV; |
| 678 | } |
| 679 | } |
| 680 | EXPORT_SYMBOL(st_nci_se_io); |
| 681 | |
| 682 | static void st_nci_se_wt_timeout(unsigned long data) |
| 683 | { |
| 684 | /* |
| 685 | * No answer from the secure element |
| 686 | * within the defined timeout. |
| 687 | * Let's send a reset request as recovery procedure. |
| 688 | * According to the situation, we first try to send a software reset |
| 689 | * to the secure element. If the next command is still not |
| 690 | * answering in time, we send to the CLF a secure element hardware |
| 691 | * reset request. |
| 692 | */ |
| 693 | /* hardware reset managed through VCC_UICC_OUT power supply */ |
| 694 | u8 param = 0x01; |
| 695 | struct st_nci_info *info = (struct st_nci_info *) data; |
| 696 | |
| 697 | pr_debug("\n"); |
| 698 | |
| 699 | info->se_info.bwi_active = false; |
| 700 | |
| 701 | if (!info->se_info.xch_error) { |
| 702 | info->se_info.xch_error = true; |
| 703 | nci_hci_send_event(info->ndlc->ndev, ST_NCI_APDU_READER_GATE, |
| 704 | ST_NCI_EVT_SE_SOFT_RESET, NULL, 0); |
| 705 | } else { |
| 706 | info->se_info.xch_error = false; |
| 707 | nci_hci_send_event(info->ndlc->ndev, ST_NCI_DEVICE_MGNT_GATE, |
| 708 | ST_NCI_EVT_SE_HARD_RESET, ¶m, 1); |
| 709 | } |
| 710 | info->se_info.cb(info->se_info.cb_context, NULL, 0, -ETIME); |
| 711 | } |
| 712 | |
| 713 | static void st_nci_se_activation_timeout(unsigned long data) |
| 714 | { |
| 715 | struct st_nci_info *info = (struct st_nci_info *) data; |
| 716 | |
| 717 | pr_debug("\n"); |
| 718 | |
| 719 | info->se_info.se_active = false; |
| 720 | |
| 721 | complete(&info->se_info.req_completion); |
| 722 | } |
| 723 | |
Christophe Ricard | 3648dc6 | 2015-10-25 22:54:39 +0100 | [diff] [blame] | 724 | int st_nci_se_init(struct nci_dev *ndev, struct st_nci_se_status *se_status) |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 725 | { |
| 726 | struct st_nci_info *info = nci_get_drvdata(ndev); |
| 727 | |
| 728 | init_completion(&info->se_info.req_completion); |
| 729 | /* initialize timers */ |
| 730 | init_timer(&info->se_info.bwi_timer); |
| 731 | info->se_info.bwi_timer.data = (unsigned long)info; |
| 732 | info->se_info.bwi_timer.function = st_nci_se_wt_timeout; |
| 733 | info->se_info.bwi_active = false; |
| 734 | |
| 735 | init_timer(&info->se_info.se_active_timer); |
| 736 | info->se_info.se_active_timer.data = (unsigned long)info; |
| 737 | info->se_info.se_active_timer.function = |
| 738 | st_nci_se_activation_timeout; |
| 739 | info->se_info.se_active = false; |
| 740 | |
| 741 | info->se_info.xch_error = false; |
| 742 | |
| 743 | info->se_info.wt_timeout = |
| 744 | ST_NCI_BWI_TO_TIMEOUT(ST_NCI_ATR_DEFAULT_BWI); |
| 745 | |
Christophe Ricard | 3648dc6 | 2015-10-25 22:54:39 +0100 | [diff] [blame] | 746 | info->se_info.se_status = se_status; |
| 747 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 748 | return 0; |
| 749 | } |
| 750 | EXPORT_SYMBOL(st_nci_se_init); |
| 751 | |
| 752 | void st_nci_se_deinit(struct nci_dev *ndev) |
| 753 | { |
| 754 | struct st_nci_info *info = nci_get_drvdata(ndev); |
| 755 | |
| 756 | if (info->se_info.bwi_active) |
| 757 | del_timer_sync(&info->se_info.bwi_timer); |
| 758 | if (info->se_info.se_active) |
| 759 | del_timer_sync(&info->se_info.se_active_timer); |
| 760 | |
| 761 | info->se_info.se_active = false; |
| 762 | info->se_info.bwi_active = false; |
| 763 | } |
| 764 | EXPORT_SYMBOL(st_nci_se_deinit); |
| 765 | |