bus.c 21 KB

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