bus.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /*
  2. * bus.c - bus driver management
  3. *
  4. * Copyright (c) 2002-3 Patrick Mochel
  5. * Copyright (c) 2002-3 Open Source Development Labs
  6. * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
  7. * Copyright (c) 2007 Novell Inc.
  8. *
  9. * This file is released under the GPLv2
  10. *
  11. */
  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 "base.h"
  19. #include "power/power.h"
  20. #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
  21. #define to_bus(obj) container_of(obj, struct bus_type_private, subsys.kobj)
  22. /*
  23. * sysfs bindings for drivers
  24. */
  25. #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
  26. static int __must_check bus_rescan_devices_helper(struct device *dev,
  27. void *data);
  28. static struct bus_type *bus_get(struct bus_type *bus)
  29. {
  30. if (bus) {
  31. kset_get(&bus->p->subsys);
  32. return bus;
  33. }
  34. return NULL;
  35. }
  36. static void bus_put(struct bus_type *bus)
  37. {
  38. if (bus)
  39. kset_put(&bus->p->subsys);
  40. }
  41. static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
  42. char *buf)
  43. {
  44. struct driver_attribute *drv_attr = to_drv_attr(attr);
  45. struct driver_private *drv_priv = to_driver(kobj);
  46. ssize_t ret = -EIO;
  47. if (drv_attr->show)
  48. ret = drv_attr->show(drv_priv->driver, buf);
  49. return ret;
  50. }
  51. static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
  52. const char *buf, size_t count)
  53. {
  54. struct driver_attribute *drv_attr = to_drv_attr(attr);
  55. struct driver_private *drv_priv = to_driver(kobj);
  56. ssize_t ret = -EIO;
  57. if (drv_attr->store)
  58. ret = drv_attr->store(drv_priv->driver, buf, count);
  59. return ret;
  60. }
  61. static const struct sysfs_ops driver_sysfs_ops = {
  62. .show = drv_attr_show,
  63. .store = drv_attr_store,
  64. };
  65. static void driver_release(struct kobject *kobj)
  66. {
  67. struct driver_private *drv_priv = to_driver(kobj);
  68. pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
  69. kfree(drv_priv);
  70. }
  71. static struct kobj_type driver_ktype = {
  72. .sysfs_ops = &driver_sysfs_ops,
  73. .release = driver_release,
  74. };
  75. /*
  76. * sysfs bindings for buses
  77. */
  78. static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
  79. char *buf)
  80. {
  81. struct bus_attribute *bus_attr = to_bus_attr(attr);
  82. struct bus_type_private *bus_priv = to_bus(kobj);
  83. ssize_t ret = 0;
  84. if (bus_attr->show)
  85. ret = bus_attr->show(bus_priv->bus, buf);
  86. return ret;
  87. }
  88. static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
  89. const char *buf, size_t count)
  90. {
  91. struct bus_attribute *bus_attr = to_bus_attr(attr);
  92. struct bus_type_private *bus_priv = to_bus(kobj);
  93. ssize_t ret = 0;
  94. if (bus_attr->store)
  95. ret = bus_attr->store(bus_priv->bus, buf, count);
  96. return ret;
  97. }
  98. static const struct sysfs_ops bus_sysfs_ops = {
  99. .show = bus_attr_show,
  100. .store = bus_attr_store,
  101. };
  102. int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)
  103. {
  104. int error;
  105. if (bus_get(bus)) {
  106. error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
  107. bus_put(bus);
  108. } else
  109. error = -EINVAL;
  110. return error;
  111. }
  112. EXPORT_SYMBOL_GPL(bus_create_file);
  113. void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)
  114. {
  115. if (bus_get(bus)) {
  116. sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
  117. bus_put(bus);
  118. }
  119. }
  120. EXPORT_SYMBOL_GPL(bus_remove_file);
  121. static struct kobj_type bus_ktype = {
  122. .sysfs_ops = &bus_sysfs_ops,
  123. };
  124. static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
  125. {
  126. struct kobj_type *ktype = get_ktype(kobj);
  127. if (ktype == &bus_ktype)
  128. return 1;
  129. return 0;
  130. }
  131. static const struct kset_uevent_ops bus_uevent_ops = {
  132. .filter = bus_uevent_filter,
  133. };
  134. static struct kset *bus_kset;
  135. #ifdef CONFIG_HOTPLUG
  136. /* Manually detach a device from its associated driver. */
  137. static ssize_t driver_unbind(struct device_driver *drv,
  138. const char *buf, size_t count)
  139. {
  140. struct bus_type *bus = bus_get(drv->bus);
  141. struct device *dev;
  142. int err = -ENODEV;
  143. dev = bus_find_device_by_name(bus, NULL, buf);
  144. if (dev && dev->driver == drv) {
  145. if (dev->parent) /* Needed for USB */
  146. device_lock(dev->parent);
  147. device_release_driver(dev);
  148. if (dev->parent)
  149. device_unlock(dev->parent);
  150. err = count;
  151. }
  152. put_device(dev);
  153. bus_put(bus);
  154. return err;
  155. }
  156. static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
  157. /*
  158. * Manually attach a device to a driver.
  159. * Note: the driver must want to bind to the device,
  160. * it is not possible to override the driver's id table.
  161. */
  162. static ssize_t driver_bind(struct device_driver *drv,
  163. const char *buf, size_t count)
  164. {
  165. struct bus_type *bus = bus_get(drv->bus);
  166. struct device *dev;
  167. int err = -ENODEV;
  168. dev = bus_find_device_by_name(bus, NULL, buf);
  169. if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
  170. if (dev->parent) /* Needed for USB */
  171. device_lock(dev->parent);
  172. device_lock(dev);
  173. err = driver_probe_device(drv, dev);
  174. device_unlock(dev);
  175. if (dev->parent)
  176. device_unlock(dev->parent);
  177. if (err > 0) {
  178. /* success */
  179. err = count;
  180. } else if (err == 0) {
  181. /* driver didn't accept device */
  182. err = -ENODEV;
  183. }
  184. }
  185. put_device(dev);
  186. bus_put(bus);
  187. return err;
  188. }
  189. static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
  190. static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
  191. {
  192. return sprintf(buf, "%d\n", bus->p->drivers_autoprobe);
  193. }
  194. static ssize_t store_drivers_autoprobe(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 store_drivers_probe(struct bus_type *bus,
  204. const char *buf, size_t count)
  205. {
  206. struct device *dev;
  207. dev = bus_find_device_by_name(bus, NULL, buf);
  208. if (!dev)
  209. return -ENODEV;
  210. if (bus_rescan_devices_helper(dev, NULL) != 0)
  211. return -EINVAL;
  212. return count;
  213. }
  214. #endif
  215. static struct device *next_device(struct klist_iter *i)
  216. {
  217. struct klist_node *n = klist_next(i);
  218. struct device *dev = NULL;
  219. struct device_private *dev_prv;
  220. if (n) {
  221. dev_prv = to_device_private_bus(n);
  222. dev = dev_prv->device;
  223. }
  224. return dev;
  225. }
  226. /**
  227. * bus_for_each_dev - device iterator.
  228. * @bus: bus type.
  229. * @start: device to start iterating from.
  230. * @data: data for the callback.
  231. * @fn: function to be called for each device.
  232. *
  233. * Iterate over @bus's list of devices, and call @fn for each,
  234. * passing it @data. If @start is not NULL, we use that device to
  235. * begin iterating from.
  236. *
  237. * We check the return of @fn each time. If it returns anything
  238. * other than 0, we break out and return that value.
  239. *
  240. * NOTE: The device that returns a non-zero value is not retained
  241. * in any way, nor is its refcount incremented. If the caller needs
  242. * to retain this data, it should do so, and increment the reference
  243. * count in the supplied callback.
  244. */
  245. int bus_for_each_dev(struct bus_type *bus, struct device *start,
  246. void *data, int (*fn)(struct device *, void *))
  247. {
  248. struct klist_iter i;
  249. struct device *dev;
  250. int error = 0;
  251. if (!bus)
  252. return -EINVAL;
  253. klist_iter_init_node(&bus->p->klist_devices, &i,
  254. (start ? &start->p->knode_bus : NULL));
  255. while ((dev = next_device(&i)) && !error)
  256. error = fn(dev, data);
  257. klist_iter_exit(&i);
  258. return error;
  259. }
  260. EXPORT_SYMBOL_GPL(bus_for_each_dev);
  261. /**
  262. * bus_find_device - device iterator for locating a particular device.
  263. * @bus: bus type
  264. * @start: Device to begin with
  265. * @data: Data to pass to match function
  266. * @match: Callback function to check device
  267. *
  268. * This is similar to the bus_for_each_dev() function above, but it
  269. * returns a reference to a device that is 'found' for later use, as
  270. * determined by the @match callback.
  271. *
  272. * The callback should return 0 if the device doesn't match and non-zero
  273. * if it does. If the callback returns non-zero, this function will
  274. * return to the caller and not iterate over any more devices.
  275. */
  276. struct device *bus_find_device(struct bus_type *bus,
  277. struct device *start, void *data,
  278. int (*match)(struct device *dev, void *data))
  279. {
  280. struct klist_iter i;
  281. struct device *dev;
  282. if (!bus)
  283. return NULL;
  284. klist_iter_init_node(&bus->p->klist_devices, &i,
  285. (start ? &start->p->knode_bus : NULL));
  286. while ((dev = next_device(&i)))
  287. if (match(dev, data) && get_device(dev))
  288. break;
  289. klist_iter_exit(&i);
  290. return dev;
  291. }
  292. EXPORT_SYMBOL_GPL(bus_find_device);
  293. static int match_name(struct device *dev, void *data)
  294. {
  295. const char *name = data;
  296. return sysfs_streq(name, dev_name(dev));
  297. }
  298. /**
  299. * bus_find_device_by_name - device iterator for locating a particular device of a specific name
  300. * @bus: bus type
  301. * @start: Device to begin with
  302. * @name: name of the device to match
  303. *
  304. * This is similar to the bus_find_device() function above, but it handles
  305. * searching by a name automatically, no need to write another strcmp matching
  306. * function.
  307. */
  308. struct device *bus_find_device_by_name(struct bus_type *bus,
  309. struct device *start, const char *name)
  310. {
  311. return bus_find_device(bus, start, (void *)name, match_name);
  312. }
  313. EXPORT_SYMBOL_GPL(bus_find_device_by_name);
  314. static struct device_driver *next_driver(struct klist_iter *i)
  315. {
  316. struct klist_node *n = klist_next(i);
  317. struct driver_private *drv_priv;
  318. if (n) {
  319. drv_priv = container_of(n, struct driver_private, knode_bus);
  320. return drv_priv->driver;
  321. }
  322. return NULL;
  323. }
  324. /**
  325. * bus_for_each_drv - driver iterator
  326. * @bus: bus we're dealing with.
  327. * @start: driver to start iterating on.
  328. * @data: data to pass to the callback.
  329. * @fn: function to call for each driver.
  330. *
  331. * This is nearly identical to the device iterator above.
  332. * We iterate over each driver that belongs to @bus, and call
  333. * @fn for each. If @fn returns anything but 0, we break out
  334. * and return it. If @start is not NULL, we use it as the head
  335. * of the list.
  336. *
  337. * NOTE: we don't return the driver that returns a non-zero
  338. * value, nor do we leave the reference count incremented for that
  339. * driver. If the caller needs to know that info, it must set it
  340. * in the callback. It must also be sure to increment the refcount
  341. * so it doesn't disappear before returning to the caller.
  342. */
  343. int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
  344. void *data, int (*fn)(struct device_driver *, void *))
  345. {
  346. struct klist_iter i;
  347. struct device_driver *drv;
  348. int error = 0;
  349. if (!bus)
  350. return -EINVAL;
  351. klist_iter_init_node(&bus->p->klist_drivers, &i,
  352. start ? &start->p->knode_bus : NULL);
  353. while ((drv = next_driver(&i)) && !error)
  354. error = fn(drv, data);
  355. klist_iter_exit(&i);
  356. return error;
  357. }
  358. EXPORT_SYMBOL_GPL(bus_for_each_drv);
  359. static int device_add_attrs(struct bus_type *bus, struct device *dev)
  360. {
  361. int error = 0;
  362. int i;
  363. if (!bus->dev_attrs)
  364. return 0;
  365. for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
  366. error = device_create_file(dev, &bus->dev_attrs[i]);
  367. if (error) {
  368. while (--i >= 0)
  369. device_remove_file(dev, &bus->dev_attrs[i]);
  370. break;
  371. }
  372. }
  373. return error;
  374. }
  375. static void device_remove_attrs(struct bus_type *bus, struct device *dev)
  376. {
  377. int i;
  378. if (bus->dev_attrs) {
  379. for (i = 0; attr_name(bus->dev_attrs[i]); i++)
  380. device_remove_file(dev, &bus->dev_attrs[i]);
  381. }
  382. }
  383. #ifdef CONFIG_SYSFS_DEPRECATED
  384. static int make_deprecated_bus_links(struct device *dev)
  385. {
  386. return sysfs_create_link(&dev->kobj,
  387. &dev->bus->p->subsys.kobj, "bus");
  388. }
  389. static void remove_deprecated_bus_links(struct device *dev)
  390. {
  391. sysfs_remove_link(&dev->kobj, "bus");
  392. }
  393. #else
  394. static inline int make_deprecated_bus_links(struct device *dev) { return 0; }
  395. static inline void remove_deprecated_bus_links(struct device *dev) { }
  396. #endif
  397. /**
  398. * bus_add_device - add device to bus
  399. * @dev: device being added
  400. *
  401. * - Add device's bus attributes.
  402. * - Create links to device's bus.
  403. * - Add the device to its bus's list of devices.
  404. */
  405. int bus_add_device(struct device *dev)
  406. {
  407. struct bus_type *bus = bus_get(dev->bus);
  408. int error = 0;
  409. if (bus) {
  410. pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
  411. error = device_add_attrs(bus, dev);
  412. if (error)
  413. goto out_put;
  414. error = sysfs_create_link(&bus->p->devices_kset->kobj,
  415. &dev->kobj, dev_name(dev));
  416. if (error)
  417. goto out_id;
  418. error = sysfs_create_link(&dev->kobj,
  419. &dev->bus->p->subsys.kobj, "subsystem");
  420. if (error)
  421. goto out_subsys;
  422. error = make_deprecated_bus_links(dev);
  423. if (error)
  424. goto out_deprecated;
  425. klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
  426. }
  427. return 0;
  428. out_deprecated:
  429. sysfs_remove_link(&dev->kobj, "subsystem");
  430. out_subsys:
  431. sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
  432. out_id:
  433. device_remove_attrs(bus, dev);
  434. out_put:
  435. bus_put(dev->bus);
  436. return error;
  437. }
  438. /**
  439. * bus_probe_device - probe drivers for a new device
  440. * @dev: device to probe
  441. *
  442. * - Automatically probe for a driver if the bus allows it.
  443. */
  444. void bus_probe_device(struct device *dev)
  445. {
  446. struct bus_type *bus = dev->bus;
  447. int ret;
  448. if (bus && bus->p->drivers_autoprobe) {
  449. ret = device_attach(dev);
  450. WARN_ON(ret < 0);
  451. }
  452. }
  453. /**
  454. * bus_remove_device - remove device from bus
  455. * @dev: device to be removed
  456. *
  457. * - Remove symlink from bus's directory.
  458. * - Delete device from bus's list.
  459. * - Detach from its driver.
  460. * - Drop reference taken in bus_add_device().
  461. */
  462. void bus_remove_device(struct device *dev)
  463. {
  464. if (dev->bus) {
  465. sysfs_remove_link(&dev->kobj, "subsystem");
  466. remove_deprecated_bus_links(dev);
  467. sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
  468. dev_name(dev));
  469. device_remove_attrs(dev->bus, dev);
  470. if (klist_node_attached(&dev->p->knode_bus))
  471. klist_del(&dev->p->knode_bus);
  472. pr_debug("bus: '%s': remove device %s\n",
  473. dev->bus->name, dev_name(dev));
  474. device_release_driver(dev);
  475. bus_put(dev->bus);
  476. }
  477. }
  478. static int driver_add_attrs(struct bus_type *bus, struct device_driver *drv)
  479. {
  480. int error = 0;
  481. int i;
  482. if (bus->drv_attrs) {
  483. for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
  484. error = driver_create_file(drv, &bus->drv_attrs[i]);
  485. if (error)
  486. goto err;
  487. }
  488. }
  489. done:
  490. return error;
  491. err:
  492. while (--i >= 0)
  493. driver_remove_file(drv, &bus->drv_attrs[i]);
  494. goto done;
  495. }
  496. static void driver_remove_attrs(struct bus_type *bus,
  497. struct device_driver *drv)
  498. {
  499. int i;
  500. if (bus->drv_attrs) {
  501. for (i = 0; attr_name(bus->drv_attrs[i]); i++)
  502. driver_remove_file(drv, &bus->drv_attrs[i]);
  503. }
  504. }
  505. #ifdef CONFIG_HOTPLUG
  506. /*
  507. * Thanks to drivers making their tables __devinit, we can't allow manual
  508. * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
  509. */
  510. static int __must_check add_bind_files(struct device_driver *drv)
  511. {
  512. int ret;
  513. ret = driver_create_file(drv, &driver_attr_unbind);
  514. if (ret == 0) {
  515. ret = driver_create_file(drv, &driver_attr_bind);
  516. if (ret)
  517. driver_remove_file(drv, &driver_attr_unbind);
  518. }
  519. return ret;
  520. }
  521. static void remove_bind_files(struct device_driver *drv)
  522. {
  523. driver_remove_file(drv, &driver_attr_bind);
  524. driver_remove_file(drv, &driver_attr_unbind);
  525. }
  526. static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
  527. static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
  528. show_drivers_autoprobe, store_drivers_autoprobe);
  529. static int add_probe_files(struct bus_type *bus)
  530. {
  531. int retval;
  532. retval = bus_create_file(bus, &bus_attr_drivers_probe);
  533. if (retval)
  534. goto out;
  535. retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
  536. if (retval)
  537. bus_remove_file(bus, &bus_attr_drivers_probe);
  538. out:
  539. return retval;
  540. }
  541. static void remove_probe_files(struct bus_type *bus)
  542. {
  543. bus_remove_file(bus, &bus_attr_drivers_autoprobe);
  544. bus_remove_file(bus, &bus_attr_drivers_probe);
  545. }
  546. #else
  547. static inline int add_bind_files(struct device_driver *drv) { return 0; }
  548. static inline void remove_bind_files(struct device_driver *drv) {}
  549. static inline int add_probe_files(struct bus_type *bus) { return 0; }
  550. static inline void remove_probe_files(struct bus_type *bus) {}
  551. #endif
  552. static ssize_t driver_uevent_store(struct device_driver *drv,
  553. const char *buf, size_t count)
  554. {
  555. enum kobject_action action;
  556. if (kobject_action_type(buf, count, &action) == 0)
  557. kobject_uevent(&drv->p->kobj, action);
  558. return count;
  559. }
  560. static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store);
  561. /**
  562. * bus_add_driver - Add a driver to the bus.
  563. * @drv: driver.
  564. */
  565. int bus_add_driver(struct device_driver *drv)
  566. {
  567. struct bus_type *bus;
  568. struct driver_private *priv;
  569. int error = 0;
  570. bus = bus_get(drv->bus);
  571. if (!bus)
  572. return -EINVAL;
  573. pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
  574. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  575. if (!priv) {
  576. error = -ENOMEM;
  577. goto out_put_bus;
  578. }
  579. klist_init(&priv->klist_devices, NULL, NULL);
  580. priv->driver = drv;
  581. drv->p = priv;
  582. priv->kobj.kset = bus->p->drivers_kset;
  583. error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
  584. "%s", drv->name);
  585. if (error)
  586. goto out_unregister;
  587. if (drv->bus->p->drivers_autoprobe) {
  588. error = driver_attach(drv);
  589. if (error)
  590. goto out_unregister;
  591. }
  592. klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
  593. module_add_driver(drv->owner, drv);
  594. error = driver_create_file(drv, &driver_attr_uevent);
  595. if (error) {
  596. printk(KERN_ERR "%s: uevent attr (%s) failed\n",
  597. __func__, drv->name);
  598. }
  599. error = driver_add_attrs(bus, drv);
  600. if (error) {
  601. /* How the hell do we get out of this pickle? Give up */
  602. printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
  603. __func__, drv->name);
  604. }
  605. if (!drv->suppress_bind_attrs) {
  606. error = add_bind_files(drv);
  607. if (error) {
  608. /* Ditto */
  609. printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
  610. __func__, drv->name);
  611. }
  612. }
  613. kobject_uevent(&priv->kobj, KOBJ_ADD);
  614. return 0;
  615. out_unregister:
  616. kobject_put(&priv->kobj);
  617. kfree(drv->p);
  618. drv->p = NULL;
  619. out_put_bus:
  620. bus_put(bus);
  621. return error;
  622. }
  623. /**
  624. * bus_remove_driver - delete driver from bus's knowledge.
  625. * @drv: driver.
  626. *
  627. * Detach the driver from the devices it controls, and remove
  628. * it from its bus's list of drivers. Finally, we drop the reference
  629. * to the bus we took in bus_add_driver().
  630. */
  631. void bus_remove_driver(struct device_driver *drv)
  632. {
  633. if (!drv->bus)
  634. return;
  635. if (!drv->suppress_bind_attrs)
  636. remove_bind_files(drv);
  637. driver_remove_attrs(drv->bus, drv);
  638. driver_remove_file(drv, &driver_attr_uevent);
  639. klist_remove(&drv->p->knode_bus);
  640. pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
  641. driver_detach(drv);
  642. module_remove_driver(drv);
  643. kobject_put(&drv->p->kobj);
  644. bus_put(drv->bus);
  645. }
  646. /* Helper for bus_rescan_devices's iter */
  647. static int __must_check bus_rescan_devices_helper(struct device *dev,
  648. void *data)
  649. {
  650. int ret = 0;
  651. if (!dev->driver) {
  652. if (dev->parent) /* Needed for USB */
  653. device_lock(dev->parent);
  654. ret = device_attach(dev);
  655. if (dev->parent)
  656. device_unlock(dev->parent);
  657. }
  658. return ret < 0 ? ret : 0;
  659. }
  660. /**
  661. * bus_rescan_devices - rescan devices on the bus for possible drivers
  662. * @bus: the bus to scan.
  663. *
  664. * This function will look for devices on the bus with no driver
  665. * attached and rescan it against existing drivers to see if it matches
  666. * any by calling device_attach() for the unbound devices.
  667. */
  668. int bus_rescan_devices(struct bus_type *bus)
  669. {
  670. return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
  671. }
  672. EXPORT_SYMBOL_GPL(bus_rescan_devices);
  673. /**
  674. * device_reprobe - remove driver for a device and probe for a new driver
  675. * @dev: the device to reprobe
  676. *
  677. * This function detaches the attached driver (if any) for the given
  678. * device and restarts the driver probing process. It is intended
  679. * to use if probing criteria changed during a devices lifetime and
  680. * driver attachment should change accordingly.
  681. */
  682. int device_reprobe(struct device *dev)
  683. {
  684. if (dev->driver) {
  685. if (dev->parent) /* Needed for USB */
  686. device_lock(dev->parent);
  687. device_release_driver(dev);
  688. if (dev->parent)
  689. device_unlock(dev->parent);
  690. }
  691. return bus_rescan_devices_helper(dev, NULL);
  692. }
  693. EXPORT_SYMBOL_GPL(device_reprobe);
  694. /**
  695. * find_bus - locate bus by name.
  696. * @name: name of bus.
  697. *
  698. * Call kset_find_obj() to iterate over list of buses to
  699. * find a bus by name. Return bus if found.
  700. *
  701. * Note that kset_find_obj increments bus' reference count.
  702. */
  703. #if 0
  704. struct bus_type *find_bus(char *name)
  705. {
  706. struct kobject *k = kset_find_obj(bus_kset, name);
  707. return k ? to_bus(k) : NULL;
  708. }
  709. #endif /* 0 */
  710. /**
  711. * bus_add_attrs - Add default attributes for this bus.
  712. * @bus: Bus that has just been registered.
  713. */
  714. static int bus_add_attrs(struct bus_type *bus)
  715. {
  716. int error = 0;
  717. int i;
  718. if (bus->bus_attrs) {
  719. for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
  720. error = bus_create_file(bus, &bus->bus_attrs[i]);
  721. if (error)
  722. goto err;
  723. }
  724. }
  725. done:
  726. return error;
  727. err:
  728. while (--i >= 0)
  729. bus_remove_file(bus, &bus->bus_attrs[i]);
  730. goto done;
  731. }
  732. static void bus_remove_attrs(struct bus_type *bus)
  733. {
  734. int i;
  735. if (bus->bus_attrs) {
  736. for (i = 0; attr_name(bus->bus_attrs[i]); i++)
  737. bus_remove_file(bus, &bus->bus_attrs[i]);
  738. }
  739. }
  740. static void klist_devices_get(struct klist_node *n)
  741. {
  742. struct device_private *dev_prv = to_device_private_bus(n);
  743. struct device *dev = dev_prv->device;
  744. get_device(dev);
  745. }
  746. static void klist_devices_put(struct klist_node *n)
  747. {
  748. struct device_private *dev_prv = to_device_private_bus(n);
  749. struct device *dev = dev_prv->device;
  750. put_device(dev);
  751. }
  752. static ssize_t bus_uevent_store(struct bus_type *bus,
  753. const char *buf, size_t count)
  754. {
  755. enum kobject_action action;
  756. if (kobject_action_type(buf, count, &action) == 0)
  757. kobject_uevent(&bus->p->subsys.kobj, action);
  758. return count;
  759. }
  760. static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
  761. /**
  762. * bus_register - register a bus with the system.
  763. * @bus: bus.
  764. *
  765. * Once we have that, we registered the bus with the kobject
  766. * infrastructure, then register the children subsystems it has:
  767. * the devices and drivers that belong to the bus.
  768. */
  769. int bus_register(struct bus_type *bus)
  770. {
  771. int retval;
  772. struct bus_type_private *priv;
  773. priv = kzalloc(sizeof(struct bus_type_private), GFP_KERNEL);
  774. if (!priv)
  775. return -ENOMEM;
  776. priv->bus = bus;
  777. bus->p = priv;
  778. BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
  779. retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
  780. if (retval)
  781. goto out;
  782. priv->subsys.kobj.kset = bus_kset;
  783. priv->subsys.kobj.ktype = &bus_ktype;
  784. priv->drivers_autoprobe = 1;
  785. retval = kset_register(&priv->subsys);
  786. if (retval)
  787. goto out;
  788. retval = bus_create_file(bus, &bus_attr_uevent);
  789. if (retval)
  790. goto bus_uevent_fail;
  791. priv->devices_kset = kset_create_and_add("devices", NULL,
  792. &priv->subsys.kobj);
  793. if (!priv->devices_kset) {
  794. retval = -ENOMEM;
  795. goto bus_devices_fail;
  796. }
  797. priv->drivers_kset = kset_create_and_add("drivers", NULL,
  798. &priv->subsys.kobj);
  799. if (!priv->drivers_kset) {
  800. retval = -ENOMEM;
  801. goto bus_drivers_fail;
  802. }
  803. klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
  804. klist_init(&priv->klist_drivers, NULL, NULL);
  805. retval = add_probe_files(bus);
  806. if (retval)
  807. goto bus_probe_files_fail;
  808. retval = bus_add_attrs(bus);
  809. if (retval)
  810. goto bus_attrs_fail;
  811. pr_debug("bus: '%s': registered\n", bus->name);
  812. return 0;
  813. bus_attrs_fail:
  814. remove_probe_files(bus);
  815. bus_probe_files_fail:
  816. kset_unregister(bus->p->drivers_kset);
  817. bus_drivers_fail:
  818. kset_unregister(bus->p->devices_kset);
  819. bus_devices_fail:
  820. bus_remove_file(bus, &bus_attr_uevent);
  821. bus_uevent_fail:
  822. kset_unregister(&bus->p->subsys);
  823. kfree(bus->p);
  824. out:
  825. bus->p = NULL;
  826. return retval;
  827. }
  828. EXPORT_SYMBOL_GPL(bus_register);
  829. /**
  830. * bus_unregister - remove a bus from the system
  831. * @bus: bus.
  832. *
  833. * Unregister the child subsystems and the bus itself.
  834. * Finally, we call bus_put() to release the refcount
  835. */
  836. void bus_unregister(struct bus_type *bus)
  837. {
  838. pr_debug("bus: '%s': unregistering\n", bus->name);
  839. bus_remove_attrs(bus);
  840. remove_probe_files(bus);
  841. kset_unregister(bus->p->drivers_kset);
  842. kset_unregister(bus->p->devices_kset);
  843. bus_remove_file(bus, &bus_attr_uevent);
  844. kset_unregister(&bus->p->subsys);
  845. kfree(bus->p);
  846. bus->p = NULL;
  847. }
  848. EXPORT_SYMBOL_GPL(bus_unregister);
  849. int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
  850. {
  851. return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
  852. }
  853. EXPORT_SYMBOL_GPL(bus_register_notifier);
  854. int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
  855. {
  856. return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
  857. }
  858. EXPORT_SYMBOL_GPL(bus_unregister_notifier);
  859. struct kset *bus_get_kset(struct bus_type *bus)
  860. {
  861. return &bus->p->subsys;
  862. }
  863. EXPORT_SYMBOL_GPL(bus_get_kset);
  864. struct klist *bus_get_device_klist(struct bus_type *bus)
  865. {
  866. return &bus->p->klist_devices;
  867. }
  868. EXPORT_SYMBOL_GPL(bus_get_device_klist);
  869. /*
  870. * Yes, this forcably breaks the klist abstraction temporarily. It
  871. * just wants to sort the klist, not change reference counts and
  872. * take/drop locks rapidly in the process. It does all this while
  873. * holding the lock for the list, so objects can't otherwise be
  874. * added/removed while we're swizzling.
  875. */
  876. static void device_insertion_sort_klist(struct device *a, struct list_head *list,
  877. int (*compare)(const struct device *a,
  878. const struct device *b))
  879. {
  880. struct list_head *pos;
  881. struct klist_node *n;
  882. struct device_private *dev_prv;
  883. struct device *b;
  884. list_for_each(pos, list) {
  885. n = container_of(pos, struct klist_node, n_node);
  886. dev_prv = to_device_private_bus(n);
  887. b = dev_prv->device;
  888. if (compare(a, b) <= 0) {
  889. list_move_tail(&a->p->knode_bus.n_node,
  890. &b->p->knode_bus.n_node);
  891. return;
  892. }
  893. }
  894. list_move_tail(&a->p->knode_bus.n_node, list);
  895. }
  896. void bus_sort_breadthfirst(struct bus_type *bus,
  897. int (*compare)(const struct device *a,
  898. const struct device *b))
  899. {
  900. LIST_HEAD(sorted_devices);
  901. struct list_head *pos, *tmp;
  902. struct klist_node *n;
  903. struct device_private *dev_prv;
  904. struct device *dev;
  905. struct klist *device_klist;
  906. device_klist = bus_get_device_klist(bus);
  907. spin_lock(&device_klist->k_lock);
  908. list_for_each_safe(pos, tmp, &device_klist->k_list) {
  909. n = container_of(pos, struct klist_node, n_node);
  910. dev_prv = to_device_private_bus(n);
  911. dev = dev_prv->device;
  912. device_insertion_sort_klist(dev, &sorted_devices, compare);
  913. }
  914. list_splice(&sorted_devices, &device_klist->k_list);
  915. spin_unlock(&device_klist->k_lock);
  916. }
  917. EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
  918. int __init buses_init(void)
  919. {
  920. bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
  921. if (!bus_kset)
  922. return -ENOMEM;
  923. return 0;
  924. }