1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright(c) 2015 - 2019 Intel Corporation. All rights reserved.
4 * Copyright(c) 2021 - 2024 Linaro Ltd.
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>
15 static DEFINE_IDA(rpmb_ida);
18 * rpmb_dev_get() - increase rpmb device ref counter
21 struct rpmb_dev *rpmb_dev_get(struct rpmb_dev *rdev)
24 get_device(&rdev->dev);
27 EXPORT_SYMBOL_GPL(rpmb_dev_get);
30 * rpmb_dev_put() - decrease rpmb device ref counter
33 void rpmb_dev_put(struct rpmb_dev *rdev)
36 put_device(&rdev->dev);
38 EXPORT_SYMBOL_GPL(rpmb_dev_put);
41 * rpmb_route_frames() - route rpmb frames to 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
48 * Returns: < 0 on failure
50 int rpmb_route_frames(struct rpmb_dev *rdev, u8 *req,
51 unsigned int req_len, u8 *rsp, unsigned int rsp_len)
53 if (!req || !req_len || !rsp || !rsp_len)
56 return rdev->descr.route_frames(rdev->dev.parent, req, req_len,
59 EXPORT_SYMBOL_GPL(rpmb_route_frames);
61 static void rpmb_dev_release(struct device *dev)
63 struct rpmb_dev *rdev = to_rpmb_dev(dev);
65 ida_free(&rpmb_ida, rdev->id);
66 kfree(rdev->descr.dev_id);
70 static struct class rpmb_class = {
72 .dev_release = rpmb_dev_release,
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
81 * Iterate over registered RPMB devices, and call @match() for each passing
82 * it the RPMB device and @data.
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.
87 * It's the callers responsibility to call rpmb_dev_put() on the returned
88 * device, when it's done with it.
90 * Returns: a matching rpmb device or NULL on failure
92 struct rpmb_dev *rpmb_dev_find_device(const void *data,
93 const struct rpmb_dev *start,
94 int (*match)(struct device *dev,
98 const struct device *start_dev = NULL;
101 start_dev = &start->dev;
102 dev = class_find_device(&rpmb_class, start_dev, data, match);
104 return dev ? to_rpmb_dev(dev) : NULL;
106 EXPORT_SYMBOL_GPL(rpmb_dev_find_device);
108 int rpmb_interface_register(struct class_interface *intf)
110 intf->class = &rpmb_class;
112 return class_interface_register(intf);
114 EXPORT_SYMBOL_GPL(rpmb_interface_register);
116 void rpmb_interface_unregister(struct class_interface *intf)
118 class_interface_unregister(intf);
120 EXPORT_SYMBOL_GPL(rpmb_interface_unregister);
123 * rpmb_dev_unregister() - unregister RPMB partition from the RPMB subsystem
124 * @rdev: the rpmb device to unregister
126 * This function should be called from the release function of the
127 * underlying device used when the RPMB device was registered.
129 * Returns: < 0 on failure
131 int rpmb_dev_unregister(struct rpmb_dev *rdev)
136 device_del(&rdev->dev);
142 EXPORT_SYMBOL_GPL(rpmb_dev_unregister);
145 * rpmb_dev_register - register RPMB partition with the RPMB subsystem
146 * @dev: storage device of the rpmb device
147 * @descr: RPMB device description
149 * While registering the RPMB partition extract needed device information
150 * while needed resources are available.
152 * Returns: a pointer to a 'struct rpmb_dev' or an ERR_PTR on failure
154 struct rpmb_dev *rpmb_dev_register(struct device *dev,
155 struct rpmb_descr *descr)
157 struct rpmb_dev *rdev;
160 if (!dev || !descr || !descr->route_frames || !descr->dev_id ||
162 return ERR_PTR(-EINVAL);
164 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
166 return ERR_PTR(-ENOMEM);
167 rdev->descr = *descr;
168 rdev->descr.dev_id = kmemdup(descr->dev_id, descr->dev_id_len,
170 if (!rdev->descr.dev_id) {
175 ret = ida_alloc(&rpmb_ida, GFP_KERNEL);
177 goto err_free_dev_id;
180 dev_set_name(&rdev->dev, "rpmb%d", rdev->id);
181 rdev->dev.class = &rpmb_class;
182 rdev->dev.parent = dev;
184 ret = device_register(&rdev->dev);
186 put_device(&rdev->dev);
190 dev_dbg(&rdev->dev, "registered device\n");
195 kfree(rdev->descr.dev_id);
200 EXPORT_SYMBOL_GPL(rpmb_dev_register);
202 static int __init rpmb_init(void)
206 ret = class_register(&rpmb_class);
208 pr_err("couldn't create class\n");
215 static void __exit rpmb_exit(void)
217 ida_destroy(&rpmb_ida);
218 class_unregister(&rpmb_class);
221 subsys_initcall(rpmb_init);
222 module_exit(rpmb_exit);
225 MODULE_DESCRIPTION("RPMB class");
226 MODULE_LICENSE("GPL");