core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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, 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, 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. /**
  160. * device_initialize - init device structure.
  161. * @dev: device.
  162. *
  163. * This prepares the device for use by other layers,
  164. * including adding it to the device hierarchy.
  165. * It is the first half of device_register(), if called by
  166. * that, though it can also be called separately, so one
  167. * may use @dev's fields (e.g. the refcount).
  168. */
  169. void device_initialize(struct device *dev)
  170. {
  171. kobj_set_kset_s(dev, devices_subsys);
  172. kobject_init(&dev->kobj);
  173. INIT_LIST_HEAD(&dev->node);
  174. INIT_LIST_HEAD(&dev->children);
  175. INIT_LIST_HEAD(&dev->driver_list);
  176. INIT_LIST_HEAD(&dev->bus_list);
  177. INIT_LIST_HEAD(&dev->dma_pools);
  178. }
  179. /**
  180. * device_add - add device to device hierarchy.
  181. * @dev: device.
  182. *
  183. * This is part 2 of device_register(), though may be called
  184. * separately _iff_ device_initialize() has been called separately.
  185. *
  186. * This adds it to the kobject hierarchy via kobject_add(), adds it
  187. * to the global and sibling lists for the device, then
  188. * adds it to the other relevant subsystems of the driver model.
  189. */
  190. int device_add(struct device *dev)
  191. {
  192. struct device *parent = NULL;
  193. int error = -EINVAL;
  194. dev = get_device(dev);
  195. if (!dev || !strlen(dev->bus_id))
  196. goto Error;
  197. parent = get_device(dev->parent);
  198. pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
  199. /* first, register with generic layer. */
  200. kobject_set_name(&dev->kobj, "%s", dev->bus_id);
  201. if (parent)
  202. dev->kobj.parent = &parent->kobj;
  203. if ((error = kobject_add(&dev->kobj)))
  204. goto Error;
  205. kobject_hotplug(&dev->kobj, KOBJ_ADD);
  206. if ((error = device_pm_add(dev)))
  207. goto PMError;
  208. if ((error = bus_add_device(dev)))
  209. goto BusError;
  210. down_write(&devices_subsys.rwsem);
  211. if (parent)
  212. list_add_tail(&dev->node, &parent->children);
  213. up_write(&devices_subsys.rwsem);
  214. /* notify platform of device entry */
  215. if (platform_notify)
  216. platform_notify(dev);
  217. Done:
  218. put_device(dev);
  219. return error;
  220. BusError:
  221. device_pm_remove(dev);
  222. PMError:
  223. kobject_hotplug(&dev->kobj, KOBJ_REMOVE);
  224. kobject_del(&dev->kobj);
  225. Error:
  226. if (parent)
  227. put_device(parent);
  228. goto Done;
  229. }
  230. /**
  231. * device_register - register a device with the system.
  232. * @dev: pointer to the device structure
  233. *
  234. * This happens in two clean steps - initialize the device
  235. * and add it to the system. The two steps can be called
  236. * separately, but this is the easiest and most common.
  237. * I.e. you should only call the two helpers separately if
  238. * have a clearly defined need to use and refcount the device
  239. * before it is added to the hierarchy.
  240. */
  241. int device_register(struct device *dev)
  242. {
  243. device_initialize(dev);
  244. return device_add(dev);
  245. }
  246. /**
  247. * get_device - increment reference count for device.
  248. * @dev: device.
  249. *
  250. * This simply forwards the call to kobject_get(), though
  251. * we do take care to provide for the case that we get a NULL
  252. * pointer passed in.
  253. */
  254. struct device * get_device(struct device * dev)
  255. {
  256. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  257. }
  258. /**
  259. * put_device - decrement reference count.
  260. * @dev: device in question.
  261. */
  262. void put_device(struct device * dev)
  263. {
  264. if (dev)
  265. kobject_put(&dev->kobj);
  266. }
  267. /**
  268. * device_del - delete device from system.
  269. * @dev: device.
  270. *
  271. * This is the first part of the device unregistration
  272. * sequence. This removes the device from the lists we control
  273. * from here, has it removed from the other driver model
  274. * subsystems it was added to in device_add(), and removes it
  275. * from the kobject hierarchy.
  276. *
  277. * NOTE: this should be called manually _iff_ device_add() was
  278. * also called manually.
  279. */
  280. void device_del(struct device * dev)
  281. {
  282. struct device * parent = dev->parent;
  283. down_write(&devices_subsys.rwsem);
  284. if (parent)
  285. list_del_init(&dev->node);
  286. up_write(&devices_subsys.rwsem);
  287. /* Notify the platform of the removal, in case they
  288. * need to do anything...
  289. */
  290. if (platform_notify_remove)
  291. platform_notify_remove(dev);
  292. bus_remove_device(dev);
  293. device_pm_remove(dev);
  294. kobject_hotplug(&dev->kobj, KOBJ_REMOVE);
  295. kobject_del(&dev->kobj);
  296. if (parent)
  297. put_device(parent);
  298. }
  299. /**
  300. * device_unregister - unregister device from system.
  301. * @dev: device going away.
  302. *
  303. * We do this in two parts, like we do device_register(). First,
  304. * we remove it from all the subsystems with device_del(), then
  305. * we decrement the reference count via put_device(). If that
  306. * is the final reference count, the device will be cleaned up
  307. * via device_release() above. Otherwise, the structure will
  308. * stick around until the final reference to the device is dropped.
  309. */
  310. void device_unregister(struct device * dev)
  311. {
  312. pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
  313. device_del(dev);
  314. put_device(dev);
  315. }
  316. /**
  317. * device_for_each_child - device child iterator.
  318. * @dev: parent struct device.
  319. * @data: data for the callback.
  320. * @fn: function to be called for each device.
  321. *
  322. * Iterate over @dev's child devices, and call @fn for each,
  323. * passing it @data.
  324. *
  325. * We check the return of @fn each time. If it returns anything
  326. * other than 0, we break out and return that value.
  327. */
  328. int device_for_each_child(struct device * dev, void * data,
  329. int (*fn)(struct device *, void *))
  330. {
  331. struct device * child;
  332. int error = 0;
  333. down_read(&devices_subsys.rwsem);
  334. list_for_each_entry(child, &dev->children, node) {
  335. if((error = fn(child, data)))
  336. break;
  337. }
  338. up_read(&devices_subsys.rwsem);
  339. return error;
  340. }
  341. /**
  342. * device_find - locate device on a bus by name.
  343. * @name: name of the device.
  344. * @bus: bus to scan for the device.
  345. *
  346. * Call kset_find_obj() to iterate over list of devices on
  347. * a bus to find device by name. Return device if found.
  348. *
  349. * Note that kset_find_obj increments device's reference count.
  350. */
  351. struct device *device_find(const char *name, struct bus_type *bus)
  352. {
  353. struct kobject *k = kset_find_obj(&bus->devices, name);
  354. if (k)
  355. return to_dev(k);
  356. return NULL;
  357. }
  358. int __init devices_init(void)
  359. {
  360. return subsystem_register(&devices_subsys);
  361. }
  362. EXPORT_SYMBOL_GPL(device_for_each_child);
  363. EXPORT_SYMBOL_GPL(device_initialize);
  364. EXPORT_SYMBOL_GPL(device_add);
  365. EXPORT_SYMBOL_GPL(device_register);
  366. EXPORT_SYMBOL_GPL(device_del);
  367. EXPORT_SYMBOL_GPL(device_unregister);
  368. EXPORT_SYMBOL_GPL(get_device);
  369. EXPORT_SYMBOL_GPL(put_device);
  370. EXPORT_SYMBOL_GPL(device_find);
  371. EXPORT_SYMBOL_GPL(device_create_file);
  372. EXPORT_SYMBOL_GPL(device_remove_file);