bus.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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) {
  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. return n ? container_of(n, struct device, knode_bus) : NULL;
  218. }
  219. /**
  220. * bus_for_each_dev - device iterator.
  221. * @bus: bus type.
  222. * @start: device to start iterating from.
  223. * @data: data for the callback.
  224. * @fn: function to be called for each device.
  225. *
  226. * Iterate over @bus's list of devices, and call @fn for each,
  227. * passing it @data. If @start is not NULL, we use that device to
  228. * begin iterating from.
  229. *
  230. * We check the return of @fn each time. If it returns anything
  231. * other than 0, we break out and return that value.
  232. *
  233. * NOTE: The device that returns a non-zero value is not retained
  234. * in any way, nor is its refcount incremented. If the caller needs
  235. * to retain this data, it should do, and increment the reference
  236. * count in the supplied callback.
  237. */
  238. int bus_for_each_dev(struct bus_type *bus, struct device *start,
  239. void *data, int (*fn)(struct device *, void *))
  240. {
  241. struct klist_iter i;
  242. struct device *dev;
  243. int error = 0;
  244. if (!bus)
  245. return -EINVAL;
  246. klist_iter_init_node(&bus->p->klist_devices, &i,
  247. (start ? &start->knode_bus : NULL));
  248. while ((dev = next_device(&i)) && !error)
  249. error = fn(dev, data);
  250. klist_iter_exit(&i);
  251. return error;
  252. }
  253. EXPORT_SYMBOL_GPL(bus_for_each_dev);
  254. /**
  255. * bus_find_device - device iterator for locating a particular device.
  256. * @bus: bus type
  257. * @start: Device to begin with
  258. * @data: Data to pass to match function
  259. * @match: Callback function to check device
  260. *
  261. * This is similar to the bus_for_each_dev() function above, but it
  262. * returns a reference to a device that is 'found' for later use, as
  263. * determined by the @match callback.
  264. *
  265. * The callback should return 0 if the device doesn't match and non-zero
  266. * if it does. If the callback returns non-zero, this function will
  267. * return to the caller and not iterate over any more devices.
  268. */
  269. struct device *bus_find_device(struct bus_type *bus,
  270. struct device *start, void *data,
  271. int (*match)(struct device *dev, void *data))
  272. {
  273. struct klist_iter i;
  274. struct device *dev;
  275. if (!bus)
  276. return NULL;
  277. klist_iter_init_node(&bus->p->klist_devices, &i,
  278. (start ? &start->knode_bus : NULL));
  279. while ((dev = next_device(&i)))
  280. if (match(dev, data) && get_device(dev))
  281. break;
  282. klist_iter_exit(&i);
  283. return dev;
  284. }
  285. EXPORT_SYMBOL_GPL(bus_find_device);
  286. static int match_name(struct device *dev, void *data)
  287. {
  288. const char *name = data;
  289. if (strcmp(name, dev->bus_id) == 0)
  290. return 1;
  291. return 0;
  292. }
  293. /**
  294. * bus_find_device_by_name - device iterator for locating a particular device of a specific name
  295. * @bus: bus type
  296. * @start: Device to begin with
  297. * @name: name of the device to match
  298. *
  299. * This is similar to the bus_find_device() function above, but it handles
  300. * searching by a name automatically, no need to write another strcmp matching
  301. * function.
  302. */
  303. struct device *bus_find_device_by_name(struct bus_type *bus,
  304. struct device *start, const char *name)
  305. {
  306. return bus_find_device(bus, start, (void *)name, match_name);
  307. }
  308. EXPORT_SYMBOL_GPL(bus_find_device_by_name);
  309. static struct device_driver *next_driver(struct klist_iter *i)
  310. {
  311. struct klist_node *n = klist_next(i);
  312. struct driver_private *drv_priv;
  313. if (n) {
  314. drv_priv = container_of(n, struct driver_private, knode_bus);
  315. return drv_priv->driver;
  316. }
  317. return NULL;
  318. }
  319. /**
  320. * bus_for_each_drv - driver iterator
  321. * @bus: bus we're dealing with.
  322. * @start: driver to start iterating on.
  323. * @data: data to pass to the callback.
  324. * @fn: function to call for each driver.
  325. *
  326. * This is nearly identical to the device iterator above.
  327. * We iterate over each driver that belongs to @bus, and call
  328. * @fn for each. If @fn returns anything but 0, we break out
  329. * and return it. If @start is not NULL, we use it as the head
  330. * of the list.
  331. *
  332. * NOTE: we don't return the driver that returns a non-zero
  333. * value, nor do we leave the reference count incremented for that
  334. * driver. If the caller needs to know that info, it must set it
  335. * in the callback. It must also be sure to increment the refcount
  336. * so it doesn't disappear before returning to the caller.
  337. */
  338. int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
  339. void *data, int (*fn)(struct device_driver *, void *))
  340. {
  341. struct klist_iter i;
  342. struct device_driver *drv;
  343. int error = 0;
  344. if (!bus)
  345. return -EINVAL;
  346. klist_iter_init_node(&bus->p->klist_drivers, &i,
  347. start ? &start->p->knode_bus : NULL);
  348. while ((drv = next_driver(&i)) && !error)
  349. error = fn(drv, data);
  350. klist_iter_exit(&i);
  351. return error;
  352. }
  353. EXPORT_SYMBOL_GPL(bus_for_each_drv);
  354. static int device_add_attrs(struct bus_type *bus, struct device *dev)
  355. {
  356. int error = 0;
  357. int i;
  358. if (!bus->dev_attrs)
  359. return 0;
  360. for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
  361. error = device_create_file(dev, &bus->dev_attrs[i]);
  362. if (error) {
  363. while (--i >= 0)
  364. device_remove_file(dev, &bus->dev_attrs[i]);
  365. break;
  366. }
  367. }
  368. return error;
  369. }
  370. static void device_remove_attrs(struct bus_type *bus, struct device *dev)
  371. {
  372. int i;
  373. if (bus->dev_attrs) {
  374. for (i = 0; attr_name(bus->dev_attrs[i]); i++)
  375. device_remove_file(dev, &bus->dev_attrs[i]);
  376. }
  377. }
  378. #ifdef CONFIG_SYSFS_DEPRECATED
  379. static int make_deprecated_bus_links(struct device *dev)
  380. {
  381. return sysfs_create_link(&dev->kobj,
  382. &dev->bus->p->subsys.kobj, "bus");
  383. }
  384. static void remove_deprecated_bus_links(struct device *dev)
  385. {
  386. sysfs_remove_link(&dev->kobj, "bus");
  387. }
  388. #else
  389. static inline int make_deprecated_bus_links(struct device *dev) { return 0; }
  390. static inline void remove_deprecated_bus_links(struct device *dev) { }
  391. #endif
  392. /**
  393. * bus_add_device - add device to bus
  394. * @dev: device being added
  395. *
  396. * - Add the device to its bus's list of devices.
  397. * - Create link to device's bus.
  398. */
  399. int bus_add_device(struct device *dev)
  400. {
  401. struct bus_type *bus = bus_get(dev->bus);
  402. int error = 0;
  403. if (bus) {
  404. pr_debug("bus: '%s': add device %s\n", bus->name, dev->bus_id);
  405. error = device_add_attrs(bus, dev);
  406. if (error)
  407. goto out_put;
  408. error = sysfs_create_link(&bus->p->devices_kset->kobj,
  409. &dev->kobj, dev->bus_id);
  410. if (error)
  411. goto out_id;
  412. error = sysfs_create_link(&dev->kobj,
  413. &dev->bus->p->subsys.kobj, "subsystem");
  414. if (error)
  415. goto out_subsys;
  416. error = make_deprecated_bus_links(dev);
  417. if (error)
  418. goto out_deprecated;
  419. }
  420. return 0;
  421. out_deprecated:
  422. sysfs_remove_link(&dev->kobj, "subsystem");
  423. out_subsys:
  424. sysfs_remove_link(&bus->p->devices_kset->kobj, dev->bus_id);
  425. out_id:
  426. device_remove_attrs(bus, dev);
  427. out_put:
  428. bus_put(dev->bus);
  429. return error;
  430. }
  431. /**
  432. * bus_attach_device - add device to bus
  433. * @dev: device tried to attach to a driver
  434. *
  435. * - Add device to bus's list of devices.
  436. * - Try to attach to driver.
  437. */
  438. void bus_attach_device(struct device *dev)
  439. {
  440. struct bus_type *bus = dev->bus;
  441. int ret = 0;
  442. if (bus) {
  443. if (bus->p->drivers_autoprobe)
  444. ret = device_attach(dev);
  445. WARN_ON(ret < 0);
  446. if (ret >= 0)
  447. klist_add_tail(&dev->knode_bus, &bus->p->klist_devices);
  448. }
  449. }
  450. /**
  451. * bus_remove_device - remove device from bus
  452. * @dev: device to be removed
  453. *
  454. * - Remove symlink from bus's directory.
  455. * - Delete device from bus's list.
  456. * - Detach from its driver.
  457. * - Drop reference taken in bus_add_device().
  458. */
  459. void bus_remove_device(struct device *dev)
  460. {
  461. if (dev->bus) {
  462. sysfs_remove_link(&dev->kobj, "subsystem");
  463. remove_deprecated_bus_links(dev);
  464. sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
  465. dev->bus_id);
  466. device_remove_attrs(dev->bus, dev);
  467. if (klist_node_attached(&dev->knode_bus))
  468. klist_del(&dev->knode_bus);
  469. pr_debug("bus: '%s': remove device %s\n",
  470. dev->bus->name, dev->bus_id);
  471. device_release_driver(dev);
  472. bus_put(dev->bus);
  473. }
  474. }
  475. static int driver_add_attrs(struct bus_type *bus, struct device_driver *drv)
  476. {
  477. int error = 0;
  478. int i;
  479. if (bus->drv_attrs) {
  480. for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
  481. error = driver_create_file(drv, &bus->drv_attrs[i]);
  482. if (error)
  483. goto err;
  484. }
  485. }
  486. done:
  487. return error;
  488. err:
  489. while (--i >= 0)
  490. driver_remove_file(drv, &bus->drv_attrs[i]);
  491. goto done;
  492. }
  493. static void driver_remove_attrs(struct bus_type *bus,
  494. struct device_driver *drv)
  495. {
  496. int i;
  497. if (bus->drv_attrs) {
  498. for (i = 0; attr_name(bus->drv_attrs[i]); i++)
  499. driver_remove_file(drv, &bus->drv_attrs[i]);
  500. }
  501. }
  502. #ifdef CONFIG_HOTPLUG
  503. /*
  504. * Thanks to drivers making their tables __devinit, we can't allow manual
  505. * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
  506. */
  507. static int __must_check add_bind_files(struct device_driver *drv)
  508. {
  509. int ret;
  510. ret = driver_create_file(drv, &driver_attr_unbind);
  511. if (ret == 0) {
  512. ret = driver_create_file(drv, &driver_attr_bind);
  513. if (ret)
  514. driver_remove_file(drv, &driver_attr_unbind);
  515. }
  516. return ret;
  517. }
  518. static void remove_bind_files(struct device_driver *drv)
  519. {
  520. driver_remove_file(drv, &driver_attr_bind);
  521. driver_remove_file(drv, &driver_attr_unbind);
  522. }
  523. static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
  524. static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
  525. show_drivers_autoprobe, store_drivers_autoprobe);
  526. static int add_probe_files(struct bus_type *bus)
  527. {
  528. int retval;
  529. retval = bus_create_file(bus, &bus_attr_drivers_probe);
  530. if (retval)
  531. goto out;
  532. retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
  533. if (retval)
  534. bus_remove_file(bus, &bus_attr_drivers_probe);
  535. out:
  536. return retval;
  537. }
  538. static void remove_probe_files(struct bus_type *bus)
  539. {
  540. bus_remove_file(bus, &bus_attr_drivers_autoprobe);
  541. bus_remove_file(bus, &bus_attr_drivers_probe);
  542. }
  543. #else
  544. static inline int add_bind_files(struct device_driver *drv) { return 0; }
  545. static inline void remove_bind_files(struct device_driver *drv) {}
  546. static inline int add_probe_files(struct bus_type *bus) { return 0; }
  547. static inline void remove_probe_files(struct bus_type *bus) {}
  548. #endif
  549. static ssize_t driver_uevent_store(struct device_driver *drv,
  550. const char *buf, size_t count)
  551. {
  552. enum kobject_action action;
  553. if (kobject_action_type(buf, count, &action) == 0)
  554. kobject_uevent(&drv->p->kobj, action);
  555. return count;
  556. }
  557. static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store);
  558. /**
  559. * bus_add_driver - Add a driver to the bus.
  560. * @drv: driver.
  561. */
  562. int bus_add_driver(struct device_driver *drv)
  563. {
  564. struct bus_type *bus;
  565. struct driver_private *priv;
  566. int error = 0;
  567. bus = bus_get(drv->bus);
  568. if (!bus)
  569. return -EINVAL;
  570. pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
  571. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  572. if (!priv) {
  573. error = -ENOMEM;
  574. goto out_put_bus;
  575. }
  576. klist_init(&priv->klist_devices, NULL, NULL);
  577. priv->driver = drv;
  578. drv->p = priv;
  579. priv->kobj.kset = bus->p->drivers_kset;
  580. error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
  581. "%s", drv->name);
  582. if (error)
  583. goto out_unregister;
  584. if (drv->bus->p->drivers_autoprobe) {
  585. error = driver_attach(drv);
  586. if (error)
  587. goto out_unregister;
  588. }
  589. klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
  590. module_add_driver(drv->owner, drv);
  591. error = driver_create_file(drv, &driver_attr_uevent);
  592. if (error) {
  593. printk(KERN_ERR "%s: uevent attr (%s) failed\n",
  594. __func__, drv->name);
  595. }
  596. error = driver_add_attrs(bus, drv);
  597. if (error) {
  598. /* How the hell do we get out of this pickle? Give up */
  599. printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
  600. __func__, drv->name);
  601. }
  602. error = add_bind_files(drv);
  603. if (error) {
  604. /* Ditto */
  605. printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
  606. __func__, drv->name);
  607. }
  608. kobject_uevent(&priv->kobj, KOBJ_ADD);
  609. return error;
  610. out_unregister:
  611. kobject_put(&priv->kobj);
  612. out_put_bus:
  613. bus_put(bus);
  614. return error;
  615. }
  616. /**
  617. * bus_remove_driver - delete driver from bus's knowledge.
  618. * @drv: driver.
  619. *
  620. * Detach the driver from the devices it controls, and remove
  621. * it from its bus's list of drivers. Finally, we drop the reference
  622. * to the bus we took in bus_add_driver().
  623. */
  624. void bus_remove_driver(struct device_driver *drv)
  625. {
  626. if (!drv->bus)
  627. return;
  628. remove_bind_files(drv);
  629. driver_remove_attrs(drv->bus, drv);
  630. driver_remove_file(drv, &driver_attr_uevent);
  631. klist_remove(&drv->p->knode_bus);
  632. pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
  633. driver_detach(drv);
  634. module_remove_driver(drv);
  635. kobject_put(&drv->p->kobj);
  636. bus_put(drv->bus);
  637. }
  638. /* Helper for bus_rescan_devices's iter */
  639. static int __must_check bus_rescan_devices_helper(struct device *dev,
  640. void *data)
  641. {
  642. int ret = 0;
  643. if (!dev->driver) {
  644. if (dev->parent) /* Needed for USB */
  645. down(&dev->parent->sem);
  646. ret = device_attach(dev);
  647. if (dev->parent)
  648. up(&dev->parent->sem);
  649. }
  650. return ret < 0 ? ret : 0;
  651. }
  652. /**
  653. * bus_rescan_devices - rescan devices on the bus for possible drivers
  654. * @bus: the bus to scan.
  655. *
  656. * This function will look for devices on the bus with no driver
  657. * attached and rescan it against existing drivers to see if it matches
  658. * any by calling device_attach() for the unbound devices.
  659. */
  660. int bus_rescan_devices(struct bus_type *bus)
  661. {
  662. return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
  663. }
  664. EXPORT_SYMBOL_GPL(bus_rescan_devices);
  665. /**
  666. * device_reprobe - remove driver for a device and probe for a new driver
  667. * @dev: the device to reprobe
  668. *
  669. * This function detaches the attached driver (if any) for the given
  670. * device and restarts the driver probing process. It is intended
  671. * to use if probing criteria changed during a devices lifetime and
  672. * driver attachment should change accordingly.
  673. */
  674. int device_reprobe(struct device *dev)
  675. {
  676. if (dev->driver) {
  677. if (dev->parent) /* Needed for USB */
  678. down(&dev->parent->sem);
  679. device_release_driver(dev);
  680. if (dev->parent)
  681. up(&dev->parent->sem);
  682. }
  683. return bus_rescan_devices_helper(dev, NULL);
  684. }
  685. EXPORT_SYMBOL_GPL(device_reprobe);
  686. /**
  687. * find_bus - locate bus by name.
  688. * @name: name of bus.
  689. *
  690. * Call kset_find_obj() to iterate over list of buses to
  691. * find a bus by name. Return bus if found.
  692. *
  693. * Note that kset_find_obj increments bus' reference count.
  694. */
  695. #if 0
  696. struct bus_type *find_bus(char *name)
  697. {
  698. struct kobject *k = kset_find_obj(bus_kset, name);
  699. return k ? to_bus(k) : NULL;
  700. }
  701. #endif /* 0 */
  702. /**
  703. * bus_add_attrs - Add default attributes for this bus.
  704. * @bus: Bus that has just been registered.
  705. */
  706. static int bus_add_attrs(struct bus_type *bus)
  707. {
  708. int error = 0;
  709. int i;
  710. if (bus->bus_attrs) {
  711. for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
  712. error = bus_create_file(bus, &bus->bus_attrs[i]);
  713. if (error)
  714. goto err;
  715. }
  716. }
  717. done:
  718. return error;
  719. err:
  720. while (--i >= 0)
  721. bus_remove_file(bus, &bus->bus_attrs[i]);
  722. goto done;
  723. }
  724. static void bus_remove_attrs(struct bus_type *bus)
  725. {
  726. int i;
  727. if (bus->bus_attrs) {
  728. for (i = 0; attr_name(bus->bus_attrs[i]); i++)
  729. bus_remove_file(bus, &bus->bus_attrs[i]);
  730. }
  731. }
  732. static void klist_devices_get(struct klist_node *n)
  733. {
  734. struct device *dev = container_of(n, struct device, knode_bus);
  735. get_device(dev);
  736. }
  737. static void klist_devices_put(struct klist_node *n)
  738. {
  739. struct device *dev = container_of(n, struct device, knode_bus);
  740. put_device(dev);
  741. }
  742. static ssize_t bus_uevent_store(struct bus_type *bus,
  743. const char *buf, size_t count)
  744. {
  745. enum kobject_action action;
  746. if (kobject_action_type(buf, count, &action) == 0)
  747. kobject_uevent(&bus->p->subsys.kobj, action);
  748. return count;
  749. }
  750. static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
  751. /**
  752. * bus_register - register a bus with the system.
  753. * @bus: bus.
  754. *
  755. * Once we have that, we registered the bus with the kobject
  756. * infrastructure, then register the children subsystems it has:
  757. * the devices and drivers that belong to the bus.
  758. */
  759. int bus_register(struct bus_type *bus)
  760. {
  761. int retval;
  762. struct bus_type_private *priv;
  763. priv = kzalloc(sizeof(struct bus_type_private), GFP_KERNEL);
  764. if (!priv)
  765. return -ENOMEM;
  766. priv->bus = bus;
  767. bus->p = priv;
  768. BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
  769. retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
  770. if (retval)
  771. goto out;
  772. priv->subsys.kobj.kset = bus_kset;
  773. priv->subsys.kobj.ktype = &bus_ktype;
  774. priv->drivers_autoprobe = 1;
  775. retval = kset_register(&priv->subsys);
  776. if (retval)
  777. goto out;
  778. retval = bus_create_file(bus, &bus_attr_uevent);
  779. if (retval)
  780. goto bus_uevent_fail;
  781. priv->devices_kset = kset_create_and_add("devices", NULL,
  782. &priv->subsys.kobj);
  783. if (!priv->devices_kset) {
  784. retval = -ENOMEM;
  785. goto bus_devices_fail;
  786. }
  787. priv->drivers_kset = kset_create_and_add("drivers", NULL,
  788. &priv->subsys.kobj);
  789. if (!priv->drivers_kset) {
  790. retval = -ENOMEM;
  791. goto bus_drivers_fail;
  792. }
  793. klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
  794. klist_init(&priv->klist_drivers, NULL, NULL);
  795. retval = add_probe_files(bus);
  796. if (retval)
  797. goto bus_probe_files_fail;
  798. retval = bus_add_attrs(bus);
  799. if (retval)
  800. goto bus_attrs_fail;
  801. pr_debug("bus: '%s': registered\n", bus->name);
  802. return 0;
  803. bus_attrs_fail:
  804. remove_probe_files(bus);
  805. bus_probe_files_fail:
  806. kset_unregister(bus->p->drivers_kset);
  807. bus_drivers_fail:
  808. kset_unregister(bus->p->devices_kset);
  809. bus_devices_fail:
  810. bus_remove_file(bus, &bus_attr_uevent);
  811. bus_uevent_fail:
  812. kset_unregister(&bus->p->subsys);
  813. kfree(bus->p);
  814. out:
  815. return retval;
  816. }
  817. EXPORT_SYMBOL_GPL(bus_register);
  818. /**
  819. * bus_unregister - remove a bus from the system
  820. * @bus: bus.
  821. *
  822. * Unregister the child subsystems and the bus itself.
  823. * Finally, we call bus_put() to release the refcount
  824. */
  825. void bus_unregister(struct bus_type *bus)
  826. {
  827. pr_debug("bus: '%s': unregistering\n", bus->name);
  828. bus_remove_attrs(bus);
  829. remove_probe_files(bus);
  830. kset_unregister(bus->p->drivers_kset);
  831. kset_unregister(bus->p->devices_kset);
  832. bus_remove_file(bus, &bus_attr_uevent);
  833. kset_unregister(&bus->p->subsys);
  834. kfree(bus->p);
  835. }
  836. EXPORT_SYMBOL_GPL(bus_unregister);
  837. int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
  838. {
  839. return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
  840. }
  841. EXPORT_SYMBOL_GPL(bus_register_notifier);
  842. int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
  843. {
  844. return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
  845. }
  846. EXPORT_SYMBOL_GPL(bus_unregister_notifier);
  847. struct kset *bus_get_kset(struct bus_type *bus)
  848. {
  849. return &bus->p->subsys;
  850. }
  851. EXPORT_SYMBOL_GPL(bus_get_kset);
  852. struct klist *bus_get_device_klist(struct bus_type *bus)
  853. {
  854. return &bus->p->klist_devices;
  855. }
  856. EXPORT_SYMBOL_GPL(bus_get_device_klist);
  857. int __init buses_init(void)
  858. {
  859. bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
  860. if (!bus_kset)
  861. return -ENOMEM;
  862. return 0;
  863. }