linux-wireless.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] NFC: Download TI NFC init script
@ 2012-01-11 12:42 ilanelias78
  2012-01-11 12:49 ` Johannes Berg
  2012-01-11 13:57 ` Ohad Ben-Cohen
  0 siblings, 2 replies; 5+ messages in thread
From: ilanelias78 @ 2012-01-11 12:42 UTC (permalink / raw)
  To: samuel, lauro.venancio, aloisio.almeida, linville
  Cc: linux-wireless, Ilan Elias

From: Ilan Elias <ilane@ti.com>

Download TI NFC init script during nfcwilink open operation,
after the NFC channel is registered with TI shared transport.
TI NFC init script is written in BTS format.
First, read the chip version via a special vendor specific command.
Second, we request the relevant BTS file from the user space, and
then send the BTS commands to the chip.

Signed-off-by: Ilan Elias <ilane@ti.com>
---
 drivers/nfc/nfcwilink.c |  290 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 284 insertions(+), 6 deletions(-)

diff --git a/drivers/nfc/nfcwilink.c b/drivers/nfc/nfcwilink.c
index 06c3642..50e5c67 100644
--- a/drivers/nfc/nfcwilink.c
+++ b/drivers/nfc/nfcwilink.c
@@ -28,6 +28,7 @@
  */
 #include <linux/platform_device.h>
 #include <linux/module.h>
+#include <linux/firmware.h>
 #include <linux/nfc.h>
 #include <net/nfc/nci.h>
 #include <net/nfc/nci_core.h>
@@ -40,6 +41,17 @@
 #define NFCWILINK_OFFSET_LEN_IN_HDR	1
 #define NFCWILINK_LEN_SIZE		2
 #define NFCWILINK_REGISTER_TIMEOUT	8000	/* 8 sec */
