core.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  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. * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
  7. * Copyright (c) 2006 Novell, Inc.
  8. *
  9. * This file is released under the GPLv2
  10. *
  11. */
  12. #include <linux/device.h>
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <linux/kdev_t.h>
  19. #include <linux/notifier.h>
  20. #include <linux/genhd.h>
  21. #include <asm/semaphore.h>
  22. #include "base.h"
  23. #include "power/power.h"
  24. int (*platform_notify)(struct device *dev) = NULL;
  25. int (*platform_notify_remove)(struct device *dev) = NULL;
  26. #ifdef CONFIG_BLOCK
  27. static inline int device_is_not_partition(struct device *dev)
  28. {
  29. return !(dev->type == &part_type);
  30. }
  31. #else
  32. static inline int device_is_not_partition(struct device *dev)
  33. {
  34. return 1;
  35. }
  36. #endif
  37. /**
  38. * dev_driver_string - Return a device's driver name, if at all possible
  39. * @dev: struct device to get the name of
  40. *
  41. * Will return the device's driver's name if it is bound to a device. If
  42. * the device is not bound to a device, it will return the name of the bus
  43. * it is attached to. If it is not attached to a bus either, an empty
  44. * string will be returned.
  45. */
  46. const char *dev_driver_string(struct device *dev)
  47. {
  48. return dev->driver ? dev->driver->name :
  49. (dev->bus ? dev->bus->name :
  50. (dev->class ? dev->class->name : ""));
  51. }
  52. EXPORT_SYMBOL(dev_driver_string);
  53. #define to_dev(obj) container_of(obj, struct device, kobj)
  54. #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
  55. static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
  56. char *buf)
  57. {
  58. struct device_attribute *dev_attr = to_dev_attr(attr);
  59. struct device *dev = to_dev(kobj);
  60. ssize_t ret = -EIO;
  61. if (dev_attr->show)
  62. ret = dev_attr->show(dev, dev_attr, buf);
  63. return ret;
  64. }
  65. static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
  66. const char *buf, size_t count)
  67. {
  68. struct device_attribute *dev_attr = to_dev_attr(attr);
  69. struct device *dev = to_dev(kobj);
  70. ssize_t ret = -EIO;
  71. if (dev_attr->store)
  72. ret = dev_attr->store(dev, dev_attr, buf, count);
  73. return ret;
  74. }
  75. static struct sysfs_ops dev_sysfs_ops = {
  76. .show = dev_attr_show,
  77. .store = dev_attr_store,
  78. };
  79. /**
  80. * device_release - free device structure.
  81. * @kobj: device's kobject.
  82. *
  83. * This is called once the reference count for the object
  84. * reaches 0. We forward the call to the device's release
  85. * method, which should handle actually freeing the structure.
  86. */
  87. static void device_release(struct kobject *kobj)
  88. {
  89. struct device *dev = to_dev(kobj);
  90. if (dev->release)
  91. dev->release(dev);
  92. else if (dev->type && dev->type->release)
  93. dev->type->release(dev);
  94. else if (dev->class && dev->class->dev_release)
  95. dev->class->dev_release(dev);
  96. else {
  97. printk(KERN_ERR "Device '%s' does not have a release() "
  98. "function, it is broken and must be fixed.\n",
  99. dev->bus_id);
  100. WARN_ON(1);
  101. }
  102. }
  103. static struct kobj_type device_ktype = {
  104. .release = device_release,
  105. .sysfs_ops = &dev_sysfs_ops,
  106. };
  107. static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
  108. {
  109. struct kobj_type *ktype = get_ktype(kobj);
  110. if (ktype == &device_ktype) {
  111. struct device *dev = to_dev(kobj);
  112. if (dev->uevent_suppress)
  113. return 0;
  114. if (dev->bus)
  115. return 1;
  116. if (dev->class)
  117. return 1;
  118. }
  119. return 0;
  120. }
  121. static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
  122. {
  123. struct device *dev = to_dev(kobj);
  124. if (dev->bus)
  125. return dev->bus->name;
  126. if (dev->class)
  127. return dev->class->name;
  128. return NULL;
  129. }
  130. static int dev_uevent(struct kset *kset, struct kobject *kobj,
  131. struct kobj_uevent_env *env)
  132. {
  133. struct device *dev = to_dev(kobj);
  134. int retval = 0;
  135. /* add the major/minor if present */
  136. if (MAJOR(dev->devt)) {
  137. add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
  138. add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
  139. }
  140. if (dev->type && dev->type->name)
  141. add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
  142. if (dev->driver)
  143. add_uevent_var(env, "DRIVER=%s", dev->driver->name);
  144. #ifdef CONFIG_SYSFS_DEPRECATED
  145. if (dev->class) {
  146. struct device *parent = dev->parent;
  147. /* find first bus device in parent chain */
  148. while (parent && !parent->bus)
  149. parent = parent->parent;
  150. if (parent && parent->bus) {
  151. const char *path;
  152. path = kobject_get_path(&parent->kobj, GFP_KERNEL);
  153. if (path) {
  154. add_uevent_var(env, "PHYSDEVPATH=%s", path);
  155. kfree(path);
  156. }
  157. add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
  158. if (parent->driver)
  159. add_uevent_var(env, "PHYSDEVDRIVER=%s",
  160. parent->driver->name);
  161. }
  162. } else if (dev->bus) {
  163. add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
  164. if (dev->driver)
  165. add_uevent_var(env, "PHYSDEVDRIVER=%s",
  166. dev->driver->name);
  167. }
  168. #endif
  169. /* have the bus specific function add its stuff */
  170. if (dev->bus && dev->bus->uevent) {
  171. retval = dev->bus->uevent(dev, env);
  172. if (retval)
  173. pr_debug("device: '%s': %s: bus uevent() returned %d\n",
  174. dev->bus_id, __FUNCTION__, retval);
  175. }
  176. /* have the class specific function add its stuff */
  177. if (dev->class && dev->class->dev_uevent) {
  178. retval = dev->class->dev_uevent(dev, env);
  179. if (retval)
  180. pr_debug("device: '%s': %s: class uevent() "
  181. "returned %d\n", dev->bus_id,
  182. __FUNCTION__, retval);
  183. }
  184. /* have the device type specific fuction add its stuff */
  185. if (dev->type && dev->type->uevent) {
  186. retval = dev->type->uevent(dev, env);
  187. if (retval)
  188. pr_debug("device: '%s': %s: dev_type uevent() "
  189. "returned %d\n", dev->bus_id,
  190. __FUNCTION__, retval);
  191. }
  192. return retval;
  193. }
  194. static struct kset_uevent_ops device_uevent_ops = {
  195. .filter = dev_uevent_filter,
  196. .name = dev_uevent_name,
  197. .uevent = dev_uevent,
  198. };
  199. static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
  200. char *buf)
  201. {
  202. struct kobject *top_kobj;
  203. struct kset *kset;
  204. struct kobj_uevent_env *env = NULL;
  205. int i;
  206. size_t count = 0;
  207. int retval;
  208. /* search the kset, the device belongs to */
  209. top_kobj = &dev->kobj;
  210. while (!top_kobj->kset && top_kobj->parent)
  211. top_kobj = top_kobj->parent;
  212. if (!top_kobj->kset)
  213. goto out;
  214. kset = top_kobj->kset;
  215. if (!kset->uevent_ops || !kset->uevent_ops->uevent)
  216. goto out;
  217. /* respect filter */
  218. if (kset->uevent_ops && kset->uevent_ops->filter)
  219. if (!kset->uevent_ops->filter(kset, &dev->kobj))
  220. goto out;
  221. env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  222. if (!env)
  223. return -ENOMEM;
  224. /* let the kset specific function add its keys */
  225. retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
  226. if (retval)
  227. goto out;
  228. /* copy keys to file */
  229. for (i = 0; i < env->envp_idx; i++)
  230. count += sprintf(&buf[count], "%s\n", env->envp[i]);
  231. out:
  232. kfree(env);
  233. return count;
  234. }
  235. static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
  236. const char *buf, size_t count)
  237. {
  238. enum kobject_action action;
  239. if (kobject_action_type(buf, count, &action) == 0) {
  240. kobject_uevent(&dev->kobj, action);
  241. goto out;
  242. }
  243. dev_err(dev, "uevent: unsupported action-string; this will "
  244. "be ignored in a future kernel version\n");
  245. kobject_uevent(&dev->kobj, KOBJ_ADD);
  246. out:
  247. return count;
  248. }
  249. static struct device_attribute uevent_attr =
  250. __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
  251. static int device_add_attributes(struct device *dev,
  252. struct device_attribute *attrs)
  253. {
  254. int error = 0;
  255. int i;
  256. if (attrs) {
  257. for (i = 0; attr_name(attrs[i]); i++) {
  258. error = device_create_file(dev, &attrs[i]);
  259. if (error)
  260. break;
  261. }
  262. if (error)
  263. while (--i >= 0)
  264. device_remove_file(dev, &attrs[i]);
  265. }
  266. return error;
  267. }
  268. static void device_remove_attributes(struct device *dev,
  269. struct device_attribute *attrs)
  270. {
  271. int i;
  272. if (attrs)
  273. for (i = 0; attr_name(attrs[i]); i++)
  274. device_remove_file(dev, &attrs[i]);
  275. }
  276. static int device_add_groups(struct device *dev,
  277. struct attribute_group **groups)
  278. {
  279. int error = 0;
  280. int i;
  281. if (groups) {
  282. for (i = 0; groups[i]; i++) {
  283. error = sysfs_create_group(&dev->kobj, groups[i]);
  284. if (error) {
  285. while (--i >= 0)
  286. sysfs_remove_group(&dev->kobj,
  287. groups[i]);
  288. break;
  289. }
  290. }
  291. }
  292. return error;
  293. }
  294. static void device_remove_groups(struct device *dev,
  295. struct attribute_group **groups)
  296. {
  297. int i;
  298. if (groups)
  299. for (i = 0; groups[i]; i++)
  300. sysfs_remove_group(&dev->kobj, groups[i]);
  301. }
  302. static int device_add_attrs(struct device *dev)
  303. {
  304. struct class *class = dev->class;
  305. struct device_type *type = dev->type;
  306. int error;
  307. if (class) {
  308. error = device_add_attributes(dev, class->dev_attrs);
  309. if (error)
  310. return error;
  311. }
  312. if (type) {
  313. error = device_add_groups(dev, type->groups);
  314. if (error)
  315. goto err_remove_class_attrs;
  316. }
  317. error = device_add_groups(dev, dev->groups);
  318. if (error)
  319. goto err_remove_type_groups;
  320. return 0;
  321. err_remove_type_groups:
  322. if (type)
  323. device_remove_groups(dev, type->groups);
  324. err_remove_class_attrs:
  325. if (class)
  326. device_remove_attributes(dev, class->dev_attrs);
  327. return error;
  328. }
  329. static void device_remove_attrs(struct device *dev)
  330. {
  331. struct class *class = dev->class;
  332. struct device_type *type = dev->type;
  333. device_remove_groups(dev, dev->groups);
  334. if (type)
  335. device_remove_groups(dev, type->groups);
  336. if (class)
  337. device_remove_attributes(dev, class->dev_attrs);
  338. }
  339. static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
  340. char *buf)
  341. {
  342. return print_dev_t(buf, dev->devt);
  343. }
  344. static struct device_attribute devt_attr =
  345. __ATTR(dev, S_IRUGO, show_dev, NULL);
  346. /* kset to create /sys/devices/ */
  347. struct kset *devices_kset;
  348. /**
  349. * device_create_file - create sysfs attribute file for device.
  350. * @dev: device.
  351. * @attr: device attribute descriptor.
  352. */
  353. int device_create_file(struct device *dev, struct device_attribute *attr)
  354. {
  355. int error = 0;
  356. if (dev)
  357. error = sysfs_create_file(&dev->kobj, &attr->attr);
  358. return error;
  359. }
  360. /**
  361. * device_remove_file - remove sysfs attribute file.
  362. * @dev: device.
  363. * @attr: device attribute descriptor.
  364. */
  365. void device_remove_file(struct device *dev, struct device_attribute *attr)
  366. {
  367. if (dev)
  368. sysfs_remove_file(&dev->kobj, &attr->attr);
  369. }
  370. /**
  371. * device_create_bin_file - create sysfs binary attribute file for device.
  372. * @dev: device.
  373. * @attr: device binary attribute descriptor.
  374. */
  375. int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
  376. {
  377. int error = -EINVAL;
  378. if (dev)
  379. error = sysfs_create_bin_file(&dev->kobj, attr);
  380. return error;
  381. }
  382. EXPORT_SYMBOL_GPL(device_create_bin_file);
  383. /**
  384. * device_remove_bin_file - remove sysfs binary attribute file
  385. * @dev: device.
  386. * @attr: device binary attribute descriptor.
  387. */
  388. void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
  389. {
  390. if (dev)
  391. sysfs_remove_bin_file(&dev->kobj, attr);
  392. }
  393. EXPORT_SYMBOL_GPL(device_remove_bin_file);
  394. /**
  395. * device_schedule_callback_owner - helper to schedule a callback for a device
  396. * @dev: device.
  397. * @func: callback function to invoke later.
  398. * @owner: module owning the callback routine
  399. *
  400. * Attribute methods must not unregister themselves or their parent device
  401. * (which would amount to the same thing). Attempts to do so will deadlock,
  402. * since unregistration is mutually exclusive with driver callbacks.
  403. *
  404. * Instead methods can call this routine, which will attempt to allocate
  405. * and schedule a workqueue request to call back @func with @dev as its
  406. * argument in the workqueue's process context. @dev will be pinned until
  407. * @func returns.
  408. *
  409. * This routine is usually called via the inline device_schedule_callback(),
  410. * which automatically sets @owner to THIS_MODULE.
  411. *
  412. * Returns 0 if the request was submitted, -ENOMEM if storage could not
  413. * be allocated, -ENODEV if a reference to @owner isn't available.
  414. *
  415. * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
  416. * underlying sysfs routine (since it is intended for use by attribute
  417. * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
  418. */
  419. int device_schedule_callback_owner(struct device *dev,
  420. void (*func)(struct device *), struct module *owner)
  421. {
  422. return sysfs_schedule_callback(&dev->kobj,
  423. (void (*)(void *)) func, dev, owner);
  424. }
  425. EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
  426. static void klist_children_get(struct klist_node *n)
  427. {
  428. struct device *dev = container_of(n, struct device, knode_parent);
  429. get_device(dev);
  430. }
  431. static void klist_children_put(struct klist_node *n)
  432. {
  433. struct device *dev = container_of(n, struct device, knode_parent);
  434. put_device(dev);
  435. }
  436. /**
  437. * device_initialize - init device structure.
  438. * @dev: device.
  439. *
  440. * This prepares the device for use by other layers,
  441. * including adding it to the device hierarchy.
  442. * It is the first half of device_register(), if called by
  443. * that, though it can also be called separately, so one
  444. * may use @dev's fields (e.g. the refcount).
  445. */
  446. void device_initialize(struct device *dev)
  447. {
  448. dev->kobj.kset = devices_kset;
  449. kobject_init(&dev->kobj, &device_ktype);
  450. klist_init(&dev->klist_children, klist_children_get,
  451. klist_children_put);
  452. INIT_LIST_HEAD(&dev->dma_pools);
  453. INIT_LIST_HEAD(&dev->node);
  454. init_MUTEX(&dev->sem);
  455. spin_lock_init(&dev->devres_lock);
  456. INIT_LIST_HEAD(&dev->devres_head);
  457. device_init_wakeup(dev, 0);
  458. set_dev_node(dev, -1);
  459. }
  460. #ifdef CONFIG_SYSFS_DEPRECATED
  461. static struct kobject *get_device_parent(struct device *dev,
  462. struct device *parent)
  463. {
  464. /* class devices without a parent live in /sys/class/<classname>/ */
  465. if (dev->class && (!parent || parent->class != dev->class))
  466. return &dev->class->subsys.kobj;
  467. /* all other devices keep their parent */
  468. else if (parent)
  469. return &parent->kobj;
  470. return NULL;
  471. }
  472. static inline void cleanup_device_parent(struct device *dev) {}
  473. static inline void cleanup_glue_dir(struct device *dev,
  474. struct kobject *glue_dir) {}
  475. #else
  476. static struct kobject *virtual_device_parent(struct device *dev)
  477. {
  478. static struct kobject *virtual_dir = NULL;
  479. if (!virtual_dir)
  480. virtual_dir = kobject_create_and_add("virtual",
  481. &devices_kset->kobj);
  482. return virtual_dir;
  483. }
  484. static struct kobject *get_device_parent(struct device *dev,
  485. struct device *parent)
  486. {
  487. int retval;
  488. if (dev->class) {
  489. struct kobject *kobj = NULL;
  490. struct kobject *parent_kobj;
  491. struct kobject *k;
  492. /*
  493. * If we have no parent, we live in "virtual".
  494. * Class-devices with a non class-device as parent, live
  495. * in a "glue" directory to prevent namespace collisions.
  496. */
  497. if (parent == NULL)
  498. parent_kobj = virtual_device_parent(dev);
  499. else if (parent->class)
  500. return &parent->kobj;
  501. else
  502. parent_kobj = &parent->kobj;
  503. /* find our class-directory at the parent and reference it */
  504. spin_lock(&dev->class->class_dirs.list_lock);
  505. list_for_each_entry(k, &dev->class->class_dirs.list, entry)
  506. if (k->parent == parent_kobj) {
  507. kobj = kobject_get(k);
  508. break;
  509. }
  510. spin_unlock(&dev->class->class_dirs.list_lock);
  511. if (kobj)
  512. return kobj;
  513. /* or create a new class-directory at the parent device */
  514. k = kobject_create();
  515. if (!k)
  516. return NULL;
  517. k->kset = &dev->class->class_dirs;
  518. retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
  519. if (retval < 0) {
  520. kobject_put(k);
  521. return NULL;
  522. }
  523. /* do not emit an uevent for this simple "glue" directory */
  524. return k;
  525. }
  526. if (parent)
  527. return &parent->kobj;
  528. return NULL;
  529. }
  530. static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
  531. {
  532. /* see if we live in a "glue" directory */
  533. if (!glue_dir || !dev->class ||
  534. glue_dir->kset != &dev->class->class_dirs)
  535. return;
  536. kobject_put(glue_dir);
  537. }
  538. static void cleanup_device_parent(struct device *dev)
  539. {
  540. cleanup_glue_dir(dev, dev->kobj.parent);
  541. }
  542. #endif
  543. static void setup_parent(struct device *dev, struct device *parent)
  544. {
  545. struct kobject *kobj;
  546. kobj = get_device_parent(dev, parent);
  547. if (kobj)
  548. dev->kobj.parent = kobj;
  549. }
  550. static int device_add_class_symlinks(struct device *dev)
  551. {
  552. int error;
  553. if (!dev->class)
  554. return 0;
  555. error = sysfs_create_link(&dev->kobj, &dev->class->subsys.kobj,
  556. "subsystem");
  557. if (error)
  558. goto out;
  559. #ifdef CONFIG_SYSFS_DEPRECATED
  560. /* stacked class devices need a symlink in the class directory */
  561. if (dev->kobj.parent != &dev->class->subsys.kobj &&
  562. device_is_not_partition(dev)) {
  563. error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
  564. dev->bus_id);
  565. if (error)
  566. goto out_subsys;
  567. }
  568. if (dev->parent && device_is_not_partition(dev)) {
  569. struct device *parent = dev->parent;
  570. char *class_name;
  571. /*
  572. * stacked class devices have the 'device' link
  573. * pointing to the bus device instead of the parent
  574. */
  575. while (parent->class && !parent->bus && parent->parent)
  576. parent = parent->parent;
  577. error = sysfs_create_link(&dev->kobj,
  578. &parent->kobj,
  579. "device");
  580. if (error)
  581. goto out_busid;
  582. class_name = make_class_name(dev->class->name,
  583. &dev->kobj);
  584. if (class_name)
  585. error = sysfs_create_link(&dev->parent->kobj,
  586. &dev->kobj, class_name);
  587. kfree(class_name);
  588. if (error)
  589. goto out_device;
  590. }
  591. return 0;
  592. out_device:
  593. if (dev->parent && device_is_not_partition(dev))
  594. sysfs_remove_link(&dev->kobj, "device");
  595. out_busid:
  596. if (dev->kobj.parent != &dev->class->subsys.kobj &&
  597. device_is_not_partition(dev))
  598. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  599. #else
  600. /* link in the class directory pointing to the device */
  601. error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
  602. dev->bus_id);
  603. if (error)
  604. goto out_subsys;
  605. if (dev->parent && device_is_not_partition(dev)) {
  606. error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  607. "device");
  608. if (error)
  609. goto out_busid;
  610. }
  611. return 0;
  612. out_busid:
  613. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  614. #endif
  615. out_subsys:
  616. sysfs_remove_link(&dev->kobj, "subsystem");
  617. out:
  618. return error;
  619. }
  620. static void device_remove_class_symlinks(struct device *dev)
  621. {
  622. if (!dev->class)
  623. return;
  624. #ifdef CONFIG_SYSFS_DEPRECATED
  625. if (dev->parent && device_is_not_partition(dev)) {
  626. char *class_name;
  627. class_name = make_class_name(dev->class->name, &dev->kobj);
  628. if (class_name) {
  629. sysfs_remove_link(&dev->parent->kobj, class_name);
  630. kfree(class_name);
  631. }
  632. sysfs_remove_link(&dev->kobj, "device");
  633. }
  634. if (dev->kobj.parent != &dev->class->subsys.kobj &&
  635. device_is_not_partition(dev))
  636. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  637. #else
  638. if (dev->parent && device_is_not_partition(dev))
  639. sysfs_remove_link(&dev->kobj, "device");
  640. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  641. #endif
  642. sysfs_remove_link(&dev->kobj, "subsystem");
  643. }
  644. /**
  645. * device_add - add device to device hierarchy.
  646. * @dev: device.
  647. *
  648. * This is part 2 of device_register(), though may be called
  649. * separately _iff_ device_initialize() has been called separately.
  650. *
  651. * This adds it to the kobject hierarchy via kobject_add(), adds it
  652. * to the global and sibling lists for the device, then
  653. * adds it to the other relevant subsystems of the driver model.
  654. */
  655. int device_add(struct device *dev)
  656. {
  657. struct device *parent = NULL;
  658. struct class_interface *class_intf;
  659. int error;
  660. dev = get_device(dev);
  661. if (!dev || !strlen(dev->bus_id)) {
  662. error = -EINVAL;
  663. goto Done;
  664. }
  665. pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
  666. parent = get_device(dev->parent);
  667. setup_parent(dev, parent);
  668. /* first, register with generic layer. */
  669. error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev->bus_id);
  670. if (error)
  671. goto Error;
  672. /* notify platform of device entry */
  673. if (platform_notify)
  674. platform_notify(dev);
  675. /* notify clients of device entry (new way) */
  676. if (dev->bus)
  677. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  678. BUS_NOTIFY_ADD_DEVICE, dev);
  679. error = device_create_file(dev, &uevent_attr);
  680. if (error)
  681. goto attrError;
  682. if (MAJOR(dev->devt)) {
  683. error = device_create_file(dev, &devt_attr);
  684. if (error)
  685. goto ueventattrError;
  686. }
  687. error = device_add_class_symlinks(dev);
  688. if (error)
  689. goto SymlinkError;
  690. error = device_add_attrs(dev);
  691. if (error)
  692. goto AttrsError;
  693. error = dpm_sysfs_add(dev);
  694. if (error)
  695. goto PMError;
  696. device_pm_add(dev);
  697. error = bus_add_device(dev);
  698. if (error)
  699. goto BusError;
  700. kobject_uevent(&dev->kobj, KOBJ_ADD);
  701. bus_attach_device(dev);
  702. if (parent)
  703. klist_add_tail(&dev->knode_parent, &parent->klist_children);
  704. if (dev->class) {
  705. down(&dev->class->sem);
  706. /* tie the class to the device */
  707. list_add_tail(&dev->node, &dev->class->devices);
  708. /* notify any interfaces that the device is here */
  709. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  710. if (class_intf->add_dev)
  711. class_intf->add_dev(dev, class_intf);
  712. up(&dev->class->sem);
  713. }
  714. Done:
  715. put_device(dev);
  716. return error;
  717. BusError:
  718. device_pm_remove(dev);
  719. PMError:
  720. if (dev->bus)
  721. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  722. BUS_NOTIFY_DEL_DEVICE, dev);
  723. device_remove_attrs(dev);
  724. AttrsError:
  725. device_remove_class_symlinks(dev);
  726. SymlinkError:
  727. if (MAJOR(dev->devt))
  728. device_remove_file(dev, &devt_attr);
  729. ueventattrError:
  730. device_remove_file(dev, &uevent_attr);
  731. attrError:
  732. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  733. kobject_del(&dev->kobj);
  734. Error:
  735. cleanup_device_parent(dev);
  736. if (parent)
  737. put_device(parent);
  738. goto Done;
  739. }
  740. /**
  741. * device_register - register a device with the system.
  742. * @dev: pointer to the device structure
  743. *
  744. * This happens in two clean steps - initialize the device
  745. * and add it to the system. The two steps can be called
  746. * separately, but this is the easiest and most common.
  747. * I.e. you should only call the two helpers separately if
  748. * have a clearly defined need to use and refcount the device
  749. * before it is added to the hierarchy.
  750. */
  751. int device_register(struct device *dev)
  752. {
  753. device_initialize(dev);
  754. return device_add(dev);
  755. }
  756. /**
  757. * get_device - increment reference count for device.
  758. * @dev: device.
  759. *
  760. * This simply forwards the call to kobject_get(), though
  761. * we do take care to provide for the case that we get a NULL
  762. * pointer passed in.
  763. */
  764. struct device *get_device(struct device *dev)
  765. {
  766. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  767. }
  768. /**
  769. * put_device - decrement reference count.
  770. * @dev: device in question.
  771. */
  772. void put_device(struct device *dev)
  773. {
  774. /* might_sleep(); */
  775. if (dev)
  776. kobject_put(&dev->kobj);
  777. }
  778. /**
  779. * device_del - delete device from system.
  780. * @dev: device.
  781. *
  782. * This is the first part of the device unregistration
  783. * sequence. This removes the device from the lists we control
  784. * from here, has it removed from the other driver model
  785. * subsystems it was added to in device_add(), and removes it
  786. * from the kobject hierarchy.
  787. *
  788. * NOTE: this should be called manually _iff_ device_add() was
  789. * also called manually.
  790. */
  791. void device_del(struct device *dev)
  792. {
  793. struct device *parent = dev->parent;
  794. struct class_interface *class_intf;
  795. device_pm_remove(dev);
  796. if (parent)
  797. klist_del(&dev->knode_parent);
  798. if (MAJOR(dev->devt))
  799. device_remove_file(dev, &devt_attr);
  800. if (dev->class) {
  801. device_remove_class_symlinks(dev);
  802. down(&dev->class->sem);
  803. /* notify any interfaces that the device is now gone */
  804. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  805. if (class_intf->remove_dev)
  806. class_intf->remove_dev(dev, class_intf);
  807. /* remove the device from the class list */
  808. list_del_init(&dev->node);
  809. up(&dev->class->sem);
  810. }
  811. device_remove_file(dev, &uevent_attr);
  812. device_remove_attrs(dev);
  813. bus_remove_device(dev);
  814. /*
  815. * Some platform devices are driven without driver attached
  816. * and managed resources may have been acquired. Make sure
  817. * all resources are released.
  818. */
  819. devres_release_all(dev);
  820. /* Notify the platform of the removal, in case they
  821. * need to do anything...
  822. */
  823. if (platform_notify_remove)
  824. platform_notify_remove(dev);
  825. if (dev->bus)
  826. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  827. BUS_NOTIFY_DEL_DEVICE, dev);
  828. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  829. cleanup_device_parent(dev);
  830. kobject_del(&dev->kobj);
  831. put_device(parent);
  832. }
  833. /**
  834. * device_unregister - unregister device from system.
  835. * @dev: device going away.
  836. *
  837. * We do this in two parts, like we do device_register(). First,
  838. * we remove it from all the subsystems with device_del(), then
  839. * we decrement the reference count via put_device(). If that
  840. * is the final reference count, the device will be cleaned up
  841. * via device_release() above. Otherwise, the structure will
  842. * stick around until the final reference to the device is dropped.
  843. */
  844. void device_unregister(struct device *dev)
  845. {
  846. pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
  847. device_del(dev);
  848. put_device(dev);
  849. }
  850. static struct device *next_device(struct klist_iter *i)
  851. {
  852. struct klist_node *n = klist_next(i);
  853. return n ? container_of(n, struct device, knode_parent) : NULL;
  854. }
  855. /**
  856. * device_for_each_child - device child iterator.
  857. * @parent: parent struct device.
  858. * @data: data for the callback.
  859. * @fn: function to be called for each device.
  860. *
  861. * Iterate over @parent's child devices, and call @fn for each,
  862. * passing it @data.
  863. *
  864. * We check the return of @fn each time. If it returns anything
  865. * other than 0, we break out and return that value.
  866. */
  867. int device_for_each_child(struct device *parent, void *data,
  868. int (*fn)(struct device *dev, void *data))
  869. {
  870. struct klist_iter i;
  871. struct device *child;
  872. int error = 0;
  873. klist_iter_init(&parent->klist_children, &i);
  874. while ((child = next_device(&i)) && !error)
  875. error = fn(child, data);
  876. klist_iter_exit(&i);
  877. return error;
  878. }
  879. /**
  880. * device_find_child - device iterator for locating a particular device.
  881. * @parent: parent struct device
  882. * @data: Data to pass to match function
  883. * @match: Callback function to check device
  884. *
  885. * This is similar to the device_for_each_child() function above, but it
  886. * returns a reference to a device that is 'found' for later use, as
  887. * determined by the @match callback.
  888. *
  889. * The callback should return 0 if the device doesn't match and non-zero
  890. * if it does. If the callback returns non-zero and a reference to the
  891. * current device can be obtained, this function will return to the caller
  892. * and not iterate over any more devices.
  893. */
  894. struct device *device_find_child(struct device *parent, void *data,
  895. int (*match)(struct device *dev, void *data))
  896. {
  897. struct klist_iter i;
  898. struct device *child;
  899. if (!parent)
  900. return NULL;
  901. klist_iter_init(&parent->klist_children, &i);
  902. while ((child = next_device(&i)))
  903. if (match(child, data) && get_device(child))
  904. break;
  905. klist_iter_exit(&i);
  906. return child;
  907. }
  908. int __init devices_init(void)
  909. {
  910. devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
  911. if (!devices_kset)
  912. return -ENOMEM;
  913. return 0;
  914. }
  915. EXPORT_SYMBOL_GPL(device_for_each_child);
  916. EXPORT_SYMBOL_GPL(device_find_child);
  917. EXPORT_SYMBOL_GPL(device_initialize);
  918. EXPORT_SYMBOL_GPL(device_add);
  919. EXPORT_SYMBOL_GPL(device_register);
  920. EXPORT_SYMBOL_GPL(device_del);
  921. EXPORT_SYMBOL_GPL(device_unregister);
  922. EXPORT_SYMBOL_GPL(get_device);
  923. EXPORT_SYMBOL_GPL(put_device);
  924. EXPORT_SYMBOL_GPL(device_create_file);
  925. EXPORT_SYMBOL_GPL(device_remove_file);
  926. static void device_create_release(struct device *dev)
  927. {
  928. pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
  929. kfree(dev);
  930. }
  931. /**
  932. * device_create - creates a device and registers it with sysfs
  933. * @class: pointer to the struct class that this device should be registered to
  934. * @parent: pointer to the parent struct device of this new device, if any
  935. * @devt: the dev_t for the char device to be added
  936. * @fmt: string for the device's name
  937. *
  938. * This function can be used by char device classes. A struct device
  939. * will be created in sysfs, registered to the specified class.
  940. *
  941. * A "dev" file will be created, showing the dev_t for the device, if
  942. * the dev_t is not 0,0.
  943. * If a pointer to a parent struct device is passed in, the newly created
  944. * struct device will be a child of that device in sysfs.
  945. * The pointer to the struct device will be returned from the call.
  946. * Any further sysfs files that might be required can be created using this
  947. * pointer.
  948. *
  949. * Note: the struct class passed to this function must have previously
  950. * been created with a call to class_create().
  951. */
  952. struct device *device_create(struct class *class, struct device *parent,
  953. dev_t devt, const char *fmt, ...)
  954. {
  955. va_list args;
  956. struct device *dev = NULL;
  957. int retval = -ENODEV;
  958. if (class == NULL || IS_ERR(class))
  959. goto error;
  960. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  961. if (!dev) {
  962. retval = -ENOMEM;
  963. goto error;
  964. }
  965. dev->devt = devt;
  966. dev->class = class;
  967. dev->parent = parent;
  968. dev->release = device_create_release;
  969. va_start(args, fmt);
  970. vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
  971. va_end(args);
  972. retval = device_register(dev);
  973. if (retval)
  974. goto error;
  975. return dev;
  976. error:
  977. kfree(dev);
  978. return ERR_PTR(retval);
  979. }
  980. EXPORT_SYMBOL_GPL(device_create);
  981. static int __match_devt(struct device *dev, void *data)
  982. {
  983. dev_t *devt = data;
  984. return dev->devt == *devt;
  985. }
  986. /**
  987. * device_destroy - removes a device that was created with device_create()
  988. * @class: pointer to the struct class that this device was registered with
  989. * @devt: the dev_t of the device that was previously registered
  990. *
  991. * This call unregisters and cleans up a device that was created with a
  992. * call to device_create().
  993. */
  994. void device_destroy(struct class *class, dev_t devt)
  995. {
  996. struct device *dev;
  997. dev = class_find_device(class, &devt, __match_devt);
  998. if (dev) {
  999. put_device(dev);
  1000. device_unregister(dev);
  1001. }
  1002. }
  1003. EXPORT_SYMBOL_GPL(device_destroy);
  1004. #ifdef CONFIG_PM_SLEEP
  1005. /**
  1006. * destroy_suspended_device - asks the PM core to remove a suspended device
  1007. * @class: pointer to the struct class that this device was registered with
  1008. * @devt: the dev_t of the device that was previously registered
  1009. *
  1010. * This call notifies the PM core of the necessity to unregister a suspended
  1011. * device created with a call to device_create() (devices cannot be
  1012. * unregistered directly while suspended, since the PM core holds their
  1013. * semaphores at that time).
  1014. *
  1015. * It can only be called within the scope of a system sleep transition. In
  1016. * practice this means it has to be directly or indirectly invoked either by
  1017. * a suspend or resume method, or by the PM core (e.g. via
  1018. * disable_nonboot_cpus() or enable_nonboot_cpus()).
  1019. */
  1020. void destroy_suspended_device(struct class *class, dev_t devt)
  1021. {
  1022. struct device *dev;
  1023. dev = class_find_device(class, &devt, __match_devt);
  1024. if (dev) {
  1025. device_pm_schedule_removal(dev);
  1026. put_device(dev);
  1027. }
  1028. }
  1029. EXPORT_SYMBOL_GPL(destroy_suspended_device);
  1030. #endif /* CONFIG_PM_SLEEP */
  1031. /**
  1032. * device_rename - renames a device
  1033. * @dev: the pointer to the struct device to be renamed
  1034. * @new_name: the new name of the device
  1035. */
  1036. int device_rename(struct device *dev, char *new_name)
  1037. {
  1038. char *old_class_name = NULL;
  1039. char *new_class_name = NULL;
  1040. char *old_device_name = NULL;
  1041. int error;
  1042. dev = get_device(dev);
  1043. if (!dev)
  1044. return -EINVAL;
  1045. pr_debug("device: '%s': %s: renaming to '%s'\n", dev->bus_id,
  1046. __FUNCTION__, new_name);
  1047. #ifdef CONFIG_SYSFS_DEPRECATED
  1048. if ((dev->class) && (dev->parent))
  1049. old_class_name = make_class_name(dev->class->name, &dev->kobj);
  1050. #endif
  1051. old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  1052. if (!old_device_name) {
  1053. error = -ENOMEM;
  1054. goto out;
  1055. }
  1056. strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE);
  1057. strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
  1058. error = kobject_rename(&dev->kobj, new_name);
  1059. if (error) {
  1060. strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
  1061. goto out;
  1062. }
  1063. #ifdef CONFIG_SYSFS_DEPRECATED
  1064. if (old_class_name) {
  1065. new_class_name = make_class_name(dev->class->name, &dev->kobj);
  1066. if (new_class_name) {
  1067. error = sysfs_create_link(&dev->parent->kobj,
  1068. &dev->kobj, new_class_name);
  1069. if (error)
  1070. goto out;
  1071. sysfs_remove_link(&dev->parent->kobj, old_class_name);
  1072. }
  1073. }
  1074. #else
  1075. if (dev->class) {
  1076. sysfs_remove_link(&dev->class->subsys.kobj, old_device_name);
  1077. error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
  1078. dev->bus_id);
  1079. if (error) {
  1080. dev_err(dev, "%s: sysfs_create_symlink failed (%d)\n",
  1081. __FUNCTION__, error);
  1082. }
  1083. }
  1084. #endif
  1085. out:
  1086. put_device(dev);
  1087. kfree(new_class_name);
  1088. kfree(old_class_name);
  1089. kfree(old_device_name);
  1090. return error;
  1091. }
  1092. EXPORT_SYMBOL_GPL(device_rename);
  1093. static int device_move_class_links(struct device *dev,
  1094. struct device *old_parent,
  1095. struct device *new_parent)
  1096. {
  1097. int error = 0;
  1098. #ifdef CONFIG_SYSFS_DEPRECATED
  1099. char *class_name;
  1100. class_name = make_class_name(dev->class->name, &dev->kobj);
  1101. if (!class_name) {
  1102. error = -ENOMEM;
  1103. goto out;
  1104. }
  1105. if (old_parent) {
  1106. sysfs_remove_link(&dev->kobj, "device");
  1107. sysfs_remove_link(&old_parent->kobj, class_name);
  1108. }
  1109. if (new_parent) {
  1110. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1111. "device");
  1112. if (error)
  1113. goto out;
  1114. error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
  1115. class_name);
  1116. if (error)
  1117. sysfs_remove_link(&dev->kobj, "device");
  1118. } else
  1119. error = 0;
  1120. out:
  1121. kfree(class_name);
  1122. return error;
  1123. #else
  1124. if (old_parent)
  1125. sysfs_remove_link(&dev->kobj, "device");
  1126. if (new_parent)
  1127. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1128. "device");
  1129. return error;
  1130. #endif
  1131. }
  1132. /**
  1133. * device_move - moves a device to a new parent
  1134. * @dev: the pointer to the struct device to be moved
  1135. * @new_parent: the new parent of the device (can by NULL)
  1136. */
  1137. int device_move(struct device *dev, struct device *new_parent)
  1138. {
  1139. int error;
  1140. struct device *old_parent;
  1141. struct kobject *new_parent_kobj;
  1142. dev = get_device(dev);
  1143. if (!dev)
  1144. return -EINVAL;
  1145. new_parent = get_device(new_parent);
  1146. new_parent_kobj = get_device_parent(dev, new_parent);
  1147. pr_debug("device: '%s': %s: moving to '%s'\n", dev->bus_id,
  1148. __FUNCTION__, new_parent ? new_parent->bus_id : "<NULL>");
  1149. error = kobject_move(&dev->kobj, new_parent_kobj);
  1150. if (error) {
  1151. cleanup_glue_dir(dev, new_parent_kobj);
  1152. put_device(new_parent);
  1153. goto out;
  1154. }
  1155. old_parent = dev->parent;
  1156. dev->parent = new_parent;
  1157. if (old_parent)
  1158. klist_remove(&dev->knode_parent);
  1159. if (new_parent)
  1160. klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
  1161. if (!dev->class)
  1162. goto out_put;
  1163. error = device_move_class_links(dev, old_parent, new_parent);
  1164. if (error) {
  1165. /* We ignore errors on cleanup since we're hosed anyway... */
  1166. device_move_class_links(dev, new_parent, old_parent);
  1167. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  1168. if (new_parent)
  1169. klist_remove(&dev->knode_parent);
  1170. if (old_parent)
  1171. klist_add_tail(&dev->knode_parent,
  1172. &old_parent->klist_children);
  1173. }
  1174. cleanup_glue_dir(dev, new_parent_kobj);
  1175. put_device(new_parent);
  1176. goto out;
  1177. }
  1178. out_put:
  1179. put_device(old_parent);
  1180. out:
  1181. put_device(dev);
  1182. return error;
  1183. }
  1184. EXPORT_SYMBOL_GPL(device_move);
  1185. /**
  1186. * device_shutdown - call ->shutdown() on each device to shutdown.
  1187. */
  1188. void device_shutdown(void)
  1189. {
  1190. struct device *dev, *devn;
  1191. list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
  1192. kobj.entry) {
  1193. if (dev->bus && dev->bus->shutdown) {
  1194. dev_dbg(dev, "shutdown\n");
  1195. dev->bus->shutdown(dev);
  1196. } else if (dev->driver && dev->driver->shutdown) {
  1197. dev_dbg(dev, "shutdown\n");
  1198. dev->driver->shutdown(dev);
  1199. }
  1200. }
  1201. }