bus.c 32 KB

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