core.c 10 KB

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