bus.c 23 KB

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