bus.c 32 KB

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