cosm_bus.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Intel MIC Platform Software Stack (MPSS)
  3. *
  4. * Copyright(c) 2015 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, version 2, as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * The full GNU General Public License is included in this distribution in
  16. * the file called "COPYING".
  17. *
  18. * Intel MIC COSM Bus Driver
  19. */
  20. #include <linux/slab.h>
  21. #include <linux/module.h>
  22. #include <linux/idr.h>
  23. #include "cosm_bus.h"
  24. /* Unique numbering for cosm devices. */
  25. static DEFINE_IDA(cosm_index_ida);
  26. static int cosm_dev_probe(struct device *d)
  27. {
  28. struct cosm_device *dev = dev_to_cosm(d);
  29. struct cosm_driver *drv = drv_to_cosm(dev->dev.driver);
  30. return drv->probe(dev);
  31. }
  32. static int cosm_dev_remove(struct device *d)
  33. {
  34. struct cosm_device *dev = dev_to_cosm(d);
  35. struct cosm_driver *drv = drv_to_cosm(dev->dev.driver);
  36. drv->remove(dev);
  37. return 0;
  38. }
  39. static struct bus_type cosm_bus = {
  40. .name = "cosm_bus",
  41. .probe = cosm_dev_probe,
  42. .remove = cosm_dev_remove,
  43. };
  44. int cosm_register_driver(struct cosm_driver *driver)
  45. {
  46. driver->driver.bus = &cosm_bus;
  47. return driver_register(&driver->driver);
  48. }
  49. EXPORT_SYMBOL_GPL(cosm_register_driver);
  50. void cosm_unregister_driver(struct cosm_driver *driver)
  51. {
  52. driver_unregister(&driver->driver);
  53. }
  54. EXPORT_SYMBOL_GPL(cosm_unregister_driver);
  55. static inline void cosm_release_dev(struct device *d)
  56. {
  57. struct cosm_device *cdev = dev_to_cosm(d);
  58. kfree(cdev);
  59. }
  60. struct cosm_device *
  61. cosm_register_device(struct device *pdev, struct cosm_hw_ops *hw_ops)
  62. {
  63. struct cosm_device *cdev;
  64. int ret;
  65. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  66. if (!cdev)
  67. return ERR_PTR(-ENOMEM);
  68. cdev->dev.parent = pdev;
  69. cdev->dev.release = cosm_release_dev;
  70. cdev->hw_ops = hw_ops;
  71. dev_set_drvdata(&cdev->dev, cdev);
  72. cdev->dev.bus = &cosm_bus;
  73. /* Assign a unique device index and hence name */
  74. ret = ida_simple_get(&cosm_index_ida, 0, 0, GFP_KERNEL);
  75. if (ret < 0)
  76. goto free_cdev;
  77. cdev->index = ret;
  78. cdev->dev.id = ret;
  79. dev_set_name(&cdev->dev, "cosm-dev%u", cdev->index);
  80. ret = device_register(&cdev->dev);
  81. if (ret)
  82. goto ida_remove;
  83. return cdev;
  84. ida_remove:
  85. ida_simple_remove(&cosm_index_ida, cdev->index);
  86. free_cdev:
  87. put_device(&cdev->dev);
  88. return ERR_PTR(ret);
  89. }
  90. EXPORT_SYMBOL_GPL(cosm_register_device);
  91. void cosm_unregister_device(struct cosm_device *dev)
  92. {
  93. int index = dev->index; /* save for after device release */
  94. device_unregister(&dev->dev);
  95. ida_simple_remove(&cosm_index_ida, index);
  96. }
  97. EXPORT_SYMBOL_GPL(cosm_unregister_device);
  98. struct cosm_device *cosm_find_cdev_by_id(int id)
  99. {
  100. struct device *dev = subsys_find_device_by_id(&cosm_bus, id, NULL);
  101. return dev ? container_of(dev, struct cosm_device, dev) : NULL;
  102. }
  103. EXPORT_SYMBOL_GPL(cosm_find_cdev_by_id);
  104. static int __init cosm_init(void)
  105. {
  106. return bus_register(&cosm_bus);
  107. }
  108. static void __exit cosm_exit(void)
  109. {
  110. bus_unregister(&cosm_bus);
  111. ida_destroy(&cosm_index_ida);
  112. }
  113. core_initcall(cosm_init);
  114. module_exit(cosm_exit);
  115. MODULE_AUTHOR("Intel Corporation");
  116. MODULE_DESCRIPTION("Intel(R) MIC card OS state management bus driver");
  117. MODULE_LICENSE("GPL v2");