/*
* 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/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 (!device_is_registered(&dev->dev)) {
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->rfkill && rfkill_blocked(dev->rfkill)) {
rc = -ERFKILL;
goto error;
}
if (!device_is_registered(&dev->dev)) {
rc = -ENODEV;
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)