core.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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. }
  332. #ifdef CONFIG_SYSFS_DEPRECATED
  333. static int setup_parent(struct device *dev, struct device *parent)
  334. {
  335. /* Set the parent to the class, not the parent device */
  336. /* this keeps sysfs from having a symlink to make old udevs happy */
  337. if (dev->class)
  338. dev->kobj.parent = &dev->class->subsys.kset.kobj;
  339. else if (parent)
  340. dev->kobj.parent = &parent->kobj;
  341. return 0;
  342. }
  343. #else
  344. static int virtual_device_parent(struct device *dev)
  345. {
  346. if (!dev->class)
  347. return -ENODEV;
  348. if (!dev->class->virtual_dir) {
  349. static struct kobject *virtual_dir = NULL;
  350. if (!virtual_dir)
  351. virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
  352. dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name);
  353. }
  354. dev->kobj.parent = dev->class->virtual_dir;
  355. return 0;
  356. }
  357. static int setup_parent(struct device *dev, struct device *parent)
  358. {
  359. int error;
  360. /* if this is a class device, and has no parent, create one */
  361. if ((dev->class) && (parent == NULL)) {
  362. error = virtual_device_parent(dev);
  363. if (error)
  364. return error;
  365. } else if (parent)
  366. dev->kobj.parent = &parent->kobj;
  367. return 0;
  368. }
  369. #endif
  370. /**
  371. * device_add - add device to device hierarchy.
  372. * @dev: device.
  373. *
  374. * This is part 2 of device_register(), though may be called
  375. * separately _iff_ device_initialize() has been called separately.
  376. *
  377. * This adds it to the kobject hierarchy via kobject_add(), adds it
  378. * to the global and sibling lists for the device, then
  379. * adds it to the other relevant subsystems of the driver model.
  380. */
  381. int device_add(struct device *dev)
  382. {
  383. struct device *parent = NULL;
  384. char *class_name = NULL;
  385. struct class_interface *class_intf;
  386. int error = -EINVAL;
  387. dev = get_device(dev);
  388. if (!dev || !strlen(dev->bus_id))
  389. goto Error;
  390. pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
  391. parent = get_device(dev->parent);
  392. error = setup_parent(dev, parent);
  393. if (error)
  394. goto Error;
  395. /* first, register with generic layer. */
  396. kobject_set_name(&dev->kobj, "%s", dev->bus_id);
  397. error = kobject_add(&dev->kobj);
  398. if (error)
  399. goto Error;
  400. /* notify platform of device entry */
  401. if (platform_notify)
  402. platform_notify(dev);
  403. /* notify clients of device entry (new way) */
  404. if (dev->bus)
  405. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  406. BUS_NOTIFY_ADD_DEVICE, dev);
  407. dev->uevent_attr.attr.name = "uevent";
  408. dev->uevent_attr.attr.mode = S_IWUSR;
  409. if (dev->driver)
  410. dev->uevent_attr.attr.owner = dev->driver->owner;
  411. dev->uevent_attr.store = store_uevent;
  412. error = device_create_file(dev, &dev->uevent_attr);
  413. if (error)
  414. goto attrError;
  415. if (MAJOR(dev->devt)) {
  416. struct device_attribute *attr;
  417. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  418. if (!attr) {
  419. error = -ENOMEM;
  420. goto ueventattrError;
  421. }
  422. attr->attr.name = "dev";
  423. attr->attr.mode = S_IRUGO;
  424. if (dev->driver)
  425. attr->attr.owner = dev->driver->owner;
  426. attr->show = show_dev;
  427. error = device_create_file(dev, attr);
  428. if (error) {
  429. kfree(attr);
  430. goto ueventattrError;
  431. }
  432. dev->devt_attr = attr;
  433. }
  434. if (dev->class) {
  435. sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
  436. "subsystem");
  437. /* If this is not a "fake" compatible device, then create the
  438. * symlink from the class to the device. */
  439. if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
  440. sysfs_create_link(&dev->class->subsys.kset.kobj,
  441. &dev->kobj, dev->bus_id);
  442. #ifdef CONFIG_SYSFS_DEPRECATED
  443. if (parent) {
  444. sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
  445. class_name = make_class_name(dev->class->name, &dev->kobj);
  446. sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
  447. }
  448. #endif
  449. }
  450. if ((error = device_add_attrs(dev)))
  451. goto AttrsError;
  452. if ((error = device_add_groups(dev)))
  453. goto GroupError;
  454. if ((error = device_pm_add(dev)))
  455. goto PMError;
  456. if ((error = bus_add_device(dev)))
  457. goto BusError;
  458. kobject_uevent(&dev->kobj, KOBJ_ADD);
  459. if ((error = bus_attach_device(dev)))
  460. goto AttachError;
  461. if (parent)
  462. klist_add_tail(&dev->knode_parent, &parent->klist_children);
  463. if (dev->class) {
  464. down(&dev->class->sem);
  465. /* tie the class to the device */
  466. list_add_tail(&dev->node, &dev->class->devices);
  467. /* notify any interfaces that the device is here */
  468. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  469. if (class_intf->add_dev)
  470. class_intf->add_dev(dev, class_intf);
  471. up(&dev->class->sem);
  472. }
  473. Done:
  474. kfree(class_name);
  475. put_device(dev);
  476. return error;
  477. AttachError:
  478. bus_remove_device(dev);
  479. BusError:
  480. device_pm_remove(dev);
  481. PMError:
  482. if (dev->bus)
  483. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  484. BUS_NOTIFY_DEL_DEVICE, dev);
  485. device_remove_groups(dev);
  486. GroupError:
  487. device_remove_attrs(dev);
  488. AttrsError:
  489. if (dev->devt_attr) {
  490. device_remove_file(dev, dev->devt_attr);
  491. kfree(dev->devt_attr);
  492. }
  493. ueventattrError:
  494. device_remove_file(dev, &dev->uevent_attr);
  495. attrError:
  496. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  497. kobject_del(&dev->kobj);
  498. Error:
  499. if (parent)
  500. put_device(parent);
  501. goto Done;
  502. }
  503. /**
  504. * device_register - register a device with the system.
  505. * @dev: pointer to the device structure
  506. *
  507. * This happens in two clean steps - initialize the device
  508. * and add it to the system. The two steps can be called
  509. * separately, but this is the easiest and most common.
  510. * I.e. you should only call the two helpers separately if
  511. * have a clearly defined need to use and refcount the device
  512. * before it is added to the hierarchy.
  513. */
  514. int device_register(struct device *dev)
  515. {
  516. device_initialize(dev);
  517. return device_add(dev);
  518. }
  519. /**
  520. * get_device - increment reference count for device.
  521. * @dev: device.
  522. *
  523. * This simply forwards the call to kobject_get(), though
  524. * we do take care to provide for the case that we get a NULL
  525. * pointer passed in.
  526. */
  527. struct device * get_device(struct device * dev)
  528. {
  529. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  530. }
  531. /**
  532. * put_device - decrement reference count.
  533. * @dev: device in question.
  534. */
  535. void put_device(struct device * dev)
  536. {
  537. if (dev)
  538. kobject_put(&dev->kobj);
  539. }
  540. /**
  541. * device_del - delete device from system.
  542. * @dev: device.
  543. *
  544. * This is the first part of the device unregistration
  545. * sequence. This removes the device from the lists we control
  546. * from here, has it removed from the other driver model
  547. * subsystems it was added to in device_add(), and removes it
  548. * from the kobject hierarchy.
  549. *
  550. * NOTE: this should be called manually _iff_ device_add() was
  551. * also called manually.
  552. */
  553. void device_del(struct device * dev)
  554. {
  555. struct device * parent = dev->parent;
  556. struct class_interface *class_intf;
  557. if (parent)
  558. klist_del(&dev->knode_parent);
  559. if (dev->devt_attr) {
  560. device_remove_file(dev, dev->devt_attr);
  561. kfree(dev->devt_attr);
  562. }
  563. if (dev->class) {
  564. sysfs_remove_link(&dev->kobj, "subsystem");
  565. /* If this is not a "fake" compatible device, remove the
  566. * symlink from the class to the device. */
  567. if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
  568. sysfs_remove_link(&dev->class->subsys.kset.kobj,
  569. dev->bus_id);
  570. #ifdef CONFIG_SYSFS_DEPRECATED
  571. if (parent) {
  572. char *class_name = make_class_name(dev->class->name,
  573. &dev->kobj);
  574. sysfs_remove_link(&dev->parent->kobj, class_name);
  575. kfree(class_name);
  576. sysfs_remove_link(&dev->kobj, "device");
  577. }
  578. #endif
  579. down(&dev->class->sem);
  580. /* notify any interfaces that the device is now gone */
  581. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  582. if (class_intf->remove_dev)
  583. class_intf->remove_dev(dev, class_intf);
  584. /* remove the device from the class list */
  585. list_del_init(&dev->node);
  586. up(&dev->class->sem);
  587. }
  588. device_remove_file(dev, &dev->uevent_attr);
  589. device_remove_groups(dev);
  590. device_remove_attrs(dev);
  591. bus_remove_device(dev);
  592. /* Notify the platform of the removal, in case they
  593. * need to do anything...
  594. */
  595. if (platform_notify_remove)
  596. platform_notify_remove(dev);
  597. if (dev->bus)
  598. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  599. BUS_NOTIFY_DEL_DEVICE, dev);
  600. device_pm_remove(dev);
  601. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  602. kobject_del(&dev->kobj);
  603. if (parent)
  604. put_device(parent);
  605. }
  606. /**
  607. * device_unregister - unregister device from system.
  608. * @dev: device going away.
  609. *
  610. * We do this in two parts, like we do device_register(). First,
  611. * we remove it from all the subsystems with device_del(), then
  612. * we decrement the reference count via put_device(). If that
  613. * is the final reference count, the device will be cleaned up
  614. * via device_release() above. Otherwise, the structure will
  615. * stick around until the final reference to the device is dropped.
  616. */
  617. void device_unregister(struct device * dev)
  618. {
  619. pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
  620. device_del(dev);
  621. put_device(dev);
  622. }
  623. static struct device * next_device(struct klist_iter * i)
  624. {
  625. struct klist_node * n = klist_next(i);
  626. return n ? container_of(n, struct device, knode_parent) : NULL;
  627. }
  628. /**
  629. * device_for_each_child - device child iterator.
  630. * @parent: parent struct device.
  631. * @data: data for the callback.
  632. * @fn: function to be called for each device.
  633. *
  634. * Iterate over @parent's child devices, and call @fn for each,
  635. * passing it @data.
  636. *
  637. * We check the return of @fn each time. If it returns anything
  638. * other than 0, we break out and return that value.
  639. */
  640. int device_for_each_child(struct device * parent, void * data,
  641. int (*fn)(struct device *, void *))
  642. {
  643. struct klist_iter i;
  644. struct device * child;
  645. int error = 0;
  646. klist_iter_init(&parent->klist_children, &i);
  647. while ((child = next_device(&i)) && !error)
  648. error = fn(child, data);
  649. klist_iter_exit(&i);
  650. return error;
  651. }
  652. /**
  653. * device_find_child - device iterator for locating a particular device.
  654. * @parent: parent struct device
  655. * @data: Data to pass to match function
  656. * @match: Callback function to check device
  657. *
  658. * This is similar to the device_for_each_child() function above, but it
  659. * returns a reference to a device that is 'found' for later use, as
  660. * determined by the @match callback.
  661. *
  662. * The callback should return 0 if the device doesn't match and non-zero
  663. * if it does. If the callback returns non-zero and a reference to the
  664. * current device can be obtained, this function will return to the caller
  665. * and not iterate over any more devices.
  666. */
  667. struct device * device_find_child(struct device *parent, void *data,
  668. int (*match)(struct device *, void *))
  669. {
  670. struct klist_iter i;
  671. struct device *child;
  672. if (!parent)
  673. return NULL;
  674. klist_iter_init(&parent->klist_children, &i);
  675. while ((child = next_device(&i)))
  676. if (match(child, data) && get_device(child))
  677. break;
  678. klist_iter_exit(&i);
  679. return child;
  680. }
  681. int __init devices_init(void)
  682. {
  683. return subsystem_register(&devices_subsys);
  684. }
  685. EXPORT_SYMBOL_GPL(device_for_each_child);
  686. EXPORT_SYMBOL_GPL(device_find_child);
  687. EXPORT_SYMBOL_GPL(device_initialize);
  688. EXPORT_SYMBOL_GPL(device_add);
  689. EXPORT_SYMBOL_GPL(device_register);
  690. EXPORT_SYMBOL_GPL(device_del);
  691. EXPORT_SYMBOL_GPL(device_unregister);
  692. EXPORT_SYMBOL_GPL(get_device);
  693. EXPORT_SYMBOL_GPL(put_device);
  694. EXPORT_SYMBOL_GPL(device_create_file);
  695. EXPORT_SYMBOL_GPL(device_remove_file);
  696. static void device_create_release(struct device *dev)
  697. {
  698. pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
  699. kfree(dev);
  700. }
  701. /**
  702. * device_create - creates a device and registers it with sysfs
  703. * @class: pointer to the struct class that this device should be registered to
  704. * @parent: pointer to the parent struct device of this new device, if any
  705. * @devt: the dev_t for the char device to be added
  706. * @fmt: string for the device's name
  707. *
  708. * This function can be used by char device classes. A struct device
  709. * will be created in sysfs, registered to the specified class.
  710. *
  711. * A "dev" file will be created, showing the dev_t for the device, if
  712. * the dev_t is not 0,0.
  713. * If a pointer to a parent struct device is passed in, the newly created
  714. * struct device will be a child of that device in sysfs.
  715. * The pointer to the struct device will be returned from the call.
  716. * Any further sysfs files that might be required can be created using this
  717. * pointer.
  718. *
  719. * Note: the struct class passed to this function must have previously
  720. * been created with a call to class_create().
  721. */
  722. struct device *device_create(struct class *class, struct device *parent,
  723. dev_t devt, const char *fmt, ...)
  724. {
  725. va_list args;
  726. struct device *dev = NULL;
  727. int retval = -ENODEV;
  728. if (class == NULL || IS_ERR(class))
  729. goto error;
  730. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  731. if (!dev) {
  732. retval = -ENOMEM;
  733. goto error;
  734. }
  735. dev->devt = devt;
  736. dev->class = class;
  737. dev->parent = parent;
  738. dev->release = device_create_release;
  739. va_start(args, fmt);
  740. vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
  741. va_end(args);
  742. retval = device_register(dev);
  743. if (retval)
  744. goto error;
  745. return dev;
  746. error:
  747. kfree(dev);
  748. return ERR_PTR(retval);
  749. }
  750. EXPORT_SYMBOL_GPL(device_create);
  751. /**
  752. * device_destroy - removes a device that was created with device_create()
  753. * @class: pointer to the struct class that this device was registered with
  754. * @devt: the dev_t of the device that was previously registered
  755. *
  756. * This call unregisters and cleans up a device that was created with a
  757. * call to device_create().
  758. */
  759. void device_destroy(struct class *class, dev_t devt)
  760. {
  761. struct device *dev = NULL;
  762. struct device *dev_tmp;
  763. down(&class->sem);
  764. list_for_each_entry(dev_tmp, &class->devices, node) {
  765. if (dev_tmp->devt == devt) {
  766. dev = dev_tmp;
  767. break;
  768. }
  769. }
  770. up(&class->sem);
  771. if (dev)
  772. device_unregister(dev);
  773. }
  774. EXPORT_SYMBOL_GPL(device_destroy);
  775. /**
  776. * device_rename - renames a device
  777. * @dev: the pointer to the struct device to be renamed
  778. * @new_name: the new name of the device
  779. */
  780. int device_rename(struct device *dev, char *new_name)
  781. {
  782. char *old_class_name = NULL;
  783. char *new_class_name = NULL;
  784. char *old_symlink_name = NULL;
  785. int error;
  786. dev = get_device(dev);
  787. if (!dev)
  788. return -EINVAL;
  789. pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
  790. #ifdef CONFIG_SYSFS_DEPRECATED
  791. if ((dev->class) && (dev->parent))
  792. old_class_name = make_class_name(dev->class->name, &dev->kobj);
  793. #endif
  794. if (dev->class) {
  795. old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  796. if (!old_symlink_name) {
  797. error = -ENOMEM;
  798. goto out_free_old_class;
  799. }
  800. strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
  801. }
  802. strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
  803. error = kobject_rename(&dev->kobj, new_name);
  804. #ifdef CONFIG_SYSFS_DEPRECATED
  805. if (old_class_name) {
  806. new_class_name = make_class_name(dev->class->name, &dev->kobj);
  807. if (new_class_name) {
  808. sysfs_create_link(&dev->parent->kobj, &dev->kobj,
  809. new_class_name);
  810. sysfs_remove_link(&dev->parent->kobj, old_class_name);
  811. }
  812. }
  813. #endif
  814. if (dev->class) {
  815. sysfs_remove_link(&dev->class->subsys.kset.kobj,
  816. old_symlink_name);
  817. sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
  818. dev->bus_id);
  819. }
  820. put_device(dev);
  821. kfree(new_class_name);
  822. kfree(old_symlink_name);
  823. out_free_old_class:
  824. kfree(old_class_name);
  825. return error;
  826. }
  827. static int device_move_class_links(struct device *dev,
  828. struct device *old_parent,
  829. struct device *new_parent)
  830. {
  831. #ifdef CONFIG_SYSFS_DEPRECATED
  832. int error;
  833. char *class_name;
  834. class_name = make_class_name(dev->class->name, &dev->kobj);
  835. if (!class_name) {
  836. error = PTR_ERR(class_name);
  837. class_name = NULL;
  838. goto out;
  839. }
  840. if (old_parent) {
  841. sysfs_remove_link(&dev->kobj, "device");
  842. sysfs_remove_link(&old_parent->kobj, class_name);
  843. }
  844. error = sysfs_create_link(&dev->kobj, &new_parent->kobj, "device");
  845. if (error)
  846. goto out;
  847. error = sysfs_create_link(&new_parent->kobj, &dev->kobj, class_name);
  848. if (error)
  849. sysfs_remove_link(&dev->kobj, "device");
  850. out:
  851. kfree(class_name);
  852. return error;
  853. #else
  854. return 0;
  855. #endif
  856. }
  857. /**
  858. * device_move - moves a device to a new parent
  859. * @dev: the pointer to the struct device to be moved
  860. * @new_parent: the new parent of the device
  861. */
  862. int device_move(struct device *dev, struct device *new_parent)
  863. {
  864. int error;
  865. struct device *old_parent;
  866. dev = get_device(dev);
  867. if (!dev)
  868. return -EINVAL;
  869. if (!device_is_registered(dev)) {
  870. error = -EINVAL;
  871. goto out;
  872. }
  873. new_parent = get_device(new_parent);
  874. if (!new_parent) {
  875. error = -EINVAL;
  876. goto out;
  877. }
  878. pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
  879. new_parent->bus_id);
  880. error = kobject_move(&dev->kobj, &new_parent->kobj);
  881. if (error) {
  882. put_device(new_parent);
  883. goto out;
  884. }
  885. old_parent = dev->parent;
  886. dev->parent = new_parent;
  887. if (old_parent)
  888. klist_remove(&dev->knode_parent);
  889. klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
  890. if (!dev->class)
  891. goto out_put;
  892. error = device_move_class_links(dev, old_parent, new_parent);
  893. if (error) {
  894. /* We ignore errors on cleanup since we're hosed anyway... */
  895. device_move_class_links(dev, new_parent, old_parent);
  896. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  897. klist_remove(&dev->knode_parent);
  898. if (old_parent)
  899. klist_add_tail(&dev->knode_parent,
  900. &old_parent->klist_children);
  901. }
  902. put_device(new_parent);
  903. goto out;
  904. }
  905. out_put:
  906. put_device(old_parent);
  907. out:
  908. put_device(dev);
  909. return error;
  910. }
  911. EXPORT_SYMBOL_GPL(device_move);