bus.c 32 KB

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