core.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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. (dev->class ? dev->class->name : ""));
  42. }
  43. EXPORT_SYMBOL(dev_driver_string);
  44. #define to_dev(obj) container_of(obj, struct device, kobj)
  45. #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
  46. static ssize_t
  47. dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
  48. {
  49. struct device_attribute * dev_attr = to_dev_attr(attr);
  50. struct device * dev = to_dev(kobj);
  51. ssize_t ret = -EIO;
  52. if (dev_attr->show)
  53. ret = dev_attr->show(dev, dev_attr, buf);
  54. return ret;
  55. }
  56. static ssize_t
  57. dev_attr_store(struct kobject * kobj, struct attribute * attr,
  58. const char * buf, size_t count)
  59. {
  60. struct device_attribute * dev_attr = to_dev_attr(attr);
  61. struct device * dev = to_dev(kobj);
  62. ssize_t ret = -EIO;
  63. if (dev_attr->store)
  64. ret = dev_attr->store(dev, dev_attr, buf, count);
  65. return ret;
  66. }
  67. static struct sysfs_ops dev_sysfs_ops = {
  68. .show = dev_attr_show,
  69. .store = dev_attr_store,
  70. };
  71. /**
  72. * device_release - free device structure.
  73. * @kobj: device's kobject.
  74. *
  75. * This is called once the reference count for the object
  76. * reaches 0. We forward the call to the device's release
  77. * method, which should handle actually freeing the structure.
  78. */
  79. static void device_release(struct kobject * kobj)
  80. {
  81. struct device * dev = to_dev(kobj);
  82. if (dev->release)
  83. dev->release(dev);
  84. else if (dev->type && dev->type->release)
  85. dev->type->release(dev);
  86. else if (dev->class && dev->class->dev_release)
  87. dev->class->dev_release(dev);
  88. else {
  89. printk(KERN_ERR "Device '%s' does not have a release() function, "
  90. "it is broken and must be fixed.\n",
  91. dev->bus_id);
  92. WARN_ON(1);
  93. }
  94. }
  95. static struct kobj_type device_ktype = {
  96. .release = device_release,
  97. .sysfs_ops = &dev_sysfs_ops,
  98. };
  99. static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
  100. {
  101. struct kobj_type *ktype = get_ktype(kobj);
  102. if (ktype == &device_ktype) {
  103. struct device *dev = to_dev(kobj);
  104. if (dev->uevent_suppress)
  105. return 0;
  106. if (dev->bus)
  107. return 1;
  108. if (dev->class)
  109. return 1;
  110. }
  111. return 0;
  112. }
  113. static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
  114. {
  115. struct device *dev = to_dev(kobj);
  116. if (dev->bus)
  117. return dev->bus->name;
  118. if (dev->class)
  119. return dev->class->name;
  120. return NULL;
  121. }
  122. static int dev_uevent(struct kset *kset, struct kobject *kobj,
  123. struct kobj_uevent_env *env)
  124. {
  125. struct device *dev = to_dev(kobj);
  126. int retval = 0;
  127. /* add the major/minor if present */
  128. if (MAJOR(dev->devt)) {
  129. add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
  130. add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
  131. }
  132. if (dev->type && dev->type->name)
  133. add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
  134. if (dev->driver)
  135. add_uevent_var(env, "DRIVER=%s", dev->driver->name);
  136. #ifdef CONFIG_SYSFS_DEPRECATED
  137. if (dev->class) {
  138. struct device *parent = dev->parent;
  139. /* find first bus device in parent chain */
  140. while (parent && !parent->bus)
  141. parent = parent->parent;
  142. if (parent && parent->bus) {
  143. const char *path;
  144. path = kobject_get_path(&parent->kobj, GFP_KERNEL);
  145. if (path) {
  146. add_uevent_var(env, "PHYSDEVPATH=%s", path);
  147. kfree(path);
  148. }
  149. add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
  150. if (parent->driver)
  151. add_uevent_var(env, "PHYSDEVDRIVER=%s",
  152. parent->driver->name);
  153. }
  154. } else if (dev->bus) {
  155. add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
  156. if (dev->driver)
  157. add_uevent_var(env, "PHYSDEVDRIVER=%s", dev->driver->name);
  158. }
  159. #endif
  160. /* have the bus specific function add its stuff */
  161. if (dev->bus && dev->bus->uevent) {
  162. retval = dev->bus->uevent(dev, env);
  163. if (retval)
  164. pr_debug ("%s: bus uevent() returned %d\n",
  165. __FUNCTION__, retval);
  166. }
  167. /* have the class specific function add its stuff */
  168. if (dev->class && dev->class->dev_uevent) {
  169. retval = dev->class->dev_uevent(dev, env);
  170. if (retval)
  171. pr_debug("%s: class uevent() returned %d\n",
  172. __FUNCTION__, retval);
  173. }
  174. /* have the device type specific fuction add its stuff */
  175. if (dev->type && dev->type->uevent) {
  176. retval = dev->type->uevent(dev, env);
  177. if (retval)
  178. pr_debug("%s: dev_type uevent() returned %d\n",
  179. __FUNCTION__, retval);
  180. }
  181. return retval;
  182. }
  183. static struct kset_uevent_ops device_uevent_ops = {
  184. .filter = dev_uevent_filter,
  185. .name = dev_uevent_name,
  186. .uevent = dev_uevent,
  187. };
  188. static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
  189. char *buf)
  190. {
  191. struct kobject *top_kobj;
  192. struct kset *kset;
  193. struct kobj_uevent_env *env = NULL;
  194. int i;
  195. size_t count = 0;
  196. int retval;
  197. /* search the kset, the device belongs to */
  198. top_kobj = &dev->kobj;
  199. while (!top_kobj->kset && top_kobj->parent)
  200. top_kobj = top_kobj->parent;
  201. if (!top_kobj->kset)
  202. goto out;
  203. kset = top_kobj->kset;
  204. if (!kset->uevent_ops || !kset->uevent_ops->uevent)
  205. goto out;
  206. /* respect filter */
  207. if (kset->uevent_ops && kset->uevent_ops->filter)
  208. if (!kset->uevent_ops->filter(kset, &dev->kobj))
  209. goto out;
  210. env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  211. if (!env)
  212. return -ENOMEM;
  213. /* let the kset specific function add its keys */
  214. retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
  215. if (retval)
  216. goto out;
  217. /* copy keys to file */
  218. for (i = 0; i < env->envp_idx; i++)
  219. count += sprintf(&buf[count], "%s\n", env->envp[i]);
  220. out:
  221. kfree(env);
  222. return count;
  223. }
  224. static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
  225. const char *buf, size_t count)
  226. {
  227. enum kobject_action action;
  228. if (kobject_action_type(buf, count, &action) == 0) {
  229. kobject_uevent(&dev->kobj, action);
  230. goto out;
  231. }
  232. dev_err(dev, "uevent: unsupported action-string; this will "
  233. "be ignored in a future kernel version\n");
  234. kobject_uevent(&dev->kobj, KOBJ_ADD);
  235. out:
  236. return count;
  237. }
  238. static struct device_attribute uevent_attr =
  239. __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
  240. static int device_add_attributes(struct device *dev,
  241. struct device_attribute *attrs)
  242. {
  243. int error = 0;
  244. int i;
  245. if (attrs) {
  246. for (i = 0; attr_name(attrs[i]); i++) {
  247. error = device_create_file(dev, &attrs[i]);
  248. if (error)
  249. break;
  250. }
  251. if (error)
  252. while (--i >= 0)
  253. device_remove_file(dev, &attrs[i]);
  254. }
  255. return error;
  256. }
  257. static void device_remove_attributes(struct device *dev,
  258. struct device_attribute *attrs)
  259. {
  260. int i;
  261. if (attrs)
  262. for (i = 0; attr_name(attrs[i]); i++)
  263. device_remove_file(dev, &attrs[i]);
  264. }
  265. static int device_add_groups(struct device *dev,
  266. struct attribute_group **groups)
  267. {
  268. int error = 0;
  269. int i;
  270. if (groups) {
  271. for (i = 0; groups[i]; i++) {
  272. error = sysfs_create_group(&dev->kobj, groups[i]);
  273. if (error) {
  274. while (--i >= 0)
  275. sysfs_remove_group(&dev->kobj, groups[i]);
  276. break;
  277. }
  278. }
  279. }
  280. return error;
  281. }
  282. static void device_remove_groups(struct device *dev,
  283. struct attribute_group **groups)
  284. {
  285. int i;
  286. if (groups)
  287. for (i = 0; groups[i]; i++)
  288. sysfs_remove_group(&dev->kobj, groups[i]);
  289. }
  290. static int device_add_attrs(struct device *dev)
  291. {
  292. struct class *class = dev->class;
  293. struct device_type *type = dev->type;
  294. int error;
  295. if (class) {
  296. error = device_add_attributes(dev, class->dev_attrs);
  297. if (error)
  298. return error;
  299. }
  300. if (type) {
  301. error = device_add_groups(dev, type->groups);
  302. if (error)
  303. goto err_remove_class_attrs;
  304. }
  305. error = device_add_groups(dev, dev->groups);
  306. if (error)
  307. goto err_remove_type_groups;
  308. return 0;
  309. err_remove_type_groups:
  310. if (type)
  311. device_remove_groups(dev, type->groups);
  312. err_remove_class_attrs:
  313. if (class)
  314. device_remove_attributes(dev, class->dev_attrs);
  315. return error;
  316. }
  317. static void device_remove_attrs(struct device *dev)
  318. {
  319. struct class *class = dev->class;
  320. struct device_type *type = dev->type;
  321. device_remove_groups(dev, dev->groups);
  322. if (type)
  323. device_remove_groups(dev, type->groups);
  324. if (class)
  325. device_remove_attributes(dev, class->dev_attrs);
  326. }
  327. static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
  328. char *buf)
  329. {
  330. return print_dev_t(buf, dev->devt);
  331. }
  332. static struct device_attribute devt_attr =
  333. __ATTR(dev, S_IRUGO, show_dev, NULL);
  334. /*
  335. * devices_subsys - structure to be registered with kobject core.
  336. */
  337. decl_subsys(devices, &device_ktype, &device_uevent_ops);
  338. /**
  339. * device_create_file - create sysfs attribute file for device.
  340. * @dev: device.
  341. * @attr: device attribute descriptor.
  342. */
  343. int device_create_file(struct device * dev, struct device_attribute * attr)
  344. {
  345. int error = 0;
  346. if (get_device(dev)) {
  347. error = sysfs_create_file(&dev->kobj, &attr->attr);
  348. put_device(dev);
  349. }
  350. return error;
  351. }
  352. /**
  353. * device_remove_file - remove sysfs attribute file.
  354. * @dev: device.
  355. * @attr: device attribute descriptor.
  356. */
  357. void device_remove_file(struct device * dev, struct device_attribute * attr)
  358. {
  359. if (get_device(dev)) {
  360. sysfs_remove_file(&dev->kobj, &attr->attr);
  361. put_device(dev);
  362. }
  363. }
  364. /**
  365. * device_create_bin_file - create sysfs binary attribute file for device.
  366. * @dev: device.
  367. * @attr: device binary attribute descriptor.
  368. */
  369. int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
  370. {
  371. int error = -EINVAL;
  372. if (dev)
  373. error = sysfs_create_bin_file(&dev->kobj, attr);
  374. return error;
  375. }
  376. EXPORT_SYMBOL_GPL(device_create_bin_file);
  377. /**
  378. * device_remove_bin_file - remove sysfs binary attribute file
  379. * @dev: device.
  380. * @attr: device binary attribute descriptor.
  381. */
  382. void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
  383. {
  384. if (dev)
  385. sysfs_remove_bin_file(&dev->kobj, attr);
  386. }
  387. EXPORT_SYMBOL_GPL(device_remove_bin_file);
  388. /**
  389. * device_schedule_callback_owner - helper to schedule a callback for a device
  390. * @dev: device.
  391. * @func: callback function to invoke later.
  392. * @owner: module owning the callback routine
  393. *
  394. * Attribute methods must not unregister themselves or their parent device
  395. * (which would amount to the same thing). Attempts to do so will deadlock,
  396. * since unregistration is mutually exclusive with driver callbacks.
  397. *
  398. * Instead methods can call this routine, which will attempt to allocate
  399. * and schedule a workqueue request to call back @func with @dev as its
  400. * argument in the workqueue's process context. @dev will be pinned until
  401. * @func returns.
  402. *
  403. * This routine is usually called via the inline device_schedule_callback(),
  404. * which automatically sets @owner to THIS_MODULE.
  405. *
  406. * Returns 0 if the request was submitted, -ENOMEM if storage could not
  407. * be allocated, -ENODEV if a reference to @owner isn't available.
  408. *
  409. * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
  410. * underlying sysfs routine (since it is intended for use by attribute
  411. * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
  412. */
  413. int device_schedule_callback_owner(struct device *dev,
  414. void (*func)(struct device *), struct module *owner)
  415. {
  416. return sysfs_schedule_callback(&dev->kobj,
  417. (void (*)(void *)) func, dev, owner);
  418. }
  419. EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
  420. static void klist_children_get(struct klist_node *n)
  421. {
  422. struct device *dev = container_of(n, struct device, knode_parent);
  423. get_device(dev);
  424. }
  425. static void klist_children_put(struct klist_node *n)
  426. {
  427. struct device *dev = container_of(n, struct device, knode_parent);
  428. put_device(dev);
  429. }
  430. /**
  431. * device_initialize - init device structure.
  432. * @dev: device.
  433. *
  434. * This prepares the device for use by other layers,
  435. * including adding it to the device hierarchy.
  436. * It is the first half of device_register(), if called by
  437. * that, though it can also be called separately, so one
  438. * may use @dev's fields (e.g. the refcount).
  439. */
  440. void device_initialize(struct device *dev)
  441. {
  442. kobj_set_kset_s(dev, devices_subsys);
  443. kobject_init(&dev->kobj);
  444. klist_init(&dev->klist_children, klist_children_get,
  445. klist_children_put);
  446. INIT_LIST_HEAD(&dev->dma_pools);
  447. INIT_LIST_HEAD(&dev->node);
  448. init_MUTEX(&dev->sem);
  449. spin_lock_init(&dev->devres_lock);
  450. INIT_LIST_HEAD(&dev->devres_head);
  451. device_init_wakeup(dev, 0);
  452. set_dev_node(dev, -1);
  453. }
  454. #ifdef CONFIG_SYSFS_DEPRECATED
  455. static struct kobject * get_device_parent(struct device *dev,
  456. struct device *parent)
  457. {
  458. /*
  459. * Set the parent to the class, not the parent device
  460. * for topmost devices in class hierarchy.
  461. * This keeps sysfs from having a symlink to make old
  462. * udevs happy
  463. */
  464. if (dev->class && (!parent || parent->class != dev->class))
  465. return &dev->class->subsys.kobj;
  466. else if (parent)
  467. return &parent->kobj;
  468. return NULL;
  469. }
  470. #else
  471. static struct kobject *virtual_device_parent(struct device *dev)
  472. {
  473. static struct kobject *virtual_dir = NULL;
  474. if (!virtual_dir)
  475. virtual_dir = kobject_add_dir(&devices_subsys.kobj, "virtual");
  476. return virtual_dir;
  477. }
  478. static struct kobject * get_device_parent(struct device *dev,
  479. struct device *parent)
  480. {
  481. if (dev->class) {
  482. struct kobject *kobj = NULL;
  483. struct kobject *parent_kobj;
  484. struct kobject *k;
  485. /*
  486. * If we have no parent, we live in "virtual".
  487. * Class-devices with a bus-device as parent, live
  488. * in a class-directory to prevent namespace collisions.
  489. */
  490. if (parent == NULL)
  491. parent_kobj = virtual_device_parent(dev);
  492. else if (parent->class)
  493. return &parent->kobj;
  494. else
  495. parent_kobj = &parent->kobj;
  496. /* find our class-directory at the parent and reference it */
  497. spin_lock(&dev->class->class_dirs.list_lock);
  498. list_for_each_entry(k, &dev->class->class_dirs.list, entry)
  499. if (k->parent == parent_kobj) {
  500. kobj = kobject_get(k);
  501. break;
  502. }
  503. spin_unlock(&dev->class->class_dirs.list_lock);
  504. if (kobj)
  505. return kobj;
  506. /* or create a new class-directory at the parent device */
  507. return kobject_kset_add_dir(&dev->class->class_dirs,
  508. parent_kobj, dev->class->name);
  509. }
  510. if (parent)
  511. return &parent->kobj;
  512. return NULL;
  513. }
  514. #endif
  515. static int setup_parent(struct device *dev, struct device *parent)
  516. {
  517. struct kobject *kobj;
  518. kobj = get_device_parent(dev, parent);
  519. if (IS_ERR(kobj))
  520. return PTR_ERR(kobj);
  521. if (kobj)
  522. dev->kobj.parent = kobj;
  523. return 0;
  524. }
  525. static int device_add_class_symlinks(struct device *dev)
  526. {
  527. int error;
  528. if (!dev->class)
  529. return 0;
  530. error = sysfs_create_link(&dev->kobj, &dev->class->subsys.kobj,
  531. "subsystem");
  532. if (error)
  533. goto out;
  534. /*
  535. * If this is not a "fake" compatible device, then create the
  536. * symlink from the class to the device.
  537. */
  538. if (dev->kobj.parent != &dev->class->subsys.kobj) {
  539. error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
  540. dev->bus_id);
  541. if (error)
  542. goto out_subsys;
  543. }
  544. if (dev->parent) {
  545. #ifdef CONFIG_SYSFS_DEPRECATED
  546. {
  547. struct device *parent = dev->parent;
  548. char *class_name;
  549. /*
  550. * In old sysfs stacked class devices had 'device'
  551. * link pointing to real device instead of parent
  552. */
  553. while (parent->class && !parent->bus && parent->parent)
  554. parent = parent->parent;
  555. error = sysfs_create_link(&dev->kobj,
  556. &parent->kobj,
  557. "device");
  558. if (error)
  559. goto out_busid;
  560. class_name = make_class_name(dev->class->name,
  561. &dev->kobj);
  562. if (class_name)
  563. error = sysfs_create_link(&dev->parent->kobj,
  564. &dev->kobj, class_name);
  565. kfree(class_name);
  566. if (error)
  567. goto out_device;
  568. }
  569. #else
  570. error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  571. "device");
  572. if (error)
  573. goto out_busid;
  574. #endif
  575. }
  576. return 0;
  577. #ifdef CONFIG_SYSFS_DEPRECATED
  578. out_device:
  579. if (dev->parent)
  580. sysfs_remove_link(&dev->kobj, "device");
  581. #endif
  582. out_busid:
  583. if (dev->kobj.parent != &dev->class->subsys.kobj)
  584. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  585. out_subsys:
  586. sysfs_remove_link(&dev->kobj, "subsystem");
  587. out:
  588. return error;
  589. }
  590. static void device_remove_class_symlinks(struct device *dev)
  591. {
  592. if (!dev->class)
  593. return;
  594. if (dev->parent) {
  595. #ifdef CONFIG_SYSFS_DEPRECATED
  596. char *class_name;
  597. class_name = make_class_name(dev->class->name, &dev->kobj);
  598. if (class_name) {
  599. sysfs_remove_link(&dev->parent->kobj, class_name);
  600. kfree(class_name);
  601. }
  602. #endif
  603. sysfs_remove_link(&dev->kobj, "device");
  604. }
  605. if (dev->kobj.parent != &dev->class->subsys.kobj)
  606. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  607. sysfs_remove_link(&dev->kobj, "subsystem");
  608. }
  609. /**
  610. * device_add - add device to device hierarchy.
  611. * @dev: device.
  612. *
  613. * This is part 2 of device_register(), though may be called
  614. * separately _iff_ device_initialize() has been called separately.
  615. *
  616. * This adds it to the kobject hierarchy via kobject_add(), adds it
  617. * to the global and sibling lists for the device, then
  618. * adds it to the other relevant subsystems of the driver model.
  619. */
  620. int device_add(struct device *dev)
  621. {
  622. struct device *parent = NULL;
  623. struct class_interface *class_intf;
  624. int error = -EINVAL;
  625. dev = get_device(dev);
  626. if (!dev || !strlen(dev->bus_id))
  627. goto Error;
  628. pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
  629. parent = get_device(dev->parent);
  630. error = setup_parent(dev, parent);
  631. if (error)
  632. goto Error;
  633. /* first, register with generic layer. */
  634. kobject_set_name(&dev->kobj, "%s", dev->bus_id);
  635. error = kobject_add(&dev->kobj);
  636. if (error)
  637. goto Error;
  638. /* notify platform of device entry */
  639. if (platform_notify)
  640. platform_notify(dev);
  641. /* notify clients of device entry (new way) */
  642. if (dev->bus)
  643. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  644. BUS_NOTIFY_ADD_DEVICE, dev);
  645. error = device_create_file(dev, &uevent_attr);
  646. if (error)
  647. goto attrError;
  648. if (MAJOR(dev->devt)) {
  649. error = device_create_file(dev, &devt_attr);
  650. if (error)
  651. goto ueventattrError;
  652. }
  653. error = device_add_class_symlinks(dev);
  654. if (error)
  655. goto SymlinkError;
  656. error = device_add_attrs(dev);
  657. if (error)
  658. goto AttrsError;
  659. error = device_pm_add(dev);
  660. if (error)
  661. goto PMError;
  662. error = bus_add_device(dev);
  663. if (error)
  664. goto BusError;
  665. kobject_uevent(&dev->kobj, KOBJ_ADD);
  666. bus_attach_device(dev);
  667. if (parent)
  668. klist_add_tail(&dev->knode_parent, &parent->klist_children);
  669. if (dev->class) {
  670. down(&dev->class->sem);
  671. /* tie the class to the device */
  672. list_add_tail(&dev->node, &dev->class->devices);
  673. /* notify any interfaces that the device is here */
  674. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  675. if (class_intf->add_dev)
  676. class_intf->add_dev(dev, class_intf);
  677. up(&dev->class->sem);
  678. }
  679. Done:
  680. put_device(dev);
  681. return error;
  682. BusError:
  683. device_pm_remove(dev);
  684. PMError:
  685. if (dev->bus)
  686. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  687. BUS_NOTIFY_DEL_DEVICE, dev);
  688. device_remove_attrs(dev);
  689. AttrsError:
  690. device_remove_class_symlinks(dev);
  691. SymlinkError:
  692. if (MAJOR(dev->devt))
  693. device_remove_file(dev, &devt_attr);
  694. if (dev->class) {
  695. sysfs_remove_link(&dev->kobj, "subsystem");
  696. /* If this is not a "fake" compatible device, remove the
  697. * symlink from the class to the device. */
  698. if (dev->kobj.parent != &dev->class->subsys.kobj)
  699. sysfs_remove_link(&dev->class->subsys.kobj,
  700. dev->bus_id);
  701. if (parent) {
  702. #ifdef CONFIG_SYSFS_DEPRECATED
  703. char *class_name = make_class_name(dev->class->name,
  704. &dev->kobj);
  705. if (class_name)
  706. sysfs_remove_link(&dev->parent->kobj,
  707. class_name);
  708. kfree(class_name);
  709. #endif
  710. sysfs_remove_link(&dev->kobj, "device");
  711. }
  712. }
  713. ueventattrError:
  714. device_remove_file(dev, &uevent_attr);
  715. attrError:
  716. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  717. kobject_del(&dev->kobj);
  718. Error:
  719. if (parent)
  720. put_device(parent);
  721. goto Done;
  722. }
  723. /**
  724. * device_register - register a device with the system.
  725. * @dev: pointer to the device structure
  726. *
  727. * This happens in two clean steps - initialize the device
  728. * and add it to the system. The two steps can be called
  729. * separately, but this is the easiest and most common.
  730. * I.e. you should only call the two helpers separately if
  731. * have a clearly defined need to use and refcount the device
  732. * before it is added to the hierarchy.
  733. */
  734. int device_register(struct device *dev)
  735. {
  736. device_initialize(dev);
  737. return device_add(dev);
  738. }
  739. /**
  740. * get_device - increment reference count for device.
  741. * @dev: device.
  742. *
  743. * This simply forwards the call to kobject_get(), though
  744. * we do take care to provide for the case that we get a NULL
  745. * pointer passed in.
  746. */
  747. struct device * get_device(struct device * dev)
  748. {
  749. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  750. }
  751. /**
  752. * put_device - decrement reference count.
  753. * @dev: device in question.
  754. */
  755. void put_device(struct device * dev)
  756. {
  757. if (dev)
  758. kobject_put(&dev->kobj);
  759. }
  760. /**
  761. * device_del - delete device from system.
  762. * @dev: device.
  763. *
  764. * This is the first part of the device unregistration
  765. * sequence. This removes the device from the lists we control
  766. * from here, has it removed from the other driver model
  767. * subsystems it was added to in device_add(), and removes it
  768. * from the kobject hierarchy.
  769. *
  770. * NOTE: this should be called manually _iff_ device_add() was
  771. * also called manually.
  772. */
  773. void device_del(struct device * dev)
  774. {
  775. struct device * parent = dev->parent;
  776. struct class_interface *class_intf;
  777. if (parent)
  778. klist_del(&dev->knode_parent);
  779. if (MAJOR(dev->devt))
  780. device_remove_file(dev, &devt_attr);
  781. if (dev->class) {
  782. sysfs_remove_link(&dev->kobj, "subsystem");
  783. /* If this is not a "fake" compatible device, remove the
  784. * symlink from the class to the device. */
  785. if (dev->kobj.parent != &dev->class->subsys.kobj)
  786. sysfs_remove_link(&dev->class->subsys.kobj,
  787. dev->bus_id);
  788. if (parent) {
  789. #ifdef CONFIG_SYSFS_DEPRECATED
  790. char *class_name = make_class_name(dev->class->name,
  791. &dev->kobj);
  792. if (class_name)
  793. sysfs_remove_link(&dev->parent->kobj,
  794. class_name);
  795. kfree(class_name);
  796. #endif
  797. sysfs_remove_link(&dev->kobj, "device");
  798. }
  799. down(&dev->class->sem);
  800. /* notify any interfaces that the device is now gone */
  801. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  802. if (class_intf->remove_dev)
  803. class_intf->remove_dev(dev, class_intf);
  804. /* remove the device from the class list */
  805. list_del_init(&dev->node);
  806. up(&dev->class->sem);
  807. /* If we live in a parent class-directory, unreference it */
  808. if (dev->kobj.parent->kset == &dev->class->class_dirs) {
  809. struct device *d;
  810. int other = 0;
  811. /*
  812. * if we are the last child of our class, delete
  813. * our class-directory at this parent
  814. */
  815. down(&dev->class->sem);
  816. list_for_each_entry(d, &dev->class->devices, node) {
  817. if (d == dev)
  818. continue;
  819. if (d->kobj.parent == dev->kobj.parent) {
  820. other = 1;
  821. break;
  822. }
  823. }
  824. if (!other)
  825. kobject_del(dev->kobj.parent);
  826. kobject_put(dev->kobj.parent);
  827. up(&dev->class->sem);
  828. }
  829. }
  830. device_remove_file(dev, &uevent_attr);
  831. device_remove_attrs(dev);
  832. bus_remove_device(dev);
  833. /*
  834. * Some platform devices are driven without driver attached
  835. * and managed resources may have been acquired. Make sure
  836. * all resources are released.
  837. */
  838. devres_release_all(dev);
  839. /* Notify the platform of the removal, in case they
  840. * need to do anything...
  841. */
  842. if (platform_notify_remove)
  843. platform_notify_remove(dev);
  844. if (dev->bus)
  845. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  846. BUS_NOTIFY_DEL_DEVICE, dev);
  847. device_pm_remove(dev);
  848. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  849. kobject_del(&dev->kobj);
  850. if (parent)
  851. put_device(parent);
  852. }
  853. /**
  854. * device_unregister - unregister device from system.
  855. * @dev: device going away.
  856. *
  857. * We do this in two parts, like we do device_register(). First,
  858. * we remove it from all the subsystems with device_del(), then
  859. * we decrement the reference count via put_device(). If that
  860. * is the final reference count, the device will be cleaned up
  861. * via device_release() above. Otherwise, the structure will
  862. * stick around until the final reference to the device is dropped.
  863. */
  864. void device_unregister(struct device * dev)
  865. {
  866. pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
  867. device_del(dev);
  868. put_device(dev);
  869. }
  870. static struct device * next_device(struct klist_iter * i)
  871. {
  872. struct klist_node * n = klist_next(i);
  873. return n ? container_of(n, struct device, knode_parent) : NULL;
  874. }
  875. /**
  876. * device_for_each_child - device child iterator.
  877. * @parent: parent struct device.
  878. * @data: data for the callback.
  879. * @fn: function to be called for each device.
  880. *
  881. * Iterate over @parent's child devices, and call @fn for each,
  882. * passing it @data.
  883. *
  884. * We check the return of @fn each time. If it returns anything
  885. * other than 0, we break out and return that value.
  886. */
  887. int device_for_each_child(struct device * parent, void * data,
  888. int (*fn)(struct device *, void *))
  889. {
  890. struct klist_iter i;
  891. struct device * child;
  892. int error = 0;
  893. klist_iter_init(&parent->klist_children, &i);
  894. while ((child = next_device(&i)) && !error)
  895. error = fn(child, data);
  896. klist_iter_exit(&i);
  897. return error;
  898. }
  899. /**
  900. * device_find_child - device iterator for locating a particular device.
  901. * @parent: parent struct device
  902. * @data: Data to pass to match function
  903. * @match: Callback function to check device
  904. *
  905. * This is similar to the device_for_each_child() function above, but it
  906. * returns a reference to a device that is 'found' for later use, as
  907. * determined by the @match callback.
  908. *
  909. * The callback should return 0 if the device doesn't match and non-zero
  910. * if it does. If the callback returns non-zero and a reference to the
  911. * current device can be obtained, this function will return to the caller
  912. * and not iterate over any more devices.
  913. */
  914. struct device * device_find_child(struct device *parent, void *data,
  915. int (*match)(struct device *, void *))
  916. {
  917. struct klist_iter i;
  918. struct device *child;
  919. if (!parent)
  920. return NULL;
  921. klist_iter_init(&parent->klist_children, &i);
  922. while ((child = next_device(&i)))
  923. if (match(child, data) && get_device(child))
  924. break;
  925. klist_iter_exit(&i);
  926. return child;
  927. }
  928. int __init devices_init(void)
  929. {
  930. return subsystem_register(&devices_subsys);
  931. }
  932. EXPORT_SYMBOL_GPL(device_for_each_child);
  933. EXPORT_SYMBOL_GPL(device_find_child);
  934. EXPORT_SYMBOL_GPL(device_initialize);
  935. EXPORT_SYMBOL_GPL(device_add);
  936. EXPORT_SYMBOL_GPL(device_register);
  937. EXPORT_SYMBOL_GPL(device_del);
  938. EXPORT_SYMBOL_GPL(device_unregister);
  939. EXPORT_SYMBOL_GPL(get_device);
  940. EXPORT_SYMBOL_GPL(put_device);
  941. EXPORT_SYMBOL_GPL(device_create_file);
  942. EXPORT_SYMBOL_GPL(device_remove_file);
  943. static void device_create_release(struct device *dev)
  944. {
  945. pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
  946. kfree(dev);
  947. }
  948. /**
  949. * device_create - creates a device and registers it with sysfs
  950. * @class: pointer to the struct class that this device should be registered to
  951. * @parent: pointer to the parent struct device of this new device, if any
  952. * @devt: the dev_t for the char device to be added
  953. * @fmt: string for the device's name
  954. *
  955. * This function can be used by char device classes. A struct device
  956. * will be created in sysfs, registered to the specified class.
  957. *
  958. * A "dev" file will be created, showing the dev_t for the device, if
  959. * the dev_t is not 0,0.
  960. * If a pointer to a parent struct device is passed in, the newly created
  961. * struct device will be a child of that device in sysfs.
  962. * The pointer to the struct device will be returned from the call.
  963. * Any further sysfs files that might be required can be created using this
  964. * pointer.
  965. *
  966. * Note: the struct class passed to this function must have previously
  967. * been created with a call to class_create().
  968. */
  969. struct device *device_create(struct class *class, struct device *parent,
  970. dev_t devt, const char *fmt, ...)
  971. {
  972. va_list args;
  973. struct device *dev = NULL;
  974. int retval = -ENODEV;
  975. if (class == NULL || IS_ERR(class))
  976. goto error;
  977. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  978. if (!dev) {
  979. retval = -ENOMEM;
  980. goto error;
  981. }
  982. dev->devt = devt;
  983. dev->class = class;
  984. dev->parent = parent;
  985. dev->release = device_create_release;
  986. va_start(args, fmt);
  987. vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
  988. va_end(args);
  989. retval = device_register(dev);
  990. if (retval)
  991. goto error;
  992. return dev;
  993. error:
  994. kfree(dev);
  995. return ERR_PTR(retval);
  996. }
  997. EXPORT_SYMBOL_GPL(device_create);
  998. /**
  999. * device_destroy - removes a device that was created with device_create()
  1000. * @class: pointer to the struct class that this device was registered with
  1001. * @devt: the dev_t of the device that was previously registered
  1002. *
  1003. * This call unregisters and cleans up a device that was created with a
  1004. * call to device_create().
  1005. */
  1006. void device_destroy(struct class *class, dev_t devt)
  1007. {
  1008. struct device *dev = NULL;
  1009. struct device *dev_tmp;
  1010. down(&class->sem);
  1011. list_for_each_entry(dev_tmp, &class->devices, node) {
  1012. if (dev_tmp->devt == devt) {
  1013. dev = dev_tmp;
  1014. break;
  1015. }
  1016. }
  1017. up(&class->sem);
  1018. if (dev)
  1019. device_unregister(dev);
  1020. }
  1021. EXPORT_SYMBOL_GPL(device_destroy);
  1022. /**
  1023. * device_rename - renames a device
  1024. * @dev: the pointer to the struct device to be renamed
  1025. * @new_name: the new name of the device
  1026. */
  1027. int device_rename(struct device *dev, char *new_name)
  1028. {
  1029. char *old_class_name = NULL;
  1030. char *new_class_name = NULL;
  1031. char *old_device_name = NULL;
  1032. int error;
  1033. dev = get_device(dev);
  1034. if (!dev)
  1035. return -EINVAL;
  1036. pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
  1037. #ifdef CONFIG_SYSFS_DEPRECATED
  1038. if ((dev->class) && (dev->parent))
  1039. old_class_name = make_class_name(dev->class->name, &dev->kobj);
  1040. #endif
  1041. old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  1042. if (!old_device_name) {
  1043. error = -ENOMEM;
  1044. goto out;
  1045. }
  1046. strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE);
  1047. strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
  1048. error = kobject_rename(&dev->kobj, new_name);
  1049. if (error) {
  1050. strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
  1051. goto out;
  1052. }
  1053. #ifdef CONFIG_SYSFS_DEPRECATED
  1054. if (old_class_name) {
  1055. new_class_name = make_class_name(dev->class->name, &dev->kobj);
  1056. if (new_class_name) {
  1057. error = sysfs_create_link(&dev->parent->kobj,
  1058. &dev->kobj, new_class_name);
  1059. if (error)
  1060. goto out;
  1061. sysfs_remove_link(&dev->parent->kobj, old_class_name);
  1062. }
  1063. }
  1064. #endif
  1065. if (dev->class) {
  1066. sysfs_remove_link(&dev->class->subsys.kobj, old_device_name);
  1067. error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
  1068. dev->bus_id);
  1069. if (error) {
  1070. /* Uh... how to unravel this if restoring can fail? */
  1071. dev_err(dev, "%s: sysfs_create_symlink failed (%d)\n",
  1072. __FUNCTION__, error);
  1073. }
  1074. }
  1075. out:
  1076. put_device(dev);
  1077. kfree(new_class_name);
  1078. kfree(old_class_name);
  1079. kfree(old_device_name);
  1080. return error;
  1081. }
  1082. EXPORT_SYMBOL_GPL(device_rename);
  1083. static int device_move_class_links(struct device *dev,
  1084. struct device *old_parent,
  1085. struct device *new_parent)
  1086. {
  1087. int error = 0;
  1088. #ifdef CONFIG_SYSFS_DEPRECATED
  1089. char *class_name;
  1090. class_name = make_class_name(dev->class->name, &dev->kobj);
  1091. if (!class_name) {
  1092. error = -ENOMEM;
  1093. goto out;
  1094. }
  1095. if (old_parent) {
  1096. sysfs_remove_link(&dev->kobj, "device");
  1097. sysfs_remove_link(&old_parent->kobj, class_name);
  1098. }
  1099. if (new_parent) {
  1100. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1101. "device");
  1102. if (error)
  1103. goto out;
  1104. error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
  1105. class_name);
  1106. if (error)
  1107. sysfs_remove_link(&dev->kobj, "device");
  1108. }
  1109. else
  1110. error = 0;
  1111. out:
  1112. kfree(class_name);
  1113. return error;
  1114. #else
  1115. if (old_parent)
  1116. sysfs_remove_link(&dev->kobj, "device");
  1117. if (new_parent)
  1118. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1119. "device");
  1120. return error;
  1121. #endif
  1122. }
  1123. /**
  1124. * device_move - moves a device to a new parent
  1125. * @dev: the pointer to the struct device to be moved
  1126. * @new_parent: the new parent of the device (can by NULL)
  1127. */
  1128. int device_move(struct device *dev, struct device *new_parent)
  1129. {
  1130. int error;
  1131. struct device *old_parent;
  1132. struct kobject *new_parent_kobj;
  1133. dev = get_device(dev);
  1134. if (!dev)
  1135. return -EINVAL;
  1136. new_parent = get_device(new_parent);
  1137. new_parent_kobj = get_device_parent (dev, new_parent);
  1138. if (IS_ERR(new_parent_kobj)) {
  1139. error = PTR_ERR(new_parent_kobj);
  1140. put_device(new_parent);
  1141. goto out;
  1142. }
  1143. pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
  1144. new_parent ? new_parent->bus_id : "<NULL>");
  1145. error = kobject_move(&dev->kobj, new_parent_kobj);
  1146. if (error) {
  1147. put_device(new_parent);
  1148. goto out;
  1149. }
  1150. old_parent = dev->parent;
  1151. dev->parent = new_parent;
  1152. if (old_parent)
  1153. klist_remove(&dev->knode_parent);
  1154. if (new_parent)
  1155. klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
  1156. if (!dev->class)
  1157. goto out_put;
  1158. error = device_move_class_links(dev, old_parent, new_parent);
  1159. if (error) {
  1160. /* We ignore errors on cleanup since we're hosed anyway... */
  1161. device_move_class_links(dev, new_parent, old_parent);
  1162. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  1163. if (new_parent)
  1164. klist_remove(&dev->knode_parent);
  1165. if (old_parent)
  1166. klist_add_tail(&dev->knode_parent,
  1167. &old_parent->klist_children);
  1168. }
  1169. put_device(new_parent);
  1170. goto out;
  1171. }
  1172. out_put:
  1173. put_device(old_parent);
  1174. out:
  1175. put_device(dev);
  1176. return error;
  1177. }
  1178. EXPORT_SYMBOL_GPL(device_move);