class_simple.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * class_simple.c - a "simple" interface for classes for simple char devices.
  3. *
  4. * Copyright (c) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
  5. * Copyright (c) 2003-2004 IBM Corp.
  6. *
  7. * This file is released under the GPLv2
  8. *
  9. */
  10. #include <linux/config.h>
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. struct class_simple {
  14. struct class class;
  15. };
  16. #define to_class_simple(d) container_of(d, struct class_simple, class)
  17. struct simple_dev {
  18. struct list_head node;
  19. struct class_device class_dev;
  20. };
  21. #define to_simple_dev(d) container_of(d, struct simple_dev, class_dev)
  22. static LIST_HEAD(simple_dev_list);
  23. static DEFINE_SPINLOCK(simple_dev_list_lock);
  24. static void release_simple_dev(struct class_device *class_dev)
  25. {
  26. struct simple_dev *s_dev = to_simple_dev(class_dev);
  27. kfree(s_dev);
  28. }
  29. static void class_simple_release(struct class *class)
  30. {
  31. struct class_simple *cs = to_class_simple(class);
  32. kfree(cs);
  33. }
  34. /**
  35. * class_simple_create - create a struct class_simple structure
  36. * @owner: pointer to the module that is to "own" this struct class_simple
  37. * @name: pointer to a string for the name of this class.
  38. *
  39. * This is used to create a struct class_simple pointer that can then be used
  40. * in calls to class_simple_device_add(). This is used when you do not wish to
  41. * create a full blown class support for a type of char devices.
  42. *
  43. * Note, the pointer created here is to be destroyed when finished by making a
  44. * call to class_simple_destroy().
  45. */
  46. struct class_simple *class_simple_create(struct module *owner, char *name)
  47. {
  48. struct class_simple *cs;
  49. int retval;
  50. cs = kmalloc(sizeof(*cs), GFP_KERNEL);
  51. if (!cs) {
  52. retval = -ENOMEM;
  53. goto error;
  54. }
  55. memset(cs, 0x00, sizeof(*cs));
  56. cs->class.name = name;
  57. cs->class.class_release = class_simple_release;
  58. cs->class.release = release_simple_dev;
  59. retval = class_register(&cs->class);
  60. if (retval)
  61. goto error;
  62. return cs;
  63. error:
  64. kfree(cs);
  65. return ERR_PTR(retval);
  66. }
  67. EXPORT_SYMBOL(class_simple_create);
  68. /**
  69. * class_simple_destroy - destroys a struct class_simple structure
  70. * @cs: pointer to the struct class_simple that is to be destroyed
  71. *
  72. * Note, the pointer to be destroyed must have been created with a call to
  73. * class_simple_create().
  74. */
  75. void class_simple_destroy(struct class_simple *cs)
  76. {
  77. if ((cs == NULL) || (IS_ERR(cs)))
  78. return;
  79. class_unregister(&cs->class);
  80. }
  81. EXPORT_SYMBOL(class_simple_destroy);
  82. /**
  83. * class_simple_device_add - adds a class device to sysfs for a character driver
  84. * @cs: pointer to the struct class_simple that this device should be registered to.
  85. * @dev: the dev_t for the device to be added.
  86. * @device: a pointer to a struct device that is assiociated with this class device.
  87. * @fmt: string for the class device's name
  88. *
  89. * This function can be used by simple char device classes that do not
  90. * implement their own class device registration. A struct class_device will
  91. * be created in sysfs, registered to the specified class. A "dev" file will
  92. * be created, showing the dev_t for the device. The pointer to the struct
  93. * class_device will be returned from the call. Any further sysfs files that
  94. * might be required can be created using this pointer.
  95. * Note: the struct class_simple passed to this function must have previously been
  96. * created with a call to class_simple_create().
  97. */
  98. struct class_device *class_simple_device_add(struct class_simple *cs, dev_t dev, struct device *device, const char *fmt, ...)
  99. {
  100. va_list args;
  101. struct simple_dev *s_dev = NULL;
  102. int retval;
  103. if ((cs == NULL) || (IS_ERR(cs))) {
  104. retval = -ENODEV;
  105. goto error;
  106. }
  107. s_dev = kmalloc(sizeof(*s_dev), GFP_KERNEL);
  108. if (!s_dev) {
  109. retval = -ENOMEM;
  110. goto error;
  111. }
  112. memset(s_dev, 0x00, sizeof(*s_dev));
  113. s_dev->class_dev.devt = dev;
  114. s_dev->class_dev.dev = device;
  115. s_dev->class_dev.class = &cs->class;
  116. va_start(args, fmt);
  117. vsnprintf(s_dev->class_dev.class_id, BUS_ID_SIZE, fmt, args);
  118. va_end(args);
  119. retval = class_device_register(&s_dev->class_dev);
  120. if (retval)
  121. goto error;
  122. spin_lock(&simple_dev_list_lock);
  123. list_add(&s_dev->node, &simple_dev_list);
  124. spin_unlock(&simple_dev_list_lock);
  125. return &s_dev->class_dev;
  126. error:
  127. kfree(s_dev);
  128. return ERR_PTR(retval);
  129. }
  130. EXPORT_SYMBOL(class_simple_device_add);
  131. /**
  132. * class_simple_set_hotplug - set the hotplug callback in the embedded struct class
  133. * @cs: pointer to the struct class_simple to hold the pointer
  134. * @hotplug: function pointer to the hotplug function
  135. *
  136. * Implement and set a hotplug function to add environment variables specific to this
  137. * class on the hotplug event.
  138. */
  139. int class_simple_set_hotplug(struct class_simple *cs,
  140. int (*hotplug)(struct class_device *dev, char **envp, int num_envp, char *buffer, int buffer_size))
  141. {
  142. if ((cs == NULL) || (IS_ERR(cs)))
  143. return -ENODEV;
  144. cs->class.hotplug = hotplug;
  145. return 0;
  146. }
  147. EXPORT_SYMBOL(class_simple_set_hotplug);
  148. /**
  149. * class_simple_device_remove - removes a class device that was created with class_simple_device_add()
  150. * @dev: the dev_t of the device that was previously registered.
  151. *
  152. * This call unregisters and cleans up a class device that was created with a
  153. * call to class_device_simple_add()
  154. */
  155. void class_simple_device_remove(dev_t dev)
  156. {
  157. struct simple_dev *s_dev = NULL;
  158. int found = 0;
  159. spin_lock(&simple_dev_list_lock);
  160. list_for_each_entry(s_dev, &simple_dev_list, node) {
  161. if (s_dev->class_dev.devt == dev) {
  162. found = 1;
  163. break;
  164. }
  165. }
  166. if (found) {
  167. list_del(&s_dev->node);
  168. spin_unlock(&simple_dev_list_lock);
  169. class_device_unregister(&s_dev->class_dev);
  170. } else {
  171. spin_unlock(&simple_dev_list_lock);
  172. }
  173. }
  174. EXPORT_SYMBOL(class_simple_device_remove);