bus.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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. * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
  7. * Copyright (c) 2007 Novell Inc.
  8. *
  9. * This file is released under the GPLv2
  10. *
  11. */
  12. #include <linux/async.h>
  13. #include <linux/device.h>
  14. #include <linux/module.h>
  15. #include <linux/errno.h>
  16. #include <linux/slab.h>
  17. #include <linux/init.h>
  18. #include <linux/string.h>
  19. #include <linux/mutex.h>
  20. #include <linux/sysfs.h>
  21. #include "base.h"
  22. #include "power/power.h"
  23. /* /sys/devices/system */
  24. static struct kset *system_kset;
  25. #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
  26. /*
  27. * sysfs bindings for drivers
  28. */
  29. #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
  30. static int __must_check bus_rescan_devices_helper(struct device *dev,
  31. void *data);
  32. static struct bus_type *bus_get(struct bus_type *bus)
  33. {
  34. if (bus) {
  35. kset_get(&bus->p->subsys);
  36. return bus;
  37. }
  38. return NULL;
  39. }
  40. static void bus_put(struct bus_type *bus)
  41. {
  42. if (bus)
  43. kset_put(&bus->p->subsys);
  44. }
  45. static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
  46. char *buf)
  47. {
  48. struct driver_attribute *drv_attr = to_drv_attr(attr);
  49. struct driver_private *drv_priv = to_driver(kobj);
  50. ssize_t ret = -EIO;
  51. if (drv_attr->show)
  52. ret = drv_attr->show(drv_priv->driver, buf);
  53. return ret;
  54. }
  55. static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
  56. const char *buf, size_t count)
  57. {
  58. struct driver_attribute *drv_attr = to_drv_attr(attr);
  59. struct driver_private *drv_priv = to_driver(kobj);
  60. ssize_t ret = -EIO;
  61. if (drv_attr->store)
  62. ret = drv_attr->store(drv_priv->driver, buf, count);
  63. return ret;
  64. }
  65. static const struct sysfs_ops driver_sysfs_ops = {
  66. .show = drv_attr_show,
  67. .store = drv_attr_store,
  68. };
  69. static void driver_release(struct kobject *kobj)
  70. {
  71. struct driver_private *drv_priv = to_driver(kobj);
  72. pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
  73. kfree(drv_priv);
  74. }
  75. static struct kobj_type driver_ktype = {
  76. .sysfs_ops = &driver_sysfs_ops,
  77. .release = driver_release,
  78. };
  79. /*
  80. * sysfs bindings for buses
  81. */
  82. static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
  83. char *buf)
  84. {
  85. struct bus_attribute *bus_attr = to_bus_attr(attr);
  86. struct subsys_private *subsys_priv = to_subsys_private(kobj);
  87. ssize_t ret = 0;
  88. if (bus_attr->show)
  89. ret = bus_attr->show(subsys_priv->bus, buf);
  90. return ret;
  91. }
  92. static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
  93. const char *buf, size_t count)
  94. {
  95. struct bus_attribute *bus_attr = to_bus_attr(attr);
  96. struct subsys_private *subsys_priv = to_subsys_private(kobj);
  97. ssize_t ret = 0;
  98. if (bus_attr->store)
  99. ret = bus_attr->store(subsys_priv->bus, buf, count);
  100. return ret;
  101. }
  102. static const struct sysfs_ops bus_sysfs_ops = {
  103. .show = bus_attr_show,
  104. .store = bus_attr_store,
  105. };
  106. int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)
  107. {
  108. int error;
  109. if (bus_get(bus)) {
  110. error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
  111. bus_put(bus);
  112. } else
  113. error = -EINVAL;
  114. return error;
  115. }
  116. EXPORT_SYMBOL_GPL(bus_create_file);
  117. void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)
  118. {
  119. if (bus_get(bus)) {
  120. sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
  121. bus_put(bus);
  122. }
  123. }
  124. EXPORT_SYMBOL_GPL(bus_remove_file);
  125. static void bus_release(struct kobject *kobj)
  126. {
  127. struct subsys_private *priv = to_subsys_private(kobj);
  128. struct bus_type *bus = priv->bus;
  129. kfree(priv);
  130. bus->p = NULL;
  131. }
  132. static struct kobj_type bus_ktype = {
  133. .sysfs_ops = &bus_sysfs_ops,
  134. .release = bus_release,
  135. };
  136. static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
  137. {
  138. struct kobj_type *ktype = get_ktype(kobj);
  139. if (ktype == &bus_ktype)
  140. return 1;
  141. return 0;
  142. }
  143. static const struct kset_uevent_ops bus_uevent_ops = {
  144. .filter = bus_uevent_filter,
  145. };
  146. static struct kset *bus_kset;
  147. /* Manually detach a device from its associated driver. */
  148. static ssize_t unbind_store(struct device_driver *drv, const char *buf,
  149. size_t count)
  150. {
  151. struct bus_type *bus = bus_get(drv->bus);
  152. struct device *dev;
  153. int err = -ENODEV;
  154. dev = bus_find_device_by_name(bus, NULL, buf);
  155. if (dev && dev->driver == drv) {
  156. if (dev->parent) /* Needed for USB */
  157. device_lock(dev->parent);
  158. device_release_driver(dev);
  159. if (dev->parent)
  160. device_unlock(dev->parent);
  161. err = count;
  162. }
  163. put_device(dev);
  164. bus_put(bus);
  165. return err;
  166. }
  167. static DRIVER_ATTR_WO(unbind);
  168. /*
  169. * Manually attach a device to a driver.
  170. * Note: the driver must want to bind to the device,
  171. * it is not possible to override the driver's id table.
  172. */
  173. static ssize_t bind_store(struct device_driver *drv, const char *buf,
  174. size_t count)
  175. {
  176. struct bus_type *bus = bus_get(drv->bus);
  177. struct device *dev;
  178. int err = -ENODEV;
  179. dev = bus_find_device_by_name(bus, NULL, buf);
  180. if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
  181. if (dev->parent) /* Needed for USB */
  182. device_lock(dev->parent);
  183. device_lock(dev);
  184. err = driver_probe_device(drv, dev);
  185. device_unlock(dev);
  186. if (dev->parent)
  187. device_unlock(dev->parent);
  188. if (err > 0) {
  189. /* success */
  190. err = count;
  191. } else if (err == 0) {
  192. /* driver didn't accept device */
  193. err = -ENODEV;
  194. }
  195. }
  196. put_device(dev);
  197. bus_put(bus);
  198. return err;
  199. }
  200. static DRIVER_ATTR_WO(bind);
  201. static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
  202. {
  203. return sprintf(buf, "%d\n", bus->p->drivers_autoprobe);
  204. }
  205. static ssize_t store_drivers_autoprobe(struct bus_type *bus,
  206. const char *buf, size_t count)
  207. {
  208. if (buf[0] == '0')
  209. bus->p->drivers_autoprobe = 0;
  210. else
  211. bus->p->drivers_autoprobe = 1;
  212. return count;
  213. }
  214. static ssize_t store_drivers_probe(struct bus_type *bus,
  215. const char *buf, size_t count)
  216. {
  217. struct device *dev;
  218. int err = -EINVAL;
  219. dev = bus_find_device_by_name(bus, NULL, buf);
  220. if (!dev)
  221. return -ENODEV;
  222. if (bus_rescan_devices_helper(dev, NULL) == 0)
  223. err = count;
  224. put_device(dev);
  225. return err;
  226. }
  227. static struct device *next_device(struct klist_iter *i)
  228. {
  229. struct klist_node *n = klist_next(i);
  230. struct device *dev = NULL;
  231. struct device_private *dev_prv;
  232. if (n) {
  233. dev_prv = to_device_private_bus(n);
  234. dev = dev_prv->device;
  235. }
  236. return dev;
  237. }
  238. /**
  239. * bus_for_each_dev - device iterator.
  240. * @bus: bus type.
  241. * @start: device to start iterating from.
  242. * @data: data for the callback.
  243. * @fn: function to be called for each device.
  244. *
  245. * Iterate over @bus's list of devices, and call @fn for each,
  246. * passing it @data. If @start is not NULL, we use that device to
  247. * begin iterating from.
  248. *
  249. * We check the return of @fn each time. If it returns anything
  250. * other than 0, we break out and return that value.
  251. *
  252. * NOTE: The device that returns a non-zero value is not retained
  253. * in any way, nor is its refcount incremented. If the caller needs
  254. * to retain this data, it should do so, and increment the reference
  255. * count in the supplied callback.
  256. */
  257. int bus_for_each_dev(struct bus_type *bus, struct device *start,
  258. void *data, int (*fn)(struct device *, void *))
  259. {
  260. struct klist_iter i;
  261. struct device *dev;
  262. int error = 0;
  263. if (!bus || !bus->p)
  264. return -EINVAL;
  265. klist_iter_init_node(&bus->p->klist_devices, &i,
  266. (start ? &start->p->knode_bus : NULL));
  267. while ((dev = next_device(&i)) && !error)
  268. error = fn(dev, data);
  269. klist_iter_exit(&i);
  270. return error;
  271. }
  272. EXPORT_SYMBOL_GPL(bus_for_each_dev);
  273. /**
  274. * bus_find_device - device iterator for locating a particular device.
  275. * @bus: bus type
  276. * @start: Device to begin with
  277. * @data: Data to pass to match function
  278. * @match: Callback function to check device
  279. *
  280. * This is similar to the bus_for_each_dev() function above, but it
  281. * returns a reference to a device that is 'found' for later use, as
  282. * determined by the @match callback.
  283. *
  284. * The callback should return 0 if the device doesn't match and non-zero
  285. * if it does. If the callback returns non-zero, this function will
  286. * return to the caller and not iterate over any more devices.
  287. */
  288. struct device *bus_find_device(struct bus_type *bus,
  289. struct device *start, void *data,
  290. int (*match)(struct device *dev, void *data))
  291. {
  292. struct klist_iter i;
  293. struct device *dev;
  294. if (!bus || !bus->p)
  295. return NULL;
  296. klist_iter_init_node(&bus->p->klist_devices, &i,
  297. (start ? &start->p->knode_bus : NULL));
  298. while ((dev = next_device(&i)))
  299. if (match(dev, data) && get_device(dev))
  300. break;
  301. klist_iter_exit(&i);
  302. return dev;
  303. }
  304. EXPORT_SYMBOL_GPL(bus_find_device);
  305. static int match_name(struct device *dev, void *data)
  306. {
  307. const char *name = data;
  308. return sysfs_streq(name, dev_name(dev));
  309. }
  310. /**
  311. * bus_find_device_by_name - device iterator for locating a particular device of a specific name
  312. * @bus: bus type
  313. * @start: Device to begin with
  314. * @name: name of the device to match
  315. *
  316. * This is similar to the bus_find_device() function above, but it handles
  317. * searching by a name automatically, no need to write another strcmp matching
  318. * function.
  319. */
  320. struct device *bus_find_device_by_name(struct bus_type *bus,
  321. struct device *start, const char *name)
  322. {
  323. return bus_find_device(bus, start, (void *)name, match_name);
  324. }
  325. EXPORT_SYMBOL_GPL(bus_find_device_by_name);
  326. /**
  327. * subsys_find_device_by_id - find a device with a specific enumeration number
  328. * @subsys: subsystem
  329. * @id: index 'id' in struct device
  330. * @hint: device to check first
  331. *
  332. * Check the hint's next object and if it is a match return it directly,
  333. * otherwise, fall back to a full list search. Either way a reference for
  334. * the returned object is taken.
  335. */
  336. struct device *subsys_find_device_by_id(struct bus_type *subsys, unsigned int id,
  337. struct device *hint)
  338. {
  339. struct klist_iter i;
  340. struct device *dev;
  341. if (!subsys)
  342. return NULL;
  343. if (hint) {
  344. klist_iter_init_node(&subsys->p->klist_devices, &i, &hint->p->knode_bus);
  345. dev = next_device(&i);
  346. if (dev && dev->id == id && get_device(dev)) {
  347. klist_iter_exit(&i);
  348. return dev;
  349. }
  350. klist_iter_exit(&i);
  351. }
  352. klist_iter_init_node(&subsys->p->klist_devices, &i, NULL);
  353. while ((dev = next_device(&i))) {
  354. if (dev->id == id && get_device(dev)) {
  355. klist_iter_exit(&i);
  356. return dev;
  357. }
  358. }
  359. klist_iter_exit(&i);
  360. return NULL;
  361. }
  362. EXPORT_SYMBOL_GPL(subsys_find_device_by_id);
  363. static struct device_driver *next_driver(struct klist_iter *i)
  364. {
  365. struct klist_node *n = klist_next(i);
  366. struct driver_private *drv_priv;
  367. if (n) {
  368. drv_priv = container_of(n, struct driver_private, knode_bus);
  369. return drv_priv->driver;
  370. }
  371. return NULL;
  372. }
  373. /**
  374. * bus_for_each_drv - driver iterator
  375. * @bus: bus we're dealing with.
  376. * @start: driver to start iterating on.
  377. * @data: data to pass to the callback.
  378. * @fn: function to call for each driver.
  379. *
  380. * This is nearly identical to the device iterator above.
  381. * We iterate over each driver that belongs to @bus, and call
  382. * @fn for each. If @fn returns anything but 0, we break out
  383. * and return it. If @start is not NULL, we use it as the head
  384. * of the list.
  385. *
  386. * NOTE: we don't return the driver that returns a non-zero
  387. * value, nor do we leave the reference count incremented for that
  388. * driver. If the caller needs to know that info, it must set it
  389. * in the callback. It must also be sure to increment the refcount
  390. * so it doesn't disappear before returning to the caller.
  391. */
  392. int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
  393. void *data, int (*fn)(struct device_driver *, void *))
  394. {
  395. struct klist_iter i;
  396. struct device_driver *drv;
  397. int error = 0;
  398. if (!bus)
  399. return -EINVAL;
  400. klist_iter_init_node(&bus->p->klist_drivers, &i,
  401. start ? &start->p->knode_bus : NULL);
  402. while ((drv = next_driver(&i)) && !error)
  403. error = fn(drv, data);
  404. klist_iter_exit(&i);
  405. return error;
  406. }
  407. EXPORT_SYMBOL_GPL(bus_for_each_drv);
  408. /**
  409. * bus_add_device - add device to bus
  410. * @dev: device being added
  411. *
  412. * - Add device's bus attributes.
  413. * - Create links to device's bus.
  414. * - Add the device to its bus's list of devices.
  415. */
  416. int bus_add_device(struct device *dev)
  417. {
  418. struct bus_type *bus = bus_get(dev->bus);
  419. int error = 0;
  420. if (bus) {
  421. pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
  422. error = device_add_groups(dev, bus->dev_groups);
  423. if (error)
  424. goto out_put;
  425. error = sysfs_create_link(&bus->p->devices_kset->kobj,
  426. &dev->kobj, dev_name(dev));
  427. if (error)
  428. goto out_groups;
  429. error = sysfs_create_link(&dev->kobj,
  430. &dev->bus->p->subsys.kobj, "subsystem");
  431. if (error)
  432. goto out_subsys;
  433. klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
  434. }
  435. return 0;
  436. out_subsys:
  437. sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
  438. out_groups:
  439. device_remove_groups(dev, bus->dev_groups);
  440. out_put:
  441. bus_put(dev->bus);
  442. return error;
  443. }
  444. /**
  445. * bus_probe_device - probe drivers for a new device
  446. * @dev: device to probe
  447. *
  448. * - Automatically probe for a driver if the bus allows it.
  449. */
  450. void bus_probe_device(struct device *dev)
  451. {
  452. struct bus_type *bus = dev->bus;
  453. struct subsys_interface *sif;
  454. if (!bus)
  455. return;
  456. if (bus->p->drivers_autoprobe)
  457. device_initial_probe(dev);
  458. mutex_lock(&bus->p->mutex);
  459. list_for_each_entry(sif, &bus->p->interfaces, node)
  460. if (sif->add_dev)
  461. sif->add_dev(dev, sif);
  462. mutex_unlock(&bus->p->mutex);
  463. }
  464. /**
  465. * bus_remove_device - remove device from bus
  466. * @dev: device to be removed
  467. *
  468. * - Remove device from all interfaces.
  469. * - Remove symlink from bus' directory.
  470. * - Delete device from bus's list.
  471. * - Detach from its driver.
  472. * - Drop reference taken in bus_add_device().
  473. */
  474. void bus_remove_device(struct device *dev)
  475. {
  476. struct bus_type *bus = dev->bus;
  477. struct subsys_interface *sif;
  478. if (!bus)
  479. return;
  480. mutex_lock(&bus->p->mutex);
  481. list_for_each_entry(sif, &bus->p->interfaces, node)
  482. if (sif->remove_dev)
  483. sif->remove_dev(dev, sif);
  484. mutex_unlock(&bus->p->mutex);
  485. sysfs_remove_link(&dev->kobj, "subsystem");
  486. sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
  487. dev_name(dev));
  488. device_remove_groups(dev, dev->bus->dev_groups);
  489. if (klist_node_attached(&dev->p->knode_bus))
  490. klist_del(&dev->p->knode_bus);
  491. pr_debug("bus: '%s': remove device %s\n",
  492. dev->bus->name, dev_name(dev));
  493. device_release_driver(dev);
  494. bus_put(dev->bus);
  495. }
  496. static int __must_check add_bind_files(struct device_driver *drv)
  497. {
  498. int ret;
  499. ret = driver_create_file(drv, &driver_attr_unbind);
  500. if (ret == 0) {
  501. ret = driver_create_file(drv, &driver_attr_bind);
  502. if (ret)
  503. driver_remove_file(drv, &driver_attr_unbind);
  504. }
  505. return ret;
  506. }
  507. static void remove_bind_files(struct device_driver *drv)
  508. {
  509. driver_remove_file(drv, &driver_attr_bind);
  510. driver_remove_file(drv, &driver_attr_unbind);
  511. }
  512. static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
  513. static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
  514. show_drivers_autoprobe, store_drivers_autoprobe);
  515. static int add_probe_files(struct bus_type *bus)
  516. {
  517. int retval;
  518. retval = bus_create_file(bus, &bus_attr_drivers_probe);
  519. if (retval)
  520. goto out;
  521. retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
  522. if (retval)
  523. bus_remove_file(bus, &bus_attr_drivers_probe);
  524. out:
  525. return retval;
  526. }
  527. static void remove_probe_files(struct bus_type *bus)
  528. {
  529. bus_remove_file(bus, &bus_attr_drivers_autoprobe);
  530. bus_remove_file(bus, &bus_attr_drivers_probe);
  531. }
  532. static ssize_t uevent_store(struct device_driver *drv, const char *buf,
  533. size_t count)
  534. {
  535. kobject_synth_uevent(&drv->p->kobj, buf, count);
  536. return count;
  537. }
  538. static DRIVER_ATTR_WO(uevent);
  539. static void driver_attach_async(void *_drv, async_cookie_t cookie)
  540. {
  541. struct device_driver *drv = _drv;
  542. int ret;
  543. ret = driver_attach(drv);
  544. pr_debug("bus: '%s': driver %s async attach completed: %d\n",
  545. drv->bus->name, drv->name, ret);
  546. }
  547. /**
  548. * bus_add_driver - Add a driver to the bus.
  549. * @drv: driver.
  550. */
  551. int bus_add_driver(struct device_driver *drv)
  552. {
  553. struct bus_type *bus;
  554. struct driver_private *priv;
  555. int error = 0;
  556. bus = bus_get(drv->bus);
  557. if (!bus)
  558. return -EINVAL;
  559. pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
  560. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  561. if (!priv) {
  562. error = -ENOMEM;
  563. goto out_put_bus;
  564. }
  565. klist_init(&priv->klist_devices, NULL, NULL);
  566. priv->driver = drv;
  567. drv->p = priv;
  568. priv->kobj.kset = bus->p->drivers_kset;
  569. error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
  570. "%s", drv->name);
  571. if (error)
  572. goto out_unregister;
  573. klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
  574. if (drv->bus->p->drivers_autoprobe) {
  575. if (driver_allows_async_probing(drv)) {
  576. pr_debug("bus: '%s': probing driver %s asynchronously\n",
  577. drv->bus->name, drv->name);
  578. async_schedule(driver_attach_async, drv);
  579. } else {
  580. error = driver_attach(drv);
  581. if (error)
  582. goto out_unregister;
  583. }
  584. }
  585. module_add_driver(drv->owner, drv);
  586. error = driver_create_file(drv, &driver_attr_uevent);
  587. if (error) {
  588. printk(KERN_ERR "%s: uevent attr (%s) failed\n",
  589. __func__, drv->name);
  590. }
  591. error = driver_add_groups(drv, bus->drv_groups);
  592. if (error) {
  593. /* How the hell do we get out of this pickle? Give up */
  594. printk(KERN_ERR "%s: driver_create_groups(%s) failed\n",
  595. __func__, drv->name);
  596. }
  597. if (!drv->suppress_bind_attrs) {
  598. error = add_bind_files(drv);
  599. if (error) {
  600. /* Ditto */
  601. printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
  602. __func__, drv->name);
  603. }
  604. }
  605. return 0;
  606. out_unregister:
  607. kobject_put(&priv->kobj);
  608. /* drv->p is freed in driver_release() */
  609. drv->p = NULL;
  610. out_put_bus:
  611. bus_put(bus);
  612. return error;
  613. }
  614. /**
  615. * bus_remove_driver - delete driver from bus's knowledge.
  616. * @drv: driver.
  617. *
  618. * Detach the driver from the devices it controls, and remove
  619. * it from its bus's list of drivers. Finally, we drop the reference
  620. * to the bus we took in bus_add_driver().
  621. */
  622. void bus_remove_driver(struct device_driver *drv)
  623. {
  624. if (!drv->bus)
  625. return;
  626. if (!drv->suppress_bind_attrs)
  627. remove_bind_files(drv);
  628. driver_remove_groups(drv, drv->bus->drv_groups);
  629. driver_remove_file(drv, &driver_attr_uevent);
  630. klist_remove(&drv->p->knode_bus);
  631. pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
  632. driver_detach(drv);
  633. module_remove_driver(drv);
  634. kobject_put(&drv->p->kobj);
  635. bus_put(drv->bus);
  636. }
  637. /* Helper for bus_rescan_devices's iter */
  638. static int __must_check bus_rescan_devices_helper(struct device *dev,
  639. void *data)
  640. {
  641. int ret = 0;
  642. if (!dev->driver) {
  643. if (dev->parent) /* Needed for USB */
  644. device_lock(dev->parent);
  645. ret = device_attach(dev);
  646. if (dev->parent)
  647. device_unlock(dev->parent);
  648. }
  649. return ret < 0 ? ret : 0;
  650. }
  651. /**
  652. * bus_rescan_devices - rescan devices on the bus for possible drivers
  653. * @bus: the bus to scan.
  654. *
  655. * This function will look for devices on the bus with no driver
  656. * attached and rescan it against existing drivers to see if it matches
  657. * any by calling device_attach() for the unbound devices.
  658. */
  659. int bus_rescan_devices(struct bus_type *bus)
  660. {
  661. return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
  662. }
  663. EXPORT_SYMBOL_GPL(bus_rescan_devices);
  664. /**
  665. * device_reprobe - remove driver for a device and probe for a new driver
  666. * @dev: the device to reprobe
  667. *
  668. * This function detaches the attached driver (if any) for the given
  669. * device and restarts the driver probing process. It is intended
  670. * to use if probing criteria changed during a devices lifetime and
  671. * driver attachment should change accordingly.
  672. */
  673. int device_reprobe(struct device *dev)
  674. {
  675. if (dev->driver) {
  676. if (dev->parent) /* Needed for USB */
  677. device_lock(dev->parent);
  678. device_release_driver(dev);
  679. if (dev->parent)
  680. device_unlock(dev->parent);
  681. }
  682. return bus_rescan_devices_helper(dev, NULL);
  683. }
  684. EXPORT_SYMBOL_GPL(device_reprobe);
  685. /**
  686. * find_bus - locate bus by name.
  687. * @name: name of bus.
  688. *
  689. * Call kset_find_obj() to iterate over list of buses to
  690. * find a bus by name. Return bus if found.
  691. *
  692. * Note that kset_find_obj increments bus' reference count.
  693. */
  694. #if 0
  695. struct bus_type *find_bus(char *name)
  696. {
  697. struct kobject *k = kset_find_obj(bus_kset, name);
  698. return k ? to_bus(k) : NULL;
  699. }
  700. #endif /* 0 */
  701. static int bus_add_groups(struct bus_type *bus,
  702. const struct attribute_group **groups)
  703. {
  704. return sysfs_create_groups(&bus->p->subsys.kobj, groups);
  705. }
  706. static void bus_remove_groups(struct bus_type *bus,
  707. const struct attribute_group **groups)
  708. {
  709. sysfs_remove_groups(&bus->p->subsys.kobj, groups);
  710. }
  711. static void klist_devices_get(struct klist_node *n)
  712. {
  713. struct device_private *dev_prv = to_device_private_bus(n);
  714. struct device *dev = dev_prv->device;
  715. get_device(dev);
  716. }
  717. static void klist_devices_put(struct klist_node *n)
  718. {
  719. struct device_private *dev_prv = to_device_private_bus(n);
  720. struct device *dev = dev_prv->device;
  721. put_device(dev);
  722. }
  723. static ssize_t bus_uevent_store(struct bus_type *bus,
  724. const char *buf, size_t count)
  725. {
  726. kobject_synth_uevent(&bus->p->subsys.kobj, buf, count);
  727. return count;
  728. }
  729. static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
  730. /**
  731. * bus_register - register a driver-core subsystem
  732. * @bus: bus to register
  733. *
  734. * Once we have that, we register the bus with the kobject
  735. * infrastructure, then register the children subsystems it has:
  736. * the devices and drivers that belong to the subsystem.
  737. */
  738. int bus_register(struct bus_type *bus)
  739. {
  740. int retval;
  741. struct subsys_private *priv;
  742. struct lock_class_key *key = &bus->lock_key;
  743. priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
  744. if (!priv)
  745. return -ENOMEM;
  746. priv->bus = bus;
  747. bus->p = priv;
  748. BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
  749. retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
  750. if (retval)
  751. goto out;
  752. priv->subsys.kobj.kset = bus_kset;
  753. priv->subsys.kobj.ktype = &bus_ktype;
  754. priv->drivers_autoprobe = 1;
  755. retval = kset_register(&priv->subsys);
  756. if (retval)
  757. goto out;
  758. retval = bus_create_file(bus, &bus_attr_uevent);
  759. if (retval)
  760. goto bus_uevent_fail;
  761. priv->devices_kset = kset_create_and_add("devices", NULL,
  762. &priv->subsys.kobj);
  763. if (!priv->devices_kset) {
  764. retval = -ENOMEM;
  765. goto bus_devices_fail;
  766. }
  767. priv->drivers_kset = kset_create_and_add("drivers", NULL,
  768. &priv->subsys.kobj);
  769. if (!priv->drivers_kset) {
  770. retval = -ENOMEM;
  771. goto bus_drivers_fail;
  772. }
  773. INIT_LIST_HEAD(&priv->interfaces);
  774. __mutex_init(&priv->mutex, "subsys mutex", key);
  775. klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
  776. klist_init(&priv->klist_drivers, NULL, NULL);
  777. retval = add_probe_files(bus);
  778. if (retval)
  779. goto bus_probe_files_fail;
  780. retval = bus_add_groups(bus, bus->bus_groups);
  781. if (retval)
  782. goto bus_groups_fail;
  783. pr_debug("bus: '%s': registered\n", bus->name);
  784. return 0;
  785. bus_groups_fail:
  786. remove_probe_files(bus);
  787. bus_probe_files_fail:
  788. kset_unregister(bus->p->drivers_kset);
  789. bus_drivers_fail:
  790. kset_unregister(bus->p->devices_kset);
  791. bus_devices_fail:
  792. bus_remove_file(bus, &bus_attr_uevent);
  793. bus_uevent_fail:
  794. kset_unregister(&bus->p->subsys);
  795. out:
  796. kfree(bus->p);
  797. bus->p = NULL;
  798. return retval;
  799. }
  800. EXPORT_SYMBOL_GPL(bus_register);
  801. /**
  802. * bus_unregister - remove a bus from the system
  803. * @bus: bus.
  804. *
  805. * Unregister the child subsystems and the bus itself.
  806. * Finally, we call bus_put() to release the refcount
  807. */
  808. void bus_unregister(struct bus_type *bus)
  809. {
  810. pr_debug("bus: '%s': unregistering\n", bus->name);
  811. if (bus->dev_root)
  812. device_unregister(bus->dev_root);
  813. bus_remove_groups(bus, bus->bus_groups);
  814. remove_probe_files(bus);
  815. kset_unregister(bus->p->drivers_kset);
  816. kset_unregister(bus->p->devices_kset);
  817. bus_remove_file(bus, &bus_attr_uevent);
  818. kset_unregister(&bus->p->subsys);
  819. }
  820. EXPORT_SYMBOL_GPL(bus_unregister);
  821. int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
  822. {
  823. return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
  824. }
  825. EXPORT_SYMBOL_GPL(bus_register_notifier);
  826. int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
  827. {
  828. return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
  829. }
  830. EXPORT_SYMBOL_GPL(bus_unregister_notifier);
  831. struct kset *bus_get_kset(struct bus_type *bus)
  832. {
  833. return &bus->p->subsys;
  834. }
  835. EXPORT_SYMBOL_GPL(bus_get_kset);
  836. struct klist *bus_get_device_klist(struct bus_type *bus)
  837. {
  838. return &bus->p->klist_devices;
  839. }
  840. EXPORT_SYMBOL_GPL(bus_get_device_klist);
  841. /*
  842. * Yes, this forcibly breaks the klist abstraction temporarily. It
  843. * just wants to sort the klist, not change reference counts and
  844. * take/drop locks rapidly in the process. It does all this while
  845. * holding the lock for the list, so objects can't otherwise be
  846. * added/removed while we're swizzling.
  847. */
  848. static void device_insertion_sort_klist(struct device *a, struct list_head *list,
  849. int (*compare)(const struct device *a,
  850. const struct device *b))
  851. {
  852. struct klist_node *n;
  853. struct device_private *dev_prv;
  854. struct device *b;
  855. list_for_each_entry(n, list, n_node) {
  856. dev_prv = to_device_private_bus(n);
  857. b = dev_prv->device;
  858. if (compare(a, b) <= 0) {
  859. list_move_tail(&a->p->knode_bus.n_node,
  860. &b->p->knode_bus.n_node);
  861. return;
  862. }
  863. }
  864. list_move_tail(&a->p->knode_bus.n_node, list);
  865. }
  866. void bus_sort_breadthfirst(struct bus_type *bus,
  867. int (*compare)(const struct device *a,
  868. const struct device *b))
  869. {
  870. LIST_HEAD(sorted_devices);
  871. struct klist_node *n, *tmp;
  872. struct device_private *dev_prv;
  873. struct device *dev;
  874. struct klist *device_klist;
  875. device_klist = bus_get_device_klist(bus);
  876. spin_lock(&device_klist->k_lock);
  877. list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) {
  878. dev_prv = to_device_private_bus(n);
  879. dev = dev_prv->device;
  880. device_insertion_sort_klist(dev, &sorted_devices, compare);
  881. }
  882. list_splice(&sorted_devices, &device_klist->k_list);
  883. spin_unlock(&device_klist->k_lock);
  884. }
  885. EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
  886. /**
  887. * subsys_dev_iter_init - initialize subsys device iterator
  888. * @iter: subsys iterator to initialize
  889. * @subsys: the subsys we wanna iterate over
  890. * @start: the device to start iterating from, if any
  891. * @type: device_type of the devices to iterate over, NULL for all
  892. *
  893. * Initialize subsys iterator @iter such that it iterates over devices
  894. * of @subsys. If @start is set, the list iteration will start there,
  895. * otherwise if it is NULL, the iteration starts at the beginning of
  896. * the list.
  897. */
  898. void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys,
  899. struct device *start, const struct device_type *type)
  900. {
  901. struct klist_node *start_knode = NULL;
  902. if (start)
  903. start_knode = &start->p->knode_bus;
  904. klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode);
  905. iter->type = type;
  906. }
  907. EXPORT_SYMBOL_GPL(subsys_dev_iter_init);
  908. /**
  909. * subsys_dev_iter_next - iterate to the next device
  910. * @iter: subsys iterator to proceed
  911. *
  912. * Proceed @iter to the next device and return it. Returns NULL if
  913. * iteration is complete.
  914. *
  915. * The returned device is referenced and won't be released till
  916. * iterator is proceed to the next device or exited. The caller is
  917. * free to do whatever it wants to do with the device including
  918. * calling back into subsys code.
  919. */
  920. struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
  921. {
  922. struct klist_node *knode;
  923. struct device *dev;
  924. for (;;) {
  925. knode = klist_next(&iter->ki);
  926. if (!knode)
  927. return NULL;
  928. dev = to_device_private_bus(knode)->device;
  929. if (!iter->type || iter->type == dev->type)
  930. return dev;
  931. }
  932. }
  933. EXPORT_SYMBOL_GPL(subsys_dev_iter_next);
  934. /**
  935. * subsys_dev_iter_exit - finish iteration
  936. * @iter: subsys iterator to finish
  937. *
  938. * Finish an iteration. Always call this function after iteration is
  939. * complete whether the iteration ran till the end or not.
  940. */
  941. void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
  942. {
  943. klist_iter_exit(&iter->ki);
  944. }
  945. EXPORT_SYMBOL_GPL(subsys_dev_iter_exit);
  946. int subsys_interface_register(struct subsys_interface *sif)
  947. {
  948. struct bus_type *subsys;
  949. struct subsys_dev_iter iter;
  950. struct device *dev;
  951. if (!sif || !sif->subsys)
  952. return -ENODEV;
  953. subsys = bus_get(sif->subsys);
  954. if (!subsys)
  955. return -EINVAL;
  956. mutex_lock(&subsys->p->mutex);
  957. list_add_tail(&sif->node, &subsys->p->interfaces);
  958. if (sif->add_dev) {
  959. subsys_dev_iter_init(&iter, subsys, NULL, NULL);
  960. while ((dev = subsys_dev_iter_next(&iter)))
  961. sif->add_dev(dev, sif);
  962. subsys_dev_iter_exit(&iter);
  963. }
  964. mutex_unlock(&subsys->p->mutex);
  965. return 0;
  966. }
  967. EXPORT_SYMBOL_GPL(subsys_interface_register);
  968. void subsys_interface_unregister(struct subsys_interface *sif)
  969. {
  970. struct bus_type *subsys;
  971. struct subsys_dev_iter iter;
  972. struct device *dev;
  973. if (!sif || !sif->subsys)
  974. return;
  975. subsys = sif->subsys;
  976. mutex_lock(&subsys->p->mutex);
  977. list_del_init(&sif->node);
  978. if (sif->remove_dev) {
  979. subsys_dev_iter_init(&iter, subsys, NULL, NULL);
  980. while ((dev = subsys_dev_iter_next(&iter)))
  981. sif->remove_dev(dev, sif);
  982. subsys_dev_iter_exit(&iter);
  983. }
  984. mutex_unlock(&subsys->p->mutex);
  985. bus_put(subsys);
  986. }
  987. EXPORT_SYMBOL_GPL(subsys_interface_unregister);
  988. static void system_root_device_release(struct device *dev)
  989. {
  990. kfree(dev);
  991. }
  992. static int subsys_register(struct bus_type *subsys,
  993. const struct attribute_group **groups,
  994. struct kobject *parent_of_root)
  995. {
  996. struct device *dev;
  997. int err;
  998. err = bus_register(subsys);
  999. if (err < 0)
  1000. return err;
  1001. dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  1002. if (!dev) {
  1003. err = -ENOMEM;
  1004. goto err_dev;
  1005. }
  1006. err = dev_set_name(dev, "%s", subsys->name);
  1007. if (err < 0)
  1008. goto err_name;
  1009. dev->kobj.parent = parent_of_root;
  1010. dev->groups = groups;
  1011. dev->release = system_root_device_release;
  1012. err = device_register(dev);
  1013. if (err < 0)
  1014. goto err_dev_reg;
  1015. subsys->dev_root = dev;
  1016. return 0;
  1017. err_dev_reg:
  1018. put_device(dev);
  1019. dev = NULL;
  1020. err_name:
  1021. kfree(dev);
  1022. err_dev:
  1023. bus_unregister(subsys);
  1024. return err;
  1025. }
  1026. /**
  1027. * subsys_system_register - register a subsystem at /sys/devices/system/
  1028. * @subsys: system subsystem
  1029. * @groups: default attributes for the root device
  1030. *
  1031. * All 'system' subsystems have a /sys/devices/system/<name> root device
  1032. * with the name of the subsystem. The root device can carry subsystem-
  1033. * wide attributes. All registered devices are below this single root
  1034. * device and are named after the subsystem with a simple enumeration
  1035. * number appended. The registered devices are not explicitly named;
  1036. * only 'id' in the device needs to be set.
  1037. *
  1038. * Do not use this interface for anything new, it exists for compatibility
  1039. * with bad ideas only. New subsystems should use plain subsystems; and
  1040. * add the subsystem-wide attributes should be added to the subsystem
  1041. * directory itself and not some create fake root-device placed in
  1042. * /sys/devices/system/<name>.
  1043. */
  1044. int subsys_system_register(struct bus_type *subsys,
  1045. const struct attribute_group **groups)
  1046. {
  1047. return subsys_register(subsys, groups, &system_kset->kobj);
  1048. }
  1049. EXPORT_SYMBOL_GPL(subsys_system_register);
  1050. /**
  1051. * subsys_virtual_register - register a subsystem at /sys/devices/virtual/
  1052. * @subsys: virtual subsystem
  1053. * @groups: default attributes for the root device
  1054. *
  1055. * All 'virtual' subsystems have a /sys/devices/system/<name> root device
  1056. * with the name of the subystem. The root device can carry subsystem-wide
  1057. * attributes. All registered devices are below this single root device.
  1058. * There's no restriction on device naming. This is for kernel software
  1059. * constructs which need sysfs interface.
  1060. */
  1061. int subsys_virtual_register(struct bus_type *subsys,
  1062. const struct attribute_group **groups)
  1063. {
  1064. struct kobject *virtual_dir;
  1065. virtual_dir = virtual_device_parent(NULL);
  1066. if (!virtual_dir)
  1067. return -ENOMEM;
  1068. return subsys_register(subsys, groups, virtual_dir);
  1069. }
  1070. EXPORT_SYMBOL_GPL(subsys_virtual_register);
  1071. int __init buses_init(void)
  1072. {
  1073. bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
  1074. if (!bus_kset)
  1075. return -ENOMEM;
  1076. system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
  1077. if (!system_kset)
  1078. return -ENOMEM;
  1079. return 0;
  1080. }