]> Git Repo - linux.git/blob - drivers/misc/rpmb-core.c
Merge tag 'i2c-for-6.14-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa...
[linux.git] / drivers / misc / rpmb-core.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright(c) 2015 - 2019 Intel Corporation. All rights reserved.
4  * Copyright(c) 2021 - 2024 Linaro Ltd.
5  */
6 #include <linux/device.h>
7 #include <linux/init.h>
8 #include <linux/kernel.h>
9 #include <linux/list.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/rpmb.h>
13 #include <linux/slab.h>
14
15 static DEFINE_IDA(rpmb_ida);
16
17 /**
18  * rpmb_dev_get() - increase rpmb device ref counter
19  * @rdev: rpmb device
20  */
21 struct rpmb_dev *rpmb_dev_get(struct rpmb_dev *rdev)
22 {
23         if (rdev)
24                 get_device(&rdev->dev);
25         return rdev;
26 }
27 EXPORT_SYMBOL_GPL(rpmb_dev_get);
28
29 /**
30  * rpmb_dev_put() - decrease rpmb device ref counter
31  * @rdev: rpmb device
32  */
33 void rpmb_dev_put(struct rpmb_dev *rdev)
34 {
35         if (rdev)
36                 put_device(&rdev->dev);
37 }
38 EXPORT_SYMBOL_GPL(rpmb_dev_put);
39
40 /**
41  * rpmb_route_frames() - route rpmb frames to rpmb device
42  * @rdev:       rpmb device
43  * @req:        rpmb request frames
44  * @req_len:    length of rpmb request frames in bytes
45  * @rsp:        rpmb response frames
46  * @rsp_len:    length of rpmb response frames in bytes
47  *
48  * Returns: < 0 on failure
49  */
50 int rpmb_route_frames(struct rpmb_dev *rdev, u8 *req,
51                       unsigned int req_len, u8 *rsp, unsigned int rsp_len)
52 {
53         if (!req || !req_len || !rsp || !rsp_len)
54                 return -EINVAL;
55
56         return rdev->descr.route_frames(rdev->dev.parent, req, req_len,
57                                         rsp, rsp_len);
58 }
59 EXPORT_SYMBOL_GPL(rpmb_route_frames);
60
61 static void rpmb_dev_release(struct device *dev)
62 {
63         struct rpmb_dev *rdev = to_rpmb_dev(dev);
64
65         ida_free(&rpmb_ida, rdev->id);
66         kfree(rdev->descr.dev_id);
67         kfree(rdev);
68 }
69
70 static struct class rpmb_class = {
71         .name = "rpmb",
72         .dev_release = rpmb_dev_release,
73 };
74
75 /**
76  * rpmb_dev_find_device() - return first matching rpmb device
77  * @start: rpmb device to begin with
78  * @data: data for the match function
79  * @match: the matching function
80  *
81  * Iterate over registered RPMB devices, and call @match() for each passing
82  * it the RPMB device and @data.
83  *
84  * The return value of @match() is checked for each call. If it returns
85  * anything other 0, break and return the found RPMB device.
86  *
87  * It's the callers responsibility to call rpmb_dev_put() on the returned
88  * device, when it's done with it.
89  *
90  * Returns: a matching rpmb device or NULL on failure
91  */
92 struct rpmb_dev *rpmb_dev_find_device(const void *data,
93                                       const struct rpmb_dev *start,
94                                       int (*match)(struct device *dev,
95                                                    const void *data))
96 {
97         struct device *dev;
98         const struct device *start_dev = NULL;
99
100         if (start)
101                 start_dev = &start->dev;
102         dev = class_find_device(&rpmb_class, start_dev, data, match);
103
104         return dev ? to_rpmb_dev(dev) : NULL;
105 }
106 EXPORT_SYMBOL_GPL(rpmb_dev_find_device);
107
108 int rpmb_interface_register(struct class_interface *intf)
109 {
110         intf->class = &rpmb_class;
111
112         return class_interface_register(intf);
113 }
114 EXPORT_SYMBOL_GPL(rpmb_interface_register);
115
116 void rpmb_interface_unregister(struct class_interface *intf)
117 {
118         class_interface_unregister(intf);
119 }
120 EXPORT_SYMBOL_GPL(rpmb_interface_unregister);
121
122 /**
123  * rpmb_dev_unregister() - unregister RPMB partition from the RPMB subsystem
124  * @rdev: the rpmb device to unregister
125  *
126  * This function should be called from the release function of the
127  * underlying device used when the RPMB device was registered.
128  *
129  * Returns: < 0 on failure
130  */
131 int rpmb_dev_unregister(struct rpmb_dev *rdev)
132 {
133         if (!rdev)
134                 return -EINVAL;
135
136         device_del(&rdev->dev);
137
138         rpmb_dev_put(rdev);
139
140         return 0;
141 }
142 EXPORT_SYMBOL_GPL(rpmb_dev_unregister);
143
144 /**
145  * rpmb_dev_register - register RPMB partition with the RPMB subsystem
146  * @dev: storage device of the rpmb device
147  * @descr: RPMB device description
148  *
149  * While registering the RPMB partition extract needed device information
150  * while needed resources are available.
151  *
152  * Returns: a pointer to a 'struct rpmb_dev' or an ERR_PTR on failure
153  */
154 struct rpmb_dev *rpmb_dev_register(struct device *dev,
155                                    struct rpmb_descr *descr)
156 {
157         struct rpmb_dev *rdev;
158         int ret;
159
160         if (!dev || !descr || !descr->route_frames || !descr->dev_id ||
161             !descr->dev_id_len)
162                 return ERR_PTR(-EINVAL);
163
164         rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
165         if (!rdev)
166                 return ERR_PTR(-ENOMEM);
167         rdev->descr = *descr;
168         rdev->descr.dev_id = kmemdup(descr->dev_id, descr->dev_id_len,
169                                      GFP_KERNEL);
170         if (!rdev->descr.dev_id) {
171                 ret = -ENOMEM;
172                 goto err_free_rdev;
173         }
174
175         ret = ida_alloc(&rpmb_ida, GFP_KERNEL);
176         if (ret < 0)
177                 goto err_free_dev_id;
178         rdev->id = ret;
179
180         dev_set_name(&rdev->dev, "rpmb%d", rdev->id);
181         rdev->dev.class = &rpmb_class;
182         rdev->dev.parent = dev;
183
184         ret = device_register(&rdev->dev);
185         if (ret) {
186                 put_device(&rdev->dev);
187                 return ERR_PTR(ret);
188         }
189
190         dev_dbg(&rdev->dev, "registered device\n");
191
192         return rdev;
193
194 err_free_dev_id:
195         kfree(rdev->descr.dev_id);
196 err_free_rdev:
197         kfree(rdev);
198         return ERR_PTR(ret);
199 }
200 EXPORT_SYMBOL_GPL(rpmb_dev_register);
201
202 static int __init rpmb_init(void)
203 {
204         int ret;
205
206         ret = class_register(&rpmb_class);
207         if (ret) {
208                 pr_err("couldn't create class\n");
209                 return ret;
210         }
211         ida_init(&rpmb_ida);
212         return 0;
213 }
214
215 static void __exit rpmb_exit(void)
216 {
217         ida_destroy(&rpmb_ida);
218         class_unregister(&rpmb_class);
219 }
220
221 subsys_initcall(rpmb_init);
222 module_exit(rpmb_exit);
223
224 MODULE_AUTHOR("Jens Wiklander <[email protected]>");
225 MODULE_DESCRIPTION("RPMB class");
226 MODULE_LICENSE("GPL");
This page took 0.043187 seconds and 4 git commands to generate.