bus.c 22 KB

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