bus.c 17 KB

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