bus.c 32 KB

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