core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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_uevent_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_uevent_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_uevent(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_uevent_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_uevent_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->uevent) {
  115. /* have the bus specific function add its stuff */
  116. retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
  117. if (retval) {
  118. pr_debug ("%s - uevent() returned %d\n",
  119. __FUNCTION__, retval);
  120. }
  121. }
  122. return retval;
  123. }
  124. static struct kset_uevent_ops device_uevent_ops = {
  125. .filter = dev_uevent_filter,
  126. .name = dev_uevent_name,
  127. .uevent = dev_uevent,
  128. };
  129. static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
  130. const char *buf, size_t count)
  131. {
  132. kobject_uevent(&dev->kobj, KOBJ_ADD);
  133. return count;
  134. }
  135. /*
  136. * devices_subsys - structure to be registered with kobject core.
  137. */
  138. decl_subsys(devices, &ktype_device, &device_uevent_ops);
  139. /**
  140. * device_create_file - create sysfs attribute file for device.
  141. * @dev: device.
  142. * @attr: device attribute descriptor.
  143. */
  144. int device_create_file(struct device * dev, struct device_attribute * attr)
  145. {
  146. int error = 0;
  147. if (get_device(dev)) {
  148. error = sysfs_create_file(&dev->kobj, &attr->attr);
  149. put_device(dev);
  150. }
  151. return error;
  152. }
  153. /**
  154. * device_remove_file - remove sysfs attribute file.
  155. * @dev: device.
  156. * @attr: device attribute descriptor.
  157. */
  158. void device_remove_file(struct device * dev, struct device_attribute * attr)
  159. {
  160. if (get_device(dev)) {
  161. sysfs_remove_file(&dev->kobj, &attr->attr);
  162. put_device(dev);
  163. }
  164. }
  165. static void klist_children_get(struct klist_node *n)
  166. {
  167. struct device *dev = container_of(n, struct device, knode_parent);
  168. get_device(dev);
  169. }
  170. static void klist_children_put(struct klist_node *n)
  171. {
  172. struct device *dev = container_of(n, struct device, knode_parent);
  173. put_device(dev);
  174. }
  175. /**
  176. * device_initialize - init device structure.
  177. * @dev: device.
  178. *
  179. * This prepares the device for use by other layers,
  180. * including adding it to the device hierarchy.
  181. * It is the first half of device_register(), if called by
  182. * that, though it can also be called separately, so one
  183. * may use @dev's fields (e.g. the refcount).
  184. */
  185. void device_initialize(struct device *dev)
  186. {
  187. kobj_set_kset_s(dev, devices_subsys);
  188. kobject_init(&dev->kobj);
  189. klist_init(&dev->klist_children, klist_children_get,
  190. klist_children_put);
  191. INIT_LIST_HEAD(&dev->dma_pools);
  192. init_MUTEX(&dev->sem);
  193. device_init_wakeup(dev, 0);
  194. }
  195. /**
  196. * device_add - add device to device hierarchy.
  197. * @dev: device.
  198. *
  199. * This is part 2 of device_register(), though may be called
  200. * separately _iff_ device_initialize() has been called separately.
  201. *
  202. * This adds it to the kobject hierarchy via kobject_add(), adds it
  203. * to the global and sibling lists for the device, then
  204. * adds it to the other relevant subsystems of the driver model.
  205. */
  206. int device_add(struct device *dev)
  207. {
  208. struct device *parent = NULL;
  209. int error = -EINVAL;
  210. dev = get_device(dev);
  211. if (!dev || !strlen(dev->bus_id))
  212. goto Error;
  213. parent = get_device(dev->parent);
  214. pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
  215. /* first, register with generic layer. */
  216. kobject_set_name(&dev->kobj, "%s", dev->bus_id);
  217. if (parent)
  218. dev->kobj.parent = &parent->kobj;
  219. if ((error = kobject_add(&dev->kobj)))
  220. goto Error;
  221. dev->uevent_attr.attr.name = "uevent";
  222. dev->uevent_attr.attr.mode = S_IWUSR;
  223. if (dev->driver)
  224. dev->uevent_attr.attr.owner = dev->driver->owner;
  225. dev->uevent_attr.store = store_uevent;
  226. device_create_file(dev, &dev->uevent_attr);
  227. kobject_uevent(&dev->kobj, KOBJ_ADD);
  228. if ((error = device_pm_add(dev)))
  229. goto PMError;
  230. if ((error = bus_add_device(dev)))
  231. goto BusError;
  232. if (parent)
  233. klist_add_tail(&dev->knode_parent, &parent->klist_children);
  234. /* notify platform of device entry */
  235. if (platform_notify)
  236. platform_notify(dev);
  237. Done:
  238. put_device(dev);
  239. return error;
  240. BusError:
  241. device_pm_remove(dev);
  242. PMError:
  243. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  244. kobject_del(&dev->kobj);
  245. Error:
  246. if (parent)
  247. put_device(parent);
  248. goto Done;
  249. }
  250. /**
  251. * device_register - register a device with the system.
  252. * @dev: pointer to the device structure
  253. *
  254. * This happens in two clean steps - initialize the device
  255. * and add it to the system. The two steps can be called
  256. * separately, but this is the easiest and most common.
  257. * I.e. you should only call the two helpers separately if
  258. * have a clearly defined need to use and refcount the device
  259. * before it is added to the hierarchy.
  260. */
  261. int device_register(struct device *dev)
  262. {
  263. device_initialize(dev);
  264. return device_add(dev);
  265. }
  266. /**
  267. * get_device - increment reference count for device.
  268. * @dev: device.
  269. *
  270. * This simply forwards the call to kobject_get(), though
  271. * we do take care to provide for the case that we get a NULL
  272. * pointer passed in.
  273. */
  274. struct device * get_device(struct device * dev)
  275. {
  276. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  277. }
  278. /**
  279. * put_device - decrement reference count.
  280. * @dev: device in question.
  281. */
  282. void put_device(struct device * dev)
  283. {
  284. if (dev)
  285. kobject_put(&dev->kobj);
  286. }
  287. /**
  288. * device_del - delete device from system.
  289. * @dev: device.
  290. *
  291. * This is the first part of the device unregistration
  292. * sequence. This removes the device from the lists we control
  293. * from here, has it removed from the other driver model
  294. * subsystems it was added to in device_add(), and removes it
  295. * from the kobject hierarchy.
  296. *
  297. * NOTE: this should be called manually _iff_ device_add() was
  298. * also called manually.
  299. */
  300. void device_del(struct device * dev)
  301. {
  302. struct device * parent = dev->parent;
  303. if (parent)
  304. klist_del(&dev->knode_parent);
  305. device_remove_file(dev, &dev->uevent_attr);
  306. /* Notify the platform of the removal, in case they
  307. * need to do anything...
  308. */
  309. if (platform_notify_remove)
  310. platform_notify_remove(dev);
  311. bus_remove_device(dev);
  312. device_pm_remove(dev);
  313. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  314. kobject_del(&dev->kobj);
  315. if (parent)
  316. put_device(parent);
  317. }
  318. /**
  319. * device_unregister - unregister device from system.
  320. * @dev: device going away.
  321. *
  322. * We do this in two parts, like we do device_register(). First,
  323. * we remove it from all the subsystems with device_del(), then
  324. * we decrement the reference count via put_device(). If that
  325. * is the final reference count, the device will be cleaned up
  326. * via device_release() above. Otherwise, the structure will
  327. * stick around until the final reference to the device is dropped.
  328. */
  329. void device_unregister(struct device * dev)
  330. {
  331. pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
  332. device_del(dev);
  333. put_device(dev);
  334. }
  335. static struct device * next_device(struct klist_iter * i)
  336. {
  337. struct klist_node * n = klist_next(i);
  338. return n ? container_of(n, struct device, knode_parent) : NULL;
  339. }
  340. /**
  341. * device_for_each_child - device child iterator.
  342. * @parent: parent struct device.
  343. * @data: data for the callback.
  344. * @fn: function to be called for each device.
  345. *
  346. * Iterate over @parent's child devices, and call @fn for each,
  347. * passing it @data.
  348. *
  349. * We check the return of @fn each time. If it returns anything
  350. * other than 0, we break out and return that value.
  351. */
  352. int device_for_each_child(struct device * parent, void * data,
  353. int (*fn)(struct device *, void *))
  354. {
  355. struct klist_iter i;
  356. struct device * child;
  357. int error = 0;
  358. klist_iter_init(&parent->klist_children, &i);
  359. while ((child = next_device(&i)) && !error)
  360. error = fn(child, data);
  361. klist_iter_exit(&i);
  362. return error;
  363. }
  364. int __init devices_init(void)
  365. {
  366. return subsystem_register(&devices_subsys);
  367. }
  368. EXPORT_SYMBOL_GPL(device_for_each_child);
  369. EXPORT_SYMBOL_GPL(device_initialize);
  370. EXPORT_SYMBOL_GPL(device_add);
  371. EXPORT_SYMBOL_GPL(device_register);
  372. EXPORT_SYMBOL_GPL(device_del);
  373. EXPORT_SYMBOL_GPL(device_unregister);
  374. EXPORT_SYMBOL_GPL(get_device);
  375. EXPORT_SYMBOL_GPL(put_device);
  376. EXPORT_SYMBOL_GPL(device_create_file);
  377. EXPORT_SYMBOL_GPL(device_remove_file);