1273 lines · 25.0 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later/* * Copyright (C) 2011 Instituto Nokia de Tecnologia * * Authors: *    Lauro Ramos Venancio <lauro.venancio@openbossa.org> *    Aloisio Almeida Jr <aloisio.almeida@openbossa.org> */ #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ #include <linux/init.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/slab.h>#include <linux/rfkill.h>#include <linux/nfc.h> #include <net/genetlink.h> #include "nfc.h" #define VERSION "0.1" #define NFC_CHECK_PRES_FREQ_MS	2000 int nfc_devlist_generation;DEFINE_MUTEX(nfc_devlist_mutex); /* NFC device ID bitmap */static DEFINE_IDA(nfc_index_ida); int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name){	int rc = 0; 	pr_debug("%s do firmware %s\n", dev_name(&dev->dev), firmware_name); 	device_lock(&dev->dev); 	if (dev->shutting_down) {		rc = -ENODEV;		goto error;	} 	if (dev->dev_up) {		rc = -EBUSY;		goto error;	} 	if (!dev->ops->fw_download) {		rc = -EOPNOTSUPP;		goto error;	} 	dev->fw_download_in_progress = true;	rc = dev->ops->fw_download(dev, firmware_name);	if (rc)		dev->fw_download_in_progress = false; error:	device_unlock(&dev->dev);	return rc;} /** * nfc_fw_download_done - inform that a firmware download was completed * * @dev: The nfc device to which firmware was downloaded * @firmware_name: The firmware filename * @result: The positive value of a standard errno value */int nfc_fw_download_done(struct nfc_dev *dev, const char *firmware_name,			 u32 result){	dev->fw_download_in_progress = false; 	return nfc_genl_fw_download_done(dev, firmware_name, result);}EXPORT_SYMBOL(nfc_fw_download_done); /** * nfc_dev_up - turn on the NFC device * * @dev: The nfc device to be turned on * * The device remains up until the nfc_dev_down function is called. */int nfc_dev_up(struct nfc_dev *dev){	int rc = 0; 	pr_debug("dev_name=%s\n", dev_name(&dev->dev)); 	device_lock(&dev->dev); 	if (dev->shutting_down) {		rc = -ENODEV;		goto error;	} 	if (dev->rfkill && rfkill_blocked(dev->rfkill)) {		rc = -ERFKILL;		goto error;	} 	if (dev->fw_download_in_progress) {		rc = -EBUSY;		goto error;	} 	if (dev->dev_up) {		rc = -EALREADY;		goto error;	} 	if (dev->ops->dev_up)		rc = dev->ops->dev_up(dev); 	if (!rc)		dev->dev_up = true; 	/* We have to enable the device before discovering SEs */	if (dev->ops->discover_se && dev->ops->discover_se(dev))		pr_err("SE discovery failed\n"); error:	device_unlock(&dev->dev);	return rc;} /** * nfc_dev_down - turn off the NFC device * * @dev: The nfc device to be turned off */int nfc_dev_down(struct nfc_dev *dev){	int rc = 0; 	pr_debug("dev_name=%s\n", dev_name(&dev->dev)); 	device_lock(&dev->dev); 	if (dev->shutting_down) {		rc = -ENODEV;		goto error;	} 	if (!dev->dev_up) {		rc = -EALREADY;		goto error;	} 	if (dev->polling || dev->active_target) {		rc = -EBUSY;		goto error;	} 	if (dev->ops->dev_down)		dev->ops->dev_down(dev); 	dev->dev_up = false; error:	device_unlock(&dev->dev);	return rc;} static int nfc_rfkill_set_block(void *data, bool blocked){	struct nfc_dev *dev = data; 	pr_debug("%s blocked %d", dev_name(&dev->dev), blocked); 	if (!blocked)		return 0; 	nfc_dev_down(dev); 	return 0;} static const struct rfkill_ops nfc_rfkill_ops = {	.set_block = nfc_rfkill_set_block,}; /** * nfc_start_poll - start polling for nfc targets * * @dev: The nfc device that must start polling * @im_protocols: bitset of nfc initiator protocols to be used for polling * @tm_protocols: bitset of nfc transport protocols to be used for polling * * The device remains polling for targets until a target is found or * the nfc_stop_poll function is called. */int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols){	int rc; 	pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",		 dev_name(&dev->dev), im_protocols, tm_protocols); 	if (!im_protocols && !tm_protocols)		return -EINVAL; 	device_lock(&dev->dev); 	if (dev->shutting_down) {		rc = -ENODEV;		goto error;	} 	if (!dev->dev_up) {		rc = -ENODEV;		goto error;	} 	if (dev->polling) {		rc = -EBUSY;		goto error;	} 	rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);	if (!rc) {		dev->polling = true;		dev->rf_mode = NFC_RF_NONE;	} error:	device_unlock(&dev->dev);	return rc;} /** * nfc_stop_poll - stop polling for nfc targets * * @dev: The nfc device that must stop polling */int nfc_stop_poll(struct nfc_dev *dev){	int rc = 0; 	pr_debug("dev_name=%s\n", dev_name(&dev->dev)); 	device_lock(&dev->dev); 	if (dev->shutting_down) {		rc = -ENODEV;		goto error;	} 	if (!dev->polling) {		rc = -EINVAL;		goto error;	} 	dev->ops->stop_poll(dev);	dev->polling = false;	dev->rf_mode = NFC_RF_NONE; error:	device_unlock(&dev->dev);	return rc;} static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx){	int i; 	for (i = 0; i < dev->n_targets; i++) {		if (dev->targets[i].idx == target_idx)			return &dev->targets[i];	} 	return NULL;} int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode){	int rc = 0;	u8 *gb;	size_t gb_len;	struct nfc_target *target; 	pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode); 	if (!dev->ops->dep_link_up)		return -EOPNOTSUPP; 	device_lock(&dev->dev); 	if (dev->shutting_down) {		rc = -ENODEV;		goto error;	} 	if (dev->dep_link_up == true) {		rc = -EALREADY;		goto error;	} 	gb = nfc_llcp_general_bytes(dev, &gb_len);	if (gb_len > NFC_MAX_GT_LEN) {		rc = -EINVAL;		goto error;	} 	target = nfc_find_target(dev, target_index);	if (target == NULL) {		rc = -ENOTCONN;		goto error;	} 	rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);	if (!rc) {		dev->active_target = target;		dev->rf_mode = NFC_RF_INITIATOR;	} error:	device_unlock(&dev->dev);	return rc;} int nfc_dep_link_down(struct nfc_dev *dev){	int rc = 0; 	pr_debug("dev_name=%s\n", dev_name(&dev->dev)); 	if (!dev->ops->dep_link_down)		return -EOPNOTSUPP; 	device_lock(&dev->dev); 	if (dev->shutting_down) {		rc = -ENODEV;		goto error;	} 	if (dev->dep_link_up == false) {		rc = -EALREADY;		goto error;	} 	rc = dev->ops->dep_link_down(dev);	if (!rc) {		dev->dep_link_up = false;		dev->active_target = NULL;		dev->rf_mode = NFC_RF_NONE;		nfc_llcp_mac_is_down(dev);		nfc_genl_dep_link_down_event(dev);	} error:	device_unlock(&dev->dev); 	return rc;} int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,		       u8 comm_mode, u8 rf_mode){	dev->dep_link_up = true; 	if (!dev->active_target && rf_mode == NFC_RF_INITIATOR) {		struct nfc_target *target; 		target = nfc_find_target(dev, target_idx);		if (target == NULL)			return -ENOTCONN; 		dev->active_target = target;	} 	dev->polling = false;	dev->rf_mode = rf_mode; 	nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode); 	return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);}EXPORT_SYMBOL(nfc_dep_link_is_up); /** * nfc_activate_target - prepare the target for data exchange * * @dev: The nfc device that found the target * @target_idx: index of the target that must be activated * @protocol: nfc protocol that will be used for data exchange */int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol){	int rc;	struct nfc_target *target; 	pr_debug("dev_name=%s target_idx=%u protocol=%u\n",		 dev_name(&dev->dev), target_idx, protocol); 	device_lock(&dev->dev); 	if (dev->shutting_down) {		rc = -ENODEV;		goto error;	} 	if (dev->active_target) {		rc = -EBUSY;		goto error;	} 	target = nfc_find_target(dev, target_idx);	if (target == NULL) {		rc = -ENOTCONN;		goto error;	} 	rc = dev->ops->activate_target(dev, target, protocol);	if (!rc) {		dev->active_target = target;		dev->rf_mode = NFC_RF_INITIATOR; 		if (dev->ops->check_presence && !dev->shutting_down)			mod_timer(&dev->check_pres_timer, jiffies +				  msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));	} error:	device_unlock(&dev->dev);	return rc;} /** * nfc_deactivate_target - deactivate a nfc target * * @dev: The nfc device that found the target * @target_idx: index of the target that must be deactivated * @mode: idle or sleep? */int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx, u8 mode){	int rc = 0; 	pr_debug("dev_name=%s target_idx=%u\n",		 dev_name(&dev->dev), target_idx); 	device_lock(&dev->dev); 	if (dev->shutting_down) {		rc = -ENODEV;		goto error;	} 	if (dev->active_target == NULL) {		rc = -ENOTCONN;		goto error;	} 	if (dev->active_target->idx != target_idx) {		rc = -ENOTCONN;		goto error;	} 	if (dev->ops->check_presence)		timer_delete_sync(&dev->check_pres_timer); 	dev->ops->deactivate_target(dev, dev->active_target, mode);	dev->active_target = NULL; error:	device_unlock(&dev->dev);	return rc;} /** * nfc_data_exchange - transceive data * * @dev: The nfc device that found the target * @target_idx: index of the target * @skb: data to be sent * @cb: callback called when the response is received * @cb_context: parameter for the callback function * * The user must wait for the callback before calling this function again. */int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,		      data_exchange_cb_t cb, void *cb_context){	int rc; 	pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",		 dev_name(&dev->dev), target_idx, skb->len); 	device_lock(&dev->dev); 	if (dev->shutting_down) {		rc = -ENODEV;		kfree_skb(skb);

Lines 1–500 of 1273.