xref: /openbmc/linux/include/net/nfc/digital.h (revision 60772e48)
1 /*
2  * NFC Digital Protocol stack
3  * Copyright (c) 2013, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  */
15 
16 #ifndef __NFC_DIGITAL_H
17 #define __NFC_DIGITAL_H
18 
19 #include <linux/skbuff.h>
20 #include <net/nfc/nfc.h>
21 
22 /**
23  * Configuration types for in_configure_hw and tg_configure_hw.
24  */
25 enum {
26 	NFC_DIGITAL_CONFIG_RF_TECH = 0,
27 	NFC_DIGITAL_CONFIG_FRAMING,
28 };
29 
30 /**
31  * RF technology values passed as param argument to in_configure_hw and
32  * tg_configure_hw for NFC_DIGITAL_CONFIG_RF_TECH configuration type.
33  */
34 enum {
35 	NFC_DIGITAL_RF_TECH_106A = 0,
36 	NFC_DIGITAL_RF_TECH_212F,
37 	NFC_DIGITAL_RF_TECH_424F,
38 	NFC_DIGITAL_RF_TECH_ISO15693,
39 	NFC_DIGITAL_RF_TECH_106B,
40 
41 	NFC_DIGITAL_RF_TECH_LAST,
42 };
43 
44 /**
45  * Framing configuration passed as param argument to in_configure_hw and
46  * tg_configure_hw for NFC_DIGITAL_CONFIG_FRAMING configuration type.
47  */
48 enum {
49 	NFC_DIGITAL_FRAMING_NFCA_SHORT = 0,
50 	NFC_DIGITAL_FRAMING_NFCA_STANDARD,
51 	NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A,
52 	NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE,
53 
54 	NFC_DIGITAL_FRAMING_NFCA_T1T,
55 	NFC_DIGITAL_FRAMING_NFCA_T2T,
56 	NFC_DIGITAL_FRAMING_NFCA_T4T,
57 	NFC_DIGITAL_FRAMING_NFCA_NFC_DEP,
58 
59 	NFC_DIGITAL_FRAMING_NFCF,
60 	NFC_DIGITAL_FRAMING_NFCF_T3T,
61 	NFC_DIGITAL_FRAMING_NFCF_NFC_DEP,
62 	NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED,
63 
64 	NFC_DIGITAL_FRAMING_ISO15693_INVENTORY,
65 	NFC_DIGITAL_FRAMING_ISO15693_T5T,
66 
67 	NFC_DIGITAL_FRAMING_NFCB,
68 	NFC_DIGITAL_FRAMING_NFCB_T4T,
69 
70 	NFC_DIGITAL_FRAMING_LAST,
71 };
72 
73 #define DIGITAL_MDAA_NFCID1_SIZE 3
74 
75 struct digital_tg_mdaa_params {
76 	u16 sens_res;
77 	u8 nfcid1[DIGITAL_MDAA_NFCID1_SIZE];
78 	u8 sel_res;
79 
80 	u8 nfcid2[NFC_NFCID2_MAXSIZE];
81 	u16 sc;
82 };
83 
84 struct nfc_digital_dev;
85 
86 /**
87  * nfc_digital_cmd_complete_t - Definition of command result callback
88  *
89  * @ddev: nfc_digital_device ref
90  * @arg: user data
91  * @resp: response data
92  *
93  * resp pointer can be an error code and will be checked with IS_ERR() macro.
94  * The callback is responsible for freeing resp sk_buff.
95  */
96 typedef void (*nfc_digital_cmd_complete_t)(struct nfc_digital_dev *ddev,
97 					   void *arg, struct sk_buff *resp);
98 
99 /**
100  * Device side NFC Digital operations
101  *
102  * Initiator mode:
103  * @in_configure_hw: Hardware configuration for RF technology and communication
104  *	framing in initiator mode. This is a synchronous function.
105  * @in_send_cmd: Initiator mode data exchange using RF technology and framing
106  *	previously set with in_configure_hw. The peer response is returned
107  *	through callback cb. If an io error occurs or the peer didn't reply
108  *	within the specified timeout (ms), the error code is passed back through
109  *	the resp pointer. This is an asynchronous function.
110  *
111  * Target mode: Only NFC-DEP protocol is supported in target mode.
112  * @tg_configure_hw: Hardware configuration for RF technology and communication
113  *	framing in target mode. This is a synchronous function.
114  * @tg_send_cmd: Target mode data exchange using RF technology and framing
115  *	previously set with tg_configure_hw. The peer next command is returned
116  *	through callback cb. If an io error occurs or the peer didn't reply
117  *	within the specified timeout (ms), the error code is passed back through
118  *	the resp pointer. This is an asynchronous function.
119  * @tg_listen: Put the device in listen mode waiting for data from the peer
120  *	device. This is an asynchronous function.
121  * @tg_listen_mdaa: If supported, put the device in automatic listen mode with
122  *	mode detection and automatic anti-collision. In this mode, the device
123  *	automatically detects the RF technology and executes the anti-collision
124  *	detection using the command responses specified in mdaa_params. The
125  *	mdaa_params structure contains SENS_RES, NFCID1, and SEL_RES for 106A RF
126  *	tech. NFCID2 and system code (sc) for 212F and 424F. The driver returns
127  *	the NFC-DEP ATR_REQ command through cb. The digital stack deducts the RF
128  *	tech by analyzing the SoD of the frame containing the ATR_REQ command.
129  *	This is an asynchronous function.
130  * @tg_listen_md: If supported, put the device in automatic listen mode with
131  *	mode detection but without automatic anti-collision. In this mode, the
132  *	device automatically detects the RF technology.  What the actual
133  *	RF technology is can be retrieved by calling @tg_get_rf_tech.
134  *	The digital stack will then perform the appropriate anti-collision
135  *	sequence.  This is an asynchronous function.
136  * @tg_get_rf_tech: Required when @tg_listen_md is supported, unused otherwise.
137  *	Return the RF Technology that was detected by the @tg_listen_md call.
138  *	This is a synchronous function.
139  *
140  * @switch_rf: Turns device radio on or off. The stack does not call explicitly
141  *	switch_rf to turn the radio on. A call to in|tg_configure_hw must turn
142  *	the device radio on.
143  * @abort_cmd: Discard the last sent command.
144  *
145  * Notes: Asynchronous functions have a timeout parameter. It is the driver
146  *	responsibility to call the digital stack back through the
147  *	nfc_digital_cmd_complete_t callback when no RF respsonse has been
148  *	received within the specified time (in milliseconds). In that case the
149  *	driver must set the resp sk_buff to ERR_PTR(-ETIMEDOUT).
150  *	Since the digital stack serializes commands to be sent, it's mandatory
151  *	for the driver to handle the timeout correctly. Otherwise the stack
152  *	would not be able to send new commands, waiting for the reply of the
153  *	current one.
154  */
155 struct nfc_digital_ops {
156 	int (*in_configure_hw)(struct nfc_digital_dev *ddev, int type,
157 			       int param);
158 	int (*in_send_cmd)(struct nfc_digital_dev *ddev, struct sk_buff *skb,
159 			   u16 timeout, nfc_digital_cmd_complete_t cb,
160 			   void *arg);
161 
162 	int (*tg_configure_hw)(struct nfc_digital_dev *ddev, int type,
163 			       int param);
164 	int (*tg_send_cmd)(struct nfc_digital_dev *ddev, struct sk_buff *skb,
165 			   u16 timeout, nfc_digital_cmd_complete_t cb,
166 			   void *arg);
167 	int (*tg_listen)(struct nfc_digital_dev *ddev, u16 timeout,
168 			 nfc_digital_cmd_complete_t cb, void *arg);
169 	int (*tg_listen_mdaa)(struct nfc_digital_dev *ddev,
170 			      struct digital_tg_mdaa_params *mdaa_params,
171 			      u16 timeout, nfc_digital_cmd_complete_t cb,
172 			      void *arg);
173 	int (*tg_listen_md)(struct nfc_digital_dev *ddev, u16 timeout,
174 			    nfc_digital_cmd_complete_t cb, void *arg);
175 	int (*tg_get_rf_tech)(struct nfc_digital_dev *ddev, u8 *rf_tech);
176 
177 	int (*switch_rf)(struct nfc_digital_dev *ddev, bool on);
178 	void (*abort_cmd)(struct nfc_digital_dev *ddev);
179 };
180 
181 #define NFC_DIGITAL_POLL_MODE_COUNT_MAX	6 /* 106A, 212F, and 424F in & tg */
182 
183 typedef int (*digital_poll_t)(struct nfc_digital_dev *ddev, u8 rf_tech);
184 
185 struct digital_poll_tech {
186 	u8 rf_tech;
187 	digital_poll_t poll_func;
188 };
189 
190 /**
191  * Driver capabilities - bit mask made of the following values
192  *
193  * @NFC_DIGITAL_DRV_CAPS_IN_CRC: The driver handles CRC calculation in initiator
194  *	mode.
195  * @NFC_DIGITAL_DRV_CAPS_TG_CRC: The driver handles CRC calculation in target
196  *	mode.
197  */
198 #define NFC_DIGITAL_DRV_CAPS_IN_CRC	0x0001
199 #define NFC_DIGITAL_DRV_CAPS_TG_CRC	0x0002
200 
201 struct nfc_digital_dev {
202 	struct nfc_dev *nfc_dev;
203 	struct nfc_digital_ops *ops;
204 
205 	u32 protocols;
206 
207 	int tx_headroom;
208 	int tx_tailroom;
209 
210 	u32 driver_capabilities;
211 	void *driver_data;
212 
213 	struct digital_poll_tech poll_techs[NFC_DIGITAL_POLL_MODE_COUNT_MAX];
214 	u8 poll_tech_count;
215 	u8 poll_tech_index;
216 	struct mutex poll_lock;
217 
218 	struct work_struct cmd_work;
219 	struct work_struct cmd_complete_work;
220 	struct list_head cmd_queue;
221 	struct mutex cmd_lock;
222 
223 	struct delayed_work poll_work;
224 
225 	u8 curr_protocol;
226 	u8 curr_rf_tech;
227 	u8 curr_nfc_dep_pni;
228 	u8 did;
229 	u16 dep_rwt;
230 
231 	u8 local_payload_max;
232 	u8 remote_payload_max;
233 
234 	struct sk_buff *chaining_skb;
235 	struct digital_data_exch *data_exch;
236 
237 	int atn_count;
238 	int nack_count;
239 
240 	struct sk_buff *saved_skb;
241 
242 	u16 target_fsc;
243 
244 	int (*skb_check_crc)(struct sk_buff *skb);
245 	void (*skb_add_crc)(struct sk_buff *skb);
246 };
247 
248 struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops,
249 						    __u32 supported_protocols,
250 						    __u32 driver_capabilities,
251 						    int tx_headroom,
252 						    int tx_tailroom);
253 void nfc_digital_free_device(struct nfc_digital_dev *ndev);
254 int nfc_digital_register_device(struct nfc_digital_dev *ndev);
255 void nfc_digital_unregister_device(struct nfc_digital_dev *ndev);
256 
257 static inline void nfc_digital_set_parent_dev(struct nfc_digital_dev *ndev,
258 					      struct device *dev)
259 {
260 	nfc_set_parent_dev(ndev->nfc_dev, dev);
261 }
262 
263 static inline void nfc_digital_set_drvdata(struct nfc_digital_dev *dev,
264 					   void *data)
265 {
266 	dev->driver_data = data;
267 }
268 
269 static inline void *nfc_digital_get_drvdata(struct nfc_digital_dev *dev)
270 {
271 	return dev->driver_data;
272 }
273 
274 #endif /* __NFC_DIGITAL_H */
275