Lines Matching +full:de +full:- +full:activated

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2011 Instituto Nokia de Tecnologia
37 pr_debug("%s do firmware %s\n", dev_name(&dev->dev), firmware_name); in nfc_fw_download()
39 device_lock(&dev->dev); in nfc_fw_download()
41 if (dev->shutting_down) { in nfc_fw_download()
42 rc = -ENODEV; in nfc_fw_download()
46 if (dev->dev_up) { in nfc_fw_download()
47 rc = -EBUSY; in nfc_fw_download()
51 if (!dev->ops->fw_download) { in nfc_fw_download()
52 rc = -EOPNOTSUPP; in nfc_fw_download()
56 dev->fw_download_in_progress = true; in nfc_fw_download()
57 rc = dev->ops->fw_download(dev, firmware_name); in nfc_fw_download()
59 dev->fw_download_in_progress = false; in nfc_fw_download()
62 device_unlock(&dev->dev); in nfc_fw_download()
67 * nfc_fw_download_done - inform that a firmware download was completed
76 dev->fw_download_in_progress = false; in nfc_fw_download_done()
83 * nfc_dev_up - turn on the NFC device
93 pr_debug("dev_name=%s\n", dev_name(&dev->dev)); in nfc_dev_up()
95 device_lock(&dev->dev); in nfc_dev_up()
97 if (dev->shutting_down) { in nfc_dev_up()
98 rc = -ENODEV; in nfc_dev_up()
102 if (dev->rfkill && rfkill_blocked(dev->rfkill)) { in nfc_dev_up()
103 rc = -ERFKILL; in nfc_dev_up()
107 if (dev->fw_download_in_progress) { in nfc_dev_up()
108 rc = -EBUSY; in nfc_dev_up()
112 if (dev->dev_up) { in nfc_dev_up()
113 rc = -EALREADY; in nfc_dev_up()
117 if (dev->ops->dev_up) in nfc_dev_up()
118 rc = dev->ops->dev_up(dev); in nfc_dev_up()
121 dev->dev_up = true; in nfc_dev_up()
124 if (dev->ops->discover_se && dev->ops->discover_se(dev)) in nfc_dev_up()
128 device_unlock(&dev->dev); in nfc_dev_up()
133 * nfc_dev_down - turn off the NFC device
141 pr_debug("dev_name=%s\n", dev_name(&dev->dev)); in nfc_dev_down()
143 device_lock(&dev->dev); in nfc_dev_down()
145 if (dev->shutting_down) { in nfc_dev_down()
146 rc = -ENODEV; in nfc_dev_down()
150 if (!dev->dev_up) { in nfc_dev_down()
151 rc = -EALREADY; in nfc_dev_down()
155 if (dev->polling || dev->active_target) { in nfc_dev_down()
156 rc = -EBUSY; in nfc_dev_down()
160 if (dev->ops->dev_down) in nfc_dev_down()
161 dev->ops->dev_down(dev); in nfc_dev_down()
163 dev->dev_up = false; in nfc_dev_down()
166 device_unlock(&dev->dev); in nfc_dev_down()
174 pr_debug("%s blocked %d", dev_name(&dev->dev), blocked); in nfc_rfkill_set_block()
189 * nfc_start_poll - start polling for nfc targets
203 dev_name(&dev->dev), im_protocols, tm_protocols); in nfc_start_poll()
206 return -EINVAL; in nfc_start_poll()
208 device_lock(&dev->dev); in nfc_start_poll()
210 if (dev->shutting_down) { in nfc_start_poll()
211 rc = -ENODEV; in nfc_start_poll()
215 if (!dev->dev_up) { in nfc_start_poll()
216 rc = -ENODEV; in nfc_start_poll()
220 if (dev->polling) { in nfc_start_poll()
221 rc = -EBUSY; in nfc_start_poll()
225 rc = dev->ops->start_poll(dev, im_protocols, tm_protocols); in nfc_start_poll()
227 dev->polling = true; in nfc_start_poll()
228 dev->rf_mode = NFC_RF_NONE; in nfc_start_poll()
232 device_unlock(&dev->dev); in nfc_start_poll()
237 * nfc_stop_poll - stop polling for nfc targets
245 pr_debug("dev_name=%s\n", dev_name(&dev->dev)); in nfc_stop_poll()
247 device_lock(&dev->dev); in nfc_stop_poll()
249 if (dev->shutting_down) { in nfc_stop_poll()
250 rc = -ENODEV; in nfc_stop_poll()
254 if (!dev->polling) { in nfc_stop_poll()
255 rc = -EINVAL; in nfc_stop_poll()
259 dev->ops->stop_poll(dev); in nfc_stop_poll()
260 dev->polling = false; in nfc_stop_poll()
261 dev->rf_mode = NFC_RF_NONE; in nfc_stop_poll()
264 device_unlock(&dev->dev); in nfc_stop_poll()
272 for (i = 0; i < dev->n_targets; i++) { in nfc_find_target()
273 if (dev->targets[i].idx == target_idx) in nfc_find_target()
274 return &dev->targets[i]; in nfc_find_target()
287 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode); in nfc_dep_link_up()
289 if (!dev->ops->dep_link_up) in nfc_dep_link_up()
290 return -EOPNOTSUPP; in nfc_dep_link_up()
292 device_lock(&dev->dev); in nfc_dep_link_up()
294 if (dev->shutting_down) { in nfc_dep_link_up()
295 rc = -ENODEV; in nfc_dep_link_up()
299 if (dev->dep_link_up == true) { in nfc_dep_link_up()
300 rc = -EALREADY; in nfc_dep_link_up()
306 rc = -EINVAL; in nfc_dep_link_up()
312 rc = -ENOTCONN; in nfc_dep_link_up()
316 rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len); in nfc_dep_link_up()
318 dev->active_target = target; in nfc_dep_link_up()
319 dev->rf_mode = NFC_RF_INITIATOR; in nfc_dep_link_up()
323 device_unlock(&dev->dev); in nfc_dep_link_up()
331 pr_debug("dev_name=%s\n", dev_name(&dev->dev)); in nfc_dep_link_down()
333 if (!dev->ops->dep_link_down) in nfc_dep_link_down()
334 return -EOPNOTSUPP; in nfc_dep_link_down()
336 device_lock(&dev->dev); in nfc_dep_link_down()
338 if (dev->shutting_down) { in nfc_dep_link_down()
339 rc = -ENODEV; in nfc_dep_link_down()
343 if (dev->dep_link_up == false) { in nfc_dep_link_down()
344 rc = -EALREADY; in nfc_dep_link_down()
348 rc = dev->ops->dep_link_down(dev); in nfc_dep_link_down()
350 dev->dep_link_up = false; in nfc_dep_link_down()
351 dev->active_target = NULL; in nfc_dep_link_down()
352 dev->rf_mode = NFC_RF_NONE; in nfc_dep_link_down()
358 device_unlock(&dev->dev); in nfc_dep_link_down()
366 dev->dep_link_up = true; in nfc_dep_link_is_up()
368 if (!dev->active_target && rf_mode == NFC_RF_INITIATOR) { in nfc_dep_link_is_up()
373 return -ENOTCONN; in nfc_dep_link_is_up()
375 dev->active_target = target; in nfc_dep_link_is_up()
378 dev->polling = false; in nfc_dep_link_is_up()
379 dev->rf_mode = rf_mode; in nfc_dep_link_is_up()
388 * nfc_activate_target - prepare the target for data exchange
391 * @target_idx: index of the target that must be activated
400 dev_name(&dev->dev), target_idx, protocol); in nfc_activate_target()
402 device_lock(&dev->dev); in nfc_activate_target()
404 if (dev->shutting_down) { in nfc_activate_target()
405 rc = -ENODEV; in nfc_activate_target()
409 if (dev->active_target) { in nfc_activate_target()
410 rc = -EBUSY; in nfc_activate_target()
416 rc = -ENOTCONN; in nfc_activate_target()
420 rc = dev->ops->activate_target(dev, target, protocol); in nfc_activate_target()
422 dev->active_target = target; in nfc_activate_target()
423 dev->rf_mode = NFC_RF_INITIATOR; in nfc_activate_target()
425 if (dev->ops->check_presence && !dev->shutting_down) in nfc_activate_target()
426 mod_timer(&dev->check_pres_timer, jiffies + in nfc_activate_target()
431 device_unlock(&dev->dev); in nfc_activate_target()
436 * nfc_deactivate_target - deactivate a nfc target
447 dev_name(&dev->dev), target_idx); in nfc_deactivate_target()
449 device_lock(&dev->dev); in nfc_deactivate_target()
451 if (dev->shutting_down) { in nfc_deactivate_target()
452 rc = -ENODEV; in nfc_deactivate_target()
456 if (dev->active_target == NULL) { in nfc_deactivate_target()
457 rc = -ENOTCONN; in nfc_deactivate_target()
461 if (dev->active_target->idx != target_idx) { in nfc_deactivate_target()
462 rc = -ENOTCONN; in nfc_deactivate_target()
466 if (dev->ops->check_presence) in nfc_deactivate_target()
467 del_timer_sync(&dev->check_pres_timer); in nfc_deactivate_target()
469 dev->ops->deactivate_target(dev, dev->active_target, mode); in nfc_deactivate_target()
470 dev->active_target = NULL; in nfc_deactivate_target()
473 device_unlock(&dev->dev); in nfc_deactivate_target()
478 * nfc_data_exchange - transceive data
493 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n", in nfc_data_exchange()
494 dev_name(&dev->dev), target_idx, skb->len); in nfc_data_exchange()
496 device_lock(&dev->dev); in nfc_data_exchange()
498 if (dev->shutting_down) { in nfc_data_exchange()
499 rc = -ENODEV; in nfc_data_exchange()
504 if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) { in nfc_data_exchange()
505 if (dev->active_target->idx != target_idx) { in nfc_data_exchange()
506 rc = -EADDRNOTAVAIL; in nfc_data_exchange()
511 if (dev->ops->check_presence) in nfc_data_exchange()
512 del_timer_sync(&dev->check_pres_timer); in nfc_data_exchange()
514 rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb, in nfc_data_exchange()
517 if (!rc && dev->ops->check_presence && !dev->shutting_down) in nfc_data_exchange()
518 mod_timer(&dev->check_pres_timer, jiffies + in nfc_data_exchange()
520 } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) { in nfc_data_exchange()
521 rc = dev->ops->tm_send(dev, skb); in nfc_data_exchange()
523 rc = -ENOTCONN; in nfc_data_exchange()
530 device_unlock(&dev->dev); in nfc_data_exchange()
538 list_for_each_entry(se, &dev->secure_elements, list) in nfc_find_se()
539 if (se->idx == se_idx) in nfc_find_se()
551 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx); in nfc_enable_se()
553 device_lock(&dev->dev); in nfc_enable_se()
555 if (dev->shutting_down) { in nfc_enable_se()
556 rc = -ENODEV; in nfc_enable_se()
560 if (!dev->dev_up) { in nfc_enable_se()
561 rc = -ENODEV; in nfc_enable_se()
565 if (dev->polling) { in nfc_enable_se()
566 rc = -EBUSY; in nfc_enable_se()
570 if (!dev->ops->enable_se || !dev->ops->disable_se) { in nfc_enable_se()
571 rc = -EOPNOTSUPP; in nfc_enable_se()
577 rc = -EINVAL; in nfc_enable_se()
581 if (se->state == NFC_SE_ENABLED) { in nfc_enable_se()
582 rc = -EALREADY; in nfc_enable_se()
586 rc = dev->ops->enable_se(dev, se_idx); in nfc_enable_se()
588 se->state = NFC_SE_ENABLED; in nfc_enable_se()
591 device_unlock(&dev->dev); in nfc_enable_se()
600 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx); in nfc_disable_se()
602 device_lock(&dev->dev); in nfc_disable_se()
604 if (dev->shutting_down) { in nfc_disable_se()
605 rc = -ENODEV; in nfc_disable_se()
609 if (!dev->dev_up) { in nfc_disable_se()
610 rc = -ENODEV; in nfc_disable_se()
614 if (!dev->ops->enable_se || !dev->ops->disable_se) { in nfc_disable_se()
615 rc = -EOPNOTSUPP; in nfc_disable_se()
621 rc = -EINVAL; in nfc_disable_se()
625 if (se->state == NFC_SE_DISABLED) { in nfc_disable_se()
626 rc = -EALREADY; in nfc_disable_se()
630 rc = dev->ops->disable_se(dev, se_idx); in nfc_disable_se()
632 se->state = NFC_SE_DISABLED; in nfc_disable_se()
635 device_unlock(&dev->dev); in nfc_disable_se()
641 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len); in nfc_set_remote_general_bytes()
649 pr_debug("dev_name=%s\n", dev_name(&dev->dev)); in nfc_get_local_general_bytes()
658 if (dev->dep_link_up == false) { in nfc_tm_data_received()
660 return -ENOLINK; in nfc_tm_data_received()
672 device_lock(&dev->dev); in nfc_tm_activated()
674 dev->polling = false; in nfc_tm_activated()
682 dev->rf_mode = NFC_RF_TARGET; in nfc_tm_activated()
690 device_unlock(&dev->dev); in nfc_tm_activated()
698 dev->dep_link_up = false; in nfc_tm_deactivated()
699 dev->rf_mode = NFC_RF_NONE; in nfc_tm_deactivated()
706 * nfc_alloc_send_skb - allocate a skb for data exchange responses
722 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; in nfc_alloc_send_skb()
726 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); in nfc_alloc_send_skb()
732 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
753 * nfc_targets_found - inform that targets were found
773 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets); in nfc_targets_found()
776 targets[i].idx = dev->target_next_idx++; in nfc_targets_found()
778 device_lock(&dev->dev); in nfc_targets_found()
780 if (dev->polling == false) { in nfc_targets_found()
781 device_unlock(&dev->dev); in nfc_targets_found()
785 dev->polling = false; in nfc_targets_found()
787 dev->targets_generation++; in nfc_targets_found()
789 kfree(dev->targets); in nfc_targets_found()
790 dev->targets = NULL; in nfc_targets_found()
793 dev->targets = kmemdup(targets, in nfc_targets_found()
797 if (!dev->targets) { in nfc_targets_found()
798 dev->n_targets = 0; in nfc_targets_found()
799 device_unlock(&dev->dev); in nfc_targets_found()
800 return -ENOMEM; in nfc_targets_found()
804 dev->n_targets = n_targets; in nfc_targets_found()
805 device_unlock(&dev->dev); in nfc_targets_found()
814 * nfc_target_lost - inform that an activated target went out of field
816 * @dev: The nfc device that had the activated target in field
819 * The device driver must call this function when the activated target
830 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx); in nfc_target_lost()
832 device_lock(&dev->dev); in nfc_target_lost()
834 for (i = 0; i < dev->n_targets; i++) { in nfc_target_lost()
835 tg = &dev->targets[i]; in nfc_target_lost()
836 if (tg->idx == target_idx) in nfc_target_lost()
840 if (i == dev->n_targets) { in nfc_target_lost()
841 device_unlock(&dev->dev); in nfc_target_lost()
842 return -EINVAL; in nfc_target_lost()
845 dev->targets_generation++; in nfc_target_lost()
846 dev->n_targets--; in nfc_target_lost()
847 dev->active_target = NULL; in nfc_target_lost()
849 if (dev->n_targets) { in nfc_target_lost()
850 memcpy(&dev->targets[i], &dev->targets[i + 1], in nfc_target_lost()
851 (dev->n_targets - i) * sizeof(struct nfc_target)); in nfc_target_lost()
853 kfree(dev->targets); in nfc_target_lost()
854 dev->targets = NULL; in nfc_target_lost()
857 device_unlock(&dev->dev); in nfc_target_lost()
876 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx); in nfc_add_se()
880 return -EALREADY; in nfc_add_se()
884 return -ENOMEM; in nfc_add_se()
886 se->idx = se_idx; in nfc_add_se()
887 se->type = type; in nfc_add_se()
888 se->state = NFC_SE_DISABLED; in nfc_add_se()
889 INIT_LIST_HEAD(&se->list); in nfc_add_se()
891 list_add(&se->list, &dev->secure_elements); in nfc_add_se()
895 list_del(&se->list); in nfc_add_se()
910 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx); in nfc_remove_se()
912 list_for_each_entry_safe(se, n, &dev->secure_elements, list) in nfc_remove_se()
913 if (se->idx == se_idx) { in nfc_remove_se()
918 list_del(&se->list); in nfc_remove_se()
924 return -EINVAL; in nfc_remove_se()
935 device_lock(&dev->dev); in nfc_se_transaction()
938 rc = -EPROTO; in nfc_se_transaction()
944 device_unlock(&dev->dev); in nfc_se_transaction()
955 device_lock(&dev->dev); in nfc_se_connectivity()
957 device_unlock(&dev->dev); in nfc_se_connectivity()
967 pr_debug("dev_name=%s\n", dev_name(&dev->dev)); in nfc_release()
969 nfc_genl_data_exit(&dev->genl_data); in nfc_release()
970 kfree(dev->targets); in nfc_release()
972 list_for_each_entry_safe(se, n, &dev->secure_elements, list) { in nfc_release()
973 nfc_genl_se_removed(dev, se->idx); in nfc_release()
974 list_del(&se->list); in nfc_release()
978 ida_free(&nfc_index_ida, dev->idx); in nfc_release()
989 device_lock(&dev->dev); in nfc_check_pres_work()
991 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) { in nfc_check_pres_work()
992 rc = dev->ops->check_presence(dev, dev->active_target); in nfc_check_pres_work()
993 if (rc == -EOPNOTSUPP) in nfc_check_pres_work()
996 u32 active_target_idx = dev->active_target->idx; in nfc_check_pres_work()
997 device_unlock(&dev->dev); in nfc_check_pres_work()
1002 if (!dev->shutting_down) in nfc_check_pres_work()
1003 mod_timer(&dev->check_pres_timer, jiffies + in nfc_check_pres_work()
1008 device_unlock(&dev->dev); in nfc_check_pres_work()
1015 schedule_work(&dev->check_pres_work); in nfc_check_pres_timeout()
1029 return dev->idx == *idx; in match_idx()
1044 * nfc_allocate_device - allocate a new nfc device
1058 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target || in nfc_allocate_device()
1059 !ops->deactivate_target || !ops->im_transceive) in nfc_allocate_device()
1072 dev->idx = rc; in nfc_allocate_device()
1074 dev->dev.class = &nfc_class; in nfc_allocate_device()
1075 dev_set_name(&dev->dev, "nfc%d", dev->idx); in nfc_allocate_device()
1076 device_initialize(&dev->dev); in nfc_allocate_device()
1078 dev->ops = ops; in nfc_allocate_device()
1079 dev->supported_protocols = supported_protocols; in nfc_allocate_device()
1080 dev->tx_headroom = tx_headroom; in nfc_allocate_device()
1081 dev->tx_tailroom = tx_tailroom; in nfc_allocate_device()
1082 INIT_LIST_HEAD(&dev->secure_elements); in nfc_allocate_device()
1084 nfc_genl_data_init(&dev->genl_data); in nfc_allocate_device()
1086 dev->rf_mode = NFC_RF_NONE; in nfc_allocate_device()
1089 dev->targets_generation = 1; in nfc_allocate_device()
1091 if (ops->check_presence) { in nfc_allocate_device()
1092 timer_setup(&dev->check_pres_timer, nfc_check_pres_timeout, 0); in nfc_allocate_device()
1093 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work); in nfc_allocate_device()
1106 * nfc_register_device - register a nfc device in the nfc subsystem
1114 pr_debug("dev_name=%s\n", dev_name(&dev->dev)); in nfc_register_device()
1118 rc = device_add(&dev->dev); in nfc_register_device()
1128 device_lock(&dev->dev); in nfc_register_device()
1129 dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev, in nfc_register_device()
1131 if (dev->rfkill) { in nfc_register_device()
1132 if (rfkill_register(dev->rfkill) < 0) { in nfc_register_device()
1133 rfkill_destroy(dev->rfkill); in nfc_register_device()
1134 dev->rfkill = NULL; in nfc_register_device()
1137 dev->shutting_down = false; in nfc_register_device()
1138 device_unlock(&dev->dev); in nfc_register_device()
1143 dev_name(&dev->dev)); in nfc_register_device()
1150 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
1158 pr_debug("dev_name=%s\n", dev_name(&dev->dev)); in nfc_unregister_device()
1163 "was removed\n", dev_name(&dev->dev)); in nfc_unregister_device()
1165 device_lock(&dev->dev); in nfc_unregister_device()
1166 if (dev->rfkill) { in nfc_unregister_device()
1167 rfkill_unregister(dev->rfkill); in nfc_unregister_device()
1168 rfkill_destroy(dev->rfkill); in nfc_unregister_device()
1169 dev->rfkill = NULL; in nfc_unregister_device()
1171 dev->shutting_down = true; in nfc_unregister_device()
1172 device_unlock(&dev->dev); in nfc_unregister_device()
1174 if (dev->ops->check_presence) { in nfc_unregister_device()
1175 del_timer_sync(&dev->check_pres_timer); in nfc_unregister_device()
1176 cancel_work_sync(&dev->check_pres_work); in nfc_unregister_device()
1183 device_del(&dev->dev); in nfc_unregister_device()