xref: /openbmc/linux/net/nfc/digital_core.c (revision b38269ec)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * NFC Digital Protocol stack
4  * Copyright (c) 2013, Intel Corporation.
5  */
6 
7 #define pr_fmt(fmt) "digital: %s: " fmt, __func__
8 
9 #include <linux/module.h>
10 
11 #include "digital.h"
12 
13 #define DIGITAL_PROTO_NFCA_RF_TECH \
14 	(NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK | \
15 	NFC_PROTO_NFC_DEP_MASK | NFC_PROTO_ISO14443_MASK)
16 
17 #define DIGITAL_PROTO_NFCB_RF_TECH	NFC_PROTO_ISO14443_B_MASK
18 
19 #define DIGITAL_PROTO_NFCF_RF_TECH \
20 	(NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK)
21 
22 #define DIGITAL_PROTO_ISO15693_RF_TECH	NFC_PROTO_ISO15693_MASK
23 
24 /* Delay between each poll frame (ms) */
25 #define DIGITAL_POLL_INTERVAL 10
26 
27 struct digital_cmd {
28 	struct list_head queue;
29 
30 	u8 type;
31 	u8 pending;
32 
33 	u16 timeout;
34 	struct sk_buff *req;
35 	struct sk_buff *resp;
36 	struct digital_tg_mdaa_params *mdaa_params;
37 
38 	nfc_digital_cmd_complete_t cmd_cb;
39 	void *cb_context;
40 };
41 
42 struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
43 				  unsigned int len)
44 {
45 	struct sk_buff *skb;
46 
47 	skb = alloc_skb(len + ddev->tx_headroom + ddev->tx_tailroom,
48 			GFP_KERNEL);
49 	if (skb)
50 		skb_reserve(skb, ddev->tx_headroom);
51 
52 	return skb;
53 }
54 
55 void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init,
56 			 u8 bitwise_inv, u8 msb_first)
57 {
58 	u16 crc;
59 
60 	crc = crc_func(init, skb->data, skb->len);
61 
62 	if (bitwise_inv)
63 		crc = ~crc;
64 
65 	if (msb_first)
66 		crc = __fswab16(crc);
67 
68 	skb_put_u8(skb, crc & 0xFF);
69 	skb_put_u8(skb, (crc >> 8) & 0xFF);
70 }
71 
72 int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func,
73 			  u16 crc_init, u8 bitwise_inv, u8 msb_first)
74 {
75 	int rc;
76 	u16 crc;
77 
78 	if (skb->len <= 2)
79 		return -EIO;
80 
81 	crc = crc_func(crc_init, skb->data, skb->len - 2);
82 
83 	if (bitwise_inv)
84 		crc = ~crc;
85 
86 	if (msb_first)
87 		crc = __swab16(crc);
88 
89 	rc = (skb->data[skb->len - 2] - (crc & 0xFF)) +
90 	     (skb->data[skb->len - 1] - ((crc >> 8) & 0xFF));
91 
92 	if (rc)
93 		return -EIO;
94 
95 	skb_trim(skb, skb->len - 2);
96 
97 	return 0;
98 }
99 
100 static inline void digital_switch_rf(struct nfc_digital_dev *ddev, bool on)
101 {
102 	ddev->ops->switch_rf(ddev, on);
103 }
104 
105 static inline void digital_abort_cmd(struct nfc_digital_dev *ddev)
106 {
107 	ddev->ops->abort_cmd(ddev);
108 }
109 
110 static void digital_wq_cmd_complete(struct work_struct *work)
111 {
112 	struct digital_cmd *cmd;
113 	struct nfc_digital_dev *ddev = container_of(work,
114 						    struct nfc_digital_dev,
115 						    cmd_complete_work);
116 
117 	mutex_lock(&ddev->cmd_lock);
118 
119 	cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
120 				       queue);
121 	if (!cmd) {
122 		mutex_unlock(&ddev->cmd_lock);
123 		return;
124 	}
125 
126 	list_del(&cmd->queue);
127 
128 	mutex_unlock(&ddev->cmd_lock);
129 
130 	if (!IS_ERR(cmd->resp))
131 		print_hex_dump_debug("DIGITAL RX: ", DUMP_PREFIX_NONE, 16, 1,
132 				     cmd->resp->data, cmd->resp->len, false);
133 
134 	cmd->cmd_cb(ddev, cmd->cb_context, cmd->resp);
135 
136 	kfree(cmd->mdaa_params);
137 	kfree(cmd);
138 
139 	schedule_work(&ddev->cmd_work);
140 }
141 
142 static void digital_send_cmd_complete(struct nfc_digital_dev *ddev,
143 				      void *arg, struct sk_buff *resp)
144 {
145 	struct digital_cmd *cmd = arg;
146 
147 	cmd->resp = resp;
148 
149 	schedule_work(&ddev->cmd_complete_work);
150 }
151 
152 static void digital_wq_cmd(struct work_struct *work)
153 {
154 	int rc;
155 	struct digital_cmd *cmd;
156 	struct digital_tg_mdaa_params *params;
157 	struct nfc_digital_dev *ddev = container_of(work,
158 						    struct nfc_digital_dev,
159 						    cmd_work);
160 
161 	mutex_lock(&ddev->cmd_lock);
162 
163 	cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
164 				       queue);
165 	if (!cmd || cmd->pending) {
166 		mutex_unlock(&ddev->cmd_lock);
167 		return;
168 	}
169 
170 	cmd->pending = 1;
171 
172 	mutex_unlock(&ddev->cmd_lock);
173 
174 	if (cmd->req)
175 		print_hex_dump_debug("DIGITAL TX: ", DUMP_PREFIX_NONE, 16, 1,
176 				     cmd->req->data, cmd->req->len, false);
177 
178 	switch (cmd->type) {
179 	case DIGITAL_CMD_IN_SEND:
180 		rc = ddev->ops->in_send_cmd(ddev, cmd->req, cmd->timeout,
181 					    digital_send_cmd_complete, cmd);
182 		break;
183 
184 	case DIGITAL_CMD_TG_SEND:
185 		rc = ddev->ops->tg_send_cmd(ddev, cmd->req, cmd->timeout,
186 					    digital_send_cmd_complete, cmd);
187 		break;
188 
189 	case DIGITAL_CMD_TG_LISTEN:
190 		rc = ddev->ops->tg_listen(ddev, cmd->timeout,
191 					  digital_send_cmd_complete, cmd);
192 		break;
193 
194 	case DIGITAL_CMD_TG_LISTEN_MDAA:
195 		params = cmd->mdaa_params;
196 
197 		rc = ddev->ops->tg_listen_mdaa(ddev, params, cmd->timeout,
198 					       digital_send_cmd_complete, cmd);
199 		break;
200 
201 	case DIGITAL_CMD_TG_LISTEN_MD:
202 		rc = ddev->ops->tg_listen_md(ddev, cmd->timeout,
203 					       digital_send_cmd_complete, cmd);
204 		break;
205 
206 	default:
207 		pr_err("Unknown cmd type %d\n", cmd->type);
208 		return;
209 	}
210 
211 	if (!rc)
212 		return;
213 
214 	pr_err("in_send_command returned err %d\n", rc);
215 
216 	mutex_lock(&ddev->cmd_lock);
217 	list_del(&cmd->queue);
218 	mutex_unlock(&ddev->cmd_lock);
219 
220 	kfree_skb(cmd->req);
221 	kfree(cmd->mdaa_params);
222 	kfree(cmd);
223 
224 	schedule_work(&ddev->cmd_work);
225 }
226 
227 int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
228 		     struct sk_buff *skb, struct digital_tg_mdaa_params *params,
229 		     u16 timeout, nfc_digital_cmd_complete_t cmd_cb,
230 		     void *cb_context)
231 {
232 	struct digital_cmd *cmd;
233 
234 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
235 	if (!cmd)
236 		return -ENOMEM;
237 
238 	cmd->type = cmd_type;
239 	cmd->timeout = timeout;
240 	cmd->req = skb;
241 	cmd->mdaa_params = params;
242 	cmd->cmd_cb = cmd_cb;
243 	cmd->cb_context = cb_context;
244 	INIT_LIST_HEAD(&cmd->queue);
245 
246 	mutex_lock(&ddev->cmd_lock);
247 	list_add_tail(&cmd->queue, &ddev->cmd_queue);
248 	mutex_unlock(&ddev->cmd_lock);
249 
250 	schedule_work(&ddev->cmd_work);
251 
252 	return 0;
253 }
254 
255 int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
256 {
257 	int rc;
258 
259 	rc = ddev->ops->in_configure_hw(ddev, type, param);
260 	if (rc)
261 		pr_err("in_configure_hw failed: %d\n", rc);
262 
263 	return rc;
264 }
265 
266 int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
267 {
268 	int rc;
269 
270 	rc = ddev->ops->tg_configure_hw(ddev, type, param);
271 	if (rc)
272 		pr_err("tg_configure_hw failed: %d\n", rc);
273 
274 	return rc;
275 }
276 
277 static int digital_tg_listen_mdaa(struct nfc_digital_dev *ddev, u8 rf_tech)
278 {
279 	struct digital_tg_mdaa_params *params;
280 
281 	params = kzalloc(sizeof(*params), GFP_KERNEL);
282 	if (!params)
283 		return -ENOMEM;
284 
285 	params->sens_res = DIGITAL_SENS_RES_NFC_DEP;
286 	get_random_bytes(params->nfcid1, sizeof(params->nfcid1));
287 	params->sel_res = DIGITAL_SEL_RES_NFC_DEP;
288 
289 	params->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
290 	params->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
291 	get_random_bytes(params->nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2);
292 	params->sc = DIGITAL_SENSF_FELICA_SC;
293 
294 	return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MDAA, NULL, params,
295 				500, digital_tg_recv_atr_req, NULL);
296 }
297 
298 static int digital_tg_listen_md(struct nfc_digital_dev *ddev, u8 rf_tech)
299 {
300 	return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MD, NULL, NULL, 500,
301 				digital_tg_recv_md_req, NULL);
302 }
303 
304 int digital_target_found(struct nfc_digital_dev *ddev,
305 			 struct nfc_target *target, u8 protocol)
306 {
307 	int rc;
308 	u8 framing;
309 	u8 rf_tech;
310 	u8 poll_tech_count;
311 	int (*check_crc)(struct sk_buff *skb);
312 	void (*add_crc)(struct sk_buff *skb);
313 
314 	rf_tech = ddev->poll_techs[ddev->poll_tech_index].rf_tech;
315 
316 	switch (protocol) {
317 	case NFC_PROTO_JEWEL:
318 		framing = NFC_DIGITAL_FRAMING_NFCA_T1T;
319 		check_crc = digital_skb_check_crc_b;
320 		add_crc = digital_skb_add_crc_b;
321 		break;
322 
323 	case NFC_PROTO_MIFARE:
324 		framing = NFC_DIGITAL_FRAMING_NFCA_T2T;
325 		check_crc = digital_skb_check_crc_a;
326 		add_crc = digital_skb_add_crc_a;
327 		break;
328 
329 	case NFC_PROTO_FELICA:
330 		framing = NFC_DIGITAL_FRAMING_NFCF_T3T;
331 		check_crc = digital_skb_check_crc_f;
332 		add_crc = digital_skb_add_crc_f;
333 		break;
334 
335 	case NFC_PROTO_NFC_DEP:
336 		if (rf_tech == NFC_DIGITAL_RF_TECH_106A) {
337 			framing = NFC_DIGITAL_FRAMING_NFCA_NFC_DEP;
338 			check_crc = digital_skb_check_crc_a;
339 			add_crc = digital_skb_add_crc_a;
340 		} else {
341 			framing = NFC_DIGITAL_FRAMING_NFCF_NFC_DEP;
342 			check_crc = digital_skb_check_crc_f;
343 			add_crc = digital_skb_add_crc_f;
344 		}
345 		break;
346 
347 	case NFC_PROTO_ISO15693:
348 		framing = NFC_DIGITAL_FRAMING_ISO15693_T5T;
349 		check_crc = digital_skb_check_crc_b;
350 		add_crc = digital_skb_add_crc_b;
351 		break;
352 
353 	case NFC_PROTO_ISO14443:
354 		framing = NFC_DIGITAL_FRAMING_NFCA_T4T;
355 		check_crc = digital_skb_check_crc_a;
356 		add_crc = digital_skb_add_crc_a;
357 		break;
358 
359 	case NFC_PROTO_ISO14443_B:
360 		framing = NFC_DIGITAL_FRAMING_NFCB_T4T;
361 		check_crc = digital_skb_check_crc_b;
362 		add_crc = digital_skb_add_crc_b;
363 		break;
364 
365 	default:
366 		pr_err("Invalid protocol %d\n", protocol);
367 		return -EINVAL;
368 	}
369 
370 	pr_debug("rf_tech=%d, protocol=%d\n", rf_tech, protocol);
371 
372 	ddev->curr_rf_tech = rf_tech;
373 
374 	if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
375 		ddev->skb_add_crc = digital_skb_add_crc_none;
376 		ddev->skb_check_crc = digital_skb_check_crc_none;
377 	} else {
378 		ddev->skb_add_crc = add_crc;
379 		ddev->skb_check_crc = check_crc;
380 	}
381 
382 	rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, framing);
383 	if (rc)
384 		return rc;
385 
386 	target->supported_protocols = (1 << protocol);
387 
388 	poll_tech_count = ddev->poll_tech_count;
389 	ddev->poll_tech_count = 0;
390 
391 	rc = nfc_targets_found(ddev->nfc_dev, target, 1);
392 	if (rc) {
393 		ddev->poll_tech_count = poll_tech_count;
394 		return rc;
395 	}
396 
397 	return 0;
398 }
399 
400 void digital_poll_next_tech(struct nfc_digital_dev *ddev)
401 {
402 	u8 rand_mod;
403 
404 	digital_switch_rf(ddev, 0);
405 
406 	mutex_lock(&ddev->poll_lock);
407 
408 	if (!ddev->poll_tech_count) {
409 		mutex_unlock(&ddev->poll_lock);
410 		return;
411 	}
412 
413 	get_random_bytes(&rand_mod, sizeof(rand_mod));
414 	ddev->poll_tech_index = rand_mod % ddev->poll_tech_count;
415 
416 	mutex_unlock(&ddev->poll_lock);
417 
418 	schedule_delayed_work(&ddev->poll_work,
419 			      msecs_to_jiffies(DIGITAL_POLL_INTERVAL));
420 }
421 
422 static void digital_wq_poll(struct work_struct *work)
423 {
424 	int rc;
425 	struct digital_poll_tech *poll_tech;
426 	struct nfc_digital_dev *ddev = container_of(work,
427 						    struct nfc_digital_dev,
428 						    poll_work.work);
429 	mutex_lock(&ddev->poll_lock);
430 
431 	if (!ddev->poll_tech_count) {
432 		mutex_unlock(&ddev->poll_lock);
433 		return;
434 	}
435 
436 	poll_tech = &ddev->poll_techs[ddev->poll_tech_index];
437 
438 	mutex_unlock(&ddev->poll_lock);
439 
440 	rc = poll_tech->poll_func(ddev, poll_tech->rf_tech);
441 	if (rc)
442 		digital_poll_next_tech(ddev);
443 }
444 
445 static void digital_add_poll_tech(struct nfc_digital_dev *ddev, u8 rf_tech,
446 				  digital_poll_t poll_func)
447 {
448 	struct digital_poll_tech *poll_tech;
449 
450 	if (ddev->poll_tech_count >= NFC_DIGITAL_POLL_MODE_COUNT_MAX)
451 		return;
452 
453 	poll_tech = &ddev->poll_techs[ddev->poll_tech_count++];
454 
455 	poll_tech->rf_tech = rf_tech;
456 	poll_tech->poll_func = poll_func;
457 }
458 
459 /**
460  * digital_start_poll - start_poll operation
461  * @nfc_dev: device to be polled
462  * @im_protocols: bitset of nfc initiator protocols to be used for polling
463  * @tm_protocols: bitset of nfc transport protocols to be used for polling
464  *
465  * For every supported protocol, the corresponding polling function is added
466  * to the table of polling technologies (ddev->poll_techs[]) using
467  * digital_add_poll_tech().
468  * When a polling function fails (by timeout or protocol error) the next one is
469  * schedule by digital_poll_next_tech() on the poll workqueue (ddev->poll_work).
470  */
471 static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
472 			      __u32 tm_protocols)
473 {
474 	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
475 	u32 matching_im_protocols, matching_tm_protocols;
476 
477 	pr_debug("protocols: im 0x%x, tm 0x%x, supported 0x%x\n", im_protocols,
478 		 tm_protocols, ddev->protocols);
479 
480 	matching_im_protocols = ddev->protocols & im_protocols;
481 	matching_tm_protocols = ddev->protocols & tm_protocols;
482 
483 	if (!matching_im_protocols && !matching_tm_protocols) {
484 		pr_err("Unknown protocol\n");
485 		return -EINVAL;
486 	}
487 
488 	if (ddev->poll_tech_count) {
489 		pr_err("Already polling\n");
490 		return -EBUSY;
491 	}
492 
493 	if (ddev->curr_protocol) {
494 		pr_err("A target is already active\n");
495 		return -EBUSY;
496 	}
497 
498 	ddev->poll_tech_count = 0;
499 	ddev->poll_tech_index = 0;
500 
501 	if (matching_im_protocols & DIGITAL_PROTO_NFCA_RF_TECH)
502 		digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
503 				      digital_in_send_sens_req);
504 
505 	if (matching_im_protocols & DIGITAL_PROTO_NFCB_RF_TECH)
506 		digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106B,
507 				      digital_in_send_sensb_req);
508 
509 	if (matching_im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) {
510 		digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
511 				      digital_in_send_sensf_req);
512 
513 		digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
514 				      digital_in_send_sensf_req);
515 	}
516 
517 	if (matching_im_protocols & DIGITAL_PROTO_ISO15693_RF_TECH)
518 		digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_ISO15693,
519 				      digital_in_send_iso15693_inv_req);
520 
521 	if (matching_tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
522 		if (ddev->ops->tg_listen_mdaa) {
523 			digital_add_poll_tech(ddev, 0,
524 					      digital_tg_listen_mdaa);
525 		} else if (ddev->ops->tg_listen_md) {
526 			digital_add_poll_tech(ddev, 0,
527 					      digital_tg_listen_md);
528 		} else {
529 			digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
530 					      digital_tg_listen_nfca);
531 
532 			digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
533 					      digital_tg_listen_nfcf);
534 
535 			digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
536 					      digital_tg_listen_nfcf);
537 		}
538 	}
539 
540 	if (!ddev->poll_tech_count) {
541 		pr_err("Unsupported protocols: im=0x%x, tm=0x%x\n",
542 		       matching_im_protocols, matching_tm_protocols);
543 		return -EINVAL;
544 	}
545 
546 	schedule_delayed_work(&ddev->poll_work, 0);
547 
548 	return 0;
549 }
550 
551 static void digital_stop_poll(struct nfc_dev *nfc_dev)
552 {
553 	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
554 
555 	mutex_lock(&ddev->poll_lock);
556 
557 	if (!ddev->poll_tech_count) {
558 		pr_err("Polling operation was not running\n");
559 		mutex_unlock(&ddev->poll_lock);
560 		return;
561 	}
562 
563 	ddev->poll_tech_count = 0;
564 
565 	mutex_unlock(&ddev->poll_lock);
566 
567 	cancel_delayed_work_sync(&ddev->poll_work);
568 
569 	digital_abort_cmd(ddev);
570 }
571 
572 static int digital_dev_up(struct nfc_dev *nfc_dev)
573 {
574 	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
575 
576 	digital_switch_rf(ddev, 1);
577 
578 	return 0;
579 }
580 
581 static int digital_dev_down(struct nfc_dev *nfc_dev)
582 {
583 	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
584 
585 	digital_switch_rf(ddev, 0);
586 
587 	return 0;
588 }
589 
590 static int digital_dep_link_up(struct nfc_dev *nfc_dev,
591 			       struct nfc_target *target,
592 			       __u8 comm_mode, __u8 *gb, size_t gb_len)
593 {
594 	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
595 	int rc;
596 
597 	rc = digital_in_send_atr_req(ddev, target, comm_mode, gb, gb_len);
598 
599 	if (!rc)
600 		ddev->curr_protocol = NFC_PROTO_NFC_DEP;
601 
602 	return rc;
603 }
604 
605 static int digital_dep_link_down(struct nfc_dev *nfc_dev)
606 {
607 	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
608 
609 	digital_abort_cmd(ddev);
610 
611 	ddev->curr_protocol = 0;
612 
613 	return 0;
614 }
615 
616 static int digital_activate_target(struct nfc_dev *nfc_dev,
617 				   struct nfc_target *target, __u32 protocol)
618 {
619 	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
620 
621 	if (ddev->poll_tech_count) {
622 		pr_err("Can't activate a target while polling\n");
623 		return -EBUSY;
624 	}
625 
626 	if (ddev->curr_protocol) {
627 		pr_err("A target is already active\n");
628 		return -EBUSY;
629 	}
630 
631 	ddev->curr_protocol = protocol;
632 
633 	return 0;
634 }
635 
636 static void digital_deactivate_target(struct nfc_dev *nfc_dev,
637 				      struct nfc_target *target,
638 				      u8 mode)
639 {
640 	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
641 
642 	if (!ddev->curr_protocol) {
643 		pr_err("No active target\n");
644 		return;
645 	}
646 
647 	digital_abort_cmd(ddev);
648 	ddev->curr_protocol = 0;
649 }
650 
651 static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb)
652 {
653 	struct nfc_digital_dev *ddev = nfc_get_drvdata(dev);
654 
655 	return digital_tg_send_dep_res(ddev, skb);
656 }
657 
658 static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg,
659 				     struct sk_buff *resp)
660 {
661 	struct digital_data_exch *data_exch = arg;
662 	int rc;
663 
664 	if (IS_ERR(resp)) {
665 		rc = PTR_ERR(resp);
666 		resp = NULL;
667 		goto done;
668 	}
669 
670 	if (ddev->curr_protocol == NFC_PROTO_MIFARE) {
671 		rc = digital_in_recv_mifare_res(resp);
672 		/* crc check is done in digital_in_recv_mifare_res() */
673 		goto done;
674 	}
675 
676 	if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
677 	    (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
678 		rc = digital_in_iso_dep_pull_sod(ddev, resp);
679 		if (rc)
680 			goto done;
681 	}
682 
683 	rc = ddev->skb_check_crc(resp);
684 
685 done:
686 	if (rc) {
687 		kfree_skb(resp);
688 		resp = NULL;
689 	}
690 
691 	data_exch->cb(data_exch->cb_context, resp, rc);
692 
693 	kfree(data_exch);
694 }
695 
696 static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
697 			   struct sk_buff *skb, data_exchange_cb_t cb,
698 			   void *cb_context)
699 {
700 	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
701 	struct digital_data_exch *data_exch;
702 	int rc;
703 
704 	data_exch = kzalloc(sizeof(*data_exch), GFP_KERNEL);
705 	if (!data_exch)
706 		return -ENOMEM;
707 
708 	data_exch->cb = cb;
709 	data_exch->cb_context = cb_context;
710 
711 	if (ddev->curr_protocol == NFC_PROTO_NFC_DEP) {
712 		rc = digital_in_send_dep_req(ddev, target, skb, data_exch);
713 		goto exit;
714 	}
715 
716 	if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
717 	    (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
718 		rc = digital_in_iso_dep_push_sod(ddev, skb);
719 		if (rc)
720 			goto exit;
721 	}
722 
723 	ddev->skb_add_crc(skb);
724 
725 	rc = digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete,
726 				 data_exch);
727 
728 exit:
729 	if (rc)
730 		kfree(data_exch);
731 
732 	return rc;
733 }
734 
735 static const struct nfc_ops digital_nfc_ops = {
736 	.dev_up = digital_dev_up,
737 	.dev_down = digital_dev_down,
738 	.start_poll = digital_start_poll,
739 	.stop_poll = digital_stop_poll,
740 	.dep_link_up = digital_dep_link_up,
741 	.dep_link_down = digital_dep_link_down,
742 	.activate_target = digital_activate_target,
743 	.deactivate_target = digital_deactivate_target,
744 	.tm_send = digital_tg_send,
745 	.im_transceive = digital_in_send,
746 };
747 
748 struct nfc_digital_dev *nfc_digital_allocate_device(const struct nfc_digital_ops *ops,
749 					    __u32 supported_protocols,
750 					    __u32 driver_capabilities,
751 					    int tx_headroom, int tx_tailroom)
752 {
753 	struct nfc_digital_dev *ddev;
754 
755 	if (!ops->in_configure_hw || !ops->in_send_cmd || !ops->tg_listen ||
756 	    !ops->tg_configure_hw || !ops->tg_send_cmd || !ops->abort_cmd ||
757 	    !ops->switch_rf || (ops->tg_listen_md && !ops->tg_get_rf_tech))
758 		return NULL;
759 
760 	ddev = kzalloc(sizeof(*ddev), GFP_KERNEL);
761 	if (!ddev)
762 		return NULL;
763 
764 	ddev->driver_capabilities = driver_capabilities;
765 	ddev->ops = ops;
766 
767 	mutex_init(&ddev->cmd_lock);
768 	INIT_LIST_HEAD(&ddev->cmd_queue);
769 
770 	INIT_WORK(&ddev->cmd_work, digital_wq_cmd);
771 	INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete);
772 
773 	mutex_init(&ddev->poll_lock);
774 	INIT_DELAYED_WORK(&ddev->poll_work, digital_wq_poll);
775 
776 	if (supported_protocols & NFC_PROTO_JEWEL_MASK)
777 		ddev->protocols |= NFC_PROTO_JEWEL_MASK;
778 	if (supported_protocols & NFC_PROTO_MIFARE_MASK)
779 		ddev->protocols |= NFC_PROTO_MIFARE_MASK;
780 	if (supported_protocols & NFC_PROTO_FELICA_MASK)
781 		ddev->protocols |= NFC_PROTO_FELICA_MASK;
782 	if (supported_protocols & NFC_PROTO_NFC_DEP_MASK)
783 		ddev->protocols |= NFC_PROTO_NFC_DEP_MASK;
784 	if (supported_protocols & NFC_PROTO_ISO15693_MASK)
785 		ddev->protocols |= NFC_PROTO_ISO15693_MASK;
786 	if (supported_protocols & NFC_PROTO_ISO14443_MASK)
787 		ddev->protocols |= NFC_PROTO_ISO14443_MASK;
788 	if (supported_protocols & NFC_PROTO_ISO14443_B_MASK)
789 		ddev->protocols |= NFC_PROTO_ISO14443_B_MASK;
790 
791 	ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
792 	ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
793 
794 	ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols,
795 					    ddev->tx_headroom,
796 					    ddev->tx_tailroom);
797 	if (!ddev->nfc_dev) {
798 		pr_err("nfc_allocate_device failed\n");
799 		goto free_dev;
800 	}
801 
802 	nfc_set_drvdata(ddev->nfc_dev, ddev);
803 
804 	return ddev;
805 
806 free_dev:
807 	kfree(ddev);
808 
809 	return NULL;
810 }
811 EXPORT_SYMBOL(nfc_digital_allocate_device);
812 
813 void nfc_digital_free_device(struct nfc_digital_dev *ddev)
814 {
815 	nfc_free_device(ddev->nfc_dev);
816 	kfree(ddev);
817 }
818 EXPORT_SYMBOL(nfc_digital_free_device);
819 
820 int nfc_digital_register_device(struct nfc_digital_dev *ddev)
821 {
822 	return nfc_register_device(ddev->nfc_dev);
823 }
824 EXPORT_SYMBOL(nfc_digital_register_device);
825 
826 void nfc_digital_unregister_device(struct nfc_digital_dev *ddev)
827 {
828 	struct digital_cmd *cmd, *n;
829 
830 	nfc_unregister_device(ddev->nfc_dev);
831 
832 	mutex_lock(&ddev->poll_lock);
833 	ddev->poll_tech_count = 0;
834 	mutex_unlock(&ddev->poll_lock);
835 
836 	cancel_delayed_work_sync(&ddev->poll_work);
837 	cancel_work_sync(&ddev->cmd_work);
838 	cancel_work_sync(&ddev->cmd_complete_work);
839 
840 	list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) {
841 		list_del(&cmd->queue);
842 
843 		/* Call the command callback if any and pass it a ENODEV error.
844 		 * This gives a chance to the command issuer to free any
845 		 * allocated buffer.
846 		 */
847 		if (cmd->cmd_cb)
848 			cmd->cmd_cb(ddev, cmd->cb_context, ERR_PTR(-ENODEV));
849 
850 		kfree(cmd->mdaa_params);
851 		kfree(cmd);
852 	}
853 }
854 EXPORT_SYMBOL(nfc_digital_unregister_device);
855 
856 MODULE_LICENSE("GPL");
857