bus.c 26 KB

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