+#define NFCWILINK_CMD_TIMEOUT		5000	/* 5 sec */
+
+#define BTS_FILE_NAME_MAX_SIZE		40
+#define BTS_FILE_HDR_MAGIC		0x42535442
+#define BTS_FILE_CMD_MAX_LEN		0xff
+#define BTS_FILE_ACTION_TYPE_SEND_CMD	1
+
+#define NCI_VS_NFCC_INFO_CMD_GID	0x2f
+#define NCI_VS_NFCC_INFO_CMD_OID	0x12
+#define NCI_VS_NFCC_INFO_RSP_GID	0x4f
+#define NCI_VS_NFCC_INFO_RSP_OID	0x12
 
 struct nfcwilink_hdr {
 	u8 chnl;
@@ -47,6 +59,36 @@ struct nfcwilink_hdr {
 	u16 len;
 } __packed;
 
+struct nci_vs_nfcc_info_cmd {
+	u8 gid;
+	u8 oid;
+	u8 plen;
+} __packed;
+
+struct nci_vs_nfcc_info_rsp {
+	u8 gid;
+	u8 oid;
+	u8 plen;
+	u8 status;
+	u8 hw_id;
+	u8 sw_ver_x;
+	u8 sw_ver_z;
+	u8 patch_id;
+} __packed;
+
+struct bts_file_hdr {
+	u32 magic;
+	u32 ver;
+	u8 rfu[24];
+	u8 actions[0];
+} __packed;
+
+struct bts_file_action {
+	u16 type;
+	u16 len;
+	u8 data[0];
+} __packed;
+
 struct nfcwilink {
 	struct platform_device		*pdev;
 	struct nci_dev			*ndev;
@@ -54,14 +96,237 @@ struct nfcwilink {
 
 	char				st_register_cb_status;
 	long				(*st_write) (struct sk_buff *);
-	struct completion		st_register_completed;
+
+	struct completion		completed;
+
+	struct nci_vs_nfcc_info_rsp	nfcc_info;
 };
 
 /* NFCWILINK driver flags */
 enum {
 	NFCWILINK_RUNNING,
+	NFCWILINK_FW_DOWNLOAD,
 };
 
+static int nfcwilink_send(struct sk_buff *skb);
+
+static inline struct sk_buff *nfcwilink_skb_alloc(unsigned int len, gfp_t how)
+{
+	struct sk_buff *skb;
+
+	skb = alloc_skb(len + NFCWILINK_HDR_LEN, how);
+	if (skb)
+		skb_reserve(skb, NFCWILINK_HDR_LEN);
+
+	return skb;
+}
+
+static void nfcwilink_fw_download_receive(struct nfcwilink *drv,
+						struct sk_buff *skb)
+{
+	struct nci_vs_nfcc_info_rsp *rsp = (void *)skb->data;
+
+	/* Detect NCI_VS_NFCC_INFO_RSP and store the result */
+	if ((skb->len > 3) && (rsp->gid == NCI_VS_NFCC_INFO_RSP_GID) &&
+		(rsp->oid == NCI_VS_NFCC_INFO_RSP_OID)) {
+		memcpy(&drv->nfcc_info, rsp,
+			sizeof(struct nci_vs_nfcc_info_rsp));
+	}
+
+	kfree_skb(skb);
+
+	complete(&drv->completed);
+}
+
+static int nfcwilink_get_bts_file_name(struct nfcwilink *drv, char *file_name)
+{
+	struct nci_vs_nfcc_info_cmd *cmd;
+	struct sk_buff *skb;
+	unsigned long comp_ret;
+	int rc;
+
+	nfc_dev_dbg(&drv->pdev->dev, "get_bts_file_name entry");
+
+	skb = nfcwilink_skb_alloc(sizeof(struct nci_vs_nfcc_info_cmd),
+					GFP_KERNEL);
+	if (!skb) {
+		nfc_dev_err(&drv->pdev->dev,
+				"no memory for nci_vs_nfcc_info_cmd");
+		return -ENOMEM;
+	}
+
+	skb->dev = (void *)drv->ndev;
+
+	cmd = (struct nci_vs_nfcc_info_cmd *)
+			skb_put(skb, sizeof(struct nci_vs_nfcc_info_cmd));
+	cmd->gid = NCI_VS_NFCC_INFO_CMD_GID;
+	cmd->oid = NCI_VS_NFCC_INFO_CMD_OID;
+	cmd->plen = 0;
+
+	drv->nfcc_info.plen = 0;
+
+	rc = nfcwilink_send(skb);
+	if (rc)
+		return rc;
+
+	comp_ret = wait_for_completion_timeout(&drv->completed,
+				msecs_to_jiffies(NFCWILINK_CMD_TIMEOUT));
+	nfc_dev_dbg(&drv->pdev->dev, "wait_for_completion_timeout returned %ld",
+			comp_ret);
+	if (comp_ret == 0) {
+		nfc_dev_err(&drv->pdev->dev,
+				"timeout on wait_for_completion_timeout");
+		return -ETIMEDOUT;
+	}
+
+	nfc_dev_dbg(&drv->pdev->dev, "nci_vs_nfcc_info_rsp: plen %d, status %d",
+			drv->nfcc_info.plen,
+			drv->nfcc_info.status);
+
+	if ((drv->nfcc_info.plen != 5) || (drv->nfcc_info.status != 0)) {
+		nfc_dev_err(&drv->pdev->dev,
+				"invalid nci_vs_nfcc_info_rsp");
+		return -EINVAL;
+	}
+
+	snprintf(file_name, BTS_FILE_NAME_MAX_SIZE,
+			"TINfcInit_%d.%d.%d.%d.bts",
+			drv->nfcc_info.hw_id,
+			drv->nfcc_info.sw_ver_x,
+			drv->nfcc_info.sw_ver_z,
+			drv->nfcc_info.patch_id);
+
+	nfc_dev_info(&drv->pdev->dev, "nfcwilink FW file name: %s", file_name);
+
+	return 0;
+}
+
+static int nfcwilink_send_bts_cmd(struct nfcwilink *drv, u8 *data, int len)
+{
+	struct nfcwilink_hdr *hdr = (struct nfcwilink_hdr *)data;
+	struct sk_buff *skb;
+	unsigned long comp_ret;
+	int rc;
+
+	nfc_dev_dbg(&drv->pdev->dev, "send_bts_cmd entry");
+
+	/* verify valid cmd for the NFC channel */
+	if ((len <= sizeof(struct nfcwilink_hdr)) ||
+		(len > BTS_FILE_CMD_MAX_LEN) ||
+		(hdr->chnl != NFCWILINK_CHNL) ||
+		(hdr->opcode != NFCWILINK_OPCODE)) {
+		nfc_dev_err(&drv->pdev->dev,
+			"ignoring invalid bts cmd, len %d, chnl %d, opcode %d",
+			len, hdr->chnl, hdr->opcode);
+		return 0;
+	}
+
+	/* remove the ST header */
+	len -= sizeof(struct nfcwilink_hdr);
+	data += sizeof(struct nfcwilink_hdr);
+
+	skb = nfcwilink_skb_alloc(len, GFP_KERNEL);
+	if (!skb) {
+		nfc_dev_err(&drv->pdev->dev, "no memory for bts cmd");
+		return -ENOMEM;
+	}
+
+	skb->dev = (void *)drv->ndev;
+
+	memcpy(skb_put(skb, len), data, len);
+
+	rc = nfcwilink_send(skb);
+	if (rc)
+		return rc;
+
+	comp_ret = wait_for_completion_timeout(&drv->completed,
+				msecs_to_jiffies(NFCWILINK_CMD_TIMEOUT));
+	nfc_dev_dbg(&drv->pdev->dev, "wait_for_completion_timeout returned %ld",
+			comp_ret);
+	if (comp_ret == 0) {
+		nfc_dev_err(&drv->pdev->dev,
+				"timeout on wait_for_completion_timeout");
+		return -ETIMEDOUT;
+	}
+
+	return 0;
+}
+
+static int nfcwilink_download_fw(struct nfcwilink *drv)
+{
+	unsigned char file_name[BTS_FILE_NAME_MAX_SIZE];
+	const struct firmware *fw;
+	struct bts_file_action *action;
+	int len;
+	u8 *ptr;
+	int rc;
+
+	nfc_dev_dbg(&drv->pdev->dev, "download_fw entry");
+
+	set_bit(NFCWILINK_FW_DOWNLOAD, &drv->flags);
+
+	rc = nfcwilink_get_bts_file_name(drv, file_name);
+	if (rc)
+		goto exit;
+
+	rc = request_firmware(&fw, file_name, &drv->pdev->dev);
+	if (rc) {
+		nfc_dev_err(&drv->pdev->dev, "request_firmware failed %d", rc);
+
+		/* if the file is not found, don't exit with failure */
+		if (rc == -ENOENT)
+			rc = 0;
+
+		goto exit;
+	}
+
+	len = fw->size;
+	ptr = (u8 *)fw->data;
+
+	if ((len == 0) || (ptr == NULL)) {
+		nfc_dev_dbg(&drv->pdev->dev,
+				"request_firmware returned size %d", len);
+		goto release_fw;
+	}
+
+	if (((struct bts_file_hdr *)ptr)->magic != BTS_FILE_HDR_MAGIC) {
+		nfc_dev_err(&drv->pdev->dev, "wrong bts magic number");
+		rc = -EINVAL;
+		goto release_fw;
+	}
+
+	/* remove the BTS header */
+	len -= sizeof(struct bts_file_hdr);
+	ptr += sizeof(struct bts_file_hdr);
+
+	while (len > 0) {
+		action = (struct bts_file_action *)ptr;
+		nfc_dev_dbg(&drv->pdev->dev, "bts_file_action type %d, len %d",
+				action->type,
+				action->len);
+
+		switch (action->type) {
+		case BTS_FILE_ACTION_TYPE_SEND_CMD:
+			rc = nfcwilink_send_bts_cmd(drv, action->data,
+							action->len);
+			if (rc)
+				goto release_fw;
+			break;
+		}
+
+		/* advance to the next action */
+		len -= (sizeof(struct bts_file_action) + action->len);
+		ptr += (sizeof(struct bts_file_action) + action->len);
+	}
+
+release_fw:
+	release_firmware(fw);
+
+exit:
+	clear_bit(NFCWILINK_FW_DOWNLOAD, &drv->flags);
+	return rc;
+}
+
 /* Called by ST when registration is complete */
 static void nfcwilink_register_complete(void *priv_data, char data)
 {
@@ -73,7 +338,7 @@ static void nfcwilink_register_complete(void *priv_data, char data)
 	drv->st_register_cb_status = data;
 
 	/* complete the wait in nfc_st_open() */
-	complete(&drv->st_register_completed);
+	complete(&drv->completed);
 }
 
 /* Called by ST when receive data is available */
@@ -96,6 +361,11 @@ static long nfcwilink_receive(void *priv_data, struct sk_buff *skb)
 	(apart for the chnl byte, which is not received in the hdr) */
 	skb_pull(skb, (NFCWILINK_HDR_LEN-1));
 
+	if (test_bit(NFCWILINK_FW_DOWNLOAD, &drv->flags)) {
+		nfcwilink_fw_download_receive(drv, skb);
+		return 0;
+	}
+
 	skb->dev = (void *) drv->ndev;
 
 	/* Forward skb to NCI core layer */
@@ -136,14 +406,14 @@ static int nfcwilink_open(struct nci_dev *ndev)
 
 	nfcwilink_proto.priv_data = drv;
 
-	init_completion(&drv->st_register_completed);
+	init_completion(&drv->completed);
 	drv->st_register_cb_status = -EINPROGRESS;
 
 	rc = st_register(&nfcwilink_proto);
 	if (rc < 0) {
 		if (rc == -EINPROGRESS) {
 			comp_ret = wait_for_completion_timeout(
-			&drv->st_register_completed,
+			&drv->completed,
 			msecs_to_jiffies(NFCWILINK_REGISTER_TIMEOUT));
 
 			nfc_dev_dbg(&drv->pdev->dev,
@@ -171,6 +441,12 @@ static int nfcwilink_open(struct nci_dev *ndev)
 	BUG_ON(nfcwilink_proto.write == NULL);
 	drv->st_write = nfcwilink_proto.write;
 
+	if (nfcwilink_download_fw(drv)) {
+		nfc_dev_err(&drv->pdev->dev, "nfcwilink_download_fw failed %d",
+				rc);
+		/* open should succeed, even if the FW download failed */
+	}
+
 	goto exit;
 
 clear_exit:
@@ -208,8 +484,10 @@ static int nfcwilink_send(struct sk_buff *skb)
 
 	nfc_dev_dbg(&drv->pdev->dev, "send entry, len %d", skb->len);
 
-	if (!test_bit(NFCWILINK_RUNNING, &drv->flags))
-		return -EBUSY;
+	if (!test_bit(NFCWILINK_RUNNING, &drv->flags)) {
+		kfree_skb(skb);
+		return -EINVAL;
+	}
 
 	/* add the ST hdr to the start of the buffer */
 	hdr.len = skb->len;
-- 
1.7.0.4


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] NFC: Download TI NFC init script
  2012-01-11 12:42 [PATCH] NFC: Download TI NFC init script ilanelias78
@ 2012-01-11 12:49 ` Johannes Berg
  2012-01-11 14:15   ` Elias, Ilan
  2012-01-11 13:57 ` Ohad Ben-Cohen
  1 sibling, 1 reply; 5+ messages in thread
From: Johannes Berg @ 2012-01-11 12:49 UTC (permalink / raw)
  To: ilanelias78
  Cc: samuel, lauro.venancio, aloisio.almeida, linville, linux-wireless,
	Ilan Elias

On Wed, 2012-01-11 at 14:42 +0200, ilanelias78@gmail.com wrote:

> @@ -47,6 +59,36 @@ struct nfcwilink_hdr {
>  	u16 len;
>  } __packed;

> +struct bts_file_hdr {
> +	u32 magic;

All this new stuff, but also the old stuff above, seems to be missing
endian annotations.

johannes


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] NFC: Download TI NFC init script
  2012-01-11 12:42 [PATCH] NFC: Download TI NFC init script ilanelias78
  2012-01-11 12:49 ` Johannes Berg
@ 2012-01-11 13:57 ` Ohad Ben-Cohen
  2012-01-11 14:45   ` Elias, Ilan
  1 sibling, 1 reply; 5+ messages in thread
From: Ohad Ben-Cohen @ 2012-01-11 13:57 UTC (permalink / raw)
  To: ilanelias78
  Cc: samuel, lauro.venancio, aloisio.almeida, linville, linux-wireless,
	Ilan Elias

Hi Ilan,

On Wed, Jan 11, 2012 at 2:42 PM,  <ilanelias78@gmail.com> wrote:
> From: Ilan Elias <ilane@ti.com>
>
> Download TI NFC init script during nfcwilink open operation,
> after the NFC channel is registered with TI shared transport.
> TI NFC init script is written in BTS format.
> First, read the chip version via a special vendor specific command.
> Second, we request the relevant BTS file from the user space, and
> then send the BTS commands to the chip.

nfcwilink_open() begins by calling the shared transport's
st_register() function, which also seem to be loading, parsing and
sending a BTS firmware to the device.

Does it mean that two firmwares are needed for these NFC devices ?

>From a quick look it seems that much of the boilerplate code for the
BTS handling is quite similar; any way to avoid some of the code
duplication (something the shared transport code doesn't try very hard
to do ;) ?

Thanks,
Ohad.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: [PATCH] NFC: Download TI NFC init script
  2012-01-11 12:49 ` Johannes Berg
@ 2012-01-11 14:15   ` Elias, Ilan
  0 siblings, 0 replies; 5+ messages in thread
From: Elias, Ilan @ 2012-01-11 14:15 UTC (permalink / raw)
  To: Johannes Berg
  Cc: samuel@sortiz.org, lauro.venancio@openbossa.org,
	aloisio.almeida@openbossa.org, linville@tuxdriver.com,
	linux-wireless@vger.kernel.org

Hi Johannes, 
 
> > @@ -47,6 +59,36 @@ struct nfcwilink_hdr {
> >  	u16 len;
> >  } __packed;
> 
> > +struct bts_file_hdr {
> > +	u32 magic;
> 
> All this new stuff, but also the old stuff above, seems to be missing
> endian annotations.

You're correct.
I'll fix it and send a new patch.

Thanks & BR,
Ilan

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: [PATCH] NFC: Download TI NFC init script
  2012-01-11 13:57 ` Ohad Ben-Cohen
@ 2012-01-11 14:45   ` Elias, Ilan
  0 siblings, 0 replies; 5+ messages in thread
From: Elias, Ilan @ 2012-01-11 14:45 UTC (permalink / raw)
  To: Ohad Ben-Cohen
  Cc: samuel@sortiz.org, lauro.venancio@openbossa.org,
	aloisio.almeida@openbossa.org, linville@tuxdriver.com,
	linux-wireless@vger.kernel.org

 Hi Ohad,

> > Download TI NFC init script during nfcwilink open operation,
> > after the NFC channel is registered with TI shared transport.
> > TI NFC init script is written in BTS format.
> > First, read the chip version via a special vendor specific command.
> > Second, we request the relevant BTS file from the user space, and
> > then send the BTS commands to the chip.
> 
> nfcwilink_open() begins by calling the shared transport's
> st_register() function, which also seem to be loading, parsing and
> sending a BTS firmware to the device.
> 
> Does it mean that two firmwares are needed for these NFC devices ?
Yes, TI NFC chip requires 2 different init scripts:
1) General init script for all cores (HCI commands), which comes first
2) NFC init script only for the NFC core (NCI commands), which comes second
 
> From a quick look it seems that much of the boilerplate code for the
> BTS handling is quite similar; any way to avoid some of the code
> duplication (something the shared transport code doesn't try very hard
> to do ;) ?
There are several major differences between the two:
1) The vesion read in st_kim is the BT chip version, using the standard HCI_Read_Local_Version_Information command. This version is regarded as the general chip version.
The version read in the nfcwilink is a different version using a special vendor specific NCI command. This version is regarded as the NFC chip version.
2) The NFC init script is in NCI commands, while the general init script is in HCI commands.
3) In NFC, we want a simplified BTS init script, e.g. with only ACTION_SEND_COMMAND. No waiting, sleeping or other actions...

To summarize, it's a new core with different commands (NCI and not HCI).
We chose BTS to make it simple and familiar, but it's a simplified BTS, and we handle the actions differently.

Thanks & BR,
Ilan

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-01-11 14:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-01-11 12:42 [PATCH] NFC: Download TI NFC init script ilanelias78
2012-01-11 12:49 ` Johannes Berg
2012-01-11 14:15   ` Elias, Ilan
2012-01-11 13:57 ` Ohad Ben-Cohen
2012-01-11 14:45   ` Elias, Ilan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).