bus.c 32 KB

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