xref: /openbmc/linux/drivers/nfc/st21nfca/se.c (revision 25ff6f8a)
146fe7771SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21c54795dSChristophe Ricard /*
31c54795dSChristophe Ricard  * Copyright (C) 2014  STMicroelectronics SAS. All rights reserved.
41c54795dSChristophe Ricard  */
51c54795dSChristophe Ricard 
61c54795dSChristophe Ricard #include <net/nfc/hci.h>
71c54795dSChristophe Ricard 
81c54795dSChristophe Ricard #include "st21nfca.h"
91c54795dSChristophe Ricard 
101c54795dSChristophe Ricard #define ST21NFCA_EVT_UICC_ACTIVATE		0x10
111c54795dSChristophe Ricard #define ST21NFCA_EVT_UICC_DEACTIVATE		0x13
121c54795dSChristophe Ricard #define ST21NFCA_EVT_SE_HARD_RESET		0x20
131c54795dSChristophe Ricard #define ST21NFCA_EVT_SE_SOFT_RESET		0x11
141c54795dSChristophe Ricard #define ST21NFCA_EVT_SE_END_OF_APDU_TRANSFER	0x21
151c54795dSChristophe Ricard #define ST21NFCA_EVT_SE_ACTIVATE		0x22
161c54795dSChristophe Ricard #define ST21NFCA_EVT_SE_DEACTIVATE		0x23
171c54795dSChristophe Ricard 
181c54795dSChristophe Ricard #define ST21NFCA_EVT_TRANSMIT_DATA		0x10
191c54795dSChristophe Ricard #define ST21NFCA_EVT_WTX_REQUEST		0x11
201c54795dSChristophe Ricard 
211c54795dSChristophe Ricard #define ST21NFCA_EVT_CONNECTIVITY		0x10
221c54795dSChristophe Ricard #define ST21NFCA_EVT_TRANSACTION		0x12
231c54795dSChristophe Ricard 
241c54795dSChristophe Ricard #define ST21NFCA_SE_TO_HOT_PLUG			1000
251c54795dSChristophe Ricard /* Connectivity pipe only */
261c54795dSChristophe Ricard #define ST21NFCA_SE_COUNT_PIPE_UICC		0x01
271c54795dSChristophe Ricard /* Connectivity + APDU Reader pipe */
281c54795dSChristophe Ricard #define ST21NFCA_SE_COUNT_PIPE_EMBEDDED	0x02
291c54795dSChristophe Ricard 
301c54795dSChristophe Ricard #define ST21NFCA_SE_MODE_OFF			0x00
311c54795dSChristophe Ricard #define ST21NFCA_SE_MODE_ON				0x01
321c54795dSChristophe Ricard 
331c54795dSChristophe Ricard #define ST21NFCA_PARAM_ATR				0x01
341c54795dSChristophe Ricard #define ST21NFCA_ATR_DEFAULT_BWI		0x04
351c54795dSChristophe Ricard 
361c54795dSChristophe Ricard /*
371c54795dSChristophe Ricard  * WT = 2^BWI/10[s], convert into msecs and add a secure
381c54795dSChristophe Ricard  * room by increasing by 2 this timeout
391c54795dSChristophe Ricard  */
401c54795dSChristophe Ricard #define ST21NFCA_BWI_TO_TIMEOUT(x)		((1 << x) * 200)
411c54795dSChristophe Ricard #define ST21NFCA_ATR_GET_Y_FROM_TD(x)	(x >> 4)
421c54795dSChristophe Ricard 
431c54795dSChristophe Ricard /* If TA is present bit 0 is set */
441c54795dSChristophe Ricard #define ST21NFCA_ATR_TA_PRESENT(x) (x & 0x01)
451c54795dSChristophe Ricard /* If TB is present bit 1 is set */
461c54795dSChristophe Ricard #define ST21NFCA_ATR_TB_PRESENT(x) (x & 0x02)
471c54795dSChristophe Ricard 
st21nfca_se_get_bwi(struct nfc_hci_dev * hdev)481c54795dSChristophe Ricard static u8 st21nfca_se_get_bwi(struct nfc_hci_dev *hdev)
491c54795dSChristophe Ricard {
501c54795dSChristophe Ricard 	int i;
511c54795dSChristophe Ricard 	u8 td;
521c54795dSChristophe Ricard 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
531c54795dSChristophe Ricard 
541c54795dSChristophe Ricard 	/* Bits 8 to 5 of the first TB for T=1 encode BWI from zero to nine */
551c54795dSChristophe Ricard 	for (i = 1; i < ST21NFCA_ESE_MAX_LENGTH; i++) {
561c54795dSChristophe Ricard 		td = ST21NFCA_ATR_GET_Y_FROM_TD(info->se_info.atr[i]);
571c54795dSChristophe Ricard 		if (ST21NFCA_ATR_TA_PRESENT(td))
581c54795dSChristophe Ricard 			i++;
591c54795dSChristophe Ricard 		if (ST21NFCA_ATR_TB_PRESENT(td)) {
601c54795dSChristophe Ricard 			i++;
611c54795dSChristophe Ricard 			return info->se_info.atr[i] >> 4;
621c54795dSChristophe Ricard 		}
631c54795dSChristophe Ricard 	}
641c54795dSChristophe Ricard 	return ST21NFCA_ATR_DEFAULT_BWI;
651c54795dSChristophe Ricard }
661c54795dSChristophe Ricard 
st21nfca_se_get_atr(struct nfc_hci_dev * hdev)671c54795dSChristophe Ricard static void st21nfca_se_get_atr(struct nfc_hci_dev *hdev)
681c54795dSChristophe Ricard {
691c54795dSChristophe Ricard 	int r;
701c54795dSChristophe Ricard 	struct sk_buff *skb;
711c54795dSChristophe Ricard 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
721c54795dSChristophe Ricard 
731c54795dSChristophe Ricard 	r = nfc_hci_get_param(hdev, ST21NFCA_APDU_READER_GATE,
741c54795dSChristophe Ricard 			ST21NFCA_PARAM_ATR, &skb);
751c54795dSChristophe Ricard 	if (r < 0)
761c54795dSChristophe Ricard 		return;
771c54795dSChristophe Ricard 
781c54795dSChristophe Ricard 	if (skb->len <= ST21NFCA_ESE_MAX_LENGTH) {
791c54795dSChristophe Ricard 		memcpy(info->se_info.atr, skb->data, skb->len);
801c54795dSChristophe Ricard 		info->se_info.wt_timeout =
811c54795dSChristophe Ricard 			ST21NFCA_BWI_TO_TIMEOUT(st21nfca_se_get_bwi(hdev));
821c54795dSChristophe Ricard 	}
831c54795dSChristophe Ricard 	kfree_skb(skb);
841c54795dSChristophe Ricard }
851c54795dSChristophe Ricard 
st21nfca_hci_control_se(struct nfc_hci_dev * hdev,u32 se_idx,u8 state)861c54795dSChristophe Ricard static int st21nfca_hci_control_se(struct nfc_hci_dev *hdev, u32 se_idx,
871c54795dSChristophe Ricard 				u8 state)
881c54795dSChristophe Ricard {
891c54795dSChristophe Ricard 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
90cde2aa99SChristophe Ricard 	int r, i;
911c54795dSChristophe Ricard 	struct sk_buff *sk_host_list;
921c54795dSChristophe Ricard 	u8 se_event, host_id;
931c54795dSChristophe Ricard 
941c54795dSChristophe Ricard 	switch (se_idx) {
951c54795dSChristophe Ricard 	case NFC_HCI_UICC_HOST_ID:
961c54795dSChristophe Ricard 		se_event = (state == ST21NFCA_SE_MODE_ON ?
971c54795dSChristophe Ricard 					ST21NFCA_EVT_UICC_ACTIVATE :
981c54795dSChristophe Ricard 					ST21NFCA_EVT_UICC_DEACTIVATE);
991c54795dSChristophe Ricard 
1001c54795dSChristophe Ricard 		info->se_info.count_pipes = 0;
1011c54795dSChristophe Ricard 		info->se_info.expected_pipes = ST21NFCA_SE_COUNT_PIPE_UICC;
1021c54795dSChristophe Ricard 		break;
1031c54795dSChristophe Ricard 	case ST21NFCA_ESE_HOST_ID:
1041c54795dSChristophe Ricard 		se_event = (state == ST21NFCA_SE_MODE_ON ?
1051c54795dSChristophe Ricard 					ST21NFCA_EVT_SE_ACTIVATE :
1061c54795dSChristophe Ricard 					ST21NFCA_EVT_SE_DEACTIVATE);
1071c54795dSChristophe Ricard 
1081c54795dSChristophe Ricard 		info->se_info.count_pipes = 0;
1091c54795dSChristophe Ricard 		info->se_info.expected_pipes = ST21NFCA_SE_COUNT_PIPE_EMBEDDED;
1101c54795dSChristophe Ricard 		break;
1111c54795dSChristophe Ricard 	default:
1121c54795dSChristophe Ricard 		return -EINVAL;
1131c54795dSChristophe Ricard 	}
1141c54795dSChristophe Ricard 
1151c54795dSChristophe Ricard 	/*
1161c54795dSChristophe Ricard 	 * Wait for an EVT_HOT_PLUG in order to
1171c54795dSChristophe Ricard 	 * retrieve a relevant host list.
1181c54795dSChristophe Ricard 	 */
1191c54795dSChristophe Ricard 	reinit_completion(&info->se_info.req_completion);
1201c54795dSChristophe Ricard 	r = nfc_hci_send_event(hdev, ST21NFCA_DEVICE_MGNT_GATE, se_event,
1211c54795dSChristophe Ricard 			       NULL, 0);
1221c54795dSChristophe Ricard 	if (r < 0)
1231c54795dSChristophe Ricard 		return r;
1241c54795dSChristophe Ricard 
1251c54795dSChristophe Ricard 	mod_timer(&info->se_info.se_active_timer, jiffies +
1261c54795dSChristophe Ricard 		msecs_to_jiffies(ST21NFCA_SE_TO_HOT_PLUG));
1271c54795dSChristophe Ricard 	info->se_info.se_active = true;
1281c54795dSChristophe Ricard 
1291c54795dSChristophe Ricard 	/* Ignore return value and check in any case the host_list */
1301c54795dSChristophe Ricard 	wait_for_completion_interruptible(&info->se_info.req_completion);
1311c54795dSChristophe Ricard 
1321c54795dSChristophe Ricard 	r = nfc_hci_get_param(hdev, NFC_HCI_ADMIN_GATE,
1331c54795dSChristophe Ricard 			NFC_HCI_ADMIN_HOST_LIST,
1341c54795dSChristophe Ricard 			&sk_host_list);
1351c54795dSChristophe Ricard 	if (r < 0)
1361c54795dSChristophe Ricard 		return r;
1371c54795dSChristophe Ricard 
138cde2aa99SChristophe Ricard 	for (i = 0; i < sk_host_list->len &&
139cde2aa99SChristophe Ricard 		sk_host_list->data[i] != se_idx; i++)
140cde2aa99SChristophe Ricard 		;
141cde2aa99SChristophe Ricard 	host_id = sk_host_list->data[i];
1421c54795dSChristophe Ricard 	kfree_skb(sk_host_list);
1431c54795dSChristophe Ricard 
1441c54795dSChristophe Ricard 	if (state == ST21NFCA_SE_MODE_ON && host_id == se_idx)
1451c54795dSChristophe Ricard 		return se_idx;
1461c54795dSChristophe Ricard 	else if (state == ST21NFCA_SE_MODE_OFF && host_id != se_idx)
1471c54795dSChristophe Ricard 		return se_idx;
1481c54795dSChristophe Ricard 
1491c54795dSChristophe Ricard 	return -1;
1501c54795dSChristophe Ricard }
1511c54795dSChristophe Ricard 
st21nfca_hci_discover_se(struct nfc_hci_dev * hdev)1521c54795dSChristophe Ricard int st21nfca_hci_discover_se(struct nfc_hci_dev *hdev)
1531c54795dSChristophe Ricard {
1541c54795dSChristophe Ricard 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
1551c54795dSChristophe Ricard 	int se_count = 0;
1561c54795dSChristophe Ricard 
15715d17170SChristophe Ricard 	if (test_bit(ST21NFCA_FACTORY_MODE, &hdev->quirks))
15815d17170SChristophe Ricard 		return 0;
15915d17170SChristophe Ricard 
1601c54795dSChristophe Ricard 	if (info->se_status->is_uicc_present) {
1611c54795dSChristophe Ricard 		nfc_add_se(hdev->ndev, NFC_HCI_UICC_HOST_ID, NFC_SE_UICC);
1621c54795dSChristophe Ricard 		se_count++;
1631c54795dSChristophe Ricard 	}
1641c54795dSChristophe Ricard 
1651c54795dSChristophe Ricard 	if (info->se_status->is_ese_present) {
1661c54795dSChristophe Ricard 		nfc_add_se(hdev->ndev, ST21NFCA_ESE_HOST_ID, NFC_SE_EMBEDDED);
1671c54795dSChristophe Ricard 		se_count++;
1681c54795dSChristophe Ricard 	}
1691c54795dSChristophe Ricard 
1701c54795dSChristophe Ricard 	return !se_count;
1711c54795dSChristophe Ricard }
1721c54795dSChristophe Ricard EXPORT_SYMBOL(st21nfca_hci_discover_se);
1731c54795dSChristophe Ricard 
st21nfca_hci_enable_se(struct nfc_hci_dev * hdev,u32 se_idx)1741c54795dSChristophe Ricard int st21nfca_hci_enable_se(struct nfc_hci_dev *hdev, u32 se_idx)
1751c54795dSChristophe Ricard {
1761c54795dSChristophe Ricard 	int r;
1771c54795dSChristophe Ricard 
1781c54795dSChristophe Ricard 	/*
1791c54795dSChristophe Ricard 	 * According to upper layer, se_idx == NFC_SE_UICC when
1801c54795dSChristophe Ricard 	 * info->se_status->is_uicc_enable is true should never happen.
1811c54795dSChristophe Ricard 	 * Same for eSE.
1821c54795dSChristophe Ricard 	 */
1831c54795dSChristophe Ricard 	r = st21nfca_hci_control_se(hdev, se_idx, ST21NFCA_SE_MODE_ON);
1841c54795dSChristophe Ricard 	if (r == ST21NFCA_ESE_HOST_ID) {
1851c54795dSChristophe Ricard 		st21nfca_se_get_atr(hdev);
1861c54795dSChristophe Ricard 		r = nfc_hci_send_event(hdev, ST21NFCA_APDU_READER_GATE,
1871c54795dSChristophe Ricard 				ST21NFCA_EVT_SE_SOFT_RESET, NULL, 0);
1881c54795dSChristophe Ricard 		if (r < 0)
1891c54795dSChristophe Ricard 			return r;
1901c54795dSChristophe Ricard 	} else if (r < 0) {
1911c54795dSChristophe Ricard 		/*
1921c54795dSChristophe Ricard 		 * The activation tentative failed, the secure element
1931c54795dSChristophe Ricard 		 * is not connected. Remove from the list.
1941c54795dSChristophe Ricard 		 */
1951c54795dSChristophe Ricard 		nfc_remove_se(hdev->ndev, se_idx);
1961c54795dSChristophe Ricard 		return r;
1971c54795dSChristophe Ricard 	}
1981c54795dSChristophe Ricard 
1991c54795dSChristophe Ricard 	return 0;
2001c54795dSChristophe Ricard }
2011c54795dSChristophe Ricard EXPORT_SYMBOL(st21nfca_hci_enable_se);
2021c54795dSChristophe Ricard 
st21nfca_hci_disable_se(struct nfc_hci_dev * hdev,u32 se_idx)2031c54795dSChristophe Ricard int st21nfca_hci_disable_se(struct nfc_hci_dev *hdev, u32 se_idx)
2041c54795dSChristophe Ricard {
2051c54795dSChristophe Ricard 	int r;
2061c54795dSChristophe Ricard 
2071c54795dSChristophe Ricard 	/*
2081c54795dSChristophe Ricard 	 * According to upper layer, se_idx == NFC_SE_UICC when
2091c54795dSChristophe Ricard 	 * info->se_status->is_uicc_enable is true should never happen
2101c54795dSChristophe Ricard 	 * Same for eSE.
2111c54795dSChristophe Ricard 	 */
2121c54795dSChristophe Ricard 	r = st21nfca_hci_control_se(hdev, se_idx, ST21NFCA_SE_MODE_OFF);
2131c54795dSChristophe Ricard 	if (r < 0)
2141c54795dSChristophe Ricard 		return r;
2151c54795dSChristophe Ricard 
2161c54795dSChristophe Ricard 	return 0;
2171c54795dSChristophe Ricard }
2181c54795dSChristophe Ricard EXPORT_SYMBOL(st21nfca_hci_disable_se);
2191c54795dSChristophe Ricard 
st21nfca_hci_se_io(struct nfc_hci_dev * hdev,u32 se_idx,u8 * apdu,size_t apdu_length,se_io_cb_t cb,void * cb_context)2201c54795dSChristophe Ricard int st21nfca_hci_se_io(struct nfc_hci_dev *hdev, u32 se_idx,
2211c54795dSChristophe Ricard 			u8 *apdu, size_t apdu_length,
2221c54795dSChristophe Ricard 			se_io_cb_t cb, void *cb_context)
2231c54795dSChristophe Ricard {
2241c54795dSChristophe Ricard 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
2251c54795dSChristophe Ricard 
2261c54795dSChristophe Ricard 	pr_debug("se_io %x\n", se_idx);
2271c54795dSChristophe Ricard 
2281c54795dSChristophe Ricard 	switch (se_idx) {
2291c54795dSChristophe Ricard 	case ST21NFCA_ESE_HOST_ID:
2301c54795dSChristophe Ricard 		info->se_info.cb = cb;
2311c54795dSChristophe Ricard 		info->se_info.cb_context = cb_context;
2321c54795dSChristophe Ricard 		mod_timer(&info->se_info.bwi_timer, jiffies +
2331c54795dSChristophe Ricard 			  msecs_to_jiffies(info->se_info.wt_timeout));
2341c54795dSChristophe Ricard 		info->se_info.bwi_active = true;
2351c54795dSChristophe Ricard 		return nfc_hci_send_event(hdev, ST21NFCA_APDU_READER_GATE,
2361c54795dSChristophe Ricard 					ST21NFCA_EVT_TRANSMIT_DATA,
2371c54795dSChristophe Ricard 					apdu, apdu_length);
2381c54795dSChristophe Ricard 	default:
239*25ff6f8aSFedor Pchelkin 		/* Need to free cb_context here as at the moment we can't
240*25ff6f8aSFedor Pchelkin 		 * clearly indicate to the caller if the callback function
241*25ff6f8aSFedor Pchelkin 		 * would be called (and free it) or not. In both cases a
242*25ff6f8aSFedor Pchelkin 		 * negative value may be returned to the caller.
243*25ff6f8aSFedor Pchelkin 		 */
244*25ff6f8aSFedor Pchelkin 		kfree(cb_context);
2451c54795dSChristophe Ricard 		return -ENODEV;
2461c54795dSChristophe Ricard 	}
2471c54795dSChristophe Ricard }
2481c54795dSChristophe Ricard EXPORT_SYMBOL(st21nfca_hci_se_io);
2491c54795dSChristophe Ricard 
st21nfca_se_wt_work(struct work_struct * work)250b413b0cbSDuoming Zhou static void st21nfca_se_wt_work(struct work_struct *work)
2511c54795dSChristophe Ricard {
2521c54795dSChristophe Ricard 	/*
2531c54795dSChristophe Ricard 	 * No answer from the secure element
2541c54795dSChristophe Ricard 	 * within the defined timeout.
2551c54795dSChristophe Ricard 	 * Let's send a reset request as recovery procedure.
2561c54795dSChristophe Ricard 	 * According to the situation, we first try to send a software reset
2571c54795dSChristophe Ricard 	 * to the secure element. If the next command is still not
2581c54795dSChristophe Ricard 	 * answering in time, we send to the CLF a secure element hardware
2591c54795dSChristophe Ricard 	 * reset request.
2601c54795dSChristophe Ricard 	 */
2611c54795dSChristophe Ricard 	/* hardware reset managed through VCC_UICC_OUT power supply */
2621c54795dSChristophe Ricard 	u8 param = 0x01;
263b413b0cbSDuoming Zhou 	struct st21nfca_hci_info *info = container_of(work,
264b413b0cbSDuoming Zhou 						struct st21nfca_hci_info,
265b413b0cbSDuoming Zhou 						se_info.timeout_work);
2661c54795dSChristophe Ricard 
2671c54795dSChristophe Ricard 	info->se_info.bwi_active = false;
2681c54795dSChristophe Ricard 
2691c54795dSChristophe Ricard 	if (!info->se_info.xch_error) {
2701c54795dSChristophe Ricard 		info->se_info.xch_error = true;
2711c54795dSChristophe Ricard 		nfc_hci_send_event(info->hdev, ST21NFCA_APDU_READER_GATE,
2721c54795dSChristophe Ricard 				ST21NFCA_EVT_SE_SOFT_RESET, NULL, 0);
2731c54795dSChristophe Ricard 	} else {
2741c54795dSChristophe Ricard 		info->se_info.xch_error = false;
2751c54795dSChristophe Ricard 		nfc_hci_send_event(info->hdev, ST21NFCA_DEVICE_MGNT_GATE,
2761c54795dSChristophe Ricard 				ST21NFCA_EVT_SE_HARD_RESET, &param, 1);
2771c54795dSChristophe Ricard 	}
2781c54795dSChristophe Ricard 	info->se_info.cb(info->se_info.cb_context, NULL, 0, -ETIME);
2791c54795dSChristophe Ricard }
2801c54795dSChristophe Ricard 
st21nfca_se_wt_timeout(struct timer_list * t)281b413b0cbSDuoming Zhou static void st21nfca_se_wt_timeout(struct timer_list *t)
282b413b0cbSDuoming Zhou {
283b413b0cbSDuoming Zhou 	struct st21nfca_hci_info *info = from_timer(info, t, se_info.bwi_timer);
284b413b0cbSDuoming Zhou 
285b413b0cbSDuoming Zhou 	schedule_work(&info->se_info.timeout_work);
286b413b0cbSDuoming Zhou }
287b413b0cbSDuoming Zhou 
st21nfca_se_activation_timeout(struct timer_list * t)28886cb30ecSKees Cook static void st21nfca_se_activation_timeout(struct timer_list *t)
2891c54795dSChristophe Ricard {
29086cb30ecSKees Cook 	struct st21nfca_hci_info *info = from_timer(info, t,
29186cb30ecSKees Cook 						    se_info.se_active_timer);
2921c54795dSChristophe Ricard 
2931c54795dSChristophe Ricard 	info->se_info.se_active = false;
2941c54795dSChristophe Ricard 
2951c54795dSChristophe Ricard 	complete(&info->se_info.req_completion);
2961c54795dSChristophe Ricard }
2971c54795dSChristophe Ricard 
2981c54795dSChristophe Ricard /*
2991c54795dSChristophe Ricard  * Returns:
3001c54795dSChristophe Ricard  * <= 0: driver handled the event, skb consumed
3011c54795dSChristophe Ricard  *    1: driver does not handle the event, please do standard processing
3021c54795dSChristophe Ricard  */
st21nfca_connectivity_event_received(struct nfc_hci_dev * hdev,u8 host,u8 event,struct sk_buff * skb)3031c54795dSChristophe Ricard int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host,
3041c54795dSChristophe Ricard 				u8 event, struct sk_buff *skb)
3051c54795dSChristophe Ricard {
3061c54795dSChristophe Ricard 	int r = 0;
3071c54795dSChristophe Ricard 	struct device *dev = &hdev->ndev->dev;
3081c54795dSChristophe Ricard 	struct nfc_evt_transaction *transaction;
309f2e19b36SMartin Faltesek 	u32 aid_len;
310f2e19b36SMartin Faltesek 	u8 params_len;
3111c54795dSChristophe Ricard 
3121c54795dSChristophe Ricard 	pr_debug("connectivity gate event: %x\n", event);
3131c54795dSChristophe Ricard 
3141c54795dSChristophe Ricard 	switch (event) {
3151c54795dSChristophe Ricard 	case ST21NFCA_EVT_CONNECTIVITY:
31672c54c42SChristophe Ricard 		r = nfc_se_connectivity(hdev->ndev, host);
3171c54795dSChristophe Ricard 	break;
3181c54795dSChristophe Ricard 	case ST21NFCA_EVT_TRANSACTION:
319f2e19b36SMartin Faltesek 		/* According to specification etsi 102 622
3201c54795dSChristophe Ricard 		 * 11.2.2.4 EVT_TRANSACTION Table 52
3211c54795dSChristophe Ricard 		 * Description	Tag	Length
3221c54795dSChristophe Ricard 		 * AID		81	5 to 16
3231c54795dSChristophe Ricard 		 * PARAMETERS	82	0 to 255
324f2e19b36SMartin Faltesek 		 *
325f2e19b36SMartin Faltesek 		 * The key differences are aid storage length is variably sized
326f2e19b36SMartin Faltesek 		 * in the packet, but fixed in nfc_evt_transaction, and that the aid_len
327f2e19b36SMartin Faltesek 		 * is u8 in the packet, but u32 in the structure, and the tags in
328f2e19b36SMartin Faltesek 		 * the packet are not included in nfc_evt_transaction.
329f2e19b36SMartin Faltesek 		 *
330f2e19b36SMartin Faltesek 		 * size in bytes: 1          1       5-16 1             1           0-255
331f2e19b36SMartin Faltesek 		 * offset:        0          1       2    aid_len + 2   aid_len + 3 aid_len + 4
332f2e19b36SMartin Faltesek 		 * member name:   aid_tag(M) aid_len aid  params_tag(M) params_len  params
333f2e19b36SMartin Faltesek 		 * example:       0x81       5-16    X    0x82 0-255    X
3341c54795dSChristophe Ricard 		 */
335f2e19b36SMartin Faltesek 		if (skb->len < 2 || skb->data[0] != NFC_EVT_TRANSACTION_AID_TAG)
3361c54795dSChristophe Ricard 			return -EPROTO;
3371c54795dSChristophe Ricard 
338f2e19b36SMartin Faltesek 		aid_len = skb->data[1];
339f2e19b36SMartin Faltesek 
340f2e19b36SMartin Faltesek 		if (skb->len < aid_len + 4 || aid_len > sizeof(transaction->aid))
341f2e19b36SMartin Faltesek 			return -EPROTO;
342f2e19b36SMartin Faltesek 
343f2e19b36SMartin Faltesek 		params_len = skb->data[aid_len + 3];
344f2e19b36SMartin Faltesek 
345f2e19b36SMartin Faltesek 		/* Verify PARAMETERS tag is (82), and final check that there is enough
346f2e19b36SMartin Faltesek 		 * space in the packet to read everything.
347f2e19b36SMartin Faltesek 		 */
348f2e19b36SMartin Faltesek 		if ((skb->data[aid_len + 2] != NFC_EVT_TRANSACTION_PARAMS_TAG) ||
349f2e19b36SMartin Faltesek 		    (skb->len < aid_len + 4 + params_len))
350f2e19b36SMartin Faltesek 			return -EPROTO;
351f2e19b36SMartin Faltesek 
352f2e19b36SMartin Faltesek 		transaction = devm_kzalloc(dev, sizeof(*transaction) + params_len, GFP_KERNEL);
3539891d068SNavid Emamdoost 		if (!transaction)
3549891d068SNavid Emamdoost 			return -ENOMEM;
3551c54795dSChristophe Ricard 
356f2e19b36SMartin Faltesek 		transaction->aid_len = aid_len;
357f2e19b36SMartin Faltesek 		transaction->params_len = params_len;
3584fbcc1a4SJordy Zomer 
359f2e19b36SMartin Faltesek 		memcpy(transaction->aid, &skb->data[2], aid_len);
360f2e19b36SMartin Faltesek 		memcpy(transaction->params, &skb->data[aid_len + 4], params_len);
3611c54795dSChristophe Ricard 
3621c54795dSChristophe Ricard 		r = nfc_se_transaction(hdev->ndev, host, transaction);
3631c54795dSChristophe Ricard 	break;
3641c54795dSChristophe Ricard 	default:
365a9e062d0SChristophe Ricard 		nfc_err(&hdev->ndev->dev, "Unexpected event on connectivity gate\n");
3661c54795dSChristophe Ricard 		return 1;
3671c54795dSChristophe Ricard 	}
3681c54795dSChristophe Ricard 	kfree_skb(skb);
3691c54795dSChristophe Ricard 	return r;
3701c54795dSChristophe Ricard }
3711c54795dSChristophe Ricard EXPORT_SYMBOL(st21nfca_connectivity_event_received);
3721c54795dSChristophe Ricard 
st21nfca_apdu_reader_event_received(struct nfc_hci_dev * hdev,u8 event,struct sk_buff * skb)3731c54795dSChristophe Ricard int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev,
3741c54795dSChristophe Ricard 					u8 event, struct sk_buff *skb)
3751c54795dSChristophe Ricard {
3761c54795dSChristophe Ricard 	int r = 0;
3771c54795dSChristophe Ricard 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
3781c54795dSChristophe Ricard 
3791c54795dSChristophe Ricard 	pr_debug("apdu reader gate event: %x\n", event);
3801c54795dSChristophe Ricard 
3811c54795dSChristophe Ricard 	switch (event) {
3821c54795dSChristophe Ricard 	case ST21NFCA_EVT_TRANSMIT_DATA:
3831c54795dSChristophe Ricard 		del_timer_sync(&info->se_info.bwi_timer);
384b413b0cbSDuoming Zhou 		cancel_work_sync(&info->se_info.timeout_work);
3851c54795dSChristophe Ricard 		info->se_info.bwi_active = false;
3861c54795dSChristophe Ricard 		r = nfc_hci_send_event(hdev, ST21NFCA_DEVICE_MGNT_GATE,
3871c54795dSChristophe Ricard 				ST21NFCA_EVT_SE_END_OF_APDU_TRANSFER, NULL, 0);
3881c54795dSChristophe Ricard 		if (r < 0)
3891c54795dSChristophe Ricard 			goto exit;
3901c54795dSChristophe Ricard 
3911c54795dSChristophe Ricard 		info->se_info.cb(info->se_info.cb_context,
3921c54795dSChristophe Ricard 			skb->data, skb->len, 0);
3931c54795dSChristophe Ricard 		break;
3941c54795dSChristophe Ricard 	case ST21NFCA_EVT_WTX_REQUEST:
3951c54795dSChristophe Ricard 		mod_timer(&info->se_info.bwi_timer, jiffies +
3961c54795dSChristophe Ricard 				msecs_to_jiffies(info->se_info.wt_timeout));
3971c54795dSChristophe Ricard 		break;
398a9e062d0SChristophe Ricard 	default:
399a9e062d0SChristophe Ricard 		nfc_err(&hdev->ndev->dev, "Unexpected event on apdu reader gate\n");
400a9e062d0SChristophe Ricard 		return 1;
4011c54795dSChristophe Ricard 	}
4021c54795dSChristophe Ricard 
4031c54795dSChristophe Ricard exit:
4041c54795dSChristophe Ricard 	kfree_skb(skb);
4051c54795dSChristophe Ricard 	return r;
4061c54795dSChristophe Ricard }
4071c54795dSChristophe Ricard EXPORT_SYMBOL(st21nfca_apdu_reader_event_received);
4081c54795dSChristophe Ricard 
st21nfca_se_init(struct nfc_hci_dev * hdev)4091c54795dSChristophe Ricard void st21nfca_se_init(struct nfc_hci_dev *hdev)
4101c54795dSChristophe Ricard {
4111c54795dSChristophe Ricard 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
4121c54795dSChristophe Ricard 
4131c54795dSChristophe Ricard 	init_completion(&info->se_info.req_completion);
414b413b0cbSDuoming Zhou 	INIT_WORK(&info->se_info.timeout_work, st21nfca_se_wt_work);
4151c54795dSChristophe Ricard 	/* initialize timers */
41686cb30ecSKees Cook 	timer_setup(&info->se_info.bwi_timer, st21nfca_se_wt_timeout, 0);
4171c54795dSChristophe Ricard 	info->se_info.bwi_active = false;
4181c54795dSChristophe Ricard 
41986cb30ecSKees Cook 	timer_setup(&info->se_info.se_active_timer,
42086cb30ecSKees Cook 		    st21nfca_se_activation_timeout, 0);
4211c54795dSChristophe Ricard 	info->se_info.se_active = false;
4221c54795dSChristophe Ricard 
4231c54795dSChristophe Ricard 	info->se_info.count_pipes = 0;
4241c54795dSChristophe Ricard 	info->se_info.expected_pipes = 0;
4251c54795dSChristophe Ricard 
4261c54795dSChristophe Ricard 	info->se_info.xch_error = false;
4271c54795dSChristophe Ricard 
4281c54795dSChristophe Ricard 	info->se_info.wt_timeout =
4291c54795dSChristophe Ricard 			ST21NFCA_BWI_TO_TIMEOUT(ST21NFCA_ATR_DEFAULT_BWI);
4301c54795dSChristophe Ricard }
4311c54795dSChristophe Ricard EXPORT_SYMBOL(st21nfca_se_init);
4321c54795dSChristophe Ricard 
st21nfca_se_deinit(struct nfc_hci_dev * hdev)4331c54795dSChristophe Ricard void st21nfca_se_deinit(struct nfc_hci_dev *hdev)
4341c54795dSChristophe Ricard {
4351c54795dSChristophe Ricard 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
4361c54795dSChristophe Ricard 
4371c54795dSChristophe Ricard 	if (info->se_info.bwi_active)
4381c54795dSChristophe Ricard 		del_timer_sync(&info->se_info.bwi_timer);
4391c54795dSChristophe Ricard 	if (info->se_info.se_active)
4401c54795dSChristophe Ricard 		del_timer_sync(&info->se_info.se_active_timer);
4411c54795dSChristophe Ricard 
442b413b0cbSDuoming Zhou 	cancel_work_sync(&info->se_info.timeout_work);
4431c54795dSChristophe Ricard 	info->se_info.bwi_active = false;
4441c54795dSChristophe Ricard 	info->se_info.se_active = false;
4451c54795dSChristophe Ricard }
4461c54795dSChristophe Ricard EXPORT_SYMBOL(st21nfca_se_deinit);
447