bus.c 24 KB

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