Skip to content

Commit 9674da8

Browse files
Eric LapuyadeSamuel Ortiz
authored andcommitted
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: Eric Lapuyade <[email protected]> Signed-off-by: Samuel Ortiz <[email protected]>
1 parent 1095e69 commit 9674da8

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

include/net/nfc/nfc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ struct nfc_ops {
7070
int (*check_presence)(struct nfc_dev *dev, struct nfc_target *target);
7171
int (*enable_se)(struct nfc_dev *dev, u32 secure_element);
7272
int (*disable_se)(struct nfc_dev *dev, u32 secure_element);
73+
int (*fw_upload)(struct nfc_dev *dev, const char *firmware_name);
7374
};
7475

7576
#define NFC_TARGET_IDX_ANY -1
@@ -104,6 +105,7 @@ struct nfc_dev {
104105
int targets_generation;
105106
struct device dev;
106107
bool dev_up;
108+
bool fw_upload_in_progress;
107109
u8 rf_mode;
108110
bool polling;
109111
struct nfc_target *active_target;

include/uapi/linux/nfc.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
* starting a poll from a device which has a secure element enabled means
7070
* we want to do SE based card emulation.
7171
* @NFC_CMD_DISABLE_SE: Disable the physical link to a specific secure element.
72+
* @NFC_CMD_FW_UPLOAD: Request to Load/flash firmware, or event to inform that
73+
* some firmware was loaded
7274
*/
7375
enum nfc_commands {
7476
NFC_CMD_UNSPEC,
@@ -92,6 +94,7 @@ enum nfc_commands {
9294
NFC_CMD_DISABLE_SE,
9395
NFC_CMD_LLC_SDREQ,
9496
NFC_EVENT_LLC_SDRES,
97+
NFC_CMD_FW_UPLOAD,
9598
/* private: internal use only */
9699
__NFC_CMD_AFTER_LAST
97100
};
@@ -121,6 +124,7 @@ enum nfc_commands {
121124
* @NFC_ATTR_LLC_PARAM_RW: Receive Window size parameter
122125
* @NFC_ATTR_LLC_PARAM_MIUX: MIU eXtension parameter
123126
* @NFC_ATTR_SE: Available Secure Elements
127+
* @NFC_ATTR_FIRMWARE_NAME: Free format firmware version
124128
*/
125129
enum nfc_attrs {
126130
NFC_ATTR_UNSPEC,
@@ -143,6 +147,7 @@ enum nfc_attrs {
143147
NFC_ATTR_LLC_PARAM_MIUX,
144148
NFC_ATTR_SE,
145149
NFC_ATTR_LLC_SDP,
150+
NFC_ATTR_FIRMWARE_NAME,
146151
/* private: internal use only */
147152
__NFC_ATTR_AFTER_LAST
148153
};
@@ -162,6 +167,7 @@ enum nfc_sdp_attr {
162167
#define NFC_SENSB_RES_MAXSIZE 12
163168
#define NFC_SENSF_RES_MAXSIZE 18
164169
#define NFC_GB_MAXSIZE 48
170+
#define NFC_FIRMWARE_NAME_MAXSIZE 32
165171

166172
/* NFC protocols */
167173
#define NFC_PROTO_JEWEL 1

net/nfc/core.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,47 @@ DEFINE_MUTEX(nfc_devlist_mutex);
4444
/* NFC device ID bitmap */
4545
static DEFINE_IDA(nfc_index_ida);
4646

47+
int nfc_fw_upload(struct nfc_dev *dev, const char *firmware_name)
48+
{
49+
int rc = 0;
50+
51+
pr_debug("%s do firmware %s\n", dev_name(&dev->dev), firmware_name);
52+
53+
device_lock(&dev->dev);
54+
55+
if (!device_is_registered(&dev->dev)) {
56+
rc = -ENODEV;
57+
goto error;
58+
}
59+
60+
if (dev->dev_up) {
61+
rc = -EBUSY;
62+
goto error;
63+
}
64+
65+
if (!dev->ops->fw_upload) {
66+
rc = -EOPNOTSUPP;
67+
goto error;
68+
}
69+
70+
dev->fw_upload_in_progress = true;
71+
rc = dev->ops->fw_upload(dev, firmware_name);
72+
if (rc)
73+
dev->fw_upload_in_progress = false;
74+
75+
error:
76+
device_unlock(&dev->dev);
77+
return rc;
78+
}
79+
80+
int nfc_fw_upload_done(struct nfc_dev *dev, const char *firmware_name)
81+
{
82+
dev->fw_upload_in_progress = false;
83+
84+
return nfc_genl_fw_upload_done(dev, firmware_name);
85+
}
86+
EXPORT_SYMBOL(nfc_fw_upload_done);
87+
4788
/**
4889
* nfc_dev_up - turn on the NFC device
4990
*
@@ -69,6 +110,11 @@ int nfc_dev_up(struct nfc_dev *dev)
69110
goto error;
70111
}
71112

113+
if (dev->fw_upload_in_progress) {
114+
rc = -EBUSY;
115+
goto error;
116+
}
117+
72118
if (dev->dev_up) {
73119
rc = -EALREADY;
74120
goto error;

net/nfc/netlink.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
5656
[NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
5757
[NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
5858
[NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
59+
[NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
60+
.len = NFC_FIRMWARE_NAME_MAXSIZE },
5961
};
6062

6163
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)
10251027
return rc;
10261028
}
10271029

1030+
static int nfc_genl_fw_upload(struct sk_buff *skb, struct genl_info *info)
1031+
{
1032+
struct nfc_dev *dev;
1033+
int rc;
1034+
u32 idx;
1035+
char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
1036+
1037+
if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
1038+
return -EINVAL;
1039+
1040+
idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1041+
1042+
dev = nfc_get_device(idx);
1043+
if (!dev)
1044+
return -ENODEV;
1045+
1046+
nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
1047+
sizeof(firmware_name));
1048+
1049+
rc = nfc_fw_upload(dev, firmware_name);
1050+
1051+
nfc_put_device(dev);
1052+
return rc;
1053+
}
1054+
1055+
int nfc_genl_fw_upload_done(struct nfc_dev *dev, const char *firmware_name)
1056+
{
1057+
struct sk_buff *msg;
1058+
void *hdr;
1059+
1060+
msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1061+
if (!msg)
1062+
return -ENOMEM;
1063+
1064+
hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1065+
NFC_CMD_FW_UPLOAD);
1066+
if (!hdr)
1067+
goto free_msg;
1068+
1069+
if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
1070+
nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
1071+
goto nla_put_failure;
1072+
1073+
genlmsg_end(msg, hdr);
1074+
1075+
genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
1076+
1077+
return 0;
1078+
1079+
nla_put_failure:
1080+
genlmsg_cancel(msg, hdr);
1081+
free_msg:
1082+
nlmsg_free(msg);
1083+
return -EMSGSIZE;
1084+
}
1085+
10281086
static struct genl_ops nfc_genl_ops[] = {
10291087
{
10301088
.cmd = NFC_CMD_GET_DEVICE,
@@ -1084,6 +1142,11 @@ static struct genl_ops nfc_genl_ops[] = {
10841142
.doit = nfc_genl_llc_sdreq,
10851143
.policy = nfc_genl_policy,
10861144
},
1145+
{
1146+
.cmd = NFC_CMD_FW_UPLOAD,
1147+
.doit = nfc_genl_fw_upload,
1148+
.policy = nfc_genl_policy,
1149+
},
10871150
};
10881151

10891152

net/nfc/nfc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ static inline void nfc_device_iter_exit(struct class_dev_iter *iter)
120120
class_dev_iter_exit(iter);
121121
}
122122

123+
int nfc_fw_upload(struct nfc_dev *dev, const char *firmware_name);
124+
int nfc_genl_fw_upload_done(struct nfc_dev *dev, const char *firmware_name);
125+
126+
int nfc_fw_upload_done(struct nfc_dev *dev, const char *firmware_name);
127+
123128
int nfc_dev_up(struct nfc_dev *dev);
124129

125130
int nfc_dev_down(struct nfc_dev *dev);

0 commit comments

Comments
 (0)