bus.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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/config.h>
  11. #include <linux/device.h>
  12. #include <linux/module.h>
  13. #include <linux/errno.h>
  14. #include <linux/init.h>
  15. #include <linux/string.h>
  16. #include "base.h"
  17. #include "power/power.h"
  18. #define to_dev(node) container_of(node, struct device, bus_list)
  19. #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
  20. #define to_bus(obj) container_of(obj, struct bus_type, subsys.kset.kobj)
  21. /*
  22. * sysfs bindings for drivers
  23. */
  24. #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
  25. #define to_driver(obj) container_of(obj, struct device_driver, kobj)
  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. decl_subsys(bus, &ktype_bus, NULL);
  109. static int __bus_for_each_dev(struct bus_type *bus, struct device *start,
  110. void *data, int (*fn)(struct device *, void *))
  111. {
  112. struct list_head *head;
  113. struct device *dev;
  114. int error = 0;
  115. if (!(bus = get_bus(bus)))
  116. return -EINVAL;
  117. head = &bus->devices.list;
  118. dev = list_prepare_entry(start, head, bus_list);
  119. list_for_each_entry_continue(dev, head, bus_list) {
  120. get_device(dev);
  121. error = fn(dev, data);
  122. put_device(dev);
  123. if (error)
  124. break;
  125. }
  126. put_bus(bus);
  127. return error;
  128. }
  129. static int __bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
  130. void * data, int (*fn)(struct device_driver *, void *))
  131. {
  132. struct list_head *head;
  133. struct device_driver *drv;
  134. int error = 0;
  135. if (!(bus = get_bus(bus)))
  136. return -EINVAL;
  137. head = &bus->drivers.list;
  138. drv = list_prepare_entry(start, head, kobj.entry);
  139. list_for_each_entry_continue(drv, head, kobj.entry) {
  140. get_driver(drv);
  141. error = fn(drv, data);
  142. put_driver(drv);
  143. if (error)
  144. break;
  145. }
  146. put_bus(bus);
  147. return error;
  148. }
  149. /**
  150. * bus_for_each_dev - device iterator.
  151. * @bus: bus type.
  152. * @start: device to start iterating from.
  153. * @data: data for the callback.
  154. * @fn: function to be called for each device.
  155. *
  156. * Iterate over @bus's list of devices, and call @fn for each,
  157. * passing it @data. If @start is not NULL, we use that device to
  158. * begin iterating from.
  159. *
  160. * We check the return of @fn each time. If it returns anything
  161. * other than 0, we break out and return that value.
  162. *
  163. * NOTE: The device that returns a non-zero value is not retained
  164. * in any way, nor is its refcount incremented. If the caller needs
  165. * to retain this data, it should do, and increment the reference
  166. * count in the supplied callback.
  167. */
  168. int bus_for_each_dev(struct bus_type * bus, struct device * start,
  169. void * data, int (*fn)(struct device *, void *))
  170. {
  171. int ret;
  172. down_read(&bus->subsys.rwsem);
  173. ret = __bus_for_each_dev(bus, start, data, fn);
  174. up_read(&bus->subsys.rwsem);
  175. return ret;
  176. }
  177. /**
  178. * bus_for_each_drv - driver iterator
  179. * @bus: bus we're dealing with.
  180. * @start: driver to start iterating on.
  181. * @data: data to pass to the callback.
  182. * @fn: function to call for each driver.
  183. *
  184. * This is nearly identical to the device iterator above.
  185. * We iterate over each driver that belongs to @bus, and call
  186. * @fn for each. If @fn returns anything but 0, we break out
  187. * and return it. If @start is not NULL, we use it as the head
  188. * of the list.
  189. *
  190. * NOTE: we don't return the driver that returns a non-zero
  191. * value, nor do we leave the reference count incremented for that
  192. * driver. If the caller needs to know that info, it must set it
  193. * in the callback. It must also be sure to increment the refcount
  194. * so it doesn't disappear before returning to the caller.
  195. */
  196. int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
  197. void * data, int (*fn)(struct device_driver *, void *))
  198. {
  199. int ret;
  200. down_read(&bus->subsys.rwsem);
  201. ret = __bus_for_each_drv(bus, start, data, fn);
  202. up_read(&bus->subsys.rwsem);
  203. return ret;
  204. }
  205. static int device_add_attrs(struct bus_type * bus, struct device * dev)
  206. {
  207. int error = 0;
  208. int i;
  209. if (bus->dev_attrs) {
  210. for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
  211. error = device_create_file(dev,&bus->dev_attrs[i]);
  212. if (error)
  213. goto Err;
  214. }
  215. }
  216. Done:
  217. return error;
  218. Err:
  219. while (--i >= 0)
  220. device_remove_file(dev,&bus->dev_attrs[i]);
  221. goto Done;
  222. }
  223. static void device_remove_attrs(struct bus_type * bus, struct device * dev)
  224. {
  225. int i;
  226. if (bus->dev_attrs) {
  227. for (i = 0; attr_name(bus->dev_attrs[i]); i++)
  228. device_remove_file(dev,&bus->dev_attrs[i]);
  229. }
  230. }
  231. /**
  232. * bus_add_device - add device to bus
  233. * @dev: device being added
  234. *
  235. * - Add the device to its bus's list of devices.
  236. * - Try to attach to driver.
  237. * - Create link to device's physical location.
  238. */
  239. int bus_add_device(struct device * dev)
  240. {
  241. struct bus_type * bus = get_bus(dev->bus);
  242. int error = 0;
  243. if (bus) {
  244. down_write(&dev->bus->subsys.rwsem);
  245. pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
  246. list_add_tail(&dev->bus_list, &dev->bus->devices.list);
  247. device_attach(dev);
  248. up_write(&dev->bus->subsys.rwsem);
  249. device_add_attrs(bus, dev);
  250. sysfs_create_link(&bus->devices.kobj, &dev->kobj, dev->bus_id);
  251. sysfs_create_link(&dev->kobj, &dev->bus->subsys.kset.kobj, "bus");
  252. }
  253. return error;
  254. }
  255. /**
  256. * bus_remove_device - remove device from bus
  257. * @dev: device to be removed
  258. *
  259. * - Remove symlink from bus's directory.
  260. * - Delete device from bus's list.
  261. * - Detach from its driver.
  262. * - Drop reference taken in bus_add_device().
  263. */
  264. void bus_remove_device(struct device * dev)
  265. {
  266. if (dev->bus) {
  267. sysfs_remove_link(&dev->kobj, "bus");
  268. sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
  269. device_remove_attrs(dev->bus, dev);
  270. down_write(&dev->bus->subsys.rwsem);
  271. pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
  272. device_release_driver(dev);
  273. list_del_init(&dev->bus_list);
  274. up_write(&dev->bus->subsys.rwsem);
  275. put_bus(dev->bus);
  276. }
  277. }
  278. static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
  279. {
  280. int error = 0;
  281. int i;
  282. if (bus->drv_attrs) {
  283. for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
  284. error = driver_create_file(drv, &bus->drv_attrs[i]);
  285. if (error)
  286. goto Err;
  287. }
  288. }
  289. Done:
  290. return error;
  291. Err:
  292. while (--i >= 0)
  293. driver_remove_file(drv, &bus->drv_attrs[i]);
  294. goto Done;
  295. }
  296. static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
  297. {
  298. int i;
  299. if (bus->drv_attrs) {
  300. for (i = 0; attr_name(bus->drv_attrs[i]); i++)
  301. driver_remove_file(drv, &bus->drv_attrs[i]);
  302. }
  303. }
  304. /**
  305. * bus_add_driver - Add a driver to the bus.
  306. * @drv: driver.
  307. *
  308. */
  309. int bus_add_driver(struct device_driver * drv)
  310. {
  311. struct bus_type * bus = get_bus(drv->bus);
  312. int error = 0;
  313. if (bus) {
  314. pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
  315. error = kobject_set_name(&drv->kobj, "%s", drv->name);
  316. if (error) {
  317. put_bus(bus);
  318. return error;
  319. }
  320. drv->kobj.kset = &bus->drivers;
  321. if ((error = kobject_register(&drv->kobj))) {
  322. put_bus(bus);
  323. return error;
  324. }
  325. down_write(&bus->subsys.rwsem);
  326. driver_attach(drv);
  327. up_write(&bus->subsys.rwsem);
  328. module_add_driver(drv->owner, drv);
  329. driver_add_attrs(bus, drv);
  330. }
  331. return error;
  332. }
  333. /**
  334. * bus_remove_driver - delete driver from bus's knowledge.
  335. * @drv: driver.
  336. *
  337. * Detach the driver from the devices it controls, and remove
  338. * it from its bus's list of drivers. Finally, we drop the reference
  339. * to the bus we took in bus_add_driver().
  340. */
  341. void bus_remove_driver(struct device_driver * drv)
  342. {
  343. if (drv->bus) {
  344. driver_remove_attrs(drv->bus, drv);
  345. down_write(&drv->bus->subsys.rwsem);
  346. pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
  347. driver_detach(drv);
  348. up_write(&drv->bus->subsys.rwsem);
  349. module_remove_driver(drv);
  350. kobject_unregister(&drv->kobj);
  351. put_bus(drv->bus);
  352. }
  353. }
  354. /* Helper for bus_rescan_devices's iter */
  355. static int bus_rescan_devices_helper(struct device *dev, void *data)
  356. {
  357. int *count = data;
  358. if (!dev->driver && device_attach(dev))
  359. (*count)++;
  360. return 0;
  361. }
  362. /**
  363. * bus_rescan_devices - rescan devices on the bus for possible drivers
  364. * @bus: the bus to scan.
  365. *
  366. * This function will look for devices on the bus with no driver
  367. * attached and rescan it against existing drivers to see if it
  368. * matches any. Calls device_attach(). Returns the number of devices
  369. * that were sucessfully bound to a driver.
  370. */
  371. int bus_rescan_devices(struct bus_type * bus)
  372. {
  373. int count = 0;
  374. down_write(&bus->subsys.rwsem);
  375. __bus_for_each_dev(bus, NULL, &count, bus_rescan_devices_helper);
  376. up_write(&bus->subsys.rwsem);
  377. return count;
  378. }
  379. struct bus_type * get_bus(struct bus_type * bus)
  380. {
  381. return bus ? container_of(subsys_get(&bus->subsys), struct bus_type, subsys) : NULL;
  382. }
  383. void put_bus(struct bus_type * bus)
  384. {
  385. subsys_put(&bus->subsys);
  386. }
  387. /**
  388. * find_bus - locate bus by name.
  389. * @name: name of bus.
  390. *
  391. * Call kset_find_obj() to iterate over list of buses to
  392. * find a bus by name. Return bus if found.
  393. *
  394. * Note that kset_find_obj increments bus' reference count.
  395. */
  396. struct bus_type * find_bus(char * name)
  397. {
  398. struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
  399. return k ? to_bus(k) : NULL;
  400. }
  401. /**
  402. * bus_add_attrs - Add default attributes for this bus.
  403. * @bus: Bus that has just been registered.
  404. */
  405. static int bus_add_attrs(struct bus_type * bus)
  406. {
  407. int error = 0;
  408. int i;
  409. if (bus->bus_attrs) {
  410. for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
  411. if ((error = bus_create_file(bus,&bus->bus_attrs[i])))
  412. goto Err;
  413. }
  414. }
  415. Done:
  416. return error;
  417. Err:
  418. while (--i >= 0)
  419. bus_remove_file(bus,&bus->bus_attrs[i]);
  420. goto Done;
  421. }
  422. static void bus_remove_attrs(struct bus_type * bus)
  423. {
  424. int i;
  425. if (bus->bus_attrs) {
  426. for (i = 0; attr_name(bus->bus_attrs[i]); i++)
  427. bus_remove_file(bus,&bus->bus_attrs[i]);
  428. }
  429. }
  430. /**
  431. * bus_register - register a bus with the system.
  432. * @bus: bus.
  433. *
  434. * Once we have that, we registered the bus with the kobject
  435. * infrastructure, then register the children subsystems it has:
  436. * the devices and drivers that belong to the bus.
  437. */
  438. int bus_register(struct bus_type * bus)
  439. {
  440. int retval;
  441. retval = kobject_set_name(&bus->subsys.kset.kobj, "%s", bus->name);
  442. if (retval)
  443. goto out;
  444. subsys_set_kset(bus, bus_subsys);
  445. retval = subsystem_register(&bus->subsys);
  446. if (retval)
  447. goto out;
  448. kobject_set_name(&bus->devices.kobj, "devices");
  449. bus->devices.subsys = &bus->subsys;
  450. retval = kset_register(&bus->devices);
  451. if (retval)
  452. goto bus_devices_fail;
  453. kobject_set_name(&bus->drivers.kobj, "drivers");
  454. bus->drivers.subsys = &bus->subsys;
  455. bus->drivers.ktype = &ktype_driver;
  456. retval = kset_register(&bus->drivers);
  457. if (retval)
  458. goto bus_drivers_fail;
  459. bus_add_attrs(bus);
  460. pr_debug("bus type '%s' registered\n", bus->name);
  461. return 0;
  462. bus_drivers_fail:
  463. kset_unregister(&bus->devices);
  464. bus_devices_fail:
  465. subsystem_unregister(&bus->subsys);
  466. out:
  467. return retval;
  468. }
  469. /**
  470. * bus_unregister - remove a bus from the system
  471. * @bus: bus.
  472. *
  473. * Unregister the child subsystems and the bus itself.
  474. * Finally, we call put_bus() to release the refcount
  475. */
  476. void bus_unregister(struct bus_type * bus)
  477. {
  478. pr_debug("bus %s: unregistering\n", bus->name);
  479. bus_remove_attrs(bus);
  480. kset_unregister(&bus->drivers);
  481. kset_unregister(&bus->devices);
  482. subsystem_unregister(&bus->subsys);
  483. }
  484. int __init buses_init(void)
  485. {
  486. return subsystem_register(&bus_subsys);
  487. }
  488. EXPORT_SYMBOL_GPL(bus_for_each_dev);
  489. EXPORT_SYMBOL_GPL(bus_for_each_drv);
  490. EXPORT_SYMBOL_GPL(bus_add_device);
  491. EXPORT_SYMBOL_GPL(bus_remove_device);
  492. EXPORT_SYMBOL_GPL(bus_register);
  493. EXPORT_SYMBOL_GPL(bus_unregister);
  494. EXPORT_SYMBOL_GPL(bus_rescan_devices);
  495. EXPORT_SYMBOL_GPL(get_bus);
  496. EXPORT_SYMBOL_GPL(put_bus);
  497. EXPORT_SYMBOL_GPL(find_bus);
  498. EXPORT_SYMBOL_GPL(bus_create_file);
  499. EXPORT_SYMBOL_GPL(bus_remove_file);