bus.c 31 KB

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