xref: /openbmc/linux/net/nfc/nci/core.c (revision 8684014d)
1 /*
2  *  The NFC Controller Interface is the communication protocol between an
3  *  NFC Controller (NFCC) and a Device Host (DH).
4  *
5  *  Copyright (C) 2011 Texas Instruments, Inc.
6  *  Copyright (C) 2014 Marvell International Ltd.
7  *
8  *  Written by Ilan Elias <ilane@ti.com>
9  *
10  *  Acknowledgements:
11  *  This file is based on hci_core.c, which was written
12  *  by Maxim Krasnyansky.
13  *
14  *  This program is free software; you can redistribute it and/or modify
15  *  it under the terms of the GNU General Public License version 2
16  *  as published by the Free Software Foundation
17  *
18  *  This program is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *  GNU General Public License for more details.
22  *
23  *  You should have received a copy of the GNU General Public License
24  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
25  *
26  */
27 
28 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
29 
30 #include <linux/module.h>
31 #include <linux/types.h>
32 #include <linux/workqueue.h>
33 #include <linux/completion.h>
34 #include <linux/export.h>
35 #include <linux/sched.h>
36 #include <linux/bitops.h>
37 #include <linux/skbuff.h>
38 
39 #include "../nfc.h"
40 #include <net/nfc/nci.h>
41 #include <net/nfc/nci_core.h>
42 #include <linux/nfc.h>
43 
44 static void nci_cmd_work(struct work_struct *work);
45 static void nci_rx_work(struct work_struct *work);
46 static void nci_tx_work(struct work_struct *work);
47 
48 /* ---- NCI requests ---- */
49 
50 void nci_req_complete(struct nci_dev *ndev, int result)
51 {
52 	if (ndev->req_status == NCI_REQ_PEND) {
53 		ndev->req_result = result;
54 		ndev->req_status = NCI_REQ_DONE;
55 		complete(&ndev->req_completion);
56 	}
57 }
58 
59 static void nci_req_cancel(struct nci_dev *ndev, int err)
60 {
61 	if (ndev->req_status == NCI_REQ_PEND) {
62 		ndev->req_result = err;
63 		ndev->req_status = NCI_REQ_CANCELED;
64 		complete(&ndev->req_completion);
65 	}
66 }
67 
68 /* Execute request and wait for completion. */
69 static int __nci_request(struct nci_dev *ndev,
70 			 void (*req)(struct nci_dev *ndev, unsigned long opt),
71 			 unsigned long opt, __u32 timeout)
72 {
73 	int rc = 0;
74 	long completion_rc;
75 
76 	ndev->req_status = NCI_REQ_PEND;
77 
78 	reinit_completion(&ndev->req_completion);
79 	req(ndev, opt);
80 	completion_rc =
81 		wait_for_completion_interruptible_timeout(&ndev->req_completion,
82 							  timeout);
83 
84 	pr_debug("wait_for_completion return %ld\n", completion_rc);
85 
86 	if (completion_rc > 0) {
87 		switch (ndev->req_status) {
88 		case NCI_REQ_DONE:
89 			rc = nci_to_errno(ndev->req_result);
90 			break;
91 
92 		case NCI_REQ_CANCELED:
93 			rc = -ndev->req_result;
94 			break;
95 
96 		default:
97 			rc = -ETIMEDOUT;
98 			break;
99 		}
100 	} else {
101 		pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
102 		       completion_rc);
103 
104 		rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
105 	}
106 
107 	ndev->req_status = ndev->req_result = 0;
108 
109 	return rc;
110 }
111 
112 static inline int nci_request(struct nci_dev *ndev,
113 			      void (*req)(struct nci_dev *ndev,
114 					  unsigned long opt),
115 			      unsigned long opt, __u32 timeout)
116 {
117 	int rc;
118 
119 	if (!test_bit(NCI_UP, &ndev->flags))
120 		return -ENETDOWN;
121 
122 	/* Serialize all requests */
123 	mutex_lock(&ndev->req_lock);
124 	rc = __nci_request(ndev, req, opt, timeout);
125 	mutex_unlock(&ndev->req_lock);
126 
127 	return rc;
128 }
129 
130 static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
131 {
132 	struct nci_core_reset_cmd cmd;
133 
134 	cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
135 	nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
136 }
137 
138 static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
139 {
140 	nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
141 }
142 
143 static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
144 {
145 	struct nci_rf_disc_map_cmd cmd;
146 	struct disc_map_config *cfg = cmd.mapping_configs;
147 	__u8 *num = &cmd.num_mapping_configs;
148 	int i;
149 
150 	/* set rf mapping configurations */
151 	*num = 0;
152 
153 	/* by default mapping is set to NCI_RF_INTERFACE_FRAME */
154 	for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
155 		if (ndev->supported_rf_interfaces[i] ==
156 		    NCI_RF_INTERFACE_ISO_DEP) {
157 			cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
158 			cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
159 				NCI_DISC_MAP_MODE_LISTEN;
160 			cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
161 			(*num)++;
162 		} else if (ndev->supported_rf_interfaces[i] ==
163 			   NCI_RF_INTERFACE_NFC_DEP) {
164 			cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
165 			cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
166 				NCI_DISC_MAP_MODE_LISTEN;
167 			cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
168 			(*num)++;
169 		}
170 
171 		if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
172 			break;
173 	}
174 
175 	nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
176 		     (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
177 }
178 
179 struct nci_set_config_param {
180 	__u8	id;
181 	size_t	len;
182 	__u8	*val;
183 };
184 
185 static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt)
186 {
187 	struct nci_set_config_param *param = (struct nci_set_config_param *)opt;
188 	struct nci_core_set_config_cmd cmd;
189 
190 	BUG_ON(param->len > NCI_MAX_PARAM_LEN);
191 
192 	cmd.num_params = 1;
193 	cmd.param.id = param->id;
194 	cmd.param.len = param->len;
195 	memcpy(cmd.param.val, param->val, param->len);
196 
197 	nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
198 }
199 
200 struct nci_rf_discover_param {
201 	__u32	im_protocols;
202 	__u32	tm_protocols;
203 };
204 
205 static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
206 {
207 	struct nci_rf_discover_param *param =
208 		(struct nci_rf_discover_param *)opt;
209 	struct nci_rf_disc_cmd cmd;
210 
211 	cmd.num_disc_configs = 0;
212 
213 	if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
214 	    (param->im_protocols & NFC_PROTO_JEWEL_MASK ||
215 	     param->im_protocols & NFC_PROTO_MIFARE_MASK ||
216 	     param->im_protocols & NFC_PROTO_ISO14443_MASK ||
217 	     param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
218 		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
219 			NCI_NFC_A_PASSIVE_POLL_MODE;
220 		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
221 		cmd.num_disc_configs++;
222 	}
223 
224 	if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
225 	    (param->im_protocols & NFC_PROTO_ISO14443_B_MASK)) {
226 		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
227 			NCI_NFC_B_PASSIVE_POLL_MODE;
228 		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
229 		cmd.num_disc_configs++;
230 	}
231 
232 	if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
233 	    (param->im_protocols & NFC_PROTO_FELICA_MASK ||
234 	     param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
235 		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
236 			NCI_NFC_F_PASSIVE_POLL_MODE;
237 		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
238 		cmd.num_disc_configs++;
239 	}
240 
241 	if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
242 	    (param->im_protocols & NFC_PROTO_ISO15693_MASK)) {
243 		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
244 			NCI_NFC_V_PASSIVE_POLL_MODE;
245 		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
246 		cmd.num_disc_configs++;
247 	}
248 
249 	if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS - 1) &&
250 	    (param->tm_protocols & NFC_PROTO_NFC_DEP_MASK)) {
251 		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
252 			NCI_NFC_A_PASSIVE_LISTEN_MODE;
253 		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
254 		cmd.num_disc_configs++;
255 		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
256 			NCI_NFC_F_PASSIVE_LISTEN_MODE;
257 		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
258 		cmd.num_disc_configs++;
259 	}
260 
261 	nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
262 		     (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
263 		     &cmd);
264 }
265 
266 struct nci_rf_discover_select_param {
267 	__u8	rf_discovery_id;
268 	__u8	rf_protocol;
269 };
270 
271 static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt)
272 {
273 	struct nci_rf_discover_select_param *param =
274 		(struct nci_rf_discover_select_param *)opt;
275 	struct nci_rf_discover_select_cmd cmd;
276 
277 	cmd.rf_discovery_id = param->rf_discovery_id;
278 	cmd.rf_protocol = param->rf_protocol;
279 
280 	switch (cmd.rf_protocol) {
281 	case NCI_RF_PROTOCOL_ISO_DEP:
282 		cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
283 		break;
284 
285 	case NCI_RF_PROTOCOL_NFC_DEP:
286 		cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
287 		break;
288 
289 	default:
290 		cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
291 		break;
292 	}
293 
294 	nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
295 		     sizeof(struct nci_rf_discover_select_cmd), &cmd);
296 }
297 
298 static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
299 {
300 	struct nci_rf_deactivate_cmd cmd;
301 
302 	cmd.type = opt;
303 
304 	nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
305 		     sizeof(struct nci_rf_deactivate_cmd), &cmd);
306 }
307 
308 static int nci_open_device(struct nci_dev *ndev)
309 {
310 	int rc = 0;
311 
312 	mutex_lock(&ndev->req_lock);
313 
314 	if (test_bit(NCI_UP, &ndev->flags)) {
315 		rc = -EALREADY;
316 		goto done;
317 	}
318 
319 	if (ndev->ops->open(ndev)) {
320 		rc = -EIO;
321 		goto done;
322 	}
323 
324 	atomic_set(&ndev->cmd_cnt, 1);
325 
326 	set_bit(NCI_INIT, &ndev->flags);
327 
328 	rc = __nci_request(ndev, nci_reset_req, 0,
329 			   msecs_to_jiffies(NCI_RESET_TIMEOUT));
330 
331 	if (ndev->ops->setup)
332 		ndev->ops->setup(ndev);
333 
334 	if (!rc) {
335 		rc = __nci_request(ndev, nci_init_req, 0,
336 				   msecs_to_jiffies(NCI_INIT_TIMEOUT));
337 	}
338 
339 	if (!rc) {
340 		rc = __nci_request(ndev, nci_init_complete_req, 0,
341 				   msecs_to_jiffies(NCI_INIT_TIMEOUT));
342 	}
343 
344 	clear_bit(NCI_INIT, &ndev->flags);
345 
346 	if (!rc) {
347 		set_bit(NCI_UP, &ndev->flags);
348 		nci_clear_target_list(ndev);
349 		atomic_set(&ndev->state, NCI_IDLE);
350 	} else {
351 		/* Init failed, cleanup */
352 		skb_queue_purge(&ndev->cmd_q);
353 		skb_queue_purge(&ndev->rx_q);
354 		skb_queue_purge(&ndev->tx_q);
355 
356 		ndev->ops->close(ndev);
357 		ndev->flags = 0;
358 	}
359 
360 done:
361 	mutex_unlock(&ndev->req_lock);
362 	return rc;
363 }
364 
365 static int nci_close_device(struct nci_dev *ndev)
366 {
367 	nci_req_cancel(ndev, ENODEV);
368 	mutex_lock(&ndev->req_lock);
369 
370 	if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
371 		del_timer_sync(&ndev->cmd_timer);
372 		del_timer_sync(&ndev->data_timer);
373 		mutex_unlock(&ndev->req_lock);
374 		return 0;
375 	}
376 
377 	/* Drop RX and TX queues */
378 	skb_queue_purge(&ndev->rx_q);
379 	skb_queue_purge(&ndev->tx_q);
380 
381 	/* Flush RX and TX wq */
382 	flush_workqueue(ndev->rx_wq);
383 	flush_workqueue(ndev->tx_wq);
384 
385 	/* Reset device */
386 	skb_queue_purge(&ndev->cmd_q);
387 	atomic_set(&ndev->cmd_cnt, 1);
388 
389 	set_bit(NCI_INIT, &ndev->flags);
390 	__nci_request(ndev, nci_reset_req, 0,
391 		      msecs_to_jiffies(NCI_RESET_TIMEOUT));
392 	clear_bit(NCI_INIT, &ndev->flags);
393 
394 	del_timer_sync(&ndev->cmd_timer);
395 
396 	/* Flush cmd wq */
397 	flush_workqueue(ndev->cmd_wq);
398 
399 	/* After this point our queues are empty
400 	 * and no works are scheduled. */
401 	ndev->ops->close(ndev);
402 
403 	/* Clear flags */
404 	ndev->flags = 0;
405 
406 	mutex_unlock(&ndev->req_lock);
407 
408 	return 0;
409 }
410 
411 /* NCI command timer function */
412 static void nci_cmd_timer(unsigned long arg)
413 {
414 	struct nci_dev *ndev = (void *) arg;
415 
416 	atomic_set(&ndev->cmd_cnt, 1);
417 	queue_work(ndev->cmd_wq, &ndev->cmd_work);
418 }
419 
420 /* NCI data exchange timer function */
421 static void nci_data_timer(unsigned long arg)
422 {
423 	struct nci_dev *ndev = (void *) arg;
424 
425 	set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
426 	queue_work(ndev->rx_wq, &ndev->rx_work);
427 }
428 
429 static int nci_dev_up(struct nfc_dev *nfc_dev)
430 {
431 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
432 
433 	return nci_open_device(ndev);
434 }
435 
436 static int nci_dev_down(struct nfc_dev *nfc_dev)
437 {
438 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
439 
440 	return nci_close_device(ndev);
441 }
442 
443 int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, __u8 *val)
444 {
445 	struct nci_set_config_param param;
446 
447 	if (!val || !len)
448 		return 0;
449 
450 	param.id = id;
451 	param.len = len;
452 	param.val = val;
453 
454 	return __nci_request(ndev, nci_set_config_req, (unsigned long)&param,
455 			     msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
456 }
457 EXPORT_SYMBOL(nci_set_config);
458 
459 static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
460 {
461 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
462 	struct nci_set_config_param param;
463 	int rc;
464 
465 	param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
466 	if ((param.val == NULL) || (param.len == 0))
467 		return 0;
468 
469 	if (param.len > NFC_MAX_GT_LEN)
470 		return -EINVAL;
471 
472 	param.id = NCI_PN_ATR_REQ_GEN_BYTES;
473 
474 	rc = nci_request(ndev, nci_set_config_req, (unsigned long)&param,
475 			 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
476 	if (rc)
477 		return rc;
478 
479 	param.id = NCI_LN_ATR_RES_GEN_BYTES;
480 
481 	return nci_request(ndev, nci_set_config_req, (unsigned long)&param,
482 			   msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
483 }
484 
485 static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
486 {
487 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
488 	int rc;
489 	__u8 val;
490 
491 	val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
492 
493 	rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
494 	if (rc)
495 		return rc;
496 
497 	val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
498 
499 	rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
500 	if (rc)
501 		return rc;
502 
503 	val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
504 
505 	return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
506 }
507 
508 static int nci_start_poll(struct nfc_dev *nfc_dev,
509 			  __u32 im_protocols, __u32 tm_protocols)
510 {
511 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
512 	struct nci_rf_discover_param param;
513 	int rc;
514 
515 	if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
516 	    (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
517 		pr_err("unable to start poll, since poll is already active\n");
518 		return -EBUSY;
519 	}
520 
521 	if (ndev->target_active_prot) {
522 		pr_err("there is an active target\n");
523 		return -EBUSY;
524 	}
525 
526 	if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
527 	    (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
528 		pr_debug("target active or w4 select, implicitly deactivate\n");
529 
530 		rc = nci_request(ndev, nci_rf_deactivate_req,
531 				 NCI_DEACTIVATE_TYPE_IDLE_MODE,
532 				 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
533 		if (rc)
534 			return -EBUSY;
535 	}
536 
537 	if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
538 		rc = nci_set_local_general_bytes(nfc_dev);
539 		if (rc) {
540 			pr_err("failed to set local general bytes\n");
541 			return rc;
542 		}
543 	}
544 
545 	if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
546 		rc = nci_set_listen_parameters(nfc_dev);
547 		if (rc)
548 			pr_err("failed to set listen parameters\n");
549 	}
550 
551 	param.im_protocols = im_protocols;
552 	param.tm_protocols = tm_protocols;
553 	rc = nci_request(ndev, nci_rf_discover_req, (unsigned long)&param,
554 			 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
555 
556 	if (!rc)
557 		ndev->poll_prots = im_protocols;
558 
559 	return rc;
560 }
561 
562 static void nci_stop_poll(struct nfc_dev *nfc_dev)
563 {
564 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
565 
566 	if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
567 	    (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
568 		pr_err("unable to stop poll, since poll is not active\n");
569 		return;
570 	}
571 
572 	nci_request(ndev, nci_rf_deactivate_req, NCI_DEACTIVATE_TYPE_IDLE_MODE,
573 		    msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
574 }
575 
576 static int nci_activate_target(struct nfc_dev *nfc_dev,
577 			       struct nfc_target *target, __u32 protocol)
578 {
579 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
580 	struct nci_rf_discover_select_param param;
581 	struct nfc_target *nci_target = NULL;
582 	int i;
583 	int rc = 0;
584 
585 	pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
586 
587 	if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
588 	    (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
589 		pr_err("there is no available target to activate\n");
590 		return -EINVAL;
591 	}
592 
593 	if (ndev->target_active_prot) {
594 		pr_err("there is already an active target\n");
595 		return -EBUSY;
596 	}
597 
598 	for (i = 0; i < ndev->n_targets; i++) {
599 		if (ndev->targets[i].idx == target->idx) {
600 			nci_target = &ndev->targets[i];
601 			break;
602 		}
603 	}
604 
605 	if (!nci_target) {
606 		pr_err("unable to find the selected target\n");
607 		return -EINVAL;
608 	}
609 
610 	if (!(nci_target->supported_protocols & (1 << protocol))) {
611 		pr_err("target does not support the requested protocol 0x%x\n",
612 		       protocol);
613 		return -EINVAL;
614 	}
615 
616 	if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
617 		param.rf_discovery_id = nci_target->logical_idx;
618 
619 		if (protocol == NFC_PROTO_JEWEL)
620 			param.rf_protocol = NCI_RF_PROTOCOL_T1T;
621 		else if (protocol == NFC_PROTO_MIFARE)
622 			param.rf_protocol = NCI_RF_PROTOCOL_T2T;
623 		else if (protocol == NFC_PROTO_FELICA)
624 			param.rf_protocol = NCI_RF_PROTOCOL_T3T;
625 		else if (protocol == NFC_PROTO_ISO14443 ||
626 			 protocol == NFC_PROTO_ISO14443_B)
627 			param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
628 		else
629 			param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
630 
631 		rc = nci_request(ndev, nci_rf_discover_select_req,
632 				 (unsigned long)&param,
633 				 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
634 	}
635 
636 	if (!rc)
637 		ndev->target_active_prot = protocol;
638 
639 	return rc;
640 }
641 
642 static void nci_deactivate_target(struct nfc_dev *nfc_dev,
643 				  struct nfc_target *target)
644 {
645 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
646 
647 	pr_debug("entry\n");
648 
649 	if (!ndev->target_active_prot) {
650 		pr_err("unable to deactivate target, no active target\n");
651 		return;
652 	}
653 
654 	ndev->target_active_prot = 0;
655 
656 	if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
657 		nci_request(ndev, nci_rf_deactivate_req,
658 			    NCI_DEACTIVATE_TYPE_SLEEP_MODE,
659 			    msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
660 	}
661 }
662 
663 static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
664 			   __u8 comm_mode, __u8 *gb, size_t gb_len)
665 {
666 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
667 	int rc;
668 
669 	pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
670 
671 	rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
672 	if (rc)
673 		return rc;
674 
675 	rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
676 					  ndev->remote_gb_len);
677 	if (!rc)
678 		rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
679 					NFC_RF_INITIATOR);
680 
681 	return rc;
682 }
683 
684 static int nci_dep_link_down(struct nfc_dev *nfc_dev)
685 {
686 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
687 	int rc;
688 
689 	pr_debug("entry\n");
690 
691 	if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
692 		nci_deactivate_target(nfc_dev, NULL);
693 	} else {
694 		if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
695 		    atomic_read(&ndev->state) == NCI_DISCOVERY) {
696 			nci_request(ndev, nci_rf_deactivate_req, 0,
697 				msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
698 		}
699 
700 		rc = nfc_tm_deactivated(nfc_dev);
701 		if (rc)
702 			pr_err("error when signaling tm deactivation\n");
703 	}
704 
705 	return 0;
706 }
707 
708 
709 static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
710 			  struct sk_buff *skb,
711 			  data_exchange_cb_t cb, void *cb_context)
712 {
713 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
714 	int rc;
715 
716 	pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
717 
718 	if (!ndev->target_active_prot) {
719 		pr_err("unable to exchange data, no active target\n");
720 		return -EINVAL;
721 	}
722 
723 	if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
724 		return -EBUSY;
725 
726 	/* store cb and context to be used on receiving data */
727 	ndev->data_exchange_cb = cb;
728 	ndev->data_exchange_cb_context = cb_context;
729 
730 	rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
731 	if (rc)
732 		clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
733 
734 	return rc;
735 }
736 
737 static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
738 {
739 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
740 	int rc;
741 
742 	rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
743 	if (rc)
744 		pr_err("unable to send data\n");
745 
746 	return rc;
747 }
748 
749 static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
750 {
751 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
752 
753 	if (ndev->ops->enable_se)
754 		return ndev->ops->enable_se(ndev, se_idx);
755 
756 	return 0;
757 }
758 
759 static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
760 {
761 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
762 
763 	if (ndev->ops->disable_se)
764 		return ndev->ops->disable_se(ndev, se_idx);
765 
766 	return 0;
767 }
768 
769 static int nci_discover_se(struct nfc_dev *nfc_dev)
770 {
771 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
772 
773 	if (ndev->ops->discover_se)
774 		return ndev->ops->discover_se(ndev);
775 
776 	return 0;
777 }
778 
779 static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
780 		     u8 *apdu, size_t apdu_length,
781 		     se_io_cb_t cb, void *cb_context)
782 {
783 	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
784 
785 	if (ndev->ops->se_io)
786 		return ndev->ops->se_io(ndev, se_idx, apdu,
787 				apdu_length, cb, cb_context);
788 
789 	return 0;
790 }
791 
792 static struct nfc_ops nci_nfc_ops = {
793 	.dev_up = nci_dev_up,
794 	.dev_down = nci_dev_down,
795 	.start_poll = nci_start_poll,
796 	.stop_poll = nci_stop_poll,
797 	.dep_link_up = nci_dep_link_up,
798 	.dep_link_down = nci_dep_link_down,
799 	.activate_target = nci_activate_target,
800 	.deactivate_target = nci_deactivate_target,
801 	.im_transceive = nci_transceive,
802 	.tm_send = nci_tm_send,
803 	.enable_se = nci_enable_se,
804 	.disable_se = nci_disable_se,
805 	.discover_se = nci_discover_se,
806 	.se_io = nci_se_io,
807 };
808 
809 /* ---- Interface to NCI drivers ---- */
810 
811 /**
812  * nci_allocate_device - allocate a new nci device
813  *
814  * @ops: device operations
815  * @supported_protocols: NFC protocols supported by the device
816  */
817 struct nci_dev *nci_allocate_device(struct nci_ops *ops,
818 				    __u32 supported_protocols,
819 				    int tx_headroom, int tx_tailroom)
820 {
821 	struct nci_dev *ndev;
822 
823 	pr_debug("supported_protocols 0x%x\n", supported_protocols);
824 
825 	if (!ops->open || !ops->close || !ops->send)
826 		return NULL;
827 
828 	if (!supported_protocols)
829 		return NULL;
830 
831 	ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
832 	if (!ndev)
833 		return NULL;
834 
835 	ndev->ops = ops;
836 	ndev->tx_headroom = tx_headroom;
837 	ndev->tx_tailroom = tx_tailroom;
838 	init_completion(&ndev->req_completion);
839 
840 	ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
841 					    supported_protocols,
842 					    tx_headroom + NCI_DATA_HDR_SIZE,
843 					    tx_tailroom);
844 	if (!ndev->nfc_dev)
845 		goto free_exit;
846 
847 	nfc_set_drvdata(ndev->nfc_dev, ndev);
848 
849 	return ndev;
850 
851 free_exit:
852 	kfree(ndev);
853 	return NULL;
854 }
855 EXPORT_SYMBOL(nci_allocate_device);
856 
857 /**
858  * nci_free_device - deallocate nci device
859  *
860  * @ndev: The nci device to deallocate
861  */
862 void nci_free_device(struct nci_dev *ndev)
863 {
864 	nfc_free_device(ndev->nfc_dev);
865 	kfree(ndev);
866 }
867 EXPORT_SYMBOL(nci_free_device);
868 
869 /**
870  * nci_register_device - register a nci device in the nfc subsystem
871  *
872  * @dev: The nci device to register
873  */
874 int nci_register_device(struct nci_dev *ndev)
875 {
876 	int rc;
877 	struct device *dev = &ndev->nfc_dev->dev;
878 	char name[32];
879 
880 	ndev->flags = 0;
881 
882 	INIT_WORK(&ndev->cmd_work, nci_cmd_work);
883 	snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
884 	ndev->cmd_wq = create_singlethread_workqueue(name);
885 	if (!ndev->cmd_wq) {
886 		rc = -ENOMEM;
887 		goto exit;
888 	}
889 
890 	INIT_WORK(&ndev->rx_work, nci_rx_work);
891 	snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
892 	ndev->rx_wq = create_singlethread_workqueue(name);
893 	if (!ndev->rx_wq) {
894 		rc = -ENOMEM;
895 		goto destroy_cmd_wq_exit;
896 	}
897 
898 	INIT_WORK(&ndev->tx_work, nci_tx_work);
899 	snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
900 	ndev->tx_wq = create_singlethread_workqueue(name);
901 	if (!ndev->tx_wq) {
902 		rc = -ENOMEM;
903 		goto destroy_rx_wq_exit;
904 	}
905 
906 	skb_queue_head_init(&ndev->cmd_q);
907 	skb_queue_head_init(&ndev->rx_q);
908 	skb_queue_head_init(&ndev->tx_q);
909 
910 	setup_timer(&ndev->cmd_timer, nci_cmd_timer,
911 		    (unsigned long) ndev);
912 	setup_timer(&ndev->data_timer, nci_data_timer,
913 		    (unsigned long) ndev);
914 
915 	mutex_init(&ndev->req_lock);
916 
917 	rc = nfc_register_device(ndev->nfc_dev);
918 	if (rc)
919 		goto destroy_rx_wq_exit;
920 
921 	goto exit;
922 
923 destroy_rx_wq_exit:
924 	destroy_workqueue(ndev->rx_wq);
925 
926 destroy_cmd_wq_exit:
927 	destroy_workqueue(ndev->cmd_wq);
928 
929 exit:
930 	return rc;
931 }
932 EXPORT_SYMBOL(nci_register_device);
933 
934 /**
935  * nci_unregister_device - unregister a nci device in the nfc subsystem
936  *
937  * @dev: The nci device to unregister
938  */
939 void nci_unregister_device(struct nci_dev *ndev)
940 {
941 	nci_close_device(ndev);
942 
943 	destroy_workqueue(ndev->cmd_wq);
944 	destroy_workqueue(ndev->rx_wq);
945 	destroy_workqueue(ndev->tx_wq);
946 
947 	nfc_unregister_device(ndev->nfc_dev);
948 }
949 EXPORT_SYMBOL(nci_unregister_device);
950 
951 /**
952  * nci_recv_frame - receive frame from NCI drivers
953  *
954  * @ndev: The nci device
955  * @skb: The sk_buff to receive
956  */
957 int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
958 {
959 	pr_debug("len %d\n", skb->len);
960 
961 	if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
962 	    !test_bit(NCI_INIT, &ndev->flags))) {
963 		kfree_skb(skb);
964 		return -ENXIO;
965 	}
966 
967 	/* Queue frame for rx worker thread */
968 	skb_queue_tail(&ndev->rx_q, skb);
969 	queue_work(ndev->rx_wq, &ndev->rx_work);
970 
971 	return 0;
972 }
973 EXPORT_SYMBOL(nci_recv_frame);
974 
975 static int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
976 {
977 	pr_debug("len %d\n", skb->len);
978 
979 	if (!ndev) {
980 		kfree_skb(skb);
981 		return -ENODEV;
982 	}
983 
984 	/* Get rid of skb owner, prior to sending to the driver. */
985 	skb_orphan(skb);
986 
987 	/* Send copy to sniffer */
988 	nfc_send_to_raw_sock(ndev->nfc_dev, skb,
989 			     RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
990 
991 	return ndev->ops->send(ndev, skb);
992 }
993 
994 /* Send NCI command */
995 int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
996 {
997 	struct nci_ctrl_hdr *hdr;
998 	struct sk_buff *skb;
999 
1000 	pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
1001 
1002 	skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
1003 	if (!skb) {
1004 		pr_err("no memory for command\n");
1005 		return -ENOMEM;
1006 	}
1007 
1008 	hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
1009 	hdr->gid = nci_opcode_gid(opcode);
1010 	hdr->oid = nci_opcode_oid(opcode);
1011 	hdr->plen = plen;
1012 
1013 	nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
1014 	nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
1015 
1016 	if (plen)
1017 		memcpy(skb_put(skb, plen), payload, plen);
1018 
1019 	skb_queue_tail(&ndev->cmd_q, skb);
1020 	queue_work(ndev->cmd_wq, &ndev->cmd_work);
1021 
1022 	return 0;
1023 }
1024 
1025 /* ---- NCI TX Data worker thread ---- */
1026 
1027 static void nci_tx_work(struct work_struct *work)
1028 {
1029 	struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
1030 	struct sk_buff *skb;
1031 
1032 	pr_debug("credits_cnt %d\n", atomic_read(&ndev->credits_cnt));
1033 
1034 	/* Send queued tx data */
1035 	while (atomic_read(&ndev->credits_cnt)) {
1036 		skb = skb_dequeue(&ndev->tx_q);
1037 		if (!skb)
1038 			return;
1039 
1040 		/* Check if data flow control is used */
1041 		if (atomic_read(&ndev->credits_cnt) !=
1042 		    NCI_DATA_FLOW_CONTROL_NOT_USED)
1043 			atomic_dec(&ndev->credits_cnt);
1044 
1045 		pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1046 			 nci_pbf(skb->data),
1047 			 nci_conn_id(skb->data),
1048 			 nci_plen(skb->data));
1049 
1050 		nci_send_frame(ndev, skb);
1051 
1052 		mod_timer(&ndev->data_timer,
1053 			  jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
1054 	}
1055 }
1056 
1057 /* ----- NCI RX worker thread (data & control) ----- */
1058 
1059 static void nci_rx_work(struct work_struct *work)
1060 {
1061 	struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
1062 	struct sk_buff *skb;
1063 
1064 	while ((skb = skb_dequeue(&ndev->rx_q))) {
1065 
1066 		/* Send copy to sniffer */
1067 		nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1068 				     RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
1069 
1070 		/* Process frame */
1071 		switch (nci_mt(skb->data)) {
1072 		case NCI_MT_RSP_PKT:
1073 			nci_rsp_packet(ndev, skb);
1074 			break;
1075 
1076 		case NCI_MT_NTF_PKT:
1077 			nci_ntf_packet(ndev, skb);
1078 			break;
1079 
1080 		case NCI_MT_DATA_PKT:
1081 			nci_rx_data_packet(ndev, skb);
1082 			break;
1083 
1084 		default:
1085 			pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
1086 			kfree_skb(skb);
1087 			break;
1088 		}
1089 	}
1090 
1091 	/* check if a data exchange timout has occurred */
1092 	if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
1093 		/* complete the data exchange transaction, if exists */
1094 		if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1095 			nci_data_exchange_complete(ndev, NULL, -ETIMEDOUT);
1096 
1097 		clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
1098 	}
1099 }
1100 
1101 /* ----- NCI TX CMD worker thread ----- */
1102 
1103 static void nci_cmd_work(struct work_struct *work)
1104 {
1105 	struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
1106 	struct sk_buff *skb;
1107 
1108 	pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
1109 
1110 	/* Send queued command */
1111 	if (atomic_read(&ndev->cmd_cnt)) {
1112 		skb = skb_dequeue(&ndev->cmd_q);
1113 		if (!skb)
1114 			return;
1115 
1116 		atomic_dec(&ndev->cmd_cnt);
1117 
1118 		pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1119 			 nci_pbf(skb->data),
1120 			 nci_opcode_gid(nci_opcode(skb->data)),
1121 			 nci_opcode_oid(nci_opcode(skb->data)),
1122 			 nci_plen(skb->data));
1123 
1124 		nci_send_frame(ndev, skb);
1125 
1126 		mod_timer(&ndev->cmd_timer,
1127 			  jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
1128 	}
1129 }
1130 
1131 MODULE_LICENSE("GPL");
1132