bus.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  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. static struct device *prev_device(struct klist_iter *i)
  278. {
  279. struct klist_node *n = klist_prev(i);
  280. struct device *dev = NULL;
  281. struct device_private *dev_prv;
  282. if (n) {
  283. dev_prv = to_device_private_bus(n);
  284. dev = dev_prv->device;
  285. }
  286. return dev;
  287. }
  288. /**
  289. * bus_for_each_dev - device iterator.
  290. * @bus: bus type.
  291. * @start: device to start iterating from.
  292. * @data: data for the callback.
  293. * @fn: function to be called for each device.
  294. *
  295. * Iterate over @bus's list of devices, and call @fn for each,
  296. * passing it @data. If @start is not NULL, we use that device to
  297. * begin iterating from.
  298. *
  299. * We check the return of @fn each time. If it returns anything
  300. * other than 0, we break out and return that value.
  301. *
  302. * NOTE: The device that returns a non-zero value is not retained
  303. * in any way, nor is its refcount incremented. If the caller needs
  304. * to retain this data, it should do so, and increment the reference
  305. * count in the supplied callback.
  306. */
  307. int bus_for_each_dev(const struct bus_type *bus, struct device *start,
  308. void *data, device_iter_t fn)
  309. {
  310. struct subsys_private *sp = bus_to_subsys(bus);
  311. struct klist_iter i;
  312. struct device *dev;
  313. int error = 0;
  314. if (!sp)
  315. return -EINVAL;
  316. klist_iter_init_node(&sp->klist_devices, &i,
  317. (start ? &start->p->knode_bus : NULL));
  318. while (!error && (dev = next_device(&i)))
  319. error = fn(dev, data);
  320. klist_iter_exit(&i);
  321. subsys_put(sp);
  322. return error;
  323. }
  324. EXPORT_SYMBOL_GPL(bus_for_each_dev);
  325. /**
  326. * bus_find_device - device iterator for locating a particular device.
  327. * @bus: bus type
  328. * @start: Device to begin with
  329. * @data: Data to pass to match function
  330. * @match: Callback function to check device
  331. *
  332. * This is similar to the bus_for_each_dev() function above, but it
  333. * returns a reference to a device that is 'found' for later use, as
  334. * determined by the @match callback.
  335. *
  336. * The callback should return 0 if the device doesn't match and non-zero
  337. * if it does. If the callback returns non-zero, this function will
  338. * return to the caller and not iterate over any more devices.
  339. */
  340. struct device *bus_find_device(const struct bus_type *bus,
  341. struct device *start, const void *data,
  342. device_match_t match)
  343. {
  344. struct subsys_private *sp = bus_to_subsys(bus);
  345. struct klist_iter i;
  346. struct device *dev;
  347. if (!sp)
  348. return NULL;
  349. klist_iter_init_node(&sp->klist_devices, &i,
  350. (start ? &start->p->knode_bus : NULL));
  351. while ((dev = next_device(&i))) {
  352. if (match(dev, data)) {
  353. get_device(dev);
  354. break;
  355. }
  356. }
  357. klist_iter_exit(&i);
  358. subsys_put(sp);
  359. return dev;
  360. }
  361. EXPORT_SYMBOL_GPL(bus_find_device);
  362. struct device *bus_find_device_reverse(const struct bus_type *bus,
  363. struct device *start, const void *data,
  364. device_match_t match)
  365. {
  366. struct subsys_private *sp = bus_to_subsys(bus);
  367. struct klist_iter i;
  368. struct device *dev;
  369. if (!sp)
  370. return NULL;
  371. klist_iter_init_node(&sp->klist_devices, &i,
  372. (start ? &start->p->knode_bus : NULL));
  373. while ((dev = prev_device(&i))) {
  374. if (match(dev, data)) {
  375. get_device(dev);
  376. break;
  377. }
  378. }
  379. klist_iter_exit(&i);
  380. subsys_put(sp);
  381. return dev;
  382. }
  383. EXPORT_SYMBOL_GPL(bus_find_device_reverse);
  384. static struct device_driver *next_driver(struct klist_iter *i)
  385. {
  386. struct klist_node *n = klist_next(i);
  387. struct driver_private *drv_priv;
  388. if (n) {
  389. drv_priv = container_of(n, struct driver_private, knode_bus);
  390. return drv_priv->driver;
  391. }
  392. return NULL;
  393. }
  394. /**
  395. * bus_for_each_drv - driver iterator
  396. * @bus: bus we're dealing with.
  397. * @start: driver to start iterating on.
  398. * @data: data to pass to the callback.
  399. * @fn: function to call for each driver.
  400. *
  401. * This is nearly identical to the device iterator above.
  402. * We iterate over each driver that belongs to @bus, and call
  403. * @fn for each. If @fn returns anything but 0, we break out
  404. * and return it. If @start is not NULL, we use it as the head
  405. * of the list.
  406. *
  407. * NOTE: we don't return the driver that returns a non-zero
  408. * value, nor do we leave the reference count incremented for that
  409. * driver. If the caller needs to know that info, it must set it
  410. * in the callback. It must also be sure to increment the refcount
  411. * so it doesn't disappear before returning to the caller.
  412. */
  413. int bus_for_each_drv(const struct bus_type *bus, struct device_driver *start,
  414. void *data, int (*fn)(struct device_driver *, void *))
  415. {
  416. struct subsys_private *sp = bus_to_subsys(bus);
  417. struct klist_iter i;
  418. struct device_driver *drv;
  419. int error = 0;
  420. if (!sp)
  421. return -EINVAL;
  422. klist_iter_init_node(&sp->klist_drivers, &i,
  423. start ? &start->p->knode_bus : NULL);
  424. while ((drv = next_driver(&i)) && !error)
  425. error = fn(drv, data);
  426. klist_iter_exit(&i);
  427. subsys_put(sp);
  428. return error;
  429. }
  430. EXPORT_SYMBOL_GPL(bus_for_each_drv);
  431. static ssize_t driver_override_store(struct device *dev,
  432. struct device_attribute *attr,
  433. const char *buf, size_t count)
  434. {
  435. int ret;
  436. ret = __device_set_driver_override(dev, buf, count);
  437. if (ret)
  438. return ret;
  439. return count;
  440. }
  441. static ssize_t driver_override_show(struct device *dev,
  442. struct device_attribute *attr, char *buf)
  443. {
  444. guard(spinlock)(&dev->driver_override.lock);
  445. return sysfs_emit(buf, "%s\n", dev->driver_override.name);
  446. }
  447. static DEVICE_ATTR_RW(driver_override);
  448. static struct attribute *driver_override_dev_attrs[] = {
  449. &dev_attr_driver_override.attr,
  450. NULL,
  451. };
  452. static const struct attribute_group driver_override_dev_group = {
  453. .attrs = driver_override_dev_attrs,
  454. };
  455. /**
  456. * bus_add_device - add device to bus
  457. * @dev: device being added
  458. *
  459. * - Add device's bus attributes.
  460. * - Create links to device's bus.
  461. * - Add the device to its bus's list of devices.
  462. */
  463. int bus_add_device(struct device *dev)
  464. {
  465. struct subsys_private *sp = bus_to_subsys(dev->bus);
  466. int error;
  467. if (!sp) {
  468. /*
  469. * This is a normal operation for many devices that do not
  470. * have a bus assigned to them, just say that all went
  471. * well.
  472. */
  473. return 0;
  474. }
  475. /*
  476. * Reference in sp is now incremented and will be dropped when
  477. * the device is removed from the bus
  478. */
  479. pr_debug("bus: '%s': add device %s\n", sp->bus->name, dev_name(dev));
  480. error = device_add_groups(dev, sp->bus->dev_groups);
  481. if (error)
  482. goto out_put;
  483. if (dev->bus->driver_override) {
  484. error = device_add_group(dev, &driver_override_dev_group);
  485. if (error)
  486. goto out_groups;
  487. }
  488. error = sysfs_create_link(&sp->devices_kset->kobj, &dev->kobj, dev_name(dev));
  489. if (error)
  490. goto out_override;
  491. error = sysfs_create_link(&dev->kobj, &sp->subsys.kobj, "subsystem");
  492. if (error)
  493. goto out_subsys;
  494. klist_add_tail(&dev->p->knode_bus, &sp->klist_devices);
  495. return 0;
  496. out_subsys:
  497. sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev));
  498. out_override:
  499. if (dev->bus->driver_override)
  500. device_remove_group(dev, &driver_override_dev_group);
  501. out_groups:
  502. device_remove_groups(dev, sp->bus->dev_groups);
  503. out_put:
  504. subsys_put(sp);
  505. return error;
  506. }
  507. /**
  508. * bus_probe_device - probe drivers for a new device
  509. * @dev: device to probe
  510. *
  511. * - Automatically probe for a driver if the bus allows it.
  512. */
  513. void bus_probe_device(struct device *dev)
  514. {
  515. struct subsys_private *sp = bus_to_subsys(dev->bus);
  516. struct subsys_interface *sif;
  517. if (!sp)
  518. return;
  519. device_initial_probe(dev);
  520. mutex_lock(&sp->mutex);
  521. list_for_each_entry(sif, &sp->interfaces, node)
  522. if (sif->add_dev)
  523. sif->add_dev(dev, sif);
  524. mutex_unlock(&sp->mutex);
  525. subsys_put(sp);
  526. }
  527. /**
  528. * bus_remove_device - remove device from bus
  529. * @dev: device to be removed
  530. *
  531. * - Remove device from all interfaces.
  532. * - Remove symlink from bus' directory.
  533. * - Delete device from bus's list.
  534. * - Detach from its driver.
  535. * - Drop reference taken in bus_add_device().
  536. */
  537. void bus_remove_device(struct device *dev)
  538. {
  539. struct subsys_private *sp = bus_to_subsys(dev->bus);
  540. struct subsys_interface *sif;
  541. if (!sp)
  542. return;
  543. mutex_lock(&sp->mutex);
  544. list_for_each_entry(sif, &sp->interfaces, node)
  545. if (sif->remove_dev)
  546. sif->remove_dev(dev, sif);
  547. mutex_unlock(&sp->mutex);
  548. sysfs_remove_link(&dev->kobj, "subsystem");
  549. sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev));
  550. if (dev->bus->driver_override)
  551. device_remove_group(dev, &driver_override_dev_group);
  552. device_remove_groups(dev, dev->bus->dev_groups);
  553. if (klist_node_attached(&dev->p->knode_bus))
  554. klist_del(&dev->p->knode_bus);
  555. pr_debug("bus: '%s': remove device %s\n",
  556. dev->bus->name, dev_name(dev));
  557. device_release_driver(dev);
  558. /*
  559. * Decrement the reference count twice, once for the bus_to_subsys()
  560. * call in the start of this function, and the second one from the
  561. * reference increment in bus_add_device()
  562. */
  563. subsys_put(sp);
  564. subsys_put(sp);
  565. }
  566. static int __must_check add_bind_files(struct device_driver *drv)
  567. {
  568. int ret;
  569. ret = driver_create_file(drv, &driver_attr_unbind);
  570. if (ret == 0) {
  571. ret = driver_create_file(drv, &driver_attr_bind);
  572. if (ret)
  573. driver_remove_file(drv, &driver_attr_unbind);
  574. }
  575. return ret;
  576. }
  577. static void remove_bind_files(struct device_driver *drv)
  578. {
  579. driver_remove_file(drv, &driver_attr_bind);
  580. driver_remove_file(drv, &driver_attr_unbind);
  581. }
  582. static BUS_ATTR_WO(drivers_probe);
  583. static BUS_ATTR_RW(drivers_autoprobe);
  584. static int add_probe_files(const struct bus_type *bus)
  585. {
  586. int retval;
  587. retval = bus_create_file(bus, &bus_attr_drivers_probe);
  588. if (retval)
  589. goto out;
  590. retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
  591. if (retval)
  592. bus_remove_file(bus, &bus_attr_drivers_probe);
  593. out:
  594. return retval;
  595. }
  596. static void remove_probe_files(const struct bus_type *bus)
  597. {
  598. bus_remove_file(bus, &bus_attr_drivers_autoprobe);
  599. bus_remove_file(bus, &bus_attr_drivers_probe);
  600. }
  601. static ssize_t uevent_store(struct device_driver *drv, const char *buf,
  602. size_t count)
  603. {
  604. int rc;
  605. rc = kobject_synth_uevent(&drv->p->kobj, buf, count);
  606. return rc ? rc : count;
  607. }
  608. static DRIVER_ATTR_WO(uevent);
  609. /**
  610. * bus_add_driver - Add a driver to the bus.
  611. * @drv: driver.
  612. */
  613. int bus_add_driver(struct device_driver *drv)
  614. {
  615. struct subsys_private *sp = bus_to_subsys(drv->bus);
  616. struct driver_private *priv;
  617. int error = 0;
  618. if (!sp)
  619. return -EINVAL;
  620. /*
  621. * Reference in sp is now incremented and will be dropped when
  622. * the driver is removed from the bus
  623. */
  624. pr_debug("bus: '%s': add driver %s\n", sp->bus->name, drv->name);
  625. priv = kzalloc_obj(*priv);
  626. if (!priv) {
  627. error = -ENOMEM;
  628. goto out_put_bus;
  629. }
  630. klist_init(&priv->klist_devices, NULL, NULL);
  631. priv->driver = drv;
  632. drv->p = priv;
  633. priv->kobj.kset = sp->drivers_kset;
  634. error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
  635. "%s", drv->name);
  636. if (error)
  637. goto out_unregister;
  638. klist_add_tail(&priv->knode_bus, &sp->klist_drivers);
  639. if (sp->drivers_autoprobe) {
  640. error = driver_attach(drv);
  641. if (error)
  642. goto out_del_list;
  643. }
  644. error = module_add_driver(drv->owner, drv);
  645. if (error) {
  646. printk(KERN_ERR "%s: failed to create module links for %s\n",
  647. __func__, drv->name);
  648. goto out_detach;
  649. }
  650. error = driver_create_file(drv, &driver_attr_uevent);
  651. if (error) {
  652. printk(KERN_ERR "%s: uevent attr (%s) failed\n",
  653. __func__, drv->name);
  654. }
  655. error = driver_add_groups(drv, sp->bus->drv_groups);
  656. if (error) {
  657. /* How the hell do we get out of this pickle? Give up */
  658. printk(KERN_ERR "%s: driver_add_groups(%s) failed\n",
  659. __func__, drv->name);
  660. }
  661. if (!drv->suppress_bind_attrs) {
  662. error = add_bind_files(drv);
  663. if (error) {
  664. /* Ditto */
  665. printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
  666. __func__, drv->name);
  667. }
  668. }
  669. return 0;
  670. out_detach:
  671. driver_detach(drv);
  672. out_del_list:
  673. klist_del(&priv->knode_bus);
  674. out_unregister:
  675. kobject_put(&priv->kobj);
  676. /* drv->p is freed in driver_release() */
  677. drv->p = NULL;
  678. out_put_bus:
  679. subsys_put(sp);
  680. return error;
  681. }
  682. /**
  683. * bus_remove_driver - delete driver from bus's knowledge.
  684. * @drv: driver.
  685. *
  686. * Detach the driver from the devices it controls, and remove
  687. * it from its bus's list of drivers. Finally, we drop the reference
  688. * to the bus we took in bus_add_driver().
  689. */
  690. void bus_remove_driver(struct device_driver *drv)
  691. {
  692. struct subsys_private *sp = bus_to_subsys(drv->bus);
  693. if (!sp)
  694. return;
  695. pr_debug("bus: '%s': remove driver %s\n", sp->bus->name, drv->name);
  696. if (!drv->suppress_bind_attrs)
  697. remove_bind_files(drv);
  698. driver_remove_groups(drv, sp->bus->drv_groups);
  699. driver_remove_file(drv, &driver_attr_uevent);
  700. klist_remove(&drv->p->knode_bus);
  701. driver_detach(drv);
  702. module_remove_driver(drv);
  703. kobject_put(&drv->p->kobj);
  704. /*
  705. * Decrement the reference count twice, once for the bus_to_subsys()
  706. * call in the start of this function, and the second one from the
  707. * reference increment in bus_add_driver()
  708. */
  709. subsys_put(sp);
  710. subsys_put(sp);
  711. }
  712. /* Helper for bus_rescan_devices's iter */
  713. static int __must_check bus_rescan_devices_helper(struct device *dev,
  714. void *data)
  715. {
  716. int ret = 0;
  717. if (!dev->driver) {
  718. if (dev->parent && dev->bus->need_parent_lock)
  719. device_lock(dev->parent);
  720. ret = device_attach(dev);
  721. if (dev->parent && dev->bus->need_parent_lock)
  722. device_unlock(dev->parent);
  723. }
  724. return ret < 0 ? ret : 0;
  725. }
  726. /**
  727. * bus_rescan_devices - rescan devices on the bus for possible drivers
  728. * @bus: the bus to scan.
  729. *
  730. * This function will look for devices on the bus with no driver
  731. * attached and rescan it against existing drivers to see if it matches
  732. * any by calling device_attach() for the unbound devices.
  733. */
  734. int bus_rescan_devices(const struct bus_type *bus)
  735. {
  736. return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
  737. }
  738. EXPORT_SYMBOL_GPL(bus_rescan_devices);
  739. /**
  740. * device_reprobe - remove driver for a device and probe for a new driver
  741. * @dev: the device to reprobe
  742. *
  743. * This function detaches the attached driver (if any) for the given
  744. * device and restarts the driver probing process. It is intended
  745. * to use if probing criteria changed during a devices lifetime and
  746. * driver attachment should change accordingly.
  747. */
  748. int device_reprobe(struct device *dev)
  749. {
  750. if (dev->driver)
  751. device_driver_detach(dev);
  752. return bus_rescan_devices_helper(dev, NULL);
  753. }
  754. EXPORT_SYMBOL_GPL(device_reprobe);
  755. static void klist_devices_get(struct klist_node *n)
  756. {
  757. struct device_private *dev_prv = to_device_private_bus(n);
  758. struct device *dev = dev_prv->device;
  759. get_device(dev);
  760. }
  761. static void klist_devices_put(struct klist_node *n)
  762. {
  763. struct device_private *dev_prv = to_device_private_bus(n);
  764. struct device *dev = dev_prv->device;
  765. put_device(dev);
  766. }
  767. static ssize_t bus_uevent_store(const struct bus_type *bus,
  768. const char *buf, size_t count)
  769. {
  770. struct subsys_private *sp = bus_to_subsys(bus);
  771. int ret;
  772. if (!sp)
  773. return -EINVAL;
  774. ret = kobject_synth_uevent(&sp->subsys.kobj, buf, count);
  775. subsys_put(sp);
  776. if (ret)
  777. return ret;
  778. return count;
  779. }
  780. /*
  781. * "open code" the old BUS_ATTR() macro here. We want to use BUS_ATTR_WO()
  782. * here, but can not use it as earlier in the file we have
  783. * DEVICE_ATTR_WO(uevent), which would cause a clash with the with the store
  784. * function name.
  785. */
  786. static struct bus_attribute bus_attr_uevent = __ATTR(uevent, 0200, NULL,
  787. bus_uevent_store);
  788. /**
  789. * bus_register - register a driver-core subsystem
  790. * @bus: bus to register
  791. *
  792. * Once we have that, we register the bus with the kobject
  793. * infrastructure, then register the children subsystems it has:
  794. * the devices and drivers that belong to the subsystem.
  795. */
  796. int bus_register(const struct bus_type *bus)
  797. {
  798. int retval;
  799. struct subsys_private *priv;
  800. struct kobject *bus_kobj;
  801. struct lock_class_key *key;
  802. priv = kzalloc_obj(struct subsys_private);
  803. if (!priv)
  804. return -ENOMEM;
  805. priv->bus = bus;
  806. BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
  807. bus_kobj = &priv->subsys.kobj;
  808. retval = kobject_set_name(bus_kobj, "%s", bus->name);
  809. if (retval)
  810. goto out;
  811. bus_kobj->kset = bus_kset;
  812. bus_kobj->ktype = &bus_ktype;
  813. priv->drivers_autoprobe = 1;
  814. retval = kset_register(&priv->subsys);
  815. if (retval)
  816. goto out;
  817. retval = bus_create_file(bus, &bus_attr_uevent);
  818. if (retval)
  819. goto bus_uevent_fail;
  820. priv->devices_kset = kset_create_and_add("devices", NULL, bus_kobj);
  821. if (!priv->devices_kset) {
  822. retval = -ENOMEM;
  823. goto bus_devices_fail;
  824. }
  825. priv->drivers_kset = kset_create_and_add("drivers", NULL, bus_kobj);
  826. if (!priv->drivers_kset) {
  827. retval = -ENOMEM;
  828. goto bus_drivers_fail;
  829. }
  830. INIT_LIST_HEAD(&priv->interfaces);
  831. key = &priv->lock_key;
  832. lockdep_register_key(key);
  833. __mutex_init(&priv->mutex, "subsys mutex", key);
  834. klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
  835. klist_init(&priv->klist_drivers, NULL, NULL);
  836. retval = add_probe_files(bus);
  837. if (retval)
  838. goto bus_probe_files_fail;
  839. retval = sysfs_create_groups(bus_kobj, bus->bus_groups);
  840. if (retval)
  841. goto bus_groups_fail;
  842. pr_debug("bus: '%s': registered\n", bus->name);
  843. return 0;
  844. bus_groups_fail:
  845. remove_probe_files(bus);
  846. bus_probe_files_fail:
  847. kset_unregister(priv->drivers_kset);
  848. bus_drivers_fail:
  849. kset_unregister(priv->devices_kset);
  850. bus_devices_fail:
  851. bus_remove_file(bus, &bus_attr_uevent);
  852. bus_uevent_fail:
  853. kset_unregister(&priv->subsys);
  854. /* Above kset_unregister() will kfree @priv */
  855. priv = NULL;
  856. out:
  857. kfree(priv);
  858. return retval;
  859. }
  860. EXPORT_SYMBOL_GPL(bus_register);
  861. /**
  862. * bus_unregister - remove a bus from the system
  863. * @bus: bus.
  864. *
  865. * Unregister the child subsystems and the bus itself.
  866. * Finally, we call bus_put() to release the refcount
  867. */
  868. void bus_unregister(const struct bus_type *bus)
  869. {
  870. struct subsys_private *sp = bus_to_subsys(bus);
  871. struct kobject *bus_kobj;
  872. if (!sp)
  873. return;
  874. pr_debug("bus: '%s': unregistering\n", bus->name);
  875. if (sp->dev_root)
  876. device_unregister(sp->dev_root);
  877. bus_kobj = &sp->subsys.kobj;
  878. sysfs_remove_groups(bus_kobj, bus->bus_groups);
  879. remove_probe_files(bus);
  880. bus_remove_file(bus, &bus_attr_uevent);
  881. kset_unregister(sp->drivers_kset);
  882. kset_unregister(sp->devices_kset);
  883. kset_unregister(&sp->subsys);
  884. subsys_put(sp);
  885. }
  886. EXPORT_SYMBOL_GPL(bus_unregister);
  887. int bus_register_notifier(const struct bus_type *bus, struct notifier_block *nb)
  888. {
  889. struct subsys_private *sp = bus_to_subsys(bus);
  890. int retval;
  891. if (!sp)
  892. return -EINVAL;
  893. retval = blocking_notifier_chain_register(&sp->bus_notifier, nb);
  894. subsys_put(sp);
  895. return retval;
  896. }
  897. EXPORT_SYMBOL_GPL(bus_register_notifier);
  898. int bus_unregister_notifier(const struct bus_type *bus, struct notifier_block *nb)
  899. {
  900. struct subsys_private *sp = bus_to_subsys(bus);
  901. int retval;
  902. if (!sp)
  903. return -EINVAL;
  904. retval = blocking_notifier_chain_unregister(&sp->bus_notifier, nb);
  905. subsys_put(sp);
  906. return retval;
  907. }
  908. EXPORT_SYMBOL_GPL(bus_unregister_notifier);
  909. void bus_notify(struct device *dev, enum bus_notifier_event value)
  910. {
  911. struct subsys_private *sp = bus_to_subsys(dev->bus);
  912. if (!sp)
  913. return;
  914. blocking_notifier_call_chain(&sp->bus_notifier, value, dev);
  915. subsys_put(sp);
  916. }
  917. struct kset *bus_get_kset(const struct bus_type *bus)
  918. {
  919. struct subsys_private *sp = bus_to_subsys(bus);
  920. struct kset *kset;
  921. if (!sp)
  922. return NULL;
  923. kset = &sp->subsys;
  924. subsys_put(sp);
  925. return kset;
  926. }
  927. EXPORT_SYMBOL_GPL(bus_get_kset);
  928. /*
  929. * Yes, this forcibly breaks the klist abstraction temporarily. It
  930. * just wants to sort the klist, not change reference counts and
  931. * take/drop locks rapidly in the process. It does all this while
  932. * holding the lock for the list, so objects can't otherwise be
  933. * added/removed while we're swizzling.
  934. */
  935. static void device_insertion_sort_klist(struct device *a, struct list_head *list,
  936. int (*compare)(const struct device *a,
  937. const struct device *b))
  938. {
  939. struct klist_node *n;
  940. struct device_private *dev_prv;
  941. struct device *b;
  942. list_for_each_entry(n, list, n_node) {
  943. dev_prv = to_device_private_bus(n);
  944. b = dev_prv->device;
  945. if (compare(a, b) <= 0) {
  946. list_move_tail(&a->p->knode_bus.n_node,
  947. &b->p->knode_bus.n_node);
  948. return;
  949. }
  950. }
  951. list_move_tail(&a->p->knode_bus.n_node, list);
  952. }
  953. void bus_sort_breadthfirst(const struct bus_type *bus,
  954. int (*compare)(const struct device *a,
  955. const struct device *b))
  956. {
  957. struct subsys_private *sp = bus_to_subsys(bus);
  958. LIST_HEAD(sorted_devices);
  959. struct klist_node *n, *tmp;
  960. struct device_private *dev_prv;
  961. struct device *dev;
  962. struct klist *device_klist;
  963. if (!sp)
  964. return;
  965. device_klist = &sp->klist_devices;
  966. spin_lock(&device_klist->k_lock);
  967. list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) {
  968. dev_prv = to_device_private_bus(n);
  969. dev = dev_prv->device;
  970. device_insertion_sort_klist(dev, &sorted_devices, compare);
  971. }
  972. list_splice(&sorted_devices, &device_klist->k_list);
  973. spin_unlock(&device_klist->k_lock);
  974. subsys_put(sp);
  975. }
  976. EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
  977. struct subsys_dev_iter {
  978. struct klist_iter ki;
  979. const struct device_type *type;
  980. };
  981. /**
  982. * subsys_dev_iter_init - initialize subsys device iterator
  983. * @iter: subsys iterator to initialize
  984. * @sp: the subsys private (i.e. bus) we wanna iterate over
  985. * @start: the device to start iterating from, if any
  986. * @type: device_type of the devices to iterate over, NULL for all
  987. *
  988. * Initialize subsys iterator @iter such that it iterates over devices
  989. * of @subsys. If @start is set, the list iteration will start there,
  990. * otherwise if it is NULL, the iteration starts at the beginning of
  991. * the list.
  992. */
  993. static void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct subsys_private *sp,
  994. struct device *start, const struct device_type *type)
  995. {
  996. struct klist_node *start_knode = NULL;
  997. if (start)
  998. start_knode = &start->p->knode_bus;
  999. klist_iter_init_node(&sp->klist_devices, &iter->ki, start_knode);
  1000. iter->type = type;
  1001. }
  1002. /**
  1003. * subsys_dev_iter_next - iterate to the next device
  1004. * @iter: subsys iterator to proceed
  1005. *
  1006. * Proceed @iter to the next device and return it. Returns NULL if
  1007. * iteration is complete.
  1008. *
  1009. * The returned device is referenced and won't be released till
  1010. * iterator is proceed to the next device or exited. The caller is
  1011. * free to do whatever it wants to do with the device including
  1012. * calling back into subsys code.
  1013. */
  1014. static struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
  1015. {
  1016. struct klist_node *knode;
  1017. struct device *dev;
  1018. for (;;) {
  1019. knode = klist_next(&iter->ki);
  1020. if (!knode)
  1021. return NULL;
  1022. dev = to_device_private_bus(knode)->device;
  1023. if (!iter->type || iter->type == dev->type)
  1024. return dev;
  1025. }
  1026. }
  1027. /**
  1028. * subsys_dev_iter_exit - finish iteration
  1029. * @iter: subsys iterator to finish
  1030. *
  1031. * Finish an iteration. Always call this function after iteration is
  1032. * complete whether the iteration ran till the end or not.
  1033. */
  1034. static void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
  1035. {
  1036. klist_iter_exit(&iter->ki);
  1037. }
  1038. int subsys_interface_register(struct subsys_interface *sif)
  1039. {
  1040. struct subsys_private *sp;
  1041. struct subsys_dev_iter iter;
  1042. struct device *dev;
  1043. if (!sif || !sif->subsys)
  1044. return -ENODEV;
  1045. sp = bus_to_subsys(sif->subsys);
  1046. if (!sp)
  1047. return -EINVAL;
  1048. /*
  1049. * Reference in sp is now incremented and will be dropped when
  1050. * the interface is removed from the bus
  1051. */
  1052. mutex_lock(&sp->mutex);
  1053. list_add_tail(&sif->node, &sp->interfaces);
  1054. if (sif->add_dev) {
  1055. subsys_dev_iter_init(&iter, sp, NULL, NULL);
  1056. while ((dev = subsys_dev_iter_next(&iter)))
  1057. sif->add_dev(dev, sif);
  1058. subsys_dev_iter_exit(&iter);
  1059. }
  1060. mutex_unlock(&sp->mutex);
  1061. return 0;
  1062. }
  1063. EXPORT_SYMBOL_GPL(subsys_interface_register);
  1064. void subsys_interface_unregister(struct subsys_interface *sif)
  1065. {
  1066. struct subsys_private *sp;
  1067. struct subsys_dev_iter iter;
  1068. struct device *dev;
  1069. if (!sif || !sif->subsys)
  1070. return;
  1071. sp = bus_to_subsys(sif->subsys);
  1072. if (!sp)
  1073. return;
  1074. mutex_lock(&sp->mutex);
  1075. list_del_init(&sif->node);
  1076. if (sif->remove_dev) {
  1077. subsys_dev_iter_init(&iter, sp, NULL, NULL);
  1078. while ((dev = subsys_dev_iter_next(&iter)))
  1079. sif->remove_dev(dev, sif);
  1080. subsys_dev_iter_exit(&iter);
  1081. }
  1082. mutex_unlock(&sp->mutex);
  1083. /*
  1084. * Decrement the reference count twice, once for the bus_to_subsys()
  1085. * call in the start of this function, and the second one from the
  1086. * reference increment in subsys_interface_register()
  1087. */
  1088. subsys_put(sp);
  1089. subsys_put(sp);
  1090. }
  1091. EXPORT_SYMBOL_GPL(subsys_interface_unregister);
  1092. static void system_root_device_release(struct device *dev)
  1093. {
  1094. kfree(dev);
  1095. }
  1096. static int subsys_register(const struct bus_type *subsys,
  1097. const struct attribute_group **groups,
  1098. struct kobject *parent_of_root)
  1099. {
  1100. struct subsys_private *sp;
  1101. struct device *dev;
  1102. int err;
  1103. err = bus_register(subsys);
  1104. if (err < 0)
  1105. return err;
  1106. sp = bus_to_subsys(subsys);
  1107. if (!sp) {
  1108. err = -EINVAL;
  1109. goto err_sp;
  1110. }
  1111. dev = kzalloc_obj(struct device);
  1112. if (!dev) {
  1113. err = -ENOMEM;
  1114. goto err_dev;
  1115. }
  1116. err = dev_set_name(dev, "%s", subsys->name);
  1117. if (err < 0)
  1118. goto err_name;
  1119. dev->kobj.parent = parent_of_root;
  1120. dev->groups = groups;
  1121. dev->release = system_root_device_release;
  1122. err = device_register(dev);
  1123. if (err < 0)
  1124. goto err_dev_reg;
  1125. sp->dev_root = dev;
  1126. subsys_put(sp);
  1127. return 0;
  1128. err_dev_reg:
  1129. put_device(dev);
  1130. dev = NULL;
  1131. err_name:
  1132. kfree(dev);
  1133. err_dev:
  1134. subsys_put(sp);
  1135. err_sp:
  1136. bus_unregister(subsys);
  1137. return err;
  1138. }
  1139. /**
  1140. * subsys_system_register - register a subsystem at /sys/devices/system/
  1141. * @subsys: system subsystem
  1142. * @groups: default attributes for the root device
  1143. *
  1144. * All 'system' subsystems have a /sys/devices/system/<name> root device
  1145. * with the name of the subsystem. The root device can carry subsystem-
  1146. * wide attributes. All registered devices are below this single root
  1147. * device and are named after the subsystem with a simple enumeration
  1148. * number appended. The registered devices are not explicitly named;
  1149. * only 'id' in the device needs to be set.
  1150. *
  1151. * Do not use this interface for anything new, it exists for compatibility
  1152. * with bad ideas only. New subsystems should use plain subsystems; and
  1153. * add the subsystem-wide attributes should be added to the subsystem
  1154. * directory itself and not some create fake root-device placed in
  1155. * /sys/devices/system/<name>.
  1156. */
  1157. int subsys_system_register(const struct bus_type *subsys,
  1158. const struct attribute_group **groups)
  1159. {
  1160. return subsys_register(subsys, groups, &system_kset->kobj);
  1161. }
  1162. EXPORT_SYMBOL_GPL(subsys_system_register);
  1163. /**
  1164. * subsys_virtual_register - register a subsystem at /sys/devices/virtual/
  1165. * @subsys: virtual subsystem
  1166. * @groups: default attributes for the root device
  1167. *
  1168. * All 'virtual' subsystems have a /sys/devices/system/<name> root device
  1169. * with the name of the subsystem. The root device can carry subsystem-wide
  1170. * attributes. All registered devices are below this single root device.
  1171. * There's no restriction on device naming. This is for kernel software
  1172. * constructs which need sysfs interface.
  1173. */
  1174. int subsys_virtual_register(const struct bus_type *subsys,
  1175. const struct attribute_group **groups)
  1176. {
  1177. struct kobject *virtual_dir;
  1178. virtual_dir = virtual_device_parent();
  1179. if (!virtual_dir)
  1180. return -ENOMEM;
  1181. return subsys_register(subsys, groups, virtual_dir);
  1182. }
  1183. EXPORT_SYMBOL_GPL(subsys_virtual_register);
  1184. /**
  1185. * driver_find - locate driver on a bus by its name.
  1186. * @name: name of the driver.
  1187. * @bus: bus to scan for the driver.
  1188. *
  1189. * Call kset_find_obj() to iterate over list of drivers on
  1190. * a bus to find driver by name. Return driver if found.
  1191. *
  1192. * This routine provides no locking to prevent the driver it returns
  1193. * from being unregistered or unloaded while the caller is using it.
  1194. * The caller is responsible for preventing this.
  1195. */
  1196. struct device_driver *driver_find(const char *name, const struct bus_type *bus)
  1197. {
  1198. struct subsys_private *sp = bus_to_subsys(bus);
  1199. struct kobject *k;
  1200. struct driver_private *priv;
  1201. if (!sp)
  1202. return NULL;
  1203. k = kset_find_obj(sp->drivers_kset, name);
  1204. subsys_put(sp);
  1205. if (!k)
  1206. return NULL;
  1207. priv = to_driver(k);
  1208. /* Drop reference added by kset_find_obj() */
  1209. kobject_put(k);
  1210. return priv->driver;
  1211. }
  1212. EXPORT_SYMBOL_GPL(driver_find);
  1213. /*
  1214. * Warning, the value could go to "removed" instantly after calling this function, so be very
  1215. * careful when calling it...
  1216. */
  1217. bool bus_is_registered(const struct bus_type *bus)
  1218. {
  1219. struct subsys_private *sp = bus_to_subsys(bus);
  1220. bool is_initialized = false;
  1221. if (sp) {
  1222. is_initialized = true;
  1223. subsys_put(sp);
  1224. }
  1225. return is_initialized;
  1226. }
  1227. /**
  1228. * bus_get_dev_root - return a pointer to the "device root" of a bus
  1229. * @bus: bus to return the device root of.
  1230. *
  1231. * If a bus has a "device root" structure, return it, WITH THE REFERENCE
  1232. * COUNT INCREMENTED.
  1233. *
  1234. * Note, when finished with the device, a call to put_device() is required.
  1235. *
  1236. * If the device root is not present (or bus is not a valid pointer), NULL
  1237. * will be returned.
  1238. */
  1239. struct device *bus_get_dev_root(const struct bus_type *bus)
  1240. {
  1241. struct subsys_private *sp = bus_to_subsys(bus);
  1242. struct device *dev_root;
  1243. if (!sp)
  1244. return NULL;
  1245. dev_root = get_device(sp->dev_root);
  1246. subsys_put(sp);
  1247. return dev_root;
  1248. }
  1249. EXPORT_SYMBOL_GPL(bus_get_dev_root);
  1250. int __init buses_init(void)
  1251. {
  1252. bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
  1253. if (!bus_kset)
  1254. return -ENOMEM;
  1255. system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
  1256. if (!system_kset) {
  1257. /* Do error handling here as devices_init() do */
  1258. kset_unregister(bus_kset);
  1259. bus_kset = NULL;
  1260. pr_err("%s: failed to create and add kset 'bus'\n", __func__);
  1261. return -ENOMEM;
  1262. }
  1263. return 0;
  1264. }