bus.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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. *
  7. * This file is released under the GPLv2
  8. *
  9. */
  10. #include <linux/device.h>
  11. #include <linux/module.h>
  12. #include <linux/errno.h>
  13. #include <linux/init.h>
  14. #include <linux/string.h>
  15. #include "base.h"
  16. #include "power/power.h"
  17. #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
  18. #define to_bus(obj) container_of(obj, struct bus_type, subsys.kset.kobj)
  19. /*
  20. * sysfs bindings for drivers
  21. */
  22. #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
  23. #define to_driver(obj) container_of(obj, struct device_driver, kobj)
  24. static ssize_t
  25. drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
  26. {
  27. struct driver_attribute * drv_attr = to_drv_attr(attr);
  28. struct device_driver * drv = to_driver(kobj);
  29. ssize_t ret = -EIO;
  30. if (drv_attr->show)
  31. ret = drv_attr->show(drv, buf);
  32. return ret;
  33. }
  34. static ssize_t
  35. drv_attr_store(struct kobject * kobj, struct attribute * attr,
  36. const char * buf, size_t count)
  37. {
  38. struct driver_attribute * drv_attr = to_drv_attr(attr);
  39. struct device_driver * drv = to_driver(kobj);
  40. ssize_t ret = -EIO;
  41. if (drv_attr->store)
  42. ret = drv_attr->store(drv, buf, count);
  43. return ret;
  44. }
  45. static struct sysfs_ops driver_sysfs_ops = {
  46. .show = drv_attr_show,
  47. .store = drv_attr_store,
  48. };
  49. static void driver_release(struct kobject * kobj)
  50. {
  51. struct device_driver * drv = to_driver(kobj);
  52. complete(&drv->unloaded);
  53. }
  54. static struct kobj_type ktype_driver = {
  55. .sysfs_ops = &driver_sysfs_ops,
  56. .release = driver_release,
  57. };
  58. /*
  59. * sysfs bindings for buses
  60. */
  61. static ssize_t
  62. bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
  63. {
  64. struct bus_attribute * bus_attr = to_bus_attr(attr);
  65. struct bus_type * bus = to_bus(kobj);
  66. ssize_t ret = 0;
  67. if (bus_attr->show)
  68. ret = bus_attr->show(bus, buf);
  69. return ret;
  70. }
  71. static ssize_t
  72. bus_attr_store(struct kobject * kobj, struct attribute * attr,
  73. const char * buf, size_t count)
  74. {
  75. struct bus_attribute * bus_attr = to_bus_attr(attr);
  76. struct bus_type * bus = to_bus(kobj);
  77. ssize_t ret = 0;
  78. if (bus_attr->store)
  79. ret = bus_attr->store(bus, buf, count);
  80. return ret;
  81. }
  82. static struct sysfs_ops bus_sysfs_ops = {
  83. .show = bus_attr_show,
  84. .store = bus_attr_store,
  85. };
  86. int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
  87. {
  88. int error;
  89. if (get_bus(bus)) {
  90. error = sysfs_create_file(&bus->subsys.kset.kobj, &attr->attr);
  91. put_bus(bus);
  92. } else
  93. error = -EINVAL;
  94. return error;
  95. }
  96. void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
  97. {
  98. if (get_bus(bus)) {
  99. sysfs_remove_file(&bus->subsys.kset.kobj, &attr->attr);
  100. put_bus(bus);
  101. }
  102. }
  103. static struct kobj_type ktype_bus = {
  104. .sysfs_ops = &bus_sysfs_ops,
  105. };
  106. static decl_subsys(bus, &ktype_bus, NULL);
  107. #ifdef CONFIG_HOTPLUG
  108. /* Manually detach a device from its associated driver. */
  109. static int driver_helper(struct device *dev, void *data)
  110. {
  111. const char *name = data;
  112. if (strcmp(name, dev->bus_id) == 0)
  113. return 1;
  114. return 0;
  115. }
  116. static ssize_t driver_unbind(struct device_driver *drv,
  117. const char *buf, size_t count)
  118. {
  119. struct bus_type *bus = get_bus(drv->bus);
  120. struct device *dev;
  121. int err = -ENODEV;
  122. dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
  123. if (dev && dev->driver == drv) {
  124. if (dev->parent) /* Needed for USB */
  125. down(&dev->parent->sem);
  126. device_release_driver(dev);
  127. if (dev->parent)
  128. up(&dev->parent->sem);
  129. err = count;
  130. }
  131. put_device(dev);
  132. put_bus(bus);
  133. return err;
  134. }
  135. static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
  136. /*
  137. * Manually attach a device to a driver.
  138. * Note: the driver must want to bind to the device,
  139. * it is not possible to override the driver's id table.
  140. */
  141. static ssize_t driver_bind(struct device_driver *drv,
  142. const char *buf, size_t count)
  143. {
  144. struct bus_type *bus = get_bus(drv->bus);
  145. struct device *dev;
  146. int err = -ENODEV;
  147. dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
  148. if (dev && dev->driver == NULL) {
  149. if (dev->parent) /* Needed for USB */
  150. down(&dev->parent->sem);
  151. down(&dev->sem);
  152. err = driver_probe_device(drv, dev);
  153. up(&dev->sem);
  154. if (dev->parent)
  155. up(&dev->parent->sem);
  156. if (err > 0) /* success */
  157. err = count;
  158. else if (err == 0) /* driver didn't accept device */
  159. err = -ENODEV;
  160. }
  161. put_device(dev);
  162. put_bus(bus);
  163. return err;
  164. }
  165. static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
  166. #endif
  167. static struct device * next_device(struct klist_iter * i)
  168. {
  169. struct klist_node * n = klist_next(i);
  170. return n ? container_of(n, struct device, knode_bus) : NULL;
  171. }
  172. /**
  173. * bus_for_each_dev - device iterator.
  174. * @bus: bus type.
  175. * @start: device to start iterating from.
  176. * @data: data for the callback.
  177. * @fn: function to be called for each device.
  178. *
  179. * Iterate over @bus's list of devices, and call @fn for each,
  180. * passing it @data. If @start is not NULL, we use that device to
  181. * begin iterating from.
  182. *
  183. * We check the return of @fn each time. If it returns anything
  184. * other than 0, we break out and return that value.
  185. *
  186. * NOTE: The device that returns a non-zero value is not retained
  187. * in any way, nor is its refcount incremented. If the caller needs
  188. * to retain this data, it should do, and increment the reference
  189. * count in the supplied callback.
  190. */
  191. int bus_for_each_dev(struct bus_type * bus, struct device * start,
  192. void * data, int (*fn)(struct device *, void *))
  193. {
  194. struct klist_iter i;
  195. struct device * dev;
  196. int error = 0;
  197. if (!bus)
  198. return -EINVAL;
  199. klist_iter_init_node(&bus->klist_devices, &i,
  200. (start ? &start->knode_bus : NULL));
  201. while ((dev = next_device(&i)) && !error)
  202. error = fn(dev, data);
  203. klist_iter_exit(&i);
  204. return error;
  205. }
  206. /**
  207. * bus_find_device - device iterator for locating a particular device.
  208. * @bus: bus type
  209. * @start: Device to begin with
  210. * @data: Data to pass to match function
  211. * @match: Callback function to check device
  212. *
  213. * This is similar to the bus_for_each_dev() function above, but it
  214. * returns a reference to a device that is 'found' for later use, as
  215. * determined by the @match callback.
  216. *
  217. * The callback should return 0 if the device doesn't match and non-zero
  218. * if it does. If the callback returns non-zero, this function will
  219. * return to the caller and not iterate over any more devices.
  220. */
  221. struct device * bus_find_device(struct bus_type *bus,
  222. struct device *start, void *data,
  223. int (*match)(struct device *, void *))
  224. {
  225. struct klist_iter i;
  226. struct device *dev;
  227. if (!bus)
  228. return NULL;
  229. klist_iter_init_node(&bus->klist_devices, &i,
  230. (start ? &start->knode_bus : NULL));
  231. while ((dev = next_device(&i)))
  232. if (match(dev, data) && get_device(dev))
  233. break;
  234. klist_iter_exit(&i);
  235. return dev;
  236. }
  237. static struct device_driver * next_driver(struct klist_iter * i)
  238. {
  239. struct klist_node * n = klist_next(i);
  240. return n ? container_of(n, struct device_driver, knode_bus) : NULL;
  241. }
  242. /**
  243. * bus_for_each_drv - driver iterator
  244. * @bus: bus we're dealing with.
  245. * @start: driver to start iterating on.
  246. * @data: data to pass to the callback.
  247. * @fn: function to call for each driver.
  248. *
  249. * This is nearly identical to the device iterator above.
  250. * We iterate over each driver that belongs to @bus, and call
  251. * @fn for each. If @fn returns anything but 0, we break out
  252. * and return it. If @start is not NULL, we use it as the head
  253. * of the list.
  254. *
  255. * NOTE: we don't return the driver that returns a non-zero
  256. * value, nor do we leave the reference count incremented for that
  257. * driver. If the caller needs to know that info, it must set it
  258. * in the callback. It must also be sure to increment the refcount
  259. * so it doesn't disappear before returning to the caller.
  260. */
  261. int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
  262. void * data, int (*fn)(struct device_driver *, void *))
  263. {
  264. struct klist_iter i;
  265. struct device_driver * drv;
  266. int error = 0;
  267. if (!bus)
  268. return -EINVAL;
  269. klist_iter_init_node(&bus->klist_drivers, &i,
  270. start ? &start->knode_bus : NULL);
  271. while ((drv = next_driver(&i)) && !error)
  272. error = fn(drv, data);
  273. klist_iter_exit(&i);
  274. return error;
  275. }
  276. static int device_add_attrs(struct bus_type * bus, struct device * dev)
  277. {
  278. int error = 0;
  279. int i;
  280. if (bus->dev_attrs) {
  281. for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
  282. error = device_create_file(dev,&bus->dev_attrs[i]);
  283. if (error)
  284. goto Err;
  285. }
  286. }
  287. Done:
  288. return error;
  289. Err:
  290. while (--i >= 0)
  291. device_remove_file(dev,&bus->dev_attrs[i]);
  292. goto Done;
  293. }
  294. static void device_remove_attrs(struct bus_type * bus, struct device * dev)
  295. {
  296. int i;
  297. if (bus->dev_attrs) {
  298. for (i = 0; attr_name(bus->dev_attrs[i]); i++)
  299. device_remove_file(dev,&bus->dev_attrs[i]);
  300. }
  301. }
  302. #ifdef CONFIG_SYSFS_DEPRECATED
  303. static int make_deprecated_bus_links(struct device *dev)
  304. {
  305. return sysfs_create_link(&dev->kobj,
  306. &dev->bus->subsys.kset.kobj, "bus");
  307. }
  308. static void remove_deprecated_bus_links(struct device *dev)
  309. {
  310. sysfs_remove_link(&dev->kobj, "bus");
  311. }
  312. #else
  313. static inline int make_deprecated_bus_links(struct device *dev) { return 0; }
  314. static inline void remove_deprecated_bus_links(struct device *dev) { }
  315. #endif
  316. /**
  317. * bus_add_device - add device to bus
  318. * @dev: device being added
  319. *
  320. * - Add the device to its bus's list of devices.
  321. * - Create link to device's bus.
  322. */
  323. int bus_add_device(struct device * dev)
  324. {
  325. struct bus_type * bus = get_bus(dev->bus);
  326. int error = 0;
  327. if (bus) {
  328. pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
  329. error = device_add_attrs(bus, dev);
  330. if (error)
  331. goto out_put;
  332. error = sysfs_create_link(&bus->devices.kobj,
  333. &dev->kobj, dev->bus_id);
  334. if (error)
  335. goto out_id;
  336. error = sysfs_create_link(&dev->kobj,
  337. &dev->bus->subsys.kset.kobj, "subsystem");
  338. if (error)
  339. goto out_subsys;
  340. error = make_deprecated_bus_links(dev);
  341. if (error)
  342. goto out_deprecated;
  343. }
  344. return 0;
  345. out_deprecated:
  346. sysfs_remove_link(&dev->kobj, "subsystem");
  347. out_subsys:
  348. sysfs_remove_link(&bus->devices.kobj, dev->bus_id);
  349. out_id:
  350. device_remove_attrs(bus, dev);
  351. out_put:
  352. put_bus(dev->bus);
  353. return error;
  354. }
  355. /**
  356. * bus_attach_device - add device to bus
  357. * @dev: device tried to attach to a driver
  358. *
  359. * - Add device to bus's list of devices.
  360. * - Try to attach to driver.
  361. */
  362. int bus_attach_device(struct device * dev)
  363. {
  364. struct bus_type *bus = dev->bus;
  365. int ret = 0;
  366. if (bus) {
  367. dev->is_registered = 1;
  368. ret = device_attach(dev);
  369. if (ret >= 0) {
  370. klist_add_tail(&dev->knode_bus, &bus->klist_devices);
  371. ret = 0;
  372. } else
  373. dev->is_registered = 0;
  374. }
  375. return ret;
  376. }
  377. /**
  378. * bus_remove_device - remove device from bus
  379. * @dev: device to be removed
  380. *
  381. * - Remove symlink from bus's directory.
  382. * - Delete device from bus's list.
  383. * - Detach from its driver.
  384. * - Drop reference taken in bus_add_device().
  385. */
  386. void bus_remove_device(struct device * dev)
  387. {
  388. if (dev->bus) {
  389. sysfs_remove_link(&dev->kobj, "subsystem");
  390. remove_deprecated_bus_links(dev);
  391. sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
  392. device_remove_attrs(dev->bus, dev);
  393. if (dev->is_registered) {
  394. dev->is_registered = 0;
  395. klist_del(&dev->knode_bus);
  396. }
  397. pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
  398. device_release_driver(dev);
  399. put_bus(dev->bus);
  400. }
  401. }
  402. static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
  403. {
  404. int error = 0;
  405. int i;
  406. if (bus->drv_attrs) {
  407. for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
  408. error = driver_create_file(drv, &bus->drv_attrs[i]);
  409. if (error)
  410. goto Err;
  411. }
  412. }
  413. Done:
  414. return error;
  415. Err:
  416. while (--i >= 0)
  417. driver_remove_file(drv, &bus->drv_attrs[i]);
  418. goto Done;
  419. }
  420. static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
  421. {
  422. int i;
  423. if (bus->drv_attrs) {
  424. for (i = 0; attr_name(bus->drv_attrs[i]); i++)
  425. driver_remove_file(drv, &bus->drv_attrs[i]);
  426. }
  427. }
  428. #ifdef CONFIG_HOTPLUG
  429. /*
  430. * Thanks to drivers making their tables __devinit, we can't allow manual
  431. * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
  432. */
  433. static int __must_check add_bind_files(struct device_driver *drv)
  434. {
  435. int ret;
  436. ret = driver_create_file(drv, &driver_attr_unbind);
  437. if (ret == 0) {
  438. ret = driver_create_file(drv, &driver_attr_bind);
  439. if (ret)
  440. driver_remove_file(drv, &driver_attr_unbind);
  441. }
  442. return ret;
  443. }
  444. static void remove_bind_files(struct device_driver *drv)
  445. {
  446. driver_remove_file(drv, &driver_attr_bind);
  447. driver_remove_file(drv, &driver_attr_unbind);
  448. }
  449. #else
  450. static inline int add_bind_files(struct device_driver *drv) { return 0; }
  451. static inline void remove_bind_files(struct device_driver *drv) {}
  452. #endif
  453. /**
  454. * bus_add_driver - Add a driver to the bus.
  455. * @drv: driver.
  456. *
  457. */
  458. int bus_add_driver(struct device_driver *drv)
  459. {
  460. struct bus_type * bus = get_bus(drv->bus);
  461. int error = 0;
  462. if (!bus)
  463. return 0;
  464. pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
  465. error = kobject_set_name(&drv->kobj, "%s", drv->name);
  466. if (error)
  467. goto out_put_bus;
  468. drv->kobj.kset = &bus->drivers;
  469. if ((error = kobject_register(&drv->kobj)))
  470. goto out_put_bus;
  471. error = driver_attach(drv);
  472. if (error)
  473. goto out_unregister;
  474. klist_add_tail(&drv->knode_bus, &bus->klist_drivers);
  475. module_add_driver(drv->owner, drv);
  476. error = driver_add_attrs(bus, drv);
  477. if (error) {
  478. /* How the hell do we get out of this pickle? Give up */
  479. printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
  480. __FUNCTION__, drv->name);
  481. }
  482. error = add_bind_files(drv);
  483. if (error) {
  484. /* Ditto */
  485. printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
  486. __FUNCTION__, drv->name);
  487. }
  488. return error;
  489. out_unregister:
  490. kobject_unregister(&drv->kobj);
  491. out_put_bus:
  492. put_bus(bus);
  493. return error;
  494. }
  495. /**
  496. * bus_remove_driver - delete driver from bus's knowledge.
  497. * @drv: driver.
  498. *
  499. * Detach the driver from the devices it controls, and remove
  500. * it from its bus's list of drivers. Finally, we drop the reference
  501. * to the bus we took in bus_add_driver().
  502. */
  503. void bus_remove_driver(struct device_driver * drv)
  504. {
  505. if (!drv->bus)
  506. return;
  507. remove_bind_files(drv);
  508. driver_remove_attrs(drv->bus, drv);
  509. klist_remove(&drv->knode_bus);
  510. pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
  511. driver_detach(drv);
  512. module_remove_driver(drv);
  513. kobject_unregister(&drv->kobj);
  514. put_bus(drv->bus);
  515. }
  516. /* Helper for bus_rescan_devices's iter */
  517. static int __must_check bus_rescan_devices_helper(struct device *dev,
  518. void *data)
  519. {
  520. int ret = 0;
  521. if (!dev->driver) {
  522. if (dev->parent) /* Needed for USB */
  523. down(&dev->parent->sem);
  524. ret = device_attach(dev);
  525. if (dev->parent)
  526. up(&dev->parent->sem);
  527. if (ret > 0)
  528. ret = 0;
  529. }
  530. return ret < 0 ? ret : 0;
  531. }
  532. /**
  533. * bus_rescan_devices - rescan devices on the bus for possible drivers
  534. * @bus: the bus to scan.
  535. *
  536. * This function will look for devices on the bus with no driver
  537. * attached and rescan it against existing drivers to see if it matches
  538. * any by calling device_attach() for the unbound devices.
  539. */
  540. int bus_rescan_devices(struct bus_type * bus)
  541. {
  542. return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
  543. }
  544. /**
  545. * device_reprobe - remove driver for a device and probe for a new driver
  546. * @dev: the device to reprobe
  547. *
  548. * This function detaches the attached driver (if any) for the given
  549. * device and restarts the driver probing process. It is intended
  550. * to use if probing criteria changed during a devices lifetime and
  551. * driver attachment should change accordingly.
  552. */
  553. int device_reprobe(struct device *dev)
  554. {
  555. if (dev->driver) {
  556. if (dev->parent) /* Needed for USB */
  557. down(&dev->parent->sem);
  558. device_release_driver(dev);
  559. if (dev->parent)
  560. up(&dev->parent->sem);
  561. }
  562. return bus_rescan_devices_helper(dev, NULL);
  563. }
  564. EXPORT_SYMBOL_GPL(device_reprobe);
  565. struct bus_type *get_bus(struct bus_type *bus)
  566. {
  567. return bus ? container_of(subsys_get(&bus->subsys),
  568. struct bus_type, subsys) : NULL;
  569. }
  570. void put_bus(struct bus_type * bus)
  571. {
  572. subsys_put(&bus->subsys);
  573. }
  574. /**
  575. * find_bus - locate bus by name.
  576. * @name: name of bus.
  577. *
  578. * Call kset_find_obj() to iterate over list of buses to
  579. * find a bus by name. Return bus if found.
  580. *
  581. * Note that kset_find_obj increments bus' reference count.
  582. */
  583. #if 0
  584. struct bus_type * find_bus(char * name)
  585. {
  586. struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
  587. return k ? to_bus(k) : NULL;
  588. }
  589. #endif /* 0 */
  590. /**
  591. * bus_add_attrs - Add default attributes for this bus.
  592. * @bus: Bus that has just been registered.
  593. */
  594. static int bus_add_attrs(struct bus_type * bus)
  595. {
  596. int error = 0;
  597. int i;
  598. if (bus->bus_attrs) {
  599. for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
  600. if ((error = bus_create_file(bus,&bus->bus_attrs[i])))
  601. goto Err;
  602. }
  603. }
  604. Done:
  605. return error;
  606. Err:
  607. while (--i >= 0)
  608. bus_remove_file(bus,&bus->bus_attrs[i]);
  609. goto Done;
  610. }
  611. static void bus_remove_attrs(struct bus_type * bus)
  612. {
  613. int i;
  614. if (bus->bus_attrs) {
  615. for (i = 0; attr_name(bus->bus_attrs[i]); i++)
  616. bus_remove_file(bus,&bus->bus_attrs[i]);
  617. }
  618. }
  619. static void klist_devices_get(struct klist_node *n)
  620. {
  621. struct device *dev = container_of(n, struct device, knode_bus);
  622. get_device(dev);
  623. }
  624. static void klist_devices_put(struct klist_node *n)
  625. {
  626. struct device *dev = container_of(n, struct device, knode_bus);
  627. put_device(dev);
  628. }
  629. /**
  630. * bus_register - register a bus with the system.
  631. * @bus: bus.
  632. *
  633. * Once we have that, we registered the bus with the kobject
  634. * infrastructure, then register the children subsystems it has:
  635. * the devices and drivers that belong to the bus.
  636. */
  637. int bus_register(struct bus_type * bus)
  638. {
  639. int retval;
  640. BLOCKING_INIT_NOTIFIER_HEAD(&bus->bus_notifier);
  641. retval = kobject_set_name(&bus->subsys.kset.kobj, "%s", bus->name);
  642. if (retval)
  643. goto out;
  644. subsys_set_kset(bus, bus_subsys);
  645. retval = subsystem_register(&bus->subsys);
  646. if (retval)
  647. goto out;
  648. kobject_set_name(&bus->devices.kobj, "devices");
  649. bus->devices.subsys = &bus->subsys;
  650. retval = kset_register(&bus->devices);
  651. if (retval)
  652. goto bus_devices_fail;
  653. kobject_set_name(&bus->drivers.kobj, "drivers");
  654. bus->drivers.subsys = &bus->subsys;
  655. bus->drivers.ktype = &ktype_driver;
  656. retval = kset_register(&bus->drivers);
  657. if (retval)
  658. goto bus_drivers_fail;
  659. klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put);
  660. klist_init(&bus->klist_drivers, NULL, NULL);
  661. retval = bus_add_attrs(bus);
  662. if (retval)
  663. goto bus_attrs_fail;
  664. pr_debug("bus type '%s' registered\n", bus->name);
  665. return 0;
  666. bus_attrs_fail:
  667. kset_unregister(&bus->drivers);
  668. bus_drivers_fail:
  669. kset_unregister(&bus->devices);
  670. bus_devices_fail:
  671. subsystem_unregister(&bus->subsys);
  672. out:
  673. return retval;
  674. }
  675. /**
  676. * bus_unregister - remove a bus from the system
  677. * @bus: bus.
  678. *
  679. * Unregister the child subsystems and the bus itself.
  680. * Finally, we call put_bus() to release the refcount
  681. */
  682. void bus_unregister(struct bus_type * bus)
  683. {
  684. pr_debug("bus %s: unregistering\n", bus->name);
  685. bus_remove_attrs(bus);
  686. kset_unregister(&bus->drivers);
  687. kset_unregister(&bus->devices);
  688. subsystem_unregister(&bus->subsys);
  689. }
  690. int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
  691. {
  692. return blocking_notifier_chain_register(&bus->bus_notifier, nb);
  693. }
  694. EXPORT_SYMBOL_GPL(bus_register_notifier);
  695. int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
  696. {
  697. return blocking_notifier_chain_unregister(&bus->bus_notifier, nb);
  698. }
  699. EXPORT_SYMBOL_GPL(bus_unregister_notifier);
  700. int __init buses_init(void)
  701. {
  702. return subsystem_register(&bus_subsys);
  703. }
  704. EXPORT_SYMBOL_GPL(bus_for_each_dev);
  705. EXPORT_SYMBOL_GPL(bus_find_device);
  706. EXPORT_SYMBOL_GPL(bus_for_each_drv);
  707. EXPORT_SYMBOL_GPL(bus_register);
  708. EXPORT_SYMBOL_GPL(bus_unregister);
  709. EXPORT_SYMBOL_GPL(bus_rescan_devices);
  710. EXPORT_SYMBOL_GPL(bus_create_file);
  711. EXPORT_SYMBOL_GPL(bus_remove_file);