industrialio-sw-device.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * The Industrial I/O core, software IIO devices functions
  3. *
  4. * Copyright (c) 2016 Intel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/kmod.h>
  13. #include <linux/list.h>
  14. #include <linux/slab.h>
  15. #include <linux/iio/sw_device.h>
  16. #include <linux/iio/configfs.h>
  17. #include <linux/configfs.h>
  18. static struct config_group *iio_devices_group;
  19. static const struct config_item_type iio_device_type_group_type;
  20. static const struct config_item_type iio_devices_group_type = {
  21. .ct_owner = THIS_MODULE,
  22. };
  23. static LIST_HEAD(iio_device_types_list);
  24. static DEFINE_MUTEX(iio_device_types_lock);
  25. static
  26. struct iio_sw_device_type *__iio_find_sw_device_type(const char *name,
  27. unsigned len)
  28. {
  29. struct iio_sw_device_type *d = NULL, *iter;
  30. list_for_each_entry(iter, &iio_device_types_list, list)
  31. if (!strcmp(iter->name, name)) {
  32. d = iter;
  33. break;
  34. }
  35. return d;
  36. }
  37. int iio_register_sw_device_type(struct iio_sw_device_type *d)
  38. {
  39. struct iio_sw_device_type *iter;
  40. int ret = 0;
  41. mutex_lock(&iio_device_types_lock);
  42. iter = __iio_find_sw_device_type(d->name, strlen(d->name));
  43. if (iter)
  44. ret = -EBUSY;
  45. else
  46. list_add_tail(&d->list, &iio_device_types_list);
  47. mutex_unlock(&iio_device_types_lock);
  48. if (ret)
  49. return ret;
  50. d->group = configfs_register_default_group(iio_devices_group, d->name,
  51. &iio_device_type_group_type);
  52. if (IS_ERR(d->group))
  53. ret = PTR_ERR(d->group);
  54. return ret;
  55. }
  56. EXPORT_SYMBOL(iio_register_sw_device_type);
  57. void iio_unregister_sw_device_type(struct iio_sw_device_type *dt)
  58. {
  59. struct iio_sw_device_type *iter;
  60. mutex_lock(&iio_device_types_lock);
  61. iter = __iio_find_sw_device_type(dt->name, strlen(dt->name));
  62. if (iter)
  63. list_del(&dt->list);
  64. mutex_unlock(&iio_device_types_lock);
  65. configfs_unregister_default_group(dt->group);
  66. }
  67. EXPORT_SYMBOL(iio_unregister_sw_device_type);
  68. static
  69. struct iio_sw_device_type *iio_get_sw_device_type(const char *name)
  70. {
  71. struct iio_sw_device_type *dt;
  72. mutex_lock(&iio_device_types_lock);
  73. dt = __iio_find_sw_device_type(name, strlen(name));
  74. if (dt && !try_module_get(dt->owner))
  75. dt = NULL;
  76. mutex_unlock(&iio_device_types_lock);
  77. return dt;
  78. }
  79. struct iio_sw_device *iio_sw_device_create(const char *type, const char *name)
  80. {
  81. struct iio_sw_device *d;
  82. struct iio_sw_device_type *dt;
  83. dt = iio_get_sw_device_type(type);
  84. if (!dt) {
  85. pr_err("Invalid device type: %s\n", type);
  86. return ERR_PTR(-EINVAL);
  87. }
  88. d = dt->ops->probe(name);
  89. if (IS_ERR(d))
  90. goto out_module_put;
  91. d->device_type = dt;
  92. return d;
  93. out_module_put:
  94. module_put(dt->owner);
  95. return d;
  96. }
  97. EXPORT_SYMBOL(iio_sw_device_create);
  98. void iio_sw_device_destroy(struct iio_sw_device *d)
  99. {
  100. struct iio_sw_device_type *dt = d->device_type;
  101. dt->ops->remove(d);
  102. module_put(dt->owner);
  103. }
  104. EXPORT_SYMBOL(iio_sw_device_destroy);
  105. static struct config_group *device_make_group(struct config_group *group,
  106. const char *name)
  107. {
  108. struct iio_sw_device *d;
  109. d = iio_sw_device_create(group->cg_item.ci_name, name);
  110. if (IS_ERR(d))
  111. return ERR_CAST(d);
  112. config_item_set_name(&d->group.cg_item, "%s", name);
  113. return &d->group;
  114. }
  115. static void device_drop_group(struct config_group *group,
  116. struct config_item *item)
  117. {
  118. struct iio_sw_device *d = to_iio_sw_device(item);
  119. iio_sw_device_destroy(d);
  120. config_item_put(item);
  121. }
  122. static struct configfs_group_operations device_ops = {
  123. .make_group = &device_make_group,
  124. .drop_item = &device_drop_group,
  125. };
  126. static const struct config_item_type iio_device_type_group_type = {
  127. .ct_group_ops = &device_ops,
  128. .ct_owner = THIS_MODULE,
  129. };
  130. static int __init iio_sw_device_init(void)
  131. {
  132. iio_devices_group =
  133. configfs_register_default_group(&iio_configfs_subsys.su_group,
  134. "devices",
  135. &iio_devices_group_type);
  136. return PTR_ERR_OR_ZERO(iio_devices_group);
  137. }
  138. module_init(iio_sw_device_init);
  139. static void __exit iio_sw_device_exit(void)
  140. {
  141. configfs_unregister_default_group(iio_devices_group);
  142. }
  143. module_exit(iio_sw_device_exit);
  144. MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
  145. MODULE_DESCRIPTION("Industrial I/O software devices support");
  146. MODULE_LICENSE("GPL v2");