bus.c 32 KB

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