0001
0002
0003
0004
0005
0006 #define pr_fmt(fmt) "hci: %s: " fmt, __func__
0007
0008 #include <linux/init.h>
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/nfc.h>
0012
0013 #include <net/nfc/nfc.h>
0014 #include <net/nfc/hci.h>
0015 #include <net/nfc/llc.h>
0016
0017 #include "hci.h"
0018
0019
0020 #define HCI_CMDS_HEADROOM 1
0021
0022 int nfc_hci_result_to_errno(u8 result)
0023 {
0024 switch (result) {
0025 case NFC_HCI_ANY_OK:
0026 return 0;
0027 case NFC_HCI_ANY_E_REG_PAR_UNKNOWN:
0028 return -EOPNOTSUPP;
0029 case NFC_HCI_ANY_E_TIMEOUT:
0030 return -ETIME;
0031 default:
0032 return -1;
0033 }
0034 }
0035 EXPORT_SYMBOL(nfc_hci_result_to_errno);
0036
0037 void nfc_hci_reset_pipes(struct nfc_hci_dev *hdev)
0038 {
0039 int i = 0;
0040
0041 for (i = 0; i < NFC_HCI_MAX_PIPES; i++) {
0042 hdev->pipes[i].gate = NFC_HCI_INVALID_GATE;
0043 hdev->pipes[i].dest_host = NFC_HCI_INVALID_HOST;
0044 }
0045 memset(hdev->gate2pipe, NFC_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe));
0046 }
0047 EXPORT_SYMBOL(nfc_hci_reset_pipes);
0048
0049 void nfc_hci_reset_pipes_per_host(struct nfc_hci_dev *hdev, u8 host)
0050 {
0051 int i = 0;
0052
0053 for (i = 0; i < NFC_HCI_MAX_PIPES; i++) {
0054 if (hdev->pipes[i].dest_host != host)
0055 continue;
0056
0057 hdev->pipes[i].gate = NFC_HCI_INVALID_GATE;
0058 hdev->pipes[i].dest_host = NFC_HCI_INVALID_HOST;
0059 }
0060 }
0061 EXPORT_SYMBOL(nfc_hci_reset_pipes_per_host);
0062
0063 static void nfc_hci_msg_tx_work(struct work_struct *work)
0064 {
0065 struct nfc_hci_dev *hdev = container_of(work, struct nfc_hci_dev,
0066 msg_tx_work);
0067 struct hci_msg *msg;
0068 struct sk_buff *skb;
0069 int r = 0;
0070
0071 mutex_lock(&hdev->msg_tx_mutex);
0072 if (hdev->shutting_down)
0073 goto exit;
0074
0075 if (hdev->cmd_pending_msg) {
0076 if (timer_pending(&hdev->cmd_timer) == 0) {
0077 if (hdev->cmd_pending_msg->cb)
0078 hdev->cmd_pending_msg->cb(hdev->
0079 cmd_pending_msg->
0080 cb_context,
0081 NULL,
0082 -ETIME);
0083 kfree(hdev->cmd_pending_msg);
0084 hdev->cmd_pending_msg = NULL;
0085 } else {
0086 goto exit;
0087 }
0088 }
0089
0090 next_msg:
0091 if (list_empty(&hdev->msg_tx_queue))
0092 goto exit;
0093
0094 msg = list_first_entry(&hdev->msg_tx_queue, struct hci_msg, msg_l);
0095 list_del(&msg->msg_l);
0096
0097 pr_debug("msg_tx_queue has a cmd to send\n");
0098 while ((skb = skb_dequeue(&msg->msg_frags)) != NULL) {
0099 r = nfc_llc_xmit_from_hci(hdev->llc, skb);
0100 if (r < 0) {
0101 kfree_skb(skb);
0102 skb_queue_purge(&msg->msg_frags);
0103 if (msg->cb)
0104 msg->cb(msg->cb_context, NULL, r);
0105 kfree(msg);
0106 break;
0107 }
0108 }
0109
0110 if (r)
0111 goto next_msg;
0112
0113 if (msg->wait_response == false) {
0114 kfree(msg);
0115 goto next_msg;
0116 }
0117
0118 hdev->cmd_pending_msg = msg;
0119 mod_timer(&hdev->cmd_timer, jiffies +
0120 msecs_to_jiffies(hdev->cmd_pending_msg->completion_delay));
0121
0122 exit:
0123 mutex_unlock(&hdev->msg_tx_mutex);
0124 }
0125
0126 static void nfc_hci_msg_rx_work(struct work_struct *work)
0127 {
0128 struct nfc_hci_dev *hdev = container_of(work, struct nfc_hci_dev,
0129 msg_rx_work);
0130 struct sk_buff *skb;
0131 const struct hcp_message *message;
0132 u8 pipe;
0133 u8 type;
0134 u8 instruction;
0135
0136 while ((skb = skb_dequeue(&hdev->msg_rx_queue)) != NULL) {
0137 pipe = skb->data[0];
0138 skb_pull(skb, NFC_HCI_HCP_PACKET_HEADER_LEN);
0139 message = (struct hcp_message *)skb->data;
0140 type = HCP_MSG_GET_TYPE(message->header);
0141 instruction = HCP_MSG_GET_CMD(message->header);
0142 skb_pull(skb, NFC_HCI_HCP_MESSAGE_HEADER_LEN);
0143
0144 nfc_hci_hcp_message_rx(hdev, pipe, type, instruction, skb);
0145 }
0146 }
0147
0148 static void __nfc_hci_cmd_completion(struct nfc_hci_dev *hdev, int err,
0149 struct sk_buff *skb)
0150 {
0151 del_timer_sync(&hdev->cmd_timer);
0152
0153 if (hdev->cmd_pending_msg->cb)
0154 hdev->cmd_pending_msg->cb(hdev->cmd_pending_msg->cb_context,
0155 skb, err);
0156 else
0157 kfree_skb(skb);
0158
0159 kfree(hdev->cmd_pending_msg);
0160 hdev->cmd_pending_msg = NULL;
0161
0162 schedule_work(&hdev->msg_tx_work);
0163 }
0164
0165 void nfc_hci_resp_received(struct nfc_hci_dev *hdev, u8 result,
0166 struct sk_buff *skb)
0167 {
0168 mutex_lock(&hdev->msg_tx_mutex);
0169
0170 if (hdev->cmd_pending_msg == NULL) {
0171 kfree_skb(skb);
0172 goto exit;
0173 }
0174
0175 __nfc_hci_cmd_completion(hdev, nfc_hci_result_to_errno(result), skb);
0176
0177 exit:
0178 mutex_unlock(&hdev->msg_tx_mutex);
0179 }
0180
0181 void nfc_hci_cmd_received(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
0182 struct sk_buff *skb)
0183 {
0184 u8 status = NFC_HCI_ANY_OK;
0185 const struct hci_create_pipe_resp *create_info;
0186 const struct hci_delete_pipe_noti *delete_info;
0187 const struct hci_all_pipe_cleared_noti *cleared_info;
0188 u8 gate;
0189
0190 pr_debug("from pipe %x cmd %x\n", pipe, cmd);
0191
0192 if (pipe >= NFC_HCI_MAX_PIPES) {
0193 status = NFC_HCI_ANY_E_NOK;
0194 goto exit;
0195 }
0196
0197 gate = hdev->pipes[pipe].gate;
0198
0199 switch (cmd) {
0200 case NFC_HCI_ADM_NOTIFY_PIPE_CREATED:
0201 if (skb->len != 5) {
0202 status = NFC_HCI_ANY_E_NOK;
0203 goto exit;
0204 }
0205 create_info = (struct hci_create_pipe_resp *)skb->data;
0206
0207 if (create_info->pipe >= NFC_HCI_MAX_PIPES) {
0208 status = NFC_HCI_ANY_E_NOK;
0209 goto exit;
0210 }
0211
0212
0213
0214
0215
0216
0217 hdev->gate2pipe[create_info->dest_gate] = create_info->pipe;
0218 hdev->pipes[create_info->pipe].gate = create_info->dest_gate;
0219 hdev->pipes[create_info->pipe].dest_host =
0220 create_info->src_host;
0221 break;
0222 case NFC_HCI_ANY_OPEN_PIPE:
0223 if (gate == NFC_HCI_INVALID_GATE) {
0224 status = NFC_HCI_ANY_E_NOK;
0225 goto exit;
0226 }
0227 break;
0228 case NFC_HCI_ADM_NOTIFY_PIPE_DELETED:
0229 if (skb->len != 1) {
0230 status = NFC_HCI_ANY_E_NOK;
0231 goto exit;
0232 }
0233 delete_info = (struct hci_delete_pipe_noti *)skb->data;
0234
0235 if (delete_info->pipe >= NFC_HCI_MAX_PIPES) {
0236 status = NFC_HCI_ANY_E_NOK;
0237 goto exit;
0238 }
0239
0240 hdev->pipes[delete_info->pipe].gate = NFC_HCI_INVALID_GATE;
0241 hdev->pipes[delete_info->pipe].dest_host = NFC_HCI_INVALID_HOST;
0242 break;
0243 case NFC_HCI_ADM_NOTIFY_ALL_PIPE_CLEARED:
0244 if (skb->len != 1) {
0245 status = NFC_HCI_ANY_E_NOK;
0246 goto exit;
0247 }
0248 cleared_info = (struct hci_all_pipe_cleared_noti *)skb->data;
0249
0250 nfc_hci_reset_pipes_per_host(hdev, cleared_info->host);
0251 break;
0252 default:
0253 pr_info("Discarded unknown cmd %x to gate %x\n", cmd, gate);
0254 break;
0255 }
0256
0257 if (hdev->ops->cmd_received)
0258 hdev->ops->cmd_received(hdev, pipe, cmd, skb);
0259
0260 exit:
0261 nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_RESPONSE,
0262 status, NULL, 0, NULL, NULL, 0);
0263
0264 kfree_skb(skb);
0265 }
0266
0267 u32 nfc_hci_sak_to_protocol(u8 sak)
0268 {
0269 switch (NFC_HCI_TYPE_A_SEL_PROT(sak)) {
0270 case NFC_HCI_TYPE_A_SEL_PROT_MIFARE:
0271 return NFC_PROTO_MIFARE_MASK;
0272 case NFC_HCI_TYPE_A_SEL_PROT_ISO14443:
0273 return NFC_PROTO_ISO14443_MASK;
0274 case NFC_HCI_TYPE_A_SEL_PROT_DEP:
0275 return NFC_PROTO_NFC_DEP_MASK;
0276 case NFC_HCI_TYPE_A_SEL_PROT_ISO14443_DEP:
0277 return NFC_PROTO_ISO14443_MASK | NFC_PROTO_NFC_DEP_MASK;
0278 default:
0279 return 0xffffffff;
0280 }
0281 }
0282 EXPORT_SYMBOL(nfc_hci_sak_to_protocol);
0283
0284 int nfc_hci_target_discovered(struct nfc_hci_dev *hdev, u8 gate)
0285 {
0286 struct nfc_target *targets;
0287 struct sk_buff *atqa_skb = NULL;
0288 struct sk_buff *sak_skb = NULL;
0289 struct sk_buff *uid_skb = NULL;
0290 int r;
0291
0292 pr_debug("from gate %d\n", gate);
0293
0294 targets = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
0295 if (targets == NULL)
0296 return -ENOMEM;
0297
0298 switch (gate) {
0299 case NFC_HCI_RF_READER_A_GATE:
0300 r = nfc_hci_get_param(hdev, NFC_HCI_RF_READER_A_GATE,
0301 NFC_HCI_RF_READER_A_ATQA, &atqa_skb);
0302 if (r < 0)
0303 goto exit;
0304
0305 r = nfc_hci_get_param(hdev, NFC_HCI_RF_READER_A_GATE,
0306 NFC_HCI_RF_READER_A_SAK, &sak_skb);
0307 if (r < 0)
0308 goto exit;
0309
0310 if (atqa_skb->len != 2 || sak_skb->len != 1) {
0311 r = -EPROTO;
0312 goto exit;
0313 }
0314
0315 targets->supported_protocols =
0316 nfc_hci_sak_to_protocol(sak_skb->data[0]);
0317 if (targets->supported_protocols == 0xffffffff) {
0318 r = -EPROTO;
0319 goto exit;
0320 }
0321
0322 targets->sens_res = be16_to_cpu(*(__be16 *)atqa_skb->data);
0323 targets->sel_res = sak_skb->data[0];
0324
0325 r = nfc_hci_get_param(hdev, NFC_HCI_RF_READER_A_GATE,
0326 NFC_HCI_RF_READER_A_UID, &uid_skb);
0327 if (r < 0)
0328 goto exit;
0329
0330 if (uid_skb->len == 0 || uid_skb->len > NFC_NFCID1_MAXSIZE) {
0331 r = -EPROTO;
0332 goto exit;
0333 }
0334
0335 memcpy(targets->nfcid1, uid_skb->data, uid_skb->len);
0336 targets->nfcid1_len = uid_skb->len;
0337
0338 if (hdev->ops->complete_target_discovered) {
0339 r = hdev->ops->complete_target_discovered(hdev, gate,
0340 targets);
0341 if (r < 0)
0342 goto exit;
0343 }
0344 break;
0345 case NFC_HCI_RF_READER_B_GATE:
0346 targets->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
0347 break;
0348 default:
0349 if (hdev->ops->target_from_gate)
0350 r = hdev->ops->target_from_gate(hdev, gate, targets);
0351 else
0352 r = -EPROTO;
0353 if (r < 0)
0354 goto exit;
0355
0356 if (hdev->ops->complete_target_discovered) {
0357 r = hdev->ops->complete_target_discovered(hdev, gate,
0358 targets);
0359 if (r < 0)
0360 goto exit;
0361 }
0362 break;
0363 }
0364
0365
0366 if (targets->hci_reader_gate == 0x00)
0367 targets->hci_reader_gate = gate;
0368
0369 r = nfc_targets_found(hdev->ndev, targets, 1);
0370
0371 exit:
0372 kfree(targets);
0373 kfree_skb(atqa_skb);
0374 kfree_skb(sak_skb);
0375 kfree_skb(uid_skb);
0376
0377 return r;
0378 }
0379 EXPORT_SYMBOL(nfc_hci_target_discovered);
0380
0381 void nfc_hci_event_received(struct nfc_hci_dev *hdev, u8 pipe, u8 event,
0382 struct sk_buff *skb)
0383 {
0384 int r = 0;
0385 u8 gate;
0386
0387 if (pipe >= NFC_HCI_MAX_PIPES) {
0388 pr_err("Discarded event %x to invalid pipe %x\n", event, pipe);
0389 goto exit;
0390 }
0391
0392 gate = hdev->pipes[pipe].gate;
0393 if (gate == NFC_HCI_INVALID_GATE) {
0394 pr_err("Discarded event %x to unopened pipe %x\n", event, pipe);
0395 goto exit;
0396 }
0397
0398 if (hdev->ops->event_received) {
0399 r = hdev->ops->event_received(hdev, pipe, event, skb);
0400 if (r <= 0)
0401 goto exit_noskb;
0402 }
0403
0404 switch (event) {
0405 case NFC_HCI_EVT_TARGET_DISCOVERED:
0406 if (skb->len < 1) {
0407 r = -EPROTO;
0408 goto exit;
0409 }
0410
0411 if (skb->data[0] == 3) {
0412
0413
0414
0415
0416
0417
0418
0419 }
0420
0421 if (skb->data[0] != 0) {
0422 r = -EPROTO;
0423 goto exit;
0424 }
0425
0426 r = nfc_hci_target_discovered(hdev, gate);
0427 break;
0428 default:
0429 pr_info("Discarded unknown event %x to gate %x\n", event, gate);
0430 r = -EINVAL;
0431 break;
0432 }
0433
0434 exit:
0435 kfree_skb(skb);
0436
0437 exit_noskb:
0438 if (r)
0439 nfc_hci_driver_failure(hdev, r);
0440 }
0441
0442 static void nfc_hci_cmd_timeout(struct timer_list *t)
0443 {
0444 struct nfc_hci_dev *hdev = from_timer(hdev, t, cmd_timer);
0445
0446 schedule_work(&hdev->msg_tx_work);
0447 }
0448
0449 static int hci_dev_connect_gates(struct nfc_hci_dev *hdev, u8 gate_count,
0450 const struct nfc_hci_gate *gates)
0451 {
0452 int r;
0453 while (gate_count--) {
0454 r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
0455 gates->gate, gates->pipe);
0456 if (r < 0)
0457 return r;
0458 gates++;
0459 }
0460
0461 return 0;
0462 }
0463
0464 static int hci_dev_session_init(struct nfc_hci_dev *hdev)
0465 {
0466 struct sk_buff *skb = NULL;
0467 int r;
0468
0469 if (hdev->init_data.gates[0].gate != NFC_HCI_ADMIN_GATE)
0470 return -EPROTO;
0471
0472 r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
0473 hdev->init_data.gates[0].gate,
0474 hdev->init_data.gates[0].pipe);
0475 if (r < 0)
0476 goto exit;
0477
0478 r = nfc_hci_get_param(hdev, NFC_HCI_ADMIN_GATE,
0479 NFC_HCI_ADMIN_SESSION_IDENTITY, &skb);
0480 if (r < 0)
0481 goto disconnect_all;
0482
0483 if (skb->len && skb->len == strlen(hdev->init_data.session_id) &&
0484 (memcmp(hdev->init_data.session_id, skb->data,
0485 skb->len) == 0) && hdev->ops->load_session) {
0486
0487
0488 r = hdev->ops->load_session(hdev);
0489
0490 if (r < 0)
0491 goto disconnect_all;
0492 } else {
0493
0494 r = nfc_hci_disconnect_all_gates(hdev);
0495 if (r < 0)
0496 goto exit;
0497
0498 r = hci_dev_connect_gates(hdev, hdev->init_data.gate_count,
0499 hdev->init_data.gates);
0500 if (r < 0)
0501 goto disconnect_all;
0502
0503 r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
0504 NFC_HCI_ADMIN_SESSION_IDENTITY,
0505 hdev->init_data.session_id,
0506 strlen(hdev->init_data.session_id));
0507 }
0508 if (r == 0)
0509 goto exit;
0510
0511 disconnect_all:
0512 nfc_hci_disconnect_all_gates(hdev);
0513
0514 exit:
0515 kfree_skb(skb);
0516
0517 return r;
0518 }
0519
0520 static int hci_dev_version(struct nfc_hci_dev *hdev)
0521 {
0522 int r;
0523 struct sk_buff *skb;
0524
0525 r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
0526 NFC_HCI_ID_MGMT_VERSION_SW, &skb);
0527 if (r == -EOPNOTSUPP) {
0528 pr_info("Software/Hardware info not available\n");
0529 return 0;
0530 }
0531 if (r < 0)
0532 return r;
0533
0534 if (skb->len != 3) {
0535 kfree_skb(skb);
0536 return -EINVAL;
0537 }
0538
0539 hdev->sw_romlib = (skb->data[0] & 0xf0) >> 4;
0540 hdev->sw_patch = skb->data[0] & 0x0f;
0541 hdev->sw_flashlib_major = skb->data[1];
0542 hdev->sw_flashlib_minor = skb->data[2];
0543
0544 kfree_skb(skb);
0545
0546 r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
0547 NFC_HCI_ID_MGMT_VERSION_HW, &skb);
0548 if (r < 0)
0549 return r;
0550
0551 if (skb->len != 3) {
0552 kfree_skb(skb);
0553 return -EINVAL;
0554 }
0555
0556 hdev->hw_derivative = (skb->data[0] & 0xe0) >> 5;
0557 hdev->hw_version = skb->data[0] & 0x1f;
0558 hdev->hw_mpw = (skb->data[1] & 0xc0) >> 6;
0559 hdev->hw_software = skb->data[1] & 0x3f;
0560 hdev->hw_bsid = skb->data[2];
0561
0562 kfree_skb(skb);
0563
0564 pr_info("SOFTWARE INFO:\n");
0565 pr_info("RomLib : %d\n", hdev->sw_romlib);
0566 pr_info("Patch : %d\n", hdev->sw_patch);
0567 pr_info("FlashLib Major : %d\n", hdev->sw_flashlib_major);
0568 pr_info("FlashLib Minor : %d\n", hdev->sw_flashlib_minor);
0569 pr_info("HARDWARE INFO:\n");
0570 pr_info("Derivative : %d\n", hdev->hw_derivative);
0571 pr_info("HW Version : %d\n", hdev->hw_version);
0572 pr_info("#MPW : %d\n", hdev->hw_mpw);
0573 pr_info("Software : %d\n", hdev->hw_software);
0574 pr_info("BSID Version : %d\n", hdev->hw_bsid);
0575
0576 return 0;
0577 }
0578
0579 static int hci_dev_up(struct nfc_dev *nfc_dev)
0580 {
0581 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
0582 int r = 0;
0583
0584 if (hdev->ops->open) {
0585 r = hdev->ops->open(hdev);
0586 if (r < 0)
0587 return r;
0588 }
0589
0590 r = nfc_llc_start(hdev->llc);
0591 if (r < 0)
0592 goto exit_close;
0593
0594 r = hci_dev_session_init(hdev);
0595 if (r < 0)
0596 goto exit_llc;
0597
0598 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
0599 NFC_HCI_EVT_END_OPERATION, NULL, 0);
0600 if (r < 0)
0601 goto exit_llc;
0602
0603 if (hdev->ops->hci_ready) {
0604 r = hdev->ops->hci_ready(hdev);
0605 if (r < 0)
0606 goto exit_llc;
0607 }
0608
0609 r = hci_dev_version(hdev);
0610 if (r < 0)
0611 goto exit_llc;
0612
0613 return 0;
0614
0615 exit_llc:
0616 nfc_llc_stop(hdev->llc);
0617
0618 exit_close:
0619 if (hdev->ops->close)
0620 hdev->ops->close(hdev);
0621
0622 return r;
0623 }
0624
0625 static int hci_dev_down(struct nfc_dev *nfc_dev)
0626 {
0627 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
0628
0629 nfc_llc_stop(hdev->llc);
0630
0631 if (hdev->ops->close)
0632 hdev->ops->close(hdev);
0633
0634 nfc_hci_reset_pipes(hdev);
0635
0636 return 0;
0637 }
0638
0639 static int hci_start_poll(struct nfc_dev *nfc_dev,
0640 u32 im_protocols, u32 tm_protocols)
0641 {
0642 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
0643
0644 if (hdev->ops->start_poll)
0645 return hdev->ops->start_poll(hdev, im_protocols, tm_protocols);
0646 else
0647 return nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
0648 NFC_HCI_EVT_READER_REQUESTED,
0649 NULL, 0);
0650 }
0651
0652 static void hci_stop_poll(struct nfc_dev *nfc_dev)
0653 {
0654 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
0655
0656 if (hdev->ops->stop_poll)
0657 hdev->ops->stop_poll(hdev);
0658 else
0659 nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
0660 NFC_HCI_EVT_END_OPERATION, NULL, 0);
0661 }
0662
0663 static int hci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
0664 __u8 comm_mode, __u8 *gb, size_t gb_len)
0665 {
0666 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
0667
0668 if (!hdev->ops->dep_link_up)
0669 return 0;
0670
0671 return hdev->ops->dep_link_up(hdev, target, comm_mode,
0672 gb, gb_len);
0673 }
0674
0675 static int hci_dep_link_down(struct nfc_dev *nfc_dev)
0676 {
0677 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
0678
0679 if (!hdev->ops->dep_link_down)
0680 return 0;
0681
0682 return hdev->ops->dep_link_down(hdev);
0683 }
0684
0685 static int hci_activate_target(struct nfc_dev *nfc_dev,
0686 struct nfc_target *target, u32 protocol)
0687 {
0688 return 0;
0689 }
0690
0691 static void hci_deactivate_target(struct nfc_dev *nfc_dev,
0692 struct nfc_target *target,
0693 u8 mode)
0694 {
0695 }
0696
0697 #define HCI_CB_TYPE_TRANSCEIVE 1
0698
0699 static void hci_transceive_cb(void *context, struct sk_buff *skb, int err)
0700 {
0701 struct nfc_hci_dev *hdev = context;
0702
0703 switch (hdev->async_cb_type) {
0704 case HCI_CB_TYPE_TRANSCEIVE:
0705
0706
0707
0708
0709
0710 if (err == 0)
0711 skb_trim(skb, skb->len - 1);
0712
0713 hdev->async_cb(hdev->async_cb_context, skb, err);
0714 break;
0715 default:
0716 if (err == 0)
0717 kfree_skb(skb);
0718 break;
0719 }
0720 }
0721
0722 static int hci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
0723 struct sk_buff *skb, data_exchange_cb_t cb,
0724 void *cb_context)
0725 {
0726 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
0727 int r;
0728
0729 pr_debug("target_idx=%d\n", target->idx);
0730
0731 switch (target->hci_reader_gate) {
0732 case NFC_HCI_RF_READER_A_GATE:
0733 case NFC_HCI_RF_READER_B_GATE:
0734 if (hdev->ops->im_transceive) {
0735 r = hdev->ops->im_transceive(hdev, target, skb, cb,
0736 cb_context);
0737 if (r <= 0)
0738 break;
0739 }
0740
0741 *(u8 *)skb_push(skb, 1) = 0;
0742
0743 hdev->async_cb_type = HCI_CB_TYPE_TRANSCEIVE;
0744 hdev->async_cb = cb;
0745 hdev->async_cb_context = cb_context;
0746
0747 r = nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
0748 NFC_HCI_WR_XCHG_DATA, skb->data,
0749 skb->len, hci_transceive_cb, hdev);
0750 break;
0751 default:
0752 if (hdev->ops->im_transceive) {
0753 r = hdev->ops->im_transceive(hdev, target, skb, cb,
0754 cb_context);
0755 if (r == 1)
0756 r = -ENOTSUPP;
0757 } else {
0758 r = -ENOTSUPP;
0759 }
0760 break;
0761 }
0762
0763 kfree_skb(skb);
0764
0765 return r;
0766 }
0767
0768 static int hci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
0769 {
0770 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
0771
0772 if (!hdev->ops->tm_send) {
0773 kfree_skb(skb);
0774 return -ENOTSUPP;
0775 }
0776
0777 return hdev->ops->tm_send(hdev, skb);
0778 }
0779
0780 static int hci_check_presence(struct nfc_dev *nfc_dev,
0781 struct nfc_target *target)
0782 {
0783 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
0784
0785 if (!hdev->ops->check_presence)
0786 return 0;
0787
0788 return hdev->ops->check_presence(hdev, target);
0789 }
0790
0791 static int hci_discover_se(struct nfc_dev *nfc_dev)
0792 {
0793 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
0794
0795 if (hdev->ops->discover_se)
0796 return hdev->ops->discover_se(hdev);
0797
0798 return 0;
0799 }
0800
0801 static int hci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
0802 {
0803 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
0804
0805 if (hdev->ops->enable_se)
0806 return hdev->ops->enable_se(hdev, se_idx);
0807
0808 return 0;
0809 }
0810
0811 static int hci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
0812 {
0813 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
0814
0815 if (hdev->ops->disable_se)
0816 return hdev->ops->disable_se(hdev, se_idx);
0817
0818 return 0;
0819 }
0820
0821 static int hci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
0822 u8 *apdu, size_t apdu_length,
0823 se_io_cb_t cb, void *cb_context)
0824 {
0825 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
0826
0827 if (hdev->ops->se_io)
0828 return hdev->ops->se_io(hdev, se_idx, apdu,
0829 apdu_length, cb, cb_context);
0830
0831 return 0;
0832 }
0833
0834 static void nfc_hci_failure(struct nfc_hci_dev *hdev, int err)
0835 {
0836 mutex_lock(&hdev->msg_tx_mutex);
0837
0838 if (hdev->cmd_pending_msg == NULL) {
0839 nfc_driver_failure(hdev->ndev, err);
0840 goto exit;
0841 }
0842
0843 __nfc_hci_cmd_completion(hdev, err, NULL);
0844
0845 exit:
0846 mutex_unlock(&hdev->msg_tx_mutex);
0847 }
0848
0849 static void nfc_hci_llc_failure(struct nfc_hci_dev *hdev, int err)
0850 {
0851 nfc_hci_failure(hdev, err);
0852 }
0853
0854 static void nfc_hci_recv_from_llc(struct nfc_hci_dev *hdev, struct sk_buff *skb)
0855 {
0856 struct hcp_packet *packet;
0857 u8 type;
0858 u8 instruction;
0859 struct sk_buff *hcp_skb;
0860 u8 pipe;
0861 struct sk_buff *frag_skb;
0862 int msg_len;
0863
0864 packet = (struct hcp_packet *)skb->data;
0865 if ((packet->header & ~NFC_HCI_FRAGMENT) == 0) {
0866 skb_queue_tail(&hdev->rx_hcp_frags, skb);
0867 return;
0868 }
0869
0870
0871 if (skb_queue_len(&hdev->rx_hcp_frags)) {
0872 pipe = packet->header & NFC_HCI_FRAGMENT;
0873 skb_queue_tail(&hdev->rx_hcp_frags, skb);
0874
0875 msg_len = 0;
0876 skb_queue_walk(&hdev->rx_hcp_frags, frag_skb) {
0877 msg_len += (frag_skb->len -
0878 NFC_HCI_HCP_PACKET_HEADER_LEN);
0879 }
0880
0881 hcp_skb = nfc_alloc_recv_skb(NFC_HCI_HCP_PACKET_HEADER_LEN +
0882 msg_len, GFP_KERNEL);
0883 if (hcp_skb == NULL) {
0884 nfc_hci_failure(hdev, -ENOMEM);
0885 return;
0886 }
0887
0888 skb_put_u8(hcp_skb, pipe);
0889
0890 skb_queue_walk(&hdev->rx_hcp_frags, frag_skb) {
0891 msg_len = frag_skb->len - NFC_HCI_HCP_PACKET_HEADER_LEN;
0892 skb_put_data(hcp_skb,
0893 frag_skb->data + NFC_HCI_HCP_PACKET_HEADER_LEN,
0894 msg_len);
0895 }
0896
0897 skb_queue_purge(&hdev->rx_hcp_frags);
0898 } else {
0899 packet->header &= NFC_HCI_FRAGMENT;
0900 hcp_skb = skb;
0901 }
0902
0903
0904
0905
0906
0907 packet = (struct hcp_packet *)hcp_skb->data;
0908 type = HCP_MSG_GET_TYPE(packet->message.header);
0909 if (type == NFC_HCI_HCP_RESPONSE) {
0910 pipe = packet->header;
0911 instruction = HCP_MSG_GET_CMD(packet->message.header);
0912 skb_pull(hcp_skb, NFC_HCI_HCP_PACKET_HEADER_LEN +
0913 NFC_HCI_HCP_MESSAGE_HEADER_LEN);
0914 nfc_hci_hcp_message_rx(hdev, pipe, type, instruction, hcp_skb);
0915 } else {
0916 skb_queue_tail(&hdev->msg_rx_queue, hcp_skb);
0917 schedule_work(&hdev->msg_rx_work);
0918 }
0919 }
0920
0921 static int hci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
0922 {
0923 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
0924
0925 if (!hdev->ops->fw_download)
0926 return -ENOTSUPP;
0927
0928 return hdev->ops->fw_download(hdev, firmware_name);
0929 }
0930
0931 static const struct nfc_ops hci_nfc_ops = {
0932 .dev_up = hci_dev_up,
0933 .dev_down = hci_dev_down,
0934 .start_poll = hci_start_poll,
0935 .stop_poll = hci_stop_poll,
0936 .dep_link_up = hci_dep_link_up,
0937 .dep_link_down = hci_dep_link_down,
0938 .activate_target = hci_activate_target,
0939 .deactivate_target = hci_deactivate_target,
0940 .im_transceive = hci_transceive,
0941 .tm_send = hci_tm_send,
0942 .check_presence = hci_check_presence,
0943 .fw_download = hci_fw_download,
0944 .discover_se = hci_discover_se,
0945 .enable_se = hci_enable_se,
0946 .disable_se = hci_disable_se,
0947 .se_io = hci_se_io,
0948 };
0949
0950 struct nfc_hci_dev *nfc_hci_allocate_device(const struct nfc_hci_ops *ops,
0951 struct nfc_hci_init_data *init_data,
0952 unsigned long quirks,
0953 u32 protocols,
0954 const char *llc_name,
0955 int tx_headroom,
0956 int tx_tailroom,
0957 int max_link_payload)
0958 {
0959 struct nfc_hci_dev *hdev;
0960
0961 if (ops->xmit == NULL)
0962 return NULL;
0963
0964 if (protocols == 0)
0965 return NULL;
0966
0967 hdev = kzalloc(sizeof(struct nfc_hci_dev), GFP_KERNEL);
0968 if (hdev == NULL)
0969 return NULL;
0970
0971 hdev->llc = nfc_llc_allocate(llc_name, hdev, ops->xmit,
0972 nfc_hci_recv_from_llc, tx_headroom,
0973 tx_tailroom, nfc_hci_llc_failure);
0974 if (hdev->llc == NULL) {
0975 kfree(hdev);
0976 return NULL;
0977 }
0978
0979 hdev->ndev = nfc_allocate_device(&hci_nfc_ops, protocols,
0980 tx_headroom + HCI_CMDS_HEADROOM,
0981 tx_tailroom);
0982 if (!hdev->ndev) {
0983 nfc_llc_free(hdev->llc);
0984 kfree(hdev);
0985 return NULL;
0986 }
0987
0988 hdev->ops = ops;
0989 hdev->max_data_link_payload = max_link_payload;
0990 hdev->init_data = *init_data;
0991
0992 nfc_set_drvdata(hdev->ndev, hdev);
0993
0994 nfc_hci_reset_pipes(hdev);
0995
0996 hdev->quirks = quirks;
0997
0998 return hdev;
0999 }
1000 EXPORT_SYMBOL(nfc_hci_allocate_device);
1001
1002 void nfc_hci_free_device(struct nfc_hci_dev *hdev)
1003 {
1004 nfc_free_device(hdev->ndev);
1005 nfc_llc_free(hdev->llc);
1006 kfree(hdev);
1007 }
1008 EXPORT_SYMBOL(nfc_hci_free_device);
1009
1010 int nfc_hci_register_device(struct nfc_hci_dev *hdev)
1011 {
1012 mutex_init(&hdev->msg_tx_mutex);
1013
1014 INIT_LIST_HEAD(&hdev->msg_tx_queue);
1015
1016 INIT_WORK(&hdev->msg_tx_work, nfc_hci_msg_tx_work);
1017
1018 timer_setup(&hdev->cmd_timer, nfc_hci_cmd_timeout, 0);
1019
1020 skb_queue_head_init(&hdev->rx_hcp_frags);
1021
1022 INIT_WORK(&hdev->msg_rx_work, nfc_hci_msg_rx_work);
1023
1024 skb_queue_head_init(&hdev->msg_rx_queue);
1025
1026 return nfc_register_device(hdev->ndev);
1027 }
1028 EXPORT_SYMBOL(nfc_hci_register_device);
1029
1030 void nfc_hci_unregister_device(struct nfc_hci_dev *hdev)
1031 {
1032 struct hci_msg *msg, *n;
1033
1034 mutex_lock(&hdev->msg_tx_mutex);
1035
1036 if (hdev->cmd_pending_msg) {
1037 if (hdev->cmd_pending_msg->cb)
1038 hdev->cmd_pending_msg->cb(
1039 hdev->cmd_pending_msg->cb_context,
1040 NULL, -ESHUTDOWN);
1041 kfree(hdev->cmd_pending_msg);
1042 hdev->cmd_pending_msg = NULL;
1043 }
1044
1045 hdev->shutting_down = true;
1046
1047 mutex_unlock(&hdev->msg_tx_mutex);
1048
1049 del_timer_sync(&hdev->cmd_timer);
1050 cancel_work_sync(&hdev->msg_tx_work);
1051
1052 cancel_work_sync(&hdev->msg_rx_work);
1053
1054 nfc_unregister_device(hdev->ndev);
1055
1056 skb_queue_purge(&hdev->rx_hcp_frags);
1057 skb_queue_purge(&hdev->msg_rx_queue);
1058
1059 list_for_each_entry_safe(msg, n, &hdev->msg_tx_queue, msg_l) {
1060 list_del(&msg->msg_l);
1061 skb_queue_purge(&msg->msg_frags);
1062 kfree(msg);
1063 }
1064 }
1065 EXPORT_SYMBOL(nfc_hci_unregister_device);
1066
1067 void nfc_hci_set_clientdata(struct nfc_hci_dev *hdev, void *clientdata)
1068 {
1069 hdev->clientdata = clientdata;
1070 }
1071 EXPORT_SYMBOL(nfc_hci_set_clientdata);
1072
1073 void *nfc_hci_get_clientdata(struct nfc_hci_dev *hdev)
1074 {
1075 return hdev->clientdata;
1076 }
1077 EXPORT_SYMBOL(nfc_hci_get_clientdata);
1078
1079 void nfc_hci_driver_failure(struct nfc_hci_dev *hdev, int err)
1080 {
1081 nfc_hci_failure(hdev, err);
1082 }
1083 EXPORT_SYMBOL(nfc_hci_driver_failure);
1084
1085 void nfc_hci_recv_frame(struct nfc_hci_dev *hdev, struct sk_buff *skb)
1086 {
1087 nfc_llc_rcv_from_drv(hdev->llc, skb);
1088 }
1089 EXPORT_SYMBOL(nfc_hci_recv_frame);
1090
1091 static int __init nfc_hci_init(void)
1092 {
1093 return nfc_llc_init();
1094 }
1095
1096 static void __exit nfc_hci_exit(void)
1097 {
1098 nfc_llc_exit();
1099 }
1100
1101 subsys_initcall(nfc_hci_init);
1102 module_exit(nfc_hci_exit);
1103
1104 MODULE_LICENSE("GPL");
1105 MODULE_DESCRIPTION("NFC HCI Core");