bus.c 32 KB

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