rpmb-core.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. static DEFINE_IDA(rpmb_ida);
  15. /**
  16. * rpmb_dev_get() - increase rpmb device ref counter
  17. * @rdev: rpmb device
  18. */
  19. struct rpmb_dev *rpmb_dev_get(struct rpmb_dev *rdev)
  20. {
  21. if (rdev)
  22. get_device(&rdev->dev);
  23. return rdev;
  24. }
  25. EXPORT_SYMBOL_GPL(rpmb_dev_get);
  26. /**
  27. * rpmb_dev_put() - decrease rpmb device ref counter
  28. * @rdev: rpmb device
  29. */
  30. void rpmb_dev_put(struct rpmb_dev *rdev)
  31. {
  32. if (rdev)
  33. put_device(&rdev->dev);
  34. }
  35. EXPORT_SYMBOL_GPL(rpmb_dev_put);
  36. /**
  37. * rpmb_route_frames() - route rpmb frames to rpmb device
  38. * @rdev: rpmb device
  39. * @req: rpmb request frames
  40. * @req_len: length of rpmb request frames in bytes
  41. * @rsp: rpmb response frames
  42. * @rsp_len: length of rpmb response frames in bytes
  43. *
  44. * Returns: < 0 on failure
  45. */
  46. int rpmb_route_frames(struct rpmb_dev *rdev, u8 *req,
  47. unsigned int req_len, u8 *rsp, unsigned int rsp_len)
  48. {
  49. if (!req || !req_len || !rsp || !rsp_len)
  50. return -EINVAL;
  51. return rdev->descr.route_frames(rdev->dev.parent, req, req_len,
  52. rsp, rsp_len);
  53. }
  54. EXPORT_SYMBOL_GPL(rpmb_route_frames);
  55. static void rpmb_dev_release(struct device *dev)
  56. {
  57. struct rpmb_dev *rdev = to_rpmb_dev(dev);
  58. ida_free(&rpmb_ida, rdev->id);
  59. kfree(rdev->descr.dev_id);
  60. kfree(rdev);
  61. }
  62. static struct class rpmb_class = {
  63. .name = "rpmb",
  64. .dev_release = rpmb_dev_release,
  65. };
  66. /**
  67. * rpmb_dev_find_device() - return first matching rpmb device
  68. * @start: rpmb device to begin with
  69. * @data: data for the match function
  70. * @match: the matching function
  71. *
  72. * Iterate over registered RPMB devices, and call @match() for each passing
  73. * it the RPMB device and @data.
  74. *
  75. * The return value of @match() is checked for each call. If it returns
  76. * anything other 0, break and return the found RPMB device.
  77. *
  78. * It's the callers responsibility to call rpmb_dev_put() on the returned
  79. * device, when it's done with it.
  80. *
  81. * Returns: a matching rpmb device or NULL on failure
  82. */
  83. struct rpmb_dev *rpmb_dev_find_device(const void *data,
  84. const struct rpmb_dev *start,
  85. int (*match)(struct device *dev,
  86. const void *data))
  87. {
  88. struct device *dev;
  89. const struct device *start_dev = NULL;
  90. if (start)
  91. start_dev = &start->dev;
  92. dev = class_find_device(&rpmb_class, start_dev, data, match);
  93. return dev ? to_rpmb_dev(dev) : NULL;
  94. }
  95. EXPORT_SYMBOL_GPL(rpmb_dev_find_device);
  96. int rpmb_interface_register(struct class_interface *intf)
  97. {
  98. intf->class = &rpmb_class;
  99. return class_interface_register(intf);
  100. }
  101. EXPORT_SYMBOL_GPL(rpmb_interface_register);
  102. void rpmb_interface_unregister(struct class_interface *intf)
  103. {
  104. class_interface_unregister(intf);
  105. }
  106. EXPORT_SYMBOL_GPL(rpmb_interface_unregister);
  107. /**
  108. * rpmb_dev_unregister() - unregister RPMB partition from the RPMB subsystem
  109. * @rdev: the rpmb device to unregister
  110. *
  111. * This function should be called from the release function of the
  112. * underlying device used when the RPMB device was registered.
  113. *
  114. * Returns: < 0 on failure
  115. */
  116. int rpmb_dev_unregister(struct rpmb_dev *rdev)
  117. {
  118. if (!rdev)
  119. return -EINVAL;
  120. device_del(&rdev->dev);
  121. rpmb_dev_put(rdev);
  122. return 0;
  123. }
  124. EXPORT_SYMBOL_GPL(rpmb_dev_unregister);
  125. /**
  126. * rpmb_dev_register - register RPMB partition with the RPMB subsystem
  127. * @dev: storage device of the rpmb device
  128. * @descr: RPMB device description
  129. *
  130. * While registering the RPMB partition extract needed device information
  131. * while needed resources are available.
  132. *
  133. * Returns: a pointer to a 'struct rpmb_dev' or an ERR_PTR on failure
  134. */
  135. struct rpmb_dev *rpmb_dev_register(struct device *dev,
  136. struct rpmb_descr *descr)
  137. {
  138. struct rpmb_dev *rdev;
  139. int ret;
  140. if (!dev || !descr || !descr->route_frames || !descr->dev_id ||
  141. !descr->dev_id_len)
  142. return ERR_PTR(-EINVAL);
  143. rdev = kzalloc_obj(*rdev);
  144. if (!rdev)
  145. return ERR_PTR(-ENOMEM);
  146. rdev->descr = *descr;
  147. rdev->descr.dev_id = kmemdup(descr->dev_id, descr->dev_id_len,
  148. GFP_KERNEL);
  149. if (!rdev->descr.dev_id) {
  150. ret = -ENOMEM;
  151. goto err_free_rdev;
  152. }
  153. ret = ida_alloc(&rpmb_ida, GFP_KERNEL);
  154. if (ret < 0)
  155. goto err_free_dev_id;
  156. rdev->id = ret;
  157. dev_set_name(&rdev->dev, "rpmb%d", rdev->id);
  158. rdev->dev.class = &rpmb_class;
  159. rdev->dev.parent = dev;
  160. ret = device_register(&rdev->dev);
  161. if (ret) {
  162. put_device(&rdev->dev);
  163. return ERR_PTR(ret);
  164. }
  165. dev_dbg(&rdev->dev, "registered device\n");
  166. return rdev;
  167. err_free_dev_id:
  168. kfree(rdev->descr.dev_id);
  169. err_free_rdev:
  170. kfree(rdev);
  171. return ERR_PTR(ret);
  172. }
  173. EXPORT_SYMBOL_GPL(rpmb_dev_register);
  174. static int __init rpmb_init(void)
  175. {
  176. int ret;
  177. ret = class_register(&rpmb_class);
  178. if (ret) {
  179. pr_err("couldn't create class\n");
  180. return ret;
  181. }
  182. ida_init(&rpmb_ida);
  183. return 0;
  184. }
  185. static void __exit rpmb_exit(void)
  186. {
  187. ida_destroy(&rpmb_ida);
  188. class_unregister(&rpmb_class);
  189. }
  190. subsys_initcall(rpmb_init);
  191. module_exit(rpmb_exit);
  192. MODULE_AUTHOR("Jens Wiklander <jens.wiklander@linaro.org>");
  193. MODULE_DESCRIPTION("RPMB class");
  194. MODULE_LICENSE("GPL");