core.c 26 KB

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