aboutsummaryrefslogtreecommitdiffstats
path: root/net/nfc/digital_dep.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/nfc/digital_dep.c')
-rw-r--r--net/nfc/digital_dep.c381
1 files changed, 381 insertions, 0 deletions
diff --git a/net/nfc/digital_dep.c b/net/nfc/digital_dep.c
new file mode 100644
index 000000000000..be984c4204d2
--- /dev/null
+++ b/net/nfc/digital_dep.c
@@ -0,0 +1,381 @@
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#include "digital.h"
17
18#define DIGITAL_NFC_DEP_FRAME_DIR_OUT 0xD4
19#define DIGITAL_NFC_DEP_FRAME_DIR_IN 0xD5
20
21#define DIGITAL_NFC_DEP_NFCA_SOD_SB 0xF0
22
23#define DIGITAL_CMD_ATR_REQ 0x00
24#define DIGITAL_CMD_ATR_RES 0x01
25#define DIGITAL_CMD_PSL_REQ 0x04
26#define DIGITAL_CMD_PSL_RES 0x05
27#define DIGITAL_CMD_DEP_REQ 0x06
28#define DIGITAL_CMD_DEP_RES 0x07
29
30#define DIGITAL_ATR_REQ_MIN_SIZE 16
31#define DIGITAL_ATR_REQ_MAX_SIZE 64
32
33#define DIGITAL_NFCID3_LEN ((u8)8)
34#define DIGITAL_LR_BITS_PAYLOAD_SIZE_254B 0x30
35#define DIGITAL_GB_BIT 0x02
36
37#define DIGITAL_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
38
39#define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10
40
41#define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
42 ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT)
43#define DIGITAL_NFC_DEP_MI_BIT_SET(pfb) ((pfb) & 0x10)
44#define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
45#define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & 0x04)
46#define DIGITAL_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
47
48#define DIGITAL_NFC_DEP_PFB_I_PDU 0x00
49#define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU 0x40
50#define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
51
52struct digital_atr_req {
53 u8 dir;
54 u8 cmd;
55 u8 nfcid3[10];
56 u8 did;
57 u8 bs;
58 u8 br;
59 u8 pp;
60 u8 gb[0];
61} __packed;
62
63struct digital_atr_res {
64 u8 dir;
65 u8 cmd;
66 u8 nfcid3[10];
67 u8 did;
68 u8 bs;
69 u8 br;
70 u8 to;
71 u8 pp;
72 u8 gb[0];
73} __packed;
74
75struct digital_psl_req {
76 u8 dir;
77 u8 cmd;
78 u8 did;
79 u8 brs;
80 u8 fsl;
81} __packed;
82
83struct digital_psl_res {
84 u8 dir;
85 u8 cmd;
86 u8 did;
87} __packed;
88
89struct digital_dep_req_res {
90 u8 dir;
91 u8 cmd;
92 u8 pfb;
93} __packed;
94
95static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
96 struct sk_buff *resp);
97
98static void digital_skb_push_dep_sod(struct nfc_digital_dev *ddev,
99 struct sk_buff *skb)
100{
101 skb_push(skb, sizeof(u8));
102
103 skb->data[0] = skb->len;
104
105 if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
106 *skb_push(skb, sizeof(u8)) = DIGITAL_NFC_DEP_NFCA_SOD_SB;
107}
108
109static int digital_skb_pull_dep_sod(struct nfc_digital_dev *ddev,
110 struct sk_buff *skb)
111{
112 u8 size;
113
114 if (skb->len < 2)
115 return -EIO;
116
117 if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
118 skb_pull(skb, sizeof(u8));
119
120 size = skb->data[0];
121 if (size != skb->len)
122 return -EIO;
123
124 skb_pull(skb, sizeof(u8));
125
126 return 0;
127}
128
129static void digital_in_recv_atr_res(struct nfc_digital_dev *ddev, void *arg,
130 struct sk_buff *resp)
131{
132 struct nfc_target *target = arg;
133 struct digital_atr_res *atr_res;
134 u8 gb_len;
135 int rc;
136
137 if (IS_ERR(resp)) {
138 rc = PTR_ERR(resp);
139 resp = NULL;
140 goto exit;
141 }
142
143 rc = ddev->skb_check_crc(resp);
144 if (rc) {
145 PROTOCOL_ERR("14.4.1.6");
146 goto exit;
147 }
148
149 rc = digital_skb_pull_dep_sod(ddev, resp);
150 if (rc) {
151 PROTOCOL_ERR("14.4.1.2");
152 goto exit;
153 }
154
155 if (resp->len < sizeof(struct digital_atr_res)) {
156 rc = -EIO;
157 goto exit;
158 }
159
160 gb_len = resp->len - sizeof(struct digital_atr_res);
161
162 atr_res = (struct digital_atr_res *)resp->data;
163
164 rc = nfc_set_remote_general_bytes(ddev->nfc_dev, atr_res->gb, gb_len);
165 if (rc)
166 goto exit;
167
168 rc = nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
169 NFC_RF_INITIATOR);
170
171 ddev->curr_nfc_dep_pni = 0;
172
173exit:
174 dev_kfree_skb(resp);
175
176 if (rc)
177 ddev->curr_protocol = 0;
178}
179
180int digital_in_send_atr_req(struct nfc_digital_dev *ddev,
181 struct nfc_target *target, __u8 comm_mode, __u8 *gb,
182 size_t gb_len)
183{
184 struct sk_buff *skb;
185 struct digital_atr_req *atr_req;
186 uint size;
187
188 size = DIGITAL_ATR_REQ_MIN_SIZE + gb_len;
189
190 if (size > DIGITAL_ATR_REQ_MAX_SIZE) {
191 PROTOCOL_ERR("14.6.1.1");
192 return -EINVAL;
193 }
194
195 skb = digital_skb_alloc(ddev, size);
196 if (!skb)
197 return -ENOMEM;
198
199 skb_put(skb, sizeof(struct digital_atr_req));
200
201 atr_req = (struct digital_atr_req *)skb->data;
202 memset(atr_req, 0, sizeof(struct digital_atr_req));
203
204 atr_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
205 atr_req->cmd = DIGITAL_CMD_ATR_REQ;
206 if (target->nfcid2_len)
207 memcpy(atr_req->nfcid3, target->nfcid2,
208 max(target->nfcid2_len, DIGITAL_NFCID3_LEN));
209 else
210 get_random_bytes(atr_req->nfcid3, DIGITAL_NFCID3_LEN);
211
212 atr_req->did = 0;
213 atr_req->bs = 0;
214 atr_req->br = 0;
215
216 atr_req->pp = DIGITAL_LR_BITS_PAYLOAD_SIZE_254B;
217
218 if (gb_len) {
219 atr_req->pp |= DIGITAL_GB_BIT;
220 memcpy(skb_put(skb, gb_len), gb, gb_len);
221 }
222
223 digital_skb_push_dep_sod(ddev, skb);
224
225 ddev->skb_add_crc(skb);
226
227 digital_in_send_cmd(ddev, skb, 500, digital_in_recv_atr_res, target);
228
229 return 0;
230}
231
232static int digital_in_send_rtox(struct nfc_digital_dev *ddev,
233 struct digital_data_exch *data_exch, u8 rtox)
234{
235 struct digital_dep_req_res *dep_req;
236 struct sk_buff *skb;
237 int rc;
238
239 skb = digital_skb_alloc(ddev, 1);
240 if (!skb)
241 return -ENOMEM;
242
243 *skb_put(skb, 1) = rtox;
244
245 skb_push(skb, sizeof(struct digital_dep_req_res));
246
247 dep_req = (struct digital_dep_req_res *)skb->data;
248
249 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
250 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
251 dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU |
252 DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT;
253
254 digital_skb_push_dep_sod(ddev, skb);
255
256 ddev->skb_add_crc(skb);
257
258 rc = digital_in_send_cmd(ddev, skb, 1500, digital_in_recv_dep_res,
259 data_exch);
260
261 return rc;
262}
263
264static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
265 struct sk_buff *resp)
266{
267 struct digital_data_exch *data_exch = arg;
268 struct digital_dep_req_res *dep_res;
269 u8 pfb;
270 uint size;
271 int rc;
272
273 if (IS_ERR(resp)) {
274 rc = PTR_ERR(resp);
275 resp = NULL;
276 goto exit;
277 }
278
279 rc = ddev->skb_check_crc(resp);
280 if (rc) {
281 PROTOCOL_ERR("14.4.1.6");
282 goto error;
283 }
284
285 rc = digital_skb_pull_dep_sod(ddev, resp);
286 if (rc) {
287 PROTOCOL_ERR("14.4.1.2");
288 goto exit;
289 }
290
291 dep_res = (struct digital_dep_req_res *)resp->data;
292
293 if (resp->len < sizeof(struct digital_dep_req_res) ||
294 dep_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN ||
295 dep_res->cmd != DIGITAL_CMD_DEP_RES) {
296 rc = -EIO;
297 goto error;
298 }
299
300 pfb = dep_res->pfb;
301
302 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
303 case DIGITAL_NFC_DEP_PFB_I_PDU:
304 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
305 PROTOCOL_ERR("14.12.3.3");
306 rc = -EIO;
307 goto error;
308 }
309
310 ddev->curr_nfc_dep_pni =
311 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
312 rc = 0;
313 break;
314
315 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
316 PR_ERR("Received a ACK/NACK PDU");
317 rc = -EIO;
318 goto error;
319
320 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
321 if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) {
322 rc = -EINVAL;
323 goto error;
324 }
325
326 rc = digital_in_send_rtox(ddev, data_exch, resp->data[3]);
327 if (rc)
328 goto error;
329
330 kfree_skb(resp);
331 return;
332 }
333
334 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb)) {
335 PR_ERR("MI bit set. Chained PDU not supported.");
336 rc = -EIO;
337 goto error;
338 }
339
340 size = sizeof(struct digital_dep_req_res);
341
342 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb))
343 size++;
344
345 if (size > resp->len) {
346 rc = -EIO;
347 goto error;
348 }
349
350 skb_pull(resp, size);
351
352exit:
353 data_exch->cb(data_exch->cb_context, resp, rc);
354
355error:
356 kfree(data_exch);
357
358 if (rc)
359 kfree_skb(resp);
360}
361
362int digital_in_send_dep_req(struct nfc_digital_dev *ddev,
363 struct nfc_target *target, struct sk_buff *skb,
364 struct digital_data_exch *data_exch)
365{
366 struct digital_dep_req_res *dep_req;
367
368 skb_push(skb, sizeof(struct digital_dep_req_res));
369
370 dep_req = (struct digital_dep_req_res *)skb->data;
371 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
372 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
373 dep_req->pfb = ddev->curr_nfc_dep_pni;
374
375 digital_skb_push_dep_sod(ddev, skb);
376
377 ddev->skb_add_crc(skb);
378
379 return digital_in_send_cmd(ddev, skb, 1500, digital_in_recv_dep_res,
380 data_exch);
381}