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