bus.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
  19. #define to_bus(obj) container_of(obj, struct bus_type, subsys.kset.kobj)
  20. /*
  21. * sysfs bindings for drivers
  22. */
  23. #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
  24. #define to_driver(obj) container_of(obj, struct device_driver, kobj)
  25. static ssize_t
  26. drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
  27. {
  28. struct driver_attribute * drv_attr = to_drv_attr(attr);
  29. struct device_driver * drv = to_driver(kobj);
  30. ssize_t ret = -EIO;
  31. if (drv_attr->show)
  32. ret = drv_attr->show(drv, buf);
  33. return ret;
  34. }
  35. static ssize_t
  36. drv_attr_store(struct kobject * kobj, struct attribute * attr,
  37. const char * buf, size_t count)
  38. {
  39. struct driver_attribute * drv_attr = to_drv_attr(attr);
  40. struct device_driver * drv = to_driver(kobj);
  41. ssize_t ret = -EIO;
  42. if (drv_attr->store)
  43. ret = drv_attr->store(drv, buf, count);
  44. return ret;
  45. }
  46. static struct sysfs_ops driver_sysfs_ops = {
  47. .show = drv_attr_show,
  48. .store = drv_attr_store,
  49. };
  50. static void driver_release(struct kobject * kobj)
  51. {
  52. struct device_driver * drv = to_driver(kobj);
  53. complete(&drv->unloaded);
  54. }
  55. static struct kobj_type ktype_driver = {
  56. .sysfs_ops = &driver_sysfs_ops,
  57. .release = driver_release,
  58. };
  59. /*
  60. * sysfs bindings for buses
  61. */
  62. static ssize_t
  63. bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
  64. {
  65. struct bus_attribute * bus_attr = to_bus_attr(attr);
  66. struct bus_type * bus = to_bus(kobj);
  67. ssize_t ret = 0;
  68. if (bus_attr->show)
  69. ret = bus_attr->show(bus, buf);
  70. return ret;
  71. }
  72. static ssize_t
  73. bus_attr_store(struct kobject * kobj, struct attribute * attr,
  74. const char * buf, size_t count)
  75. {
  76. struct bus_attribute * bus_attr = to_bus_attr(attr);
  77. struct bus_type * bus = to_bus(kobj);
  78. ssize_t ret = 0;
  79. if (bus_attr->store)
  80. ret = bus_attr->store(bus, buf, count);
  81. return ret;
  82. }
  83. static struct sysfs_ops bus_sysfs_ops = {
  84. .show = bus_attr_show,
  85. .store = bus_attr_store,
  86. };
  87. int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
  88. {
  89. int error;
  90. if (get_bus(bus)) {
  91. error = sysfs_create_file(&bus->subsys.kset.kobj, &attr->attr);
  92. put_bus(bus);
  93. } else
  94. error = -EINVAL;
  95. return error;
  96. }
  97. void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
  98. {
  99. if (get_bus(bus)) {
  100. sysfs_remove_file(&bus->subsys.kset.kobj, &attr->attr);
  101. put_bus(bus);
  102. }
  103. }
  104. static struct kobj_type ktype_bus = {
  105. .sysfs_ops = &bus_sysfs_ops,
  106. };
  107. decl_subsys(bus, &ktype_bus, NULL);
  108. static struct device * next_device(struct klist_iter * i)
  109. {
  110. struct klist_node * n = klist_next(i);
  111. return n ? container_of(n, struct device, knode_bus) : NULL;
  112. }
  113. /**
  114. * bus_for_each_dev - device iterator.
  115. * @bus: bus type.
  116. * @start: device to start iterating from.
  117. * @data: data for the callback.
  118. * @fn: function to be called for each device.
  119. *
  120. * Iterate over @bus's list of devices, and call @fn for each,
  121. * passing it @data. If @start is not NULL, we use that device to
  122. * begin iterating from.
  123. *
  124. * We check the return of @fn each time. If it returns anything
  125. * other than 0, we break out and return that value.
  126. *
  127. * NOTE: The device that returns a non-zero value is not retained
  128. * in any way, nor is its refcount incremented. If the caller needs
  129. * to retain this data, it should do, and increment the reference
  130. * count in the supplied callback.
  131. */
  132. int bus_for_each_dev(struct bus_type * bus, struct device * start,
  133. void * data, int (*fn)(struct device *, void *))
  134. {
  135. struct klist_iter i;
  136. struct device * dev;
  137. int error = 0;
  138. if (!bus)
  139. return -EINVAL;
  140. klist_iter_init_node(&bus->klist_devices, &i,
  141. (start ? &start->knode_bus : NULL));
  142. while ((dev = next_device(&i)) && !error)
  143. error = fn(dev, data);
  144. klist_iter_exit(&i);
  145. return error;
  146. }
  147. static struct device_driver * next_driver(struct klist_iter * i)
  148. {
  149. struct klist_node * n = klist_next(i);
  150. return n ? container_of(n, struct device_driver, knode_bus) : NULL;
  151. }
  152. /**
  153. * bus_for_each_drv - driver iterator
  154. * @bus: bus we're dealing with.
  155. * @start: driver to start iterating on.
  156. * @data: data to pass to the callback.
  157. * @fn: function to call for each driver.
  158. *
  159. * This is nearly identical to the device iterator above.
  160. * We iterate over each driver that belongs to @bus, and call
  161. * @fn for each. If @fn returns anything but 0, we break out
  162. * and return it. If @start is not NULL, we use it as the head
  163. * of the list.
  164. *
  165. * NOTE: we don't return the driver that returns a non-zero
  166. * value, nor do we leave the reference count incremented for that
  167. * driver. If the caller needs to know that info, it must set it
  168. * in the callback. It must also be sure to increment the refcount
  169. * so it doesn't disappear before returning to the caller.
  170. */
  171. int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
  172. void * data, int (*fn)(struct device_driver *, void *))
  173. {
  174. struct klist_iter i;
  175. struct device_driver * drv;
  176. int error = 0;
  177. if (!bus)
  178. return -EINVAL;
  179. klist_iter_init_node(&bus->klist_drivers, &i,
  180. start ? &start->knode_bus : NULL);
  181. while ((drv = next_driver(&i)) && !error)
  182. error = fn(drv, data);
  183. klist_iter_exit(&i);
  184. return error;
  185. }
  186. static int device_add_attrs(struct bus_type * bus, struct device * dev)
  187. {
  188. int error = 0;
  189. int i;
  190. if (bus->dev_attrs) {
  191. for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
  192. error = device_create_file(dev,&bus->dev_attrs[i]);
  193. if (error)
  194. goto Err;
  195. }
  196. }
  197. Done:
  198. return error;
  199. Err:
  200. while (--i >= 0)
  201. device_remove_file(dev,&bus->dev_attrs[i]);
  202. goto Done;
  203. }
  204. static void device_remove_attrs(struct bus_type * bus, struct device * dev)
  205. {
  206. int i;
  207. if (bus->dev_attrs) {
  208. for (i = 0; attr_name(bus->dev_attrs[i]); i++)
  209. device_remove_file(dev,&bus->dev_attrs[i]);
  210. }
  211. }
  212. /**
  213. * bus_add_device - add device to bus
  214. * @dev: device being added
  215. *
  216. * - Add the device to its bus's list of devices.
  217. * - Try to attach to driver.
  218. * - Create link to device's physical location.
  219. */
  220. int bus_add_device(struct device * dev)
  221. {
  222. struct bus_type * bus = get_bus(dev->bus);
  223. int error = 0;
  224. if (bus) {
  225. pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
  226. error = device_attach(dev);
  227. klist_add_tail(&bus->klist_devices, &dev->knode_bus);
  228. if (error >= 0)
  229. error = device_add_attrs(bus, dev);
  230. if (!error) {
  231. sysfs_create_link(&bus->devices.kobj, &dev->kobj, dev->bus_id);
  232. sysfs_create_link(&dev->kobj, &dev->bus->subsys.kset.kobj, "bus");
  233. }
  234. }
  235. return error;
  236. }
  237. /**
  238. * bus_remove_device - remove device from bus
  239. * @dev: device to be removed
  240. *
  241. * - Remove symlink from bus's directory.
  242. * - Delete device from bus's list.
  243. * - Detach from its driver.
  244. * - Drop reference taken in bus_add_device().
  245. */
  246. void bus_remove_device(struct device * dev)
  247. {
  248. if (dev->bus) {
  249. sysfs_remove_link(&dev->kobj, "bus");
  250. sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
  251. device_remove_attrs(dev->bus, dev);
  252. klist_remove(&dev->knode_bus);
  253. pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
  254. device_release_driver(dev);
  255. put_bus(dev->bus);
  256. }
  257. }
  258. static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
  259. {
  260. int error = 0;
  261. int i;
  262. if (bus->drv_attrs) {
  263. for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
  264. error = driver_create_file(drv, &bus->drv_attrs[i]);
  265. if (error)
  266. goto Err;
  267. }
  268. }
  269. Done:
  270. return error;
  271. Err:
  272. while (--i >= 0)
  273. driver_remove_file(drv, &bus->drv_attrs[i]);
  274. goto Done;
  275. }
  276. static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
  277. {
  278. int i;
  279. if (bus->drv_attrs) {
  280. for (i = 0; attr_name(bus->drv_attrs[i]); i++)
  281. driver_remove_file(drv, &bus->drv_attrs[i]);
  282. }
  283. }
  284. /**
  285. * bus_add_driver - Add a driver to the bus.
  286. * @drv: driver.
  287. *
  288. */
  289. int bus_add_driver(struct device_driver * drv)
  290. {
  291. struct bus_type * bus = get_bus(drv->bus);
  292. int error = 0;
  293. if (bus) {
  294. pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
  295. error = kobject_set_name(&drv->kobj, "%s", drv->name);
  296. if (error) {
  297. put_bus(bus);
  298. return error;
  299. }
  300. drv->kobj.kset = &bus->drivers;
  301. if ((error = kobject_register(&drv->kobj))) {
  302. put_bus(bus);
  303. return error;
  304. }
  305. driver_attach(drv);
  306. klist_add_tail(&bus->klist_drivers, &drv->knode_bus);
  307. module_add_driver(drv->owner, drv);
  308. driver_add_attrs(bus, drv);
  309. }
  310. return error;
  311. }
  312. /**
  313. * bus_remove_driver - delete driver from bus's knowledge.
  314. * @drv: driver.
  315. *
  316. * Detach the driver from the devices it controls, and remove
  317. * it from its bus's list of drivers. Finally, we drop the reference
  318. * to the bus we took in bus_add_driver().
  319. */
  320. void bus_remove_driver(struct device_driver * drv)
  321. {
  322. if (drv->bus) {
  323. driver_remove_attrs(drv->bus, drv);
  324. klist_remove(&drv->knode_bus);
  325. pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
  326. driver_detach(drv);
  327. module_remove_driver(drv);
  328. kobject_unregister(&drv->kobj);
  329. put_bus(drv->bus);
  330. }
  331. }
  332. /* Helper for bus_rescan_devices's iter */
  333. static int bus_rescan_devices_helper(struct device *dev, void *data)
  334. {
  335. int *count = data;
  336. if (!dev->driver && (device_attach(dev) > 0))
  337. (*count)++;
  338. return 0;
  339. }
  340. /**
  341. * bus_rescan_devices - rescan devices on the bus for possible drivers
  342. * @bus: the bus to scan.
  343. *
  344. * This function will look for devices on the bus with no driver
  345. * attached and rescan it against existing drivers to see if it
  346. * matches any. Calls device_attach(). Returns the number of devices
  347. * that were sucessfully bound to a driver.
  348. */
  349. int bus_rescan_devices(struct bus_type * bus)
  350. {
  351. int count = 0;
  352. bus_for_each_dev(bus, NULL, &count, bus_rescan_devices_helper);
  353. return count;
  354. }
  355. struct bus_type * get_bus(struct bus_type * bus)
  356. {
  357. return bus ? container_of(subsys_get(&bus->subsys), struct bus_type, subsys) : NULL;
  358. }
  359. void put_bus(struct bus_type * bus)
  360. {
  361. subsys_put(&bus->subsys);
  362. }
  363. /**
  364. * find_bus - locate bus by name.
  365. * @name: name of bus.
  366. *
  367. * Call kset_find_obj() to iterate over list of buses to
  368. * find a bus by name. Return bus if found.
  369. *
  370. * Note that kset_find_obj increments bus' reference count.
  371. */
  372. struct bus_type * find_bus(char * name)
  373. {
  374. struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
  375. return k ? to_bus(k) : NULL;
  376. }
  377. /**
  378. * bus_add_attrs - Add default attributes for this bus.
  379. * @bus: Bus that has just been registered.
  380. */
  381. static int bus_add_attrs(struct bus_type * bus)
  382. {
  383. int error = 0;
  384. int i;
  385. if (bus->bus_attrs) {
  386. for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
  387. if ((error = bus_create_file(bus,&bus->bus_attrs[i])))
  388. goto Err;
  389. }
  390. }
  391. Done:
  392. return error;
  393. Err:
  394. while (--i >= 0)
  395. bus_remove_file(bus,&bus->bus_attrs[i]);
  396. goto Done;
  397. }
  398. static void bus_remove_attrs(struct bus_type * bus)
  399. {
  400. int i;
  401. if (bus->bus_attrs) {
  402. for (i = 0; attr_name(bus->bus_attrs[i]); i++)
  403. bus_remove_file(bus,&bus->bus_attrs[i]);
  404. }
  405. }
  406. /**
  407. * bus_register - register a bus with the system.
  408. * @bus: bus.
  409. *
  410. * Once we have that, we registered the bus with the kobject
  411. * infrastructure, then register the children subsystems it has:
  412. * the devices and drivers that belong to the bus.
  413. */
  414. int bus_register(struct bus_type * bus)
  415. {
  416. int retval;
  417. retval = kobject_set_name(&bus->subsys.kset.kobj, "%s", bus->name);
  418. if (retval)
  419. goto out;
  420. subsys_set_kset(bus, bus_subsys);
  421. retval = subsystem_register(&bus->subsys);
  422. if (retval)
  423. goto out;
  424. kobject_set_name(&bus->devices.kobj, "devices");
  425. bus->devices.subsys = &bus->subsys;
  426. retval = kset_register(&bus->devices);
  427. if (retval)
  428. goto bus_devices_fail;
  429. kobject_set_name(&bus->drivers.kobj, "drivers");
  430. bus->drivers.subsys = &bus->subsys;
  431. bus->drivers.ktype = &ktype_driver;
  432. retval = kset_register(&bus->drivers);
  433. if (retval)
  434. goto bus_drivers_fail;
  435. klist_init(&bus->klist_devices);
  436. klist_init(&bus->klist_drivers);
  437. bus_add_attrs(bus);
  438. pr_debug("bus type '%s' registered\n", bus->name);
  439. return 0;
  440. bus_drivers_fail:
  441. kset_unregister(&bus->devices);
  442. bus_devices_fail:
  443. subsystem_unregister(&bus->subsys);
  444. out:
  445. return retval;
  446. }
  447. /**
  448. * bus_unregister - remove a bus from the system
  449. * @bus: bus.
  450. *
  451. * Unregister the child subsystems and the bus itself.
  452. * Finally, we call put_bus() to release the refcount
  453. */
  454. void bus_unregister(struct bus_type * bus)
  455. {
  456. pr_debug("bus %s: unregistering\n", bus->name);
  457. bus_remove_attrs(bus);
  458. kset_unregister(&bus->drivers);
  459. kset_unregister(&bus->devices);
  460. subsystem_unregister(&bus->subsys);
  461. }
  462. int __init buses_init(void)
  463. {
  464. return subsystem_register(&bus_subsys);
  465. }
  466. EXPORT_SYMBOL_GPL(bus_for_each_dev);
  467. EXPORT_SYMBOL_GPL(bus_for_each_drv);
  468. EXPORT_SYMBOL_GPL(bus_add_device);
  469. EXPORT_SYMBOL_GPL(bus_remove_device);
  470. EXPORT_SYMBOL_GPL(bus_register);
  471. EXPORT_SYMBOL_GPL(bus_unregister);
  472. EXPORT_SYMBOL_GPL(bus_rescan_devices);
  473. EXPORT_SYMBOL_GPL(get_bus);
  474. EXPORT_SYMBOL_GPL(put_bus);
  475. EXPORT_SYMBOL_GPL(find_bus);
  476. EXPORT_SYMBOL_GPL(bus_create_file);
  477. EXPORT_SYMBOL_GPL(bus_remove_file);