Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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