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