/*
* Copyright (C) 2011 Instituto Nokia de Tecnologia
*
* Authors:
* Lauro Ramos Venancio <lauro.venancio@openbossa.org>
* Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#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/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);
/**
* 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 (!device_is_registered(&dev->dev)) {
rc = -ENODEV;
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;
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 (!device_is_registered(&dev->dev)) {
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;
}
/**
* nfc_start_poll - start polling for nfc targets
*
* @dev: The nfc device that must start polling
* @protocols: bitset of nfc protocols that must 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 (!device_is_registered(&dev->dev)) {
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 (!device_is_registered(&dev->dev)) {
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;
if (dev->n_targets == 0)
return NULL;
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;