main.c 6.7 KB
Newer Older
1 2 3
/*
 * Marvell NFC driver: major functions
 *
4
 * Copyright (C) 2014-2015 Marvell International Ltd.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This software file (the "File") is distributed by Marvell International
 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
 * (the "License").  You may use, redistribute and/or modify this File in
 * accordance with the terms and conditions of the License, a copy of which
 * is available on the worldwide web at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
 *
 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
 * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
 * this warranty disclaimer.
 */

#include <linux/module.h>
20 21
#include <linux/gpio.h>
#include <linux/delay.h>
22
#include <linux/of_gpio.h>
23 24 25 26 27 28 29 30 31 32 33 34 35
#include <linux/nfc.h>
#include <net/nfc/nci.h>
#include <net/nfc/nci_core.h>
#include "nfcmrvl.h"

static int nfcmrvl_nci_open(struct nci_dev *ndev)
{
	struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
	int err;

	if (test_and_set_bit(NFCMRVL_NCI_RUNNING, &priv->flags))
		return 0;

V
Vincent Cuissard 已提交
36 37 38
	/* Reset possible fault of previous session */
	clear_bit(NFCMRVL_PHY_ERROR, &priv->flags);

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
	err = priv->if_ops->nci_open(priv);

	if (err)
		clear_bit(NFCMRVL_NCI_RUNNING, &priv->flags);

	return err;
}

static int nfcmrvl_nci_close(struct nci_dev *ndev)
{
	struct nfcmrvl_private *priv = nci_get_drvdata(ndev);

	if (!test_and_clear_bit(NFCMRVL_NCI_RUNNING, &priv->flags))
		return 0;

	priv->if_ops->nci_close(priv);

	return 0;
}

static int nfcmrvl_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
{
	struct nfcmrvl_private *priv = nci_get_drvdata(ndev);

	nfc_info(priv->dev, "send entry, len %d\n", skb->len);

	skb->dev = (void *)ndev;

67
	if (priv->config.hci_muxed) {
68 69 70 71 72 73 74 75 76 77
		unsigned char *hdr;
		unsigned char len = skb->len;

		hdr = (char *) skb_push(skb, NFCMRVL_HCI_EVENT_HEADER_SIZE);
		hdr[0] = NFCMRVL_HCI_COMMAND_CODE;
		hdr[1] = NFCMRVL_HCI_OGF;
		hdr[2] = NFCMRVL_HCI_OCF;
		hdr[3] = len;
	}

78 79 80
	return priv->if_ops->nci_send(priv, skb);
}

81 82
static int nfcmrvl_nci_setup(struct nci_dev *ndev)
{
83 84 85
	__u8 val = 1;

	nci_set_config(ndev, NFCMRVL_PB_BAIL_OUT, 1, &val);
86 87 88
	return 0;
}

89 90 91 92 93 94
static int nfcmrvl_nci_fw_download(struct nci_dev *ndev,
				   const char *firmware_name)
{
	return nfcmrvl_fw_dnld_start(ndev, firmware_name);
}

95 96 97 98
static struct nci_ops nfcmrvl_nci_ops = {
	.open = nfcmrvl_nci_open,
	.close = nfcmrvl_nci_close,
	.send = nfcmrvl_nci_send,
99
	.setup = nfcmrvl_nci_setup,
100
	.fw_download = nfcmrvl_nci_fw_download,
101 102
};

103 104
struct nfcmrvl_private *nfcmrvl_nci_register_dev(enum nfcmrvl_phy phy,
				void *drv_data,
105 106 107
				struct nfcmrvl_if_ops *ops,
				struct device *dev,
				struct nfcmrvl_platform_data *pdata)
108 109 110
{
	struct nfcmrvl_private *priv;
	int rc;
111 112
	int headroom;
	int tailroom;
113 114 115 116 117 118 119 120 121
	u32 protocols;

	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return ERR_PTR(-ENOMEM);

	priv->drv_data = drv_data;
	priv->if_ops = ops;
	priv->dev = dev;
122
	priv->phy = phy;
123

124 125 126
	memcpy(&priv->config, pdata, sizeof(*pdata));

	if (priv->config.reset_n_io) {
127
		rc = devm_gpio_request_one(dev,
128
					   priv->config.reset_n_io,
129 130 131 132 133
					   GPIOF_OUT_INIT_LOW,
					   "nfcmrvl_reset_n");
		if (rc < 0)
			nfc_err(dev, "failed to request reset_n io\n");
	}
134

V
Vincent Cuissard 已提交
135 136 137 138 139
	if (phy == NFCMRVL_PHY_SPI) {
		headroom = NCI_SPI_HDR_LEN;
		tailroom = 1;
	} else
		headroom = tailroom = 0;
140

141
	if (priv->config.hci_muxed)
142
		headroom += NFCMRVL_HCI_EVENT_HEADER_SIZE;
143 144

	protocols = NFC_PROTO_JEWEL_MASK
145 146
		| NFC_PROTO_MIFARE_MASK
		| NFC_PROTO_FELICA_MASK
147 148
		| NFC_PROTO_ISO14443_MASK
		| NFC_PROTO_ISO14443_B_MASK
149
		| NFC_PROTO_ISO15693_MASK
150 151
		| NFC_PROTO_NFC_DEP_MASK;

152
	priv->ndev = nci_allocate_device(&nfcmrvl_nci_ops, protocols,
153
					 headroom, tailroom);
154
	if (!priv->ndev) {
J
Joe Perches 已提交
155
		nfc_err(dev, "nci_allocate_device failed\n");
156 157
		rc = -ENOMEM;
		goto error;
158 159 160 161 162 163
	}

	nci_set_drvdata(priv->ndev, priv);

	rc = nci_register_device(priv->ndev);
	if (rc) {
J
Joe Perches 已提交
164
		nfc_err(dev, "nci_register_device failed %d\n", rc);
165 166 167 168 169 170 171 172 173 174
		goto error_free_dev;
	}

	/* Ensure that controller is powered off */
	nfcmrvl_chip_halt(priv);

	rc = nfcmrvl_fw_dnld_init(priv);
	if (rc) {
		nfc_err(dev, "failed to initialize FW download %d\n", rc);
		goto error_free_dev;
175 176 177 178
	}

	nfc_info(dev, "registered with nci successfully\n");
	return priv;
179

180 181
error_free_dev:
	nci_free_device(priv->ndev);
182 183 184
error:
	kfree(priv);
	return ERR_PTR(rc);
185 186 187 188 189 190 191
}
EXPORT_SYMBOL_GPL(nfcmrvl_nci_register_dev);

