bus.c 30 KB

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