drm/nouveau: fix kernel-doc comments
[drm/drm-misc.git] / drivers / misc / rpmb-core.c
blob2d653926cdbbb03976496f6034a0024cd0fc5fd4
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>
15 static DEFINE_IDA(rpmb_ida);
17 /**
18 * rpmb_dev_get() - increase rpmb device ref counter
19 * @rdev: rpmb device
21 struct rpmb_dev *rpmb_dev_get(struct rpmb_dev *rdev)
23 if (rdev)
24 get_device(&rdev->dev);
25 return rdev;
27 EXPORT_SYMBOL_GPL(rpmb_dev_get);
29 /**
30 * rpmb_dev_put() - decrease rpmb device ref counter
31 * @rdev: rpmb device
33 void rpmb_dev_put(struct rpmb_dev *rdev)
35 if (rdev)
36 put_device(&rdev->dev);
38 EXPORT_SYMBOL_GPL(rpmb_dev_put);
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
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)
54 return -EINVAL;
56 return rdev->descr.route_frames(rdev->dev.parent, req, req_len,
57 rsp, rsp_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);
67 kfree(rdev);
70 static struct class rpmb_class = {
71 .name = "rpmb",
72 .dev_release = rpmb_dev_release,
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
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,
95 const void *data))
97 struct device *dev;
98 const struct device *start_dev = NULL;
100 if (start)
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)
133 if (!rdev)
134 return -EINVAL;
136 device_del(&rdev->dev);
138 rpmb_dev_put(rdev);
140 return 0;
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;
158 int ret;
160 if (!dev || !descr || !descr->route_frames || !descr->dev_id ||
161 !descr->dev_id_len)
162 return ERR_PTR(-EINVAL);
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;
175 ret = ida_alloc(&rpmb_ida, GFP_KERNEL);
176 if (ret < 0)
177 goto err_free_dev_id;
178 rdev->id = ret;
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);
185 if (ret) {
186 put_device(&rdev->dev);
187 return ERR_PTR(ret);
190 dev_dbg(&rdev->dev, "registered device\n");
192 return rdev;
194 err_free_dev_id:
195 kfree(rdev->descr.dev_id);
196 err_free_rdev:
197 kfree(rdev);
198 return ERR_PTR(ret);
200 EXPORT_SYMBOL_GPL(rpmb_dev_register);
202 static int __init rpmb_init(void)
204 int ret;
206 ret = class_register(&rpmb_class);
207 if (ret) {
208 pr_err("couldn't create class\n");
209 return ret;
211 ida_init(&rpmb_ida);
212 return 0;
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);
224 MODULE_AUTHOR("Jens Wiklander <jens.wiklander@linaro.org>");
225 MODULE_DESCRIPTION("RPMB class");
226 MODULE_LICENSE("GPL");