void nfcmrvl_nci_unregister_dev(struct nfcmrvl_private *priv)
{
	struct nci_dev *ndev = priv->ndev;

192 193 194 195 196
	if (priv->ndev->nfc_dev->fw_download_in_progress)
		nfcmrvl_fw_dnld_abort(priv);

	nfcmrvl_fw_dnld_deinit(priv);

197 198 199 200 201 202
	nci_unregister_device(ndev);
	nci_free_device(ndev);
	kfree(priv);
}
EXPORT_SYMBOL_GPL(nfcmrvl_nci_unregister_dev);

203
int nfcmrvl_nci_recv_frame(struct nfcmrvl_private *priv, struct sk_buff *skb)
204
{
205
	if (priv->config.hci_muxed) {
206 207 208 209 210 211 212 213 214 215 216
		if (skb->data[0] == NFCMRVL_HCI_EVENT_CODE &&
		    skb->data[1] == NFCMRVL_HCI_NFC_EVENT_CODE) {
			/* Data packet, let's extract NCI payload */
			skb_pull(skb, NFCMRVL_HCI_EVENT_HEADER_SIZE);
		} else {
			/* Skip this packet */
			kfree_skb(skb);
			return 0;
		}
	}

217 218 219 220 221
	if (priv->ndev->nfc_dev->fw_download_in_progress) {
		nfcmrvl_fw_dnld_recv_frame(priv, skb);
		return 0;
	}

222 223 224 225 226 227 228
	if (test_bit(NFCMRVL_NCI_RUNNING, &priv->flags))
		nci_recv_frame(priv->ndev, skb);
	else {
		/* Drop this packet since nobody wants it */
		kfree_skb(skb);
		return 0;
	}
229

230
	return 0;
231 232 233
}
EXPORT_SYMBOL_GPL(nfcmrvl_nci_recv_frame);

234 235
void nfcmrvl_chip_reset(struct nfcmrvl_private *priv)
{
V
Vincent Cuissard 已提交
236 237
	/* Reset possible fault of previous session */
	clear_bit(NFCMRVL_PHY_ERROR, &priv->flags);
238

239
	if (priv->config.reset_n_io) {
240
		nfc_info(priv->dev, "reset the chip\n");
241
		gpio_set_value(priv->config.reset_n_io, 0);
242
		usleep_range(5000, 10000);
243
		gpio_set_value(priv->config.reset_n_io, 1);
244 245 246 247
	} else
		nfc_info(priv->dev, "no reset available on this interface\n");
}

248 249 250 251 252 253
void nfcmrvl_chip_halt(struct nfcmrvl_private *priv)
{
	if (priv->config.reset_n_io)
		gpio_set_value(priv->config.reset_n_io, 0);
}

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
#ifdef CONFIG_OF

int nfcmrvl_parse_dt(struct device_node *node,
		     struct nfcmrvl_platform_data *pdata)
{
	int reset_n_io;

	reset_n_io = of_get_named_gpio(node, "reset-n-io", 0);
	if (reset_n_io < 0) {
		pr_info("no reset-n-io config\n");
		reset_n_io = 0;
	} else if (!gpio_is_valid(reset_n_io)) {
		pr_err("invalid reset-n-io GPIO\n");
		return reset_n_io;
	}
	pdata->reset_n_io = reset_n_io;

	if (of_find_property(node, "hci-muxed", NULL))
		pdata->hci_muxed = 1;
	else
		pdata->hci_muxed = 0;

	return 0;
}

#else

int nfcmrvl_parse_dt(struct device_node *node,
		     struct nfcmrvl_platform_data *pdata)
{
	return -ENODEV;
}

#endif
EXPORT_SYMBOL_GPL(nfcmrvl_parse_dt);

290
MODULE_AUTHOR("Marvell International Ltd.");
291
MODULE_DESCRIPTION("Marvell NFC driver");
292
MODULE_LICENSE("GPL v2");