bus.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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. /* Manually detach a device from its associated driver. */
  109. static int driver_helper(struct device *dev, void *data)
  110. {
  111. const char *name = data;
  112. if (strcmp(name, dev->bus_id) == 0)
  113. return 1;
  114. return 0;
  115. }
  116. static ssize_t driver_unbind(struct device_driver *drv,
  117. const char *buf, size_t count)
  118. {
  119. struct bus_type *bus = get_bus(drv->bus);
  120. struct device *dev;
  121. int err = -ENODEV;
  122. dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
  123. if (dev && dev->driver == drv) {
  124. device_release_driver(dev);
  125. err = count;
  126. }
  127. put_device(dev);
  128. put_bus(bus);
  129. return err;
  130. }
  131. static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
  132. /*
  133. * Manually attach a device to a driver.
  134. * Note: the driver must want to bind to the device,
  135. * it is not possible to override the driver's id table.
  136. */
  137. static ssize_t driver_bind(struct device_driver *drv,
  138. const char *buf, size_t count)
  139. {
  140. struct bus_type *bus = get_bus(drv->bus);
  141. struct device *dev;
  142. int err = -ENODEV;
  143. dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
  144. if (dev && dev->driver == NULL) {
  145. down(&dev->sem);
  146. err = driver_probe_device(drv, dev);
  147. up(&dev->sem);
  148. }
  149. put_device(dev);
  150. put_bus(bus);
  151. return err;
  152. }
  153. static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
  154. static struct device * next_device(struct klist_iter * i)
  155. {
  156. struct klist_node * n = klist_next(i);
  157. return n ? container_of(n, struct device, knode_bus) : NULL;
  158. }
  159. /**
  160. * bus_for_each_dev - device iterator.
  161. * @bus: bus type.
  162. * @start: device to start iterating from.
  163. * @data: data for the callback.
  164. * @fn: function to be called for each device.
  165. *
  166. * Iterate over @bus's list of devices, and call @fn for each,
  167. * passing it @data. If @start is not NULL, we use that device to
  168. * begin iterating from.
  169. *
  170. * We check the return of @fn each time. If it returns anything
  171. * other than 0, we break out and return that value.
  172. *
  173. * NOTE: The device that returns a non-zero value is not retained
  174. * in any way, nor is its refcount incremented. If the caller needs
  175. * to retain this data, it should do, and increment the reference
  176. * count in the supplied callback.
  177. */
  178. int bus_for_each_dev(struct bus_type * bus, struct device * start,
  179. void * data, int (*fn)(struct device *, void *))
  180. {
  181. struct klist_iter i;
  182. struct device * dev;
  183. int error = 0;
  184. if (!bus)
  185. return -EINVAL;
  186. klist_iter_init_node(&bus->klist_devices, &i,
  187. (start ? &start->knode_bus : NULL));
  188. while ((dev = next_device(&i)) && !error)
  189. error = fn(dev, data);
  190. klist_iter_exit(&i);
  191. return error;
  192. }
  193. /**
  194. * bus_find_device - device iterator for locating a particular device.
  195. * @bus: bus type
  196. * @start: Device to begin with
  197. * @data: Data to pass to match function
  198. * @match: Callback function to check device
  199. *
  200. * This is similar to the bus_for_each_dev() function above, but it
  201. * returns a reference to a device that is 'found' for later use, as
  202. * determined by the @match callback.
  203. *
  204. * The callback should return 0 if the device doesn't match and non-zero
  205. * if it does. If the callback returns non-zero, this function will
  206. * return to the caller and not iterate over any more devices.
  207. */
  208. struct device * bus_find_device(struct bus_type *bus,
  209. struct device *start, void *data,
  210. int (*match)(struct device *, void *))
  211. {
  212. struct klist_iter i;
  213. struct device *dev;
  214. if (!bus)
  215. return NULL;
  216. klist_iter_init_node(&bus->klist_devices, &i,
  217. (start ? &start->knode_bus : NULL));
  218. while ((dev = next_device(&i)))
  219. if (match(dev, data) && get_device(dev))
  220. break;
  221. klist_iter_exit(&i);
  222. return dev;
  223. }
  224. static struct device_driver * next_driver(struct klist_iter * i)
  225. {
  226. struct klist_node * n = klist_next(i);
  227. return n ? container_of(n, struct device_driver, knode_bus) : NULL;
  228. }
  229. /**
  230. * bus_for_each_drv - driver iterator
  231. * @bus: bus we're dealing with.
  232. * @start: driver to start iterating on.
  233. * @data: data to pass to the callback.
  234. * @fn: function to call for each driver.
  235. *
  236. * This is nearly identical to the device iterator above.
  237. * We iterate over each driver that belongs to @bus, and call
  238. * @fn for each. If @fn returns anything but 0, we break out
  239. * and return it. If @start is not NULL, we use it as the head
  240. * of the list.
  241. *
  242. * NOTE: we don't return the driver that returns a non-zero
  243. * value, nor do we leave the reference count incremented for that
  244. * driver. If the caller needs to know that info, it must set it
  245. * in the callback. It must also be sure to increment the refcount
  246. * so it doesn't disappear before returning to the caller.
  247. */
  248. int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
  249. void * data, int (*fn)(struct device_driver *, void *))
  250. {
  251. struct klist_iter i;
  252. struct device_driver * drv;
  253. int error = 0;
  254. if (!bus)
  255. return -EINVAL;
  256. klist_iter_init_node(&bus->klist_drivers, &i,
  257. start ? &start->knode_bus : NULL);
  258. while ((drv = next_driver(&i)) && !error)
  259. error = fn(drv, data);
  260. klist_iter_exit(&i);
  261. return error;
  262. }
  263. static int device_add_attrs(struct bus_type * bus, struct device * dev)
  264. {
  265. int error = 0;
  266. int i;
  267. if (bus->dev_attrs) {
  268. for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
  269. error = device_create_file(dev,&bus->dev_attrs[i]);
  270. if (error)
  271. goto Err;
  272. }
  273. }
  274. Done:
  275. return error;
  276. Err:
  277. while (--i >= 0)
  278. device_remove_file(dev,&bus->dev_attrs[i]);
  279. goto Done;
  280. }
  281. static void device_remove_attrs(struct bus_type * bus, struct device * dev)
  282. {
  283. int i;
  284. if (bus->dev_attrs) {
  285. for (i = 0; attr_name(bus->dev_attrs[i]); i++)
  286. device_remove_file(dev,&bus->dev_attrs[i]);
  287. }
  288. }
  289. /**
  290. * bus_add_device - add device to bus
  291. * @dev: device being added
  292. *
  293. * - Add the device to its bus's list of devices.
  294. * - Try to attach to driver.
  295. * - Create link to device's physical location.
  296. */
  297. int bus_add_device(struct device * dev)
  298. {
  299. struct bus_type * bus = get_bus(dev->bus);
  300. int error = 0;
  301. if (bus) {
  302. pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
  303. device_attach(dev);
  304. klist_add_tail(&dev->knode_bus, &bus->klist_devices);
  305. error = device_add_attrs(bus, dev);
  306. if (!error) {
  307. sysfs_create_link(&bus->devices.kobj, &dev->kobj, dev->bus_id);
  308. sysfs_create_link(&dev->kobj, &dev->bus->subsys.kset.kobj, "bus");
  309. }
  310. }
  311. return error;
  312. }
  313. /**
  314. * bus_remove_device - remove device from bus
  315. * @dev: device to be removed
  316. *
  317. * - Remove symlink from bus's directory.
  318. * - Delete device from bus's list.
  319. * - Detach from its driver.
  320. * - Drop reference taken in bus_add_device().
  321. */
  322. void bus_remove_device(struct device * dev)
  323. {
  324. if (dev->bus) {
  325. sysfs_remove_link(&dev->kobj, "bus");
  326. sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
  327. device_remove_attrs(dev->bus, dev);
  328. klist_remove(&dev->knode_bus);
  329. pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
  330. device_release_driver(dev);
  331. put_bus(dev->bus);
  332. }
  333. }
  334. static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
  335. {
  336. int error = 0;
  337. int i;
  338. if (bus->drv_attrs) {
  339. for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
  340. error = driver_create_file(drv, &bus->drv_attrs[i]);
  341. if (error)
  342. goto Err;
  343. }
  344. }
  345. Done:
  346. return error;
  347. Err:
  348. while (--i >= 0)
  349. driver_remove_file(drv, &bus->drv_attrs[i]);
  350. goto Done;
  351. }
  352. static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
  353. {
  354. int i;
  355. if (bus->drv_attrs) {
  356. for (i = 0; attr_name(bus->drv_attrs[i]); i++)
  357. driver_remove_file(drv, &bus->drv_attrs[i]);
  358. }
  359. }
  360. /**
  361. * bus_add_driver - Add a driver to the bus.
  362. * @drv: driver.
  363. *
  364. */
  365. int bus_add_driver(struct device_driver * drv)
  366. {
  367. struct bus_type * bus = get_bus(drv->bus);
  368. int error = 0;
  369. if (bus) {
  370. pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
  371. error = kobject_set_name(&drv->kobj, "%s", drv->name);
  372. if (error) {
  373. put_bus(bus);
  374. return error;
  375. }
  376. drv->kobj.kset = &bus->drivers;
  377. if ((error = kobject_register(&drv->kobj))) {
  378. put_bus(bus);
  379. return error;
  380. }
  381. driver_attach(drv);
  382. klist_add_tail(&drv->knode_bus, &bus->klist_drivers);
  383. module_add_driver(drv->owner, drv);
  384. driver_add_attrs(bus, drv);
  385. driver_create_file(drv, &driver_attr_unbind);
  386. driver_create_file(drv, &driver_attr_bind);
  387. }
  388. return error;
  389. }
  390. /**
  391. * bus_remove_driver - delete driver from bus's knowledge.
  392. * @drv: driver.
  393. *
  394. * Detach the driver from the devices it controls, and remove
  395. * it from its bus's list of drivers. Finally, we drop the reference
  396. * to the bus we took in bus_add_driver().
  397. */
  398. void bus_remove_driver(struct device_driver * drv)
  399. {
  400. if (drv->bus) {
  401. driver_remove_file(drv, &driver_attr_bind);
  402. driver_remove_file(drv, &driver_attr_unbind);
  403. driver_remove_attrs(drv->bus, drv);
  404. klist_remove(&drv->knode_bus);
  405. pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
  406. driver_detach(drv);
  407. module_remove_driver(drv);
  408. kobject_unregister(&drv->kobj);
  409. put_bus(drv->bus);
  410. }
  411. }
  412. /* Helper for bus_rescan_devices's iter */
  413. static int bus_rescan_devices_helper(struct device *dev, void *data)
  414. {
  415. if (!dev->driver)
  416. device_attach(dev);
  417. return 0;
  418. }
  419. /**
  420. * bus_rescan_devices - rescan devices on the bus for possible drivers
  421. * @bus: the bus to scan.
  422. *
  423. * This function will look for devices on the bus with no driver
  424. * attached and rescan it against existing drivers to see if it matches
  425. * any by calling device_attach() for the unbound devices.
  426. */
  427. void bus_rescan_devices(struct bus_type * bus)
  428. {
  429. bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
  430. }
  431. struct bus_type * get_bus(struct bus_type * bus)
  432. {
  433. return bus ? container_of(subsys_get(&bus->subsys), struct bus_type, subsys) : NULL;
  434. }
  435. void put_bus(struct bus_type * bus)
  436. {
  437. subsys_put(&bus->subsys);
  438. }
  439. /**
  440. * find_bus - locate bus by name.
  441. * @name: name of bus.
  442. *
  443. * Call kset_find_obj() to iterate over list of buses to
  444. * find a bus by name. Return bus if found.
  445. *
  446. * Note that kset_find_obj increments bus' reference count.
  447. */
  448. struct bus_type * find_bus(char * name)
  449. {
  450. struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
  451. return k ? to_bus(k) : NULL;
  452. }
  453. /**
  454. * bus_add_attrs - Add default attributes for this bus.
  455. * @bus: Bus that has just been registered.
  456. */
  457. static int bus_add_attrs(struct bus_type * bus)
  458. {
  459. int error = 0;
  460. int i;
  461. if (bus->bus_attrs) {
  462. for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
  463. if ((error = bus_create_file(bus,&bus->bus_attrs[i])))
  464. goto Err;
  465. }
  466. }
  467. Done:
  468. return error;
  469. Err:
  470. while (--i >= 0)
  471. bus_remove_file(bus,&bus->bus_attrs[i]);
  472. goto Done;
  473. }
  474. static void bus_remove_attrs(struct bus_type * bus)
  475. {
  476. int i;
  477. if (bus->bus_attrs) {
  478. for (i = 0; attr_name(bus->bus_attrs[i]); i++)
  479. bus_remove_file(bus,&bus->bus_attrs[i]);
  480. }
  481. }
  482. static void klist_devices_get(struct klist_node *n)
  483. {
  484. struct device *dev = container_of(n, struct device, knode_bus);
  485. get_device(dev);
  486. }
  487. static void klist_devices_put(struct klist_node *n)
  488. {
  489. struct device *dev = container_of(n, struct device, knode_bus);
  490. put_device(dev);
  491. }
  492. static void klist_drivers_get(struct klist_node *n)
  493. {
  494. struct device_driver *drv = container_of(n, struct device_driver,
  495. knode_bus);
  496. get_driver(drv);
  497. }
  498. static void klist_drivers_put(struct klist_node *n)
  499. {
  500. struct device_driver *drv = container_of(n, struct device_driver,
  501. knode_bus);
  502. put_driver(drv);
  503. }
  504. /**
  505. * bus_register - register a bus with the system.
  506. * @bus: bus.
  507. *
  508. * Once we have that, we registered the bus with the kobject
  509. * infrastructure, then register the children subsystems it has:
  510. * the devices and drivers that belong to the bus.
  511. */
  512. int bus_register(struct bus_type * bus)
  513. {
  514. int retval;
  515. retval = kobject_set_name(&bus->subsys.kset.kobj, "%s", bus->name);
  516. if (retval)
  517. goto out;
  518. subsys_set_kset(bus, bus_subsys);
  519. retval = subsystem_register(&bus->subsys);
  520. if (retval)
  521. goto out;
  522. kobject_set_name(&bus->devices.kobj, "devices");
  523. bus->devices.subsys = &bus->subsys;
  524. retval = kset_register(&bus->devices);
  525. if (retval)
  526. goto bus_devices_fail;
  527. kobject_set_name(&bus->drivers.kobj, "drivers");
  528. bus->drivers.subsys = &bus->subsys;
  529. bus->drivers.ktype = &ktype_driver;
  530. retval = kset_register(&bus->drivers);
  531. if (retval)
  532. goto bus_drivers_fail;
  533. klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put);
  534. klist_init(&bus->klist_drivers, klist_drivers_get, klist_drivers_put);
  535. bus_add_attrs(bus);
  536. pr_debug("bus type '%s' registered\n", bus->name);
  537. return 0;
  538. bus_drivers_fail:
  539. kset_unregister(&bus->devices);
  540. bus_devices_fail:
  541. subsystem_unregister(&bus->subsys);
  542. out:
  543. return retval;
  544. }
  545. /**
  546. * bus_unregister - remove a bus from the system
  547. * @bus: bus.
  548. *
  549. * Unregister the child subsystems and the bus itself.
  550. * Finally, we call put_bus() to release the refcount
  551. */
  552. void bus_unregister(struct bus_type * bus)
  553. {
  554. pr_debug("bus %s: unregistering\n", bus->name);
  555. bus_remove_attrs(bus);
  556. kset_unregister(&bus->drivers);
  557. kset_unregister(&bus->devices);
  558. subsystem_unregister(&bus->subsys);
  559. }
  560. int __init buses_init(void)
  561. {
  562. return subsystem_register(&bus_subsys);
  563. }
  564. EXPORT_SYMBOL_GPL(bus_for_each_dev);
  565. EXPORT_SYMBOL_GPL(bus_find_device);
  566. EXPORT_SYMBOL_GPL(bus_for_each_drv);
  567. EXPORT_SYMBOL_GPL(bus_add_device);
  568. EXPORT_SYMBOL_GPL(bus_remove_device);
  569. EXPORT_SYMBOL_GPL(bus_register);
  570. EXPORT_SYMBOL_GPL(bus_unregister);
  571. EXPORT_SYMBOL_GPL(bus_rescan_devices);
  572. EXPORT_SYMBOL_GPL(get_bus);
  573. EXPORT_SYMBOL_GPL(put_bus);
  574. EXPORT_SYMBOL_GPL(find_bus);
  575. EXPORT_SYMBOL_GPL(bus_create_file);
  576. EXPORT_SYMBOL_GPL(bus_remove_file);