core.c 32 KB

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