bus.c 32 KB

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