1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright (C) 2011  Intel Corporation. All rights reserved.
4  */
5 
6 #ifndef __NET_HCI_H
7 #define __NET_HCI_H
8 
9 #include <linux/skbuff.h>
10 
11 #include <net/nfc/nfc.h>
12 
13 struct nfc_hci_dev;
14 
15 struct nfc_hci_ops {
16 	int (*open) (struct nfc_hci_dev *hdev);
17 	void (*close) (struct nfc_hci_dev *hdev);
18 	int (*load_session) (struct nfc_hci_dev *hdev);
19 	int (*hci_ready) (struct nfc_hci_dev *hdev);
20 	/*
21 	 * xmit must always send the complete buffer before
22 	 * returning. Returned result must be 0 for success
23 	 * or negative for failure.
24 	 */
25 	int (*xmit) (struct nfc_hci_dev *hdev, struct sk_buff *skb);
26 	int (*start_poll) (struct nfc_hci_dev *hdev,
27 			   u32 im_protocols, u32 tm_protocols);
28 	void (*stop_poll) (struct nfc_hci_dev *hdev);
29 	int (*dep_link_up)(struct nfc_hci_dev *hdev, struct nfc_target *target,
30 			   u8 comm_mode, u8 *gb, size_t gb_len);
31 	int (*dep_link_down)(struct nfc_hci_dev *hdev);
32 	int (*target_from_gate) (struct nfc_hci_dev *hdev, u8 gate,
33 				 struct nfc_target *target);
34 	int (*complete_target_discovered) (struct nfc_hci_dev *hdev, u8 gate,
35 					   struct nfc_target *target);
36 	int (*im_transceive) (struct nfc_hci_dev *hdev,
37 			      struct nfc_target *target, struct sk_buff *skb,
38 			      data_exchange_cb_t cb, void *cb_context);
39 	int (*tm_send)(struct nfc_hci_dev *hdev, struct sk_buff *skb);
40 	int (*check_presence)(struct nfc_hci_dev *hdev,
41 			      struct nfc_target *target);
42 	int (*event_received)(struct nfc_hci_dev *hdev, u8 pipe, u8 event,
43 			      struct sk_buff *skb);
44 	void (*cmd_received)(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
45 			    struct sk_buff *skb);
46 	int (*fw_download)(struct nfc_hci_dev *hdev, const char *firmware_name);
47 	int (*discover_se)(struct nfc_hci_dev *dev);
48 	int (*enable_se)(struct nfc_hci_dev *dev, u32 se_idx);
49 	int (*disable_se)(struct nfc_hci_dev *dev, u32 se_idx);
50 	int (*se_io)(struct nfc_hci_dev *dev, u32 se_idx,
51 		      u8 *apdu, size_t apdu_length,
52 		      se_io_cb_t cb, void *cb_context);
53 };
54 
55 /* Pipes */
56 #define NFC_HCI_DO_NOT_CREATE_PIPE	0x81
57 #define NFC_HCI_INVALID_PIPE	0x80
58 #define NFC_HCI_INVALID_GATE	0xFF
59 #define NFC_HCI_INVALID_HOST	0x80
60 #define NFC_HCI_LINK_MGMT_PIPE	0x00
61 #define NFC_HCI_ADMIN_PIPE	0x01
62 
63 struct nfc_hci_gate {
64 	u8 gate;
65 	u8 pipe;
66 };
67 
68 struct nfc_hci_pipe {
69 	u8 gate;
70 	u8 dest_host;
71 };
72 
73 #define NFC_HCI_MAX_CUSTOM_GATES	50
74 /*
75  * According to specification 102 622 chapter 4.4 Pipes,
76  * the pipe identifier is 7 bits long.
77  */
78 #define NFC_HCI_MAX_PIPES		128
79 struct nfc_hci_init_data {
80 	u8 gate_count;
81 	struct nfc_hci_gate gates[NFC_HCI_MAX_CUSTOM_GATES];
82 	char session_id[9];
83 };
84 
85 typedef int (*xmit) (struct sk_buff *skb, void *cb_data);
86 
87 #define NFC_HCI_MAX_GATES		256
88 
89 /*
90  * These values can be specified by a driver to indicate it requires some
91  * adaptation of the HCI standard.
92  *
93  * NFC_HCI_QUIRK_SHORT_CLEAR - send HCI_ADM_CLEAR_ALL_PIPE cmd with no params
94  */
95 enum {
96 	NFC_HCI_QUIRK_SHORT_CLEAR	= 0,
97 };
98 
99 struct nfc_hci_dev {
100 	struct nfc_dev *ndev;
101 
102 	u32 max_data_link_payload;
103 
104 	bool shutting_down;
105 
106 	struct mutex msg_tx_mutex;
107 
108 	struct list_head msg_tx_queue;
109 
110 	struct work_struct msg_tx_work;
111 
112 	struct timer_list cmd_timer;
113 	struct hci_msg *cmd_pending_msg;
114 
115 	struct sk_buff_head rx_hcp_frags;
116 
117 	struct work_struct msg_rx_work;
118 
119 	struct sk_buff_head msg_rx_queue;
120 
121 	struct nfc_hci_ops *ops;
122 
123 	struct nfc_llc *llc;
124 
125 	struct nfc_hci_init_data init_data;
126 
127 	void *clientdata;
128 
129 	u8 gate2pipe[NFC_HCI_MAX_GATES];
130 	struct nfc_hci_pipe pipes[NFC_HCI_MAX_PIPES];
131 
132 	u8 sw_romlib;
133 	u8 sw_patch;
134 	u8 sw_flashlib_major;
135 	u8 sw_flashlib_minor;
136 
137 	u8 hw_derivative;
138 	u8 hw_version;
139 	u8 hw_mpw;
140 	u8 hw_software;
141 	u8 hw_bsid;
142 
143 	int async_cb_type;
144 	data_exchange_cb_t async_cb;
145 	void *async_cb_context;
146 
147 	u8 *gb;
148 	size_t gb_len;
149 
150 	unsigned long quirks;
151 };
152 
153 /* hci device allocation */
154 struct nfc_hci_dev *nfc_hci_allocate_device(struct nfc_hci_ops *ops,
155 					    struct nfc_hci_init_data *init_data,
156 					    unsigned long quirks,
157 					    u32 protocols,
158 					    const char *llc_name,
159 					    int tx_headroom,
160 					    int tx_tailroom,
161 					    int max_link_payload);
162 void nfc_hci_free_device(struct nfc_hci_dev *hdev);
163 
164 int nfc_hci_register_device(struct nfc_hci_dev *hdev);
165 void nfc_hci_unregister_device(struct nfc_hci_dev *hdev);
166 
167 void nfc_hci_set_clientdata(struct nfc_hci_dev *hdev, void *clientdata);
168 void *nfc_hci_get_clientdata(struct nfc_hci_dev *hdev);
169 
nfc_hci_set_vendor_cmds(struct nfc_hci_dev * hdev,struct nfc_vendor_cmd * cmds,int n_cmds)170 static inline int nfc_hci_set_vendor_cmds(struct nfc_hci_dev *hdev,
171 					  struct nfc_vendor_cmd *cmds,
172 					  int n_cmds)
173 {
174 	return nfc_set_vendor_cmds(hdev->ndev, cmds, n_cmds);
175 }
176 
177 void nfc_hci_driver_failure(struct nfc_hci_dev *hdev, int err);
178 
179 int nfc_hci_result_to_errno(u8 result);
180 void nfc_hci_reset_pipes(struct nfc_hci_dev *dev);
181 void nfc_hci_reset_pipes_per_host(struct nfc_hci_dev *hdev, u8 host);
182 
183 /* Host IDs */
184 #define NFC_HCI_HOST_CONTROLLER_ID	0x00
185 #define NFC_HCI_TERMINAL_HOST_ID	0x01
186 #define NFC_HCI_UICC_HOST_ID		0x02
187 
188 /* Host Controller Gates and registry indexes */
189 #define NFC_HCI_ADMIN_GATE 0x00
190 #define NFC_HCI_ADMIN_SESSION_IDENTITY	0x01
191 #define NFC_HCI_ADMIN_MAX_PIPE		0x02
192 #define NFC_HCI_ADMIN_WHITELIST		0x03
193 #define NFC_HCI_ADMIN_HOST_LIST		0x04
194 
195 #define NFC_HCI_LOOPBACK_GATE		0x04
196 
197 #define NFC_HCI_ID_MGMT_GATE		0x05
198 #define NFC_HCI_ID_MGMT_VERSION_SW	0x01
199 #define NFC_HCI_ID_MGMT_VERSION_HW	0x03
200 #define NFC_HCI_ID_MGMT_VENDOR_NAME	0x04
201 #define NFC_HCI_ID_MGMT_MODEL_ID	0x05
202 #define NFC_HCI_ID_MGMT_HCI_VERSION	0x02
203 #define NFC_HCI_ID_MGMT_GATES_LIST	0x06
204 
205 #define NFC_HCI_LINK_MGMT_GATE		0x06
206 #define NFC_HCI_LINK_MGMT_REC_ERROR	0x01
207 
208 #define NFC_HCI_RF_READER_B_GATE			0x11
209 #define NFC_HCI_RF_READER_B_PUPI			0x03
210 #define NFC_HCI_RF_READER_B_APPLICATION_DATA		0x04
211 #define NFC_HCI_RF_READER_B_AFI				0x02
212 #define NFC_HCI_RF_READER_B_HIGHER_LAYER_RESPONSE	0x01
213 #define NFC_HCI_RF_READER_B_HIGHER_LAYER_DATA		0x05
214 
215 #define NFC_HCI_RF_READER_A_GATE		0x13
216 #define NFC_HCI_RF_READER_A_UID			0x02
217 #define NFC_HCI_RF_READER_A_ATQA		0x04
218 #define NFC_HCI_RF_READER_A_APPLICATION_DATA	0x05
219 #define NFC_HCI_RF_READER_A_SAK			0x03
220 #define NFC_HCI_RF_READER_A_FWI_SFGT		0x06
221 #define NFC_HCI_RF_READER_A_DATARATE_MAX	0x01
222 
223 #define NFC_HCI_TYPE_A_SEL_PROT(x)		(((x) & 0x60) >> 5)
224 #define NFC_HCI_TYPE_A_SEL_PROT_MIFARE		0
225 #define NFC_HCI_TYPE_A_SEL_PROT_ISO14443	1
226 #define NFC_HCI_TYPE_A_SEL_PROT_DEP		2
227 #define NFC_HCI_TYPE_A_SEL_PROT_ISO14443_DEP	3
228 
229 /* Generic events */
230 #define NFC_HCI_EVT_HCI_END_OF_OPERATION	0x01
231 #define NFC_HCI_EVT_POST_DATA			0x02
232 #define NFC_HCI_EVT_HOT_PLUG			0x03
233 
234 /* Generic commands */
235 #define NFC_HCI_ANY_SET_PARAMETER	0x01
236 #define NFC_HCI_ANY_GET_PARAMETER	0x02
237 #define NFC_HCI_ANY_OPEN_PIPE		0x03
238 #define NFC_HCI_ANY_CLOSE_PIPE		0x04
239 
240 /* Reader RF gates events */
241 #define NFC_HCI_EVT_READER_REQUESTED	0x10
242 #define NFC_HCI_EVT_END_OPERATION	0x11
243 
244 /* Reader Application gate events */
245 #define NFC_HCI_EVT_TARGET_DISCOVERED	0x10
246 
247 /* receiving messages from lower layer */
248 void nfc_hci_resp_received(struct nfc_hci_dev *hdev, u8 result,
249 			   struct sk_buff *skb);
250 void nfc_hci_cmd_received(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
251 			  struct sk_buff *skb);
252 void nfc_hci_event_received(struct nfc_hci_dev *hdev, u8 pipe, u8 event,
253 			    struct sk_buff *skb);
254 void nfc_hci_recv_frame(struct nfc_hci_dev *hdev, struct sk_buff *skb);
255 
256 /* connecting to gates and sending hci instructions */
257 int nfc_hci_connect_gate(struct nfc_hci_dev *hdev, u8 dest_host, u8 dest_gate,
258 			 u8 pipe);
259 int nfc_hci_disconnect_gate(struct nfc_hci_dev *hdev, u8 gate);
260 int nfc_hci_disconnect_all_gates(struct nfc_hci_dev *hdev);
261 int nfc_hci_get_param(struct nfc_hci_dev *hdev, u8 gate, u8 idx,
262 		      struct sk_buff **skb);
263 int nfc_hci_set_param(struct nfc_hci_dev *hdev, u8 gate, u8 idx,
264 		      const u8 *param, size_t param_len);
265 int nfc_hci_send_cmd(struct nfc_hci_dev *hdev, u8 gate, u8 cmd,
266 		     const u8 *param, size_t param_len, struct sk_buff **skb);
267 int nfc_hci_send_cmd_async(struct nfc_hci_dev *hdev, u8 gate, u8 cmd,
268 			   const u8 *param, size_t param_len,
269 			   data_exchange_cb_t cb, void *cb_context);
270 int nfc_hci_send_event(struct nfc_hci_dev *hdev, u8 gate, u8 event,
271 		       const u8 *param, size_t param_len);
272 int nfc_hci_target_discovered(struct nfc_hci_dev *hdev, u8 gate);
273 u32 nfc_hci_sak_to_protocol(u8 sak);
274 
275 #endif /* __NET_HCI_H */
276