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