bus.c 32 KB

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