attribute_container.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * attribute_container.c - implementation of a simple container for classes
  3. *
  4. * Copyright (c) 2005 - James Bottomley <James.Bottomley@steeleye.com>
  5. *
  6. * This file is licensed under GPLv2
  7. *
  8. * The basic idea here is to enable a device to be attached to an
  9. * aritrary numer of classes without having to allocate storage for them.
  10. * Instead, the contained classes select the devices they need to attach
  11. * to via a matching function.
  12. */
  13. #include <linux/attribute_container.h>
  14. #include <linux/init.h>
  15. #include <linux/device.h>
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/list.h>
  19. #include <linux/module.h>
  20. /* This is a private structure used to tie the classdev and the
  21. * container .. it should never be visible outside this file */
  22. struct internal_container {
  23. struct klist_node node;
  24. struct attribute_container *cont;
  25. struct class_device classdev;
  26. };
  27. /**
  28. * attribute_container_classdev_to_container - given a classdev, return the container
  29. *
  30. * @classdev: the class device created by attribute_container_add_device.
  31. *
  32. * Returns the container associated with this classdev.
  33. */
  34. struct attribute_container *
  35. attribute_container_classdev_to_container(struct class_device *classdev)
  36. {
  37. struct internal_container *ic =
  38. container_of(classdev, struct internal_container, classdev);
  39. return ic->cont;
  40. }
  41. EXPORT_SYMBOL_GPL(attribute_container_classdev_to_container);
  42. static struct list_head attribute_container_list;
  43. static DECLARE_MUTEX(attribute_container_mutex);
  44. /**
  45. * attribute_container_register - register an attribute container
  46. *
  47. * @cont: The container to register. This must be allocated by the
  48. * callee and should also be zeroed by it.
  49. */
  50. int
  51. attribute_container_register(struct attribute_container *cont)
  52. {
  53. INIT_LIST_HEAD(&cont->node);
  54. klist_init(&cont->containers);
  55. down(&attribute_container_mutex);
  56. list_add_tail(&cont->node, &attribute_container_list);
  57. up(&attribute_container_mutex);
  58. return 0;
  59. }
  60. EXPORT_SYMBOL_GPL(attribute_container_register);
  61. /**
  62. * attribute_container_unregister - remove a container registration
  63. *
  64. * @cont: previously registered container to remove
  65. */
  66. int
  67. attribute_container_unregister(struct attribute_container *cont)
  68. {
  69. int retval = -EBUSY;
  70. down(&attribute_container_mutex);
  71. spin_lock(&cont->containers.k_lock);
  72. if (!list_empty(&cont->containers.k_list))
  73. goto out;
  74. retval = 0;
  75. list_del(&cont->node);
  76. out:
  77. spin_unlock(&cont->containers.k_lock);
  78. up(&attribute_container_mutex);
  79. return retval;
  80. }
  81. EXPORT_SYMBOL_GPL(attribute_container_unregister);
  82. /* private function used as class release */
  83. static void attribute_container_release(struct class_device *classdev)
  84. {
  85. struct internal_container *ic
  86. = container_of(classdev, struct internal_container, classdev);
  87. struct device *dev = classdev->dev;
  88. kfree(ic);
  89. put_device(dev);
  90. }
  91. /**
  92. * attribute_container_add_device - see if any container is interested in dev
  93. *
  94. * @dev: device to add attributes to
  95. * @fn: function to trigger addition of class device.
  96. *
  97. * This function allocates storage for the class device(s) to be
  98. * attached to dev (one for each matching attribute_container). If no
  99. * fn is provided, the code will simply register the class device via
  100. * class_device_add. If a function is provided, it is expected to add
  101. * the class device at the appropriate time. One of the things that
  102. * might be necessary is to allocate and initialise the classdev and
  103. * then add it a later time. To do this, call this routine for
  104. * allocation and initialisation and then use
  105. * attribute_container_device_trigger() to call class_device_add() on
  106. * it. Note: after this, the class device contains a reference to dev
  107. * which is not relinquished until the release of the classdev.
  108. */
  109. void
  110. attribute_container_add_device(struct device *dev,
  111. int (*fn)(struct attribute_container *,
  112. struct device *,
  113. struct class_device *))
  114. {
  115. struct attribute_container *cont;
  116. down(&attribute_container_mutex);
  117. list_for_each_entry(cont, &attribute_container_list, node) {
  118. struct internal_container *ic;
  119. if (attribute_container_no_classdevs(cont))
  120. continue;
  121. if (!cont->match(cont, dev))
  122. continue;
  123. ic = kmalloc(sizeof(struct internal_container), GFP_KERNEL);
  124. if (!ic) {
  125. dev_printk(KERN_ERR, dev, "failed to allocate class container\n");
  126. continue;
  127. }
  128. memset(ic, 0, sizeof(struct internal_container));
  129. ic->cont = cont;
  130. class_device_initialize(&ic->classdev);
  131. ic->classdev.dev = get_device(dev);
  132. ic->classdev.class = cont->class;
  133. cont->class->release = attribute_container_release;
  134. strcpy(ic->classdev.class_id, dev->bus_id);
  135. if (fn)
  136. fn(cont, dev, &ic->classdev);
  137. else
  138. attribute_container_add_class_device(&ic->classdev);
  139. klist_add_tail(&ic->node, &cont->containers);
  140. }
  141. up(&attribute_container_mutex);
  142. }
  143. /* FIXME: can't break out of this unless klist_iter_exit is also
  144. * called before doing the break
  145. */
  146. #define klist_for_each_entry(pos, head, member, iter) \
  147. for (klist_iter_init(head, iter); (pos = ({ \
  148. struct klist_node *n = klist_next(iter); \
  149. n ? ({ klist_iter_exit(iter) ; NULL; }) : \
  150. container_of(n, typeof(*pos), member);\
  151. }) ) != NULL; )
  152. /**
  153. * attribute_container_remove_device - make device eligible for removal.
  154. *
  155. * @dev: The generic device
  156. * @fn: A function to call to remove the device
  157. *
  158. * This routine triggers device removal. If fn is NULL, then it is
  159. * simply done via class_device_unregister (note that if something
  160. * still has a reference to the classdev, then the memory occupied
  161. * will not be freed until the classdev is released). If you want a
  162. * two phase release: remove from visibility and then delete the
  163. * device, then you should use this routine with a fn that calls
  164. * class_device_del() and then use
  165. * attribute_container_device_trigger() to do the final put on the
  166. * classdev.
  167. */
  168. void
  169. attribute_container_remove_device(struct device *dev,
  170. void (*fn)(struct attribute_container *,
  171. struct device *,
  172. struct class_device *))
  173. {
  174. struct attribute_container *cont;
  175. down(&attribute_container_mutex);
  176. list_for_each_entry(cont, &attribute_container_list, node) {
  177. struct internal_container *ic;
  178. struct klist_iter iter;
  179. if (attribute_container_no_classdevs(cont))
  180. continue;
  181. if (!cont->match(cont, dev))
  182. continue;
  183. klist_for_each_entry(ic, &cont->containers, node, &iter) {
  184. if (dev != ic->classdev.dev)
  185. continue;
  186. klist_remove(&ic->node);
  187. if (fn)
  188. fn(cont, dev, &ic->classdev);
  189. else {
  190. attribute_container_remove_attrs(&ic->classdev);
  191. class_device_unregister(&ic->classdev);
  192. }
  193. }
  194. }
  195. up(&attribute_container_mutex);
  196. }
  197. EXPORT_SYMBOL_GPL(attribute_container_remove_device);
  198. /**
  199. * attribute_container_device_trigger - execute a trigger for each matching classdev
  200. *
  201. * @dev: The generic device to run the trigger for
  202. * @fn the function to execute for each classdev.
  203. *
  204. * This funcion is for executing a trigger when you need to know both
  205. * the container and the classdev. If you only care about the
  206. * container, then use attribute_container_trigger() instead.
  207. */
  208. void
  209. attribute_container_device_trigger(struct device *dev,
  210. int (*fn)(struct attribute_container *,
  211. struct device *,
  212. struct class_device *))
  213. {
  214. struct attribute_container *cont;
  215. down(&attribute_container_mutex);
  216. list_for_each_entry(cont, &attribute_container_list, node) {
  217. struct internal_container *ic;
  218. struct klist_iter iter;
  219. if (!cont->match(cont, dev))
  220. continue;
  221. if (attribute_container_no_classdevs(cont)) {
  222. fn(cont, dev, NULL);
  223. continue;
  224. }
  225. klist_for_each_entry(ic, &cont->containers, node, &iter) {
  226. if (dev == ic->classdev.dev)
  227. fn(cont, dev, &ic->classdev);
  228. }
  229. }
  230. up(&attribute_container_mutex);
  231. }
  232. EXPORT_SYMBOL_GPL(attribute_container_device_trigger);
  233. /**
  234. * attribute_container_trigger - trigger a function for each matching container
  235. *
  236. * @dev: The generic device to activate the trigger for
  237. * @fn: the function to trigger
  238. *
  239. * This routine triggers a function that only needs to know the
  240. * matching containers (not the classdev) associated with a device.
  241. * It is more lightweight than attribute_container_device_trigger, so
  242. * should be used in preference unless the triggering function
  243. * actually needs to know the classdev.
  244. */
  245. void
  246. attribute_container_trigger(struct device *dev,
  247. int (*fn)(struct attribute_container *,
  248. struct device *))
  249. {
  250. struct attribute_container *cont;
  251. down(&attribute_container_mutex);
  252. list_for_each_entry(cont, &attribute_container_list, node) {
  253. if (cont->match(cont, dev))
  254. fn(cont, dev);
  255. }
  256. up(&attribute_container_mutex);
  257. }
  258. EXPORT_SYMBOL_GPL(attribute_container_trigger);
  259. /**
  260. * attribute_container_add_attrs - add attributes
  261. *
  262. * @classdev: The class device
  263. *
  264. * This simply creates all the class device sysfs files from the
  265. * attributes listed in the container
  266. */
  267. int
  268. attribute_container_add_attrs(struct class_device *classdev)
  269. {
  270. struct attribute_container *cont =
  271. attribute_container_classdev_to_container(classdev);
  272. struct class_device_attribute **attrs = cont->attrs;
  273. int i, error;
  274. if (!attrs)
  275. return 0;
  276. for (i = 0; attrs[i]; i++) {
  277. error = class_device_create_file(classdev, attrs[i]);
  278. if (error)
  279. return error;
  280. }
  281. return 0;
  282. }
  283. EXPORT_SYMBOL_GPL(attribute_container_add_attrs);
  284. /**
  285. * attribute_container_add_class_device - same function as class_device_add
  286. *
  287. * @classdev: the class device to add
  288. *
  289. * This performs essentially the same function as class_device_add except for
  290. * attribute containers, namely add the classdev to the system and then
  291. * create the attribute files
  292. */
  293. int
  294. attribute_container_add_class_device(struct class_device *classdev)
  295. {
  296. int error = class_device_add(classdev);
  297. if (error)
  298. return error;
  299. return attribute_container_add_attrs(classdev);
  300. }
  301. EXPORT_SYMBOL_GPL(attribute_container_add_class_device);
  302. /**
  303. * attribute_container_add_class_device_adapter - simple adapter for triggers
  304. *
  305. * This function is identical to attribute_container_add_class_device except
  306. * that it is designed to be called from the triggers
  307. */
  308. int
  309. attribute_container_add_class_device_adapter(struct attribute_container *cont,
  310. struct device *dev,
  311. struct class_device *classdev)
  312. {
  313. return attribute_container_add_class_device(classdev);
  314. }
  315. EXPORT_SYMBOL_GPL(attribute_container_add_class_device_adapter);
  316. /**
  317. * attribute_container_remove_attrs - remove any attribute files
  318. *
  319. * @classdev: The class device to remove the files from
  320. *
  321. */
  322. void
  323. attribute_container_remove_attrs(struct class_device *classdev)
  324. {
  325. struct attribute_container *cont =
  326. attribute_container_classdev_to_container(classdev);
  327. struct class_device_attribute **attrs = cont->attrs;
  328. int i;
  329. if (!attrs)
  330. return;
  331. for (i = 0; attrs[i]; i++)
  332. class_device_remove_file(classdev, attrs[i]);
  333. }
  334. EXPORT_SYMBOL_GPL(attribute_container_remove_attrs);
  335. /**
  336. * attribute_container_class_device_del - equivalent of class_device_del
  337. *
  338. * @classdev: the class device
  339. *
  340. * This function simply removes all the attribute files and then calls
  341. * class_device_del.
  342. */
  343. void
  344. attribute_container_class_device_del(struct class_device *classdev)
  345. {
  346. attribute_container_remove_attrs(classdev);
  347. class_device_del(classdev);
  348. }
  349. EXPORT_SYMBOL_GPL(attribute_container_class_device_del);
  350. /**
  351. * attribute_container_find_class_device - find the corresponding class_device
  352. *
  353. * @cont: the container
  354. * @dev: the generic device
  355. *
  356. * Looks up the device in the container's list of class devices and returns
  357. * the corresponding class_device.
  358. */
  359. struct class_device *
  360. attribute_container_find_class_device(struct attribute_container *cont,
  361. struct device *dev)
  362. {
  363. struct class_device *cdev = NULL;
  364. struct internal_container *ic;
  365. struct klist_iter iter;
  366. klist_for_each_entry(ic, &cont->containers, node, &iter) {
  367. if (ic->classdev.dev == dev) {
  368. cdev = &ic->classdev;
  369. /* FIXME: must exit iterator then break */
  370. klist_iter_exit(&iter);
  371. break;
  372. }
  373. }
  374. return cdev;
  375. }
  376. EXPORT_SYMBOL_GPL(attribute_container_find_class_device);
  377. int __init
  378. attribute_container_init(void)
  379. {
  380. INIT_LIST_HEAD(&attribute_container_list);
  381. return 0;
  382. }