core.c 28 KB

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