xref: /openbmc/linux/net/nfc/nci/core.c (revision dd1431e5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  The NFC Controller Interface is the communication protocol between an
4  *  NFC Controller (NFCC) and a Device Host (DH).
5  *
6  *  Copyright (C) 2011 Texas Instruments, Inc.
7  *  Copyright (C) 2014 Marvell International Ltd.
8  *
9  *  Written by Ilan Elias <ilane@ti.com>
10  *
11  *  Acknowledgements:
12  *  This file is based on hci_core.c, which was written
13  *  by Maxim Krasnyansky.
14  */
15 
16 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
17 
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/workqueue.h>
22 #include <linux/completion.h>
23 #include <linux/export.h>
24 #include <linux/sched.h>
25 #include <linux/bitops.h>
26 #include <linux/skbuff.h>
27 
28 #include "../nfc.h"
29 #include <net/nfc/nci.h>
30 #include <net/nfc/nci_core.h>
31 #include <linux/nfc.h>
32 
33 struct core_conn_create_data {
34 	int length;
35 	struct nci_core_conn_create_cmd *cmd;
36 };
37 
38 static void nci_cmd_work(struct work_struct *work);
39 static void nci_rx_work(struct work_struct *work);
40 static void nci_tx_work(struct work_struct *work);
41 
42 struct nci_conn_info *nci_get_conn_info_by_conn_id(struct nci_dev *ndev,
43 						   int conn_id)
44 {
45 	struct nci_conn_info *conn_info;
46 
47 	list_for_each_entry(conn_info, &ndev->conn_info_list, list) {
48 		if (conn_info->conn_id == conn_id)
49 			return conn_info;
50 	}
51 
52 	return NULL;
53 }
54 
55 int nci_get_conn_info_by_dest_type_params(struct nci_dev *ndev, u8 dest_type,
56 					  struct dest_spec_params *params)
57 {
58 	struct nci_conn_info *conn_info;
59 
60 	list_for_each_entry(conn_info, &ndev->conn_info_list, list) {
61 		if (conn_info->dest_type == dest_type) {
62 			if (!params)
63 				return conn_info->conn_id;
64 
65 			if (params->id == conn_info->dest_params->id &&
66 			    params->protocol == conn_info->dest_params->protocol)
67 				return conn_info->conn_id;
68 		}
69 	}
70 
71 	return -EINVAL;
72 }
73 EXPORT_SYMBOL(nci_get_conn_info_by_dest_type_params);
74 
75 /* ---- NCI requests ---- */
76 
77 void nci_req_complete(struct nci_dev *ndev, int result)
78 {
79 	if (ndev->req_status == NCI_REQ_PEND) {
80 		ndev->req_result = result;
81 		ndev->req_status = NCI_REQ_DONE;
82 		complete(&ndev->req_completion);
83 	}
84 }
85 EXPORT_SYMBOL(nci_req_complete);
86 
87 static void nci_req_cancel(struct nci_dev *ndev, int err)
88 {
89 	if (ndev->req_status == NCI_REQ_PEND) {
90 		ndev->req_result = err;
91 		ndev->req_status = NCI_REQ_CANCELED;
92 		complete(&ndev->req_completion);
93 	}
94 }
95 
96 /* Execute request and wait for completion. */
97 static int __nci_request(struct nci_dev *ndev,
98 			 void (*req)(struct nci_dev *ndev, unsigned long opt),
99 			 unsigned long opt, __u32 timeout)
100 {
101 	int rc = 0;
102 	long completion_rc;
103 
104 	ndev->req_status = NCI_REQ_PEND;
105 
106 	reinit_completion(&ndev->req_completion);
107 	req(ndev, opt);
108 	completion_rc =
109 		wait_for_completion_interruptible_timeout(&ndev->req_completion,
110 							  timeout);
111 
112 	pr_debug("wait_for_completion return %ld\n", completion_rc);
113 
114 	if (completion_rc > 0) {
115 		switch (ndev->req_status) {
116 		case NCI_REQ_DONE:
117 			rc = nci_to_errno(ndev->req_result);
118 			break;
119 
120 		case NCI_REQ_CANCELED:
121 			rc = -ndev->req_result;
122 			break;
123 
124 		default:
125 			rc = -ETIMEDOUT;
126 			break;
127 		}
128 	} else {
129 		pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
130 		       completion_rc);
131 
132 		rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
133 	}
134 
135 	ndev->req_status = ndev->req_result = 0;
136 
137 	return rc;
138 }
139 
140 inline int nci_request(struct nci_dev *ndev,
141 		       void (*req)(struct nci_dev *ndev,
142 				   unsigned long opt),
143 		       unsigned long opt, __u32 timeout)
144 {
145 	int rc;
146 
147 	if (!test_bit(NCI_UP, &ndev->flags))
148 		return -ENETDOWN;
149 
150 	/* Serialize all requests */
151 	mutex_lock(&ndev->req_lock);
152 	rc = __nci_request(ndev, req, opt, timeout);
153 	mutex_unlock(&ndev->req_lock);
154 
155 	return rc;
156 }
157 
158 static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
159 {
160 	struct nci_core_reset_cmd cmd;
161 
162 	cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
163 	nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
164 }
165 
166 static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
167 {
168 	u8 plen = 0;
169 
170 	if (opt)
171 		plen = sizeof(struct nci_core_init_v2_cmd);
172 
173 	nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, plen, (void *)opt);
174 }
175 
176 static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
177 {
178 	struct nci_rf_disc_map_cmd cmd;
179 	struct disc_map_config *cfg = cmd.mapping_configs;
180 	__u8 *num = &cmd.num_mapping_configs;
181 	int i;
182 
183 	/* set rf mapping configurations */
184 	*num = 0;
185 
186 	/* by default mapping is set to NCI_RF_INTERFACE_FRAME */
187 	for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
188 		if (ndev->supported_rf_interfaces[i] ==
189 		    NCI_RF_INTERFACE_ISO_DEP) {
190 			cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
191 			cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
192 				NCI_DISC_MAP_MODE_LISTEN;
193 			cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
194 			(*num)++;
195 		} else if (ndev->supported_rf_interfaces[i] ==
196 			   NCI_RF_INTERFACE_NFC_DEP) {
197 			cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
198 			cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
199 				NCI_DISC_MAP_MODE_LISTEN;
200 			cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
201 			(*num)++;
202 		}
203 
204 		if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
205 			break;
206 	}
207 
208 	nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
209 		     (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
210 }
211 
212 struct nci_set_config_param {
213 	__u8	id;
214 	size_t	len;
215 	__u8	*val;
216 };
217 
218 static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt)
219 {
220 	struct nci_set_config_param *param = (struct nci_set_config_param *)opt;
221 	struct nci_core_set_config_cmd cmd;
222 
223 	BUG_ON(param->len > NCI_MAX_PARAM_LEN);
224 
225 	cmd.num_params = 1;
226 	cmd.param.id = param->id;
227 	cmd.param.len = param->len;
228 	memcpy(cmd.param.val, param->val, param->len);
229 
230 	nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
231 }
232 
233 struct nci_rf_discover_param {
234 	__u32	im_protocols;
235 	__u32	tm_protocols;
236 };
237 
238 static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
239 {
240 	struct nci_rf_discover_param *param =
241 		(struct nci_rf_discover_param *)opt;
242 	struct nci_rf_disc_cmd cmd;
243 
244 	cmd.num_disc_configs = 0;
245 
246 	if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
247 	    (param->im_protocols & NFC_PROTO_JEWEL_MASK ||
248 	     param->im_protocols & NFC_PROTO_MIFARE_MASK ||
249 	     param->im_protocols & NFC_PROTO_ISO14443_MASK ||
250 	     param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
251 		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
252 			NCI_NFC_A_PASSIVE_POLL_MODE;
253 		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
254 		cmd.num_disc_configs++;
255 	}
256 
257 	if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
258 	    (param->im_protocols & NFC_PROTO_ISO14443_B_MASK)) {
259 		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
260 			NCI_NFC_B_PASSIVE_POLL_MODE;
261 		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
262 		cmd.num_disc_configs++;
263 	}
264 
265 	if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
266 	    (param->im_protocols & NFC_PROTO_FELICA_MASK ||
267 	     param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
268 		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
269 			NCI_NFC_F_PASSIVE_POLL_MODE;
270 		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
271 		cmd.num_disc_configs++;
272 	}
273 
274 	if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
275 	    (param->im_protocols & NFC_PROTO_ISO15693_MASK)) {
276 		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
277 			NCI_NFC_V_PASSIVE_POLL_MODE;
278 		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
279 		cmd.num_disc_configs++;
280 	}
281 
282 	if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS - 1) &&
283 	    (param->tm_protocols & NFC_PROTO_NFC_DEP_MASK)) {
284 		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
285 			NCI_NFC_A_PASSIVE_LISTEN_MODE;
286 		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
287 		cmd.num_disc_configs++;
288 		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
289 			NCI_NFC_F_PASSIVE_LISTEN_MODE;
290 		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
291 		cmd.num_disc_configs++;
292 	}
293 
294 	nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
295 		     (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
296 		     &cmd);
297 }
298 
299 struct nci_rf_discover_select_param {
300 	__u8	rf_discovery_id;
301 	__u8	rf_protocol;
302 };
303 
304 static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt)
305 {
306 	struct nci_rf_discover_select_param *param =
307 		(struct nci_rf_discover_select_param *)opt;
308 	struct nci_rf_discover_select_cmd cmd;
309 
310 	cmd.rf_discovery_id = param->rf_discovery_id;
311 	cmd.rf_protocol = param->rf_protocol;
312 
313 	switch (cmd.rf_protocol) {
314 	case NCI_RF_PROTOCOL_ISO_DEP:
315 		cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
316 		break;
317 
318 	case NCI_RF_PROTOCOL_NFC_DEP:
319 		cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
320 		break;
321 
322 	default:
323 		cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
324 		break;
325 	}
326 
327 	nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
328 		     sizeof(struct nci_rf_discover_select_cmd), &cmd);
329 }
330 
331 static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
332 {
333 	struct nci_rf_deactivate_cmd cmd;
334 
335 	cmd.type = opt;
336 
337 	nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
338 		     sizeof(struct nci_rf_deactivate_cmd), &cmd);
339 }
340 
341 struct nci_cmd_param {
342 	__u16 opcode;
343 	size_t len;
344 	__u8 *payload;
345 };
346 
347 static void nci_generic_req(struct nci_dev *ndev, unsigned long opt)
348 {
349 	struct nci_cmd_param *param =
350 		(struct nci_cmd_param *)opt;
351 
352 	nci_send_cmd(ndev, param->opcode, param->len, param->payload);
353 }
354 
355 int nci_prop_cmd(struct nci_dev *ndev, __u8 oid, size_t len, __u8 *payload)
356 {
357 	struct nci_cmd_param param;
358 
359 	param.opcode = nci_opcode_pack(NCI_GID_PROPRIETARY, oid);
360 	param.len = len;
361 	param.payload = payload;
362 
363 	return __nci_request(ndev, nci_generic_req, (unsigned long)&param,
364 			     msecs_to_jiffies(NCI_CMD_TIMEOUT));
365 }
366 EXPORT_SYMBOL(nci_prop_cmd);
367 
368 int nci_core_cmd(struct nci_dev *ndev, __u16 opcode, size_t len, __u8 *payload)
369 {
370 	struct nci_cmd_param param;
371 
372 	param.opcode = opcode;
373 	param.len = len;
374 	param.payload = payload;
375 
376 	return __nci_request(ndev, nci_generic_req, (unsigned long)&param,
377 			     msecs_to_jiffies(NCI_CMD_TIMEOUT));
378 }
379 EXPORT_SYMBOL(nci_core_cmd);
380 
381 int nci_core_reset(struct nci_dev *ndev)
382 {
383 	return __nci_request(ndev, nci_reset_req, 0,
384 			     msecs_to_jiffies(NCI_RESET_TIMEOUT));
385 }
386 EXPORT_SYMBOL(nci_core_reset);
387 
388 int nci_core_init(struct nci_dev *ndev)
389 {
390 	return __nci_request(ndev, nci_init_req, 0,
391 			     msecs_to_jiffies(NCI_INIT_TIMEOUT));
392 }
393 EXPORT_SYMBOL(nci_core_init);
394 
395 struct nci_loopback_data {
396 	u8 conn_id;
397 	struct sk_buff *data;
398 };
399 
400 static void nci_send_data_req(struct nci_dev *ndev, unsigned long opt)
401 {
402 	struct nci_loopback_data *data = (struct nci_loopback_data *)opt;
403 
404 	nci_send_data(ndev, data->conn_id, data->data);
405 }
406 
407 static void nci_nfcc_loopback_cb(void *context, struct sk_buff *skb, int err)
408 {
409 	struct nci_dev *ndev = (struct nci_dev *)context;
410 	struct nci_conn_info    *conn_info;
411 
412 	conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
413 	if (!conn_info) {
414 		nci_req_complete(ndev, NCI_STATUS_REJECTED);
415 		return;
416 	}
417 
418 	conn_info->rx_skb = skb;
419 
420 	nci_req_complete(ndev, NCI_STATUS_OK);
421 }
422 
423 int nci_nfcc_loopback(struct nci_dev *ndev, void *data, size_t data_len,
424 		      struct sk_buff **resp)
425 {
426 	int r;
427 	struct nci_loopback_data loopback_data;
428 	struct nci_conn_info *conn_info;
429 	struct sk_buff *skb;
430 	int conn_id = nci_get_conn_info_by_dest_type_params(ndev,
431 					NCI_DESTINATION_NFCC_LOOPBACK, NULL);
432 
433 	if (conn_id < 0) {
434 		r = nci_core_conn_create(ndev, NCI_DESTINATION_NFCC_LOOPBACK,
435 					 0, 0, NULL);
436 		if (r != NCI_STATUS_OK)
437 			return r;
438 
439 		conn_id = nci_get_conn_info_by_dest_type_params(ndev,
440 					NCI_DESTINATION_NFCC_LOOPBACK,
441 					NULL);
442 	}
443 
444 	conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
445 	if (!conn_info)
446 		return -EPROTO;
447 
448 	/* store cb and context to be used on receiving data */
449 	conn_info->data_exchange_cb = nci_nfcc_loopback_cb;
450 	conn_info->data_exchange_cb_context = ndev;
451 
452 	skb = nci_skb_alloc(ndev, NCI_DATA_HDR_SIZE + data_len, GFP_KERNEL);
453 	if (!skb)
454 		return -ENOMEM;
455 
456 	skb_reserve(skb, NCI_DATA_HDR_SIZE);
457 	skb_put_data(skb, data, data_len);
458 
459 	loopback_data.conn_id = conn_id;
460 	loopback_data.data = skb;
461 
462 	ndev->cur_conn_id = conn_id;
463 	r = nci_request(ndev, nci_send_data_req, (unsigned long)&loopback_data,
464 			msecs_to_jiffies(NCI_DATA_TIMEOUT));
465 	if (r == NCI_STATUS_OK && resp)
466 		*resp = conn_info->rx_skb;
467 
468 	return r;
469 }
470 EXPORT_SYMBOL(nci_nfcc_loopback);
471 
472 static int nci_open_device(struct nci_dev *ndev)
473 {
474 	int rc = 0;
475 
476 	mutex_lock(&ndev->req_lock);
477 
478 	if (test_bit(NCI_UP, &ndev->flags)) {
479 		rc = -EALREADY;
480 		goto done;
481 	}
482 
483 	if (ndev->ops->open(ndev)) {
484 		rc = -EIO;
485 		goto done;
486 	}
487 
488 	atomic_set(&ndev->cmd_cnt, 1);
489 
490 	set_bit(NCI_INIT, &ndev->flags);
491 
492 	if (ndev->ops->init)
493 		rc = ndev->ops->init(ndev);
494 
495 	if (!rc) {
496 		rc = __nci_request(ndev, nci_reset_req, 0,
497 				   msecs_to_jiffies(NCI_RESET_TIMEOUT));
498 	}
499 
500 	if (!rc && ndev->ops->setup) {
501 		rc = ndev->ops->setup(ndev);
502 	}
503 
504 	if (!rc) {
505 		struct nci_core_init_v2_cmd nci_init_v2_cmd = {
506 			.feature1 = NCI_FEATURE_DISABLE,
507 			.feature2 = NCI_FEATURE_DISABLE
508 		};
509 		unsigned long opt = 0;
510 
511 		if (ndev->nci_ver & NCI_VER_2_MASK)
512 			opt = (unsigned long)&nci_init_v2_cmd;
513 
514 		rc = __nci_request(ndev, nci_init_req, opt,
515 				   msecs_to_jiffies(NCI_INIT_TIMEOUT));
516 	}
517 
518 	if (!rc && ndev->ops->post_setup)
519 		rc = ndev->ops->post_setup(ndev);
520 
521 	if (!rc) {
522 		rc = __nci_request(ndev, nci_init_complete_req, 0,
523 				   msecs_to_jiffies(NCI_INIT_TIMEOUT));
524 	}
525 
526 	clear_bit(NCI_INIT, &ndev->flags);
527 
528 	if (!rc) {
529 		set_bit(NCI_UP, &ndev->flags);
530 		nci_clear_target_list(ndev);
531 		atomic_set(&ndev->state, NCI_IDLE);
532 	} else {
533 		/* Init failed, cleanup */
534 		skb_queue_purge(&ndev->cmd_q);
535 		skb_queue_purge(&ndev->rx_q);
536 		skb_queue_purge(&ndev->tx_q);
537 
538 		ndev->ops->close(ndev);
539 		ndev->flags = 0;
540 	}
541 
542 done:
543 	mutex_unlock(&ndev->req_lock);
544 	return rc;
545 }
546 
547 static int nci_close_device(struct nci_dev *ndev)
548 {
549 	nci_req_cancel(ndev, ENODEV);
550 	mutex_lock(&ndev->req_lock);
551 
552 	if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
553 		del_timer_sync(&ndev->cmd_timer);
554 		del_timer_sync(&ndev->data_timer);
555 		mutex_unlock(&ndev->req_lock);
556 		return 0;
557 	}
558 
559 	/* Drop RX and TX queues */
560 	skb_queue_purge(&ndev->rx_q);
561 	skb_queue_purge(&ndev->tx_q);
562 
563 	/* Flush RX and TX wq */
564 	flush_workqueue(ndev->rx_wq);
565 	flush_workqueue(ndev->tx_wq);
566 
567 	/* Reset device */
568 	skb_queue_purge(&ndev->cmd_q);
569 	atomic_set(&ndev->cmd_cnt, 1);
570 
571 	set_bit(NCI_INIT, &ndev->flags);
572 	__nci_request(ndev, nci_reset_req, 0,
573 		      msecs_to_jiffies(NCI_RESET_TIMEOUT));
574 
575 	/* After this point our queues are empty
576 	 * and no works are scheduled.
577 	 */
578 	ndev->ops->close(ndev);
579 
580 	clear_bit(NCI_INIT, &ndev->flags);
581 
582 	/* Flush cmd wq */
583 	flush_workqueue(ndev->cmd_wq);
584 
585 	del_timer_sync(&ndev->cmd_timer);
586 
587 	/* Clear flags */
588 	ndev->flags = 0;
589 
590 	mutex_unlock(&ndev->req_lock);
591 
592 	return 0;
593 }
594 
595 /* NCI command timer function */
596 static void nci_cmd_timer(struct timer_list *t)
597 {
598 	struct nci_dev *ndev = from_timer(ndev, t, cmd_timer);
599 
600 	atomic_set(&ndev->cmd_cnt, 1);
601 	queue_work(ndev->cmd_wq, &ndev->cmd_work);
602 }
603 
604 /* NCI data exchange timer function */
605 static void nci_data_timer(struct timer_list *t)
606 {
607 	struct nci_dev *ndev = from_timer(ndev, t, data_timer);
608 
609 	set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
610 	queue_work(ndev->rx_wq, &ndev->rx_work);
611 }
612 
613 static int nci_dev_up(struct nfc_dev *nfc_dev)
614 {
615 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
616 
617 	return nci_open_device(ndev);
618 }
619 
620 static int nci_dev_down(struct nfc_dev *nfc_dev)
621 {
622 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
623 
624 	return nci_close_device(ndev);
625 }
626 
627 int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, __u8 *val)
628 {
629 	struct nci_set_config_param param;
630 
631 	if (!val || !len)
632 		return 0;
633 
634 	param.id = id;
635 	param.len = len;
636 	param.val = val;
637 
638 	return __nci_request(ndev, nci_set_config_req, (unsigned long)&param,
639 			     msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
640 }
641 EXPORT_SYMBOL(nci_set_config);
642 
643 static void nci_nfcee_discover_req(struct nci_dev *ndev, unsigned long opt)
644 {
645 	struct nci_nfcee_discover_cmd cmd;
646 	__u8 action = opt;
647 
648 	cmd.discovery_action = action;
649 
650 	nci_send_cmd(ndev, NCI_OP_NFCEE_DISCOVER_CMD, 1, &cmd);
651 }
652 
653 int nci_nfcee_discover(struct nci_dev *ndev, u8 action)
654 {
655 	return __nci_request(ndev, nci_nfcee_discover_req, action,
656 				msecs_to_jiffies(NCI_CMD_TIMEOUT));
657 }
658 EXPORT_SYMBOL(nci_nfcee_discover);
659 
660 static void nci_nfcee_mode_set_req(struct nci_dev *ndev, unsigned long opt)
661 {
662 	struct nci_nfcee_mode_set_cmd *cmd =
663 					(struct nci_nfcee_mode_set_cmd *)opt;
664 
665 	nci_send_cmd(ndev, NCI_OP_NFCEE_MODE_SET_CMD,
666 		     sizeof(struct nci_nfcee_mode_set_cmd), cmd);
667 }
668 
669 int nci_nfcee_mode_set(struct nci_dev *ndev, u8 nfcee_id, u8 nfcee_mode)
670 {
671 	struct nci_nfcee_mode_set_cmd cmd;
672 
673 	cmd.nfcee_id = nfcee_id;
674 	cmd.nfcee_mode = nfcee_mode;
675 
676 	return __nci_request(ndev, nci_nfcee_mode_set_req,
677 			     (unsigned long)&cmd,
678 			     msecs_to_jiffies(NCI_CMD_TIMEOUT));
679 }
680 EXPORT_SYMBOL(nci_nfcee_mode_set);
681 
682 static void nci_core_conn_create_req(struct nci_dev *ndev, unsigned long opt)
683 {
684 	struct core_conn_create_data *data =
685 					(struct core_conn_create_data *)opt;
686 
687 	nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, data->length, data->cmd);
688 }
689 
690 int nci_core_conn_create(struct nci_dev *ndev, u8 destination_type,
691 			 u8 number_destination_params,
692 			 size_t params_len,
693 			 struct core_conn_create_dest_spec_params *params)
694 {
695 	int r;
696 	struct nci_core_conn_create_cmd *cmd;
697 	struct core_conn_create_data data;
698 
699 	data.length = params_len + sizeof(struct nci_core_conn_create_cmd);
700 	cmd = kzalloc(data.length, GFP_KERNEL);
701 	if (!cmd)
702 		return -ENOMEM;
703 
704 	cmd->destination_type = destination_type;
705 	cmd->number_destination_params = number_destination_params;
706 
707 	data.cmd = cmd;
708 
709 	if (params) {
710 		memcpy(cmd->params, params, params_len);
711 		if (params->length > 0)
712 			memcpy(&ndev->cur_params,
713 			       &params->value[DEST_SPEC_PARAMS_ID_INDEX],
714 			       sizeof(struct dest_spec_params));
715 		else
716 			ndev->cur_params.id = 0;
717 	} else {
718 		ndev->cur_params.id = 0;
719 	}
720 	ndev->cur_dest_type = destination_type;
721 
722 	r = __nci_request(ndev, nci_core_conn_create_req, (unsigned long)&data,
723 			  msecs_to_jiffies(NCI_CMD_TIMEOUT));
724 	kfree(cmd);
725 	return r;
726 }
727 EXPORT_SYMBOL(nci_core_conn_create);
728 
729 static void nci_core_conn_close_req(struct nci_dev *ndev, unsigned long opt)
730 {
731 	__u8 conn_id = opt;
732 
733 	nci_send_cmd(ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 1, &conn_id);
734 }
735 
736 int nci_core_conn_close(struct nci_dev *ndev, u8 conn_id)
737 {
738 	ndev->cur_conn_id = conn_id;
739 	return __nci_request(ndev, nci_core_conn_close_req, conn_id,
740 			     msecs_to_jiffies(NCI_CMD_TIMEOUT));
741 }
742 EXPORT_SYMBOL(nci_core_conn_close);
743 
744 static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
745 {
746 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
747 	struct nci_set_config_param param;
748 	int rc;
749 
750 	param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
751 	if ((param.val == NULL) || (param.len == 0))
752 		return 0;
753 
754 	if (param.len > NFC_MAX_GT_LEN)
755 		return -EINVAL;
756 
757 	param.id = NCI_PN_ATR_REQ_GEN_BYTES;
758 
759 	rc = nci_request(ndev, nci_set_config_req, (unsigned long)&param,
760 			 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
761 	if (rc)
762 		return rc;
763 
764 	param.id = NCI_LN_ATR_RES_GEN_BYTES;
765 
766 	return nci_request(ndev, nci_set_config_req, (unsigned long)&param,
767 			   msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
768 }
769 
770 static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
771 {
772 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
773 	int rc;
774 	__u8 val;
775 
776 	val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
777 
778 	rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
779 	if (rc)
780 		return rc;
781 
782 	val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
783 
784 	rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
785 	if (rc)
786 		return rc;
787 
788 	val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
789 
790 	return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
791 }
792 
793 static int nci_start_poll(struct nfc_dev *nfc_dev,
794 			  __u32 im_protocols, __u32 tm_protocols)
795 {
796 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
797 	struct nci_rf_discover_param param;
798 	int rc;
799 
800 	if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
801 	    (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
802 		pr_err("unable to start poll, since poll is already active\n");
803 		return -EBUSY;
804 	}
805 
806 	if (ndev->target_active_prot) {
807 		pr_err("there is an active target\n");
808 		return -EBUSY;
809 	}
810 
811 	if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
812 	    (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
813 		pr_debug("target active or w4 select, implicitly deactivate\n");
814 
815 		rc = nci_request(ndev, nci_rf_deactivate_req,
816 				 NCI_DEACTIVATE_TYPE_IDLE_MODE,
817 				 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
818 		if (rc)
819 			return -EBUSY;
820 	}
821 
822 	if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
823 		rc = nci_set_local_general_bytes(nfc_dev);
824 		if (rc) {
825 			pr_err("failed to set local general bytes\n");
826 			return rc;
827 		}
828 	}
829 
830 	if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
831 		rc = nci_set_listen_parameters(nfc_dev);
832 		if (rc)
833 			pr_err("failed to set listen parameters\n");
834 	}
835 
836 	param.im_protocols = im_protocols;
837 	param.tm_protocols = tm_protocols;
838 	rc = nci_request(ndev, nci_rf_discover_req, (unsigned long)&param,
839 			 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
840 
841 	if (!rc)
842 		ndev->poll_prots = im_protocols;
843 
844 	return rc;
845 }
846 
847 static void nci_stop_poll(struct nfc_dev *nfc_dev)
848 {
849 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
850 
851 	if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
852 	    (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
853 		pr_err("unable to stop poll, since poll is not active\n");
854 		return;
855 	}
856 
857 	nci_request(ndev, nci_rf_deactivate_req, NCI_DEACTIVATE_TYPE_IDLE_MODE,
858 		    msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
859 }
860 
861 static int nci_activate_target(struct nfc_dev *nfc_dev,
862 			       struct nfc_target *target, __u32 protocol)
863 {
864 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
865 	struct nci_rf_discover_select_param param;
866 	struct nfc_target *nci_target = NULL;
867 	int i;
868 	int rc = 0;
869 
870 	pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
871 
872 	if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
873 	    (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
874 		pr_err("there is no available target to activate\n");
875 		return -EINVAL;
876 	}
877 
878 	if (ndev->target_active_prot) {
879 		pr_err("there is already an active target\n");
880 		return -EBUSY;
881 	}
882 
883 	for (i = 0; i < ndev->n_targets; i++) {
884 		if (ndev->targets[i].idx == target->idx) {
885 			nci_target = &ndev->targets[i];
886 			break;
887 		}
888 	}
889 
890 	if (!nci_target) {
891 		pr_err("unable to find the selected target\n");
892 		return -EINVAL;
893 	}
894 
895 	if (!(nci_target->supported_protocols & (1 << protocol))) {
896 		pr_err("target does not support the requested protocol 0x%x\n",
897 		       protocol);
898 		return -EINVAL;
899 	}
900 
901 	if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
902 		param.rf_discovery_id = nci_target->logical_idx;
903 
904 		if (protocol == NFC_PROTO_JEWEL)
905 			param.rf_protocol = NCI_RF_PROTOCOL_T1T;
906 		else if (protocol == NFC_PROTO_MIFARE)
907 			param.rf_protocol = NCI_RF_PROTOCOL_T2T;
908 		else if (protocol == NFC_PROTO_FELICA)
909 			param.rf_protocol = NCI_RF_PROTOCOL_T3T;
910 		else if (protocol == NFC_PROTO_ISO14443 ||
911 			 protocol == NFC_PROTO_ISO14443_B)
912 			param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
913 		else
914 			param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
915 
916 		rc = nci_request(ndev, nci_rf_discover_select_req,
917 				 (unsigned long)&param,
918 				 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
919 	}
920 
921 	if (!rc)
922 		ndev->target_active_prot = protocol;
923 
924 	return rc;
925 }
926 
927 static void nci_deactivate_target(struct nfc_dev *nfc_dev,
928 				  struct nfc_target *target,
929 				  __u8 mode)
930 {
931 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
932 	u8 nci_mode = NCI_DEACTIVATE_TYPE_IDLE_MODE;
933 
934 	pr_debug("entry\n");
935 
936 	if (!ndev->target_active_prot) {
937 		pr_err("unable to deactivate target, no active target\n");
938 		return;
939 	}
940 
941 	ndev->target_active_prot = 0;
942 
943 	switch (mode) {
944 	case NFC_TARGET_MODE_SLEEP:
945 		nci_mode = NCI_DEACTIVATE_TYPE_SLEEP_MODE;
946 		break;
947 	}
948 
949 	if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
950 		nci_request(ndev, nci_rf_deactivate_req, nci_mode,
951 			    msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
952 	}
953 }
954 
955 static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
956 			   __u8 comm_mode, __u8 *gb, size_t gb_len)
957 {
958 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
959 	int rc;
960 
961 	pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
962 
963 	rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
964 	if (rc)
965 		return rc;
966 
967 	rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
968 					  ndev->remote_gb_len);
969 	if (!rc)
970 		rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
971 					NFC_RF_INITIATOR);
972 
973 	return rc;
974 }
975 
976 static int nci_dep_link_down(struct nfc_dev *nfc_dev)
977 {
978 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
979 	int rc;
980 
981 	pr_debug("entry\n");
982 
983 	if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
984 		nci_deactivate_target(nfc_dev, NULL, NCI_DEACTIVATE_TYPE_IDLE_MODE);
985 	} else {
986 		if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
987 		    atomic_read(&ndev->state) == NCI_DISCOVERY) {
988 			nci_request(ndev, nci_rf_deactivate_req, 0,
989 				msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
990 		}
991 
992 		rc = nfc_tm_deactivated(nfc_dev);
993 		if (rc)
994 			pr_err("error when signaling tm deactivation\n");
995 	}
996 
997 	return 0;
998 }
999 
1000 
1001 static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
1002 			  struct sk_buff *skb,
1003 			  data_exchange_cb_t cb, void *cb_context)
1004 {
1005 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1006 	int rc;
1007 	struct nci_conn_info    *conn_info;
1008 
1009 	conn_info = ndev->rf_conn_info;
1010 	if (!conn_info)
1011 		return -EPROTO;
1012 
1013 	pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
1014 
1015 	if (!ndev->target_active_prot) {
1016 		pr_err("unable to exchange data, no active target\n");
1017 		return -EINVAL;
1018 	}
1019 
1020 	if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1021 		return -EBUSY;
1022 
1023 	/* store cb and context to be used on receiving data */
1024 	conn_info->data_exchange_cb = cb;
1025 	conn_info->data_exchange_cb_context = cb_context;
1026 
1027 	rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1028 	if (rc)
1029 		clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
1030 
1031 	return rc;
1032 }
1033 
1034 static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
1035 {
1036 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1037 	int rc;
1038 
1039 	rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1040 	if (rc)
1041 		pr_err("unable to send data\n");
1042 
1043 	return rc;
1044 }
1045 
1046 static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1047 {
1048 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1049 
1050 	if (ndev->ops->enable_se)
1051 		return ndev->ops->enable_se(ndev, se_idx);
1052 
1053 	return 0;
1054 }
1055 
1056 static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1057 {
1058 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1059 
1060 	if (ndev->ops->disable_se)
1061 		return ndev->ops->disable_se(ndev, se_idx);
1062 
1063 	return 0;
1064 }
1065 
1066 static int nci_discover_se(struct nfc_dev *nfc_dev)
1067 {
1068 	int r;
1069 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1070 
1071 	if (ndev->ops->discover_se) {
1072 		r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
1073 		if (r != NCI_STATUS_OK)
1074 			return -EPROTO;
1075 
1076 		return ndev->ops->discover_se(ndev);
1077 	}
1078 
1079 	return 0;
1080 }
1081 
1082 static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
1083 		     u8 *apdu, size_t apdu_length,
1084 		     se_io_cb_t cb, void *cb_context)
1085 {
1086 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1087 
1088 	if (ndev->ops->se_io)
1089 		return ndev->ops->se_io(ndev, se_idx, apdu,
1090 				apdu_length, cb, cb_context);
1091 
1092 	return 0;
1093 }
1094 
1095 static int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
1096 {
1097 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1098 
1099 	if (!ndev->ops->fw_download)
1100 		return -ENOTSUPP;
1101 
1102 	return ndev->ops->fw_download(ndev, firmware_name);
1103 }
1104 
1105 static struct nfc_ops nci_nfc_ops = {
1106 	.dev_up = nci_dev_up,
1107 	.dev_down = nci_dev_down,
1108 	.start_poll = nci_start_poll,
1109 	.stop_poll = nci_stop_poll,
1110 	.dep_link_up = nci_dep_link_up,
1111 	.dep_link_down = nci_dep_link_down,
1112 	.activate_target = nci_activate_target,
1113 	.deactivate_target = nci_deactivate_target,
1114 	.im_transceive = nci_transceive,
1115 	.tm_send = nci_tm_send,
1116 	.enable_se = nci_enable_se,
1117 	.disable_se = nci_disable_se,
1118 	.discover_se = nci_discover_se,
1119 	.se_io = nci_se_io,
1120 	.fw_download = nci_fw_download,
1121 };
1122 
1123 /* ---- Interface to NCI drivers ---- */
1124 /**
1125  * nci_allocate_device - allocate a new nci device
1126  *
1127  * @ops: device operations
1128  * @supported_protocols: NFC protocols supported by the device
1129  * @tx_headroom: Reserved space at beginning of skb
1130  * @tx_tailroom: Reserved space at end of skb
1131  */
1132 struct nci_dev *nci_allocate_device(struct nci_ops *ops,
1133 				    __u32 supported_protocols,
1134 				    int tx_headroom, int tx_tailroom)
1135 {
1136 	struct nci_dev *ndev;
1137 
1138 	pr_debug("supported_protocols 0x%x\n", supported_protocols);
1139 
1140 	if (!ops->open || !ops->close || !ops->send)
1141 		return NULL;
1142 
1143 	if (!supported_protocols)
1144 		return NULL;
1145 
1146 	ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
1147 	if (!ndev)
1148 		return NULL;
1149 
1150 	ndev->ops = ops;
1151 
1152 	if (ops->n_prop_ops > NCI_MAX_PROPRIETARY_CMD) {
1153 		pr_err("Too many proprietary commands: %zd\n",
1154 		       ops->n_prop_ops);
1155 		ops->prop_ops = NULL;
1156 		ops->n_prop_ops = 0;
1157 	}
1158 
1159 	ndev->tx_headroom = tx_headroom;
1160 	ndev->tx_tailroom = tx_tailroom;
1161 	init_completion(&ndev->req_completion);
1162 
1163 	ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
1164 					    supported_protocols,
1165 					    tx_headroom + NCI_DATA_HDR_SIZE,
1166 					    tx_tailroom);
1167 	if (!ndev->nfc_dev)
1168 		goto free_nci;
1169 
1170 	ndev->hci_dev = nci_hci_allocate(ndev);
1171 	if (!ndev->hci_dev)
1172 		goto free_nfc;
1173 
1174 	nfc_set_drvdata(ndev->nfc_dev, ndev);
1175 
1176 	return ndev;
1177 
1178 free_nfc:
1179 	nfc_free_device(ndev->nfc_dev);
1180 free_nci:
1181 	kfree(ndev);
1182 	return NULL;
1183 }
1184 EXPORT_SYMBOL(nci_allocate_device);
1185 
1186 /**
1187  * nci_free_device - deallocate nci device
1188  *
1189  * @ndev: The nci device to deallocate
1190  */
1191 void nci_free_device(struct nci_dev *ndev)
1192 {
1193 	nfc_free_device(ndev->nfc_dev);
1194 	kfree(ndev);
1195 }
1196 EXPORT_SYMBOL(nci_free_device);
1197 
1198 /**
1199  * nci_register_device - register a nci device in the nfc subsystem
1200  *
1201  * @ndev: The nci device to register
1202  */
1203 int nci_register_device(struct nci_dev *ndev)
1204 {
1205 	int rc;
1206 	struct device *dev = &ndev->nfc_dev->dev;
1207 	char name[32];
1208 
1209 	ndev->flags = 0;
1210 
1211 	INIT_WORK(&ndev->cmd_work, nci_cmd_work);
1212 	snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
1213 	ndev->cmd_wq = create_singlethread_workqueue(name);
1214 	if (!ndev->cmd_wq) {
1215 		rc = -ENOMEM;
1216 		goto exit;
1217 	}
1218 
1219 	INIT_WORK(&ndev->rx_work, nci_rx_work);
1220 	snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
1221 	ndev->rx_wq = create_singlethread_workqueue(name);
1222 	if (!ndev->rx_wq) {
1223 		rc = -ENOMEM;
1224 		goto destroy_cmd_wq_exit;
1225 	}
1226 
1227 	INIT_WORK(&ndev->tx_work, nci_tx_work);
1228 	snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
1229 	ndev->tx_wq = create_singlethread_workqueue(name);
1230 	if (!ndev->tx_wq) {
1231 		rc = -ENOMEM;
1232 		goto destroy_rx_wq_exit;
1233 	}
1234 
1235 	skb_queue_head_init(&ndev->cmd_q);
1236 	skb_queue_head_init(&ndev->rx_q);
1237 	skb_queue_head_init(&ndev->tx_q);
1238 
1239 	timer_setup(&ndev->cmd_timer, nci_cmd_timer, 0);
1240 	timer_setup(&ndev->data_timer, nci_data_timer, 0);
1241 
1242 	mutex_init(&ndev->req_lock);
1243 	INIT_LIST_HEAD(&ndev->conn_info_list);
1244 
1245 	rc = nfc_register_device(ndev->nfc_dev);
1246 	if (rc)
1247 		goto destroy_tx_wq_exit;
1248 
1249 	goto exit;
1250 
1251 destroy_tx_wq_exit:
1252 	destroy_workqueue(ndev->tx_wq);
1253 
1254 destroy_rx_wq_exit:
1255 	destroy_workqueue(ndev->rx_wq);
1256 
1257 destroy_cmd_wq_exit:
1258 	destroy_workqueue(ndev->cmd_wq);
1259 
1260 exit:
1261 	return rc;
1262 }
1263 EXPORT_SYMBOL(nci_register_device);
1264 
1265 /**
1266  * nci_unregister_device - unregister a nci device in the nfc subsystem
1267  *
1268  * @ndev: The nci device to unregister
1269  */
1270 void nci_unregister_device(struct nci_dev *ndev)
1271 {
1272 	struct nci_conn_info    *conn_info, *n;
1273 
1274 	nci_close_device(ndev);
1275 
1276 	destroy_workqueue(ndev->cmd_wq);
1277 	destroy_workqueue(ndev->rx_wq);
1278 	destroy_workqueue(ndev->tx_wq);
1279 
1280 	list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
1281 		list_del(&conn_info->list);
1282 		/* conn_info is allocated with devm_kzalloc */
1283 	}
1284 
1285 	nfc_unregister_device(ndev->nfc_dev);
1286 }
1287 EXPORT_SYMBOL(nci_unregister_device);
1288 
1289 /**
1290  * nci_recv_frame - receive frame from NCI drivers
1291  *
1292  * @ndev: The nci device
1293  * @skb: The sk_buff to receive
1294  */
1295 int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
1296 {
1297 	pr_debug("len %d\n", skb->len);
1298 
1299 	if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
1300 	    !test_bit(NCI_INIT, &ndev->flags))) {
1301 		kfree_skb(skb);
1302 		return -ENXIO;
1303 	}
1304 
1305 	/* Queue frame for rx worker thread */
1306 	skb_queue_tail(&ndev->rx_q, skb);
1307 	queue_work(ndev->rx_wq, &ndev->rx_work);
1308 
1309 	return 0;
1310 }
1311 EXPORT_SYMBOL(nci_recv_frame);
1312 
1313 int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
1314 {
1315 	pr_debug("len %d\n", skb->len);
1316 
1317 	if (!ndev) {
1318 		kfree_skb(skb);
1319 		return -ENODEV;
1320 	}
1321 
1322 	/* Get rid of skb owner, prior to sending to the driver. */
1323 	skb_orphan(skb);
1324 
1325 	/* Send copy to sniffer */
1326 	nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1327 			     RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
1328 
1329 	return ndev->ops->send(ndev, skb);
1330 }
1331 EXPORT_SYMBOL(nci_send_frame);
1332 
1333 /* Send NCI command */
1334 int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
1335 {
1336 	struct nci_ctrl_hdr *hdr;
1337 	struct sk_buff *skb;
1338 
1339 	pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
1340 
1341 	skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
1342 	if (!skb) {
1343 		pr_err("no memory for command\n");
1344 		return -ENOMEM;
1345 	}
1346 
1347 	hdr = skb_put(skb, NCI_CTRL_HDR_SIZE);
1348 	hdr->gid = nci_opcode_gid(opcode);
1349 	hdr->oid = nci_opcode_oid(opcode);
1350 	hdr->plen = plen;
1351 
1352 	nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
1353 	nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
1354 
1355 	if (plen)
1356 		skb_put_data(skb, payload, plen);
1357 
1358 	skb_queue_tail(&ndev->cmd_q, skb);
1359 	queue_work(ndev->cmd_wq, &ndev->cmd_work);
1360 
1361 	return 0;
1362 }
1363 EXPORT_SYMBOL(nci_send_cmd);
1364 
1365 /* Proprietary commands API */
1366 static struct nci_driver_ops *ops_cmd_lookup(struct nci_driver_ops *ops,
1367 					     size_t n_ops,
1368 					     __u16 opcode)
1369 {
1370 	size_t i;
1371 	struct nci_driver_ops *op;
1372 
1373 	if (!ops || !n_ops)
1374 		return NULL;
1375 
1376 	for (i = 0; i < n_ops; i++) {
1377 		op = &ops[i];
1378 		if (op->opcode == opcode)
1379 			return op;
1380 	}
1381 
1382 	return NULL;
1383 }
1384 
1385 static int nci_op_rsp_packet(struct nci_dev *ndev, __u16 rsp_opcode,
1386 			     struct sk_buff *skb, struct nci_driver_ops *ops,
1387 			     size_t n_ops)
1388 {
1389 	struct nci_driver_ops *op;
1390 
1391 	op = ops_cmd_lookup(ops, n_ops, rsp_opcode);
1392 	if (!op || !op->rsp)
1393 		return -ENOTSUPP;
1394 
1395 	return op->rsp(ndev, skb);
1396 }
1397 
1398 static int nci_op_ntf_packet(struct nci_dev *ndev, __u16 ntf_opcode,
1399 			     struct sk_buff *skb, struct nci_driver_ops *ops,
1400 			     size_t n_ops)
1401 {
1402 	struct nci_driver_ops *op;
1403 
1404 	op = ops_cmd_lookup(ops, n_ops, ntf_opcode);
1405 	if (!op || !op->ntf)
1406 		return -ENOTSUPP;
1407 
1408 	return op->ntf(ndev, skb);
1409 }
1410 
1411 int nci_prop_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1412 			struct sk_buff *skb)
1413 {
1414 	return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1415 				 ndev->ops->n_prop_ops);
1416 }
1417 
1418 int nci_prop_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1419 			struct sk_buff *skb)
1420 {
1421 	return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1422 				 ndev->ops->n_prop_ops);
1423 }
1424 
1425 int nci_core_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1426 			struct sk_buff *skb)
1427 {
1428 	return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->core_ops,
1429 				  ndev->ops->n_core_ops);
1430 }
1431 
1432 int nci_core_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1433 			struct sk_buff *skb)
1434 {
1435 	return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->core_ops,
1436 				 ndev->ops->n_core_ops);
1437 }
1438 
1439 /* ---- NCI TX Data worker thread ---- */
1440 
1441 static void nci_tx_work(struct work_struct *work)
1442 {
1443 	struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
1444 	struct nci_conn_info    *conn_info;
1445 	struct sk_buff *skb;
1446 
1447 	conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
1448 	if (!conn_info)
1449 		return;
1450 
1451 	pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
1452 
1453 	/* Send queued tx data */
1454 	while (atomic_read(&conn_info->credits_cnt)) {
1455 		skb = skb_dequeue(&ndev->tx_q);
1456 		if (!skb)
1457 			return;
1458 
1459 		/* Check if data flow control is used */
1460 		if (atomic_read(&conn_info->credits_cnt) !=
1461 		    NCI_DATA_FLOW_CONTROL_NOT_USED)
1462 			atomic_dec(&conn_info->credits_cnt);
1463 
1464 		pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1465 			 nci_pbf(skb->data),
1466 			 nci_conn_id(skb->data),
1467 			 nci_plen(skb->data));
1468 
1469 		nci_send_frame(ndev, skb);
1470 
1471 		mod_timer(&ndev->data_timer,
1472 			  jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
1473 	}
1474 }
1475 
1476 /* ----- NCI RX worker thread (data & control) ----- */
1477 
1478 static void nci_rx_work(struct work_struct *work)
1479 {
1480 	struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
1481 	struct sk_buff *skb;
1482 
1483 	while ((skb = skb_dequeue(&ndev->rx_q))) {
1484 
1485 		/* Send copy to sniffer */
1486 		nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1487 				     RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
1488 
1489 		/* Process frame */
1490 		switch (nci_mt(skb->data)) {
1491 		case NCI_MT_RSP_PKT:
1492 			nci_rsp_packet(ndev, skb);
1493 			break;
1494 
1495 		case NCI_MT_NTF_PKT:
1496 			nci_ntf_packet(ndev, skb);
1497 			break;
1498 
1499 		case NCI_MT_DATA_PKT:
1500 			nci_rx_data_packet(ndev, skb);
1501 			break;
1502 
1503 		default:
1504 			pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
1505 			kfree_skb(skb);
1506 			break;
1507 		}
1508 	}
1509 
1510 	/* check if a data exchange timeout has occurred */
1511 	if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
1512 		/* complete the data exchange transaction, if exists */
1513 		if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1514 			nci_data_exchange_complete(ndev, NULL,
1515 						   ndev->cur_conn_id,
1516 						   -ETIMEDOUT);
1517 
1518 		clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
1519 	}
1520 }
1521 
1522 /* ----- NCI TX CMD worker thread ----- */
1523 
1524 static void nci_cmd_work(struct work_struct *work)
1525 {
1526 	struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
1527 	struct sk_buff *skb;
1528 
1529 	pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
1530 
1531 	/* Send queued command */
1532 	if (atomic_read(&ndev->cmd_cnt)) {
1533 		skb = skb_dequeue(&ndev->cmd_q);
1534 		if (!skb)
1535 			return;
1536 
1537 		atomic_dec(&ndev->cmd_cnt);
1538 
1539 		pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1540 			 nci_pbf(skb->data),
1541 			 nci_opcode_gid(nci_opcode(skb->data)),
1542 			 nci_opcode_oid(nci_opcode(skb->data)),
1543 			 nci_plen(skb->data));
1544 
1545 		nci_send_frame(ndev, skb);
1546 
1547 		mod_timer(&ndev->cmd_timer,
1548 			  jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
1549 	}
1550 }
1551 
1552 MODULE_LICENSE("GPL");
1553