xref: /linux/net/nfc/hci/core.c (revision 905e46acd3272d04566fec49afbd7ad9e2ed9ae3)
1 /*
2  * Copyright (C) 2012  Intel Corporation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #define pr_fmt(fmt) "hci: %s: " fmt, __func__
19 
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/nfc.h>
24 
25 #include <net/nfc/nfc.h>
26 #include <net/nfc/hci.h>
27 #include <net/nfc/llc.h>
28 
29 #include "hci.h"
30 
31 /* Largest headroom needed for outgoing HCI commands */
32 #define HCI_CMDS_HEADROOM 1
33 
34 int nfc_hci_result_to_errno(u8 result)
35 {
36 	switch (result) {
37 	case NFC_HCI_ANY_OK:
38 		return 0;
39 	case NFC_HCI_ANY_E_REG_PAR_UNKNOWN:
40 		return -EOPNOTSUPP;
41 	case NFC_HCI_ANY_E_TIMEOUT:
42 		return -ETIME;
43 	default:
44 		return -1;
45 	}
46 }
47 EXPORT_SYMBOL(nfc_hci_result_to_errno);
48 
49 void nfc_hci_reset_pipes(struct nfc_hci_dev *hdev)
50 {
51 	int i = 0;
52 
53 	for (i = 0; i < NFC_HCI_MAX_PIPES; i++) {
54 		hdev->pipes[i].gate = NFC_HCI_INVALID_GATE;
55 		hdev->pipes[i].dest_host = NFC_HCI_INVALID_HOST;
56 	}
57 	memset(hdev->gate2pipe, NFC_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe));
58 }
59 EXPORT_SYMBOL(nfc_hci_reset_pipes);
60 
61 void nfc_hci_reset_pipes_per_host(struct nfc_hci_dev *hdev, u8 host)
62 {
63 	int i = 0;
64 
65 	for (i = 0; i < NFC_HCI_MAX_PIPES; i++) {
66 		if (hdev->pipes[i].dest_host != host)
67 			continue;
68 
69 		hdev->pipes[i].gate = NFC_HCI_INVALID_GATE;
70 		hdev->pipes[i].dest_host = NFC_HCI_INVALID_HOST;
71 	}
72 }
73 EXPORT_SYMBOL(nfc_hci_reset_pipes_per_host);
74 
75 static void nfc_hci_msg_tx_work(struct work_struct *work)
76 {
77 	struct nfc_hci_dev *hdev = container_of(work, struct nfc_hci_dev,
78 						msg_tx_work);
79 	struct hci_msg *msg;
80 	struct sk_buff *skb;
81 	int r = 0;
82 
83 	mutex_lock(&hdev->msg_tx_mutex);
84 	if (hdev->shutting_down)
85 		goto exit;
86 
87 	if (hdev->cmd_pending_msg) {
88 		if (timer_pending(&hdev->cmd_timer) == 0) {
89 			if (hdev->cmd_pending_msg->cb)
90 				hdev->cmd_pending_msg->cb(hdev->
91 							  cmd_pending_msg->
92 							  cb_context,
93 							  NULL,
94 							  -ETIME);
95 			kfree(hdev->cmd_pending_msg);
96 			hdev->cmd_pending_msg = NULL;
97 		} else {
98 			goto exit;
99 		}
100 	}
101 
102 next_msg:
103 	if (list_empty(&hdev->msg_tx_queue))
104 		goto exit;
105 
106 	msg = list_first_entry(&hdev->msg_tx_queue, struct hci_msg, msg_l);
107 	list_del(&msg->msg_l);
108 
109 	pr_debug("msg_tx_queue has a cmd to send\n");
110 	while ((skb = skb_dequeue(&msg->msg_frags)) != NULL) {
111 		r = nfc_llc_xmit_from_hci(hdev->llc, skb);
112 		if (r < 0) {
113 			kfree_skb(skb);
114 			skb_queue_purge(&msg->msg_frags);
115 			if (msg->cb)
116 				msg->cb(msg->cb_context, NULL, r);
117 			kfree(msg);
118 			break;
119 		}
120 	}
121 
122 	if (r)
123 		goto next_msg;
124 
125 	if (msg->wait_response == false) {
126 		kfree(msg);
127 		goto next_msg;
128 	}
129 
130 	hdev->cmd_pending_msg = msg;
131 	mod_timer(&hdev->cmd_timer, jiffies +
132 		  msecs_to_jiffies(hdev->cmd_pending_msg->completion_delay));
133 
134 exit:
135 	mutex_unlock(&hdev->msg_tx_mutex);
136 }
137 
138 static void nfc_hci_msg_rx_work(struct work_struct *work)
139 {
140 	struct nfc_hci_dev *hdev = container_of(work, struct nfc_hci_dev,
141 						msg_rx_work);
142 	struct sk_buff *skb;
143 	struct hcp_message *message;
144 	u8 pipe;
145 	u8 type;
146 	u8 instruction;
147 
148 	while ((skb = skb_dequeue(&hdev->msg_rx_queue)) != NULL) {
149 		pipe = skb->data[0];
150 		skb_pull(skb, NFC_HCI_HCP_PACKET_HEADER_LEN);
151 		message = (struct hcp_message *)skb->data;
152 		type = HCP_MSG_GET_TYPE(message->header);
153 		instruction = HCP_MSG_GET_CMD(message->header);
154 		skb_pull(skb, NFC_HCI_HCP_MESSAGE_HEADER_LEN);
155 
156 		nfc_hci_hcp_message_rx(hdev, pipe, type, instruction, skb);
157 	}
158 }
159 
160 static void __nfc_hci_cmd_completion(struct nfc_hci_dev *hdev, int err,
161 				     struct sk_buff *skb)
162 {
163 	del_timer_sync(&hdev->cmd_timer);
164 
165 	if (hdev->cmd_pending_msg->cb)
166 		hdev->cmd_pending_msg->cb(hdev->cmd_pending_msg->cb_context,
167 					  skb, err);
168 	else
169 		kfree_skb(skb);
170 
171 	kfree(hdev->cmd_pending_msg);
172 	hdev->cmd_pending_msg = NULL;
173 
174 	schedule_work(&hdev->msg_tx_work);
175 }
176 
177 void nfc_hci_resp_received(struct nfc_hci_dev *hdev, u8 result,
178 			   struct sk_buff *skb)
179 {
180 	mutex_lock(&hdev->msg_tx_mutex);
181 
182 	if (hdev->cmd_pending_msg == NULL) {
183 		kfree_skb(skb);
184 		goto exit;
185 	}
186 
187 	__nfc_hci_cmd_completion(hdev, nfc_hci_result_to_errno(result), skb);
188 
189 exit:
190 	mutex_unlock(&hdev->msg_tx_mutex);
191 }
192 
193 void nfc_hci_cmd_received(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
194 			  struct sk_buff *skb)
195 {
196 	u8 gate = hdev->pipes[pipe].gate;
197 	u8 status = NFC_HCI_ANY_OK;
198 	struct hci_create_pipe_resp *create_info;
199 	struct hci_delete_pipe_noti *delete_info;
200 	struct hci_all_pipe_cleared_noti *cleared_info;
201 
202 	pr_debug("from gate %x pipe %x cmd %x\n", gate, pipe, cmd);
203 
204 	switch (cmd) {
205 	case NFC_HCI_ADM_NOTIFY_PIPE_CREATED:
206 		if (skb->len != 5) {
207 			status = NFC_HCI_ANY_E_NOK;
208 			goto exit;
209 		}
210 		create_info = (struct hci_create_pipe_resp *)skb->data;
211 
212 		/* Save the new created pipe and bind with local gate,
213 		 * the description for skb->data[3] is destination gate id
214 		 * but since we received this cmd from host controller, we
215 		 * are the destination and it is our local gate
216 		 */
217 		hdev->gate2pipe[create_info->dest_gate] = create_info->pipe;
218 		hdev->pipes[create_info->pipe].gate = create_info->dest_gate;
219 		hdev->pipes[create_info->pipe].dest_host =
220 							create_info->src_host;
221 		break;
222 	case NFC_HCI_ANY_OPEN_PIPE:
223 		if (gate == NFC_HCI_INVALID_GATE) {
224 			status = NFC_HCI_ANY_E_NOK;
225 			goto exit;
226 		}
227 		break;
228 	case NFC_HCI_ADM_NOTIFY_PIPE_DELETED:
229 		if (skb->len != 1) {
230 			status = NFC_HCI_ANY_E_NOK;
231 			goto exit;
232 		}
233 		delete_info = (struct hci_delete_pipe_noti *)skb->data;
234 
235 		hdev->pipes[delete_info->pipe].gate = NFC_HCI_INVALID_GATE;
236 		hdev->pipes[delete_info->pipe].dest_host = NFC_HCI_INVALID_HOST;
237 		break;
238 	case NFC_HCI_ADM_NOTIFY_ALL_PIPE_CLEARED:
239 		if (skb->len != 1) {
240 			status = NFC_HCI_ANY_E_NOK;
241 			goto exit;
242 		}
243 		cleared_info = (struct hci_all_pipe_cleared_noti *)skb->data;
244 
245 		nfc_hci_reset_pipes_per_host(hdev, cleared_info->host);
246 		break;
247 	default:
248 		pr_info("Discarded unknown cmd %x to gate %x\n", cmd, gate);
249 		break;
250 	}
251 
252 	if (hdev->ops->cmd_received)
253 		hdev->ops->cmd_received(hdev, pipe, cmd, skb);
254 
255 exit:
256 	nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_RESPONSE,
257 			       status, NULL, 0, NULL, NULL, 0);
258 
259 	kfree_skb(skb);
260 }
261 
262 u32 nfc_hci_sak_to_protocol(u8 sak)
263 {
264 	switch (NFC_HCI_TYPE_A_SEL_PROT(sak)) {
265 	case NFC_HCI_TYPE_A_SEL_PROT_MIFARE:
266 		return NFC_PROTO_MIFARE_MASK;
267 	case NFC_HCI_TYPE_A_SEL_PROT_ISO14443:
268 		return NFC_PROTO_ISO14443_MASK;
269 	case NFC_HCI_TYPE_A_SEL_PROT_DEP:
270 		return NFC_PROTO_NFC_DEP_MASK;
271 	case NFC_HCI_TYPE_A_SEL_PROT_ISO14443_DEP:
272 		return NFC_PROTO_ISO14443_MASK | NFC_PROTO_NFC_DEP_MASK;
273 	default:
274 		return 0xffffffff;
275 	}
276 }
277 EXPORT_SYMBOL(nfc_hci_sak_to_protocol);
278 
279 int nfc_hci_target_discovered(struct nfc_hci_dev *hdev, u8 gate)
280 {
281 	struct nfc_target *targets;
282 	struct sk_buff *atqa_skb = NULL;
283 	struct sk_buff *sak_skb = NULL;
284 	struct sk_buff *uid_skb = NULL;
285 	int r;
286 
287 	pr_debug("from gate %d\n", gate);
288 
289 	targets = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
290 	if (targets == NULL)
291 		return -ENOMEM;
292 
293 	switch (gate) {
294 	case NFC_HCI_RF_READER_A_GATE:
295 		r = nfc_hci_get_param(hdev, NFC_HCI_RF_READER_A_GATE,
296 				      NFC_HCI_RF_READER_A_ATQA, &atqa_skb);
297 		if (r < 0)
298 			goto exit;
299 
300 		r = nfc_hci_get_param(hdev, NFC_HCI_RF_READER_A_GATE,
301 				      NFC_HCI_RF_READER_A_SAK, &sak_skb);
302 		if (r < 0)
303 			goto exit;
304 
305 		if (atqa_skb->len != 2 || sak_skb->len != 1) {
306 			r = -EPROTO;
307 			goto exit;
308 		}
309 
310 		targets->supported_protocols =
311 				nfc_hci_sak_to_protocol(sak_skb->data[0]);
312 		if (targets->supported_protocols == 0xffffffff) {
313 			r = -EPROTO;
314 			goto exit;
315 		}
316 
317 		targets->sens_res = be16_to_cpu(*(__be16 *)atqa_skb->data);
318 		targets->sel_res = sak_skb->data[0];
319 
320 		r = nfc_hci_get_param(hdev, NFC_HCI_RF_READER_A_GATE,
321 				      NFC_HCI_RF_READER_A_UID, &uid_skb);
322 		if (r < 0)
323 			goto exit;
324 
325 		if (uid_skb->len == 0 || uid_skb->len > NFC_NFCID1_MAXSIZE) {
326 			r = -EPROTO;
327 			goto exit;
328 		}
329 
330 		memcpy(targets->nfcid1, uid_skb->data, uid_skb->len);
331 		targets->nfcid1_len = uid_skb->len;
332 
333 		if (hdev->ops->complete_target_discovered) {
334 			r = hdev->ops->complete_target_discovered(hdev, gate,
335 								  targets);
336 			if (r < 0)
337 				goto exit;
338 		}
339 		break;
340 	case NFC_HCI_RF_READER_B_GATE:
341 		targets->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
342 		break;
343 	default:
344 		if (hdev->ops->target_from_gate)
345 			r = hdev->ops->target_from_gate(hdev, gate, targets);
346 		else
347 			r = -EPROTO;
348 		if (r < 0)
349 			goto exit;
350 
351 		if (hdev->ops->complete_target_discovered) {
352 			r = hdev->ops->complete_target_discovered(hdev, gate,
353 								  targets);
354 			if (r < 0)
355 				goto exit;
356 		}
357 		break;
358 	}
359 
360 	/* if driver set the new gate, we will skip the old one */
361 	if (targets->hci_reader_gate == 0x00)
362 		targets->hci_reader_gate = gate;
363 
364 	r = nfc_targets_found(hdev->ndev, targets, 1);
365 
366 exit:
367 	kfree(targets);
368 	kfree_skb(atqa_skb);
369 	kfree_skb(sak_skb);
370 	kfree_skb(uid_skb);
371 
372 	return r;
373 }
374 EXPORT_SYMBOL(nfc_hci_target_discovered);
375 
376 void nfc_hci_event_received(struct nfc_hci_dev *hdev, u8 pipe, u8 event,
377 			    struct sk_buff *skb)
378 {
379 	int r = 0;
380 	u8 gate = hdev->pipes[pipe].gate;
381 
382 	if (gate == NFC_HCI_INVALID_GATE) {
383 		pr_err("Discarded event %x to unopened pipe %x\n", event, pipe);
384 		goto exit;
385 	}
386 
387 	if (hdev->ops->event_received) {
388 		r = hdev->ops->event_received(hdev, pipe, event, skb);
389 		if (r <= 0)
390 			goto exit_noskb;
391 	}
392 
393 	switch (event) {
394 	case NFC_HCI_EVT_TARGET_DISCOVERED:
395 		if (skb->len < 1) {	/* no status data? */
396 			r = -EPROTO;
397 			goto exit;
398 		}
399 
400 		if (skb->data[0] == 3) {
401 			/* TODO: Multiple targets in field, none activated
402 			 * poll is supposedly stopped, but there is no
403 			 * single target to activate, so nothing to report
404 			 * up.
405 			 * if we need to restart poll, we must save the
406 			 * protocols from the initial poll and reuse here.
407 			 */
408 		}
409 
410 		if (skb->data[0] != 0) {
411 			r = -EPROTO;
412 			goto exit;
413 		}
414 
415 		r = nfc_hci_target_discovered(hdev, gate);
416 		break;
417 	default:
418 		pr_info("Discarded unknown event %x to gate %x\n", event, gate);
419 		r = -EINVAL;
420 		break;
421 	}
422 
423 exit:
424 	kfree_skb(skb);
425 
426 exit_noskb:
427 	if (r)
428 		nfc_hci_driver_failure(hdev, r);
429 }
430 
431 static void nfc_hci_cmd_timeout(unsigned long data)
432 {
433 	struct nfc_hci_dev *hdev = (struct nfc_hci_dev *)data;
434 
435 	schedule_work(&hdev->msg_tx_work);
436 }
437 
438 static int hci_dev_connect_gates(struct nfc_hci_dev *hdev, u8 gate_count,
439 				 struct nfc_hci_gate *gates)
440 {
441 	int r;
442 	while (gate_count--) {
443 		r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
444 					 gates->gate, gates->pipe);
445 		if (r < 0)
446 			return r;
447 		gates++;
448 	}
449 
450 	return 0;
451 }
452 
453 static int hci_dev_session_init(struct nfc_hci_dev *hdev)
454 {
455 	struct sk_buff *skb = NULL;
456 	int r;
457 
458 	if (hdev->init_data.gates[0].gate != NFC_HCI_ADMIN_GATE)
459 		return -EPROTO;
460 
461 	r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
462 				 hdev->init_data.gates[0].gate,
463 				 hdev->init_data.gates[0].pipe);
464 	if (r < 0)
465 		goto exit;
466 
467 	r = nfc_hci_get_param(hdev, NFC_HCI_ADMIN_GATE,
468 			      NFC_HCI_ADMIN_SESSION_IDENTITY, &skb);
469 	if (r < 0)
470 		goto disconnect_all;
471 
472 	if (skb->len && skb->len == strlen(hdev->init_data.session_id) &&
473 		(memcmp(hdev->init_data.session_id, skb->data,
474 			   skb->len) == 0) && hdev->ops->load_session) {
475 		/* Restore gate<->pipe table from some proprietary location. */
476 
477 		r = hdev->ops->load_session(hdev);
478 
479 		if (r < 0)
480 			goto disconnect_all;
481 	} else {
482 
483 		r = nfc_hci_disconnect_all_gates(hdev);
484 		if (r < 0)
485 			goto exit;
486 
487 		r = hci_dev_connect_gates(hdev, hdev->init_data.gate_count,
488 					  hdev->init_data.gates);
489 		if (r < 0)
490 			goto disconnect_all;
491 
492 		r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
493 				NFC_HCI_ADMIN_SESSION_IDENTITY,
494 				hdev->init_data.session_id,
495 				strlen(hdev->init_data.session_id));
496 	}
497 	if (r == 0)
498 		goto exit;
499 
500 disconnect_all:
501 	nfc_hci_disconnect_all_gates(hdev);
502 
503 exit:
504 	kfree_skb(skb);
505 
506 	return r;
507 }
508 
509 static int hci_dev_version(struct nfc_hci_dev *hdev)
510 {
511 	int r;
512 	struct sk_buff *skb;
513 
514 	r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
515 			      NFC_HCI_ID_MGMT_VERSION_SW, &skb);
516 	if (r == -EOPNOTSUPP) {
517 		pr_info("Software/Hardware info not available\n");
518 		return 0;
519 	}
520 	if (r < 0)
521 		return r;
522 
523 	if (skb->len != 3) {
524 		kfree_skb(skb);
525 		return -EINVAL;
526 	}
527 
528 	hdev->sw_romlib = (skb->data[0] & 0xf0) >> 4;
529 	hdev->sw_patch = skb->data[0] & 0x0f;
530 	hdev->sw_flashlib_major = skb->data[1];
531 	hdev->sw_flashlib_minor = skb->data[2];
532 
533 	kfree_skb(skb);
534 
535 	r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
536 			      NFC_HCI_ID_MGMT_VERSION_HW, &skb);
537 	if (r < 0)
538 		return r;
539 
540 	if (skb->len != 3) {
541 		kfree_skb(skb);
542 		return -EINVAL;
543 	}
544 
545 	hdev->hw_derivative = (skb->data[0] & 0xe0) >> 5;
546 	hdev->hw_version = skb->data[0] & 0x1f;
547 	hdev->hw_mpw = (skb->data[1] & 0xc0) >> 6;
548 	hdev->hw_software = skb->data[1] & 0x3f;
549 	hdev->hw_bsid = skb->data[2];
550 
551 	kfree_skb(skb);
552 
553 	pr_info("SOFTWARE INFO:\n");
554 	pr_info("RomLib         : %d\n", hdev->sw_romlib);
555 	pr_info("Patch          : %d\n", hdev->sw_patch);
556 	pr_info("FlashLib Major : %d\n", hdev->sw_flashlib_major);
557 	pr_info("FlashLib Minor : %d\n", hdev->sw_flashlib_minor);
558 	pr_info("HARDWARE INFO:\n");
559 	pr_info("Derivative     : %d\n", hdev->hw_derivative);
560 	pr_info("HW Version     : %d\n", hdev->hw_version);
561 	pr_info("#MPW           : %d\n", hdev->hw_mpw);
562 	pr_info("Software       : %d\n", hdev->hw_software);
563 	pr_info("BSID Version   : %d\n", hdev->hw_bsid);
564 
565 	return 0;
566 }
567 
568 static int hci_dev_up(struct nfc_dev *nfc_dev)
569 {
570 	struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
571 	int r = 0;
572 
573 	if (hdev->ops->open) {
574 		r = hdev->ops->open(hdev);
575 		if (r < 0)
576 			return r;
577 	}
578 
579 	r = nfc_llc_start(hdev->llc);
580 	if (r < 0)
581 		goto exit_close;
582 
583 	r = hci_dev_session_init(hdev);
584 	if (r < 0)
585 		goto exit_llc;
586 
587 	r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
588 			       NFC_HCI_EVT_END_OPERATION, NULL, 0);
589 	if (r < 0)
590 		goto exit_llc;
591 
592 	if (hdev->ops->hci_ready) {
593 		r = hdev->ops->hci_ready(hdev);
594 		if (r < 0)
595 			goto exit_llc;
596 	}
597 
598 	r = hci_dev_version(hdev);
599 	if (r < 0)
600 		goto exit_llc;
601 
602 	return 0;
603 
604 exit_llc:
605 	nfc_llc_stop(hdev->llc);
606 
607 exit_close:
608 	if (hdev->ops->close)
609 		hdev->ops->close(hdev);
610 
611 	return r;
612 }
613 
614 static int hci_dev_down(struct nfc_dev *nfc_dev)
615 {
616 	struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
617 
618 	nfc_llc_stop(hdev->llc);
619 
620 	if (hdev->ops->close)
621 		hdev->ops->close(hdev);
622 
623 	nfc_hci_reset_pipes(hdev);
624 
625 	return 0;
626 }
627 
628 static int hci_start_poll(struct nfc_dev *nfc_dev,
629 			  u32 im_protocols, u32 tm_protocols)
630 {
631 	struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
632 
633 	if (hdev->ops->start_poll)
634 		return hdev->ops->start_poll(hdev, im_protocols, tm_protocols);
635 	else
636 		return nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
637 					  NFC_HCI_EVT_READER_REQUESTED,
638 					  NULL, 0);
639 }
640 
641 static void hci_stop_poll(struct nfc_dev *nfc_dev)
642 {
643 	struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
644 
645 	if (hdev->ops->stop_poll)
646 		hdev->ops->stop_poll(hdev);
647 	else
648 		nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
649 				   NFC_HCI_EVT_END_OPERATION, NULL, 0);
650 }
651 
652 static int hci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
653 				__u8 comm_mode, __u8 *gb, size_t gb_len)
654 {
655 	struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
656 
657 	if (!hdev->ops->dep_link_up)
658 		return 0;
659 
660 	return hdev->ops->dep_link_up(hdev, target, comm_mode,
661 				      gb, gb_len);
662 }
663 
664 static int hci_dep_link_down(struct nfc_dev *nfc_dev)
665 {
666 	struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
667 
668 	if (!hdev->ops->dep_link_down)
669 		return 0;
670 
671 	return hdev->ops->dep_link_down(hdev);
672 }
673 
674 static int hci_activate_target(struct nfc_dev *nfc_dev,
675 			       struct nfc_target *target, u32 protocol)
676 {
677 	return 0;
678 }
679 
680 static void hci_deactivate_target(struct nfc_dev *nfc_dev,
681 				  struct nfc_target *target,
682 				  u8 mode)
683 {
684 }
685 
686 #define HCI_CB_TYPE_TRANSCEIVE 1
687 
688 static void hci_transceive_cb(void *context, struct sk_buff *skb, int err)
689 {
690 	struct nfc_hci_dev *hdev = context;
691 
692 	switch (hdev->async_cb_type) {
693 	case HCI_CB_TYPE_TRANSCEIVE:
694 		/*
695 		 * TODO: Check RF Error indicator to make sure data is valid.
696 		 * It seems that HCI cmd can complete without error, but data
697 		 * can be invalid if an RF error occured? Ignore for now.
698 		 */
699 		if (err == 0)
700 			skb_trim(skb, skb->len - 1); /* RF Err ind */
701 
702 		hdev->async_cb(hdev->async_cb_context, skb, err);
703 		break;
704 	default:
705 		if (err == 0)
706 			kfree_skb(skb);
707 		break;
708 	}
709 }
710 
711 static int hci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
712 			  struct sk_buff *skb, data_exchange_cb_t cb,
713 			  void *cb_context)
714 {
715 	struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
716 	int r;
717 
718 	pr_debug("target_idx=%d\n", target->idx);
719 
720 	switch (target->hci_reader_gate) {
721 	case NFC_HCI_RF_READER_A_GATE:
722 	case NFC_HCI_RF_READER_B_GATE:
723 		if (hdev->ops->im_transceive) {
724 			r = hdev->ops->im_transceive(hdev, target, skb, cb,
725 						     cb_context);
726 			if (r <= 0)	/* handled */
727 				break;
728 		}
729 
730 		*skb_push(skb, 1) = 0;	/* CTR, see spec:10.2.2.1 */
731 
732 		hdev->async_cb_type = HCI_CB_TYPE_TRANSCEIVE;
733 		hdev->async_cb = cb;
734 		hdev->async_cb_context = cb_context;
735 
736 		r = nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
737 					   NFC_HCI_WR_XCHG_DATA, skb->data,
738 					   skb->len, hci_transceive_cb, hdev);
739 		break;
740 	default:
741 		if (hdev->ops->im_transceive) {
742 			r = hdev->ops->im_transceive(hdev, target, skb, cb,
743 						     cb_context);
744 			if (r == 1)
745 				r = -ENOTSUPP;
746 		} else {
747 			r = -ENOTSUPP;
748 		}
749 		break;
750 	}
751 
752 	kfree_skb(skb);
753 
754 	return r;
755 }
756 
757 static int hci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
758 {
759 	struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
760 
761 	if (!hdev->ops->tm_send) {
762 		kfree_skb(skb);
763 		return -ENOTSUPP;
764 	}
765 
766 	return hdev->ops->tm_send(hdev, skb);
767 }
768 
769 static int hci_check_presence(struct nfc_dev *nfc_dev,
770 			      struct nfc_target *target)
771 {
772 	struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
773 
774 	if (!hdev->ops->check_presence)
775 		return 0;
776 
777 	return hdev->ops->check_presence(hdev, target);
778 }
779 
780 static int hci_discover_se(struct nfc_dev *nfc_dev)
781 {
782 	struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
783 
784 	if (hdev->ops->discover_se)
785 		return hdev->ops->discover_se(hdev);
786 
787 	return 0;
788 }
789 
790 static int hci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
791 {
792 	struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
793 
794 	if (hdev->ops->enable_se)
795 		return hdev->ops->enable_se(hdev, se_idx);
796 
797 	return 0;
798 }
799 
800 static int hci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
801 {
802 	struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
803 
804 	if (hdev->ops->disable_se)
805 		return hdev->ops->disable_se(hdev, se_idx);
806 
807 	return 0;
808 }
809 
810 static int hci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
811 		     u8 *apdu, size_t apdu_length,
812 		     se_io_cb_t cb, void *cb_context)
813 {
814 	struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
815 
816 	if (hdev->ops->se_io)
817 		return hdev->ops->se_io(hdev, se_idx, apdu,
818 					apdu_length, cb, cb_context);
819 
820 	return 0;
821 }
822 
823 static void nfc_hci_failure(struct nfc_hci_dev *hdev, int err)
824 {
825 	mutex_lock(&hdev->msg_tx_mutex);
826 
827 	if (hdev->cmd_pending_msg == NULL) {
828 		nfc_driver_failure(hdev->ndev, err);
829 		goto exit;
830 	}
831 
832 	__nfc_hci_cmd_completion(hdev, err, NULL);
833 
834 exit:
835 	mutex_unlock(&hdev->msg_tx_mutex);
836 }
837 
838 static void nfc_hci_llc_failure(struct nfc_hci_dev *hdev, int err)
839 {
840 	nfc_hci_failure(hdev, err);
841 }
842 
843 static void nfc_hci_recv_from_llc(struct nfc_hci_dev *hdev, struct sk_buff *skb)
844 {
845 	struct hcp_packet *packet;
846 	u8 type;
847 	u8 instruction;
848 	struct sk_buff *hcp_skb;
849 	u8 pipe;
850 	struct sk_buff *frag_skb;
851 	int msg_len;
852 
853 	packet = (struct hcp_packet *)skb->data;
854 	if ((packet->header & ~NFC_HCI_FRAGMENT) == 0) {
855 		skb_queue_tail(&hdev->rx_hcp_frags, skb);
856 		return;
857 	}
858 
859 	/* it's the last fragment. Does it need re-aggregation? */
860 	if (skb_queue_len(&hdev->rx_hcp_frags)) {
861 		pipe = packet->header & NFC_HCI_FRAGMENT;
862 		skb_queue_tail(&hdev->rx_hcp_frags, skb);
863 
864 		msg_len = 0;
865 		skb_queue_walk(&hdev->rx_hcp_frags, frag_skb) {
866 			msg_len += (frag_skb->len -
867 				    NFC_HCI_HCP_PACKET_HEADER_LEN);
868 		}
869 
870 		hcp_skb = nfc_alloc_recv_skb(NFC_HCI_HCP_PACKET_HEADER_LEN +
871 					     msg_len, GFP_KERNEL);
872 		if (hcp_skb == NULL) {
873 			nfc_hci_failure(hdev, -ENOMEM);
874 			return;
875 		}
876 
877 		*skb_put(hcp_skb, NFC_HCI_HCP_PACKET_HEADER_LEN) = pipe;
878 
879 		skb_queue_walk(&hdev->rx_hcp_frags, frag_skb) {
880 			msg_len = frag_skb->len - NFC_HCI_HCP_PACKET_HEADER_LEN;
881 			memcpy(skb_put(hcp_skb, msg_len),
882 			       frag_skb->data + NFC_HCI_HCP_PACKET_HEADER_LEN,
883 			       msg_len);
884 		}
885 
886 		skb_queue_purge(&hdev->rx_hcp_frags);
887 	} else {
888 		packet->header &= NFC_HCI_FRAGMENT;
889 		hcp_skb = skb;
890 	}
891 
892 	/* if this is a response, dispatch immediately to
893 	 * unblock waiting cmd context. Otherwise, enqueue to dispatch
894 	 * in separate context where handler can also execute command.
895 	 */
896 	packet = (struct hcp_packet *)hcp_skb->data;
897 	type = HCP_MSG_GET_TYPE(packet->message.header);
898 	if (type == NFC_HCI_HCP_RESPONSE) {
899 		pipe = packet->header;
900 		instruction = HCP_MSG_GET_CMD(packet->message.header);
901 		skb_pull(hcp_skb, NFC_HCI_HCP_PACKET_HEADER_LEN +
902 			 NFC_HCI_HCP_MESSAGE_HEADER_LEN);
903 		nfc_hci_hcp_message_rx(hdev, pipe, type, instruction, hcp_skb);
904 	} else {
905 		skb_queue_tail(&hdev->msg_rx_queue, hcp_skb);
906 		schedule_work(&hdev->msg_rx_work);
907 	}
908 }
909 
910 static int hci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
911 {
912 	struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
913 
914 	if (!hdev->ops->fw_download)
915 		return -ENOTSUPP;
916 
917 	return hdev->ops->fw_download(hdev, firmware_name);
918 }
919 
920 static struct nfc_ops hci_nfc_ops = {
921 	.dev_up = hci_dev_up,
922 	.dev_down = hci_dev_down,
923 	.start_poll = hci_start_poll,
924 	.stop_poll = hci_stop_poll,
925 	.dep_link_up = hci_dep_link_up,
926 	.dep_link_down = hci_dep_link_down,
927 	.activate_target = hci_activate_target,
928 	.deactivate_target = hci_deactivate_target,
929 	.im_transceive = hci_transceive,
930 	.tm_send = hci_tm_send,
931 	.check_presence = hci_check_presence,
932 	.fw_download = hci_fw_download,
933 	.discover_se = hci_discover_se,
934 	.enable_se = hci_enable_se,
935 	.disable_se = hci_disable_se,
936 	.se_io = hci_se_io,
937 };
938 
939 struct nfc_hci_dev *nfc_hci_allocate_device(struct nfc_hci_ops *ops,
940 					    struct nfc_hci_init_data *init_data,
941 					    unsigned long quirks,
942 					    u32 protocols,
943 					    const char *llc_name,
944 					    int tx_headroom,
945 					    int tx_tailroom,
946 					    int max_link_payload)
947 {
948 	struct nfc_hci_dev *hdev;
949 
950 	if (ops->xmit == NULL)
951 		return NULL;
952 
953 	if (protocols == 0)
954 		return NULL;
955 
956 	hdev = kzalloc(sizeof(struct nfc_hci_dev), GFP_KERNEL);
957 	if (hdev == NULL)
958 		return NULL;
959 
960 	hdev->llc = nfc_llc_allocate(llc_name, hdev, ops->xmit,
961 				     nfc_hci_recv_from_llc, tx_headroom,
962 				     tx_tailroom, nfc_hci_llc_failure);
963 	if (hdev->llc == NULL) {
964 		kfree(hdev);
965 		return NULL;
966 	}
967 
968 	hdev->ndev = nfc_allocate_device(&hci_nfc_ops, protocols,
969 					 tx_headroom + HCI_CMDS_HEADROOM,
970 					 tx_tailroom);
971 	if (!hdev->ndev) {
972 		nfc_llc_free(hdev->llc);
973 		kfree(hdev);
974 		return NULL;
975 	}
976 
977 	hdev->ops = ops;
978 	hdev->max_data_link_payload = max_link_payload;
979 	hdev->init_data = *init_data;
980 
981 	nfc_set_drvdata(hdev->ndev, hdev);
982 
983 	nfc_hci_reset_pipes(hdev);
984 
985 	hdev->quirks = quirks;
986 
987 	return hdev;
988 }
989 EXPORT_SYMBOL(nfc_hci_allocate_device);
990 
991 void nfc_hci_free_device(struct nfc_hci_dev *hdev)
992 {
993 	nfc_free_device(hdev->ndev);
994 	nfc_llc_free(hdev->llc);
995 	kfree(hdev);
996 }
997 EXPORT_SYMBOL(nfc_hci_free_device);
998 
999 int nfc_hci_register_device(struct nfc_hci_dev *hdev)
1000 {
1001 	mutex_init(&hdev->msg_tx_mutex);
1002 
1003 	INIT_LIST_HEAD(&hdev->msg_tx_queue);
1004 
1005 	INIT_WORK(&hdev->msg_tx_work, nfc_hci_msg_tx_work);
1006 
1007 	init_timer(&hdev->cmd_timer);
1008 	hdev->cmd_timer.data = (unsigned long)hdev;
1009 	hdev->cmd_timer.function = nfc_hci_cmd_timeout;
1010 
1011 	skb_queue_head_init(&hdev->rx_hcp_frags);
1012 
1013 	INIT_WORK(&hdev->msg_rx_work, nfc_hci_msg_rx_work);
1014 
1015 	skb_queue_head_init(&hdev->msg_rx_queue);
1016 
1017 	return nfc_register_device(hdev->ndev);
1018 }
1019 EXPORT_SYMBOL(nfc_hci_register_device);
1020 
1021 void nfc_hci_unregister_device(struct nfc_hci_dev *hdev)
1022 {
1023 	struct hci_msg *msg, *n;
1024 
1025 	mutex_lock(&hdev->msg_tx_mutex);
1026 
1027 	if (hdev->cmd_pending_msg) {
1028 		if (hdev->cmd_pending_msg->cb)
1029 			hdev->cmd_pending_msg->cb(
1030 					     hdev->cmd_pending_msg->cb_context,
1031 					     NULL, -ESHUTDOWN);
1032 		kfree(hdev->cmd_pending_msg);
1033 		hdev->cmd_pending_msg = NULL;
1034 	}
1035 
1036 	hdev->shutting_down = true;
1037 
1038 	mutex_unlock(&hdev->msg_tx_mutex);
1039 
1040 	del_timer_sync(&hdev->cmd_timer);
1041 	cancel_work_sync(&hdev->msg_tx_work);
1042 
1043 	cancel_work_sync(&hdev->msg_rx_work);
1044 
1045 	nfc_unregister_device(hdev->ndev);
1046 
1047 	skb_queue_purge(&hdev->rx_hcp_frags);
1048 	skb_queue_purge(&hdev->msg_rx_queue);
1049 
1050 	list_for_each_entry_safe(msg, n, &hdev->msg_tx_queue, msg_l) {
1051 		list_del(&msg->msg_l);
1052 		skb_queue_purge(&msg->msg_frags);
1053 		kfree(msg);
1054 	}
1055 }
1056 EXPORT_SYMBOL(nfc_hci_unregister_device);
1057 
1058 void nfc_hci_set_clientdata(struct nfc_hci_dev *hdev, void *clientdata)
1059 {
1060 	hdev->clientdata = clientdata;
1061 }
1062 EXPORT_SYMBOL(nfc_hci_set_clientdata);
1063 
1064 void *nfc_hci_get_clientdata(struct nfc_hci_dev *hdev)
1065 {
1066 	return hdev->clientdata;
1067 }
1068 EXPORT_SYMBOL(nfc_hci_get_clientdata);
1069 
1070 void nfc_hci_driver_failure(struct nfc_hci_dev *hdev, int err)
1071 {
1072 	nfc_hci_failure(hdev, err);
1073 }
1074 EXPORT_SYMBOL(nfc_hci_driver_failure);
1075 
1076 void nfc_hci_recv_frame(struct nfc_hci_dev *hdev, struct sk_buff *skb)
1077 {
1078 	nfc_llc_rcv_from_drv(hdev->llc, skb);
1079 }
1080 EXPORT_SYMBOL(nfc_hci_recv_frame);
1081 
1082 static int __init nfc_hci_init(void)
1083 {
1084 	return nfc_llc_init();
1085 }
1086 
1087 static void __exit nfc_hci_exit(void)
1088 {
1089 	nfc_llc_exit();
1090 }
1091 
1092 subsys_initcall(nfc_hci_init);
1093 module_exit(nfc_hci_exit);
1094 
1095 MODULE_LICENSE("GPL");
1096 MODULE_DESCRIPTION("NFC HCI Core");
1097