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