Commit 9674da87 authored by Eric Lapuyade's avatar Eric Lapuyade Committed by Samuel Ortiz

NFC: Add firmware upload netlink command

As several NFC chipsets can have their firmwares upgraded and
reflashed, this patchset adds a new netlink command to trigger
that the driver loads or flashes a new firmware. This will allows
userspace triggered firmware upgrade through netlink.
The firmware name or hint is passed as a parameter, and the driver
will eventually fetch the firmware binary through the request_firmware
API.
The cmd can only be executed when the nfc dev is not in use. Actual
firmware loading/flashing is an asynchronous operation. Result of the
operation shall send a new event up to user space through the nfc dev
multicast socket. During operation, the nfc dev is not openable and
thus not usable.
Signed-off-by: default avatarEric Lapuyade <eric.lapuyade@intel.com>
Signed-off-by: default avatarSamuel Ortiz <sameo@linux.intel.com>
parent 1095e69f
...@@ -70,6 +70,7 @@ struct nfc_ops { ...@@ -70,6 +70,7 @@ struct nfc_ops {
int (*check_presence)(struct nfc_dev *dev, struct nfc_target *target); int (*check_presence)(struct nfc_dev *dev, struct nfc_target *target);
int (*enable_se)(struct nfc_dev *dev, u32 secure_element); int (*enable_se)(struct nfc_dev *dev, u32 secure_element);
int (*disable_se)(struct nfc_dev *dev, u32 secure_element); int (*disable_se)(struct nfc_dev *dev, u32 secure_element);
int (*fw_upload)(struct nfc_dev *dev, const char *firmware_name);
}; };
#define NFC_TARGET_IDX_ANY -1 #define NFC_TARGET_IDX_ANY -1
...@@ -104,6 +105,7 @@ struct nfc_dev { ...@@ -104,6 +105,7 @@ struct nfc_dev {
int targets_generation; int targets_generation;
struct device dev; struct device dev;
bool dev_up; bool dev_up;
bool fw_upload_in_progress;
u8 rf_mode; u8 rf_mode;
bool polling; bool polling;
struct nfc_target *active_target; struct nfc_target *active_target;
......
...@@ -69,6 +69,8 @@ ...@@ -69,6 +69,8 @@
* starting a poll from a device which has a secure element enabled means * starting a poll from a device which has a secure element enabled means
* we want to do SE based card emulation. * we want to do SE based card emulation.
* @NFC_CMD_DISABLE_SE: Disable the physical link to a specific secure element. * @NFC_CMD_DISABLE_SE: Disable the physical link to a specific secure element.
* @NFC_CMD_FW_UPLOAD: Request to Load/flash firmware, or event to inform that
* some firmware was loaded
*/ */
enum nfc_commands { enum nfc_commands {
NFC_CMD_UNSPEC, NFC_CMD_UNSPEC,
...@@ -92,6 +94,7 @@ enum nfc_commands { ...@@ -92,6 +94,7 @@ enum nfc_commands {
NFC_CMD_DISABLE_SE, NFC_CMD_DISABLE_SE,
NFC_CMD_LLC_SDREQ, NFC_CMD_LLC_SDREQ,
NFC_EVENT_LLC_SDRES, NFC_EVENT_LLC_SDRES,
NFC_CMD_FW_UPLOAD,
/* private: internal use only */ /* private: internal use only */
__NFC_CMD_AFTER_LAST __NFC_CMD_AFTER_LAST
}; };
...@@ -121,6 +124,7 @@ enum nfc_commands { ...@@ -121,6 +124,7 @@ enum nfc_commands {
* @NFC_ATTR_LLC_PARAM_RW: Receive Window size parameter * @NFC_ATTR_LLC_PARAM_RW: Receive Window size parameter
* @NFC_ATTR_LLC_PARAM_MIUX: MIU eXtension parameter * @NFC_ATTR_LLC_PARAM_MIUX: MIU eXtension parameter
* @NFC_ATTR_SE: Available Secure Elements * @NFC_ATTR_SE: Available Secure Elements
* @NFC_ATTR_FIRMWARE_NAME: Free format firmware version
*/ */
enum nfc_attrs { enum nfc_attrs {
NFC_ATTR_UNSPEC, NFC_ATTR_UNSPEC,
...@@ -143,6 +147,7 @@ enum nfc_attrs { ...@@ -143,6 +147,7 @@ enum nfc_attrs {
NFC_ATTR_LLC_PARAM_MIUX, NFC_ATTR_LLC_PARAM_MIUX,
NFC_ATTR_SE, NFC_ATTR_SE,
NFC_ATTR_LLC_SDP, NFC_ATTR_LLC_SDP,
NFC_ATTR_FIRMWARE_NAME,
/* private: internal use only */ /* private: internal use only */
__NFC_ATTR_AFTER_LAST __NFC_ATTR_AFTER_LAST
}; };
...@@ -162,6 +167,7 @@ enum nfc_sdp_attr { ...@@ -162,6 +167,7 @@ enum nfc_sdp_attr {
#define NFC_SENSB_RES_MAXSIZE 12 #define NFC_SENSB_RES_MAXSIZE 12
#define NFC_SENSF_RES_MAXSIZE 18 #define NFC_SENSF_RES_MAXSIZE 18
#define NFC_GB_MAXSIZE 48 #define NFC_GB_MAXSIZE 48
#define NFC_FIRMWARE_NAME_MAXSIZE 32
/* NFC protocols */ /* NFC protocols */
#define NFC_PROTO_JEWEL 1 #define NFC_PROTO_JEWEL 1
......
...@@ -44,6 +44,47 @@ DEFINE_MUTEX(nfc_devlist_mutex); ...@@ -44,6 +44,47 @@ DEFINE_MUTEX(nfc_devlist_mutex);
/* NFC device ID bitmap */ /* NFC device ID bitmap */
static DEFINE_IDA(nfc_index_ida); static DEFINE_IDA(nfc_index_ida);
int nfc_fw_upload(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_upload) {
rc = -EOPNOTSUPP;
goto error;
}
dev->fw_upload_in_progress = true;
rc = dev->ops->fw_upload(dev, firmware_name);
if (rc)
dev->fw_upload_in_progress = false;
error:
device_unlock(&dev->dev);
return rc;
}
int nfc_fw_upload_done(struct nfc_dev *dev, const char *firmware_name)
{
dev->fw_upload_in_progress = false;
return nfc_genl_fw_upload_done(dev, firmware_name);
}
EXPORT_SYMBOL(nfc_fw_upload_done);
/** /**
* nfc_dev_up - turn on the NFC device * nfc_dev_up - turn on the NFC device
* *
...@@ -69,6 +110,11 @@ int nfc_dev_up(struct nfc_dev *dev) ...@@ -69,6 +110,11 @@ int nfc_dev_up(struct nfc_dev *dev)
goto error; goto error;
} }
if (dev->fw_upload_in_progress) {
rc = -EBUSY;
goto error;
}
if (dev->dev_up) { if (dev->dev_up) {
rc = -EALREADY; rc = -EALREADY;
goto error; goto error;
......
...@@ -56,6 +56,8 @@ static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = { ...@@ -56,6 +56,8 @@ static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
[NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 }, [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
[NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 }, [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
[NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED }, [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
[NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
.len = NFC_FIRMWARE_NAME_MAXSIZE },
}; };
static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = { static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
...@@ -1025,6 +1027,62 @@ static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info) ...@@ -1025,6 +1027,62 @@ static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
return rc; return rc;
} }
static int nfc_genl_fw_upload(struct sk_buff *skb, struct genl_info *info)
{
struct nfc_dev *dev;
int rc;
u32 idx;
char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
return -EINVAL;
idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
dev = nfc_get_device(idx);
if (!dev)
return -ENODEV;
nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
sizeof(firmware_name));
rc = nfc_fw_upload(dev, firmware_name);
nfc_put_device(dev);
return rc;
}
int nfc_genl_fw_upload_done(struct nfc_dev *dev, const char *firmware_name)
{
struct sk_buff *msg;
void *hdr;
msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
if (!msg)
return -ENOMEM;
hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
NFC_CMD_FW_UPLOAD);
if (!hdr)
goto free_msg;
if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
goto nla_put_failure;
genlmsg_end(msg, hdr);
genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
return 0;
nla_put_failure:
genlmsg_cancel(msg, hdr);
free_msg:
nlmsg_free(msg);
return -EMSGSIZE;
}
static struct genl_ops nfc_genl_ops[] = { static struct genl_ops nfc_genl_ops[] = {
{ {
.cmd = NFC_CMD_GET_DEVICE, .cmd = NFC_CMD_GET_DEVICE,
...@@ -1084,6 +1142,11 @@ static struct genl_ops nfc_genl_ops[] = { ...@@ -1084,6 +1142,11 @@ static struct genl_ops nfc_genl_ops[] = {
.doit = nfc_genl_llc_sdreq, .doit = nfc_genl_llc_sdreq,
.policy = nfc_genl_policy, .policy = nfc_genl_policy,
}, },
{
.cmd = NFC_CMD_FW_UPLOAD,
.doit = nfc_genl_fw_upload,
.policy = nfc_genl_policy,
},
}; };
......
...@@ -120,6 +120,11 @@ static inline void nfc_device_iter_exit(struct class_dev_iter *iter) ...@@ -120,6 +120,11 @@ static inline void nfc_device_iter_exit(struct class_dev_iter *iter)
class_dev_iter_exit(iter); class_dev_iter_exit(iter);
} }
int nfc_fw_upload(struct nfc_dev *dev, const char *firmware_name);
int nfc_genl_fw_upload_done(struct nfc_dev *dev, const char *firmware_name);
int nfc_fw_upload_done(struct nfc_dev *dev, const char *firmware_name);
int nfc_dev_up(struct nfc_dev *dev); int nfc_dev_up(struct nfc_dev *dev);
int nfc_dev_down(struct nfc_dev *dev); int nfc_dev_down(struct nfc_dev *dev);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment