bus.c 31 KB

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