bus.c 23 KB

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