core.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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 = dpm_sysfs_add(dev);
  660. if (error)
  661. goto PMError;
  662. device_pm_add(dev);
  663. error = bus_add_device(dev);
  664. if (error)
  665. goto BusError;
  666. kobject_uevent(&dev->kobj, KOBJ_ADD);
  667. bus_attach_device(dev);
  668. if (parent)
  669. klist_add_tail(&dev->knode_parent, &parent->klist_children);
  670. if (dev->class) {
  671. down(&dev->class->sem);
  672. /* tie the class to the device */
  673. list_add_tail(&dev->node, &dev->class->devices);
  674. /* notify any interfaces that the device is here */
  675. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  676. if (class_intf->add_dev)
  677. class_intf->add_dev(dev, class_intf);
  678. up(&dev->class->sem);
  679. }
  680. Done:
  681. put_device(dev);
  682. return error;
  683. BusError:
  684. device_pm_remove(dev);
  685. dpm_sysfs_remove(dev);
  686. PMError:
  687. if (dev->bus)
  688. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  689. BUS_NOTIFY_DEL_DEVICE, dev);
  690. device_remove_attrs(dev);
  691. AttrsError:
  692. device_remove_class_symlinks(dev);
  693. SymlinkError:
  694. if (MAJOR(dev->devt))
  695. device_remove_file(dev, &devt_attr);
  696. if (dev->class) {
  697. sysfs_remove_link(&dev->kobj, "subsystem");
  698. /* If this is not a "fake" compatible device, remove the
  699. * symlink from the class to the device. */
  700. if (dev->kobj.parent != &dev->class->subsys.kobj)
  701. sysfs_remove_link(&dev->class->subsys.kobj,
  702. dev->bus_id);
  703. if (parent) {
  704. #ifdef CONFIG_SYSFS_DEPRECATED
  705. char *class_name = make_class_name(dev->class->name,
  706. &dev->kobj);
  707. if (class_name)
  708. sysfs_remove_link(&dev->parent->kobj,
  709. class_name);
  710. kfree(class_name);
  711. #endif
  712. sysfs_remove_link(&dev->kobj, "device");
  713. }
  714. }
  715. ueventattrError:
  716. device_remove_file(dev, &uevent_attr);
  717. attrError:
  718. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  719. kobject_del(&dev->kobj);
  720. Error:
  721. if (parent)
  722. put_device(parent);
  723. goto Done;
  724. }
  725. /**
  726. * device_register - register a device with the system.
  727. * @dev: pointer to the device structure
  728. *
  729. * This happens in two clean steps - initialize the device
  730. * and add it to the system. The two steps can be called
  731. * separately, but this is the easiest and most common.
  732. * I.e. you should only call the two helpers separately if
  733. * have a clearly defined need to use and refcount the device
  734. * before it is added to the hierarchy.
  735. */
  736. int device_register(struct device *dev)
  737. {
  738. device_initialize(dev);
  739. return device_add(dev);
  740. }
  741. /**
  742. * get_device - increment reference count for device.
  743. * @dev: device.
  744. *
  745. * This simply forwards the call to kobject_get(), though
  746. * we do take care to provide for the case that we get a NULL
  747. * pointer passed in.
  748. */
  749. struct device * get_device(struct device * dev)
  750. {
  751. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  752. }
  753. /**
  754. * put_device - decrement reference count.
  755. * @dev: device in question.
  756. */
  757. void put_device(struct device * dev)
  758. {
  759. if (dev)
  760. kobject_put(&dev->kobj);
  761. }
  762. /**
  763. * device_del - delete device from system.
  764. * @dev: device.
  765. *
  766. * This is the first part of the device unregistration
  767. * sequence. This removes the device from the lists we control
  768. * from here, has it removed from the other driver model
  769. * subsystems it was added to in device_add(), and removes it
  770. * from the kobject hierarchy.
  771. *
  772. * NOTE: this should be called manually _iff_ device_add() was
  773. * also called manually.
  774. */
  775. void device_del(struct device * dev)
  776. {
  777. struct device * parent = dev->parent;
  778. struct class_interface *class_intf;
  779. if (parent)
  780. klist_del(&dev->knode_parent);
  781. if (MAJOR(dev->devt))
  782. device_remove_file(dev, &devt_attr);
  783. if (dev->class) {
  784. sysfs_remove_link(&dev->kobj, "subsystem");
  785. /* If this is not a "fake" compatible device, remove the
  786. * symlink from the class to the device. */
  787. if (dev->kobj.parent != &dev->class->subsys.kobj)
  788. sysfs_remove_link(&dev->class->subsys.kobj,
  789. dev->bus_id);
  790. if (parent) {
  791. #ifdef CONFIG_SYSFS_DEPRECATED
  792. char *class_name = make_class_name(dev->class->name,
  793. &dev->kobj);
  794. if (class_name)
  795. sysfs_remove_link(&dev->parent->kobj,
  796. class_name);
  797. kfree(class_name);
  798. #endif
  799. sysfs_remove_link(&dev->kobj, "device");
  800. }
  801. down(&dev->class->sem);
  802. /* notify any interfaces that the device is now gone */
  803. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  804. if (class_intf->remove_dev)
  805. class_intf->remove_dev(dev, class_intf);
  806. /* remove the device from the class list */
  807. list_del_init(&dev->node);
  808. up(&dev->class->sem);
  809. /* If we live in a parent class-directory, unreference it */
  810. if (dev->kobj.parent->kset == &dev->class->class_dirs) {
  811. struct device *d;
  812. int other = 0;
  813. /*
  814. * if we are the last child of our class, delete
  815. * our class-directory at this parent
  816. */
  817. down(&dev->class->sem);
  818. list_for_each_entry(d, &dev->class->devices, node) {
  819. if (d == dev)
  820. continue;
  821. if (d->kobj.parent == dev->kobj.parent) {
  822. other = 1;
  823. break;
  824. }
  825. }
  826. if (!other)
  827. kobject_del(dev->kobj.parent);
  828. kobject_put(dev->kobj.parent);
  829. up(&dev->class->sem);
  830. }
  831. }
  832. device_remove_file(dev, &uevent_attr);
  833. device_remove_attrs(dev);
  834. bus_remove_device(dev);
  835. /*
  836. * Some platform devices are driven without driver attached
  837. * and managed resources may have been acquired. Make sure
  838. * all resources are released.
  839. */
  840. devres_release_all(dev);
  841. /* Notify the platform of the removal, in case they
  842. * need to do anything...
  843. */
  844. if (platform_notify_remove)
  845. platform_notify_remove(dev);
  846. if (dev->bus)
  847. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  848. BUS_NOTIFY_DEL_DEVICE, dev);
  849. device_pm_remove(dev);
  850. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  851. kobject_del(&dev->kobj);
  852. if (parent)
  853. put_device(parent);
  854. }
  855. /**
  856. * device_unregister - unregister device from system.
  857. * @dev: device going away.
  858. *
  859. * We do this in two parts, like we do device_register(). First,
  860. * we remove it from all the subsystems with device_del(), then
  861. * we decrement the reference count via put_device(). If that
  862. * is the final reference count, the device will be cleaned up
  863. * via device_release() above. Otherwise, the structure will
  864. * stick around until the final reference to the device is dropped.
  865. */
  866. void device_unregister(struct device * dev)
  867. {
  868. pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
  869. device_del(dev);
  870. put_device(dev);
  871. }
  872. static struct device * next_device(struct klist_iter * i)
  873. {
  874. struct klist_node * n = klist_next(i);
  875. return n ? container_of(n, struct device, knode_parent) : NULL;
  876. }
  877. /**
  878. * device_for_each_child - device child iterator.
  879. * @parent: parent struct device.
  880. * @data: data for the callback.
  881. * @fn: function to be called for each device.
  882. *
  883. * Iterate over @parent's child devices, and call @fn for each,
  884. * passing it @data.
  885. *
  886. * We check the return of @fn each time. If it returns anything
  887. * other than 0, we break out and return that value.
  888. */
  889. int device_for_each_child(struct device * parent, void * data,
  890. int (*fn)(struct device *, void *))
  891. {
  892. struct klist_iter i;
  893. struct device * child;
  894. int error = 0;
  895. klist_iter_init(&parent->klist_children, &i);
  896. while ((child = next_device(&i)) && !error)
  897. error = fn(child, data);
  898. klist_iter_exit(&i);
  899. return error;
  900. }
  901. /**
  902. * device_find_child - device iterator for locating a particular device.
  903. * @parent: parent struct device
  904. * @data: Data to pass to match function
  905. * @match: Callback function to check device
  906. *
  907. * This is similar to the device_for_each_child() function above, but it
  908. * returns a reference to a device that is 'found' for later use, as
  909. * determined by the @match callback.
  910. *
  911. * The callback should return 0 if the device doesn't match and non-zero
  912. * if it does. If the callback returns non-zero and a reference to the
  913. * current device can be obtained, this function will return to the caller
  914. * and not iterate over any more devices.
  915. */
  916. struct device * device_find_child(struct device *parent, void *data,
  917. int (*match)(struct device *, void *))
  918. {
  919. struct klist_iter i;
  920. struct device *child;
  921. if (!parent)
  922. return NULL;
  923. klist_iter_init(&parent->klist_children, &i);
  924. while ((child = next_device(&i)))
  925. if (match(child, data) && get_device(child))
  926. break;
  927. klist_iter_exit(&i);
  928. return child;
  929. }
  930. int __init devices_init(void)
  931. {
  932. return subsystem_register(&devices_subsys);
  933. }
  934. EXPORT_SYMBOL_GPL(device_for_each_child);
  935. EXPORT_SYMBOL_GPL(device_find_child);
  936. EXPORT_SYMBOL_GPL(device_initialize);
  937. EXPORT_SYMBOL_GPL(device_add);
  938. EXPORT_SYMBOL_GPL(device_register);
  939. EXPORT_SYMBOL_GPL(device_del);
  940. EXPORT_SYMBOL_GPL(device_unregister);
  941. EXPORT_SYMBOL_GPL(get_device);
  942. EXPORT_SYMBOL_GPL(put_device);
  943. EXPORT_SYMBOL_GPL(device_create_file);
  944. EXPORT_SYMBOL_GPL(device_remove_file);
  945. static void device_create_release(struct device *dev)
  946. {
  947. pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
  948. kfree(dev);
  949. }
  950. /**
  951. * device_create - creates a device and registers it with sysfs
  952. * @class: pointer to the struct class that this device should be registered to
  953. * @parent: pointer to the parent struct device of this new device, if any
  954. * @devt: the dev_t for the char device to be added
  955. * @fmt: string for the device's name
  956. *
  957. * This function can be used by char device classes. A struct device
  958. * will be created in sysfs, registered to the specified class.
  959. *
  960. * A "dev" file will be created, showing the dev_t for the device, if
  961. * the dev_t is not 0,0.
  962. * If a pointer to a parent struct device is passed in, the newly created
  963. * struct device will be a child of that device in sysfs.
  964. * The pointer to the struct device will be returned from the call.
  965. * Any further sysfs files that might be required can be created using this
  966. * pointer.
  967. *
  968. * Note: the struct class passed to this function must have previously
  969. * been created with a call to class_create().
  970. */
  971. struct device *device_create(struct class *class, struct device *parent,
  972. dev_t devt, const char *fmt, ...)
  973. {
  974. va_list args;
  975. struct device *dev = NULL;
  976. int retval = -ENODEV;
  977. if (class == NULL || IS_ERR(class))
  978. goto error;
  979. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  980. if (!dev) {
  981. retval = -ENOMEM;
  982. goto error;
  983. }
  984. dev->devt = devt;
  985. dev->class = class;
  986. dev->parent = parent;
  987. dev->release = device_create_release;
  988. va_start(args, fmt);
  989. vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
  990. va_end(args);
  991. retval = device_register(dev);
  992. if (retval)
  993. goto error;
  994. return dev;
  995. error:
  996. kfree(dev);
  997. return ERR_PTR(retval);
  998. }
  999. EXPORT_SYMBOL_GPL(device_create);
  1000. /**
  1001. * device_destroy - removes a device that was created with device_create()
  1002. * @class: pointer to the struct class that this device was registered with
  1003. * @devt: the dev_t of the device that was previously registered
  1004. *
  1005. * This call unregisters and cleans up a device that was created with a
  1006. * call to device_create().
  1007. */
  1008. void device_destroy(struct class *class, dev_t devt)
  1009. {
  1010. struct device *dev = NULL;
  1011. struct device *dev_tmp;
  1012. down(&class->sem);
  1013. list_for_each_entry(dev_tmp, &class->devices, node) {
  1014. if (dev_tmp->devt == devt) {
  1015. dev = dev_tmp;
  1016. break;
  1017. }
  1018. }
  1019. up(&class->sem);
  1020. if (dev)
  1021. device_unregister(dev);
  1022. }
  1023. EXPORT_SYMBOL_GPL(device_destroy);
  1024. /**
  1025. * device_rename - renames a device
  1026. * @dev: the pointer to the struct device to be renamed
  1027. * @new_name: the new name of the device
  1028. */
  1029. int device_rename(struct device *dev, char *new_name)
  1030. {
  1031. char *old_class_name = NULL;
  1032. char *new_class_name = NULL;
  1033. char *old_device_name = NULL;
  1034. int error;
  1035. dev = get_device(dev);
  1036. if (!dev)
  1037. return -EINVAL;
  1038. pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
  1039. #ifdef CONFIG_SYSFS_DEPRECATED
  1040. if ((dev->class) && (dev->parent))
  1041. old_class_name = make_class_name(dev->class->name, &dev->kobj);
  1042. #endif
  1043. old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  1044. if (!old_device_name) {
  1045. error = -ENOMEM;
  1046. goto out;
  1047. }
  1048. strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE);
  1049. strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
  1050. error = kobject_rename(&dev->kobj, new_name);
  1051. if (error) {
  1052. strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
  1053. goto out;
  1054. }
  1055. #ifdef CONFIG_SYSFS_DEPRECATED
  1056. if (old_class_name) {
  1057. new_class_name = make_class_name(dev->class->name, &dev->kobj);
  1058. if (new_class_name) {
  1059. error = sysfs_create_link(&dev->parent->kobj,
  1060. &dev->kobj, new_class_name);
  1061. if (error)
  1062. goto out;
  1063. sysfs_remove_link(&dev->parent->kobj, old_class_name);
  1064. }
  1065. }
  1066. #else
  1067. if (dev->class) {
  1068. sysfs_remove_link(&dev->class->subsys.kobj, old_device_name);
  1069. error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
  1070. dev->bus_id);
  1071. if (error) {
  1072. dev_err(dev, "%s: sysfs_create_symlink failed (%d)\n",
  1073. __FUNCTION__, error);
  1074. }
  1075. }
  1076. #endif
  1077. out:
  1078. put_device(dev);
  1079. kfree(new_class_name);
  1080. kfree(old_class_name);
  1081. kfree(old_device_name);
  1082. return error;
  1083. }
  1084. EXPORT_SYMBOL_GPL(device_rename);
  1085. static int device_move_class_links(struct device *dev,
  1086. struct device *old_parent,
  1087. struct device *new_parent)
  1088. {
  1089. int error = 0;
  1090. #ifdef CONFIG_SYSFS_DEPRECATED
  1091. char *class_name;
  1092. class_name = make_class_name(dev->class->name, &dev->kobj);
  1093. if (!class_name) {
  1094. error = -ENOMEM;
  1095. goto out;
  1096. }
  1097. if (old_parent) {
  1098. sysfs_remove_link(&dev->kobj, "device");
  1099. sysfs_remove_link(&old_parent->kobj, class_name);
  1100. }
  1101. if (new_parent) {
  1102. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1103. "device");
  1104. if (error)
  1105. goto out;
  1106. error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
  1107. class_name);
  1108. if (error)
  1109. sysfs_remove_link(&dev->kobj, "device");
  1110. }
  1111. else
  1112. error = 0;
  1113. out:
  1114. kfree(class_name);
  1115. return error;
  1116. #else
  1117. if (old_parent)
  1118. sysfs_remove_link(&dev->kobj, "device");
  1119. if (new_parent)
  1120. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1121. "device");
  1122. return error;
  1123. #endif
  1124. }
  1125. /**
  1126. * device_move - moves a device to a new parent
  1127. * @dev: the pointer to the struct device to be moved
  1128. * @new_parent: the new parent of the device (can by NULL)
  1129. */
  1130. int device_move(struct device *dev, struct device *new_parent)
  1131. {
  1132. int error;
  1133. struct device *old_parent;
  1134. struct kobject *new_parent_kobj;
  1135. dev = get_device(dev);
  1136. if (!dev)
  1137. return -EINVAL;
  1138. new_parent = get_device(new_parent);
  1139. new_parent_kobj = get_device_parent (dev, new_parent);
  1140. if (IS_ERR(new_parent_kobj)) {
  1141. error = PTR_ERR(new_parent_kobj);
  1142. put_device(new_parent);
  1143. goto out;
  1144. }
  1145. pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
  1146. new_parent ? new_parent->bus_id : "<NULL>");
  1147. error = kobject_move(&dev->kobj, new_parent_kobj);
  1148. if (error) {
  1149. put_device(new_parent);
  1150. goto out;
  1151. }
  1152. old_parent = dev->parent;
  1153. dev->parent = new_parent;
  1154. if (old_parent)
  1155. klist_remove(&dev->knode_parent);
  1156. if (new_parent)
  1157. klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
  1158. if (!dev->class)
  1159. goto out_put;
  1160. error = device_move_class_links(dev, old_parent, new_parent);
  1161. if (error) {
  1162. /* We ignore errors on cleanup since we're hosed anyway... */
  1163. device_move_class_links(dev, new_parent, old_parent);
  1164. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  1165. if (new_parent)
  1166. klist_remove(&dev->knode_parent);
  1167. if (old_parent)
  1168. klist_add_tail(&dev->knode_parent,
  1169. &old_parent->klist_children);
  1170. }
  1171. put_device(new_parent);
  1172. goto out;
  1173. }
  1174. out_put:
  1175. put_device(old_parent);
  1176. out:
  1177. put_device(dev);
  1178. return error;
  1179. }
  1180. EXPORT_SYMBOL_GPL(device_move);