core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * drivers/base/core.c - core driver model code (device registration, etc)
  3. *
  4. * Copyright (c) 2002-3 Patrick Mochel
  5. * Copyright (c) 2002-3 Open Source Development Labs
  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. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/string.h>
  17. #include <asm/semaphore.h>
  18. #include "base.h"
  19. #include "power/power.h"
  20. int (*platform_notify)(struct device * dev) = NULL;
  21. int (*platform_notify_remove)(struct device * dev) = NULL;
  22. /*
  23. * sysfs bindings for devices.
  24. */
  25. #define to_dev(obj) container_of(obj, struct device, kobj)
  26. #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
  27. static ssize_t
  28. dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
  29. {
  30. struct device_attribute * dev_attr = to_dev_attr(attr);
  31. struct device * dev = to_dev(kobj);
  32. ssize_t ret = -EIO;
  33. if (dev_attr->show)
  34. ret = dev_attr->show(dev, dev_attr, buf);
  35. return ret;
  36. }
  37. static ssize_t
  38. dev_attr_store(struct kobject * kobj, struct attribute * attr,
  39. const char * buf, size_t count)
  40. {
  41. struct device_attribute * dev_attr = to_dev_attr(attr);
  42. struct device * dev = to_dev(kobj);
  43. ssize_t ret = -EIO;
  44. if (dev_attr->store)
  45. ret = dev_attr->store(dev, dev_attr, buf, count);
  46. return ret;
  47. }
  48. static struct sysfs_ops dev_sysfs_ops = {
  49. .show = dev_attr_show,
  50. .store = dev_attr_store,
  51. };
  52. /**
  53. * device_release - free device structure.
  54. * @kobj: device's kobject.
  55. *
  56. * This is called once the reference count for the object
  57. * reaches 0. We forward the call to the device's release
  58. * method, which should handle actually freeing the structure.
  59. */
  60. static void device_release(struct kobject * kobj)
  61. {
  62. struct device * dev = to_dev(kobj);
  63. if (dev->release)
  64. dev->release(dev);
  65. else {
  66. printk(KERN_ERR "Device '%s' does not have a release() function, "
  67. "it is broken and must be fixed.\n",
  68. dev->bus_id);
  69. WARN_ON(1);
  70. }
  71. }
  72. static struct kobj_type ktype_device = {
  73. .release = device_release,
  74. .sysfs_ops = &dev_sysfs_ops,
  75. };
  76. static int dev_hotplug_filter(struct kset *kset, struct kobject *kobj)
  77. {
  78. struct kobj_type *ktype = get_ktype(kobj);
  79. if (ktype == &ktype_device) {
  80. struct device *dev = to_dev(kobj);
  81. if (dev->bus)
  82. return 1;
  83. }
  84. return 0;
  85. }
  86. static const char *dev_hotplug_name(struct kset *kset, struct kobject *kobj)
  87. {
  88. struct device *dev = to_dev(kobj);
  89. return dev->bus->name;
  90. }
  91. static int dev_hotplug(struct kset *kset, struct kobject *kobj, char **envp,
  92. int num_envp, char *buffer, int buffer_size)
  93. {
  94. struct device *dev = to_dev(kobj);
  95. int i = 0;
  96. int length = 0;
  97. int retval = 0;
  98. /* add bus name of physical device */
  99. if (dev->bus)
  100. add_hotplug_env_var(envp, num_envp, &i,
  101. buffer, buffer_size, &length,
  102. "PHYSDEVBUS=%s", dev->bus->name);
  103. /* add driver name of physical device */
  104. if (dev->driver)
  105. add_hotplug_env_var(envp, num_envp, &i,
  106. buffer, buffer_size, &length,
  107. "PHYSDEVDRIVER=%s", dev->driver->name);
  108. /* terminate, set to next free slot, shrink available space */
  109. envp[i] = NULL;
  110. envp = &envp[i];
  111. num_envp -= i;
  112. buffer = &buffer[length];
  113. buffer_size -= length;
  114. if (dev->bus && dev->bus->hotplug) {
  115. /* have the bus specific function add its stuff */
  116. retval = dev->bus->hotplug (dev, envp, num_envp, buffer, buffer_size);
  117. if (retval) {
  118. pr_debug ("%s - hotplug() returned %d\n",
  119. __FUNCTION__, retval);
  120. }
  121. }
  122. return retval;
  123. }
  124. static struct kset_hotplug_ops device_hotplug_ops = {
  125. .filter = dev_hotplug_filter,
  126. .name = dev_hotplug_name,
  127. .hotplug = dev_hotplug,
  128. };
  129. /**
  130. * device_subsys - structure to be registered with kobject core.
  131. */
  132. decl_subsys(devices, &ktype_device, &device_hotplug_ops);
  133. /**
  134. * device_create_file - create sysfs attribute file for device.
  135. * @dev: device.
  136. * @attr: device attribute descriptor.
  137. */
  138. int device_create_file(struct device * dev, struct device_attribute * attr)
  139. {
  140. int error = 0;
  141. if (get_device(dev)) {
  142. error = sysfs_create_file(&dev->kobj, &attr->attr);
  143. put_device(dev);
  144. }
  145. return error;
  146. }
  147. /**
  148. * device_remove_file - remove sysfs attribute file.
  149. * @dev: device.
  150. * @attr: device attribute descriptor.
  151. */
  152. void device_remove_file(struct device * dev, struct device_attribute * attr)
  153. {
  154. if (get_device(dev)) {
  155. sysfs_remove_file(&dev->kobj, &attr->attr);
  156. put_device(dev);
  157. }
  158. }
  159. static void klist_children_get(struct klist_node *n)
  160. {
  161. struct device *dev = container_of(n, struct device, knode_parent);
  162. get_device(dev);
  163. }
  164. static void klist_children_put(struct klist_node *n)
  165. {
  166. struct device *dev = container_of(n, struct device, knode_parent);
  167. put_device(dev);
  168. }
  169. /**
  170. * device_initialize - init device structure.
  171. * @dev: device.
  172. *
  173. * This prepares the device for use by other layers,
  174. * including adding it to the device hierarchy.
  175. * It is the first half of device_register(), if called by
  176. * that, though it can also be called separately, so one
  177. * may use @dev's fields (e.g. the refcount).
  178. */
  179. void device_initialize(struct device *dev)
  180. {
  181. kobj_set_kset_s(dev, devices_subsys);
  182. kobject_init(&dev->kobj);
  183. klist_init(&dev->klist_children, klist_children_get,
  184. klist_children_put);
  185. INIT_LIST_HEAD(&dev->dma_pools);
  186. init_MUTEX(&dev->sem);
  187. }
  188. /**
  189. * device_add - add device to device hierarchy.
  190. * @dev: device.
  191. *
  192. * This is part 2 of device_register(), though may be called
  193. * separately _iff_ device_initialize() has been called separately.
  194. *
  195. * This adds it to the kobject hierarchy via kobject_add(), adds it
  196. * to the global and sibling lists for the device, then
  197. * adds it to the other relevant subsystems of the driver model.
  198. */
  199. int device_add(struct device *dev)
  200. {
  201. struct device *parent = NULL;
  202. int error = -EINVAL;
  203. dev = get_device(dev);
  204. if (!dev || !strlen(dev->bus_id))
  205. goto Error;
  206. parent = get_device(dev->parent);
  207. pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
  208. /* first, register with generic layer. */
  209. kobject_set_name(&dev->kobj, "%s", dev->bus_id);
  210. if (parent)
  211. dev->kobj.parent = &parent->kobj;
  212. if ((error = kobject_add(&dev->kobj)))
  213. goto Error;
  214. kobject_hotplug(&dev->kobj, KOBJ_ADD);
  215. if ((error = device_pm_add(dev)))
  216. goto PMError;
  217. if ((error = bus_add_device(dev)))
  218. goto BusError;
  219. if (parent)
  220. klist_add_tail(&dev->knode_parent, &parent->klist_children);
  221. /* notify platform of device entry */
  222. if (platform_notify)
  223. platform_notify(dev);
  224. Done:
  225. put_device(dev);
  226. return error;
  227. BusError:
  228. device_pm_remove(dev);
  229. PMError:
  230. kobject_hotplug(&dev->kobj, KOBJ_REMOVE);
  231. kobject_del(&dev->kobj);
  232. Error:
  233. if (parent)
  234. put_device(parent);
  235. goto Done;
  236. }
  237. /**
  238. * device_register - register a device with the system.
  239. * @dev: pointer to the device structure
  240. *
  241. * This happens in two clean steps - initialize the device
  242. * and add it to the system. The two steps can be called
  243. * separately, but this is the easiest and most common.
  244. * I.e. you should only call the two helpers separately if
  245. * have a clearly defined need to use and refcount the device
  246. * before it is added to the hierarchy.
  247. */
  248. int device_register(struct device *dev)
  249. {
  250. device_initialize(dev);
  251. return device_add(dev);
  252. }
  253. /**
  254. * get_device - increment reference count for device.
  255. * @dev: device.
  256. *
  257. * This simply forwards the call to kobject_get(), though
  258. * we do take care to provide for the case that we get a NULL
  259. * pointer passed in.
  260. */
  261. struct device * get_device(struct device * dev)
  262. {
  263. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  264. }
  265. /**
  266. * put_device - decrement reference count.
  267. * @dev: device in question.
  268. */
  269. void put_device(struct device * dev)
  270. {
  271. if (dev)
  272. kobject_put(&dev->kobj);
  273. }
  274. /**
  275. * device_del - delete device from system.
  276. * @dev: device.
  277. *
  278. * This is the first part of the device unregistration
  279. * sequence. This removes the device from the lists we control
  280. * from here, has it removed from the other driver model
  281. * subsystems it was added to in device_add(), and removes it
  282. * from the kobject hierarchy.
  283. *
  284. * NOTE: this should be called manually _iff_ device_add() was
  285. * also called manually.
  286. */
  287. void device_del(struct device * dev)
  288. {
  289. struct device * parent = dev->parent;
  290. if (parent)
  291. klist_del(&dev->knode_parent);
  292. /* Notify the platform of the removal, in case they
  293. * need to do anything...
  294. */
  295. if (platform_notify_remove)
  296. platform_notify_remove(dev);
  297. bus_remove_device(dev);
  298. device_pm_remove(dev);
  299. kobject_hotplug(&dev->kobj, KOBJ_REMOVE);
  300. kobject_del(&dev->kobj);
  301. if (parent)
  302. put_device(parent);
  303. }
  304. /**
  305. * device_unregister - unregister device from system.
  306. * @dev: device going away.
  307. *
  308. * We do this in two parts, like we do device_register(). First,
  309. * we remove it from all the subsystems with device_del(), then
  310. * we decrement the reference count via put_device(). If that
  311. * is the final reference count, the device will be cleaned up
  312. * via device_release() above. Otherwise, the structure will
  313. * stick around until the final reference to the device is dropped.
  314. */
  315. void device_unregister(struct device * dev)
  316. {
  317. pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
  318. device_del(dev);
  319. put_device(dev);
  320. }
  321. static struct device * next_device(struct klist_iter * i)
  322. {
  323. struct klist_node * n = klist_next(i);
  324. return n ? container_of(n, struct device, knode_parent) : NULL;
  325. }
  326. /**
  327. * device_for_each_child - device child iterator.
  328. * @dev: parent struct device.
  329. * @data: data for the callback.
  330. * @fn: function to be called for each device.
  331. *
  332. * Iterate over @dev's child devices, and call @fn for each,
  333. * passing it @data.
  334. *
  335. * We check the return of @fn each time. If it returns anything
  336. * other than 0, we break out and return that value.
  337. */
  338. int device_for_each_child(struct device * parent, void * data,
  339. int (*fn)(struct device *, void *))
  340. {
  341. struct klist_iter i;
  342. struct device * child;
  343. int error = 0;
  344. klist_iter_init(&parent->klist_children, &i);
  345. while ((child = next_device(&i)) && !error)
  346. error = fn(child, data);
  347. klist_iter_exit(&i);
  348. return error;
  349. }
  350. int __init devices_init(void)
  351. {
  352. return subsystem_register(&devices_subsys);
  353. }
  354. EXPORT_SYMBOL_GPL(device_for_each_child);
  355. EXPORT_SYMBOL_GPL(device_initialize);
  356. EXPORT_SYMBOL_GPL(device_add);
  357. EXPORT_SYMBOL_GPL(device_register);
  358. EXPORT_SYMBOL_GPL(device_del);
  359. EXPORT_SYMBOL_GPL(device_unregister);
  360. EXPORT_SYMBOL_GPL(get_device);
  361. EXPORT_SYMBOL_GPL(put_device);
  362. EXPORT_SYMBOL_GPL(device_create_file);
  363. EXPORT_SYMBOL_GPL(device_remove_file);