blob: 27769ac89d274928a5db673c54c060e23819395b [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
Samuel Ortizc5da0e42013-09-20 09:05:48 +020016#define pr_fmt(fmt) "digital: %s: " fmt, __func__
17
Thierry Escande4b10884eb2013-09-19 17:55:25 +020018#include <linux/module.h>
19
20#include "digital.h"
21
Thierry Escande59ee2362013-09-19 17:55:26 +020022#define DIGITAL_PROTO_NFCA_RF_TECH \
Shikha Singhce2e56c2015-11-20 06:40:19 -050023 (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK | \
24 NFC_PROTO_NFC_DEP_MASK | NFC_PROTO_ISO14443_MASK)
Thierry Escande59ee2362013-09-19 17:55:26 +020025
Mark A. Greer24734602014-03-31 17:36:38 -070026#define DIGITAL_PROTO_NFCB_RF_TECH NFC_PROTO_ISO14443_B_MASK
27
Thierry Escande7d0911c2013-09-19 17:55:29 +020028#define DIGITAL_PROTO_NFCF_RF_TECH \
29 (NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK)
Thierry Escande8c0695e42013-09-19 17:55:28 +020030
Mark A. Greera381d482014-01-21 16:23:59 -070031#define DIGITAL_PROTO_ISO15693_RF_TECH NFC_PROTO_ISO15693_MASK
32
Thierry Escande7854a442016-06-07 16:21:52 +020033/* Delay between each poll frame (ms) */
34#define DIGITAL_POLL_INTERVAL 10
35
Thierry Escande59ee2362013-09-19 17:55:26 +020036struct digital_cmd {
37 struct list_head queue;
38
39 u8 type;
40 u8 pending;
41
42 u16 timeout;
43 struct sk_buff *req;
44 struct sk_buff *resp;
Thierry Escande1c7a4c22013-09-19 17:55:30 +020045 struct digital_tg_mdaa_params *mdaa_params;
Thierry Escande59ee2362013-09-19 17:55:26 +020046
47 nfc_digital_cmd_complete_t cmd_cb;
48 void *cb_context;
49};
50
51struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
52 unsigned int len)
53{
54 struct sk_buff *skb;
55
56 skb = alloc_skb(len + ddev->tx_headroom + ddev->tx_tailroom,
57 GFP_KERNEL);
58 if (skb)
59 skb_reserve(skb, ddev->tx_headroom);
60
61 return skb;
62}
63
Thierry Escande2c66dae2013-09-19 17:55:27 +020064void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init,
65 u8 bitwise_inv, u8 msb_first)
66{
67 u16 crc;
68
69 crc = crc_func(init, skb->data, skb->len);
70
71 if (bitwise_inv)
72 crc = ~crc;
73
74 if (msb_first)
75 crc = __fswab16(crc);
76
77 *skb_put(skb, 1) = crc & 0xFF;
78 *skb_put(skb, 1) = (crc >> 8) & 0xFF;
79}
80
81int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func,
82 u16 crc_init, u8 bitwise_inv, u8 msb_first)
83{
84 int rc;
85 u16 crc;
86
87 if (skb->len <= 2)
88 return -EIO;
89
90 crc = crc_func(crc_init, skb->data, skb->len - 2);
91
92 if (bitwise_inv)
93 crc = ~crc;
94
95 if (msb_first)
96 crc = __swab16(crc);
97
98 rc = (skb->data[skb->len - 2] - (crc & 0xFF)) +
99 (skb->data[skb->len - 1] - ((crc >> 8) & 0xFF));
100
101 if (rc)
102 return -EIO;
103
104 skb_trim(skb, skb->len - 2);
105
106 return 0;
107}
108
Thierry Escande59ee2362013-09-19 17:55:26 +0200109static inline void digital_switch_rf(struct nfc_digital_dev *ddev, bool on)
110{
111 ddev->ops->switch_rf(ddev, on);
112}
113
114static inline void digital_abort_cmd(struct nfc_digital_dev *ddev)
115{
116 ddev->ops->abort_cmd(ddev);
117}
118
119static void digital_wq_cmd_complete(struct work_struct *work)
120{
121 struct digital_cmd *cmd;
122 struct nfc_digital_dev *ddev = container_of(work,
123 struct nfc_digital_dev,
124 cmd_complete_work);
125
126 mutex_lock(&ddev->cmd_lock);
127
128 cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
129 queue);
130 if (!cmd) {
131 mutex_unlock(&ddev->cmd_lock);
132 return;
133 }
134
135 list_del(&cmd->queue);
136
137 mutex_unlock(&ddev->cmd_lock);
138
139 if (!IS_ERR(cmd->resp))
140 print_hex_dump_debug("DIGITAL RX: ", DUMP_PREFIX_NONE, 16, 1,
141 cmd->resp->data, cmd->resp->len, false);
142
143 cmd->cmd_cb(ddev, cmd->cb_context, cmd->resp);
144
Thierry Escande1c7a4c22013-09-19 17:55:30 +0200145 kfree(cmd->mdaa_params);
Thierry Escande59ee2362013-09-19 17:55:26 +0200146 kfree(cmd);
147
148 schedule_work(&ddev->cmd_work);
149}
150
151static void digital_send_cmd_complete(struct nfc_digital_dev *ddev,
152 void *arg, struct sk_buff *resp)
153{
154 struct digital_cmd *cmd = arg;
155
156 cmd->resp = resp;
157
158 schedule_work(&ddev->cmd_complete_work);
159}
160
161static void digital_wq_cmd(struct work_struct *work)
162{
163 int rc;
164 struct digital_cmd *cmd;
Thierry Escande1c7a4c22013-09-19 17:55:30 +0200165 struct digital_tg_mdaa_params *params;
Thierry Escande59ee2362013-09-19 17:55:26 +0200166 struct nfc_digital_dev *ddev = container_of(work,
167 struct nfc_digital_dev,
168 cmd_work);
169
170 mutex_lock(&ddev->cmd_lock);
171
172 cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
173 queue);
174 if (!cmd || cmd->pending) {
175 mutex_unlock(&ddev->cmd_lock);
176 return;
177 }
178
179 mutex_unlock(&ddev->cmd_lock);
180
181 if (cmd->req)
182 print_hex_dump_debug("DIGITAL TX: ", DUMP_PREFIX_NONE, 16, 1,
183 cmd->req->data, cmd->req->len, false);
184
185 switch (cmd->type) {
186 case DIGITAL_CMD_IN_SEND:
187 rc = ddev->ops->in_send_cmd(ddev, cmd->req, cmd->timeout,
188 digital_send_cmd_complete, cmd);
189 break;
Thierry Escande1c7a4c22013-09-19 17:55:30 +0200190
191 case DIGITAL_CMD_TG_SEND:
192 rc = ddev->ops->tg_send_cmd(ddev, cmd->req, cmd->timeout,
193 digital_send_cmd_complete, cmd);
194 break;
195
196 case DIGITAL_CMD_TG_LISTEN:
197 rc = ddev->ops->tg_listen(ddev, cmd->timeout,
198 digital_send_cmd_complete, cmd);
199 break;
200
201 case DIGITAL_CMD_TG_LISTEN_MDAA:
202 params = cmd->mdaa_params;
203
204 rc = ddev->ops->tg_listen_mdaa(ddev, params, cmd->timeout,
205 digital_send_cmd_complete, cmd);
206 break;
207
Mark A. Greerbf30a672014-07-21 21:24:39 -0700208 case DIGITAL_CMD_TG_LISTEN_MD:
209 rc = ddev->ops->tg_listen_md(ddev, cmd->timeout,
210 digital_send_cmd_complete, cmd);
211 break;
212
Thierry Escande59ee2362013-09-19 17:55:26 +0200213 default:
Samuel Ortiz26042532013-09-20 16:56:40 +0200214 pr_err("Unknown cmd type %d\n", cmd->type);
Thierry Escande59ee2362013-09-19 17:55:26 +0200215 return;
216 }
217
218 if (!rc)
219 return;
220
Samuel Ortiz26042532013-09-20 16:56:40 +0200221 pr_err("in_send_command returned err %d\n", rc);
Thierry Escande59ee2362013-09-19 17:55:26 +0200222
223 mutex_lock(&ddev->cmd_lock);
224 list_del(&cmd->queue);
225 mutex_unlock(&ddev->cmd_lock);
226
227 kfree_skb(cmd->req);
Thierry Escande1c7a4c22013-09-19 17:55:30 +0200228 kfree(cmd->mdaa_params);
Thierry Escande59ee2362013-09-19 17:55:26 +0200229 kfree(cmd);
230
231 schedule_work(&ddev->cmd_work);
232}
233
234int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
Thierry Escande1c7a4c22013-09-19 17:55:30 +0200235 struct sk_buff *skb, struct digital_tg_mdaa_params *params,
236 u16 timeout, nfc_digital_cmd_complete_t cmd_cb,
237 void *cb_context)
Thierry Escande59ee2362013-09-19 17:55:26 +0200238{
239 struct digital_cmd *cmd;
240
241 cmd = kzalloc(sizeof(struct digital_cmd), GFP_KERNEL);
242 if (!cmd)
243 return -ENOMEM;
244
245 cmd->type = cmd_type;
246 cmd->timeout = timeout;
247 cmd->req = skb;
Thierry Escande1c7a4c22013-09-19 17:55:30 +0200248 cmd->mdaa_params = params;
Thierry Escande59ee2362013-09-19 17:55:26 +0200249 cmd->cmd_cb = cmd_cb;
250 cmd->cb_context = cb_context;
251 INIT_LIST_HEAD(&cmd->queue);
252
253 mutex_lock(&ddev->cmd_lock);
254 list_add_tail(&cmd->queue, &ddev->cmd_queue);
255 mutex_unlock(&ddev->cmd_lock);
256
257 schedule_work(&ddev->cmd_work);
258
259 return 0;
260}
261
262int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
263{
264 int rc;
265
266 rc = ddev->ops->in_configure_hw(ddev, type, param);
267 if (rc)
Samuel Ortiz26042532013-09-20 16:56:40 +0200268 pr_err("in_configure_hw failed: %d\n", rc);
Thierry Escande59ee2362013-09-19 17:55:26 +0200269
270 return rc;
271}
272
Thierry Escande1c7a4c22013-09-19 17:55:30 +0200273int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
274{
275 int rc;
276
277 rc = ddev->ops->tg_configure_hw(ddev, type, param);
278 if (rc)
Samuel Ortiz26042532013-09-20 16:56:40 +0200279 pr_err("tg_configure_hw failed: %d\n", rc);
Thierry Escande1c7a4c22013-09-19 17:55:30 +0200280
281 return rc;
282}
283
284static int digital_tg_listen_mdaa(struct nfc_digital_dev *ddev, u8 rf_tech)
285{
286 struct digital_tg_mdaa_params *params;
287
288 params = kzalloc(sizeof(struct digital_tg_mdaa_params), GFP_KERNEL);
289 if (!params)
290 return -ENOMEM;
291
292 params->sens_res = DIGITAL_SENS_RES_NFC_DEP;
293 get_random_bytes(params->nfcid1, sizeof(params->nfcid1));
294 params->sel_res = DIGITAL_SEL_RES_NFC_DEP;
295
296 params->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
297 params->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
298 get_random_bytes(params->nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2);
299 params->sc = DIGITAL_SENSF_FELICA_SC;
300
301 return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MDAA, NULL, params,
302 500, digital_tg_recv_atr_req, NULL);
303}
304
Mark A. Greerbf30a672014-07-21 21:24:39 -0700305static int digital_tg_listen_md(struct nfc_digital_dev *ddev, u8 rf_tech)
306{
307 return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MD, NULL, NULL, 500,
308 digital_tg_recv_md_req, NULL);
309}
310
Thierry Escande2c66dae2013-09-19 17:55:27 +0200311int digital_target_found(struct nfc_digital_dev *ddev,
312 struct nfc_target *target, u8 protocol)
313{
314 int rc;
315 u8 framing;
316 u8 rf_tech;
Mark A. Greer0529a7a2014-07-02 09:03:49 -0700317 u8 poll_tech_count;
Thierry Escande2c66dae2013-09-19 17:55:27 +0200318 int (*check_crc)(struct sk_buff *skb);
319 void (*add_crc)(struct sk_buff *skb);
320
321 rf_tech = ddev->poll_techs[ddev->poll_tech_index].rf_tech;
322
323 switch (protocol) {
324 case NFC_PROTO_JEWEL:
325 framing = NFC_DIGITAL_FRAMING_NFCA_T1T;
326 check_crc = digital_skb_check_crc_b;
327 add_crc = digital_skb_add_crc_b;
328 break;
329
330 case NFC_PROTO_MIFARE:
331 framing = NFC_DIGITAL_FRAMING_NFCA_T2T;
332 check_crc = digital_skb_check_crc_a;
333 add_crc = digital_skb_add_crc_a;
334 break;
335
Thierry Escande8c0695e42013-09-19 17:55:28 +0200336 case NFC_PROTO_FELICA:
337 framing = NFC_DIGITAL_FRAMING_NFCF_T3T;
338 check_crc = digital_skb_check_crc_f;
339 add_crc = digital_skb_add_crc_f;
340 break;
341
Thierry Escande7d0911c2013-09-19 17:55:29 +0200342 case NFC_PROTO_NFC_DEP:
343 if (rf_tech == NFC_DIGITAL_RF_TECH_106A) {
344 framing = NFC_DIGITAL_FRAMING_NFCA_NFC_DEP;
345 check_crc = digital_skb_check_crc_a;
346 add_crc = digital_skb_add_crc_a;
347 } else {
348 framing = NFC_DIGITAL_FRAMING_NFCF_NFC_DEP;
349 check_crc = digital_skb_check_crc_f;
350 add_crc = digital_skb_add_crc_f;
351 }
352 break;
353
Mark A. Greera381d482014-01-21 16:23:59 -0700354 case NFC_PROTO_ISO15693:
Mark A. Greerceeee422014-03-06 07:39:19 -0700355 framing = NFC_DIGITAL_FRAMING_ISO15693_T5T;
Mark A. Greera381d482014-01-21 16:23:59 -0700356 check_crc = digital_skb_check_crc_b;
357 add_crc = digital_skb_add_crc_b;
Thierry Escande564af142014-02-12 12:13:23 +0100358 break;
Thierry Escande12e3d242014-01-27 00:31:31 +0100359
360 case NFC_PROTO_ISO14443:
361 framing = NFC_DIGITAL_FRAMING_NFCA_T4T;
362 check_crc = digital_skb_check_crc_a;
363 add_crc = digital_skb_add_crc_a;
Mark A. Greera381d482014-01-21 16:23:59 -0700364 break;
365
Mark A. Greer24734602014-03-31 17:36:38 -0700366 case NFC_PROTO_ISO14443_B:
367 framing = NFC_DIGITAL_FRAMING_NFCB_T4T;
368 check_crc = digital_skb_check_crc_b;
369 add_crc = digital_skb_add_crc_b;
370 break;
371
Thierry Escande2c66dae2013-09-19 17:55:27 +0200372 default:
Samuel Ortiz26042532013-09-20 16:56:40 +0200373 pr_err("Invalid protocol %d\n", protocol);
Thierry Escande2c66dae2013-09-19 17:55:27 +0200374 return -EINVAL;
375 }
376
Samuel Ortiz26042532013-09-20 16:56:40 +0200377 pr_debug("rf_tech=%d, protocol=%d\n", rf_tech, protocol);
Thierry Escande2c66dae2013-09-19 17:55:27 +0200378
379 ddev->curr_rf_tech = rf_tech;
Thierry Escande2c66dae2013-09-19 17:55:27 +0200380
381 if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
382 ddev->skb_add_crc = digital_skb_add_crc_none;
383 ddev->skb_check_crc = digital_skb_check_crc_none;
384 } else {
385 ddev->skb_add_crc = add_crc;
386 ddev->skb_check_crc = check_crc;
387 }
388
389 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, framing);
390 if (rc)
391 return rc;
392
393 target->supported_protocols = (1 << protocol);
Thierry Escande2c66dae2013-09-19 17:55:27 +0200394
Mark A. Greer0529a7a2014-07-02 09:03:49 -0700395 poll_tech_count = ddev->poll_tech_count;
Thierry Escande2c66dae2013-09-19 17:55:27 +0200396 ddev->poll_tech_count = 0;
397
Mark A. Greer0529a7a2014-07-02 09:03:49 -0700398 rc = nfc_targets_found(ddev->nfc_dev, target, 1);
399 if (rc) {
400 ddev->poll_tech_count = poll_tech_count;
401 return rc;
402 }
403
Thierry Escande2c66dae2013-09-19 17:55:27 +0200404 return 0;
405}
406
Thierry Escande59ee2362013-09-19 17:55:26 +0200407void digital_poll_next_tech(struct nfc_digital_dev *ddev)
408{
Thierry Escande9dc33702014-05-10 13:06:02 +0200409 u8 rand_mod;
410
Thierry Escande59ee2362013-09-19 17:55:26 +0200411 digital_switch_rf(ddev, 0);
412
413 mutex_lock(&ddev->poll_lock);
414
415 if (!ddev->poll_tech_count) {
416 mutex_unlock(&ddev->poll_lock);
417 return;
418 }
419
Thierry Escande9dc33702014-05-10 13:06:02 +0200420 get_random_bytes(&rand_mod, sizeof(rand_mod));
421 ddev->poll_tech_index = rand_mod % ddev->poll_tech_count;
Thierry Escande59ee2362013-09-19 17:55:26 +0200422
423 mutex_unlock(&ddev->poll_lock);
424
Thierry Escande7854a442016-06-07 16:21:52 +0200425 schedule_delayed_work(&ddev->poll_work,
426 msecs_to_jiffies(DIGITAL_POLL_INTERVAL));
Thierry Escande59ee2362013-09-19 17:55:26 +0200427}
428
429static void digital_wq_poll(struct work_struct *work)
430{
431 int rc;
432 struct digital_poll_tech *poll_tech;
433 struct nfc_digital_dev *ddev = container_of(work,
434 struct nfc_digital_dev,
Thierry Escande7854a442016-06-07 16:21:52 +0200435 poll_work.work);
Thierry Escande59ee2362013-09-19 17:55:26 +0200436 mutex_lock(&ddev->poll_lock);
437
438 if (!ddev->poll_tech_count) {
439 mutex_unlock(&ddev->poll_lock);
440 return;
441 }
442
443 poll_tech = &ddev->poll_techs[ddev->poll_tech_index];
444
445 mutex_unlock(&ddev->poll_lock);
446
447 rc = poll_tech->poll_func(ddev, poll_tech->rf_tech);
448 if (rc)
449 digital_poll_next_tech(ddev);
450}
451
452static void digital_add_poll_tech(struct nfc_digital_dev *ddev, u8 rf_tech,
453 digital_poll_t poll_func)
454{
455 struct digital_poll_tech *poll_tech;
456
457 if (ddev->poll_tech_count >= NFC_DIGITAL_POLL_MODE_COUNT_MAX)
458 return;
459
460 poll_tech = &ddev->poll_techs[ddev->poll_tech_count++];
461
462 poll_tech->rf_tech = rf_tech;
463 poll_tech->poll_func = poll_func;
464}
465
466/**
467 * start_poll operation
468 *
469 * For every supported protocol, the corresponding polling function is added
470 * to the table of polling technologies (ddev->poll_techs[]) using
471 * digital_add_poll_tech().
472 * When a polling function fails (by timeout or protocol error) the next one is
473 * schedule by digital_poll_next_tech() on the poll workqueue (ddev->poll_work).
474 */
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200475static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
476 __u32 tm_protocols)
477{
Thierry Escande59ee2362013-09-19 17:55:26 +0200478 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
479 u32 matching_im_protocols, matching_tm_protocols;
480
Samuel Ortiz26042532013-09-20 16:56:40 +0200481 pr_debug("protocols: im 0x%x, tm 0x%x, supported 0x%x\n", im_protocols,
Samuel Ortizc5da0e42013-09-20 09:05:48 +0200482 tm_protocols, ddev->protocols);
Thierry Escande59ee2362013-09-19 17:55:26 +0200483
484 matching_im_protocols = ddev->protocols & im_protocols;
485 matching_tm_protocols = ddev->protocols & tm_protocols;
486
487 if (!matching_im_protocols && !matching_tm_protocols) {
Samuel Ortiz26042532013-09-20 16:56:40 +0200488 pr_err("Unknown protocol\n");
Thierry Escande59ee2362013-09-19 17:55:26 +0200489 return -EINVAL;
490 }
491
492 if (ddev->poll_tech_count) {
Samuel Ortiz26042532013-09-20 16:56:40 +0200493 pr_err("Already polling\n");
Thierry Escande59ee2362013-09-19 17:55:26 +0200494 return -EBUSY;
495 }
496
497 if (ddev->curr_protocol) {
Samuel Ortiz26042532013-09-20 16:56:40 +0200498 pr_err("A target is already active\n");
Thierry Escande59ee2362013-09-19 17:55:26 +0200499 return -EBUSY;
500 }
501
502 ddev->poll_tech_count = 0;
503 ddev->poll_tech_index = 0;
504
505 if (matching_im_protocols & DIGITAL_PROTO_NFCA_RF_TECH)
506 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
507 digital_in_send_sens_req);
508
Mark A. Greer24734602014-03-31 17:36:38 -0700509 if (matching_im_protocols & DIGITAL_PROTO_NFCB_RF_TECH)
510 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106B,
511 digital_in_send_sensb_req);
512
Axel Lin4f913d42014-02-22 10:16:11 +0800513 if (matching_im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) {
Thierry Escande8c0695e42013-09-19 17:55:28 +0200514 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
515 digital_in_send_sensf_req);
516
517 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
518 digital_in_send_sensf_req);
519 }
520
Mark A. Greera381d482014-01-21 16:23:59 -0700521 if (matching_im_protocols & DIGITAL_PROTO_ISO15693_RF_TECH)
522 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_ISO15693,
523 digital_in_send_iso15693_inv_req);
524
Axel Lin4f913d42014-02-22 10:16:11 +0800525 if (matching_tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
Thierry Escande1c7a4c22013-09-19 17:55:30 +0200526 if (ddev->ops->tg_listen_mdaa) {
527 digital_add_poll_tech(ddev, 0,
528 digital_tg_listen_mdaa);
Mark A. Greerbf30a672014-07-21 21:24:39 -0700529 } else if (ddev->ops->tg_listen_md) {
530 digital_add_poll_tech(ddev, 0,
531 digital_tg_listen_md);
Thierry Escande1c7a4c22013-09-19 17:55:30 +0200532 } else {
533 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
534 digital_tg_listen_nfca);
535
536 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
537 digital_tg_listen_nfcf);
538
539 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
540 digital_tg_listen_nfcf);
541 }
542 }
543
Thierry Escande59ee2362013-09-19 17:55:26 +0200544 if (!ddev->poll_tech_count) {
Samuel Ortiz26042532013-09-20 16:56:40 +0200545 pr_err("Unsupported protocols: im=0x%x, tm=0x%x\n",
Thierry Escande59ee2362013-09-19 17:55:26 +0200546 matching_im_protocols, matching_tm_protocols);
547 return -EINVAL;
548 }
549
Thierry Escande7854a442016-06-07 16:21:52 +0200550 schedule_delayed_work(&ddev->poll_work, 0);
Thierry Escande59ee2362013-09-19 17:55:26 +0200551
552 return 0;
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200553}
554
555static void digital_stop_poll(struct nfc_dev *nfc_dev)
556{
Thierry Escande59ee2362013-09-19 17:55:26 +0200557 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
558
559 mutex_lock(&ddev->poll_lock);
560
561 if (!ddev->poll_tech_count) {
Samuel Ortiz26042532013-09-20 16:56:40 +0200562 pr_err("Polling operation was not running\n");
Thierry Escande59ee2362013-09-19 17:55:26 +0200563 mutex_unlock(&ddev->poll_lock);
564 return;
565 }
566
567 ddev->poll_tech_count = 0;
568
569 mutex_unlock(&ddev->poll_lock);
570
Thierry Escande7854a442016-06-07 16:21:52 +0200571 cancel_delayed_work_sync(&ddev->poll_work);
Thierry Escande59ee2362013-09-19 17:55:26 +0200572
573 digital_abort_cmd(ddev);
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200574}
575
576static int digital_dev_up(struct nfc_dev *nfc_dev)
577{
Thierry Escande59ee2362013-09-19 17:55:26 +0200578 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
579
580 digital_switch_rf(ddev, 1);
581
582 return 0;
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200583}
584
585static int digital_dev_down(struct nfc_dev *nfc_dev)
586{
Thierry Escande59ee2362013-09-19 17:55:26 +0200587 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
588
589 digital_switch_rf(ddev, 0);
590
591 return 0;
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200592}
593
594static int digital_dep_link_up(struct nfc_dev *nfc_dev,
595 struct nfc_target *target,
596 __u8 comm_mode, __u8 *gb, size_t gb_len)
597{
Thierry Escande7d0911c2013-09-19 17:55:29 +0200598 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
Thierry Escande48e10442014-01-06 23:34:37 +0100599 int rc;
Thierry Escande7d0911c2013-09-19 17:55:29 +0200600
Thierry Escande48e10442014-01-06 23:34:37 +0100601 rc = digital_in_send_atr_req(ddev, target, comm_mode, gb, gb_len);
602
603 if (!rc)
604 ddev->curr_protocol = NFC_PROTO_NFC_DEP;
605
606 return rc;
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200607}
608
609static int digital_dep_link_down(struct nfc_dev *nfc_dev)
610{
Thierry Escande7d0911c2013-09-19 17:55:29 +0200611 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
612
613 ddev->curr_protocol = 0;
614
615 return 0;
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200616}
617
618static int digital_activate_target(struct nfc_dev *nfc_dev,
619 struct nfc_target *target, __u32 protocol)
620{
Thierry Escande48e10442014-01-06 23:34:37 +0100621 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
622
623 if (ddev->poll_tech_count) {
624 pr_err("Can't activate a target while polling\n");
625 return -EBUSY;
626 }
627
628 if (ddev->curr_protocol) {
629 pr_err("A target is already active\n");
630 return -EBUSY;
631 }
632
633 ddev->curr_protocol = protocol;
634
Thierry Escande59ee2362013-09-19 17:55:26 +0200635 return 0;
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200636}
637
638static void digital_deactivate_target(struct nfc_dev *nfc_dev,
Christophe Ricard96d45812015-10-25 22:54:43 +0100639 struct nfc_target *target,
640 u8 mode)
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200641{
Thierry Escande59ee2362013-09-19 17:55:26 +0200642 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
643
Thierry Escande48e10442014-01-06 23:34:37 +0100644 if (!ddev->curr_protocol) {
645 pr_err("No active target\n");
646 return;
647 }
648
Thierry Escande59ee2362013-09-19 17:55:26 +0200649 ddev->curr_protocol = 0;
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200650}
651
652static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb)
653{
Thierry Escande1c7a4c22013-09-19 17:55:30 +0200654 struct nfc_digital_dev *ddev = nfc_get_drvdata(dev);
655
656 return digital_tg_send_dep_res(ddev, skb);
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200657}
658
Thierry Escande2c66dae2013-09-19 17:55:27 +0200659static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg,
660 struct sk_buff *resp)
661{
662 struct digital_data_exch *data_exch = arg;
663 int rc;
664
665 if (IS_ERR(resp)) {
666 rc = PTR_ERR(resp);
Thierry Escandec8130072014-01-27 00:31:32 +0100667 resp = NULL;
Thierry Escande2c66dae2013-09-19 17:55:27 +0200668 goto done;
669 }
670
Thierry Escandec8130072014-01-27 00:31:32 +0100671 if (ddev->curr_protocol == NFC_PROTO_MIFARE) {
Thierry Escande2c66dae2013-09-19 17:55:27 +0200672 rc = digital_in_recv_mifare_res(resp);
Thierry Escandec8130072014-01-27 00:31:32 +0100673 /* crc check is done in digital_in_recv_mifare_res() */
674 goto done;
675 }
Thierry Escande2c66dae2013-09-19 17:55:27 +0200676
Mark A. Greer24734602014-03-31 17:36:38 -0700677 if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
678 (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
Thierry Escandec8130072014-01-27 00:31:32 +0100679 rc = digital_in_iso_dep_pull_sod(ddev, resp);
680 if (rc)
681 goto done;
682 }
683
684 rc = ddev->skb_check_crc(resp);
685
686done:
Thierry Escande2c66dae2013-09-19 17:55:27 +0200687 if (rc) {
688 kfree_skb(resp);
689 resp = NULL;
690 }
691
Thierry Escande2c66dae2013-09-19 17:55:27 +0200692 data_exch->cb(data_exch->cb_context, resp, rc);
693
694 kfree(data_exch);
695}
696
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200697static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
698 struct sk_buff *skb, data_exchange_cb_t cb,
699 void *cb_context)
700{
Thierry Escande2c66dae2013-09-19 17:55:27 +0200701 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
702 struct digital_data_exch *data_exch;
Thierry Escandec8130072014-01-27 00:31:32 +0100703 int rc;
Thierry Escande2c66dae2013-09-19 17:55:27 +0200704
705 data_exch = kzalloc(sizeof(struct digital_data_exch), GFP_KERNEL);
706 if (!data_exch) {
Samuel Ortiz26042532013-09-20 16:56:40 +0200707 pr_err("Failed to allocate data_exch struct\n");
Thierry Escande2c66dae2013-09-19 17:55:27 +0200708 return -ENOMEM;
709 }
710
711 data_exch->cb = cb;
712 data_exch->cb_context = cb_context;
713
Thierry Escande6ea73982014-02-12 14:27:51 +0100714 if (ddev->curr_protocol == NFC_PROTO_NFC_DEP) {
715 rc = digital_in_send_dep_req(ddev, target, skb, data_exch);
716 goto exit;
717 }
Thierry Escande7d0911c2013-09-19 17:55:29 +0200718
Mark A. Greer24734602014-03-31 17:36:38 -0700719 if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
720 (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
Thierry Escandec8130072014-01-27 00:31:32 +0100721 rc = digital_in_iso_dep_push_sod(ddev, skb);
722 if (rc)
Thierry Escande6ea73982014-02-12 14:27:51 +0100723 goto exit;
Thierry Escandec8130072014-01-27 00:31:32 +0100724 }
725
Thierry Escande2c66dae2013-09-19 17:55:27 +0200726 ddev->skb_add_crc(skb);
727
Thierry Escande6ea73982014-02-12 14:27:51 +0100728 rc = digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete,
729 data_exch);
730
731exit:
732 if (rc)
733 kfree(data_exch);
734
735 return rc;
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200736}
737
738static struct nfc_ops digital_nfc_ops = {
739 .dev_up = digital_dev_up,
740 .dev_down = digital_dev_down,
741 .start_poll = digital_start_poll,
742 .stop_poll = digital_stop_poll,
743 .dep_link_up = digital_dep_link_up,
744 .dep_link_down = digital_dep_link_down,
745 .activate_target = digital_activate_target,
746 .deactivate_target = digital_deactivate_target,
747 .tm_send = digital_tg_send,
748 .im_transceive = digital_in_send,
749};
750
751struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops,
752 __u32 supported_protocols,
753 __u32 driver_capabilities,
754 int tx_headroom, int tx_tailroom)
755{
756 struct nfc_digital_dev *ddev;
757
758 if (!ops->in_configure_hw || !ops->in_send_cmd || !ops->tg_listen ||
759 !ops->tg_configure_hw || !ops->tg_send_cmd || !ops->abort_cmd ||
Mark A. Greerbf30a672014-07-21 21:24:39 -0700760 !ops->switch_rf || (ops->tg_listen_md && !ops->tg_get_rf_tech))
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200761 return NULL;
762
763 ddev = kzalloc(sizeof(struct nfc_digital_dev), GFP_KERNEL);
Samuel Ortiz26042532013-09-20 16:56:40 +0200764 if (!ddev)
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200765 return NULL;
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200766
767 ddev->driver_capabilities = driver_capabilities;
768 ddev->ops = ops;
769
Thierry Escande59ee2362013-09-19 17:55:26 +0200770 mutex_init(&ddev->cmd_lock);
771 INIT_LIST_HEAD(&ddev->cmd_queue);
772
773 INIT_WORK(&ddev->cmd_work, digital_wq_cmd);
774 INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete);
775
776 mutex_init(&ddev->poll_lock);
Thierry Escande7854a442016-06-07 16:21:52 +0200777 INIT_DELAYED_WORK(&ddev->poll_work, digital_wq_poll);
Thierry Escande59ee2362013-09-19 17:55:26 +0200778
779 if (supported_protocols & NFC_PROTO_JEWEL_MASK)
780 ddev->protocols |= NFC_PROTO_JEWEL_MASK;
781 if (supported_protocols & NFC_PROTO_MIFARE_MASK)
782 ddev->protocols |= NFC_PROTO_MIFARE_MASK;
Thierry Escande8c0695e42013-09-19 17:55:28 +0200783 if (supported_protocols & NFC_PROTO_FELICA_MASK)
784 ddev->protocols |= NFC_PROTO_FELICA_MASK;
Thierry Escande7d0911c2013-09-19 17:55:29 +0200785 if (supported_protocols & NFC_PROTO_NFC_DEP_MASK)
786 ddev->protocols |= NFC_PROTO_NFC_DEP_MASK;
Mark A. Greera381d482014-01-21 16:23:59 -0700787 if (supported_protocols & NFC_PROTO_ISO15693_MASK)
788 ddev->protocols |= NFC_PROTO_ISO15693_MASK;
Thierry Escande12e3d242014-01-27 00:31:31 +0100789 if (supported_protocols & NFC_PROTO_ISO14443_MASK)
790 ddev->protocols |= NFC_PROTO_ISO14443_MASK;
Mark A. Greer24734602014-03-31 17:36:38 -0700791 if (supported_protocols & NFC_PROTO_ISO14443_B_MASK)
792 ddev->protocols |= NFC_PROTO_ISO14443_B_MASK;
Thierry Escande59ee2362013-09-19 17:55:26 +0200793
794 ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
795 ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200796
797 ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols,
798 ddev->tx_headroom,
799 ddev->tx_tailroom);
800 if (!ddev->nfc_dev) {
Samuel Ortiz26042532013-09-20 16:56:40 +0200801 pr_err("nfc_allocate_device failed\n");
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200802 goto free_dev;
803 }
804
805 nfc_set_drvdata(ddev->nfc_dev, ddev);
806
807 return ddev;
808
809free_dev:
810 kfree(ddev);
811
812 return NULL;
813}
814EXPORT_SYMBOL(nfc_digital_allocate_device);
815
816void nfc_digital_free_device(struct nfc_digital_dev *ddev)
817{
818 nfc_free_device(ddev->nfc_dev);
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200819 kfree(ddev);
820}
821EXPORT_SYMBOL(nfc_digital_free_device);
822
823int nfc_digital_register_device(struct nfc_digital_dev *ddev)
824{
825 return nfc_register_device(ddev->nfc_dev);
826}
827EXPORT_SYMBOL(nfc_digital_register_device);
828
829void nfc_digital_unregister_device(struct nfc_digital_dev *ddev)
830{
Thierry Escande59ee2362013-09-19 17:55:26 +0200831 struct digital_cmd *cmd, *n;
832
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200833 nfc_unregister_device(ddev->nfc_dev);
Thierry Escande59ee2362013-09-19 17:55:26 +0200834
835 mutex_lock(&ddev->poll_lock);
836 ddev->poll_tech_count = 0;
837 mutex_unlock(&ddev->poll_lock);
838
Thierry Escande7854a442016-06-07 16:21:52 +0200839 cancel_delayed_work_sync(&ddev->poll_work);
Thierry Escande59ee2362013-09-19 17:55:26 +0200840 cancel_work_sync(&ddev->cmd_work);
841 cancel_work_sync(&ddev->cmd_complete_work);
842
843 list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) {
844 list_del(&cmd->queue);
Thierry Escande1c7a4c22013-09-19 17:55:30 +0200845 kfree(cmd->mdaa_params);
Thierry Escande59ee2362013-09-19 17:55:26 +0200846 kfree(cmd);
847 }
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200848}
849EXPORT_SYMBOL(nfc_digital_unregister_device);
850
851MODULE_LICENSE("GPL");