bus.c 32 KB

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