18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2011  Intel Corporation. All rights reserved.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#ifndef __NET_HCI_H
78c2ecf20Sopenharmony_ci#define __NET_HCI_H
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <net/nfc/nfc.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistruct nfc_hci_dev;
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistruct nfc_hci_ops {
168c2ecf20Sopenharmony_ci	int (*open) (struct nfc_hci_dev *hdev);
178c2ecf20Sopenharmony_ci	void (*close) (struct nfc_hci_dev *hdev);
188c2ecf20Sopenharmony_ci	int (*load_session) (struct nfc_hci_dev *hdev);
198c2ecf20Sopenharmony_ci	int (*hci_ready) (struct nfc_hci_dev *hdev);
208c2ecf20Sopenharmony_ci	/*
218c2ecf20Sopenharmony_ci	 * xmit must always send the complete buffer before
228c2ecf20Sopenharmony_ci	 * returning. Returned result must be 0 for success
238c2ecf20Sopenharmony_ci	 * or negative for failure.
248c2ecf20Sopenharmony_ci	 */
258c2ecf20Sopenharmony_ci	int (*xmit) (struct nfc_hci_dev *hdev, struct sk_buff *skb);
268c2ecf20Sopenharmony_ci	int (*start_poll) (struct nfc_hci_dev *hdev,
278c2ecf20Sopenharmony_ci			   u32 im_protocols, u32 tm_protocols);
288c2ecf20Sopenharmony_ci	void (*stop_poll) (struct nfc_hci_dev *hdev);
298c2ecf20Sopenharmony_ci	int (*dep_link_up)(struct nfc_hci_dev *hdev, struct nfc_target *target,
308c2ecf20Sopenharmony_ci			   u8 comm_mode, u8 *gb, size_t gb_len);
318c2ecf20Sopenharmony_ci	int (*dep_link_down)(struct nfc_hci_dev *hdev);
328c2ecf20Sopenharmony_ci	int (*target_from_gate) (struct nfc_hci_dev *hdev, u8 gate,
338c2ecf20Sopenharmony_ci				 struct nfc_target *target);
348c2ecf20Sopenharmony_ci	int (*complete_target_discovered) (struct nfc_hci_dev *hdev, u8 gate,
358c2ecf20Sopenharmony_ci					   struct nfc_target *target);
368c2ecf20Sopenharmony_ci	int (*im_transceive) (struct nfc_hci_dev *hdev,
378c2ecf20Sopenharmony_ci			      struct nfc_target *target, struct sk_buff *skb,
388c2ecf20Sopenharmony_ci			      data_exchange_cb_t cb, void *cb_context);
398c2ecf20Sopenharmony_ci	int (*tm_send)(struct nfc_hci_dev *hdev, struct sk_buff *skb);
408c2ecf20Sopenharmony_ci	int (*check_presence)(struct nfc_hci_dev *hdev,
418c2ecf20Sopenharmony_ci			      struct nfc_target *target);
428c2ecf20Sopenharmony_ci	int (*event_received)(struct nfc_hci_dev *hdev, u8 pipe, u8 event,
438c2ecf20Sopenharmony_ci			      struct sk_buff *skb);
448c2ecf20Sopenharmony_ci	void (*cmd_received)(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
458c2ecf20Sopenharmony_ci			    struct sk_buff *skb);
468c2ecf20Sopenharmony_ci	int (*fw_download)(struct nfc_hci_dev *hdev, const char *firmware_name);
478c2ecf20Sopenharmony_ci	int (*discover_se)(struct nfc_hci_dev *dev);
488c2ecf20Sopenharmony_ci	int (*enable_se)(struct nfc_hci_dev *dev, u32 se_idx);
498c2ecf20Sopenharmony_ci	int (*disable_se)(struct nfc_hci_dev *dev, u32 se_idx);
508c2ecf20Sopenharmony_ci	int (*se_io)(struct nfc_hci_dev *dev, u32 se_idx,
518c2ecf20Sopenharmony_ci		      u8 *apdu, size_t apdu_length,
528c2ecf20Sopenharmony_ci		      se_io_cb_t cb, void *cb_context);
538c2ecf20Sopenharmony_ci};
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/* Pipes */
568c2ecf20Sopenharmony_ci#define NFC_HCI_DO_NOT_CREATE_PIPE	0x81
578c2ecf20Sopenharmony_ci#define NFC_HCI_INVALID_PIPE	0x80
588c2ecf20Sopenharmony_ci#define NFC_HCI_INVALID_GATE	0xFF
598c2ecf20Sopenharmony_ci#define NFC_HCI_INVALID_HOST	0x80
608c2ecf20Sopenharmony_ci#define NFC_HCI_LINK_MGMT_PIPE	0x00
618c2ecf20Sopenharmony_ci#define NFC_HCI_ADMIN_PIPE	0x01
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistruct nfc_hci_gate {
648c2ecf20Sopenharmony_ci	u8 gate;
658c2ecf20Sopenharmony_ci	u8 pipe;
668c2ecf20Sopenharmony_ci};
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistruct nfc_hci_pipe {
698c2ecf20Sopenharmony_ci	u8 gate;
708c2ecf20Sopenharmony_ci	u8 dest_host;
718c2ecf20Sopenharmony_ci};
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#define NFC_HCI_MAX_CUSTOM_GATES	50
748c2ecf20Sopenharmony_ci/*
758c2ecf20Sopenharmony_ci * According to specification 102 622 chapter 4.4 Pipes,
768c2ecf20Sopenharmony_ci * the pipe identifier is 7 bits long.
778c2ecf20Sopenharmony_ci */
788c2ecf20Sopenharmony_ci#define NFC_HCI_MAX_PIPES		128
798c2ecf20Sopenharmony_cistruct nfc_hci_init_data {
808c2ecf20Sopenharmony_ci	u8 gate_count;
818c2ecf20Sopenharmony_ci	struct nfc_hci_gate gates[NFC_HCI_MAX_CUSTOM_GATES];
828c2ecf20Sopenharmony_ci	char session_id[9];
838c2ecf20Sopenharmony_ci};
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_citypedef int (*xmit) (struct sk_buff *skb, void *cb_data);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci#define NFC_HCI_MAX_GATES		256
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/*
908c2ecf20Sopenharmony_ci * These values can be specified by a driver to indicate it requires some
918c2ecf20Sopenharmony_ci * adaptation of the HCI standard.
928c2ecf20Sopenharmony_ci *
938c2ecf20Sopenharmony_ci * NFC_HCI_QUIRK_SHORT_CLEAR - send HCI_ADM_CLEAR_ALL_PIPE cmd with no params
948c2ecf20Sopenharmony_ci */
958c2ecf20Sopenharmony_cienum {
968c2ecf20Sopenharmony_ci	NFC_HCI_QUIRK_SHORT_CLEAR	= 0,
978c2ecf20Sopenharmony_ci};
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistruct nfc_hci_dev {
1008c2ecf20Sopenharmony_ci	struct nfc_dev *ndev;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	u32 max_data_link_payload;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	bool shutting_down;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	struct mutex msg_tx_mutex;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	struct list_head msg_tx_queue;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	struct work_struct msg_tx_work;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	struct timer_list cmd_timer;
1138c2ecf20Sopenharmony_ci	struct hci_msg *cmd_pending_msg;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	struct sk_buff_head rx_hcp_frags;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	struct work_struct msg_rx_work;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	struct sk_buff_head msg_rx_queue;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	struct nfc_hci_ops *ops;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	struct nfc_llc *llc;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	struct nfc_hci_init_data init_data;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	void *clientdata;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	u8 gate2pipe[NFC_HCI_MAX_GATES];
1308c2ecf20Sopenharmony_ci	struct nfc_hci_pipe pipes[NFC_HCI_MAX_PIPES];
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	u8 sw_romlib;
1338c2ecf20Sopenharmony_ci	u8 sw_patch;
1348c2ecf20Sopenharmony_ci	u8 sw_flashlib_major;
1358c2ecf20Sopenharmony_ci	u8 sw_flashlib_minor;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	u8 hw_derivative;
1388c2ecf20Sopenharmony_ci	u8 hw_version;
1398c2ecf20Sopenharmony_ci	u8 hw_mpw;
1408c2ecf20Sopenharmony_ci	u8 hw_software;
1418c2ecf20Sopenharmony_ci	u8 hw_bsid;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	int async_cb_type;
1448c2ecf20Sopenharmony_ci	data_exchange_cb_t async_cb;
1458c2ecf20Sopenharmony_ci	void *async_cb_context;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	u8 *gb;
1488c2ecf20Sopenharmony_ci	size_t gb_len;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	unsigned long quirks;
1518c2ecf20Sopenharmony_ci};
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci/* hci device allocation */
1548c2ecf20Sopenharmony_cistruct nfc_hci_dev *nfc_hci_allocate_device(struct nfc_hci_ops *ops,
1558c2ecf20Sopenharmony_ci					    struct nfc_hci_init_data *init_data,
1568c2ecf20Sopenharmony_ci					    unsigned long quirks,
1578c2ecf20Sopenharmony_ci					    u32 protocols,
1588c2ecf20Sopenharmony_ci					    const char *llc_name,
1598c2ecf20Sopenharmony_ci					    int tx_headroom,
1608c2ecf20Sopenharmony_ci					    int tx_tailroom,
1618c2ecf20Sopenharmony_ci					    int max_link_payload);
1628c2ecf20Sopenharmony_civoid nfc_hci_free_device(struct nfc_hci_dev *hdev);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ciint nfc_hci_register_device(struct nfc_hci_dev *hdev);
1658c2ecf20Sopenharmony_civoid nfc_hci_unregister_device(struct nfc_hci_dev *hdev);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_civoid nfc_hci_set_clientdata(struct nfc_hci_dev *hdev, void *clientdata);
1688c2ecf20Sopenharmony_civoid *nfc_hci_get_clientdata(struct nfc_hci_dev *hdev);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic inline int nfc_hci_set_vendor_cmds(struct nfc_hci_dev *hdev,
1718c2ecf20Sopenharmony_ci					  struct nfc_vendor_cmd *cmds,
1728c2ecf20Sopenharmony_ci					  int n_cmds)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	return nfc_set_vendor_cmds(hdev->ndev, cmds, n_cmds);
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_civoid nfc_hci_driver_failure(struct nfc_hci_dev *hdev, int err);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ciint nfc_hci_result_to_errno(u8 result);
1808c2ecf20Sopenharmony_civoid nfc_hci_reset_pipes(struct nfc_hci_dev *dev);
1818c2ecf20Sopenharmony_civoid nfc_hci_reset_pipes_per_host(struct nfc_hci_dev *hdev, u8 host);
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci/* Host IDs */
1848c2ecf20Sopenharmony_ci#define NFC_HCI_HOST_CONTROLLER_ID	0x00
1858c2ecf20Sopenharmony_ci#define NFC_HCI_TERMINAL_HOST_ID	0x01
1868c2ecf20Sopenharmony_ci#define NFC_HCI_UICC_HOST_ID		0x02
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci/* Host Controller Gates and registry indexes */
1898c2ecf20Sopenharmony_ci#define NFC_HCI_ADMIN_GATE 0x00
1908c2ecf20Sopenharmony_ci#define NFC_HCI_ADMIN_SESSION_IDENTITY	0x01
1918c2ecf20Sopenharmony_ci#define NFC_HCI_ADMIN_MAX_PIPE		0x02
1928c2ecf20Sopenharmony_ci#define NFC_HCI_ADMIN_WHITELIST		0x03
1938c2ecf20Sopenharmony_ci#define NFC_HCI_ADMIN_HOST_LIST		0x04
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci#define NFC_HCI_LOOPBACK_GATE		0x04
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci#define NFC_HCI_ID_MGMT_GATE		0x05
1988c2ecf20Sopenharmony_ci#define NFC_HCI_ID_MGMT_VERSION_SW	0x01
1998c2ecf20Sopenharmony_ci#define NFC_HCI_ID_MGMT_VERSION_HW	0x03
2008c2ecf20Sopenharmony_ci#define NFC_HCI_ID_MGMT_VENDOR_NAME	0x04
2018c2ecf20Sopenharmony_ci#define NFC_HCI_ID_MGMT_MODEL_ID	0x05
2028c2ecf20Sopenharmony_ci#define NFC_HCI_ID_MGMT_HCI_VERSION	0x02
2038c2ecf20Sopenharmony_ci#define NFC_HCI_ID_MGMT_GATES_LIST	0x06
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci#define NFC_HCI_LINK_MGMT_GATE		0x06
2068c2ecf20Sopenharmony_ci#define NFC_HCI_LINK_MGMT_REC_ERROR	0x01
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci#define NFC_HCI_RF_READER_B_GATE			0x11
2098c2ecf20Sopenharmony_ci#define NFC_HCI_RF_READER_B_PUPI			0x03
2108c2ecf20Sopenharmony_ci#define NFC_HCI_RF_READER_B_APPLICATION_DATA		0x04
2118c2ecf20Sopenharmony_ci#define NFC_HCI_RF_READER_B_AFI				0x02
2128c2ecf20Sopenharmony_ci#define NFC_HCI_RF_READER_B_HIGHER_LAYER_RESPONSE	0x01
2138c2ecf20Sopenharmony_ci#define NFC_HCI_RF_READER_B_HIGHER_LAYER_DATA		0x05
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci#define NFC_HCI_RF_READER_A_GATE		0x13
2168c2ecf20Sopenharmony_ci#define NFC_HCI_RF_READER_A_UID			0x02
2178c2ecf20Sopenharmony_ci#define NFC_HCI_RF_READER_A_ATQA		0x04
2188c2ecf20Sopenharmony_ci#define NFC_HCI_RF_READER_A_APPLICATION_DATA	0x05
2198c2ecf20Sopenharmony_ci#define NFC_HCI_RF_READER_A_SAK			0x03
2208c2ecf20Sopenharmony_ci#define NFC_HCI_RF_READER_A_FWI_SFGT		0x06
2218c2ecf20Sopenharmony_ci#define NFC_HCI_RF_READER_A_DATARATE_MAX	0x01
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci#define NFC_HCI_TYPE_A_SEL_PROT(x)		(((x) & 0x60) >> 5)
2248c2ecf20Sopenharmony_ci#define NFC_HCI_TYPE_A_SEL_PROT_MIFARE		0
2258c2ecf20Sopenharmony_ci#define NFC_HCI_TYPE_A_SEL_PROT_ISO14443	1
2268c2ecf20Sopenharmony_ci#define NFC_HCI_TYPE_A_SEL_PROT_DEP		2
2278c2ecf20Sopenharmony_ci#define NFC_HCI_TYPE_A_SEL_PROT_ISO14443_DEP	3
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci/* Generic events */
2308c2ecf20Sopenharmony_ci#define NFC_HCI_EVT_HCI_END_OF_OPERATION	0x01
2318c2ecf20Sopenharmony_ci#define NFC_HCI_EVT_POST_DATA			0x02
2328c2ecf20Sopenharmony_ci#define NFC_HCI_EVT_HOT_PLUG			0x03
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci/* Generic commands */
2358c2ecf20Sopenharmony_ci#define NFC_HCI_ANY_SET_PARAMETER	0x01
2368c2ecf20Sopenharmony_ci#define NFC_HCI_ANY_GET_PARAMETER	0x02
2378c2ecf20Sopenharmony_ci#define NFC_HCI_ANY_OPEN_PIPE		0x03
2388c2ecf20Sopenharmony_ci#define NFC_HCI_ANY_CLOSE_PIPE		0x04
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci/* Reader RF gates events */
2418c2ecf20Sopenharmony_ci#define NFC_HCI_EVT_READER_REQUESTED	0x10
2428c2ecf20Sopenharmony_ci#define NFC_HCI_EVT_END_OPERATION	0x11
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci/* Reader Application gate events */
2458c2ecf20Sopenharmony_ci#define NFC_HCI_EVT_TARGET_DISCOVERED	0x10
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci/* receiving messages from lower layer */
2488c2ecf20Sopenharmony_civoid nfc_hci_resp_received(struct nfc_hci_dev *hdev, u8 result,
2498c2ecf20Sopenharmony_ci			   struct sk_buff *skb);
2508c2ecf20Sopenharmony_civoid nfc_hci_cmd_received(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
2518c2ecf20Sopenharmony_ci			  struct sk_buff *skb);
2528c2ecf20Sopenharmony_civoid nfc_hci_event_received(struct nfc_hci_dev *hdev, u8 pipe, u8 event,
2538c2ecf20Sopenharmony_ci			    struct sk_buff *skb);
2548c2ecf20Sopenharmony_civoid nfc_hci_recv_frame(struct nfc_hci_dev *hdev, struct sk_buff *skb);
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci/* connecting to gates and sending hci instructions */
2578c2ecf20Sopenharmony_ciint nfc_hci_connect_gate(struct nfc_hci_dev *hdev, u8 dest_host, u8 dest_gate,
2588c2ecf20Sopenharmony_ci			 u8 pipe);
2598c2ecf20Sopenharmony_ciint nfc_hci_disconnect_gate(struct nfc_hci_dev *hdev, u8 gate);
2608c2ecf20Sopenharmony_ciint nfc_hci_disconnect_all_gates(struct nfc_hci_dev *hdev);
2618c2ecf20Sopenharmony_ciint nfc_hci_get_param(struct nfc_hci_dev *hdev, u8 gate, u8 idx,
2628c2ecf20Sopenharmony_ci		      struct sk_buff **skb);
2638c2ecf20Sopenharmony_ciint nfc_hci_set_param(struct nfc_hci_dev *hdev, u8 gate, u8 idx,
2648c2ecf20Sopenharmony_ci		      const u8 *param, size_t param_len);
2658c2ecf20Sopenharmony_ciint nfc_hci_send_cmd(struct nfc_hci_dev *hdev, u8 gate, u8 cmd,
2668c2ecf20Sopenharmony_ci		     const u8 *param, size_t param_len, struct sk_buff **skb);
2678c2ecf20Sopenharmony_ciint nfc_hci_send_cmd_async(struct nfc_hci_dev *hdev, u8 gate, u8 cmd,
2688c2ecf20Sopenharmony_ci			   const u8 *param, size_t param_len,
2698c2ecf20Sopenharmony_ci			   data_exchange_cb_t cb, void *cb_context);
2708c2ecf20Sopenharmony_ciint nfc_hci_send_event(struct nfc_hci_dev *hdev, u8 gate, u8 event,
2718c2ecf20Sopenharmony_ci		       const u8 *param, size_t param_len);
2728c2ecf20Sopenharmony_ciint nfc_hci_target_discovered(struct nfc_hci_dev *hdev, u8 gate);
2738c2ecf20Sopenharmony_ciu32 nfc_hci_sak_to_protocol(u8 sak);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci#endif /* __NET_HCI_H */
276