xref: /openbmc/linux/include/net/nfc/digital.h (revision 37be287c)
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 
39 	NFC_DIGITAL_RF_TECH_LAST,
40 };
41 
42 /**
43  * Framing configuration passed as param argument to in_configure_hw and
44  * tg_configure_hw for NFC_DIGITAL_CONFIG_FRAMING configuration type.
45  */
46 enum {
47 	NFC_DIGITAL_FRAMING_NFCA_SHORT = 0,
48 	NFC_DIGITAL_FRAMING_NFCA_STANDARD,
49 	NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A,
50 
51 	NFC_DIGITAL_FRAMING_NFCA_T1T,
52 	NFC_DIGITAL_FRAMING_NFCA_T2T,
53 	NFC_DIGITAL_FRAMING_NFCA_NFC_DEP,
54 
55 	NFC_DIGITAL_FRAMING_NFCF,
56 	NFC_DIGITAL_FRAMING_NFCF_T3T,
57 	NFC_DIGITAL_FRAMING_NFCF_NFC_DEP,
58 	NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED,
59 
60 	NFC_DIGITAL_FRAMING_LAST,
61 };
62 
63 #define DIGITAL_MDAA_NFCID1_SIZE 3
64 
65 struct digital_tg_mdaa_params {
66 	u16 sens_res;
67 	u8 nfcid1[DIGITAL_MDAA_NFCID1_SIZE];
68 	u8 sel_res;
69 
70 	u8 nfcid2[NFC_NFCID2_MAXSIZE];
71 	u16 sc;
72 };
73 
74 struct nfc_digital_dev;
75 
76 /**
77  * nfc_digital_cmd_complete_t - Definition of command result callback
78  *
79  * @ddev: nfc_digital_device ref
80  * @arg: user data
81  * @resp: response data
82  *
83  * resp pointer can be an error code and will be checked with IS_ERR() macro.
84  * The callback is responsible for freeing resp sk_buff.
85  */
86 typedef void (*nfc_digital_cmd_complete_t)(struct nfc_digital_dev *ddev,
87 					   void *arg, struct sk_buff *resp);
88 
89 /**
90  * Device side NFC Digital operations
91  *
92  * Initiator mode:
93  * @in_configure_hw: Hardware configuration for RF technology and communication
94  *	framing in initiator mode. This is a synchronous function.
95  * @in_send_cmd: Initiator mode data exchange using RF technology and framing
96  *	previously set with in_configure_hw. The peer response is returned
97  *	through callback cb. If an io error occurs or the peer didn't reply
98  *	within the specified timeout (ms), the error code is passed back through
99  *	the resp pointer. This is an asynchronous function.
100  *
101  * Target mode: Only NFC-DEP protocol is supported in target mode.
102  * @tg_configure_hw: Hardware configuration for RF technology and communication
103  *	framing in target mode. This is a synchronous function.
104  * @tg_send_cmd: Target mode data exchange using RF technology and framing
105  *	previously set with tg_configure_hw. The peer next command is returned
106  *	through callback cb. If an io error occurs or the peer didn't reply
107  *	within the specified timeout (ms), the error code is passed back through
108  *	the resp pointer. This is an asynchronous function.
109  * @tg_listen: Put the device in listen mode waiting for data from the peer
110  *	device. This is an asynchronous function.
111  * @tg_listen_mdaa: If supported, put the device in automatic listen mode with
112  *	mode detection and automatic anti-collision. In this mode, the device
113  *	automatically detects the RF technology and executes the anti-collision
114  *	detection using the command responses specified in mdaa_params. The
115  *	mdaa_params structure contains SENS_RES, NFCID1, and SEL_RES for 106A RF
116  *	tech. NFCID2 and system code (sc) for 212F and 424F. The driver returns
117  *	the NFC-DEP ATR_REQ command through cb. The digital stack deducts the RF
118  *	tech by analyzing the SoD of the frame containing the ATR_REQ command.
119  *	This is an asynchronous function.
120  *
121  * @switch_rf: Turns device radio on or off. The stack does not call explicitly
122  *	switch_rf to turn the radio on. A call to in|tg_configure_hw must turn
123  *	the device radio on.
124  * @abort_cmd: Discard the last sent command.
125  *
126  * Notes: Asynchronous functions have a timeout parameter. It is the driver
127  *	responsibility to call the digital stack back through the
128  *	nfc_digital_cmd_complete_t callback when no RF respsonse has been
129  *	received within the specified time (in milliseconds). In that case the
130  *	driver must set the resp sk_buff to ERR_PTR(-ETIMEDOUT).
131  *	Since the digital stack serializes commands to be sent, it's mandatory
132  *	for the driver to handle the timeout correctly. Otherwise the stack
133  *	would not be able to send new commands, waiting for the reply of the
134  *	current one.
135  */
136 struct nfc_digital_ops {
137 	int (*in_configure_hw)(struct nfc_digital_dev *ddev, int type,
138 			       int param);
139 	int (*in_send_cmd)(struct nfc_digital_dev *ddev, struct sk_buff *skb,
140 			   u16 timeout, nfc_digital_cmd_complete_t cb,
141 			   void *arg);
142 
143 	int (*tg_configure_hw)(struct nfc_digital_dev *ddev, int type,
144 			       int param);
145 	int (*tg_send_cmd)(struct nfc_digital_dev *ddev, struct sk_buff *skb,
146 			   u16 timeout, nfc_digital_cmd_complete_t cb,
147 			   void *arg);
148 	int (*tg_listen)(struct nfc_digital_dev *ddev, u16 timeout,
149 			 nfc_digital_cmd_complete_t cb, void *arg);
150 	int (*tg_listen_mdaa)(struct nfc_digital_dev *ddev,
151 			      struct digital_tg_mdaa_params *mdaa_params,
152 			      u16 timeout, nfc_digital_cmd_complete_t cb,
153 			      void *arg);
154 
155 	int (*switch_rf)(struct nfc_digital_dev *ddev, bool on);
156 	void (*abort_cmd)(struct nfc_digital_dev *ddev);
157 };
158 
159 #define NFC_DIGITAL_POLL_MODE_COUNT_MAX	6 /* 106A, 212F, and 424F in & tg */
160 
161 typedef int (*digital_poll_t)(struct nfc_digital_dev *ddev, u8 rf_tech);
162 
163 struct digital_poll_tech {
164 	u8 rf_tech;
165 	digital_poll_t poll_func;
166 };
167 
168 /**
169  * Driver capabilities - bit mask made of the following values
170  *
171  * @NFC_DIGITAL_DRV_CAPS_IN_CRC: The driver handles CRC calculation in initiator
172  *	mode.
173  * @NFC_DIGITAL_DRV_CAPS_TG_CRC: The driver handles CRC calculation in target
174  *	mode.
175  */
176 #define NFC_DIGITAL_DRV_CAPS_IN_CRC	0x0001
177 #define NFC_DIGITAL_DRV_CAPS_TG_CRC	0x0002
178 
179 struct nfc_digital_dev {
180 	struct nfc_dev *nfc_dev;
181 	struct nfc_digital_ops *ops;
182 
183 	u32 protocols;
184 
185 	int tx_headroom;
186 	int tx_tailroom;
187 
188 	u32 driver_capabilities;
189 	void *driver_data;
190 
191 	struct digital_poll_tech poll_techs[NFC_DIGITAL_POLL_MODE_COUNT_MAX];
192 	u8 poll_tech_count;
193 	u8 poll_tech_index;
194 	struct mutex poll_lock;
195 
196 	struct work_struct cmd_work;
197 	struct work_struct cmd_complete_work;
198 	struct list_head cmd_queue;
199 	struct mutex cmd_lock;
200 
201 	struct work_struct poll_work;
202 
203 	u8 curr_protocol;
204 	u8 curr_rf_tech;
205 	u8 curr_nfc_dep_pni;
206 
207 	int (*skb_check_crc)(struct sk_buff *skb);
208 	void (*skb_add_crc)(struct sk_buff *skb);
209 };
210 
211 struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops,
212 						    __u32 supported_protocols,
213 						    __u32 driver_capabilities,
214 						    int tx_headroom,
215 						    int tx_tailroom);
216 void nfc_digital_free_device(struct nfc_digital_dev *ndev);
217 int nfc_digital_register_device(struct nfc_digital_dev *ndev);
218 void nfc_digital_unregister_device(struct nfc_digital_dev *ndev);
219 
220 static inline void nfc_digital_set_parent_dev(struct nfc_digital_dev *ndev,
221 					      struct device *dev)
222 {
223 	nfc_set_parent_dev(ndev->nfc_dev, dev);
224 }
225 
226 static inline void nfc_digital_set_drvdata(struct nfc_digital_dev *dev,
227 					   void *data)
228 {
229 	dev->driver_data = data;
230 }
231 
232 static inline void *nfc_digital_get_drvdata(struct nfc_digital_dev *dev)
233 {
234 	return dev->driver_data;
235 }
236 
237 #endif /* __NFC_DIGITAL_H */
238