attribute_container.c 12 KB

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