bus.c 32 KB

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