bus.c 33 KB

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