core.c 34 KB

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