bus.c 32 KB

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