xref: /linux/drivers/platform/chrome/cros_ec_vbc.c (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
127755cf7SEnric Balletbo i Serra // SPDX-License-Identifier: GPL-2.0+
227755cf7SEnric Balletbo i Serra // Expose the vboot context nvram to userspace
327755cf7SEnric Balletbo i Serra //
427755cf7SEnric Balletbo i Serra // Copyright (C) 2012 Google, Inc.
527755cf7SEnric Balletbo i Serra // Copyright (C) 2015 Collabora Ltd.
618800fc7SEmilio López 
718800fc7SEmilio López #include <linux/of.h>
818800fc7SEmilio López #include <linux/platform_device.h>
9*0cb7b263STzung-Bi Shih #include <linux/mod_devicetable.h>
10acb9900fSEnric Balletbo i Serra #include <linux/module.h>
11840d9f13SEnric Balletbo i Serra #include <linux/platform_data/cros_ec_commands.h>
12840d9f13SEnric Balletbo i Serra #include <linux/platform_data/cros_ec_proto.h>
1318800fc7SEmilio López #include <linux/slab.h>
1418800fc7SEmilio López 
15acb9900fSEnric Balletbo i Serra #define DRV_NAME "cros-ec-vbc"
16acb9900fSEnric Balletbo i Serra 
vboot_context_read(struct file * filp,struct kobject * kobj,struct bin_attribute * att,char * buf,loff_t pos,size_t count)1718800fc7SEmilio López static ssize_t vboot_context_read(struct file *filp, struct kobject *kobj,
1818800fc7SEmilio López 				  struct bin_attribute *att, char *buf,
1918800fc7SEmilio López 				  loff_t pos, size_t count)
2018800fc7SEmilio López {
219aa7bd45SWang Qing 	struct device *dev = kobj_to_dev(kobj);
2279a3d603SGwendal Grignou 	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
2318800fc7SEmilio López 	struct cros_ec_device *ecdev = ec->ec_dev;
2418800fc7SEmilio López 	struct cros_ec_command *msg;
2559a9ccf1SGustavo A. R. Silva 	/*
2659a9ccf1SGustavo A. R. Silva 	 * This should be a pointer to the same type as op field in
2759a9ccf1SGustavo A. R. Silva 	 * struct ec_params_vbnvcontext.
2859a9ccf1SGustavo A. R. Silva 	 */
2959a9ccf1SGustavo A. R. Silva 	uint32_t *params_op;
3018800fc7SEmilio López 	int err;
3159a9ccf1SGustavo A. R. Silva 	const size_t para_sz = sizeof(*params_op);
3218800fc7SEmilio López 	const size_t resp_sz = sizeof(struct ec_response_vbnvcontext);
3318800fc7SEmilio López 	const size_t payload = max(para_sz, resp_sz);
3418800fc7SEmilio López 
3518800fc7SEmilio López 	msg = kmalloc(sizeof(*msg) + payload, GFP_KERNEL);
3618800fc7SEmilio López 	if (!msg)
3718800fc7SEmilio López 		return -ENOMEM;
3818800fc7SEmilio López 
3918800fc7SEmilio López 	/* NB: we only kmalloc()ated enough space for the op field */
4059a9ccf1SGustavo A. R. Silva 	params_op = (uint32_t *)msg->data;
4159a9ccf1SGustavo A. R. Silva 	*params_op = EC_VBNV_CONTEXT_OP_READ;
4218800fc7SEmilio López 
4318800fc7SEmilio López 	msg->version = EC_VER_VBNV_CONTEXT;
4418800fc7SEmilio López 	msg->command = EC_CMD_VBNV_CONTEXT;
4518800fc7SEmilio López 	msg->outsize = para_sz;
4618800fc7SEmilio López 	msg->insize = resp_sz;
4718800fc7SEmilio López 
486bc15ad7SEnric Balletbo i Serra 	err = cros_ec_cmd_xfer_status(ecdev, msg);
4918800fc7SEmilio López 	if (err < 0) {
5018800fc7SEmilio López 		dev_err(dev, "Error sending read request: %d\n", err);
5118800fc7SEmilio López 		kfree(msg);
5218800fc7SEmilio López 		return err;
5318800fc7SEmilio López 	}
5418800fc7SEmilio López 
5518800fc7SEmilio López 	memcpy(buf, msg->data, resp_sz);
5618800fc7SEmilio López 
5718800fc7SEmilio López 	kfree(msg);
5818800fc7SEmilio López 	return resp_sz;
5918800fc7SEmilio López }
6018800fc7SEmilio López 
vboot_context_write(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t pos,size_t count)6118800fc7SEmilio López static ssize_t vboot_context_write(struct file *filp, struct kobject *kobj,
6218800fc7SEmilio López 				   struct bin_attribute *attr, char *buf,
6318800fc7SEmilio López 				   loff_t pos, size_t count)
6418800fc7SEmilio López {
659aa7bd45SWang Qing 	struct device *dev = kobj_to_dev(kobj);
6679a3d603SGwendal Grignou 	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
6718800fc7SEmilio López 	struct cros_ec_device *ecdev = ec->ec_dev;
6818800fc7SEmilio López 	struct ec_params_vbnvcontext *params;
6918800fc7SEmilio López 	struct cros_ec_command *msg;
7018800fc7SEmilio López 	int err;
7118800fc7SEmilio López 	const size_t para_sz = sizeof(*params);
7218800fc7SEmilio López 	const size_t data_sz = sizeof(params->block);
7318800fc7SEmilio López 
7418800fc7SEmilio López 	/* Only write full values */
7518800fc7SEmilio López 	if (count != data_sz)
7618800fc7SEmilio López 		return -EINVAL;
7718800fc7SEmilio López 
7818800fc7SEmilio López 	msg = kmalloc(sizeof(*msg) + para_sz, GFP_KERNEL);
7918800fc7SEmilio López 	if (!msg)
8018800fc7SEmilio López 		return -ENOMEM;
8118800fc7SEmilio López 
8218800fc7SEmilio López 	params = (struct ec_params_vbnvcontext *)msg->data;
8318800fc7SEmilio López 	params->op = EC_VBNV_CONTEXT_OP_WRITE;
8418800fc7SEmilio López 	memcpy(params->block, buf, data_sz);
8518800fc7SEmilio López 
8618800fc7SEmilio López 	msg->version = EC_VER_VBNV_CONTEXT;
8718800fc7SEmilio López 	msg->command = EC_CMD_VBNV_CONTEXT;
8818800fc7SEmilio López 	msg->outsize = para_sz;
8918800fc7SEmilio López 	msg->insize = 0;
9018800fc7SEmilio López 
916bc15ad7SEnric Balletbo i Serra 	err = cros_ec_cmd_xfer_status(ecdev, msg);
9218800fc7SEmilio López 	if (err < 0) {
9318800fc7SEmilio López 		dev_err(dev, "Error sending write request: %d\n", err);
9418800fc7SEmilio López 		kfree(msg);
9518800fc7SEmilio López 		return err;
9618800fc7SEmilio López 	}
9718800fc7SEmilio López 
9818800fc7SEmilio López 	kfree(msg);
9918800fc7SEmilio López 	return data_sz;
10018800fc7SEmilio López }
10118800fc7SEmilio López 
10218800fc7SEmilio López static BIN_ATTR_RW(vboot_context, 16);
10318800fc7SEmilio López 
10418800fc7SEmilio López static struct bin_attribute *cros_ec_vbc_bin_attrs[] = {
10518800fc7SEmilio López 	&bin_attr_vboot_context,
10618800fc7SEmilio López 	NULL
10718800fc7SEmilio López };
10818800fc7SEmilio López 
109d7c1fef7SRikard Falkeborn static const struct attribute_group cros_ec_vbc_attr_group = {
11018800fc7SEmilio López 	.name = "vbc",
11118800fc7SEmilio López 	.bin_attrs = cros_ec_vbc_bin_attrs,
11218800fc7SEmilio López };
113acb9900fSEnric Balletbo i Serra 
cros_ec_vbc_probe(struct platform_device * pd)114acb9900fSEnric Balletbo i Serra static int cros_ec_vbc_probe(struct platform_device *pd)
115acb9900fSEnric Balletbo i Serra {
116acb9900fSEnric Balletbo i Serra 	struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
117acb9900fSEnric Balletbo i Serra 	struct device *dev = &pd->dev;
118acb9900fSEnric Balletbo i Serra 	int ret;
119acb9900fSEnric Balletbo i Serra 
120acb9900fSEnric Balletbo i Serra 	ret = sysfs_create_group(&ec_dev->class_dev.kobj,
121acb9900fSEnric Balletbo i Serra 				 &cros_ec_vbc_attr_group);
122acb9900fSEnric Balletbo i Serra 	if (ret < 0)
123acb9900fSEnric Balletbo i Serra 		dev_err(dev, "failed to create %s attributes. err=%d\n",
124acb9900fSEnric Balletbo i Serra 			cros_ec_vbc_attr_group.name, ret);
125acb9900fSEnric Balletbo i Serra 
126acb9900fSEnric Balletbo i Serra 	return ret;
127acb9900fSEnric Balletbo i Serra }
128acb9900fSEnric Balletbo i Serra 
cros_ec_vbc_remove(struct platform_device * pd)12958b15196SUwe Kleine-König static void cros_ec_vbc_remove(struct platform_device *pd)
130acb9900fSEnric Balletbo i Serra {
131acb9900fSEnric Balletbo i Serra 	struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
132acb9900fSEnric Balletbo i Serra 
133acb9900fSEnric Balletbo i Serra 	sysfs_remove_group(&ec_dev->class_dev.kobj,
134acb9900fSEnric Balletbo i Serra 			   &cros_ec_vbc_attr_group);
135acb9900fSEnric Balletbo i Serra }
136acb9900fSEnric Balletbo i Serra 
137*0cb7b263STzung-Bi Shih static const struct platform_device_id cros_ec_vbc_id[] = {
138*0cb7b263STzung-Bi Shih 	{ DRV_NAME, 0 },
139*0cb7b263STzung-Bi Shih 	{}
140*0cb7b263STzung-Bi Shih };
141*0cb7b263STzung-Bi Shih MODULE_DEVICE_TABLE(platform, cros_ec_vbc_id);
142*0cb7b263STzung-Bi Shih 
143acb9900fSEnric Balletbo i Serra static struct platform_driver cros_ec_vbc_driver = {
144acb9900fSEnric Balletbo i Serra 	.driver = {
145acb9900fSEnric Balletbo i Serra 		.name = DRV_NAME,
146acb9900fSEnric Balletbo i Serra 	},
147acb9900fSEnric Balletbo i Serra 	.probe = cros_ec_vbc_probe,
14858b15196SUwe Kleine-König 	.remove_new = cros_ec_vbc_remove,
149*0cb7b263STzung-Bi Shih 	.id_table = cros_ec_vbc_id,
150acb9900fSEnric Balletbo i Serra };
151acb9900fSEnric Balletbo i Serra 
152acb9900fSEnric Balletbo i Serra module_platform_driver(cros_ec_vbc_driver);
153acb9900fSEnric Balletbo i Serra 
154acb9900fSEnric Balletbo i Serra MODULE_LICENSE("GPL");
155acb9900fSEnric Balletbo i Serra MODULE_DESCRIPTION("Expose the vboot context nvram to userspace");
156