bus.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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/slab.h>
  16. #include <linux/init.h>
  17. #include <linux/string.h>
  18. #include "base.h"
  19. #include "power/power.h"
  20. #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
  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 const struct sysfs_ops driver_sysfs_ops = {
  61. .show = drv_attr_show,
  62. .store = drv_attr_store,
  63. };
  64. static void driver_release(struct kobject *kobj)
  65. {
  66. struct driver_private *drv_priv = to_driver(kobj);
  67. pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
  68. kfree(drv_priv);
  69. }
  70. static struct kobj_type driver_ktype = {
  71. .sysfs_ops = &driver_sysfs_ops,
  72. .release = driver_release,
  73. };
  74. /*
  75. * sysfs bindings for buses
  76. */
  77. static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
  78. char *buf)
  79. {
  80. struct bus_attribute *bus_attr = to_bus_attr(attr);
  81. struct subsys_private *subsys_priv = to_subsys_private(kobj);
  82. ssize_t ret = 0;
  83. if (bus_attr->show)
  84. ret = bus_attr->show(subsys_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 subsys_private *subsys_priv = to_subsys_private(kobj);
  92. ssize_t ret = 0;
  93. if (bus_attr->store)
  94. ret = bus_attr->store(subsys_priv->bus, buf, count);
  95. return ret;
  96. }
  97. static const 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 const struct kset_uevent_ops bus_uevent_ops = {
  131. .filter = bus_uevent_filter,
  132. };
  133. static struct kset *bus_kset;
  134. #ifdef CONFIG_HOTPLUG
  135. /* Manually detach a device from its associated driver. */
  136. static ssize_t driver_unbind(struct device_driver *drv,
  137. const char *buf, size_t count)
  138. {
  139. struct bus_type *bus = bus_get(drv->bus);
  140. struct device *dev;
  141. int err = -ENODEV;
  142. dev = bus_find_device_by_name(bus, NULL, buf);
  143. if (dev && dev->driver == drv) {
  144. if (dev->parent) /* Needed for USB */
  145. device_lock(dev->parent);
  146. device_release_driver(dev);
  147. if (dev->parent)
  148. device_unlock(dev->parent);
  149. err = count;
  150. }
  151. put_device(dev);
  152. bus_put(bus);
  153. return err;
  154. }
  155. static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
  156. /*
  157. * Manually attach a device to a driver.
  158. * Note: the driver must want to bind to the device,
  159. * it is not possible to override the driver's id table.
  160. */
  161. static ssize_t driver_bind(struct device_driver *drv,
  162. const char *buf, size_t count)
  163. {
  164. struct bus_type *bus = bus_get(drv->bus);
  165. struct device *dev;
  166. int err = -ENODEV;
  167. dev = bus_find_device_by_name(bus, NULL, buf);
  168. if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
  169. if (dev->parent) /* Needed for USB */
  170. device_lock(dev->parent);
  171. device_lock(dev);
  172. err = driver_probe_device(drv, dev);
  173. device_unlock(dev);
  174. if (dev->parent)
  175. device_unlock(dev->parent);
  176. if (err > 0) {
  177. /* success */
  178. err = count;
  179. } else if (err == 0) {
  180. /* driver didn't accept device */
  181. err = -ENODEV;
  182. }
  183. }
  184. put_device(dev);
  185. bus_put(bus);
  186. return err;
  187. }
  188. static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
  189. static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
  190. {
  191. return sprintf(buf, "%d\n", bus->p->drivers_autoprobe);
  192. }
  193. static ssize_t store_drivers_autoprobe(struct bus_type *bus,
  194. const char *buf, size_t count)
  195. {
  196. if (buf[0] == '0')
  197. bus->p->drivers_autoprobe = 0;
  198. else
  199. bus->p->drivers_autoprobe = 1;
  200. return count;
  201. }
  202. static ssize_t store_drivers_probe(struct bus_type *bus,
  203. const char *buf, size_t count)
  204. {
  205. struct device *dev;
  206. dev = bus_find_device_by_name(bus, NULL, buf);
  207. if (!dev)
  208. return -ENODEV;
  209. if (bus_rescan_devices_helper(dev, NULL) != 0)
  210. return -EINVAL;
  211. return count;
  212. }
  213. #endif
  214. static struct device *next_device(struct klist_iter *i)
  215. {
  216. struct klist_node *n = klist_next(i);
  217. struct device *dev = NULL;
  218. struct device_private *dev_prv;
  219. if (n) {
  220. dev_prv = to_device_private_bus(n);
  221. dev = dev_prv->device;
  222. }
  223. return dev;
  224. }
  225. /**
  226. * bus_for_each_dev - device iterator.
  227. * @bus: bus type.
  228. * @start: device to start iterating from.
  229. * @data: data for the callback.
  230. * @fn: function to be called for each device.
  231. *
  232. * Iterate over @bus's list of devices, and call @fn for each,
  233. * passing it @data. If @start is not NULL, we use that device to
  234. * begin iterating from.
  235. *
  236. * We check the return of @fn each time. If it returns anything
  237. * other than 0, we break out and return that value.
  238. *
  239. * NOTE: The device that returns a non-zero value is not retained
  240. * in any way, nor is its refcount incremented. If the caller needs
  241. * to retain this data, it should do so, and increment the reference
  242. * count in the supplied callback.
  243. */
  244. int bus_for_each_dev(struct bus_type *bus, struct device *start,
  245. void *data, int (*fn)(struct device *, void *))
  246. {
  247. struct klist_iter i;
  248. struct device *dev;
  249. int error = 0;
  250. if (!bus)
  251. return -EINVAL;
  252. klist_iter_init_node(&bus->p->klist_devices, &i,
  253. (start ? &start->p->knode_bus : NULL));
  254. while ((dev = next_device(&i)) && !error)
  255. error = fn(dev, data);
  256. klist_iter_exit(&i);
  257. return error;
  258. }
  259. EXPORT_SYMBOL_GPL(bus_for_each_dev);
  260. /**
  261. * bus_find_device - device iterator for locating a particular device.
  262. * @bus: bus type
  263. * @start: Device to begin with
  264. * @data: Data to pass to match function
  265. * @match: Callback function to check device
  266. *
  267. * This is similar to the bus_for_each_dev() function above, but it
  268. * returns a reference to a device that is 'found' for later use, as
  269. * determined by the @match callback.
  270. *
  271. * The callback should return 0 if the device doesn't match and non-zero
  272. * if it does. If the callback returns non-zero, this function will
  273. * return to the caller and not iterate over any more devices.
  274. */
  275. struct device *bus_find_device(struct bus_type *bus,
  276. struct device *start, void *data,
  277. int (*match)(struct device *dev, void *data))
  278. {
  279. struct klist_iter i;
  280. struct device *dev;
  281. if (!bus)
  282. return NULL;
  283. klist_iter_init_node(&bus->p->klist_devices, &i,
  284. (start ? &start->p->knode_bus : NULL));
  285. while ((dev = next_device(&i)))
  286. if (match(dev, data) && get_device(dev))
  287. break;
  288. klist_iter_exit(&i);
  289. return dev;
  290. }
  291. EXPORT_SYMBOL_GPL(bus_find_device);
  292. static int match_name(struct device *dev, void *data)
  293. {
  294. const char *name = data;
  295. return sysfs_streq(name, dev_name(dev));
  296. }
  297. /**
  298. * bus_find_device_by_name - device iterator for locating a particular device of a specific name
  299. * @bus: bus type
  300. * @start: Device to begin with
  301. * @name: name of the device to match
  302. *
  303. * This is similar to the bus_find_device() function above, but it handles
  304. * searching by a name automatically, no need to write another strcmp matching
  305. * function.
  306. */
  307. struct device *bus_find_device_by_name(struct bus_type *bus,
  308. struct device *start, const char *name)
  309. {
  310. return bus_find_device(bus, start, (void *)name, match_name);
  311. }
  312. EXPORT_SYMBOL_GPL(bus_find_device_by_name);
  313. static struct device_driver *next_driver(struct klist_iter *i)
  314. {
  315. struct klist_node *n = klist_next(i);
  316. struct driver_private *drv_priv;
  317. if (n) {
  318. drv_priv = container_of(n, struct driver_private, knode_bus);
  319. return drv_priv->driver;
  320. }
  321. return NULL;
  322. }
  323. /**
  324. * bus_for_each_drv - driver iterator
  325. * @bus: bus we're dealing with.
  326. * @start: driver to start iterating on.
  327. * @data: data to pass to the callback.
  328. * @fn: function to call for each driver.
  329. *
  330. * This is nearly identical to the device iterator above.
  331. * We iterate over each driver that belongs to @bus, and call
  332. * @fn for each. If @fn returns anything but 0, we break out
  333. * and return it. If @start is not NULL, we use it as the head
  334. * of the list.
  335. *
  336. * NOTE: we don't return the driver that returns a non-zero
  337. * value, nor do we leave the reference count incremented for that
  338. * driver. If the caller needs to know that info, it must set it
  339. * in the callback. It must also be sure to increment the refcount
  340. * so it doesn't disappear before returning to the caller.
  341. */
  342. int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
  343. void *data, int (*fn)(struct device_driver *, void *))
  344. {
  345. struct klist_iter i;
  346. struct device_driver *drv;
  347. int error = 0;
  348. if (!bus)
  349. return -EINVAL;
  350. klist_iter_init_node(&bus->p->klist_drivers, &i,
  351. start ? &start->p->knode_bus : NULL);
  352. while ((drv = next_driver(&i)) && !error)
  353. error = fn(drv, data);
  354. klist_iter_exit(&i);
  355. return error;
  356. }
  357. EXPORT_SYMBOL_GPL(bus_for_each_drv);
  358. static int device_add_attrs(struct bus_type *bus, struct device *dev)
  359. {
  360. int error = 0;
  361. int i;
  362. if (!bus->dev_attrs)
  363. return 0;
  364. for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
  365. error = device_create_file(dev, &bus->dev_attrs[i]);
  366. if (error) {
  367. while (--i >= 0)
  368. device_remove_file(dev, &bus->dev_attrs[i]);
  369. break;
  370. }
  371. }
  372. return error;
  373. }
  374. static void device_remove_attrs(struct bus_type *bus, struct device *dev)
  375. {
  376. int i;
  377. if (bus->dev_attrs) {
  378. for (i = 0; attr_name(bus->dev_attrs[i]); i++)
  379. device_remove_file(dev, &bus->dev_attrs[i]);
  380. }
  381. }
  382. /**
  383. * bus_add_device - add device to bus
  384. * @dev: device being added
  385. *
  386. * - Add device's bus attributes.
  387. * - Create links to device's bus.
  388. * - Add the device to its bus's list of devices.
  389. */
  390. int bus_add_device(struct device *dev)
  391. {
  392. struct bus_type *bus = bus_get(dev->bus);
  393. int error = 0;
  394. if (bus) {
  395. pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
  396. error = device_add_attrs(bus, dev);
  397. if (error)
  398. goto out_put;
  399. error = sysfs_create_link(&bus->p->devices_kset->kobj,
  400. &dev->kobj, dev_name(dev));
  401. if (error)
  402. goto out_id;
  403. error = sysfs_create_link(&dev->kobj,
  404. &dev->bus->p->subsys.kobj, "subsystem");
  405. if (error)
  406. goto out_subsys;
  407. klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
  408. }
  409. return 0;
  410. out_subsys:
  411. sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
  412. out_id:
  413. device_remove_attrs(bus, dev);
  414. out_put:
  415. bus_put(dev->bus);
  416. return error;
  417. }
  418. /**
  419. * bus_probe_device - probe drivers for a new device
  420. * @dev: device to probe
  421. *
  422. * - Automatically probe for a driver if the bus allows it.
  423. */
  424. void bus_probe_device(struct device *dev)
  425. {
  426. struct bus_type *bus = dev->bus;
  427. int ret;
  428. if (bus && bus->p->drivers_autoprobe) {
  429. ret = device_attach(dev);
  430. WARN_ON(ret < 0);
  431. }
  432. }
  433. /**
  434. * bus_remove_device - remove device from bus
  435. * @dev: device to be removed
  436. *
  437. * - Remove symlink from bus's directory.
  438. * - Delete device from bus's list.
  439. * - Detach from its driver.
  440. * - Drop reference taken in bus_add_device().
  441. */
  442. void bus_remove_device(struct device *dev)
  443. {
  444. if (dev->bus) {
  445. sysfs_remove_link(&dev->kobj, "subsystem");
  446. sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
  447. dev_name(dev));
  448. device_remove_attrs(dev->bus, dev);
  449. if (klist_node_attached(&dev->p->knode_bus))
  450. klist_del(&dev->p->knode_bus);
  451. pr_debug("bus: '%s': remove device %s\n",
  452. dev->bus->name, dev_name(dev));
  453. device_release_driver(dev);
  454. bus_put(dev->bus);
  455. }
  456. }
  457. static int driver_add_attrs(struct bus_type *bus, struct device_driver *drv)
  458. {
  459. int error = 0;
  460. int i;
  461. if (bus->drv_attrs) {
  462. for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
  463. error = driver_create_file(drv, &bus->drv_attrs[i]);
  464. if (error)
  465. goto err;
  466. }
  467. }
  468. done:
  469. return error;
  470. err:
  471. while (--i >= 0)
  472. driver_remove_file(drv, &bus->drv_attrs[i]);
  473. goto done;
  474. }
  475. static void driver_remove_attrs(struct bus_type *bus,
  476. struct device_driver *drv)
  477. {
  478. int i;
  479. if (bus->drv_attrs) {
  480. for (i = 0; attr_name(bus->drv_attrs[i]); i++)
  481. driver_remove_file(drv, &bus->drv_attrs[i]);
  482. }
  483. }
  484. #ifdef CONFIG_HOTPLUG
  485. /*
  486. * Thanks to drivers making their tables __devinit, we can't allow manual
  487. * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
  488. */
  489. static int __must_check add_bind_files(struct device_driver *drv)
  490. {
  491. int ret;
  492. ret = driver_create_file(drv, &driver_attr_unbind);
  493. if (ret == 0) {
  494. ret = driver_create_file(drv, &driver_attr_bind);
  495. if (ret)
  496. driver_remove_file(drv, &driver_attr_unbind);
  497. }
  498. return ret;
  499. }
  500. static void remove_bind_files(struct device_driver *drv)
  501. {
  502. driver_remove_file(drv, &driver_attr_bind);
  503. driver_remove_file(drv, &driver_attr_unbind);
  504. }
  505. static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
  506. static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
  507. show_drivers_autoprobe, store_drivers_autoprobe);
  508. static int add_probe_files(struct bus_type *bus)
  509. {
  510. int retval;
  511. retval = bus_create_file(bus, &bus_attr_drivers_probe);
  512. if (retval)
  513. goto out;
  514. retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
  515. if (retval)
  516. bus_remove_file(bus, &bus_attr_drivers_probe);
  517. out:
  518. return retval;
  519. }
  520. static void remove_probe_files(struct bus_type *bus)
  521. {
  522. bus_remove_file(bus, &bus_attr_drivers_autoprobe);
  523. bus_remove_file(bus, &bus_attr_drivers_probe);
  524. }
  525. #else
  526. static inline int add_bind_files(struct device_driver *drv) { return 0; }
  527. static inline void remove_bind_files(struct device_driver *drv) {}
  528. static inline int add_probe_files(struct bus_type *bus) { return 0; }
  529. static inline void remove_probe_files(struct bus_type *bus) {}
  530. #endif
  531. static ssize_t driver_uevent_store(struct device_driver *drv,
  532. const char *buf, size_t count)
  533. {
  534. enum kobject_action action;
  535. if (kobject_action_type(buf, count, &action) == 0)
  536. kobject_uevent(&drv->p->kobj, action);
  537. return count;
  538. }
  539. static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store);
  540. /**
  541. * bus_add_driver - Add a driver to the bus.
  542. * @drv: driver.
  543. */
  544. int bus_add_driver(struct device_driver *drv)
  545. {
  546. struct bus_type *bus;
  547. struct driver_private *priv;
  548. int error = 0;
  549. bus = bus_get(drv->bus);
  550. if (!bus)
  551. return -EINVAL;
  552. pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
  553. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  554. if (!priv) {
  555. error = -ENOMEM;
  556. goto out_put_bus;
  557. }
  558. klist_init(&priv->klist_devices, NULL, NULL);
  559. priv->driver = drv;
  560. drv->p = priv;
  561. priv->kobj.kset = bus->p->drivers_kset;
  562. error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
  563. "%s", drv->name);
  564. if (error)
  565. goto out_unregister;
  566. if (drv->bus->p->drivers_autoprobe) {
  567. error = driver_attach(drv);
  568. if (error)
  569. goto out_unregister;
  570. }
  571. klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
  572. module_add_driver(drv->owner, drv);
  573. error = driver_create_file(drv, &driver_attr_uevent);
  574. if (error) {
  575. printk(KERN_ERR "%s: uevent attr (%s) failed\n",
  576. __func__, drv->name);
  577. }
  578. error = driver_add_attrs(bus, drv);
  579. if (error) {
  580. /* How the hell do we get out of this pickle? Give up */
  581. printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
  582. __func__, drv->name);
  583. }
  584. if (!drv->suppress_bind_attrs) {
  585. error = add_bind_files(drv);
  586. if (error) {
  587. /* Ditto */
  588. printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
  589. __func__, drv->name);
  590. }
  591. }
  592. kobject_uevent(&priv->kobj, KOBJ_ADD);
  593. return 0;
  594. out_unregister:
  595. kobject_put(&priv->kobj);
  596. kfree(drv->p);
  597. drv->p = NULL;
  598. out_put_bus:
  599. bus_put(bus);
  600. return error;
  601. }
  602. /**
  603. * bus_remove_driver - delete driver from bus's knowledge.
  604. * @drv: driver.
  605. *
  606. * Detach the driver from the devices it controls, and remove
  607. * it from its bus's list of drivers. Finally, we drop the reference
  608. * to the bus we took in bus_add_driver().
  609. */
  610. void bus_remove_driver(struct device_driver *drv)
  611. {
  612. if (!drv->bus)
  613. return;
  614. if (!drv->suppress_bind_attrs)
  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. device_lock(dev->parent);
  633. ret = device_attach(dev);
  634. if (dev->parent)
  635. device_unlock(dev->parent);
  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. device_lock(dev->parent);
  666. device_release_driver(dev);
  667. if (dev->parent)
  668. device_unlock(dev->parent);
  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_private *dev_prv = to_device_private_bus(n);
  722. struct device *dev = dev_prv->device;
  723. get_device(dev);
  724. }
  725. static void klist_devices_put(struct klist_node *n)
  726. {
  727. struct device_private *dev_prv = to_device_private_bus(n);
  728. struct device *dev = dev_prv->device;
  729. put_device(dev);
  730. }
  731. static ssize_t bus_uevent_store(struct bus_type *bus,
  732. const char *buf, size_t count)
  733. {
  734. enum kobject_action action;
  735. if (kobject_action_type(buf, count, &action) == 0)
  736. kobject_uevent(&bus->p->subsys.kobj, action);
  737. return count;
  738. }
  739. static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
  740. /**
  741. * bus_register - register a bus with the system.
  742. * @bus: bus.
  743. *
  744. * Once we have that, we registered the bus with the kobject
  745. * infrastructure, then register the children subsystems it has:
  746. * the devices and drivers that belong to the bus.
  747. */
  748. int bus_register(struct bus_type *bus)
  749. {
  750. int retval;
  751. struct subsys_private *priv;
  752. priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
  753. if (!priv)
  754. return -ENOMEM;
  755. priv->bus = bus;
  756. bus->p = priv;
  757. BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
  758. retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
  759. if (retval)
  760. goto out;
  761. priv->subsys.kobj.kset = bus_kset;
  762. priv->subsys.kobj.ktype = &bus_ktype;
  763. priv->drivers_autoprobe = 1;
  764. retval = kset_register(&priv->subsys);
  765. if (retval)
  766. goto out;
  767. retval = bus_create_file(bus, &bus_attr_uevent);
  768. if (retval)
  769. goto bus_uevent_fail;
  770. priv->devices_kset = kset_create_and_add("devices", NULL,
  771. &priv->subsys.kobj);
  772. if (!priv->devices_kset) {
  773. retval = -ENOMEM;
  774. goto bus_devices_fail;
  775. }
  776. priv->drivers_kset = kset_create_and_add("drivers", NULL,
  777. &priv->subsys.kobj);
  778. if (!priv->drivers_kset) {
  779. retval = -ENOMEM;
  780. goto bus_drivers_fail;
  781. }
  782. klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
  783. klist_init(&priv->klist_drivers, NULL, NULL);
  784. retval = add_probe_files(bus);
  785. if (retval)
  786. goto bus_probe_files_fail;
  787. retval = bus_add_attrs(bus);
  788. if (retval)
  789. goto bus_attrs_fail;
  790. pr_debug("bus: '%s': registered\n", bus->name);
  791. return 0;
  792. bus_attrs_fail:
  793. remove_probe_files(bus);
  794. bus_probe_files_fail:
  795. kset_unregister(bus->p->drivers_kset);
  796. bus_drivers_fail:
  797. kset_unregister(bus->p->devices_kset);
  798. bus_devices_fail:
  799. bus_remove_file(bus, &bus_attr_uevent);
  800. bus_uevent_fail:
  801. kset_unregister(&bus->p->subsys);
  802. out:
  803. kfree(bus->p);
  804. bus->p = NULL;
  805. return retval;
  806. }
  807. EXPORT_SYMBOL_GPL(bus_register);
  808. /**
  809. * bus_unregister - remove a bus from the system
  810. * @bus: bus.
  811. *
  812. * Unregister the child subsystems and the bus itself.
  813. * Finally, we call bus_put() to release the refcount
  814. */
  815. void bus_unregister(struct bus_type *bus)
  816. {
  817. pr_debug("bus: '%s': unregistering\n", bus->name);
  818. bus_remove_attrs(bus);
  819. remove_probe_files(bus);
  820. kset_unregister(bus->p->drivers_kset);
  821. kset_unregister(bus->p->devices_kset);
  822. bus_remove_file(bus, &bus_attr_uevent);
  823. kset_unregister(&bus->p->subsys);
  824. kfree(bus->p);
  825. bus->p = NULL;
  826. }
  827. EXPORT_SYMBOL_GPL(bus_unregister);
  828. int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
  829. {
  830. return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
  831. }
  832. EXPORT_SYMBOL_GPL(bus_register_notifier);
  833. int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
  834. {
  835. return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
  836. }
  837. EXPORT_SYMBOL_GPL(bus_unregister_notifier);
  838. struct kset *bus_get_kset(struct bus_type *bus)
  839. {
  840. return &bus->p->subsys;
  841. }
  842. EXPORT_SYMBOL_GPL(bus_get_kset);
  843. struct klist *bus_get_device_klist(struct bus_type *bus)
  844. {
  845. return &bus->p->klist_devices;
  846. }
  847. EXPORT_SYMBOL_GPL(bus_get_device_klist);
  848. /*
  849. * Yes, this forcibly breaks the klist abstraction temporarily. It
  850. * just wants to sort the klist, not change reference counts and
  851. * take/drop locks rapidly in the process. It does all this while
  852. * holding the lock for the list, so objects can't otherwise be
  853. * added/removed while we're swizzling.
  854. */
  855. static void device_insertion_sort_klist(struct device *a, struct list_head *list,
  856. int (*compare)(const struct device *a,
  857. const struct device *b))
  858. {
  859. struct list_head *pos;
  860. struct klist_node *n;
  861. struct device_private *dev_prv;
  862. struct device *b;
  863. list_for_each(pos, list) {
  864. n = container_of(pos, struct klist_node, n_node);
  865. dev_prv = to_device_private_bus(n);
  866. b = dev_prv->device;
  867. if (compare(a, b) <= 0) {
  868. list_move_tail(&a->p->knode_bus.n_node,
  869. &b->p->knode_bus.n_node);
  870. return;
  871. }
  872. }
  873. list_move_tail(&a->p->knode_bus.n_node, list);
  874. }
  875. void bus_sort_breadthfirst(struct bus_type *bus,
  876. int (*compare)(const struct device *a,
  877. const struct device *b))
  878. {
  879. LIST_HEAD(sorted_devices);
  880. struct list_head *pos, *tmp;
  881. struct klist_node *n;
  882. struct device_private *dev_prv;
  883. struct device *dev;
  884. struct klist *device_klist;
  885. device_klist = bus_get_device_klist(bus);
  886. spin_lock(&device_klist->k_lock);
  887. list_for_each_safe(pos, tmp, &device_klist->k_list) {
  888. n = container_of(pos, struct klist_node, n_node);
  889. dev_prv = to_device_private_bus(n);
  890. dev = dev_prv->device;
  891. device_insertion_sort_klist(dev, &sorted_devices, compare);
  892. }
  893. list_splice(&sorted_devices, &device_klist->k_list);
  894. spin_unlock(&device_klist->k_lock);
  895. }
  896. EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
  897. int __init buses_init(void)
  898. {
  899. bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
  900. if (!bus_kset)
  901. return -ENOMEM;
  902. return 0;
  903. }