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
);
16 static DEFINE_MUTEX(rpmb_mutex
);
19 * rpmb_dev_get() - increase rpmb device ref counter
22 struct rpmb_dev
*rpmb_dev_get(struct rpmb_dev
*rdev
)
25 get_device(&rdev
->dev
);
28 EXPORT_SYMBOL_GPL(rpmb_dev_get
);
31 * rpmb_dev_put() - decrease rpmb device ref counter
34 void rpmb_dev_put(struct rpmb_dev
*rdev
)
37 put_device(&rdev
->dev
);
39 EXPORT_SYMBOL_GPL(rpmb_dev_put
);
42 * rpmb_route_frames() - route rpmb frames to rpmb device
44 * @req: rpmb request frames
45 * @req_len: length of rpmb request frames in bytes
46 * @rsp: rpmb response frames
47 * @rsp_len: length of rpmb response frames in bytes
49 * Returns: < 0 on failure
51 int rpmb_route_frames(struct rpmb_dev
*rdev
, u8
*req
,
52 unsigned int req_len
, u8
*rsp
, unsigned int rsp_len
)
54 if (!req
|| !req_len
|| !rsp
|| !rsp_len
)
57 return rdev
->descr
.route_frames(rdev
->dev
.parent
, req
, req_len
,
60 EXPORT_SYMBOL_GPL(rpmb_route_frames
);
62 static void rpmb_dev_release(struct device
*dev
)
64 struct rpmb_dev
*rdev
= to_rpmb_dev(dev
);
66 mutex_lock(&rpmb_mutex
);
67 ida_simple_remove(&rpmb_ida
, rdev
->id
);
68 mutex_unlock(&rpmb_mutex
);
69 kfree(rdev
->descr
.dev_id
);
73 static struct class rpmb_class
= {
75 .dev_release
= rpmb_dev_release
,
79 * rpmb_dev_find_device() - return first matching rpmb device
80 * @start: rpmb device to begin with
81 * @data: data for the match function
82 * @match: the matching function
84 * Iterate over registered RPMB devices, and call @match() for each passing
85 * it the RPMB device and @data.
87 * The return value of @match() is checked for each call. If it returns
88 * anything other 0, break and return the found RPMB device.
90 * It's the callers responsibility to call rpmb_dev_put() on the returned
91 * device, when it's done with it.
93 * Returns: a matching rpmb device or NULL on failure
95 struct rpmb_dev
*rpmb_dev_find_device(const void *data
,
96 const struct rpmb_dev
*start
,
97 int (*match
)(struct device
*dev
,
101 const struct device
*start_dev
= NULL
;
104 start_dev
= &start
->dev
;
105 dev
= class_find_device(&rpmb_class
, start_dev
, data
, match
);
107 return dev
? to_rpmb_dev(dev
) : NULL
;
109 EXPORT_SYMBOL_GPL(rpmb_dev_find_device
);
111 int rpmb_interface_register(struct class_interface
*intf
)
113 intf
->class = &rpmb_class
;
115 return class_interface_register(intf
);
117 EXPORT_SYMBOL_GPL(rpmb_interface_register
);
119 void rpmb_interface_unregister(struct class_interface
*intf
)
121 class_interface_unregister(intf
);
123 EXPORT_SYMBOL_GPL(rpmb_interface_unregister
);
126 * rpmb_dev_unregister() - unregister RPMB partition from the RPMB subsystem
127 * @rdev: the rpmb device to unregister
129 * This function should be called from the release function of the
130 * underlying device used when the RPMB device was registered.
132 * Returns: < 0 on failure
134 int rpmb_dev_unregister(struct rpmb_dev
*rdev
)
139 device_del(&rdev
->dev
);
145 EXPORT_SYMBOL_GPL(rpmb_dev_unregister
);
148 * rpmb_dev_register - register RPMB partition with the RPMB subsystem
149 * @dev: storage device of the rpmb device
150 * @descr: RPMB device description
152 * While registering the RPMB partition extract needed device information
153 * while needed resources are available.
155 * Returns: a pointer to a 'struct rpmb_dev' or an ERR_PTR on failure
157 struct rpmb_dev
*rpmb_dev_register(struct device
*dev
,
158 struct rpmb_descr
*descr
)
160 struct rpmb_dev
*rdev
;
163 if (!dev
|| !descr
|| !descr
->route_frames
|| !descr
->dev_id
||
165 return ERR_PTR(-EINVAL
);
167 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
169 return ERR_PTR(-ENOMEM
);
170 rdev
->descr
= *descr
;
171 rdev
->descr
.dev_id
= kmemdup(descr
->dev_id
, descr
->dev_id_len
,
173 if (!rdev
->descr
.dev_id
) {
178 mutex_lock(&rpmb_mutex
);
179 ret
= ida_simple_get(&rpmb_ida
, 0, 0, GFP_KERNEL
);
180 mutex_unlock(&rpmb_mutex
);
182 goto err_free_dev_id
;
185 dev_set_name(&rdev
->dev
, "rpmb%d", rdev
->id
);
186 rdev
->dev
.class = &rpmb_class
;
187 rdev
->dev
.parent
= dev
;
189 ret
= device_register(&rdev
->dev
);
191 put_device(&rdev
->dev
);
195 dev_dbg(&rdev
->dev
, "registered device\n");
200 kfree(rdev
->descr
.dev_id
);
205 EXPORT_SYMBOL_GPL(rpmb_dev_register
);
207 static int __init
rpmb_init(void)
211 ret
= class_register(&rpmb_class
);
213 pr_err("couldn't create class\n");
220 static void __exit
rpmb_exit(void)
222 ida_destroy(&rpmb_ida
);
223 class_unregister(&rpmb_class
);
226 subsys_initcall(rpmb_init
);
227 module_exit(rpmb_exit
);
229 MODULE_AUTHOR("Jens Wiklander <jens.wiklander@linaro.org>");
230 MODULE_DESCRIPTION("RPMB class");
231 MODULE_LICENSE("GPL");