core.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. /*
  2. * drivers/base/core.c - core driver model code (device registration, etc)
  3. *
  4. * Copyright (c) 2002-3 Patrick Mochel
  5. * Copyright (c) 2002-3 Open Source Development Labs
  6. * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
  7. * Copyright (c) 2006 Novell, Inc.
  8. *
  9. * This file is released under the GPLv2
  10. *
  11. */
  12. #include <linux/device.h>
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <linux/kdev_t.h>
  19. #include <linux/notifier.h>
  20. #include <asm/semaphore.h>
  21. #include "base.h"
  22. #include "power/power.h"
  23. int (*platform_notify)(struct device * dev) = NULL;
  24. int (*platform_notify_remove)(struct device * dev) = NULL;
  25. /*
  26. * sysfs bindings for devices.
  27. */
  28. /**
  29. * dev_driver_string - Return a device's driver name, if at all possible
  30. * @dev: struct device to get the name of
  31. *
  32. * Will return the device's driver's name if it is bound to a device. If
  33. * the device is not bound to a device, it will return the name of the bus
  34. * it is attached to. If it is not attached to a bus either, an empty
  35. * string will be returned.
  36. */
  37. const char *dev_driver_string(struct device *dev)
  38. {
  39. return dev->driver ? dev->driver->name :
  40. (dev->bus ? dev->bus->name : "");
  41. }
  42. EXPORT_SYMBOL(dev_driver_string);
  43. #define to_dev(obj) container_of(obj, struct device, kobj)
  44. #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
  45. static ssize_t
  46. dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
  47. {
  48. struct device_attribute * dev_attr = to_dev_attr(attr);
  49. struct device * dev = to_dev(kobj);
  50. ssize_t ret = -EIO;
  51. if (dev_attr->show)
  52. ret = dev_attr->show(dev, dev_attr, buf);
  53. return ret;
  54. }
  55. static ssize_t
  56. dev_attr_store(struct kobject * kobj, struct attribute * attr,
  57. const char * buf, size_t count)
  58. {
  59. struct device_attribute * dev_attr = to_dev_attr(attr);
  60. struct device * dev = to_dev(kobj);
  61. ssize_t ret = -EIO;
  62. if (dev_attr->store)
  63. ret = dev_attr->store(dev, dev_attr, buf, count);
  64. return ret;
  65. }
  66. static struct sysfs_ops dev_sysfs_ops = {
  67. .show = dev_attr_show,
  68. .store = dev_attr_store,
  69. };
  70. /**
  71. * device_release - free device structure.
  72. * @kobj: device's kobject.
  73. *
  74. * This is called once the reference count for the object
  75. * reaches 0. We forward the call to the device's release
  76. * method, which should handle actually freeing the structure.
  77. */
  78. static void device_release(struct kobject * kobj)
  79. {
  80. struct device * dev = to_dev(kobj);
  81. if (dev->release)
  82. dev->release(dev);
  83. else if (dev->class && dev->class->dev_release)
  84. dev->class->dev_release(dev);
  85. else {
  86. printk(KERN_ERR "Device '%s' does not have a release() function, "
  87. "it is broken and must be fixed.\n",
  88. dev->bus_id);
  89. WARN_ON(1);
  90. }
  91. }
  92. static struct kobj_type ktype_device = {
  93. .release = device_release,
  94. .sysfs_ops = &dev_sysfs_ops,
  95. };
  96. static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
  97. {
  98. struct kobj_type *ktype = get_ktype(kobj);
  99. if (ktype == &ktype_device) {
  100. struct device *dev = to_dev(kobj);
  101. if (dev->bus)
  102. return 1;
  103. if (dev->class)
  104. return 1;
  105. }
  106. return 0;
  107. }
  108. static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
  109. {
  110. struct device *dev = to_dev(kobj);
  111. if (dev->bus)
  112. return dev->bus->name;
  113. if (dev->class)
  114. return dev->class->name;
  115. return NULL;
  116. }
  117. static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
  118. int num_envp, char *buffer, int buffer_size)
  119. {
  120. struct device *dev = to_dev(kobj);
  121. int i = 0;
  122. int length = 0;
  123. int retval = 0;
  124. /* add the major/minor if present */
  125. if (MAJOR(dev->devt)) {
  126. add_uevent_var(envp, num_envp, &i,
  127. buffer, buffer_size, &length,
  128. "MAJOR=%u", MAJOR(dev->devt));
  129. add_uevent_var(envp, num_envp, &i,
  130. buffer, buffer_size, &length,
  131. "MINOR=%u", MINOR(dev->devt));
  132. }
  133. #ifdef CONFIG_SYSFS_DEPRECATED
  134. /* add bus name (same as SUBSYSTEM, deprecated) */
  135. if (dev->bus)
  136. add_uevent_var(envp, num_envp, &i,
  137. buffer, buffer_size, &length,
  138. "PHYSDEVBUS=%s", dev->bus->name);
  139. #endif
  140. /* add driver name (PHYSDEV* values are deprecated)*/
  141. if (dev->driver) {
  142. add_uevent_var(envp, num_envp, &i,
  143. buffer, buffer_size, &length,
  144. "DRIVER=%s", dev->driver->name);
  145. #ifdef CONFIG_SYSFS_DEPRECATED
  146. add_uevent_var(envp, num_envp, &i,
  147. buffer, buffer_size, &length,
  148. "PHYSDEVDRIVER=%s", dev->driver->name);
  149. #endif
  150. }
  151. /* terminate, set to next free slot, shrink available space */
  152. envp[i] = NULL;
  153. envp = &envp[i];
  154. num_envp -= i;
  155. buffer = &buffer[length];
  156. buffer_size -= length;
  157. if (dev->bus && dev->bus->uevent) {
  158. /* have the bus specific function add its stuff */
  159. retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
  160. if (retval) {
  161. pr_debug ("%s - uevent() returned %d\n",
  162. __FUNCTION__, retval);
  163. }
  164. }
  165. if (dev->class && dev->class->dev_uevent) {
  166. /* have the class specific function add its stuff */
  167. retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
  168. if (retval) {
  169. pr_debug("%s - dev_uevent() returned %d\n",
  170. __FUNCTION__, retval);
  171. }
  172. }
  173. return retval;
  174. }
  175. static struct kset_uevent_ops device_uevent_ops = {
  176. .filter = dev_uevent_filter,
  177. .name = dev_uevent_name,
  178. .uevent = dev_uevent,
  179. };
  180. static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
  181. const char *buf, size_t count)
  182. {
  183. kobject_uevent(&dev->kobj, KOBJ_ADD);
  184. return count;
  185. }
  186. static int device_add_groups(struct device *dev)
  187. {
  188. int i;
  189. int error = 0;
  190. if (dev->groups) {
  191. for (i = 0; dev->groups[i]; i++) {
  192. error = sysfs_create_group(&dev->kobj, dev->groups[i]);
  193. if (error) {
  194. while (--i >= 0)
  195. sysfs_remove_group(&dev->kobj, dev->groups[i]);
  196. goto out;
  197. }
  198. }
  199. }
  200. out:
  201. return error;
  202. }
  203. static void device_remove_groups(struct device *dev)
  204. {
  205. int i;
  206. if (dev->groups) {
  207. for (i = 0; dev->groups[i]; i++) {
  208. sysfs_remove_group(&dev->kobj, dev->groups[i]);
  209. }
  210. }
  211. }
  212. static int device_add_attrs(struct device *dev)
  213. {
  214. struct class *class = dev->class;
  215. int error = 0;
  216. int i;
  217. if (!class)
  218. return 0;
  219. if (class->dev_attrs) {
  220. for (i = 0; attr_name(class->dev_attrs[i]); i++) {
  221. error = device_create_file(dev, &class->dev_attrs[i]);
  222. if (error)
  223. break;
  224. }
  225. }
  226. if (error)
  227. while (--i >= 0)
  228. device_remove_file(dev, &class->dev_attrs[i]);
  229. return error;
  230. }
  231. static void device_remove_attrs(struct device *dev)
  232. {
  233. struct class *class = dev->class;
  234. int i;
  235. if (!class)
  236. return;
  237. if (class->dev_attrs) {
  238. for (i = 0; attr_name(class->dev_attrs[i]); i++)
  239. device_remove_file(dev, &class->dev_attrs[i]);
  240. }
  241. }
  242. static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
  243. char *buf)
  244. {
  245. return print_dev_t(buf, dev->devt);
  246. }
  247. /*
  248. * devices_subsys - structure to be registered with kobject core.
  249. */
  250. decl_subsys(devices, &ktype_device, &device_uevent_ops);
  251. /**
  252. * device_create_file - create sysfs attribute file for device.
  253. * @dev: device.
  254. * @attr: device attribute descriptor.
  255. */
  256. int device_create_file(struct device * dev, struct device_attribute * attr)
  257. {
  258. int error = 0;
  259. if (get_device(dev)) {
  260. error = sysfs_create_file(&dev->kobj, &attr->attr);
  261. put_device(dev);
  262. }
  263. return error;
  264. }
  265. /**
  266. * device_remove_file - remove sysfs attribute file.
  267. * @dev: device.
  268. * @attr: device attribute descriptor.
  269. */
  270. void device_remove_file(struct device * dev, struct device_attribute * attr)
  271. {
  272. if (get_device(dev)) {
  273. sysfs_remove_file(&dev->kobj, &attr->attr);
  274. put_device(dev);
  275. }
  276. }
  277. /**
  278. * device_create_bin_file - create sysfs binary attribute file for device.
  279. * @dev: device.
  280. * @attr: device binary attribute descriptor.
  281. */
  282. int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
  283. {
  284. int error = -EINVAL;
  285. if (dev)
  286. error = sysfs_create_bin_file(&dev->kobj, attr);
  287. return error;
  288. }
  289. EXPORT_SYMBOL_GPL(device_create_bin_file);
  290. /**
  291. * device_remove_bin_file - remove sysfs binary attribute file
  292. * @dev: device.
  293. * @attr: device binary attribute descriptor.
  294. */
  295. void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
  296. {
  297. if (dev)
  298. sysfs_remove_bin_file(&dev->kobj, attr);
  299. }
  300. EXPORT_SYMBOL_GPL(device_remove_bin_file);
  301. static void klist_children_get(struct klist_node *n)
  302. {
  303. struct device *dev = container_of(n, struct device, knode_parent);
  304. get_device(dev);
  305. }
  306. static void klist_children_put(struct klist_node *n)
  307. {
  308. struct device *dev = container_of(n, struct device, knode_parent);
  309. put_device(dev);
  310. }
  311. /**
  312. * device_initialize - init device structure.
  313. * @dev: device.
  314. *
  315. * This prepares the device for use by other layers,
  316. * including adding it to the device hierarchy.
  317. * It is the first half of device_register(), if called by
  318. * that, though it can also be called separately, so one
  319. * may use @dev's fields (e.g. the refcount).
  320. */
  321. void device_initialize(struct device *dev)
  322. {
  323. kobj_set_kset_s(dev, devices_subsys);
  324. kobject_init(&dev->kobj);
  325. klist_init(&dev->klist_children, klist_children_get,
  326. klist_children_put);
  327. INIT_LIST_HEAD(&dev->dma_pools);
  328. INIT_LIST_HEAD(&dev->node);
  329. init_MUTEX(&dev->sem);
  330. device_init_wakeup(dev, 0);
  331. set_dev_node(dev, -1);
  332. }
  333. #ifdef CONFIG_SYSFS_DEPRECATED
  334. static int setup_parent(struct device *dev, struct device *parent)
  335. {
  336. /* Set the parent to the class, not the parent device */
  337. /* this keeps sysfs from having a symlink to make old udevs happy */
  338. if (dev->class)
  339. dev->kobj.parent = &dev->class->subsys.kset.kobj;
  340. else if (parent)
  341. dev->kobj.parent = &parent->kobj;
  342. return 0;
  343. }
  344. #else
  345. static int virtual_device_parent(struct device *dev)
  346. {
  347. if (!dev->class)
  348. return -ENODEV;
  349. if (!dev->class->virtual_dir) {
  350. static struct kobject *virtual_dir = NULL;
  351. if (!virtual_dir)
  352. virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
  353. dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name);
  354. }
  355. dev->kobj.parent = dev->class->virtual_dir;
  356. return 0;
  357. }
  358. static int setup_parent(struct device *dev, struct device *parent)
  359. {
  360. int error;
  361. /* if this is a class device, and has no parent, create one */
  362. if ((dev->class) && (parent == NULL)) {
  363. error = virtual_device_parent(dev);
  364. if (error)
  365. return error;
  366. } else if (parent)
  367. dev->kobj.parent = &parent->kobj;
  368. return 0;
  369. }
  370. #endif
  371. /**
  372. * device_add - add device to device hierarchy.
  373. * @dev: device.
  374. *
  375. * This is part 2 of device_register(), though may be called
  376. * separately _iff_ device_initialize() has been called separately.
  377. *
  378. * This adds it to the kobject hierarchy via kobject_add(), adds it
  379. * to the global and sibling lists for the device, then
  380. * adds it to the other relevant subsystems of the driver model.
  381. */
  382. int device_add(struct device *dev)
  383. {
  384. struct device *parent = NULL;
  385. char *class_name = NULL;
  386. struct class_interface *class_intf;
  387. int error = -EINVAL;
  388. dev = get_device(dev);
  389. if (!dev || !strlen(dev->bus_id))
  390. goto Error;
  391. pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
  392. parent = get_device(dev->parent);
  393. error = setup_parent(dev, parent);
  394. if (error)
  395. goto Error;
  396. /* first, register with generic layer. */
  397. kobject_set_name(&dev->kobj, "%s", dev->bus_id);
  398. error = kobject_add(&dev->kobj);
  399. if (error)
  400. goto Error;
  401. /* notify platform of device entry */
  402. if (platform_notify)
  403. platform_notify(dev);
  404. /* notify clients of device entry (new way) */
  405. if (dev->bus)
  406. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  407. BUS_NOTIFY_ADD_DEVICE, dev);
  408. dev->uevent_attr.attr.name = "uevent";
  409. dev->uevent_attr.attr.mode = S_IWUSR;
  410. if (dev->driver)
  411. dev->uevent_attr.attr.owner = dev->driver->owner;
  412. dev->uevent_attr.store = store_uevent;
  413. error = device_create_file(dev, &dev->uevent_attr);
  414. if (error)
  415. goto attrError;
  416. if (MAJOR(dev->devt)) {
  417. struct device_attribute *attr;
  418. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  419. if (!attr) {
  420. error = -ENOMEM;
  421. goto ueventattrError;
  422. }
  423. attr->attr.name = "dev";
  424. attr->attr.mode = S_IRUGO;
  425. if (dev->driver)
  426. attr->attr.owner = dev->driver->owner;
  427. attr->show = show_dev;
  428. error = device_create_file(dev, attr);
  429. if (error) {
  430. kfree(attr);
  431. goto ueventattrError;
  432. }
  433. dev->devt_attr = attr;
  434. }
  435. if (dev->class) {
  436. sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
  437. "subsystem");
  438. /* If this is not a "fake" compatible device, then create the
  439. * symlink from the class to the device. */
  440. if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
  441. sysfs_create_link(&dev->class->subsys.kset.kobj,
  442. &dev->kobj, dev->bus_id);
  443. #ifdef CONFIG_SYSFS_DEPRECATED
  444. if (parent) {
  445. sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
  446. class_name = make_class_name(dev->class->name, &dev->kobj);
  447. sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
  448. }
  449. #endif
  450. }
  451. if ((error = device_add_attrs(dev)))
  452. goto AttrsError;
  453. if ((error = device_add_groups(dev)))
  454. goto GroupError;
  455. if ((error = device_pm_add(dev)))
  456. goto PMError;
  457. if ((error = bus_add_device(dev)))
  458. goto BusError;
  459. kobject_uevent(&dev->kobj, KOBJ_ADD);
  460. if ((error = bus_attach_device(dev)))
  461. goto AttachError;
  462. if (parent)
  463. klist_add_tail(&dev->knode_parent, &parent->klist_children);
  464. if (dev->class) {
  465. down(&dev->class->sem);
  466. /* tie the class to the device */
  467. list_add_tail(&dev->node, &dev->class->devices);
  468. /* notify any interfaces that the device is here */
  469. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  470. if (class_intf->add_dev)
  471. class_intf->add_dev(dev, class_intf);
  472. up(&dev->class->sem);
  473. }
  474. Done:
  475. kfree(class_name);
  476. put_device(dev);
  477. return error;
  478. AttachError:
  479. bus_remove_device(dev);
  480. BusError:
  481. device_pm_remove(dev);
  482. PMError:
  483. if (dev->bus)
  484. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  485. BUS_NOTIFY_DEL_DEVICE, dev);
  486. device_remove_groups(dev);
  487. GroupError:
  488. device_remove_attrs(dev);
  489. AttrsError:
  490. if (dev->devt_attr) {
  491. device_remove_file(dev, dev->devt_attr);
  492. kfree(dev->devt_attr);
  493. }
  494. ueventattrError:
  495. device_remove_file(dev, &dev->uevent_attr);
  496. attrError:
  497. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  498. kobject_del(&dev->kobj);
  499. Error:
  500. if (parent)
  501. put_device(parent);
  502. goto Done;
  503. }
  504. /**
  505. * device_register - register a device with the system.
  506. * @dev: pointer to the device structure
  507. *
  508. * This happens in two clean steps - initialize the device
  509. * and add it to the system. The two steps can be called
  510. * separately, but this is the easiest and most common.
  511. * I.e. you should only call the two helpers separately if
  512. * have a clearly defined need to use and refcount the device
  513. * before it is added to the hierarchy.
  514. */
  515. int device_register(struct device *dev)
  516. {
  517. device_initialize(dev);
  518. return device_add(dev);
  519. }
  520. /**
  521. * get_device - increment reference count for device.
  522. * @dev: device.
  523. *
  524. * This simply forwards the call to kobject_get(), though
  525. * we do take care to provide for the case that we get a NULL
  526. * pointer passed in.
  527. */
  528. struct device * get_device(struct device * dev)
  529. {
  530. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  531. }
  532. /**
  533. * put_device - decrement reference count.
  534. * @dev: device in question.
  535. */
  536. void put_device(struct device * dev)
  537. {
  538. if (dev)
  539. kobject_put(&dev->kobj);
  540. }
  541. /**
  542. * device_del - delete device from system.
  543. * @dev: device.
  544. *
  545. * This is the first part of the device unregistration
  546. * sequence. This removes the device from the lists we control
  547. * from here, has it removed from the other driver model
  548. * subsystems it was added to in device_add(), and removes it
  549. * from the kobject hierarchy.
  550. *
  551. * NOTE: this should be called manually _iff_ device_add() was
  552. * also called manually.
  553. */
  554. void device_del(struct device * dev)
  555. {
  556. struct device * parent = dev->parent;
  557. struct class_interface *class_intf;
  558. if (parent)
  559. klist_del(&dev->knode_parent);
  560. if (dev->devt_attr) {
  561. device_remove_file(dev, dev->devt_attr);
  562. kfree(dev->devt_attr);
  563. }
  564. if (dev->class) {
  565. sysfs_remove_link(&dev->kobj, "subsystem");
  566. /* If this is not a "fake" compatible device, remove the
  567. * symlink from the class to the device. */
  568. if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
  569. sysfs_remove_link(&dev->class->subsys.kset.kobj,
  570. dev->bus_id);
  571. #ifdef CONFIG_SYSFS_DEPRECATED
  572. if (parent) {
  573. char *class_name = make_class_name(dev->class->name,
  574. &dev->kobj);
  575. sysfs_remove_link(&dev->parent->kobj, class_name);
  576. kfree(class_name);
  577. sysfs_remove_link(&dev->kobj, "device");
  578. }
  579. #endif
  580. down(&dev->class->sem);
  581. /* notify any interfaces that the device is now gone */
  582. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  583. if (class_intf->remove_dev)
  584. class_intf->remove_dev(dev, class_intf);
  585. /* remove the device from the class list */
  586. list_del_init(&dev->node);
  587. up(&dev->class->sem);
  588. }
  589. device_remove_file(dev, &dev->uevent_attr);
  590. device_remove_groups(dev);
  591. device_remove_attrs(dev);
  592. bus_remove_device(dev);
  593. /* Notify the platform of the removal, in case they
  594. * need to do anything...
  595. */
  596. if (platform_notify_remove)
  597. platform_notify_remove(dev);
  598. if (dev->bus)
  599. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  600. BUS_NOTIFY_DEL_DEVICE, dev);
  601. device_pm_remove(dev);
  602. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  603. kobject_del(&dev->kobj);
  604. if (parent)
  605. put_device(parent);
  606. }
  607. /**
  608. * device_unregister - unregister device from system.
  609. * @dev: device going away.
  610. *
  611. * We do this in two parts, like we do device_register(). First,
  612. * we remove it from all the subsystems with device_del(), then
  613. * we decrement the reference count via put_device(). If that
  614. * is the final reference count, the device will be cleaned up
  615. * via device_release() above. Otherwise, the structure will
  616. * stick around until the final reference to the device is dropped.
  617. */
  618. void device_unregister(struct device * dev)
  619. {
  620. pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
  621. device_del(dev);
  622. put_device(dev);
  623. }
  624. static struct device * next_device(struct klist_iter * i)
  625. {
  626. struct klist_node * n = klist_next(i);
  627. return n ? container_of(n, struct device, knode_parent) : NULL;
  628. }
  629. /**
  630. * device_for_each_child - device child iterator.
  631. * @parent: parent struct device.
  632. * @data: data for the callback.
  633. * @fn: function to be called for each device.
  634. *
  635. * Iterate over @parent's child devices, and call @fn for each,
  636. * passing it @data.
  637. *
  638. * We check the return of @fn each time. If it returns anything
  639. * other than 0, we break out and return that value.
  640. */
  641. int device_for_each_child(struct device * parent, void * data,
  642. int (*fn)(struct device *, void *))
  643. {
  644. struct klist_iter i;
  645. struct device * child;
  646. int error = 0;
  647. klist_iter_init(&parent->klist_children, &i);
  648. while ((child = next_device(&i)) && !error)
  649. error = fn(child, data);
  650. klist_iter_exit(&i);
  651. return error;
  652. }
  653. /**
  654. * device_find_child - device iterator for locating a particular device.
  655. * @parent: parent struct device
  656. * @data: Data to pass to match function
  657. * @match: Callback function to check device
  658. *
  659. * This is similar to the device_for_each_child() function above, but it
  660. * returns a reference to a device that is 'found' for later use, as
  661. * determined by the @match callback.
  662. *
  663. * The callback should return 0 if the device doesn't match and non-zero
  664. * if it does. If the callback returns non-zero and a reference to the
  665. * current device can be obtained, this function will return to the caller
  666. * and not iterate over any more devices.
  667. */
  668. struct device * device_find_child(struct device *parent, void *data,
  669. int (*match)(struct device *, void *))
  670. {
  671. struct klist_iter i;
  672. struct device *child;
  673. if (!parent)
  674. return NULL;
  675. klist_iter_init(&parent->klist_children, &i);
  676. while ((child = next_device(&i)))
  677. if (match(child, data) && get_device(child))
  678. break;
  679. klist_iter_exit(&i);
  680. return child;
  681. }
  682. int __init devices_init(void)
  683. {
  684. return subsystem_register(&devices_subsys);
  685. }
  686. EXPORT_SYMBOL_GPL(device_for_each_child);
  687. EXPORT_SYMBOL_GPL(device_find_child);
  688. EXPORT_SYMBOL_GPL(device_initialize);
  689. EXPORT_SYMBOL_GPL(device_add);
  690. EXPORT_SYMBOL_GPL(device_register);
  691. EXPORT_SYMBOL_GPL(device_del);
  692. EXPORT_SYMBOL_GPL(device_unregister);
  693. EXPORT_SYMBOL_GPL(get_device);
  694. EXPORT_SYMBOL_GPL(put_device);
  695. EXPORT_SYMBOL_GPL(device_create_file);
  696. EXPORT_SYMBOL_GPL(device_remove_file);
  697. static void device_create_release(struct device *dev)
  698. {
  699. pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
  700. kfree(dev);
  701. }
  702. /**
  703. * device_create - creates a device and registers it with sysfs
  704. * @class: pointer to the struct class that this device should be registered to
  705. * @parent: pointer to the parent struct device of this new device, if any
  706. * @devt: the dev_t for the char device to be added
  707. * @fmt: string for the device's name
  708. *
  709. * This function can be used by char device classes. A struct device
  710. * will be created in sysfs, registered to the specified class.
  711. *
  712. * A "dev" file will be created, showing the dev_t for the device, if
  713. * the dev_t is not 0,0.
  714. * If a pointer to a parent struct device is passed in, the newly created
  715. * struct device will be a child of that device in sysfs.
  716. * The pointer to the struct device will be returned from the call.
  717. * Any further sysfs files that might be required can be created using this
  718. * pointer.
  719. *
  720. * Note: the struct class passed to this function must have previously
  721. * been created with a call to class_create().
  722. */
  723. struct device *device_create(struct class *class, struct device *parent,
  724. dev_t devt, const char *fmt, ...)
  725. {
  726. va_list args;
  727. struct device *dev = NULL;
  728. int retval = -ENODEV;
  729. if (class == NULL || IS_ERR(class))
  730. goto error;
  731. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  732. if (!dev) {
  733. retval = -ENOMEM;
  734. goto error;
  735. }
  736. dev->devt = devt;
  737. dev->class = class;
  738. dev->parent = parent;
  739. dev->release = device_create_release;
  740. va_start(args, fmt);
  741. vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
  742. va_end(args);
  743. retval = device_register(dev);
  744. if (retval)
  745. goto error;
  746. return dev;
  747. error:
  748. kfree(dev);
  749. return ERR_PTR(retval);
  750. }
  751. EXPORT_SYMBOL_GPL(device_create);
  752. /**
  753. * device_destroy - removes a device that was created with device_create()
  754. * @class: pointer to the struct class that this device was registered with
  755. * @devt: the dev_t of the device that was previously registered
  756. *
  757. * This call unregisters and cleans up a device that was created with a
  758. * call to device_create().
  759. */
  760. void device_destroy(struct class *class, dev_t devt)
  761. {
  762. struct device *dev = NULL;
  763. struct device *dev_tmp;
  764. down(&class->sem);
  765. list_for_each_entry(dev_tmp, &class->devices, node) {
  766. if (dev_tmp->devt == devt) {
  767. dev = dev_tmp;
  768. break;
  769. }
  770. }
  771. up(&class->sem);
  772. if (dev)
  773. device_unregister(dev);
  774. }
  775. EXPORT_SYMBOL_GPL(device_destroy);
  776. /**
  777. * device_rename - renames a device
  778. * @dev: the pointer to the struct device to be renamed
  779. * @new_name: the new name of the device
  780. */
  781. int device_rename(struct device *dev, char *new_name)
  782. {
  783. char *old_class_name = NULL;
  784. char *new_class_name = NULL;
  785. char *old_symlink_name = NULL;
  786. int error;
  787. dev = get_device(dev);
  788. if (!dev)
  789. return -EINVAL;
  790. pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
  791. #ifdef CONFIG_SYSFS_DEPRECATED
  792. if ((dev->class) && (dev->parent))
  793. old_class_name = make_class_name(dev->class->name, &dev->kobj);
  794. #endif
  795. if (dev->class) {
  796. old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  797. if (!old_symlink_name) {
  798. error = -ENOMEM;
  799. goto out_free_old_class;
  800. }
  801. strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
  802. }
  803. strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
  804. error = kobject_rename(&dev->kobj, new_name);
  805. #ifdef CONFIG_SYSFS_DEPRECATED
  806. if (old_class_name) {
  807. new_class_name = make_class_name(dev->class->name, &dev->kobj);
  808. if (new_class_name) {
  809. sysfs_create_link(&dev->parent->kobj, &dev->kobj,
  810. new_class_name);
  811. sysfs_remove_link(&dev->parent->kobj, old_class_name);
  812. }
  813. }
  814. #endif
  815. if (dev->class) {
  816. sysfs_remove_link(&dev->class->subsys.kset.kobj,
  817. old_symlink_name);
  818. sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
  819. dev->bus_id);
  820. }
  821. put_device(dev);
  822. kfree(new_class_name);
  823. kfree(old_symlink_name);
  824. out_free_old_class:
  825. kfree(old_class_name);
  826. return error;
  827. }
  828. static int device_move_class_links(struct device *dev,
  829. struct device *old_parent,
  830. struct device *new_parent)
  831. {
  832. #ifdef CONFIG_SYSFS_DEPRECATED
  833. int error;
  834. char *class_name;
  835. class_name = make_class_name(dev->class->name, &dev->kobj);
  836. if (!class_name) {
  837. error = PTR_ERR(class_name);
  838. class_name = NULL;
  839. goto out;
  840. }
  841. if (old_parent) {
  842. sysfs_remove_link(&dev->kobj, "device");
  843. sysfs_remove_link(&old_parent->kobj, class_name);
  844. }
  845. error = sysfs_create_link(&dev->kobj, &new_parent->kobj, "device");
  846. if (error)
  847. goto out;
  848. error = sysfs_create_link(&new_parent->kobj, &dev->kobj, class_name);
  849. if (error)
  850. sysfs_remove_link(&dev->kobj, "device");
  851. out:
  852. kfree(class_name);
  853. return error;
  854. #else
  855. return 0;
  856. #endif
  857. }
  858. /**
  859. * device_move - moves a device to a new parent
  860. * @dev: the pointer to the struct device to be moved
  861. * @new_parent: the new parent of the device
  862. */
  863. int device_move(struct device *dev, struct device *new_parent)
  864. {
  865. int error;
  866. struct device *old_parent;
  867. dev = get_device(dev);
  868. if (!dev)
  869. return -EINVAL;
  870. new_parent = get_device(new_parent);
  871. if (!new_parent) {
  872. error = -EINVAL;
  873. goto out;
  874. }
  875. pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
  876. new_parent->bus_id);
  877. error = kobject_move(&dev->kobj, &new_parent->kobj);
  878. if (error) {
  879. put_device(new_parent);
  880. goto out;
  881. }
  882. old_parent = dev->parent;
  883. dev->parent = new_parent;
  884. if (old_parent)
  885. klist_remove(&dev->knode_parent);
  886. klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
  887. if (!dev->class)
  888. goto out_put;
  889. error = device_move_class_links(dev, old_parent, new_parent);
  890. if (error) {
  891. /* We ignore errors on cleanup since we're hosed anyway... */
  892. device_move_class_links(dev, new_parent, old_parent);
  893. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  894. klist_remove(&dev->knode_parent);
  895. if (old_parent)
  896. klist_add_tail(&dev->knode_parent,
  897. &old_parent->klist_children);
  898. }
  899. put_device(new_parent);
  900. goto out;
  901. }
  902. out_put:
  903. put_device(old_parent);
  904. out:
  905. put_device(dev);
  906. return error;
  907. }
  908. EXPORT_SYMBOL_GPL(device_move);