Linux Audio

Check our new training course

Loading...
Note: File does not exist in v4.6.
  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
 15static DEFINE_IDA(rpmb_ida);
 16
 17/**
 18 * rpmb_dev_get() - increase rpmb device ref counter
 19 * @rdev: rpmb device
 20 */
 21struct rpmb_dev *rpmb_dev_get(struct rpmb_dev *rdev)
 22{
 23	if (rdev)
 24		get_device(&rdev->dev);
 25	return rdev;
 26}
 27EXPORT_SYMBOL_GPL(rpmb_dev_get);
 28
 29/**
 30 * rpmb_dev_put() - decrease rpmb device ref counter
 31 * @rdev: rpmb device
 32 */
 33void rpmb_dev_put(struct rpmb_dev *rdev)
 34{
 35	if (rdev)
 36		put_device(&rdev->dev);
 37}
 38EXPORT_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 */
 50int 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}
 59EXPORT_SYMBOL_GPL(rpmb_route_frames);
 60
 61static 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
 70static 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 */
 92struct 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}
106EXPORT_SYMBOL_GPL(rpmb_dev_find_device);
107
108int rpmb_interface_register(struct class_interface *intf)
109{
110	intf->class = &rpmb_class;
111
112	return class_interface_register(intf);
113}
114EXPORT_SYMBOL_GPL(rpmb_interface_register);
115
116void rpmb_interface_unregister(struct class_interface *intf)
117{
118	class_interface_unregister(intf);
119}
120EXPORT_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 */
131int 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}
142EXPORT_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 */
154struct 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
194err_free_dev_id:
195	kfree(rdev->descr.dev_id);
196err_free_rdev:
197	kfree(rdev);
198	return ERR_PTR(ret);
199}
200EXPORT_SYMBOL_GPL(rpmb_dev_register);
201
202static 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
215static void __exit rpmb_exit(void)
216{
217	ida_destroy(&rpmb_ida);
218	class_unregister(&rpmb_class);
219}
220
221subsys_initcall(rpmb_init);
222module_exit(rpmb_exit);
223
224MODULE_AUTHOR("Jens Wiklander <jens.wiklander@linaro.org>");
225MODULE_DESCRIPTION("RPMB class");
226MODULE_LICENSE("GPL");