rpmb-core.c 5.3 KB

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