bus.c 22 KB

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