bus.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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
  41. drv_attr_show(struct kobject * kobj, struct attribute * attr, 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
  51. drv_attr_store(struct kobject * kobj, struct attribute * attr,
  52. const char * buf, size_t count)
  53. {
  54. struct driver_attribute * drv_attr = to_drv_attr(attr);
  55. struct driver_private *drv_priv = to_driver(kobj);
  56. ssize_t ret = -EIO;
  57. if (drv_attr->store)
  58. ret = drv_attr->store(drv_priv->driver, buf, count);
  59. return ret;
  60. }
  61. static struct sysfs_ops driver_sysfs_ops = {
  62. .show = drv_attr_show,
  63. .store = drv_attr_store,
  64. };
  65. static void driver_release(struct kobject *kobj)
  66. {
  67. struct driver_private *drv_priv = to_driver(kobj);
  68. pr_debug("%s: freeing %s\n", __FUNCTION__, kobject_name(kobj));
  69. kfree(drv_priv);
  70. }
  71. static struct kobj_type driver_ktype = {
  72. .sysfs_ops = &driver_sysfs_ops,
  73. .release = driver_release,
  74. };
  75. /*
  76. * sysfs bindings for buses
  77. */
  78. static ssize_t
  79. bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
  80. {
  81. struct bus_attribute * bus_attr = to_bus_attr(attr);
  82. struct bus_type_private *bus_priv = to_bus(kobj);
  83. ssize_t ret = 0;
  84. if (bus_attr->show)
  85. ret = bus_attr->show(bus_priv->bus, buf);
  86. return ret;
  87. }
  88. static ssize_t
  89. bus_attr_store(struct kobject * kobj, struct attribute * attr,
  90. const char * buf, size_t count)
  91. {
  92. struct bus_attribute * bus_attr = to_bus_attr(attr);
  93. struct bus_type_private *bus_priv = to_bus(kobj);
  94. ssize_t ret = 0;
  95. if (bus_attr->store)
  96. ret = bus_attr->store(bus_priv->bus, buf, count);
  97. return ret;
  98. }
  99. static struct sysfs_ops bus_sysfs_ops = {
  100. .show = bus_attr_show,
  101. .store = bus_attr_store,
  102. };
  103. int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
  104. {
  105. int error;
  106. if (bus_get(bus)) {
  107. error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
  108. bus_put(bus);
  109. } else
  110. error = -EINVAL;
  111. return error;
  112. }
  113. void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
  114. {
  115. if (bus_get(bus)) {
  116. sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
  117. bus_put(bus);
  118. }
  119. }
  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) /* success */
  184. err = count;
  185. else if (err == 0) /* driver didn't accept device */
  186. err = -ENODEV;
  187. }
  188. put_device(dev);
  189. bus_put(bus);
  190. return err;
  191. }
  192. static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
  193. static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
  194. {
  195. return sprintf(buf, "%d\n", bus->p->drivers_autoprobe);
  196. }
  197. static ssize_t store_drivers_autoprobe(struct bus_type *bus,
  198. const char *buf, size_t count)
  199. {
  200. if (buf[0] == '0')
  201. bus->p->drivers_autoprobe = 0;
  202. else
  203. bus->p->drivers_autoprobe = 1;
  204. return count;
  205. }
  206. static ssize_t store_drivers_probe(struct bus_type *bus,
  207. const char *buf, size_t count)
  208. {
  209. struct device *dev;
  210. dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
  211. if (!dev)
  212. return -ENODEV;
  213. if (bus_rescan_devices_helper(dev, NULL) != 0)
  214. return -EINVAL;
  215. return count;
  216. }
  217. #endif
  218. static struct device * next_device(struct klist_iter * i)
  219. {
  220. struct klist_node * n = klist_next(i);
  221. return n ? container_of(n, struct device, knode_bus) : NULL;
  222. }
  223. /**
  224. * bus_for_each_dev - device iterator.
  225. * @bus: bus type.
  226. * @start: device to start iterating from.
  227. * @data: data for the callback.
  228. * @fn: function to be called for each device.
  229. *
  230. * Iterate over @bus's list of devices, and call @fn for each,
  231. * passing it @data. If @start is not NULL, we use that device to
  232. * begin iterating from.
  233. *
  234. * We check the return of @fn each time. If it returns anything
  235. * other than 0, we break out and return that value.
  236. *
  237. * NOTE: The device that returns a non-zero value is not retained
  238. * in any way, nor is its refcount incremented. If the caller needs
  239. * to retain this data, it should do, and increment the reference
  240. * count in the supplied callback.
  241. */
  242. int bus_for_each_dev(struct bus_type * bus, struct device * start,
  243. void * data, int (*fn)(struct device *, void *))
  244. {
  245. struct klist_iter i;
  246. struct device * dev;
  247. int error = 0;
  248. if (!bus)
  249. return -EINVAL;
  250. klist_iter_init_node(&bus->p->klist_devices, &i,
  251. (start ? &start->knode_bus : NULL));
  252. while ((dev = next_device(&i)) && !error)
  253. error = fn(dev, data);
  254. klist_iter_exit(&i);
  255. return error;
  256. }
  257. /**
  258. * bus_find_device - device iterator for locating a particular device.
  259. * @bus: bus type
  260. * @start: Device to begin with
  261. * @data: Data to pass to match function
  262. * @match: Callback function to check device
  263. *
  264. * This is similar to the bus_for_each_dev() function above, but it
  265. * returns a reference to a device that is 'found' for later use, as
  266. * determined by the @match callback.
  267. *
  268. * The callback should return 0 if the device doesn't match and non-zero
  269. * if it does. If the callback returns non-zero, this function will
  270. * return to the caller and not iterate over any more devices.
  271. */
  272. struct device * bus_find_device(struct bus_type *bus,
  273. struct device *start, void *data,
  274. int (*match)(struct device *, void *))
  275. {
  276. struct klist_iter i;
  277. struct device *dev;
  278. if (!bus)
  279. return NULL;
  280. klist_iter_init_node(&bus->p->klist_devices, &i,
  281. (start ? &start->knode_bus : NULL));
  282. while ((dev = next_device(&i)))
  283. if (match(dev, data) && get_device(dev))
  284. break;
  285. klist_iter_exit(&i);
  286. return dev;
  287. }
  288. static struct device_driver * next_driver(struct klist_iter * i)
  289. {
  290. struct klist_node * n = klist_next(i);
  291. struct driver_private *drv_priv;
  292. if (n) {
  293. drv_priv = container_of(n, struct driver_private, knode_bus);
  294. return drv_priv->driver;
  295. }
  296. return 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->p->klist_drivers, &i,
  326. start ? &start->p->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->p->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->p->devices_kset->kobj,
  387. &dev->kobj, dev->bus_id);
  388. if (error)
  389. goto out_id;
  390. error = sysfs_create_link(&dev->kobj,
  391. &dev->bus->p->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->p->devices_kset->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->p->drivers_autoprobe)
  423. ret = device_attach(dev);
  424. WARN_ON(ret < 0);
  425. if (ret >= 0)
  426. klist_add_tail(&dev->knode_bus, &bus->p->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->p->devices_kset->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->p->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;
  546. struct driver_private *priv;
  547. int error = 0;
  548. bus = bus_get(drv->bus);
  549. if (!bus)
  550. return -EINVAL;
  551. pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
  552. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  553. if (!priv)
  554. return -ENOMEM;
  555. error = kobject_set_name(&priv->kobj, "%s", drv->name);
  556. if (error)
  557. goto out_put_bus;
  558. priv->kobj.kset = bus->p->drivers_kset;
  559. priv->kobj.ktype = &driver_ktype;
  560. klist_init(&priv->klist_devices, NULL, NULL);
  561. priv->driver = drv;
  562. drv->p = priv;
  563. error = kobject_register(&priv->kobj);
  564. if (error)
  565. goto out_put_bus;
  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. __FUNCTION__, 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. __FUNCTION__, drv->name);
  583. }
  584. error = add_bind_files(drv);
  585. if (error) {
  586. /* Ditto */
  587. printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
  588. __FUNCTION__, drv->name);
  589. }
  590. return error;
  591. out_unregister:
  592. kobject_unregister(&priv->kobj);
  593. out_put_bus:
  594. bus_put(bus);
  595. return error;
  596. }
  597. /**
  598. * bus_remove_driver - delete driver from bus's knowledge.
  599. * @drv: driver.
  600. *
  601. * Detach the driver from the devices it controls, and remove
  602. * it from its bus's list of drivers. Finally, we drop the reference
  603. * to the bus we took in bus_add_driver().
  604. */
  605. void bus_remove_driver(struct device_driver * drv)
  606. {
  607. if (!drv->bus)
  608. return;
  609. remove_bind_files(drv);
  610. driver_remove_attrs(drv->bus, drv);
  611. driver_remove_file(drv, &driver_attr_uevent);
  612. klist_remove(&drv->p->knode_bus);
  613. pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
  614. driver_detach(drv);
  615. module_remove_driver(drv);
  616. kobject_unregister(&drv->p->kobj);
  617. bus_put(drv->bus);
  618. }
  619. /* Helper for bus_rescan_devices's iter */
  620. static int __must_check bus_rescan_devices_helper(struct device *dev,
  621. void *data)
  622. {
  623. int ret = 0;
  624. if (!dev->driver) {
  625. if (dev->parent) /* Needed for USB */
  626. down(&dev->parent->sem);
  627. ret = device_attach(dev);
  628. if (dev->parent)
  629. up(&dev->parent->sem);
  630. }
  631. return ret < 0 ? ret : 0;
  632. }
  633. /**
  634. * bus_rescan_devices - rescan devices on the bus for possible drivers
  635. * @bus: the bus to scan.
  636. *
  637. * This function will look for devices on the bus with no driver
  638. * attached and rescan it against existing drivers to see if it matches
  639. * any by calling device_attach() for the unbound devices.
  640. */
  641. int bus_rescan_devices(struct bus_type * bus)
  642. {
  643. return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
  644. }
  645. /**
  646. * device_reprobe - remove driver for a device and probe for a new driver
  647. * @dev: the device to reprobe
  648. *
  649. * This function detaches the attached driver (if any) for the given
  650. * device and restarts the driver probing process. It is intended
  651. * to use if probing criteria changed during a devices lifetime and
  652. * driver attachment should change accordingly.
  653. */
  654. int device_reprobe(struct device *dev)
  655. {
  656. if (dev->driver) {
  657. if (dev->parent) /* Needed for USB */
  658. down(&dev->parent->sem);
  659. device_release_driver(dev);
  660. if (dev->parent)
  661. up(&dev->parent->sem);
  662. }
  663. return bus_rescan_devices_helper(dev, NULL);
  664. }
  665. EXPORT_SYMBOL_GPL(device_reprobe);
  666. /**
  667. * find_bus - locate bus by name.
  668. * @name: name of bus.
  669. *
  670. * Call kset_find_obj() to iterate over list of buses to
  671. * find a bus by name. Return bus if found.
  672. *
  673. * Note that kset_find_obj increments bus' reference count.
  674. */
  675. #if 0
  676. struct bus_type * find_bus(char * name)
  677. {
  678. struct kobject * k = kset_find_obj(bus_kset, name);
  679. return k ? to_bus(k) : NULL;
  680. }
  681. #endif /* 0 */
  682. /**
  683. * bus_add_attrs - Add default attributes for this bus.
  684. * @bus: Bus that has just been registered.
  685. */
  686. static int bus_add_attrs(struct bus_type * bus)
  687. {
  688. int error = 0;
  689. int i;
  690. if (bus->bus_attrs) {
  691. for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
  692. error = bus_create_file(bus,&bus->bus_attrs[i]);
  693. if (error)
  694. goto Err;
  695. }
  696. }
  697. Done:
  698. return error;
  699. Err:
  700. while (--i >= 0)
  701. bus_remove_file(bus,&bus->bus_attrs[i]);
  702. goto Done;
  703. }
  704. static void bus_remove_attrs(struct bus_type * bus)
  705. {
  706. int i;
  707. if (bus->bus_attrs) {
  708. for (i = 0; attr_name(bus->bus_attrs[i]); i++)
  709. bus_remove_file(bus,&bus->bus_attrs[i]);
  710. }
  711. }
  712. static void klist_devices_get(struct klist_node *n)
  713. {
  714. struct device *dev = container_of(n, struct device, knode_bus);
  715. get_device(dev);
  716. }
  717. static void klist_devices_put(struct klist_node *n)
  718. {
  719. struct device *dev = container_of(n, struct device, knode_bus);
  720. put_device(dev);
  721. }
  722. static ssize_t bus_uevent_store(struct bus_type *bus,
  723. const char *buf, size_t count)
  724. {
  725. enum kobject_action action;
  726. if (kobject_action_type(buf, count, &action) == 0)
  727. kobject_uevent(&bus->p->subsys.kobj, action);
  728. return count;
  729. }
  730. static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
  731. /**
  732. * bus_register - register a bus with the system.
  733. * @bus: bus.
  734. *
  735. * Once we have that, we registered the bus with the kobject
  736. * infrastructure, then register the children subsystems it has:
  737. * the devices and drivers that belong to the bus.
  738. */
  739. int bus_register(struct bus_type * bus)
  740. {
  741. int retval;
  742. struct bus_type_private *priv;
  743. priv = kzalloc(sizeof(struct bus_type_private), GFP_KERNEL);
  744. if (!priv)
  745. return -ENOMEM;
  746. priv->bus = bus;
  747. bus->p = priv;
  748. BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
  749. retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
  750. if (retval)
  751. goto out;
  752. priv->subsys.kobj.kset = bus_kset;
  753. priv->subsys.kobj.ktype = &bus_ktype;
  754. priv->drivers_autoprobe = 1;
  755. retval = kset_register(&priv->subsys);
  756. if (retval)
  757. goto out;
  758. retval = bus_create_file(bus, &bus_attr_uevent);
  759. if (retval)
  760. goto bus_uevent_fail;
  761. priv->devices_kset = kset_create_and_add("devices", NULL,
  762. &priv->subsys.kobj);
  763. if (!priv->devices_kset) {
  764. retval = -ENOMEM;
  765. goto bus_devices_fail;
  766. }
  767. priv->drivers_kset = kset_create_and_add("drivers", NULL,
  768. &priv->subsys.kobj);
  769. if (!priv->drivers_kset) {
  770. retval = -ENOMEM;
  771. goto bus_drivers_fail;
  772. }
  773. klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
  774. klist_init(&priv->klist_drivers, NULL, NULL);
  775. retval = add_probe_files(bus);
  776. if (retval)
  777. goto bus_probe_files_fail;
  778. retval = bus_add_attrs(bus);
  779. if (retval)
  780. goto bus_attrs_fail;
  781. pr_debug("bus type '%s' registered\n", bus->name);
  782. return 0;
  783. bus_attrs_fail:
  784. remove_probe_files(bus);
  785. bus_probe_files_fail:
  786. kset_unregister(bus->p->drivers_kset);
  787. bus_drivers_fail:
  788. kset_unregister(bus->p->devices_kset);
  789. bus_devices_fail:
  790. bus_remove_file(bus, &bus_attr_uevent);
  791. bus_uevent_fail:
  792. kset_unregister(&bus->p->subsys);
  793. kfree(bus->p);
  794. out:
  795. return retval;
  796. }
  797. /**
  798. * bus_unregister - remove a bus from the system
  799. * @bus: bus.
  800. *
  801. * Unregister the child subsystems and the bus itself.
  802. * Finally, we call bus_put() to release the refcount
  803. */
  804. void bus_unregister(struct bus_type * bus)
  805. {
  806. pr_debug("bus %s: unregistering\n", bus->name);
  807. bus_remove_attrs(bus);
  808. remove_probe_files(bus);
  809. kset_unregister(bus->p->drivers_kset);
  810. kset_unregister(bus->p->devices_kset);
  811. bus_remove_file(bus, &bus_attr_uevent);
  812. kset_unregister(&bus->p->subsys);
  813. kfree(bus->p);
  814. }
  815. int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
  816. {
  817. return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
  818. }
  819. EXPORT_SYMBOL_GPL(bus_register_notifier);
  820. int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
  821. {
  822. return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
  823. }
  824. EXPORT_SYMBOL_GPL(bus_unregister_notifier);
  825. struct kset *bus_get_kset(struct bus_type *bus)
  826. {
  827. return &bus->p->subsys;
  828. }
  829. EXPORT_SYMBOL_GPL(bus_get_kset);
  830. struct klist *bus_get_device_klist(struct bus_type *bus)
  831. {
  832. return &bus->p->klist_devices;
  833. }
  834. EXPORT_SYMBOL_GPL(bus_get_device_klist);
  835. int __init buses_init(void)
  836. {
  837. bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
  838. if (!bus_kset)
  839. return -ENOMEM;
  840. return 0;
  841. }
  842. EXPORT_SYMBOL_GPL(bus_for_each_dev);
  843. EXPORT_SYMBOL_GPL(bus_find_device);
  844. EXPORT_SYMBOL_GPL(bus_for_each_drv);
  845. EXPORT_SYMBOL_GPL(bus_register);
  846. EXPORT_SYMBOL_GPL(bus_unregister);
  847. EXPORT_SYMBOL_GPL(bus_rescan_devices);
  848. EXPORT_SYMBOL_GPL(bus_create_file);
  849. EXPORT_SYMBOL_GPL(bus_remove_file);