bus.c 32 KB

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