bus.c 33 KB

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