]> Git Repo - linux.git/blob - drivers/platform/chrome/cros_ec_vbc.c
dma-mapping: don't return errors from dma_set_max_seg_size
[linux.git] / drivers / platform / chrome / cros_ec_vbc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 // Expose the vboot context nvram to userspace
3 //
4 // Copyright (C) 2012 Google, Inc.
5 // Copyright (C) 2015 Collabora Ltd.
6
7 #include <linux/of.h>
8 #include <linux/platform_device.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/module.h>
11 #include <linux/platform_data/cros_ec_commands.h>
12 #include <linux/platform_data/cros_ec_proto.h>
13 #include <linux/slab.h>
14
15 #define DRV_NAME "cros-ec-vbc"
16
17 static ssize_t vboot_context_read(struct file *filp, struct kobject *kobj,
18                                   struct bin_attribute *att, char *buf,
19                                   loff_t pos, size_t count)
20 {
21         struct device *dev = kobj_to_dev(kobj);
22         struct cros_ec_dev *ec = to_cros_ec_dev(dev);
23         struct cros_ec_device *ecdev = ec->ec_dev;
24         struct cros_ec_command *msg;
25         /*
26          * This should be a pointer to the same type as op field in
27          * struct ec_params_vbnvcontext.
28          */
29         uint32_t *params_op;
30         int err;
31         const size_t para_sz = sizeof(*params_op);
32         const size_t resp_sz = sizeof(struct ec_response_vbnvcontext);
33         const size_t payload = max(para_sz, resp_sz);
34
35         msg = kmalloc(sizeof(*msg) + payload, GFP_KERNEL);
36         if (!msg)
37                 return -ENOMEM;
38
39         /* NB: we only kmalloc()ated enough space for the op field */
40         params_op = (uint32_t *)msg->data;
41         *params_op = EC_VBNV_CONTEXT_OP_READ;
42
43         msg->version = EC_VER_VBNV_CONTEXT;
44         msg->command = EC_CMD_VBNV_CONTEXT;
45         msg->outsize = para_sz;
46         msg->insize = resp_sz;
47
48         err = cros_ec_cmd_xfer_status(ecdev, msg);
49         if (err < 0) {
50                 dev_err(dev, "Error sending read request: %d\n", err);
51                 kfree(msg);
52                 return err;
53         }
54
55         memcpy(buf, msg->data, resp_sz);
56
57         kfree(msg);
58         return resp_sz;
59 }
60
61 static ssize_t vboot_context_write(struct file *filp, struct kobject *kobj,
62                                    struct bin_attribute *attr, char *buf,
63                                    loff_t pos, size_t count)
64 {
65         struct device *dev = kobj_to_dev(kobj);
66         struct cros_ec_dev *ec = to_cros_ec_dev(dev);
67         struct cros_ec_device *ecdev = ec->ec_dev;
68         struct ec_params_vbnvcontext *params;
69         struct cros_ec_command *msg;
70         int err;
71         const size_t para_sz = sizeof(*params);
72         const size_t data_sz = sizeof(params->block);
73
74         /* Only write full values */
75         if (count != data_sz)
76                 return -EINVAL;
77
78         msg = kmalloc(sizeof(*msg) + para_sz, GFP_KERNEL);
79         if (!msg)
80                 return -ENOMEM;
81
82         params = (struct ec_params_vbnvcontext *)msg->data;
83         params->op = EC_VBNV_CONTEXT_OP_WRITE;
84         memcpy(params->block, buf, data_sz);
85
86         msg->version = EC_VER_VBNV_CONTEXT;
87         msg->command = EC_CMD_VBNV_CONTEXT;
88         msg->outsize = para_sz;
89         msg->insize = 0;
90
91         err = cros_ec_cmd_xfer_status(ecdev, msg);
92         if (err < 0) {
93                 dev_err(dev, "Error sending write request: %d\n", err);
94                 kfree(msg);
95                 return err;
96         }
97
98         kfree(msg);
99         return data_sz;
100 }
101
102 static BIN_ATTR_RW(vboot_context, 16);
103
104 static struct bin_attribute *cros_ec_vbc_bin_attrs[] = {
105         &bin_attr_vboot_context,
106         NULL
107 };
108
109 static const struct attribute_group cros_ec_vbc_attr_group = {
110         .name = "vbc",
111         .bin_attrs = cros_ec_vbc_bin_attrs,
112 };
113
114 static int cros_ec_vbc_probe(struct platform_device *pd)
115 {
116         struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
117         struct device *dev = &pd->dev;
118         int ret;
119
120         ret = sysfs_create_group(&ec_dev->class_dev.kobj,
121                                  &cros_ec_vbc_attr_group);
122         if (ret < 0)
123                 dev_err(dev, "failed to create %s attributes. err=%d\n",
124                         cros_ec_vbc_attr_group.name, ret);
125
126         return ret;
127 }
128
129 static void cros_ec_vbc_remove(struct platform_device *pd)
130 {
131         struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
132
133         sysfs_remove_group(&ec_dev->class_dev.kobj,
134                            &cros_ec_vbc_attr_group);
135 }
136
137 static const struct platform_device_id cros_ec_vbc_id[] = {
138         { DRV_NAME, 0 },
139         {}
140 };
141 MODULE_DEVICE_TABLE(platform, cros_ec_vbc_id);
142
143 static struct platform_driver cros_ec_vbc_driver = {
144         .driver = {
145                 .name = DRV_NAME,
146         },
147         .probe = cros_ec_vbc_probe,
148         .remove_new = cros_ec_vbc_remove,
149         .id_table = cros_ec_vbc_id,
150 };
151
152 module_platform_driver(cros_ec_vbc_driver);
153
154 MODULE_LICENSE("GPL");
155 MODULE_DESCRIPTION("Expose the vboot context nvram to userspace");
This page took 0.034194 seconds and 4 git commands to generate.