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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2012  Intel Corporation. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #define pr_fmt(fmt) "hci: %s: " fmt, __func__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <net/nfc/hci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include "hci.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #define MAX_FWI 4949
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static int nfc_hci_execute_cmd_async(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 			       const u8 *param, size_t param_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 			       data_exchange_cb_t cb, void *cb_context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	pr_debug("exec cmd async through pipe=%d, cmd=%d, plen=%zd\n", pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 		 cmd, param_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	/* TODO: Define hci cmd execution delay. Should it be the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	 * for all commands?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	return nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_COMMAND, cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 				      param, param_len, cb, cb_context, MAX_FWI);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * HCI command execution completion callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * err will be a standard linux error (may be converted from HCI response)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * skb contains the response data and must be disposed, or may be NULL if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * an error occured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static void nfc_hci_execute_cb(void *context, struct sk_buff *skb, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct hcp_exec_waiter *hcp_ew = (struct hcp_exec_waiter *)context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	pr_debug("HCI Cmd completed with result=%d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	hcp_ew->exec_result = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	if (hcp_ew->exec_result == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		hcp_ew->result_skb = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	hcp_ew->exec_complete = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	wake_up(hcp_ew->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static int nfc_hci_execute_cmd(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 			       const u8 *param, size_t param_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 			       struct sk_buff **skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(ew_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	struct hcp_exec_waiter hcp_ew;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	hcp_ew.wq = &ew_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	hcp_ew.exec_complete = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	hcp_ew.result_skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	pr_debug("exec cmd sync through pipe=%d, cmd=%d, plen=%zd\n", pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		 cmd, param_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	/* TODO: Define hci cmd execution delay. Should it be the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	 * for all commands?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	hcp_ew.exec_result = nfc_hci_hcp_message_tx(hdev, pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 						    NFC_HCI_HCP_COMMAND, cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 						    param, param_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 						    nfc_hci_execute_cb, &hcp_ew,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 						    MAX_FWI);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (hcp_ew.exec_result < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		return hcp_ew.exec_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	wait_event(ew_wq, hcp_ew.exec_complete == true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (hcp_ew.exec_result == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		if (skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			*skb = hcp_ew.result_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			kfree_skb(hcp_ew.result_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	return hcp_ew.exec_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) int nfc_hci_send_event(struct nfc_hci_dev *hdev, u8 gate, u8 event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		       const u8 *param, size_t param_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	u8 pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	pr_debug("%d to gate %d\n", event, gate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	pipe = hdev->gate2pipe[gate];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (pipe == NFC_HCI_INVALID_PIPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		return -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	return nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_EVENT, event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 				      param, param_len, NULL, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) EXPORT_SYMBOL(nfc_hci_send_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)  * Execute an hci command sent to gate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)  * skb will contain response data if success. skb can be NULL if you are not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)  * interested by the response.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) int nfc_hci_send_cmd(struct nfc_hci_dev *hdev, u8 gate, u8 cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		     const u8 *param, size_t param_len, struct sk_buff **skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	u8 pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	pr_debug("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	pipe = hdev->gate2pipe[gate];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (pipe == NFC_HCI_INVALID_PIPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		return -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	return nfc_hci_execute_cmd(hdev, pipe, cmd, param, param_len, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) EXPORT_SYMBOL(nfc_hci_send_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) int nfc_hci_send_cmd_async(struct nfc_hci_dev *hdev, u8 gate, u8 cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			   const u8 *param, size_t param_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			   data_exchange_cb_t cb, void *cb_context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	u8 pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	pr_debug("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	pipe = hdev->gate2pipe[gate];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (pipe == NFC_HCI_INVALID_PIPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		return -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	return nfc_hci_execute_cmd_async(hdev, pipe, cmd, param, param_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 					 cb, cb_context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) EXPORT_SYMBOL(nfc_hci_send_cmd_async);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) int nfc_hci_set_param(struct nfc_hci_dev *hdev, u8 gate, u8 idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		      const u8 *param, size_t param_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	u8 *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	/* TODO ELa: reg idx must be inserted before param, but we don't want
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	 * to ask the caller to do it to keep a simpler API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	 * For now, just create a new temporary param buffer. This is far from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	 * optimal though, and the plan is to modify APIs to pass idx down to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	 * nfc_hci_hcp_message_tx where the frame is actually built, thereby
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	 * eliminating the need for the temp allocation-copy here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	pr_debug("idx=%d to gate %d\n", idx, gate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	tmp = kmalloc(1 + param_len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	if (tmp == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	*tmp = idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	memcpy(tmp + 1, param, param_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	r = nfc_hci_send_cmd(hdev, gate, NFC_HCI_ANY_SET_PARAMETER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			     tmp, param_len + 1, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	kfree(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) EXPORT_SYMBOL(nfc_hci_set_param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) int nfc_hci_get_param(struct nfc_hci_dev *hdev, u8 gate, u8 idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		      struct sk_buff **skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	pr_debug("gate=%d regidx=%d\n", gate, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	return nfc_hci_send_cmd(hdev, gate, NFC_HCI_ANY_GET_PARAMETER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 				&idx, 1, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) EXPORT_SYMBOL(nfc_hci_get_param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static int nfc_hci_open_pipe(struct nfc_hci_dev *hdev, u8 pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	pr_debug("pipe=%d\n", pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	r = nfc_hci_execute_cmd(hdev, pipe, NFC_HCI_ANY_OPEN_PIPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 				NULL, 0, &skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	if (r == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		/* dest host other than host controller will send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		 * number of pipes already open on this gate before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		 * execution. The number can be found in skb->data[0]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static int nfc_hci_close_pipe(struct nfc_hci_dev *hdev, u8 pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	pr_debug("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	return nfc_hci_execute_cmd(hdev, pipe, NFC_HCI_ANY_CLOSE_PIPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 				   NULL, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static u8 nfc_hci_create_pipe(struct nfc_hci_dev *hdev, u8 dest_host,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			      u8 dest_gate, int *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	struct hci_create_pipe_params params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	struct hci_create_pipe_resp *resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	u8 pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	pr_debug("gate=%d\n", dest_gate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	params.src_gate = NFC_HCI_ADMIN_GATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	params.dest_host = dest_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	params.dest_gate = dest_gate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	*result = nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 				      NFC_HCI_ADM_CREATE_PIPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 				      (u8 *) &params, sizeof(params), &skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	if (*result < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		return NFC_HCI_INVALID_PIPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	resp = (struct hci_create_pipe_resp *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	pipe = resp->pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	pr_debug("pipe created=%d\n", pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	return pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static int nfc_hci_delete_pipe(struct nfc_hci_dev *hdev, u8 pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	pr_debug("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	return nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 				   NFC_HCI_ADM_DELETE_PIPE, &pipe, 1, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static int nfc_hci_clear_all_pipes(struct nfc_hci_dev *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	u8 param[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	size_t param_len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	/* TODO: Find out what the identity reference data is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	 * and fill param with it. HCI spec 6.1.3.5 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	pr_debug("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if (test_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &hdev->quirks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		param_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	return nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 				   NFC_HCI_ADM_CLEAR_ALL_PIPE, param, param_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 				   NULL);
^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) int nfc_hci_disconnect_gate(struct nfc_hci_dev *hdev, u8 gate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	u8 pipe = hdev->gate2pipe[gate];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	pr_debug("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	if (pipe == NFC_HCI_INVALID_PIPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		return -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	r = nfc_hci_close_pipe(hdev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	if (pipe != NFC_HCI_LINK_MGMT_PIPE && pipe != NFC_HCI_ADMIN_PIPE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		r = nfc_hci_delete_pipe(hdev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	hdev->gate2pipe[gate] = NFC_HCI_INVALID_PIPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) EXPORT_SYMBOL(nfc_hci_disconnect_gate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) int nfc_hci_disconnect_all_gates(struct nfc_hci_dev *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	pr_debug("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	r = nfc_hci_clear_all_pipes(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	nfc_hci_reset_pipes(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) EXPORT_SYMBOL(nfc_hci_disconnect_all_gates);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) int nfc_hci_connect_gate(struct nfc_hci_dev *hdev, u8 dest_host, u8 dest_gate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			 u8 pipe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	bool pipe_created = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	pr_debug("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	if (pipe == NFC_HCI_DO_NOT_CREATE_PIPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	if (hdev->gate2pipe[dest_gate] != NFC_HCI_INVALID_PIPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		return -EADDRINUSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	if (pipe != NFC_HCI_INVALID_PIPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		goto open_pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	switch (dest_gate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	case NFC_HCI_LINK_MGMT_GATE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		pipe = NFC_HCI_LINK_MGMT_PIPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	case NFC_HCI_ADMIN_GATE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		pipe = NFC_HCI_ADMIN_PIPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		pipe = nfc_hci_create_pipe(hdev, dest_host, dest_gate, &r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		if (pipe == NFC_HCI_INVALID_PIPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		pipe_created = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) open_pipe:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	r = nfc_hci_open_pipe(hdev, pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		if (pipe_created)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			if (nfc_hci_delete_pipe(hdev, pipe) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 				/* TODO: Cannot clean by deleting pipe...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 				 * -> inconsistent state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	hdev->pipes[pipe].gate = dest_gate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	hdev->pipes[pipe].dest_host = dest_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	hdev->gate2pipe[dest_gate] = pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) EXPORT_SYMBOL(nfc_hci_connect_gate);