bus.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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/init.h>
  16. #include <linux/string.h>
  17. #include "base.h"
  18. #include "power/power.h"
  19. #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
  20. #define to_bus(obj) container_of(obj, struct bus_type_private, subsys.kobj)
  21. /*
  22. * sysfs bindings for drivers
  23. */
  24. #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
  25. static int __must_check bus_rescan_devices_helper(struct device *dev,
  26. void *data);
  27. static struct bus_type *bus_get(struct bus_type *bus)
  28. {
  29. if (bus) {
  30. kset_get(&bus->p->subsys);
  31. return bus;
  32. }
  33. return NULL;
  34. }
  35. static void bus_put(struct bus_type *bus)
  36. {
  37. if (bus)
  38. kset_put(&bus->p->subsys);
  39. }
  40. static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
  41. char *buf)
  42. {
  43. struct driver_attribute *drv_attr = to_drv_attr(attr);
  44. struct driver_private *drv_priv = to_driver(kobj);
  45. ssize_t ret = -EIO;
  46. if (drv_attr->show)
  47. ret = drv_attr->show(drv_priv->driver, buf);
  48. return ret;
  49. }
  50. static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
  51. const char *buf, size_t count)
  52. {
  53. struct driver_attribute *drv_attr = to_drv_attr(attr);
  54. struct driver_private *drv_priv = to_driver(kobj);
  55. ssize_t ret = -EIO;
  56. if (drv_attr->store)
  57. ret = drv_attr->store(drv_priv->driver, buf, count);
  58. return ret;
  59. }
  60. static struct sysfs_ops driver_sysfs_ops = {
  61. .show = drv_attr_show,
  62. .store = drv_attr_store,
  63. };
  64. static void driver_release(struct kobject *kobj)
  65. {
  66. struct driver_private *drv_priv = to_driver(kobj);
  67. pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
  68. kfree(drv_priv);
  69. }
  70. static struct kobj_type driver_ktype = {
  71. .sysfs_ops = &driver_sysfs_ops,
  72. .release = driver_release,
  73. };
  74. /*
  75. * sysfs bindings for buses
  76. */
  77. static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
  78. char *buf)
  79. {
  80. struct bus_attribute *bus_attr = to_bus_attr(attr);
  81. struct bus_type_private *bus_priv = to_bus(kobj);
  82. ssize_t ret = 0;
  83. if (bus_attr->show)
  84. ret = bus_attr->show(bus_priv->bus, buf);
  85. return ret;
  86. }
  87. static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
  88. const char *buf, size_t count)
  89. {
  90. struct bus_attribute *bus_attr = to_bus_attr(attr);
  91. struct bus_type_private *bus_priv = to_bus(kobj);
  92. ssize_t ret = 0;
  93. if (bus_attr->store)
  94. ret = bus_attr->store(bus_priv->bus, buf, count);
  95. return ret;
  96. }
  97. static struct sysfs_ops bus_sysfs_ops = {
  98. .show = bus_attr_show,
  99. .store = bus_attr_store,
  100. };
  101. int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)
  102. {
  103. int error;
  104. if (bus_get(bus)) {
  105. error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
  106. bus_put(bus);
  107. } else
  108. error = -EINVAL;
  109. return error;
  110. }
  111. EXPORT_SYMBOL_GPL(bus_create_file);
  112. void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)
  113. {
  114. if (bus_get(bus)) {
  115. sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
  116. bus_put(bus);
  117. }
  118. }
  119. EXPORT_SYMBOL_GPL(bus_remove_file);
  120. static struct kobj_type bus_ktype = {
  121. .sysfs_ops = &bus_sysfs_ops,
  122. };
  123. static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
  124. {
  125. struct kobj_type *ktype = get_ktype(kobj);
  126. if (ktype == &bus_ktype)
  127. return 1;
  128. return 0;
  129. }
  130. static struct kset_uevent_ops bus_uevent_ops = {
  131. .filter = bus_uevent_filter,
  132. };
  133. static struct kset *bus_kset;
  134. #ifdef CONFIG_HOTPLUG
  135. /* Manually detach a device from its associated driver. */
  136. static ssize_t driver_unbind(struct device_driver *drv,
  137. const char *buf, size_t count)
  138. {
  139. struct bus_type *bus = bus_get(drv->bus);
  140. struct device *dev;
  141. int err = -ENODEV;
  142. dev = bus_find_device_by_name(bus, NULL, buf);
  143. if (dev && dev->driver == drv) {
  144. if (dev->parent) /* Needed for USB */
  145. down(&dev->parent->sem);
  146. device_release_driver(dev);
  147. if (dev->parent)
  148. up(&dev->parent->sem);
  149. err = count;
  150. }
  151. put_device(dev);
  152. bus_put(bus);
  153. return err;
  154. }
  155. static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
  156. /*
  157. * Manually attach a device to a driver.
  158. * Note: the driver must want to bind to the device,
  159. * it is not possible to override the driver's id table.
  160. */
  161. static ssize_t driver_bind(struct device_driver *drv,
  162. const char *buf, size_t count)
  163. {
  164. struct bus_type *bus = bus_get(drv->bus);
  165. struct device *dev;
  166. int err = -ENODEV;
  167. dev = bus_find_device_by_name(bus, NULL, buf);
  168. if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
  169. if (dev->parent) /* Needed for USB */
  170. down(&dev->parent->sem);
  171. down(&dev->sem);
  172. err = driver_probe_device(drv, dev);
  173. up(&dev->sem);
  174. if (dev->parent)
  175. up(&dev->parent->sem);
  176. if (err > 0) {
  177. /* success */
  178. err = count;
  179. } else if (err == 0) {
  180. /* driver didn't accept device */
  181. err = -ENODEV;
  182. }
  183. }
  184. put_device(dev);
  185. bus_put(bus);
  186. return err;
  187. }
  188. static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
  189. static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
  190. {
  191. return sprintf(buf, "%d\n", bus->p->drivers_autoprobe);
  192. }
  193. static ssize_t store_drivers_autoprobe(struct bus_type *bus,
  194. const char *buf, size_t count)
  195. {
  196. if (buf[0] == '0')
  197. bus->p->drivers_autoprobe = 0;
  198. else
  199. bus->p->drivers_autoprobe = 1;
  200. return count;
  201. }
  202. static ssize_t store_drivers_probe(struct bus_type *bus,
  203. const char *buf, size_t count)
  204. {
  205. struct device *dev;
  206. dev = bus_find_device_by_name(bus, NULL, buf);
  207. if (!dev)
  208. return -ENODEV;
  209. if (bus_rescan_devices_helper(dev, NULL) != 0)
  210. return -EINVAL;
  211. return count;
  212. }
  213. #endif
  214. static struct device *next_device(struct klist_iter *i)
  215. {
  216. struct klist_node *n = klist_next(i);
  217. struct device *dev = NULL;
  218. struct device_private *dev_prv;
  219. if (n) {
  220. dev_prv = to_device_private_bus(n);
  221. dev = dev_prv->device;
  222. }
  223. return dev;
  224. }
  225. /**
  226. * bus_for_each_dev - device iterator.
  227. * @bus: bus type.
  228. * @start: device to start iterating from.
  229. * @data: data for the callback.
  230. * @fn: function to be called for each device.
  231. *
  232. * Iterate over @bus's list of devices, and call @fn for each,
  233. * passing it @data. If @start is not NULL, we use that device to
  234. * begin iterating from.
  235. *
  236. * We check the return of @fn each time. If it returns anything
  237. * other than 0, we break out and return that value.
  238. *
  239. * NOTE: The device that returns a non-zero value is not retained
  240. * in any way, nor is its refcount incremented. If the caller needs
  241. * to retain this data, it should do, and increment the reference
  242. * count in the supplied callback.
  243. */
  244. int bus_for_each_dev(struct bus_type *bus, struct device *start,
  245. void *data, int (*fn)(struct device *, void *))
  246. {
  247. struct klist_iter i;
  248. struct device *dev;
  249. int error = 0;
  250. if (!bus)
  251. return -EINVAL;
  252. klist_iter_init_node(&bus->p->klist_devices, &i,
  253. (start ? &start->p->knode_bus : NULL));
  254. while ((dev = next_device(&i)) && !error)
  255. error = fn(dev, data);
  256. klist_iter_exit(&i);
  257. return error;
  258. }
  259. EXPORT_SYMBOL_GPL(bus_for_each_dev);
  260. /**
  261. * bus_find_device - device iterator for locating a particular device.
  262. * @bus: bus type
  263. * @start: Device to begin with
  264. * @data: Data to pass to match function
  265. * @match: Callback function to check device
  266. *
  267. * This is similar to the bus_for_each_dev() function above, but it
  268. * returns a reference to a device that is 'found' for later use, as
  269. * determined by the @match callback.
  270. *
  271. * The callback should return 0 if the device doesn't match and non-zero
  272. * if it does. If the callback returns non-zero, this function will
  273. * return to the caller and not iterate over any more devices.
  274. */
  275. struct device *bus_find_device(struct bus_type *bus,
  276. struct device *start, void *data,
  277. int (*match)(struct device *dev, void *data))
  278. {
  279. struct klist_iter i;
  280. struct device *dev;
  281. if (!bus)
  282. return NULL;
  283. klist_iter_init_node(&bus->p->klist_devices, &i,
  284. (start ? &start->p->knode_bus : NULL));
  285. while ((dev = next_device(&i)))
  286. if (match(dev, data) && get_device(dev))
  287. break;
  288. klist_iter_exit(&i);
  289. return dev;
  290. }
  291. EXPORT_SYMBOL_GPL(bus_find_device);
  292. static int match_name(struct device *dev, void *data)
  293. {
  294. const char *name = data;
  295. return sysfs_streq(name, dev_name(dev));
  296. }
  297. /**
  298. * bus_find_device_by_name - device iterator for locating a particular device of a specific name
  299. * @bus: bus type
  300. * @start: Device to begin with
  301. * @name: name of the device to match
  302. *
  303. * This is similar to the bus_find_device() function above, but it handles
  304. * searching by a name automatically, no need to write another strcmp matching
  305. * function.
  306. */
  307. struct device *bus_find_device_by_name(struct bus_type *bus,
  308. struct device *start, const char *name)
  309. {
  310. return bus_find_device(bus, start, (void *)name, match_name);
  311. }
  312. EXPORT_SYMBOL_GPL(bus_find_device_by_name);
  313. static struct device_driver *next_driver(struct klist_iter *i)
  314. {
  315. struct klist_node *n = klist_next(i);
  316. struct driver_private *drv_priv;
  317. if (n) {
  318. drv_priv = container_of(n, struct driver_private, knode_bus);
  319. return drv_priv->driver;
  320. }
  321. return NULL;
  322. }
  323. /**
  324. * bus_for_each_drv - driver iterator
  325. * @bus: bus we're dealing with.
  326. * @start: driver to start iterating on.
  327. * @data: data to pass to the callback.
  328. * @fn: function to call for each driver.
  329. *
  330. * This is nearly identical to the device iterator above.
  331. * We iterate over each driver that belongs to @bus, and call
  332. * @fn for each. If @fn returns anything but 0, we break out
  333. * and return it. If @start is not NULL, we use it as the head
  334. * of the list.
  335. *
  336. * NOTE: we don't return the driver that returns a non-zero
  337. * value, nor do we leave the reference count incremented for that
  338. * driver. If the caller needs to know that info, it must set it
  339. * in the callback. It must also be sure to increment the refcount
  340. * so it doesn't disappear before returning to the caller.
  341. */
  342. int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
  343. void *data, int (*fn)(struct device_driver *, void *))
  344. {
  345. struct klist_iter i;
  346. struct device_driver *drv;
  347. int error = 0;
  348. if (!bus)
  349. return -EINVAL;
  350. klist_iter_init_node(&bus->p->klist_drivers, &i,
  351. start ? &start->p->knode_bus : NULL);
  352. while ((drv = next_driver(&i)) && !error)
  353. error = fn(drv, data);
  354. klist_iter_exit(&i);
  355. return error;
  356. }
  357. EXPORT_SYMBOL_GPL(bus_for_each_drv);
  358. static int device_add_attrs(struct bus_type *bus, struct device *dev)
  359. {
  360. int error = 0;
  361. int i;
  362. if (!bus->dev_attrs)
  363. return 0;
  364. for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
  365. error = device_create_file(dev, &bus->dev_attrs[i]);
  366. if (error) {
  367. while (--i >= 0)
  368. device_remove_file(dev, &bus->dev_attrs[i]);
  369. break;
  370. }
  371. }
  372. return error;
  373. }
  374. static void device_remove_attrs(struct bus_type *bus, struct device *dev)
  375. {
  376. int i;
  377. if (bus->dev_attrs) {
  378. for (i = 0; attr_name(bus->dev_attrs[i]); i++)
  379. device_remove_file(dev, &bus->dev_attrs[i]);
  380. }
  381. }
  382. #ifdef CONFIG_SYSFS_DEPRECATED
  383. static int make_deprecated_bus_links(struct device *dev)
  384. {
  385. return sysfs_create_link(&dev->kobj,
  386. &dev->bus->p->subsys.kobj, "bus");
  387. }
  388. static void remove_deprecated_bus_links(struct device *dev)
  389. {
  390. sysfs_remove_link(&dev->kobj, "bus");
  391. }
  392. #else
  393. static inline int make_deprecated_bus_links(struct device *dev) { return 0; }
  394. static inline void remove_deprecated_bus_links(struct device *dev) { }
  395. #endif
  396. /**
  397. * bus_add_device - add device to bus
  398. * @dev: device being added
  399. *
  400. * - Add the device to its bus's list of devices.
  401. * - Create link to device's bus.
  402. */
  403. int bus_add_device(struct device *dev)
  404. {
  405. struct bus_type *bus = bus_get(dev->bus);
  406. int error = 0;
  407. if (bus) {
  408. pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
  409. error = device_add_attrs(bus, dev);
  410. if (error)
  411. goto out_put;
  412. error = sysfs_create_link(&bus->p->devices_kset->kobj,
  413. &dev->kobj, dev_name(dev));
  414. if (error)
  415. goto out_id;
  416. error = sysfs_create_link(&dev->kobj,
  417. &dev->bus->p->subsys.kobj, "subsystem");
  418. if (error)
  419. goto out_subsys;
  420. error = make_deprecated_bus_links(dev);
  421. if (error)
  422. goto out_deprecated;
  423. }
  424. return 0;
  425. out_deprecated:
  426. sysfs_remove_link(&dev->kobj, "subsystem");
  427. out_subsys:
  428. sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
  429. out_id:
  430. device_remove_attrs(bus, dev);
  431. out_put:
  432. bus_put(dev->bus);
  433. return error;
  434. }
  435. /**
  436. * bus_attach_device - add device to bus
  437. * @dev: device tried to attach to a driver
  438. *
  439. * - Add device to bus's list of devices.
  440. * - Try to attach to driver.
  441. */
  442. void bus_attach_device(struct device *dev)
  443. {
  444. struct bus_type *bus = dev->bus;
  445. int ret = 0;
  446. if (bus) {
  447. if (bus->p->drivers_autoprobe)
  448. ret = device_attach(dev);
  449. WARN_ON(ret < 0);
  450. if (ret >= 0)
  451. klist_add_tail(&dev->p->knode_bus,
  452. &bus->p->klist_devices);
  453. }
  454. }
  455. /**
  456. * bus_remove_device - remove device from bus
  457. * @dev: device to be removed
  458. *
  459. * - Remove symlink from bus's directory.
  460. * - Delete device from bus's list.
  461. * - Detach from its driver.
  462. * - Drop reference taken in bus_add_device().
  463. */
  464. void bus_remove_device(struct device *dev)
  465. {
  466. if (dev->bus) {
  467. sysfs_remove_link(&dev->kobj, "subsystem");
  468. remove_deprecated_bus_links(dev);
  469. sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
  470. dev_name(dev));
  471. device_remove_attrs(dev->bus, dev);
  472. if (klist_node_attached(&dev->p->knode_bus))
  473. klist_del(&dev->p->knode_bus);
  474. pr_debug("bus: '%s': remove device %s\n",
  475. dev->bus->name, dev_name(dev));
  476. device_release_driver(dev);
  477. bus_put(dev->bus);
  478. }
  479. }
  480. static int driver_add_attrs(struct bus_type *bus, struct device_driver *drv)
  481. {
  482. int error = 0;
  483. int i;
  484. if (bus->drv_attrs) {
  485. for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
  486. error = driver_create_file(drv, &bus->drv_attrs[i]);
  487. if (error)
  488. goto err;
  489. }
  490. }
  491. done:
  492. return error;
  493. err:
  494. while (--i >= 0)
  495. driver_remove_file(drv, &bus->drv_attrs[i]);
  496. goto done;
  497. }
  498. static void driver_remove_attrs(struct bus_type *bus,
  499. struct device_driver *drv)
  500. {
  501. int i;
  502. if (bus->drv_attrs) {
  503. for (i = 0; attr_name(bus->drv_attrs[i]); i++)
  504. driver_remove_file(drv, &bus->drv_attrs[i]);
  505. }
  506. }
  507. #ifdef CONFIG_HOTPLUG
  508. /*
  509. * Thanks to drivers making their tables __devinit, we can't allow manual
  510. * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
  511. */
  512. static int __must_check add_bind_files(struct device_driver *drv)
  513. {
  514. int ret;
  515. ret = driver_create_file(drv, &driver_attr_unbind);
  516. if (ret == 0) {
  517. ret = driver_create_file(drv, &driver_attr_bind);
  518. if (ret)
  519. driver_remove_file(drv, &driver_attr_unbind);
  520. }
  521. return ret;
  522. }
  523. static void remove_bind_files(struct device_driver *drv)
  524. {
  525. driver_remove_file(drv, &driver_attr_bind);
  526. driver_remove_file(drv, &driver_attr_unbind);
  527. }
  528. static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
  529. static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
  530. show_drivers_autoprobe, store_drivers_autoprobe);
  531. static int add_probe_files(struct bus_type *bus)
  532. {
  533. int retval;
  534. retval = bus_create_file(bus, &bus_attr_drivers_probe);
  535. if (retval)
  536. goto out;
  537. retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
  538. if (retval)
  539. bus_remove_file(bus, &bus_attr_drivers_probe);
  540. out:
  541. return retval;
  542. }
  543. static void remove_probe_files(struct bus_type *bus)
  544. {
  545. bus_remove_file(bus, &bus_attr_drivers_autoprobe);
  546. bus_remove_file(bus, &bus_attr_drivers_probe);
  547. }
  548. #else
  549. static inline int add_bind_files(struct device_driver *drv) { return 0; }
  550. static inline void remove_bind_files(struct device_driver *drv) {}
  551. static inline int add_probe_files(struct bus_type *bus) { return 0; }
  552. static inline void remove_probe_files(struct bus_type *bus) {}
  553. #endif
  554. static ssize_t driver_uevent_store(struct device_driver *drv,
  555. const char *buf, size_t count)
  556. {
  557. enum kobject_action action;
  558. if (kobject_action_type(buf, count, &action) == 0)
  559. kobject_uevent(&drv->p->kobj, action);
  560. return count;
  561. }
  562. static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store);
  563. /**
  564. * bus_add_driver - Add a driver to the bus.
  565. * @drv: driver.
  566. */
  567. int bus_add_driver(struct device_driver *drv)
  568. {
  569. struct bus_type *bus;
  570. struct driver_private *priv;
  571. int error = 0;
  572. bus = bus_get(drv->bus);
  573. if (!bus)
  574. return -EINVAL;
  575. pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
  576. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  577. if (!priv) {
  578. error = -ENOMEM;
  579. goto out_put_bus;
  580. }
  581. klist_init(&priv->klist_devices, NULL, NULL);
  582. priv->driver = drv;
  583. drv->p = priv;
  584. priv->kobj.kset = bus->p->drivers_kset;
  585. error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
  586. "%s", drv->name);
  587. if (error)
  588. goto out_unregister;
  589. if (drv->bus->p->drivers_autoprobe) {
  590. error = driver_attach(drv);
  591. if (error)
  592. goto out_unregister;
  593. }
  594. klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
  595. module_add_driver(drv->owner, drv);
  596. error = driver_create_file(drv, &driver_attr_uevent);
  597. if (error) {
  598. printk(KERN_ERR "%s: uevent attr (%s) failed\n",
  599. __func__, drv->name);
  600. }
  601. error = driver_add_attrs(bus, drv);
  602. if (error) {
  603. /* How the hell do we get out of this pickle? Give up */
  604. printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
  605. __func__, drv->name);
  606. }
  607. error = add_bind_files(drv);
  608. if (error) {
  609. /* Ditto */
  610. printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
  611. __func__, drv->name);
  612. }
  613. kobject_uevent(&priv->kobj, KOBJ_ADD);
  614. return error;
  615. out_unregister:
  616. kobject_put(&priv->kobj);
  617. out_put_bus:
  618. bus_put(bus);
  619. return error;
  620. }
  621. /**
  622. * bus_remove_driver - delete driver from bus's knowledge.
  623. * @drv: driver.
  624. *
  625. * Detach the driver from the devices it controls, and remove
  626. * it from its bus's list of drivers. Finally, we drop the reference
  627. * to the bus we took in bus_add_driver().
  628. */
  629. void bus_remove_driver(struct device_driver *drv)
  630. {
  631. if (!drv->bus)
  632. return;
  633. remove_bind_files(drv);
  634. driver_remove_attrs(drv->bus, drv);
  635. driver_remove_file(drv, &driver_attr_uevent);
  636. klist_remove(&drv->p->knode_bus);
  637. pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
  638. driver_detach(drv);
  639. module_remove_driver(drv);
  640. kobject_put(&drv->p->kobj);
  641. bus_put(drv->bus);
  642. }
  643. /* Helper for bus_rescan_devices's iter */
  644. static int __must_check bus_rescan_devices_helper(struct device *dev,
  645. void *data)
  646. {
  647. int ret = 0;
  648. if (!dev->driver) {
  649. if (dev->parent) /* Needed for USB */
  650. down(&dev->parent->sem);
  651. ret = device_attach(dev);
  652. if (dev->parent)
  653. up(&dev->parent->sem);
  654. }
  655. return ret < 0 ? ret : 0;
  656. }
  657. /**
  658. * bus_rescan_devices - rescan devices on the bus for possible drivers
  659. * @bus: the bus to scan.
  660. *
  661. * This function will look for devices on the bus with no driver
  662. * attached and rescan it against existing drivers to see if it matches
  663. * any by calling device_attach() for the unbound devices.
  664. */
  665. int bus_rescan_devices(struct bus_type *bus)
  666. {
  667. return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
  668. }
  669. EXPORT_SYMBOL_GPL(bus_rescan_devices);
  670. /**
  671. * device_reprobe - remove driver for a device and probe for a new driver
  672. * @dev: the device to reprobe
  673. *
  674. * This function detaches the attached driver (if any) for the given
  675. * device and restarts the driver probing process. It is intended
  676. * to use if probing criteria changed during a devices lifetime and
  677. * driver attachment should change accordingly.
  678. */
  679. int device_reprobe(struct device *dev)
  680. {
  681. if (dev->driver) {
  682. if (dev->parent) /* Needed for USB */
  683. down(&dev->parent->sem);
  684. device_release_driver(dev);
  685. if (dev->parent)
  686. up(&dev->parent->sem);
  687. }
  688. return bus_rescan_devices_helper(dev, NULL);
  689. }
  690. EXPORT_SYMBOL_GPL(device_reprobe);
  691. /**
  692. * find_bus - locate bus by name.
  693. * @name: name of bus.
  694. *
  695. * Call kset_find_obj() to iterate over list of buses to
  696. * find a bus by name. Return bus if found.
  697. *
  698. * Note that kset_find_obj increments bus' reference count.
  699. */
  700. #if 0
  701. struct bus_type *find_bus(char *name)
  702. {
  703. struct kobject *k = kset_find_obj(bus_kset, name);
  704. return k ? to_bus(k) : NULL;
  705. }
  706. #endif /* 0 */
  707. /**
  708. * bus_add_attrs - Add default attributes for this bus.
  709. * @bus: Bus that has just been registered.
  710. */
  711. static int bus_add_attrs(struct bus_type *bus)
  712. {
  713. int error = 0;
  714. int i;
  715. if (bus->bus_attrs) {
  716. for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
  717. error = bus_create_file(bus, &bus->bus_attrs[i]);
  718. if (error)
  719. goto err;
  720. }
  721. }
  722. done:
  723. return error;
  724. err:
  725. while (--i >= 0)
  726. bus_remove_file(bus, &bus->bus_attrs[i]);
  727. goto done;
  728. }
  729. static void bus_remove_attrs(struct bus_type *bus)
  730. {
  731. int i;
  732. if (bus->bus_attrs) {
  733. for (i = 0; attr_name(bus->bus_attrs[i]); i++)
  734. bus_remove_file(bus, &bus->bus_attrs[i]);
  735. }
  736. }
  737. static void klist_devices_get(struct klist_node *n)
  738. {
  739. struct device_private *dev_prv = to_device_private_bus(n);
  740. struct device *dev = dev_prv->device;
  741. get_device(dev);
  742. }
  743. static void klist_devices_put(struct klist_node *n)
  744. {
  745. struct device_private *dev_prv = to_device_private_bus(n);
  746. struct device *dev = dev_prv->device;
  747. put_device(dev);
  748. }
  749. static ssize_t bus_uevent_store(struct bus_type *bus,
  750. const char *buf, size_t count)
  751. {
  752. enum kobject_action action;
  753. if (kobject_action_type(buf, count, &action) == 0)
  754. kobject_uevent(&bus->p->subsys.kobj, action);
  755. return count;
  756. }
  757. static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
  758. /**
  759. * bus_register - register a bus with the system.
  760. * @bus: bus.
  761. *
  762. * Once we have that, we registered the bus with the kobject
  763. * infrastructure, then register the children subsystems it has:
  764. * the devices and drivers that belong to the bus.
  765. */
  766. int bus_register(struct bus_type *bus)
  767. {
  768. int retval;
  769. struct bus_type_private *priv;
  770. priv = kzalloc(sizeof(struct bus_type_private), GFP_KERNEL);
  771. if (!priv)
  772. return -ENOMEM;
  773. priv->bus = bus;
  774. bus->p = priv;
  775. BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
  776. retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
  777. if (retval)
  778. goto out;
  779. priv->subsys.kobj.kset = bus_kset;
  780. priv->subsys.kobj.ktype = &bus_ktype;
  781. priv->drivers_autoprobe = 1;
  782. retval = kset_register(&priv->subsys);
  783. if (retval)
  784. goto out;
  785. retval = bus_create_file(bus, &bus_attr_uevent);
  786. if (retval)
  787. goto bus_uevent_fail;
  788. priv->devices_kset = kset_create_and_add("devices", NULL,
  789. &priv->subsys.kobj);
  790. if (!priv->devices_kset) {
  791. retval = -ENOMEM;
  792. goto bus_devices_fail;
  793. }
  794. priv->drivers_kset = kset_create_and_add("drivers", NULL,
  795. &priv->subsys.kobj);
  796. if (!priv->drivers_kset) {
  797. retval = -ENOMEM;
  798. goto bus_drivers_fail;
  799. }
  800. klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
  801. klist_init(&priv->klist_drivers, NULL, NULL);
  802. retval = add_probe_files(bus);
  803. if (retval)
  804. goto bus_probe_files_fail;
  805. retval = bus_add_attrs(bus);
  806. if (retval)
  807. goto bus_attrs_fail;
  808. pr_debug("bus: '%s': registered\n", bus->name);
  809. return 0;
  810. bus_attrs_fail:
  811. remove_probe_files(bus);
  812. bus_probe_files_fail:
  813. kset_unregister(bus->p->drivers_kset);
  814. bus_drivers_fail:
  815. kset_unregister(bus->p->devices_kset);
  816. bus_devices_fail:
  817. bus_remove_file(bus, &bus_attr_uevent);
  818. bus_uevent_fail:
  819. kset_unregister(&bus->p->subsys);
  820. kfree(bus->p);
  821. out:
  822. bus->p = NULL;
  823. return retval;
  824. }
  825. EXPORT_SYMBOL_GPL(bus_register);
  826. /**
  827. * bus_unregister - remove a bus from the system
  828. * @bus: bus.
  829. *
  830. * Unregister the child subsystems and the bus itself.
  831. * Finally, we call bus_put() to release the refcount
  832. */
  833. void bus_unregister(struct bus_type *bus)
  834. {
  835. pr_debug("bus: '%s': unregistering\n", bus->name);
  836. bus_remove_attrs(bus);
  837. remove_probe_files(bus);
  838. kset_unregister(bus->p->drivers_kset);
  839. kset_unregister(bus->p->devices_kset);
  840. bus_remove_file(bus, &bus_attr_uevent);
  841. kset_unregister(&bus->p->subsys);
  842. kfree(bus->p);
  843. bus->p = NULL;
  844. }
  845. EXPORT_SYMBOL_GPL(bus_unregister);
  846. int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
  847. {
  848. return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
  849. }
  850. EXPORT_SYMBOL_GPL(bus_register_notifier);
  851. int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
  852. {
  853. return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
  854. }
  855. EXPORT_SYMBOL_GPL(bus_unregister_notifier);
  856. struct kset *bus_get_kset(struct bus_type *bus)
  857. {
  858. return &bus->p->subsys;
  859. }
  860. EXPORT_SYMBOL_GPL(bus_get_kset);
  861. struct klist *bus_get_device_klist(struct bus_type *bus)
  862. {
  863. return &bus->p->klist_devices;
  864. }
  865. EXPORT_SYMBOL_GPL(bus_get_device_klist);
  866. /*
  867. * Yes, this forcably breaks the klist abstraction temporarily. It
  868. * just wants to sort the klist, not change reference counts and
  869. * take/drop locks rapidly in the process. It does all this while
  870. * holding the lock for the list, so objects can't otherwise be
  871. * added/removed while we're swizzling.
  872. */
  873. static void device_insertion_sort_klist(struct device *a, struct list_head *list,
  874. int (*compare)(const struct device *a,
  875. const struct device *b))
  876. {
  877. struct list_head *pos;
  878. struct klist_node *n;
  879. struct device_private *dev_prv;
  880. struct device *b;
  881. list_for_each(pos, list) {
  882. n = container_of(pos, struct klist_node, n_node);
  883. dev_prv = to_device_private_bus(n);
  884. b = dev_prv->device;
  885. if (compare(a, b) <= 0) {
  886. list_move_tail(&a->p->knode_bus.n_node,
  887. &b->p->knode_bus.n_node);
  888. return;
  889. }
  890. }
  891. list_move_tail(&a->p->knode_bus.n_node, list);
  892. }
  893. void bus_sort_breadthfirst(struct bus_type *bus,
  894. int (*compare)(const struct device *a,
  895. const struct device *b))
  896. {
  897. LIST_HEAD(sorted_devices);
  898. struct list_head *pos, *tmp;
  899. struct klist_node *n;
  900. struct device_private *dev_prv;
  901. struct device *dev;
  902. struct klist *device_klist;
  903. device_klist = bus_get_device_klist(bus);
  904. spin_lock(&device_klist->k_lock);
  905. list_for_each_safe(pos, tmp, &device_klist->k_list) {
  906. n = container_of(pos, struct klist_node, n_node);
  907. dev_prv = to_device_private_bus(n);
  908. dev = dev_prv->device;
  909. device_insertion_sort_klist(dev, &sorted_devices, compare);
  910. }
  911. list_splice(&sorted_devices, &device_klist->k_list);
  912. spin_unlock(&device_klist->k_lock);
  913. }
  914. EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
  915. int __init buses_init(void)
  916. {
  917. bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
  918. if (!bus_kset)
  919. return -ENOMEM;
  920. return 0;
  921. }