bus.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * bus.c - bus driver management
  4. *
  5. * Copyright (c) 2002-3 Patrick Mochel
  6. * Copyright (c) 2002-3 Open Source Development Labs
  7. * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
  8. * Copyright (c) 2007 Novell Inc.
  9. * Copyright (c) 2023 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  10. */
  11. #include <linux/async.h>
  12. #include <linux/device/bus.h>
  13. #include <linux/device.h>
  14. #include <linux/module.h>
  15. #include <linux/errno.h>
  16. #include <linux/slab.h>
  17. #include <linux/init.h>
  18. #include <linux/string.h>
  19. #include <linux/mutex.h>
  20. #include <linux/sysfs.h>
  21. #include "base.h"
  22. #include "power/power.h"
  23. /* /sys/devices/system */
  24. static struct kset *system_kset;
  25. /* /sys/bus */
  26. static struct kset *bus_kset;
  27. #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
  28. /*
  29. * sysfs bindings for drivers
  30. */
  31. #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
  32. #define DRIVER_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
  33. struct driver_attribute driver_attr_##_name = \
  34. __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
  35. static int __must_check bus_rescan_devices_helper(struct device *dev,
  36. void *data);
  37. /**
  38. * bus_to_subsys - Turn a struct bus_type into a struct subsys_private
  39. *
  40. * @bus: pointer to the struct bus_type to look up
  41. *
  42. * The driver core internals needs to work on the subsys_private structure, not
  43. * the external struct bus_type pointer. This function walks the list of
  44. * registered busses in the system and finds the matching one and returns the
  45. * internal struct subsys_private that relates to that bus.
  46. *
  47. * Note, the reference count of the return value is INCREMENTED if it is not
  48. * NULL. A call to subsys_put() must be done when finished with the pointer in
  49. * order for it to be properly freed.
  50. */
  51. struct subsys_private *bus_to_subsys(const struct bus_type *bus)
  52. {
  53. struct subsys_private *sp = NULL;
  54. struct kobject *kobj;
  55. if (!bus || !bus_kset)
  56. return NULL;
  57. spin_lock(&bus_kset->list_lock);
  58. if (list_empty(&bus_kset->list))
  59. goto done;
  60. list_for_each_entry(kobj, &bus_kset->list, entry) {
  61. struct kset *kset = container_of(kobj, struct kset, kobj);
  62. sp = container_of_const(kset, struct subsys_private, subsys);
  63. if (sp->bus == bus)
  64. goto done;
  65. }
  66. sp = NULL;
  67. done:
  68. sp = subsys_get(sp);
  69. spin_unlock(&bus_kset->list_lock);
  70. return sp;
  71. }
  72. static const struct bus_type *bus_get(const struct bus_type *bus)
  73. {
  74. struct subsys_private *sp = bus_to_subsys(bus);
  75. if (sp)
  76. return bus;
  77. return NULL;
  78. }
  79. static void bus_put(const struct bus_type *bus)
  80. {
  81. struct subsys_private *sp = bus_to_subsys(bus);
  82. /* two puts are required as the call to bus_to_subsys incremented it again */
  83. subsys_put(sp);
  84. subsys_put(sp);
  85. }
  86. static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
  87. char *buf)
  88. {
  89. struct driver_attribute *drv_attr = to_drv_attr(attr);
  90. struct driver_private *drv_priv = to_driver(kobj);
  91. ssize_t ret = -EIO;
  92. if (drv_attr->show)
  93. ret = drv_attr->show(drv_priv->driver, buf);
  94. return ret;
  95. }
  96. static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
  97. const char *buf, size_t count)
  98. {
  99. struct driver_attribute *drv_attr = to_drv_attr(attr);
  100. struct driver_private *drv_priv = to_driver(kobj);
  101. ssize_t ret = -EIO;
  102. if (drv_attr->store)
  103. ret = drv_attr->store(drv_priv->driver, buf, count);
  104. return ret;
  105. }
  106. static const struct sysfs_ops driver_sysfs_ops = {
  107. .show = drv_attr_show,
  108. .store = drv_attr_store,
  109. };
  110. static void driver_release(struct kobject *kobj)
  111. {
  112. struct driver_private *drv_priv = to_driver(kobj);
  113. pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
  114. kfree(drv_priv);
  115. }
  116. static const struct kobj_type driver_ktype = {
  117. .sysfs_ops = &driver_sysfs_ops,
  118. .release = driver_release,
  119. };
  120. /*
  121. * sysfs bindings for buses
  122. */
  123. static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
  124. char *buf)
  125. {
  126. struct bus_attribute *bus_attr = to_bus_attr(attr);
  127. struct subsys_private *subsys_priv = to_subsys_private(kobj);
  128. /* return -EIO for reading a bus attribute without show() */
  129. ssize_t ret = -EIO;
  130. if (bus_attr->show)
  131. ret = bus_attr->show(subsys_priv->bus, buf);
  132. return ret;
  133. }
  134. static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
  135. const char *buf, size_t count)
  136. {
  137. struct bus_attribute *bus_attr = to_bus_attr(attr);
  138. struct subsys_private *subsys_priv = to_subsys_private(kobj);
  139. /* return -EIO for writing a bus attribute without store() */
  140. ssize_t ret = -EIO;
  141. if (bus_attr->store)
  142. ret = bus_attr->store(subsys_priv->bus, buf, count);
  143. return ret;
  144. }
  145. static const struct sysfs_ops bus_sysfs_ops = {
  146. .show = bus_attr_show,
  147. .store = bus_attr_store,
  148. };
  149. int bus_create_file(const struct bus_type *bus, struct bus_attribute *attr)
  150. {
  151. struct subsys_private *sp = bus_to_subsys(bus);
  152. int error;
  153. if (!sp)
  154. return -EINVAL;
  155. error = sysfs_create_file(&sp->subsys.kobj, &attr->attr);
  156. subsys_put(sp);
  157. return error;
  158. }
  159. EXPORT_SYMBOL_GPL(bus_create_file);
  160. void bus_remove_file(const struct bus_type *bus, struct bus_attribute *attr)
  161. {
  162. struct subsys_private *sp = bus_to_subsys(bus);
  163. if (!sp)
  164. return;
  165. sysfs_remove_file(&sp->subsys.kobj, &attr->attr);
  166. subsys_put(sp);
  167. }
  168. EXPORT_SYMBOL_GPL(bus_remove_file);
  169. static void bus_release(struct kobject *kobj)
  170. {
  171. struct subsys_private *priv = to_subsys_private(kobj);
  172. lockdep_unregister_key(&priv->lock_key);
  173. kfree(priv);
  174. }
  175. static const struct kobj_type bus_ktype = {
  176. .sysfs_ops = &bus_sysfs_ops,
  177. .release = bus_release,
  178. };
  179. static int bus_uevent_filter(const struct kobject *kobj)
  180. {
  181. const struct kobj_type *ktype = get_ktype(kobj);
  182. if (ktype == &bus_ktype)
  183. return 1;
  184. return 0;
  185. }
  186. static const struct kset_uevent_ops bus_uevent_ops = {
  187. .filter = bus_uevent_filter,
  188. };
  189. /* Manually detach a device from its associated driver. */
  190. static ssize_t unbind_store(struct device_driver *drv, const char *buf,
  191. size_t count)
  192. {
  193. const struct bus_type *bus = bus_get(drv->bus);
  194. struct device *dev;
  195. int err = -ENODEV;
  196. dev = bus_find_device_by_name(bus, NULL, buf);
  197. if (dev && dev->driver == drv) {
  198. device_driver_detach(dev);
  199. err = count;
  200. }
  201. put_device(dev);
  202. bus_put(bus);
  203. return err;
  204. }
  205. static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, 0200, NULL, unbind_store);
  206. /*
  207. * Manually attach a device to a driver.
  208. * Note: the driver must want to bind to the device,
  209. * it is not possible to override the driver's id table.
  210. */
  211. static ssize_t bind_store(struct device_driver *drv, const char *buf,
  212. size_t count)
  213. {
  214. const struct bus_type *bus = bus_get(drv->bus);
  215. struct device *dev;
  216. int err = -ENODEV;
  217. dev = bus_find_device_by_name(bus, NULL, buf);
  218. if (dev && driver_match_device(drv, dev)) {
  219. err = device_driver_attach(drv, dev);
  220. if (!err) {
  221. /* success */
  222. err = count;
  223. }
  224. }
  225. put_device(dev);
  226. bus_put(bus);
  227. return err;
  228. }
  229. static DRIVER_ATTR_IGNORE_LOCKDEP(bind, 0200, NULL, bind_store);
  230. static ssize_t drivers_autoprobe_show(const struct bus_type *bus, char *buf)
  231. {
  232. struct subsys_private *sp = bus_to_subsys(bus);
  233. int ret;
  234. if (!sp)
  235. return -EINVAL;
  236. ret = sysfs_emit(buf, "%d\n", sp->drivers_autoprobe);
  237. subsys_put(sp);
  238. return ret;
  239. }
  240. static ssize_t drivers_autoprobe_store(const struct bus_type *bus,
  241. const char *buf, size_t count)
  242. {
  243. struct subsys_private *sp = bus_to_subsys(bus);
  244. if (!sp)
  245. return -EINVAL;
  246. if (buf[0] == '0')
  247. sp->drivers_autoprobe = 0;
  248. else
  249. sp->drivers_autoprobe = 1;
  250. subsys_put(sp);
  251. return count;
  252. }
  253. static ssize_t drivers_probe_store(const struct bus_type *bus,
  254. const char *buf, size_t count)
  255. {
  256. struct device *dev;
  257. int err = -EINVAL;
  258. dev = bus_find_device_by_name(bus, NULL, buf);
  259. if (!dev)
  260. return -ENODEV;
  261. if (bus_rescan_devices_helper(dev, NULL) == 0)
  262. err = count;
  263. put_device(dev);
  264. return err;
  265. }
  266. static struct device *next_device(struct klist_iter *i)
  267. {
  268. struct klist_node *n = klist_next(i);
  269. struct device *dev = NULL;
  270. struct device_private *dev_prv;
  271. if (n) {
  272. dev_prv = to_device_private_bus(n);
  273. dev = dev_prv->device;
  274. }
  275. return dev;
  276. }
  277. /**
  278. * bus_for_each_dev - device iterator.
  279. * @bus: bus type.
  280. * @start: device to start iterating from.
  281. * @data: data for the callback.
  282. * @fn: function to be called for each device.
  283. *
  284. * Iterate over @bus's list of devices, and call @fn for each,
  285. * passing it @data. If @start is not NULL, we use that device to
  286. * begin iterating from.
  287. *
  288. * We check the return of @fn each time. If it returns anything
  289. * other than 0, we break out and return that value.
  290. *
  291. * NOTE: The device that returns a non-zero value is not retained
  292. * in any way, nor is its refcount incremented. If the caller needs
  293. * to retain this data, it should do so, and increment the reference
  294. * count in the supplied callback.
  295. */
  296. int bus_for_each_dev(const struct bus_type *bus, struct device *start,
  297. void *data, int (*fn)(struct device *, void *))
  298. {
  299. struct subsys_private *sp = bus_to_subsys(bus);
  300. struct klist_iter i;
  301. struct device *dev;
  302. int error = 0;
  303. if (!sp)
  304. return -EINVAL;
  305. klist_iter_init_node(&sp->klist_devices, &i,
  306. (start ? &start->p->knode_bus : NULL));
  307. while (!error && (dev = next_device(&i)))
  308. error = fn(dev, data);
  309. klist_iter_exit(&i);
  310. subsys_put(sp);
  311. return error;
  312. }
  313. EXPORT_SYMBOL_GPL(bus_for_each_dev);
  314. /**
  315. * bus_find_device - device iterator for locating a particular device.
  316. * @bus: bus type
  317. * @start: Device to begin with
  318. * @data: Data to pass to match function
  319. * @match: Callback function to check device
  320. *
  321. * This is similar to the bus_for_each_dev() function above, but it
  322. * returns a reference to a device that is 'found' for later use, as
  323. * determined by the @match callback.
  324. *
  325. * The callback should return 0 if the device doesn't match and non-zero
  326. * if it does. If the callback returns non-zero, this function will
  327. * return to the caller and not iterate over any more devices.
  328. */
  329. struct device *bus_find_device(const struct bus_type *bus,
  330. struct device *start, const void *data,
  331. device_match_t match)
  332. {
  333. struct subsys_private *sp = bus_to_subsys(bus);
  334. struct klist_iter i;
  335. struct device *dev;
  336. if (!sp)
  337. return NULL;
  338. klist_iter_init_node(&sp->klist_devices, &i,
  339. (start ? &start->p->knode_bus : NULL));
  340. while ((dev = next_device(&i)))
  341. if (match(dev, data) && get_device(dev))
  342. break;
  343. klist_iter_exit(&i);
  344. subsys_put(sp);
  345. return dev;
  346. }
  347. EXPORT_SYMBOL_GPL(bus_find_device);
  348. static struct device_driver *next_driver(struct klist_iter *i)
  349. {
  350. struct klist_node *n = klist_next(i);
  351. struct driver_private *drv_priv;
  352. if (n) {
  353. drv_priv = container_of(n, struct driver_private, knode_bus);
  354. return drv_priv->driver;
  355. }
  356. return NULL;
  357. }
  358. /**
  359. * bus_for_each_drv - driver iterator
  360. * @bus: bus we're dealing with.
  361. * @start: driver to start iterating on.
  362. * @data: data to pass to the callback.
  363. * @fn: function to call for each driver.
  364. *
  365. * This is nearly identical to the device iterator above.
  366. * We iterate over each driver that belongs to @bus, and call
  367. * @fn for each. If @fn returns anything but 0, we break out
  368. * and return it. If @start is not NULL, we use it as the head
  369. * of the list.
  370. *
  371. * NOTE: we don't return the driver that returns a non-zero
  372. * value, nor do we leave the reference count incremented for that
  373. * driver. If the caller needs to know that info, it must set it
  374. * in the callback. It must also be sure to increment the refcount
  375. * so it doesn't disappear before returning to the caller.
  376. */
  377. int bus_for_each_drv(const struct bus_type *bus, struct device_driver *start,
  378. void *data, int (*fn)(struct device_driver *, void *))
  379. {
  380. struct subsys_private *sp = bus_to_subsys(bus);
  381. struct klist_iter i;
  382. struct device_driver *drv;
  383. int error = 0;
  384. if (!sp)
  385. return -EINVAL;
  386. klist_iter_init_node(&sp->klist_drivers, &i,
  387. start ? &start->p->knode_bus : NULL);
  388. while ((drv = next_driver(&i)) && !error)
  389. error = fn(drv, data);
  390. klist_iter_exit(&i);
  391. subsys_put(sp);
  392. return error;
  393. }
  394. EXPORT_SYMBOL_GPL(bus_for_each_drv);
  395. /**
  396. * bus_add_device - add device to bus
  397. * @dev: device being added
  398. *
  399. * - Add device's bus attributes.
  400. * - Create links to device's bus.
  401. * - Add the device to its bus's list of devices.
  402. */
  403. int bus_add_device(struct device *dev)
  404. {
  405. struct subsys_private *sp = bus_to_subsys(dev->bus);
  406. int error;
  407. if (!sp) {
  408. /*
  409. * This is a normal operation for many devices that do not
  410. * have a bus assigned to them, just say that all went
  411. * well.
  412. */
  413. return 0;
  414. }
  415. /*
  416. * Reference in sp is now incremented and will be dropped when
  417. * the device is removed from the bus
  418. */
  419. pr_debug("bus: '%s': add device %s\n", sp->bus->name, dev_name(dev));
  420. error = device_add_groups(dev, sp->bus->dev_groups);
  421. if (error)
  422. goto out_put;
  423. error = sysfs_create_link(&sp->devices_kset->kobj, &dev->kobj, dev_name(dev));
  424. if (error)
  425. goto out_groups;
  426. error = sysfs_create_link(&dev->kobj, &sp->subsys.kobj, "subsystem");
  427. if (error)
  428. goto out_subsys;
  429. klist_add_tail(&dev->p->knode_bus, &sp->klist_devices);
  430. return 0;
  431. out_subsys:
  432. sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev));
  433. out_groups:
  434. device_remove_groups(dev, sp->bus->dev_groups);
  435. out_put:
  436. subsys_put(sp);
  437. return error;
  438. }
  439. /**
  440. * bus_probe_device - probe drivers for a new device
  441. * @dev: device to probe
  442. *
  443. * - Automatically probe for a driver if the bus allows it.
  444. */
  445. void bus_probe_device(struct device *dev)
  446. {
  447. struct subsys_private *sp = bus_to_subsys(dev->bus);
  448. struct subsys_interface *sif;
  449. if (!sp)
  450. return;
  451. if (sp->drivers_autoprobe)
  452. device_initial_probe(dev);
  453. mutex_lock(&sp->mutex);
  454. list_for_each_entry(sif, &sp->interfaces, node)
  455. if (sif->add_dev)
  456. sif->add_dev(dev, sif);
  457. mutex_unlock(&sp->mutex);
  458. subsys_put(sp);
  459. }
  460. /**
  461. * bus_remove_device - remove device from bus
  462. * @dev: device to be removed
  463. *
  464. * - Remove device from all interfaces.
  465. * - Remove symlink from bus' directory.
  466. * - Delete device from bus's list.
  467. * - Detach from its driver.
  468. * - Drop reference taken in bus_add_device().
  469. */
  470. void bus_remove_device(struct device *dev)
  471. {
  472. struct subsys_private *sp = bus_to_subsys(dev->bus);
  473. struct subsys_interface *sif;
  474. if (!sp)
  475. return;
  476. mutex_lock(&sp->mutex);
  477. list_for_each_entry(sif, &sp->interfaces, node)
  478. if (sif->remove_dev)
  479. sif->remove_dev(dev, sif);
  480. mutex_unlock(&sp->mutex);
  481. sysfs_remove_link(&dev->kobj, "subsystem");
  482. sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev));
  483. device_remove_groups(dev, dev->bus->dev_groups);
  484. if (klist_node_attached(&dev->p->knode_bus))
  485. klist_del(&dev->p->knode_bus);
  486. pr_debug("bus: '%s': remove device %s\n",
  487. dev->bus->name, dev_name(dev));
  488. device_release_driver(dev);
  489. /*
  490. * Decrement the reference count twice, once for the bus_to_subsys()
  491. * call in the start of this function, and the second one from the
  492. * reference increment in bus_add_device()
  493. */
  494. subsys_put(sp);
  495. subsys_put(sp);
  496. }
  497. static int __must_check add_bind_files(struct device_driver *drv)
  498. {
  499. int ret;
  500. ret = driver_create_file(drv, &driver_attr_unbind);
  501. if (ret == 0) {
  502. ret = driver_create_file(drv, &driver_attr_bind);
  503. if (ret)
  504. driver_remove_file(drv, &driver_attr_unbind);
  505. }
  506. return ret;
  507. }
  508. static void remove_bind_files(struct device_driver *drv)
  509. {
  510. driver_remove_file(drv, &driver_attr_bind);
  511. driver_remove_file(drv, &driver_attr_unbind);
  512. }
  513. static BUS_ATTR_WO(drivers_probe);
  514. static BUS_ATTR_RW(drivers_autoprobe);
  515. static int add_probe_files(const struct bus_type *bus)
  516. {
  517. int retval;
  518. retval = bus_create_file(bus, &bus_attr_drivers_probe);
  519. if (retval)
  520. goto out;
  521. retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
  522. if (retval)
  523. bus_remove_file(bus, &bus_attr_drivers_probe);
  524. out:
  525. return retval;
  526. }
  527. static void remove_probe_files(const struct bus_type *bus)
  528. {
  529. bus_remove_file(bus, &bus_attr_drivers_autoprobe);
  530. bus_remove_file(bus, &bus_attr_drivers_probe);
  531. }
  532. static ssize_t uevent_store(struct device_driver *drv, const char *buf,
  533. size_t count)
  534. {
  535. int rc;
  536. rc = kobject_synth_uevent(&drv->p->kobj, buf, count);
  537. return rc ? rc : count;
  538. }
  539. static DRIVER_ATTR_WO(uevent);
  540. /**
  541. * bus_add_driver - Add a driver to the bus.
  542. * @drv: driver.
  543. */
  544. int bus_add_driver(struct device_driver *drv)
  545. {
  546. struct subsys_private *sp = bus_to_subsys(drv->bus);
  547. struct driver_private *priv;
  548. int error = 0;
  549. if (!sp)
  550. return -EINVAL;
  551. /*
  552. * Reference in sp is now incremented and will be dropped when
  553. * the driver is removed from the bus
  554. */
  555. pr_debug("bus: '%s': add driver %s\n", sp->bus->name, drv->name);
  556. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  557. if (!priv) {
  558. error = -ENOMEM;
  559. goto out_put_bus;
  560. }
  561. klist_init(&priv->klist_devices, NULL, NULL);
  562. priv->driver = drv;
  563. drv->p = priv;
  564. priv->kobj.kset = sp->drivers_kset;
  565. error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
  566. "%s", drv->name);
  567. if (error)
  568. goto out_unregister;
  569. klist_add_tail(&priv->knode_bus, &sp->klist_drivers);
  570. if (sp->drivers_autoprobe) {
  571. error = driver_attach(drv);
  572. if (error)
  573. goto out_del_list;
  574. }
  575. error = module_add_driver(drv->owner, drv);
  576. if (error) {
  577. printk(KERN_ERR "%s: failed to create module links for %s\n",
  578. __func__, drv->name);
  579. goto out_detach;
  580. }
  581. error = driver_create_file(drv, &driver_attr_uevent);
  582. if (error) {
  583. printk(KERN_ERR "%s: uevent attr (%s) failed\n",
  584. __func__, drv->name);
  585. }
  586. error = driver_add_groups(drv, sp->bus->drv_groups);
  587. if (error) {
  588. /* How the hell do we get out of this pickle? Give up */
  589. printk(KERN_ERR "%s: driver_add_groups(%s) failed\n",
  590. __func__, drv->name);
  591. }
  592. if (!drv->suppress_bind_attrs) {
  593. error = add_bind_files(drv);
  594. if (error) {
  595. /* Ditto */
  596. printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
  597. __func__, drv->name);
  598. }
  599. }
  600. return 0;
  601. out_detach:
  602. driver_detach(drv);
  603. out_del_list:
  604. klist_del(&priv->knode_bus);
  605. out_unregister:
  606. kobject_put(&priv->kobj);
  607. /* drv->p is freed in driver_release() */
  608. drv->p = NULL;
  609. out_put_bus:
  610. subsys_put(sp);
  611. return error;
  612. }
  613. /**
  614. * bus_remove_driver - delete driver from bus's knowledge.
  615. * @drv: driver.
  616. *
  617. * Detach the driver from the devices it controls, and remove
  618. * it from its bus's list of drivers. Finally, we drop the reference
  619. * to the bus we took in bus_add_driver().
  620. */
  621. void bus_remove_driver(struct device_driver *drv)
  622. {
  623. struct subsys_private *sp = bus_to_subsys(drv->bus);
  624. if (!sp)
  625. return;
  626. pr_debug("bus: '%s': remove driver %s\n", sp->bus->name, drv->name);
  627. if (!drv->suppress_bind_attrs)
  628. remove_bind_files(drv);
  629. driver_remove_groups(drv, sp->bus->drv_groups);
  630. driver_remove_file(drv, &driver_attr_uevent);
  631. klist_remove(&drv->p->knode_bus);
  632. driver_detach(drv);
  633. module_remove_driver(drv);
  634. kobject_put(&drv->p->kobj);
  635. /*
  636. * Decrement the reference count twice, once for the bus_to_subsys()
  637. * call in the start of this function, and the second one from the
  638. * reference increment in bus_add_driver()
  639. */
  640. subsys_put(sp);
  641. subsys_put(sp);
  642. }
  643. /* Helper for bus_rescan_devices's iter */
  644. static int __must_check bus_rescan_devices_helper(struct device *dev,
  645. void *data)
  646. {
  647. int ret = 0;
  648. if (!dev->driver) {
  649. if (dev->parent && dev->bus->need_parent_lock)
  650. device_lock(dev->parent);
  651. ret = device_attach(dev);
  652. if (dev->parent && dev->bus->need_parent_lock)
  653. device_unlock(dev->parent);
  654. }
  655. return ret < 0 ? ret : 0;
  656. }
  657. /**
  658. * bus_rescan_devices - rescan devices on the bus for possible drivers
  659. * @bus: the bus to scan.
  660. *
  661. * This function will look for devices on the bus with no driver
  662. * attached and rescan it against existing drivers to see if it matches
  663. * any by calling device_attach() for the unbound devices.
  664. */
  665. int bus_rescan_devices(const struct bus_type *bus)
  666. {
  667. return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
  668. }
  669. EXPORT_SYMBOL_GPL(bus_rescan_devices);
  670. /**
  671. * device_reprobe - remove driver for a device and probe for a new driver
  672. * @dev: the device to reprobe
  673. *
  674. * This function detaches the attached driver (if any) for the given
  675. * device and restarts the driver probing process. It is intended
  676. * to use if probing criteria changed during a devices lifetime and
  677. * driver attachment should change accordingly.
  678. */
  679. int device_reprobe(struct device *dev)
  680. {
  681. if (dev->driver)
  682. device_driver_detach(dev);
  683. return bus_rescan_devices_helper(dev, NULL);
  684. }
  685. EXPORT_SYMBOL_GPL(device_reprobe);
  686. static void klist_devices_get(struct klist_node *n)
  687. {
  688. struct device_private *dev_prv = to_device_private_bus(n);
  689. struct device *dev = dev_prv->device;
  690. get_device(dev);
  691. }
  692. static void klist_devices_put(struct klist_node *n)
  693. {
  694. struct device_private *dev_prv = to_device_private_bus(n);
  695. struct device *dev = dev_prv->device;
  696. put_device(dev);
  697. }
  698. static ssize_t bus_uevent_store(const struct bus_type *bus,
  699. const char *buf, size_t count)
  700. {
  701. struct subsys_private *sp = bus_to_subsys(bus);
  702. int ret;
  703. if (!sp)
  704. return -EINVAL;
  705. ret = kobject_synth_uevent(&sp->subsys.kobj, buf, count);
  706. subsys_put(sp);
  707. if (ret)
  708. return ret;
  709. return count;
  710. }
  711. /*
  712. * "open code" the old BUS_ATTR() macro here. We want to use BUS_ATTR_WO()
  713. * here, but can not use it as earlier in the file we have
  714. * DEVICE_ATTR_WO(uevent), which would cause a clash with the with the store
  715. * function name.
  716. */
  717. static struct bus_attribute bus_attr_uevent = __ATTR(uevent, 0200, NULL,
  718. bus_uevent_store);
  719. /**
  720. * bus_register - register a driver-core subsystem
  721. * @bus: bus to register
  722. *
  723. * Once we have that, we register the bus with the kobject
  724. * infrastructure, then register the children subsystems it has:
  725. * the devices and drivers that belong to the subsystem.
  726. */
  727. int bus_register(const struct bus_type *bus)
  728. {
  729. int retval;
  730. struct subsys_private *priv;
  731. struct kobject *bus_kobj;
  732. struct lock_class_key *key;
  733. priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
  734. if (!priv)
  735. return -ENOMEM;
  736. priv->bus = bus;
  737. BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
  738. bus_kobj = &priv->subsys.kobj;
  739. retval = kobject_set_name(bus_kobj, "%s", bus->name);
  740. if (retval)
  741. goto out;
  742. bus_kobj->kset = bus_kset;
  743. bus_kobj->ktype = &bus_ktype;
  744. priv->drivers_autoprobe = 1;
  745. retval = kset_register(&priv->subsys);
  746. if (retval)
  747. goto out;
  748. retval = bus_create_file(bus, &bus_attr_uevent);
  749. if (retval)
  750. goto bus_uevent_fail;
  751. priv->devices_kset = kset_create_and_add("devices", NULL, bus_kobj);
  752. if (!priv->devices_kset) {
  753. retval = -ENOMEM;
  754. goto bus_devices_fail;
  755. }
  756. priv->drivers_kset = kset_create_and_add("drivers", NULL, bus_kobj);
  757. if (!priv->drivers_kset) {
  758. retval = -ENOMEM;
  759. goto bus_drivers_fail;
  760. }
  761. INIT_LIST_HEAD(&priv->interfaces);
  762. key = &priv->lock_key;
  763. lockdep_register_key(key);
  764. __mutex_init(&priv->mutex, "subsys mutex", key);
  765. klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
  766. klist_init(&priv->klist_drivers, NULL, NULL);
  767. retval = add_probe_files(bus);
  768. if (retval)
  769. goto bus_probe_files_fail;
  770. retval = sysfs_create_groups(bus_kobj, bus->bus_groups);
  771. if (retval)
  772. goto bus_groups_fail;
  773. pr_debug("bus: '%s': registered\n", bus->name);
  774. return 0;
  775. bus_groups_fail:
  776. remove_probe_files(bus);
  777. bus_probe_files_fail:
  778. kset_unregister(priv->drivers_kset);
  779. bus_drivers_fail:
  780. kset_unregister(priv->devices_kset);
  781. bus_devices_fail:
  782. bus_remove_file(bus, &bus_attr_uevent);
  783. bus_uevent_fail:
  784. kset_unregister(&priv->subsys);
  785. /* Above kset_unregister() will kfree @priv */
  786. priv = NULL;
  787. out:
  788. kfree(priv);
  789. return retval;
  790. }
  791. EXPORT_SYMBOL_GPL(bus_register);
  792. /**
  793. * bus_unregister - remove a bus from the system
  794. * @bus: bus.
  795. *
  796. * Unregister the child subsystems and the bus itself.
  797. * Finally, we call bus_put() to release the refcount
  798. */
  799. void bus_unregister(const struct bus_type *bus)
  800. {
  801. struct subsys_private *sp = bus_to_subsys(bus);
  802. struct kobject *bus_kobj;
  803. if (!sp)
  804. return;
  805. pr_debug("bus: '%s': unregistering\n", bus->name);
  806. if (sp->dev_root)
  807. device_unregister(sp->dev_root);
  808. bus_kobj = &sp->subsys.kobj;
  809. sysfs_remove_groups(bus_kobj, bus->bus_groups);
  810. remove_probe_files(bus);
  811. bus_remove_file(bus, &bus_attr_uevent);
  812. kset_unregister(sp->drivers_kset);
  813. kset_unregister(sp->devices_kset);
  814. kset_unregister(&sp->subsys);
  815. subsys_put(sp);
  816. }
  817. EXPORT_SYMBOL_GPL(bus_unregister);
  818. int bus_register_notifier(const struct bus_type *bus, struct notifier_block *nb)
  819. {
  820. struct subsys_private *sp = bus_to_subsys(bus);
  821. int retval;
  822. if (!sp)
  823. return -EINVAL;
  824. retval = blocking_notifier_chain_register(&sp->bus_notifier, nb);
  825. subsys_put(sp);
  826. return retval;
  827. }
  828. EXPORT_SYMBOL_GPL(bus_register_notifier);
  829. int bus_unregister_notifier(const struct bus_type *bus, struct notifier_block *nb)
  830. {
  831. struct subsys_private *sp = bus_to_subsys(bus);
  832. int retval;
  833. if (!sp)
  834. return -EINVAL;
  835. retval = blocking_notifier_chain_unregister(&sp->bus_notifier, nb);
  836. subsys_put(sp);
  837. return retval;
  838. }
  839. EXPORT_SYMBOL_GPL(bus_unregister_notifier);
  840. void bus_notify(struct device *dev, enum bus_notifier_event value)
  841. {
  842. struct subsys_private *sp = bus_to_subsys(dev->bus);
  843. if (!sp)
  844. return;
  845. blocking_notifier_call_chain(&sp->bus_notifier, value, dev);
  846. subsys_put(sp);
  847. }
  848. struct kset *bus_get_kset(const struct bus_type *bus)
  849. {
  850. struct subsys_private *sp = bus_to_subsys(bus);
  851. struct kset *kset;
  852. if (!sp)
  853. return NULL;
  854. kset = &sp->subsys;
  855. subsys_put(sp);
  856. return kset;
  857. }
  858. EXPORT_SYMBOL_GPL(bus_get_kset);
  859. /*
  860. * Yes, this forcibly breaks the klist abstraction temporarily. It
  861. * just wants to sort the klist, not change reference counts and
  862. * take/drop locks rapidly in the process. It does all this while
  863. * holding the lock for the list, so objects can't otherwise be
  864. * added/removed while we're swizzling.
  865. */
  866. static void device_insertion_sort_klist(struct device *a, struct list_head *list,
  867. int (*compare)(const struct device *a,
  868. const struct device *b))
  869. {
  870. struct klist_node *n;
  871. struct device_private *dev_prv;
  872. struct device *b;
  873. list_for_each_entry(n, list, n_node) {
  874. dev_prv = to_device_private_bus(n);
  875. b = dev_prv->device;
  876. if (compare(a, b) <= 0) {
  877. list_move_tail(&a->p->knode_bus.n_node,
  878. &b->p->knode_bus.n_node);
  879. return;
  880. }
  881. }
  882. list_move_tail(&a->p->knode_bus.n_node, list);
  883. }
  884. void bus_sort_breadthfirst(const struct bus_type *bus,
  885. int (*compare)(const struct device *a,
  886. const struct device *b))
  887. {
  888. struct subsys_private *sp = bus_to_subsys(bus);
  889. LIST_HEAD(sorted_devices);
  890. struct klist_node *n, *tmp;
  891. struct device_private *dev_prv;
  892. struct device *dev;
  893. struct klist *device_klist;
  894. if (!sp)
  895. return;
  896. device_klist = &sp->klist_devices;
  897. spin_lock(&device_klist->k_lock);
  898. list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) {
  899. dev_prv = to_device_private_bus(n);
  900. dev = dev_prv->device;
  901. device_insertion_sort_klist(dev, &sorted_devices, compare);
  902. }
  903. list_splice(&sorted_devices, &device_klist->k_list);
  904. spin_unlock(&device_klist->k_lock);
  905. subsys_put(sp);
  906. }
  907. EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
  908. struct subsys_dev_iter {
  909. struct klist_iter ki;
  910. const struct device_type *type;
  911. };
  912. /**
  913. * subsys_dev_iter_init - initialize subsys device iterator
  914. * @iter: subsys iterator to initialize
  915. * @sp: the subsys private (i.e. bus) we wanna iterate over
  916. * @start: the device to start iterating from, if any
  917. * @type: device_type of the devices to iterate over, NULL for all
  918. *
  919. * Initialize subsys iterator @iter such that it iterates over devices
  920. * of @subsys. If @start is set, the list iteration will start there,
  921. * otherwise if it is NULL, the iteration starts at the beginning of
  922. * the list.
  923. */
  924. static void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct subsys_private *sp,
  925. struct device *start, const struct device_type *type)
  926. {
  927. struct klist_node *start_knode = NULL;
  928. if (start)
  929. start_knode = &start->p->knode_bus;
  930. klist_iter_init_node(&sp->klist_devices, &iter->ki, start_knode);
  931. iter->type = type;
  932. }
  933. /**
  934. * subsys_dev_iter_next - iterate to the next device
  935. * @iter: subsys iterator to proceed
  936. *
  937. * Proceed @iter to the next device and return it. Returns NULL if
  938. * iteration is complete.
  939. *
  940. * The returned device is referenced and won't be released till
  941. * iterator is proceed to the next device or exited. The caller is
  942. * free to do whatever it wants to do with the device including
  943. * calling back into subsys code.
  944. */
  945. static struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
  946. {
  947. struct klist_node *knode;
  948. struct device *dev;
  949. for (;;) {
  950. knode = klist_next(&iter->ki);
  951. if (!knode)
  952. return NULL;
  953. dev = to_device_private_bus(knode)->device;
  954. if (!iter->type || iter->type == dev->type)
  955. return dev;
  956. }
  957. }
  958. /**
  959. * subsys_dev_iter_exit - finish iteration
  960. * @iter: subsys iterator to finish
  961. *
  962. * Finish an iteration. Always call this function after iteration is
  963. * complete whether the iteration ran till the end or not.
  964. */
  965. static void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
  966. {
  967. klist_iter_exit(&iter->ki);
  968. }
  969. int subsys_interface_register(struct subsys_interface *sif)
  970. {
  971. struct subsys_private *sp;
  972. struct subsys_dev_iter iter;
  973. struct device *dev;
  974. if (!sif || !sif->subsys)
  975. return -ENODEV;
  976. sp = bus_to_subsys(sif->subsys);
  977. if (!sp)
  978. return -EINVAL;
  979. /*
  980. * Reference in sp is now incremented and will be dropped when
  981. * the interface is removed from the bus
  982. */
  983. mutex_lock(&sp->mutex);
  984. list_add_tail(&sif->node, &sp->interfaces);
  985. if (sif->add_dev) {
  986. subsys_dev_iter_init(&iter, sp, NULL, NULL);
  987. while ((dev = subsys_dev_iter_next(&iter)))
  988. sif->add_dev(dev, sif);
  989. subsys_dev_iter_exit(&iter);
  990. }
  991. mutex_unlock(&sp->mutex);
  992. return 0;
  993. }
  994. EXPORT_SYMBOL_GPL(subsys_interface_register);
  995. void subsys_interface_unregister(struct subsys_interface *sif)
  996. {
  997. struct subsys_private *sp;
  998. struct subsys_dev_iter iter;
  999. struct device *dev;
  1000. if (!sif || !sif->subsys)
  1001. return;
  1002. sp = bus_to_subsys(sif->subsys);
  1003. if (!sp)
  1004. return;
  1005. mutex_lock(&sp->mutex);
  1006. list_del_init(&sif->node);
  1007. if (sif->remove_dev) {
  1008. subsys_dev_iter_init(&iter, sp, NULL, NULL);
  1009. while ((dev = subsys_dev_iter_next(&iter)))
  1010. sif->remove_dev(dev, sif);
  1011. subsys_dev_iter_exit(&iter);
  1012. }
  1013. mutex_unlock(&sp->mutex);
  1014. /*
  1015. * Decrement the reference count twice, once for the bus_to_subsys()
  1016. * call in the start of this function, and the second one from the
  1017. * reference increment in subsys_interface_register()
  1018. */
  1019. subsys_put(sp);
  1020. subsys_put(sp);
  1021. }
  1022. EXPORT_SYMBOL_GPL(subsys_interface_unregister);
  1023. static void system_root_device_release(struct device *dev)
  1024. {
  1025. kfree(dev);
  1026. }
  1027. static int subsys_register(const struct bus_type *subsys,
  1028. const struct attribute_group **groups,
  1029. struct kobject *parent_of_root)
  1030. {
  1031. struct subsys_private *sp;
  1032. struct device *dev;
  1033. int err;
  1034. err = bus_register(subsys);
  1035. if (err < 0)
  1036. return err;
  1037. sp = bus_to_subsys(subsys);
  1038. if (!sp) {
  1039. err = -EINVAL;
  1040. goto err_sp;
  1041. }
  1042. dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  1043. if (!dev) {
  1044. err = -ENOMEM;
  1045. goto err_dev;
  1046. }
  1047. err = dev_set_name(dev, "%s", subsys->name);
  1048. if (err < 0)
  1049. goto err_name;
  1050. dev->kobj.parent = parent_of_root;
  1051. dev->groups = groups;
  1052. dev->release = system_root_device_release;
  1053. err = device_register(dev);
  1054. if (err < 0)
  1055. goto err_dev_reg;
  1056. sp->dev_root = dev;
  1057. subsys_put(sp);
  1058. return 0;
  1059. err_dev_reg:
  1060. put_device(dev);
  1061. dev = NULL;
  1062. err_name:
  1063. kfree(dev);
  1064. err_dev:
  1065. subsys_put(sp);
  1066. err_sp:
  1067. bus_unregister(subsys);
  1068. return err;
  1069. }
  1070. /**
  1071. * subsys_system_register - register a subsystem at /sys/devices/system/
  1072. * @subsys: system subsystem
  1073. * @groups: default attributes for the root device
  1074. *
  1075. * All 'system' subsystems have a /sys/devices/system/<name> root device
  1076. * with the name of the subsystem. The root device can carry subsystem-
  1077. * wide attributes. All registered devices are below this single root
  1078. * device and are named after the subsystem with a simple enumeration
  1079. * number appended. The registered devices are not explicitly named;
  1080. * only 'id' in the device needs to be set.
  1081. *
  1082. * Do not use this interface for anything new, it exists for compatibility
  1083. * with bad ideas only. New subsystems should use plain subsystems; and
  1084. * add the subsystem-wide attributes should be added to the subsystem
  1085. * directory itself and not some create fake root-device placed in
  1086. * /sys/devices/system/<name>.
  1087. */
  1088. int subsys_system_register(const struct bus_type *subsys,
  1089. const struct attribute_group **groups)
  1090. {
  1091. return subsys_register(subsys, groups, &system_kset->kobj);
  1092. }
  1093. EXPORT_SYMBOL_GPL(subsys_system_register);
  1094. /**
  1095. * subsys_virtual_register - register a subsystem at /sys/devices/virtual/
  1096. * @subsys: virtual subsystem
  1097. * @groups: default attributes for the root device
  1098. *
  1099. * All 'virtual' subsystems have a /sys/devices/system/<name> root device
  1100. * with the name of the subystem. The root device can carry subsystem-wide
  1101. * attributes. All registered devices are below this single root device.
  1102. * There's no restriction on device naming. This is for kernel software
  1103. * constructs which need sysfs interface.
  1104. */
  1105. int subsys_virtual_register(const struct bus_type *subsys,
  1106. const struct attribute_group **groups)
  1107. {
  1108. struct kobject *virtual_dir;
  1109. virtual_dir = virtual_device_parent();
  1110. if (!virtual_dir)
  1111. return -ENOMEM;
  1112. return subsys_register(subsys, groups, virtual_dir);
  1113. }
  1114. EXPORT_SYMBOL_GPL(subsys_virtual_register);
  1115. /**
  1116. * driver_find - locate driver on a bus by its name.
  1117. * @name: name of the driver.
  1118. * @bus: bus to scan for the driver.
  1119. *
  1120. * Call kset_find_obj() to iterate over list of drivers on
  1121. * a bus to find driver by name. Return driver if found.
  1122. *
  1123. * This routine provides no locking to prevent the driver it returns
  1124. * from being unregistered or unloaded while the caller is using it.
  1125. * The caller is responsible for preventing this.
  1126. */
  1127. struct device_driver *driver_find(const char *name, const struct bus_type *bus)
  1128. {
  1129. struct subsys_private *sp = bus_to_subsys(bus);
  1130. struct kobject *k;
  1131. struct driver_private *priv;
  1132. if (!sp)
  1133. return NULL;
  1134. k = kset_find_obj(sp->drivers_kset, name);
  1135. subsys_put(sp);
  1136. if (!k)
  1137. return NULL;
  1138. priv = to_driver(k);
  1139. /* Drop reference added by kset_find_obj() */
  1140. kobject_put(k);
  1141. return priv->driver;
  1142. }
  1143. EXPORT_SYMBOL_GPL(driver_find);
  1144. /*
  1145. * Warning, the value could go to "removed" instantly after calling this function, so be very
  1146. * careful when calling it...
  1147. */
  1148. bool bus_is_registered(const struct bus_type *bus)
  1149. {
  1150. struct subsys_private *sp = bus_to_subsys(bus);
  1151. bool is_initialized = false;
  1152. if (sp) {
  1153. is_initialized = true;
  1154. subsys_put(sp);
  1155. }
  1156. return is_initialized;
  1157. }
  1158. /**
  1159. * bus_get_dev_root - return a pointer to the "device root" of a bus
  1160. * @bus: bus to return the device root of.
  1161. *
  1162. * If a bus has a "device root" structure, return it, WITH THE REFERENCE
  1163. * COUNT INCREMENTED.
  1164. *
  1165. * Note, when finished with the device, a call to put_device() is required.
  1166. *
  1167. * If the device root is not present (or bus is not a valid pointer), NULL
  1168. * will be returned.
  1169. */
  1170. struct device *bus_get_dev_root(const struct bus_type *bus)
  1171. {
  1172. struct subsys_private *sp = bus_to_subsys(bus);
  1173. struct device *dev_root;
  1174. if (!sp)
  1175. return NULL;
  1176. dev_root = get_device(sp->dev_root);
  1177. subsys_put(sp);
  1178. return dev_root;
  1179. }
  1180. EXPORT_SYMBOL_GPL(bus_get_dev_root);
  1181. int __init buses_init(void)
  1182. {
  1183. bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
  1184. if (!bus_kset)
  1185. return -ENOMEM;
  1186. system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
  1187. if (!system_kset) {
  1188. /* Do error handling here as devices_init() do */
  1189. kset_unregister(bus_kset);
  1190. bus_kset = NULL;
  1191. pr_err("%s: failed to create and add kset 'bus'\n", __func__);
  1192. return -ENOMEM;
  1193. }
  1194. return 0;
  1195. }