bus.c 32 KB

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