Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (C) 2011 Instituto Nokia de Tecnologia
0004  *
0005  * Authors:
0006  *    Lauro Ramos Venancio <lauro.venancio@openbossa.org>
0007  *    Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
0008  */
0009 
0010 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
0011 
0012 #include <linux/init.h>
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 #include <linux/slab.h>
0016 #include <linux/rfkill.h>
0017 #include <linux/nfc.h>
0018 
0019 #include <net/genetlink.h>
0020 
0021 #include "nfc.h"
0022 
0023 #define VERSION "0.1"
0024 
0025 #define NFC_CHECK_PRES_FREQ_MS  2000
0026 
0027 int nfc_devlist_generation;
0028 DEFINE_MUTEX(nfc_devlist_mutex);
0029 
0030 /* NFC device ID bitmap */
0031 static DEFINE_IDA(nfc_index_ida);
0032 
0033 int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name)
0034 {
0035     int rc = 0;
0036 
0037     pr_debug("%s do firmware %s\n", dev_name(&dev->dev), firmware_name);
0038 
0039     device_lock(&dev->dev);
0040 
0041     if (dev->shutting_down) {
0042         rc = -ENODEV;
0043         goto error;
0044     }
0045 
0046     if (dev->dev_up) {
0047         rc = -EBUSY;
0048         goto error;
0049     }
0050 
0051     if (!dev->ops->fw_download) {
0052         rc = -EOPNOTSUPP;
0053         goto error;
0054     }
0055 
0056     dev->fw_download_in_progress = true;
0057     rc = dev->ops->fw_download(dev, firmware_name);
0058     if (rc)
0059         dev->fw_download_in_progress = false;
0060 
0061 error:
0062     device_unlock(&dev->dev);
0063     return rc;
0064 }
0065 
0066 /**
0067  * nfc_fw_download_done - inform that a firmware download was completed
0068  *
0069  * @dev: The nfc device to which firmware was downloaded
0070  * @firmware_name: The firmware filename
0071  * @result: The positive value of a standard errno value
0072  */
0073 int nfc_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
0074              u32 result)
0075 {
0076     dev->fw_download_in_progress = false;
0077 
0078     return nfc_genl_fw_download_done(dev, firmware_name, result);
0079 }
0080 EXPORT_SYMBOL(nfc_fw_download_done);
0081 
0082 /**
0083  * nfc_dev_up - turn on the NFC device
0084  *
0085  * @dev: The nfc device to be turned on
0086  *
0087  * The device remains up until the nfc_dev_down function is called.
0088  */
0089 int nfc_dev_up(struct nfc_dev *dev)
0090 {
0091     int rc = 0;
0092 
0093     pr_debug("dev_name=%s\n", dev_name(&dev->dev));
0094 
0095     device_lock(&dev->dev);
0096 
0097     if (dev->shutting_down) {
0098         rc = -ENODEV;
0099         goto error;
0100     }
0101 
0102     if (dev->rfkill && rfkill_blocked(dev->rfkill)) {
0103         rc = -ERFKILL;
0104         goto error;
0105     }
0106 
0107     if (dev->fw_download_in_progress) {
0108         rc = -EBUSY;
0109         goto error;
0110     }
0111 
0112     if (dev->dev_up) {
0113         rc = -EALREADY;
0114         goto error;
0115     }
0116 
0117     if (dev->ops->dev_up)
0118         rc = dev->ops->dev_up(dev);
0119 
0120     if (!rc)
0121         dev->dev_up = true;
0122 
0123     /* We have to enable the device before discovering SEs */
0124     if (dev->ops->discover_se && dev->ops->discover_se(dev))
0125         pr_err("SE discovery failed\n");
0126 
0127 error:
0128     device_unlock(&dev->dev);
0129     return rc;
0130 }
0131 
0132 /**
0133  * nfc_dev_down - turn off the NFC device
0134  *
0135  * @dev: The nfc device to be turned off
0136  */
0137 int nfc_dev_down(struct nfc_dev *dev)
0138 {
0139     int rc = 0;
0140 
0141     pr_debug("dev_name=%s\n", dev_name(&dev->dev));
0142 
0143     device_lock(&dev->dev);
0144 
0145     if (dev->shutting_down) {
0146         rc = -ENODEV;
0147         goto error;
0148     }
0149 
0150     if (!dev->dev_up) {
0151         rc = -EALREADY;
0152         goto error;
0153     }
0154 
0155     if (dev->polling || dev->active_target) {
0156         rc = -EBUSY;
0157         goto error;
0158     }
0159 
0160     if (dev->ops->dev_down)
0161         dev->ops->dev_down(dev);
0162 
0163     dev->dev_up = false;
0164 
0165 error:
0166     device_unlock(&dev->dev);
0167     return rc;
0168 }
0169 
0170 static int nfc_rfkill_set_block(void *data, bool blocked)
0171 {
0172     struct nfc_dev *dev = data;
0173 
0174     pr_debug("%s blocked %d", dev_name(&dev->dev), blocked);
0175 
0176     if (!blocked)
0177         return 0;
0178 
0179     nfc_dev_down(dev);
0180 
0181     return 0;
0182 }
0183 
0184 static const struct rfkill_ops nfc_rfkill_ops = {
0185     .set_block = nfc_rfkill_set_block,
0186 };
0187 
0188 /**
0189  * nfc_start_poll - start polling for nfc targets
0190  *
0191  * @dev: The nfc device that must start polling
0192  * @im_protocols: bitset of nfc initiator protocols to be used for polling
0193  * @tm_protocols: bitset of nfc transport protocols to be used for polling
0194  *
0195  * The device remains polling for targets until a target is found or
0196  * the nfc_stop_poll function is called.
0197  */
0198 int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
0199 {
0200     int rc;
0201 
0202     pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
0203          dev_name(&dev->dev), im_protocols, tm_protocols);
0204 
0205     if (!im_protocols && !tm_protocols)
0206         return -EINVAL;
0207 
0208     device_lock(&dev->dev);
0209 
0210     if (dev->shutting_down) {
0211         rc = -ENODEV;
0212         goto error;
0213     }
0214 
0215     if (!dev->dev_up) {
0216         rc = -ENODEV;
0217         goto error;
0218     }
0219 
0220     if (dev->polling) {
0221         rc = -EBUSY;
0222         goto error;
0223     }
0224 
0225     rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
0226     if (!rc) {
0227         dev->polling = true;
0228         dev->rf_mode = NFC_RF_NONE;
0229     }
0230 
0231 error:
0232     device_unlock(&dev->dev);
0233     return rc;
0234 }
0235 
0236 /**
0237  * nfc_stop_poll - stop polling for nfc targets
0238  *
0239  * @dev: The nfc device that must stop polling
0240  */
0241 int nfc_stop_poll(struct nfc_dev *dev)
0242 {
0243     int rc = 0;
0244 
0245     pr_debug("dev_name=%s\n", dev_name(&dev->dev));
0246 
0247     device_lock(&dev->dev);
0248 
0249     if (dev->shutting_down) {
0250         rc = -ENODEV;
0251         goto error;
0252     }
0253 
0254     if (!dev->polling) {
0255         rc = -EINVAL;
0256         goto error;
0257     }
0258 
0259     dev->ops->stop_poll(dev);
0260     dev->polling = false;
0261     dev->rf_mode = NFC_RF_NONE;
0262 
0263 error:
0264     device_unlock(&dev->dev);
0265     return rc;
0266 }
0267 
0268 static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
0269 {
0270     int i;
0271 
0272     for (i = 0; i < dev->n_targets; i++) {
0273         if (dev->targets[i].idx == target_idx)
0274             return &dev->targets[i];
0275     }
0276 
0277     return NULL;
0278 }
0279 
0280 int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
0281 {
0282     int rc = 0;
0283     u8 *gb;
0284     size_t gb_len;
0285     struct nfc_target *target;
0286 
0287     pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
0288 
0289     if (!dev->ops->dep_link_up)
0290         return -EOPNOTSUPP;
0291 
0292     device_lock(&dev->dev);
0293 
0294     if (dev->shutting_down) {
0295         rc = -ENODEV;
0296         goto error;
0297     }
0298 
0299     if (dev->dep_link_up == true) {
0300         rc = -EALREADY;
0301         goto error;
0302     }
0303 
0304     gb = nfc_llcp_general_bytes(dev, &gb_len);
0305     if (gb_len > NFC_MAX_GT_LEN) {
0306         rc = -EINVAL;
0307         goto error;
0308     }
0309 
0310     target = nfc_find_target(dev, target_index);
0311     if (target == NULL) {
0312         rc = -ENOTCONN;
0313         goto error;
0314     }
0315 
0316     rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
0317     if (!rc) {
0318         dev->active_target = target;
0319         dev->rf_mode = NFC_RF_INITIATOR;
0320     }
0321 
0322 error:
0323     device_unlock(&dev->dev);
0324     return rc;
0325 }
0326 
0327 int nfc_dep_link_down(struct nfc_dev *dev)
0328 {
0329     int rc = 0;
0330 
0331     pr_debug("dev_name=%s\n", dev_name(&dev->dev));
0332 
0333     if (!dev->ops->dep_link_down)
0334         return -EOPNOTSUPP;
0335 
0336     device_lock(&dev->dev);
0337 
0338     if (dev->shutting_down) {
0339         rc = -ENODEV;
0340         goto error;
0341     }
0342 
0343     if (dev->dep_link_up == false) {
0344         rc = -EALREADY;
0345         goto error;
0346     }
0347 
0348     rc = dev->ops->dep_link_down(dev);
0349     if (!rc) {
0350         dev->dep_link_up = false;
0351         dev->active_target = NULL;
0352         dev->rf_mode = NFC_RF_NONE;
0353         nfc_llcp_mac_is_down(dev);
0354         nfc_genl_dep_link_down_event(dev);
0355     }
0356 
0357 error:
0358     device_unlock(&dev->dev);
0359 
0360     return rc;
0361 }
0362 
0363 int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
0364                u8 comm_mode, u8 rf_mode)
0365 {
0366     dev->dep_link_up = true;
0367 
0368     if (!dev->active_target && rf_mode == NFC_RF_INITIATOR) {
0369         struct nfc_target *target;
0370 
0371         target = nfc_find_target(dev, target_idx);
0372         if (target == NULL)
0373             return -ENOTCONN;
0374 
0375         dev->active_target = target;
0376     }
0377 
0378     dev->polling = false;
0379     dev->rf_mode = rf_mode;
0380 
0381     nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
0382 
0383     return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
0384 }
0385 EXPORT_SYMBOL(nfc_dep_link_is_up);
0386 
0387 /**
0388  * nfc_activate_target - prepare the target for data exchange
0389  *
0390  * @dev: The nfc device that found the target
0391  * @target_idx: index of the target that must be activated
0392  * @protocol: nfc protocol that will be used for data exchange
0393  */
0394 int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
0395 {
0396     int rc;
0397     struct nfc_target *target;
0398 
0399     pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
0400          dev_name(&dev->dev), target_idx, protocol);
0401 
0402     device_lock(&dev->dev);
0403 
0404     if (dev->shutting_down) {
0405         rc = -ENODEV;
0406         goto error;
0407     }
0408 
0409     if (dev->active_target) {
0410         rc = -EBUSY;
0411         goto error;
0412     }
0413 
0414     target = nfc_find_target(dev, target_idx);
0415     if (target == NULL) {
0416         rc = -ENOTCONN;
0417         goto error;
0418     }
0419 
0420     rc = dev->ops->activate_target(dev, target, protocol);
0421     if (!rc) {
0422         dev->active_target = target;
0423         dev->rf_mode = NFC_RF_INITIATOR;
0424 
0425         if (dev->ops->check_presence && !dev->shutting_down)
0426             mod_timer(&dev->check_pres_timer, jiffies +
0427                   msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
0428     }
0429 
0430 error:
0431     device_unlock(&dev->dev);
0432     return rc;
0433 }
0434 
0435 /**
0436  * nfc_deactivate_target - deactivate a nfc target
0437  *
0438  * @dev: The nfc device that found the target
0439  * @target_idx: index of the target that must be deactivated
0440  * @mode: idle or sleep?
0441  */
0442 int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx, u8 mode)
0443 {
0444     int rc = 0;
0445 
0446     pr_debug("dev_name=%s target_idx=%u\n",
0447          dev_name(&dev->dev), target_idx);
0448 
0449     device_lock(&dev->dev);
0450 
0451     if (dev->shutting_down) {
0452         rc = -ENODEV;
0453         goto error;
0454     }
0455 
0456     if (dev->active_target == NULL) {
0457         rc = -ENOTCONN;
0458         goto error;
0459     }
0460 
0461     if (dev->active_target->idx != target_idx) {
0462         rc = -ENOTCONN;
0463         goto error;
0464     }
0465 
0466     if (dev->ops->check_presence)
0467         del_timer_sync(&dev->check_pres_timer);
0468 
0469     dev->ops->deactivate_target(dev, dev->active_target, mode);
0470     dev->active_target = NULL;
0471 
0472 error:
0473     device_unlock(&dev->dev);
0474     return rc;
0475 }
0476 
0477 /**
0478  * nfc_data_exchange - transceive data
0479  *
0480  * @dev: The nfc device that found the target
0481  * @target_idx: index of the target
0482  * @skb: data to be sent
0483  * @cb: callback called when the response is received
0484  * @cb_context: parameter for the callback function
0485  *
0486  * The user must wait for the callback before calling this function again.
0487  */
0488 int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
0489               data_exchange_cb_t cb, void *cb_context)
0490 {
0491     int rc;
0492 
0493     pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
0494          dev_name(&dev->dev), target_idx, skb->len);
0495 
0496     device_lock(&dev->dev);
0497 
0498     if (dev->shutting_down) {
0499         rc = -ENODEV;
0500         kfree_skb(skb);
0501         goto error;
0502     }
0503 
0504     if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
0505         if (dev->active_target->idx != target_idx) {
0506             rc = -EADDRNOTAVAIL;
0507             kfree_skb(skb);
0508             goto error;
0509         }
0510 
0511         if (dev->ops->check_presence)
0512             del_timer_sync(&dev->check_pres_timer);
0513 
0514         rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
0515                          cb_context);
0516 
0517         if (!rc && dev->ops->check_presence && !dev->shutting_down)
0518             mod_timer(&dev->check_pres_timer, jiffies +
0519                   msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
0520     } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
0521         rc = dev->ops->tm_send(dev, skb);
0522     } else {
0523         rc = -ENOTCONN;
0524         kfree_skb(skb);
0525         goto error;
0526     }
0527 
0528 
0529 error:
0530     device_unlock(&dev->dev);
0531     return rc;
0532 }
0533 
0534 struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx)
0535 {
0536     struct nfc_se *se;
0537 
0538     list_for_each_entry(se, &dev->secure_elements, list)
0539         if (se->idx == se_idx)
0540             return se;
0541 
0542     return NULL;
0543 }
0544 EXPORT_SYMBOL(nfc_find_se);
0545 
0546 int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
0547 {
0548     struct nfc_se *se;
0549     int rc;
0550 
0551     pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
0552 
0553     device_lock(&dev->dev);
0554 
0555     if (dev->shutting_down) {
0556         rc = -ENODEV;
0557         goto error;
0558     }
0559 
0560     if (!dev->dev_up) {
0561         rc = -ENODEV;
0562         goto error;
0563     }
0564 
0565     if (dev->polling) {
0566         rc = -EBUSY;
0567         goto error;
0568     }
0569 
0570     if (!dev->ops->enable_se || !dev->ops->disable_se) {
0571         rc = -EOPNOTSUPP;
0572         goto error;
0573     }
0574 
0575     se = nfc_find_se(dev, se_idx);
0576     if (!se) {
0577         rc = -EINVAL;
0578         goto error;
0579     }
0580 
0581     if (se->state == NFC_SE_ENABLED) {
0582         rc = -EALREADY;
0583         goto error;
0584     }
0585 
0586     rc = dev->ops->enable_se(dev, se_idx);
0587     if (rc >= 0)
0588         se->state = NFC_SE_ENABLED;
0589 
0590 error:
0591     device_unlock(&dev->dev);
0592     return rc;
0593 }
0594 
0595 int nfc_disable_se(struct nfc_dev *dev, u32 se_idx)
0596 {
0597     struct nfc_se *se;
0598     int rc;
0599 
0600     pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
0601 
0602     device_lock(&dev->dev);
0603 
0604     if (dev->shutting_down) {
0605         rc = -ENODEV;
0606         goto error;
0607     }
0608 
0609     if (!dev->dev_up) {
0610         rc = -ENODEV;
0611         goto error;
0612     }
0613 
0614     if (!dev->ops->enable_se || !dev->ops->disable_se) {
0615         rc = -EOPNOTSUPP;
0616         goto error;
0617     }
0618 
0619     se = nfc_find_se(dev, se_idx);
0620     if (!se) {
0621         rc = -EINVAL;
0622         goto error;
0623     }
0624 
0625     if (se->state == NFC_SE_DISABLED) {
0626         rc = -EALREADY;
0627         goto error;
0628     }
0629 
0630     rc = dev->ops->disable_se(dev, se_idx);
0631     if (rc >= 0)
0632         se->state = NFC_SE_DISABLED;
0633 
0634 error:
0635     device_unlock(&dev->dev);
0636     return rc;
0637 }
0638 
0639 int nfc_set_remote_general_bytes(struct nfc_dev *dev, const u8 *gb, u8 gb_len)
0640 {
0641     pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
0642 
0643     return nfc_llcp_set_remote_gb(dev, gb, gb_len);
0644 }
0645 EXPORT_SYMBOL(nfc_set_remote_general_bytes);
0646 
0647 u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
0648 {
0649     pr_debug("dev_name=%s\n", dev_name(&dev->dev));
0650 
0651     return nfc_llcp_general_bytes(dev, gb_len);
0652 }
0653 EXPORT_SYMBOL(nfc_get_local_general_bytes);
0654 
0655 int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
0656 {
0657     /* Only LLCP target mode for now */
0658     if (dev->dep_link_up == false) {
0659         kfree_skb(skb);
0660         return -ENOLINK;
0661     }
0662 
0663     return nfc_llcp_data_received(dev, skb);
0664 }
0665 EXPORT_SYMBOL(nfc_tm_data_received);
0666 
0667 int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
0668              const u8 *gb, size_t gb_len)
0669 {
0670     int rc;
0671 
0672     device_lock(&dev->dev);
0673 
0674     dev->polling = false;
0675 
0676     if (gb != NULL) {
0677         rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
0678         if (rc < 0)
0679             goto out;
0680     }
0681 
0682     dev->rf_mode = NFC_RF_TARGET;
0683 
0684     if (protocol == NFC_PROTO_NFC_DEP_MASK)
0685         nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
0686 
0687     rc = nfc_genl_tm_activated(dev, protocol);
0688 
0689 out:
0690     device_unlock(&dev->dev);
0691 
0692     return rc;
0693 }
0694 EXPORT_SYMBOL(nfc_tm_activated);
0695 
0696 int nfc_tm_deactivated(struct nfc_dev *dev)
0697 {
0698     dev->dep_link_up = false;
0699     dev->rf_mode = NFC_RF_NONE;
0700 
0701     return nfc_genl_tm_deactivated(dev);
0702 }
0703 EXPORT_SYMBOL(nfc_tm_deactivated);
0704 
0705 /**
0706  * nfc_alloc_send_skb - allocate a skb for data exchange responses
0707  *
0708  * @dev: device sending the response
0709  * @sk: socket sending the response
0710  * @flags: MSG_DONTWAIT flag
0711  * @size: size to allocate
0712  * @err: pointer to memory to store the error code
0713  */
0714 struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
0715                    unsigned int flags, unsigned int size,
0716                    unsigned int *err)
0717 {
0718     struct sk_buff *skb;
0719     unsigned int total_size;
0720 
0721     total_size = size +
0722         dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
0723 
0724     skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
0725     if (skb)
0726         skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
0727 
0728     return skb;
0729 }
0730 
0731 /**
0732  * nfc_alloc_recv_skb - allocate a skb for data exchange responses
0733  *
0734  * @size: size to allocate
0735  * @gfp: gfp flags
0736  */
0737 struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
0738 {
0739     struct sk_buff *skb;
0740     unsigned int total_size;
0741 
0742     total_size = size + 1;
0743     skb = alloc_skb(total_size, gfp);
0744 
0745     if (skb)
0746         skb_reserve(skb, 1);
0747 
0748     return skb;
0749 }
0750 EXPORT_SYMBOL(nfc_alloc_recv_skb);
0751 
0752 /**
0753  * nfc_targets_found - inform that targets were found
0754  *
0755  * @dev: The nfc device that found the targets
0756  * @targets: array of nfc targets found
0757  * @n_targets: targets array size
0758  *
0759  * The device driver must call this function when one or many nfc targets
0760  * are found. After calling this function, the device driver must stop
0761  * polling for targets.
0762  * NOTE: This function can be called with targets=NULL and n_targets=0 to
0763  * notify a driver error, meaning that the polling operation cannot complete.
0764  * IMPORTANT: this function must not be called from an atomic context.
0765  * In addition, it must also not be called from a context that would prevent
0766  * the NFC Core to call other nfc ops entry point concurrently.
0767  */
0768 int nfc_targets_found(struct nfc_dev *dev,
0769               struct nfc_target *targets, int n_targets)
0770 {
0771     int i;
0772 
0773     pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
0774 
0775     for (i = 0; i < n_targets; i++)
0776         targets[i].idx = dev->target_next_idx++;
0777 
0778     device_lock(&dev->dev);
0779 
0780     if (dev->polling == false) {
0781         device_unlock(&dev->dev);
0782         return 0;
0783     }
0784 
0785     dev->polling = false;
0786 
0787     dev->targets_generation++;
0788 
0789     kfree(dev->targets);
0790     dev->targets = NULL;
0791 
0792     if (targets) {
0793         dev->targets = kmemdup(targets,
0794                        n_targets * sizeof(struct nfc_target),
0795                        GFP_ATOMIC);
0796 
0797         if (!dev->targets) {
0798             dev->n_targets = 0;
0799             device_unlock(&dev->dev);
0800             return -ENOMEM;
0801         }
0802     }
0803 
0804     dev->n_targets = n_targets;
0805     device_unlock(&dev->dev);
0806 
0807     nfc_genl_targets_found(dev);
0808 
0809     return 0;
0810 }
0811 EXPORT_SYMBOL(nfc_targets_found);
0812 
0813 /**
0814  * nfc_target_lost - inform that an activated target went out of field
0815  *
0816  * @dev: The nfc device that had the activated target in field
0817  * @target_idx: the nfc index of the target
0818  *
0819  * The device driver must call this function when the activated target
0820  * goes out of the field.
0821  * IMPORTANT: this function must not be called from an atomic context.
0822  * In addition, it must also not be called from a context that would prevent
0823  * the NFC Core to call other nfc ops entry point concurrently.
0824  */
0825 int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
0826 {
0827     const struct nfc_target *tg;
0828     int i;
0829 
0830     pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
0831 
0832     device_lock(&dev->dev);
0833 
0834     for (i = 0; i < dev->n_targets; i++) {
0835         tg = &dev->targets[i];
0836         if (tg->idx == target_idx)
0837             break;
0838     }
0839 
0840     if (i == dev->n_targets) {
0841         device_unlock(&dev->dev);
0842         return -EINVAL;
0843     }
0844 
0845     dev->targets_generation++;
0846     dev->n_targets--;
0847     dev->active_target = NULL;
0848 
0849     if (dev->n_targets) {
0850         memcpy(&dev->targets[i], &dev->targets[i + 1],
0851                (dev->n_targets - i) * sizeof(struct nfc_target));
0852     } else {
0853         kfree(dev->targets);
0854         dev->targets = NULL;
0855     }
0856 
0857     device_unlock(&dev->dev);
0858 
0859     nfc_genl_target_lost(dev, target_idx);
0860 
0861     return 0;
0862 }
0863 EXPORT_SYMBOL(nfc_target_lost);
0864 
0865 inline void nfc_driver_failure(struct nfc_dev *dev, int err)
0866 {
0867     nfc_targets_found(dev, NULL, 0);
0868 }
0869 EXPORT_SYMBOL(nfc_driver_failure);
0870 
0871 int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type)
0872 {
0873     struct nfc_se *se;
0874     int rc;
0875 
0876     pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
0877 
0878     se = nfc_find_se(dev, se_idx);
0879     if (se)
0880         return -EALREADY;
0881 
0882     se = kzalloc(sizeof(struct nfc_se), GFP_KERNEL);
0883     if (!se)
0884         return -ENOMEM;
0885 
0886     se->idx = se_idx;
0887     se->type = type;
0888     se->state = NFC_SE_DISABLED;
0889     INIT_LIST_HEAD(&se->list);
0890 
0891     list_add(&se->list, &dev->secure_elements);
0892 
0893     rc = nfc_genl_se_added(dev, se_idx, type);
0894     if (rc < 0) {
0895         list_del(&se->list);
0896         kfree(se);
0897 
0898         return rc;
0899     }
0900 
0901     return 0;
0902 }
0903 EXPORT_SYMBOL(nfc_add_se);
0904 
0905 int nfc_remove_se(struct nfc_dev *dev, u32 se_idx)
0906 {
0907     struct nfc_se *se, *n;
0908     int rc;
0909 
0910     pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
0911 
0912     list_for_each_entry_safe(se, n, &dev->secure_elements, list)
0913         if (se->idx == se_idx) {
0914             rc = nfc_genl_se_removed(dev, se_idx);
0915             if (rc < 0)
0916                 return rc;
0917 
0918             list_del(&se->list);
0919             kfree(se);
0920 
0921             return 0;
0922         }
0923 
0924     return -EINVAL;
0925 }
0926 EXPORT_SYMBOL(nfc_remove_se);
0927 
0928 int nfc_se_transaction(struct nfc_dev *dev, u8 se_idx,
0929                struct nfc_evt_transaction *evt_transaction)
0930 {
0931     int rc;
0932 
0933     pr_debug("transaction: %x\n", se_idx);
0934 
0935     device_lock(&dev->dev);
0936 
0937     if (!evt_transaction) {
0938         rc = -EPROTO;
0939         goto out;
0940     }
0941 
0942     rc = nfc_genl_se_transaction(dev, se_idx, evt_transaction);
0943 out:
0944     device_unlock(&dev->dev);
0945     return rc;
0946 }
0947 EXPORT_SYMBOL(nfc_se_transaction);
0948 
0949 int nfc_se_connectivity(struct nfc_dev *dev, u8 se_idx)
0950 {
0951     int rc;
0952 
0953     pr_debug("connectivity: %x\n", se_idx);
0954 
0955     device_lock(&dev->dev);
0956     rc = nfc_genl_se_connectivity(dev, se_idx);
0957     device_unlock(&dev->dev);
0958     return rc;
0959 }
0960 EXPORT_SYMBOL(nfc_se_connectivity);
0961 
0962 static void nfc_release(struct device *d)
0963 {
0964     struct nfc_dev *dev = to_nfc_dev(d);
0965     struct nfc_se *se, *n;
0966 
0967     pr_debug("dev_name=%s\n", dev_name(&dev->dev));
0968 
0969     nfc_genl_data_exit(&dev->genl_data);
0970     kfree(dev->targets);
0971 
0972     list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
0973             nfc_genl_se_removed(dev, se->idx);
0974             list_del(&se->list);
0975             kfree(se);
0976     }
0977 
0978     ida_free(&nfc_index_ida, dev->idx);
0979 
0980     kfree(dev);
0981 }
0982 
0983 static void nfc_check_pres_work(struct work_struct *work)
0984 {
0985     struct nfc_dev *dev = container_of(work, struct nfc_dev,
0986                        check_pres_work);
0987     int rc;
0988 
0989     device_lock(&dev->dev);
0990 
0991     if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
0992         rc = dev->ops->check_presence(dev, dev->active_target);
0993         if (rc == -EOPNOTSUPP)
0994             goto exit;
0995         if (rc) {
0996             u32 active_target_idx = dev->active_target->idx;
0997             device_unlock(&dev->dev);
0998             nfc_target_lost(dev, active_target_idx);
0999             return;
1000         }
1001 
1002         if (!dev->shutting_down)
1003             mod_timer(&dev->check_pres_timer, jiffies +
1004                   msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
1005     }
1006 
1007 exit:
1008     device_unlock(&dev->dev);
1009 }
1010 
1011 static void nfc_check_pres_timeout(struct timer_list *t)
1012 {
1013     struct nfc_dev *dev = from_timer(dev, t, check_pres_timer);
1014 
1015     schedule_work(&dev->check_pres_work);
1016 }
1017 
1018 struct class nfc_class = {
1019     .name = "nfc",
1020     .dev_release = nfc_release,
1021 };
1022 EXPORT_SYMBOL(nfc_class);
1023 
1024 static int match_idx(struct device *d, const void *data)
1025 {
1026     struct nfc_dev *dev = to_nfc_dev(d);
1027     const unsigned int *idx = data;
1028 
1029     return dev->idx == *idx;
1030 }
1031 
1032 struct nfc_dev *nfc_get_device(unsigned int idx)
1033 {
1034     struct device *d;
1035 
1036     d = class_find_device(&nfc_class, NULL, &idx, match_idx);
1037     if (!d)
1038         return NULL;
1039 
1040     return to_nfc_dev(d);
1041 }
1042 
1043 /**
1044  * nfc_allocate_device - allocate a new nfc device
1045  *
1046  * @ops: device operations
1047  * @supported_protocols: NFC protocols supported by the device
1048  * @tx_headroom: reserved space at beginning of skb
1049  * @tx_tailroom: reserved space at end of skb
1050  */
1051 struct nfc_dev *nfc_allocate_device(const struct nfc_ops *ops,
1052                     u32 supported_protocols,
1053                     int tx_headroom, int tx_tailroom)
1054 {
1055     struct nfc_dev *dev;
1056     int rc;
1057 
1058     if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
1059         !ops->deactivate_target || !ops->im_transceive)
1060         return NULL;
1061 
1062     if (!supported_protocols)
1063         return NULL;
1064 
1065     dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
1066     if (!dev)
1067         return NULL;
1068 
1069     rc = ida_alloc(&nfc_index_ida, GFP_KERNEL);
1070     if (rc < 0)
1071         goto err_free_dev;
1072     dev->idx = rc;
1073 
1074     dev->dev.class = &nfc_class;
1075     dev_set_name(&dev->dev, "nfc%d", dev->idx);
1076     device_initialize(&dev->dev);
1077 
1078     dev->ops = ops;
1079     dev->supported_protocols = supported_protocols;
1080     dev->tx_headroom = tx_headroom;
1081     dev->tx_tailroom = tx_tailroom;
1082     INIT_LIST_HEAD(&dev->secure_elements);
1083 
1084     nfc_genl_data_init(&dev->genl_data);
1085 
1086     dev->rf_mode = NFC_RF_NONE;
1087 
1088     /* first generation must not be 0 */
1089     dev->targets_generation = 1;
1090 
1091     if (ops->check_presence) {
1092         timer_setup(&dev->check_pres_timer, nfc_check_pres_timeout, 0);
1093         INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
1094     }
1095 
1096     return dev;
1097 
1098 err_free_dev:
1099     kfree(dev);
1100 
1101     return NULL;
1102 }
1103 EXPORT_SYMBOL(nfc_allocate_device);
1104 
1105 /**
1106  * nfc_register_device - register a nfc device in the nfc subsystem
1107  *
1108  * @dev: The nfc device to register
1109  */
1110 int nfc_register_device(struct nfc_dev *dev)
1111 {
1112     int rc;
1113 
1114     pr_debug("dev_name=%s\n", dev_name(&dev->dev));
1115 
1116     mutex_lock(&nfc_devlist_mutex);
1117     nfc_devlist_generation++;
1118     rc = device_add(&dev->dev);
1119     mutex_unlock(&nfc_devlist_mutex);
1120 
1121     if (rc < 0)
1122         return rc;
1123 
1124     rc = nfc_llcp_register_device(dev);
1125     if (rc)
1126         pr_err("Could not register llcp device\n");
1127 
1128     device_lock(&dev->dev);
1129     dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev,
1130                    RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev);
1131     if (dev->rfkill) {
1132         if (rfkill_register(dev->rfkill) < 0) {
1133             rfkill_destroy(dev->rfkill);
1134             dev->rfkill = NULL;
1135         }
1136     }
1137     dev->shutting_down = false;
1138     device_unlock(&dev->dev);
1139 
1140     rc = nfc_genl_device_added(dev);
1141     if (rc)
1142         pr_debug("The userspace won't be notified that the device %s was added\n",
1143              dev_name(&dev->dev));
1144 
1145     return 0;
1146 }
1147 EXPORT_SYMBOL(nfc_register_device);
1148 
1149 /**
1150  * nfc_unregister_device - unregister a nfc device in the nfc subsystem
1151  *
1152  * @dev: The nfc device to unregister
1153  */
1154 void nfc_unregister_device(struct nfc_dev *dev)
1155 {
1156     int rc;
1157 
1158     pr_debug("dev_name=%s\n", dev_name(&dev->dev));
1159 
1160     rc = nfc_genl_device_removed(dev);
1161     if (rc)
1162         pr_debug("The userspace won't be notified that the device %s "
1163              "was removed\n", dev_name(&dev->dev));
1164 
1165     device_lock(&dev->dev);
1166     if (dev->rfkill) {
1167         rfkill_unregister(dev->rfkill);
1168         rfkill_destroy(dev->rfkill);
1169         dev->rfkill = NULL;
1170     }
1171     dev->shutting_down = true;
1172     device_unlock(&dev->dev);
1173 
1174     if (dev->ops->check_presence) {
1175         del_timer_sync(&dev->check_pres_timer);
1176         cancel_work_sync(&dev->check_pres_work);
1177     }
1178 
1179     nfc_llcp_unregister_device(dev);
1180 
1181     mutex_lock(&nfc_devlist_mutex);
1182     nfc_devlist_generation++;
1183     device_del(&dev->dev);
1184     mutex_unlock(&nfc_devlist_mutex);
1185 }
1186 EXPORT_SYMBOL(nfc_unregister_device);
1187 
1188 static int __init nfc_init(void)
1189 {
1190     int rc;
1191 
1192     pr_info("NFC Core ver %s\n", VERSION);
1193 
1194     rc = class_register(&nfc_class);
1195     if (rc)
1196         return rc;
1197 
1198     rc = nfc_genl_init();
1199     if (rc)
1200         goto err_genl;
1201 
1202     /* the first generation must not be 0 */
1203     nfc_devlist_generation = 1;
1204 
1205     rc = rawsock_init();
1206     if (rc)
1207         goto err_rawsock;
1208 
1209     rc = nfc_llcp_init();
1210     if (rc)
1211         goto err_llcp_sock;
1212 
1213     rc = af_nfc_init();
1214     if (rc)
1215         goto err_af_nfc;
1216 
1217     return 0;
1218 
1219 err_af_nfc:
1220     nfc_llcp_exit();
1221 err_llcp_sock:
1222     rawsock_exit();
1223 err_rawsock:
1224     nfc_genl_exit();
1225 err_genl:
1226     class_unregister(&nfc_class);
1227     return rc;
1228 }
1229 
1230 static void __exit nfc_exit(void)
1231 {
1232     af_nfc_exit();
1233     nfc_llcp_exit();
1234     rawsock_exit();
1235     nfc_genl_exit();
1236     class_unregister(&nfc_class);
1237 }
1238 
1239 subsys_initcall(nfc_init);
1240 module_exit(nfc_exit);
1241 
1242 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
1243 MODULE_DESCRIPTION("NFC Core ver " VERSION);
1244 MODULE_VERSION(VERSION);
1245 MODULE_LICENSE("GPL");
1246 MODULE_ALIAS_NETPROTO(PF_NFC);
1247 MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);