core.c 33 KB

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