core.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  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 <linux/genhd.h>
  21. #include <linux/kallsyms.h>
  22. #include <linux/semaphore.h>
  23. #include <linux/mutex.h>
  24. #include "base.h"
  25. #include "power/power.h"
  26. int (*platform_notify)(struct device *dev) = NULL;
  27. int (*platform_notify_remove)(struct device *dev) = NULL;
  28. static struct kobject *dev_kobj;
  29. struct kobject *sysfs_dev_char_kobj;
  30. struct kobject *sysfs_dev_block_kobj;
  31. #ifdef CONFIG_BLOCK
  32. static inline int device_is_not_partition(struct device *dev)
  33. {
  34. return !(dev->type == &part_type);
  35. }
  36. #else
  37. static inline int device_is_not_partition(struct device *dev)
  38. {
  39. return 1;
  40. }
  41. #endif
  42. /**
  43. * dev_driver_string - Return a device's driver name, if at all possible
  44. * @dev: struct device to get the name of
  45. *
  46. * Will return the device's driver's name if it is bound to a device. If
  47. * the device is not bound to a device, it will return the name of the bus
  48. * it is attached to. If it is not attached to a bus either, an empty
  49. * string will be returned.
  50. */
  51. const char *dev_driver_string(const struct device *dev)
  52. {
  53. return dev->driver ? dev->driver->name :
  54. (dev->bus ? dev->bus->name :
  55. (dev->class ? dev->class->name : ""));
  56. }
  57. EXPORT_SYMBOL(dev_driver_string);
  58. #define to_dev(obj) container_of(obj, struct device, kobj)
  59. #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
  60. static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
  61. char *buf)
  62. {
  63. struct device_attribute *dev_attr = to_dev_attr(attr);
  64. struct device *dev = to_dev(kobj);
  65. ssize_t ret = -EIO;
  66. if (dev_attr->show)
  67. ret = dev_attr->show(dev, dev_attr, buf);
  68. if (ret >= (ssize_t)PAGE_SIZE) {
  69. print_symbol("dev_attr_show: %s returned bad count\n",
  70. (unsigned long)dev_attr->show);
  71. }
  72. return ret;
  73. }
  74. static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
  75. const char *buf, size_t count)
  76. {
  77. struct device_attribute *dev_attr = to_dev_attr(attr);
  78. struct device *dev = to_dev(kobj);
  79. ssize_t ret = -EIO;
  80. if (dev_attr->store)
  81. ret = dev_attr->store(dev, dev_attr, buf, count);
  82. return ret;
  83. }
  84. static struct sysfs_ops dev_sysfs_ops = {
  85. .show = dev_attr_show,
  86. .store = dev_attr_store,
  87. };
  88. /**
  89. * device_release - free device structure.
  90. * @kobj: device's kobject.
  91. *
  92. * This is called once the reference count for the object
  93. * reaches 0. We forward the call to the device's release
  94. * method, which should handle actually freeing the structure.
  95. */
  96. static void device_release(struct kobject *kobj)
  97. {
  98. struct device *dev = to_dev(kobj);
  99. struct device_private *p = dev->p;
  100. if (dev->release)
  101. dev->release(dev);
  102. else if (dev->type && dev->type->release)
  103. dev->type->release(dev);
  104. else if (dev->class && dev->class->dev_release)
  105. dev->class->dev_release(dev);
  106. else
  107. WARN(1, KERN_ERR "Device '%s' does not have a release() "
  108. "function, it is broken and must be fixed.\n",
  109. dev_name(dev));
  110. kfree(p);
  111. }
  112. static struct kobj_type device_ktype = {
  113. .release = device_release,
  114. .sysfs_ops = &dev_sysfs_ops,
  115. };
  116. static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
  117. {
  118. struct kobj_type *ktype = get_ktype(kobj);
  119. if (ktype == &device_ktype) {
  120. struct device *dev = to_dev(kobj);
  121. if (dev->uevent_suppress)
  122. return 0;
  123. if (dev->bus)
  124. return 1;
  125. if (dev->class)
  126. return 1;
  127. }
  128. return 0;
  129. }
  130. static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
  131. {
  132. struct device *dev = to_dev(kobj);
  133. if (dev->bus)
  134. return dev->bus->name;
  135. if (dev->class)
  136. return dev->class->name;
  137. return NULL;
  138. }
  139. static int dev_uevent(struct kset *kset, struct kobject *kobj,
  140. struct kobj_uevent_env *env)
  141. {
  142. struct device *dev = to_dev(kobj);
  143. int retval = 0;
  144. /* add the major/minor if present */
  145. if (MAJOR(dev->devt)) {
  146. add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
  147. add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
  148. }
  149. if (dev->type && dev->type->name)
  150. add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
  151. if (dev->driver)
  152. add_uevent_var(env, "DRIVER=%s", dev->driver->name);
  153. #ifdef CONFIG_SYSFS_DEPRECATED
  154. if (dev->class) {
  155. struct device *parent = dev->parent;
  156. /* find first bus device in parent chain */
  157. while (parent && !parent->bus)
  158. parent = parent->parent;
  159. if (parent && parent->bus) {
  160. const char *path;
  161. path = kobject_get_path(&parent->kobj, GFP_KERNEL);
  162. if (path) {
  163. add_uevent_var(env, "PHYSDEVPATH=%s", path);
  164. kfree(path);
  165. }
  166. add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
  167. if (parent->driver)
  168. add_uevent_var(env, "PHYSDEVDRIVER=%s",
  169. parent->driver->name);
  170. }
  171. } else if (dev->bus) {
  172. add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
  173. if (dev->driver)
  174. add_uevent_var(env, "PHYSDEVDRIVER=%s",
  175. dev->driver->name);
  176. }
  177. #endif
  178. /* have the bus specific function add its stuff */
  179. if (dev->bus && dev->bus->uevent) {
  180. retval = dev->bus->uevent(dev, env);
  181. if (retval)
  182. pr_debug("device: '%s': %s: bus uevent() returned %d\n",
  183. dev_name(dev), __func__, retval);
  184. }
  185. /* have the class specific function add its stuff */
  186. if (dev->class && dev->class->dev_uevent) {
  187. retval = dev->class->dev_uevent(dev, env);
  188. if (retval)
  189. pr_debug("device: '%s': %s: class uevent() "
  190. "returned %d\n", dev_name(dev),
  191. __func__, retval);
  192. }
  193. /* have the device type specific fuction add its stuff */
  194. if (dev->type && dev->type->uevent) {
  195. retval = dev->type->uevent(dev, env);
  196. if (retval)
  197. pr_debug("device: '%s': %s: dev_type uevent() "
  198. "returned %d\n", dev_name(dev),
  199. __func__, retval);
  200. }
  201. return retval;
  202. }
  203. static struct kset_uevent_ops device_uevent_ops = {
  204. .filter = dev_uevent_filter,
  205. .name = dev_uevent_name,
  206. .uevent = dev_uevent,
  207. };
  208. static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
  209. char *buf)
  210. {
  211. struct kobject *top_kobj;
  212. struct kset *kset;
  213. struct kobj_uevent_env *env = NULL;
  214. int i;
  215. size_t count = 0;
  216. int retval;
  217. /* search the kset, the device belongs to */
  218. top_kobj = &dev->kobj;
  219. while (!top_kobj->kset && top_kobj->parent)
  220. top_kobj = top_kobj->parent;
  221. if (!top_kobj->kset)
  222. goto out;
  223. kset = top_kobj->kset;
  224. if (!kset->uevent_ops || !kset->uevent_ops->uevent)
  225. goto out;
  226. /* respect filter */
  227. if (kset->uevent_ops && kset->uevent_ops->filter)
  228. if (!kset->uevent_ops->filter(kset, &dev->kobj))
  229. goto out;
  230. env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  231. if (!env)
  232. return -ENOMEM;
  233. /* let the kset specific function add its keys */
  234. retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
  235. if (retval)
  236. goto out;
  237. /* copy keys to file */
  238. for (i = 0; i < env->envp_idx; i++)
  239. count += sprintf(&buf[count], "%s\n", env->envp[i]);
  240. out:
  241. kfree(env);
  242. return count;
  243. }
  244. static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
  245. const char *buf, size_t count)
  246. {
  247. enum kobject_action action;
  248. if (kobject_action_type(buf, count, &action) == 0) {
  249. kobject_uevent(&dev->kobj, action);
  250. goto out;
  251. }
  252. dev_err(dev, "uevent: unsupported action-string; this will "
  253. "be ignored in a future kernel version\n");
  254. kobject_uevent(&dev->kobj, KOBJ_ADD);
  255. out:
  256. return count;
  257. }
  258. static struct device_attribute uevent_attr =
  259. __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
  260. static int device_add_attributes(struct device *dev,
  261. struct device_attribute *attrs)
  262. {
  263. int error = 0;
  264. int i;
  265. if (attrs) {
  266. for (i = 0; attr_name(attrs[i]); i++) {
  267. error = device_create_file(dev, &attrs[i]);
  268. if (error)
  269. break;
  270. }
  271. if (error)
  272. while (--i >= 0)
  273. device_remove_file(dev, &attrs[i]);
  274. }
  275. return error;
  276. }
  277. static void device_remove_attributes(struct device *dev,
  278. struct device_attribute *attrs)
  279. {
  280. int i;
  281. if (attrs)
  282. for (i = 0; attr_name(attrs[i]); i++)
  283. device_remove_file(dev, &attrs[i]);
  284. }
  285. static int device_add_groups(struct device *dev,
  286. struct attribute_group **groups)
  287. {
  288. int error = 0;
  289. int i;
  290. if (groups) {
  291. for (i = 0; groups[i]; i++) {
  292. error = sysfs_create_group(&dev->kobj, groups[i]);
  293. if (error) {
  294. while (--i >= 0)
  295. sysfs_remove_group(&dev->kobj,
  296. groups[i]);
  297. break;
  298. }
  299. }
  300. }
  301. return error;
  302. }
  303. static void device_remove_groups(struct device *dev,
  304. struct attribute_group **groups)
  305. {
  306. int i;
  307. if (groups)
  308. for (i = 0; groups[i]; i++)
  309. sysfs_remove_group(&dev->kobj, groups[i]);
  310. }
  311. static int device_add_attrs(struct device *dev)
  312. {
  313. struct class *class = dev->class;
  314. struct device_type *type = dev->type;
  315. int error;
  316. if (class) {
  317. error = device_add_attributes(dev, class->dev_attrs);
  318. if (error)
  319. return error;
  320. }
  321. if (type) {
  322. error = device_add_groups(dev, type->groups);
  323. if (error)
  324. goto err_remove_class_attrs;
  325. }
  326. error = device_add_groups(dev, dev->groups);
  327. if (error)
  328. goto err_remove_type_groups;
  329. return 0;
  330. err_remove_type_groups:
  331. if (type)
  332. device_remove_groups(dev, type->groups);
  333. err_remove_class_attrs:
  334. if (class)
  335. device_remove_attributes(dev, class->dev_attrs);
  336. return error;
  337. }
  338. static void device_remove_attrs(struct device *dev)
  339. {
  340. struct class *class = dev->class;
  341. struct device_type *type = dev->type;
  342. device_remove_groups(dev, dev->groups);
  343. if (type)
  344. device_remove_groups(dev, type->groups);
  345. if (class)
  346. device_remove_attributes(dev, class->dev_attrs);
  347. }
  348. static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
  349. char *buf)
  350. {
  351. return print_dev_t(buf, dev->devt);
  352. }
  353. static struct device_attribute devt_attr =
  354. __ATTR(dev, S_IRUGO, show_dev, NULL);
  355. /* kset to create /sys/devices/ */
  356. struct kset *devices_kset;
  357. /**
  358. * device_create_file - create sysfs attribute file for device.
  359. * @dev: device.
  360. * @attr: device attribute descriptor.
  361. */
  362. int device_create_file(struct device *dev, struct device_attribute *attr)
  363. {
  364. int error = 0;
  365. if (dev)
  366. error = sysfs_create_file(&dev->kobj, &attr->attr);
  367. return error;
  368. }
  369. /**
  370. * device_remove_file - remove sysfs attribute file.
  371. * @dev: device.
  372. * @attr: device attribute descriptor.
  373. */
  374. void device_remove_file(struct device *dev, struct device_attribute *attr)
  375. {
  376. if (dev)
  377. sysfs_remove_file(&dev->kobj, &attr->attr);
  378. }
  379. /**
  380. * device_create_bin_file - create sysfs binary attribute file for device.
  381. * @dev: device.
  382. * @attr: device binary attribute descriptor.
  383. */
  384. int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
  385. {
  386. int error = -EINVAL;
  387. if (dev)
  388. error = sysfs_create_bin_file(&dev->kobj, attr);
  389. return error;
  390. }
  391. EXPORT_SYMBOL_GPL(device_create_bin_file);
  392. /**
  393. * device_remove_bin_file - remove sysfs binary attribute file
  394. * @dev: device.
  395. * @attr: device binary attribute descriptor.
  396. */
  397. void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
  398. {
  399. if (dev)
  400. sysfs_remove_bin_file(&dev->kobj, attr);
  401. }
  402. EXPORT_SYMBOL_GPL(device_remove_bin_file);
  403. /**
  404. * device_schedule_callback_owner - helper to schedule a callback for a device
  405. * @dev: device.
  406. * @func: callback function to invoke later.
  407. * @owner: module owning the callback routine
  408. *
  409. * Attribute methods must not unregister themselves or their parent device
  410. * (which would amount to the same thing). Attempts to do so will deadlock,
  411. * since unregistration is mutually exclusive with driver callbacks.
  412. *
  413. * Instead methods can call this routine, which will attempt to allocate
  414. * and schedule a workqueue request to call back @func with @dev as its
  415. * argument in the workqueue's process context. @dev will be pinned until
  416. * @func returns.
  417. *
  418. * This routine is usually called via the inline device_schedule_callback(),
  419. * which automatically sets @owner to THIS_MODULE.
  420. *
  421. * Returns 0 if the request was submitted, -ENOMEM if storage could not
  422. * be allocated, -ENODEV if a reference to @owner isn't available.
  423. *
  424. * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
  425. * underlying sysfs routine (since it is intended for use by attribute
  426. * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
  427. */
  428. int device_schedule_callback_owner(struct device *dev,
  429. void (*func)(struct device *), struct module *owner)
  430. {
  431. return sysfs_schedule_callback(&dev->kobj,
  432. (void (*)(void *)) func, dev, owner);
  433. }
  434. EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
  435. static void klist_children_get(struct klist_node *n)
  436. {
  437. struct device *dev = container_of(n, struct device, knode_parent);
  438. get_device(dev);
  439. }
  440. static void klist_children_put(struct klist_node *n)
  441. {
  442. struct device *dev = container_of(n, struct device, knode_parent);
  443. put_device(dev);
  444. }
  445. /**
  446. * device_initialize - init device structure.
  447. * @dev: device.
  448. *
  449. * This prepares the device for use by other layers by initializing
  450. * its fields.
  451. * It is the first half of device_register(), if called by
  452. * that function, though it can also be called separately, so one
  453. * may use @dev's fields. In particular, get_device()/put_device()
  454. * may be used for reference counting of @dev after calling this
  455. * function.
  456. *
  457. * NOTE: Use put_device() to give up your reference instead of freeing
  458. * @dev directly once you have called this function.
  459. */
  460. void device_initialize(struct device *dev)
  461. {
  462. dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
  463. if (!dev->p) {
  464. WARN_ON(1);
  465. return;
  466. }
  467. dev->p->device = dev;
  468. dev->kobj.kset = devices_kset;
  469. kobject_init(&dev->kobj, &device_ktype);
  470. klist_init(&dev->klist_children, klist_children_get,
  471. klist_children_put);
  472. INIT_LIST_HEAD(&dev->dma_pools);
  473. init_MUTEX(&dev->sem);
  474. spin_lock_init(&dev->devres_lock);
  475. INIT_LIST_HEAD(&dev->devres_head);
  476. device_init_wakeup(dev, 0);
  477. device_pm_init(dev);
  478. set_dev_node(dev, -1);
  479. }
  480. #ifdef CONFIG_SYSFS_DEPRECATED
  481. static struct kobject *get_device_parent(struct device *dev,
  482. struct device *parent)
  483. {
  484. /* class devices without a parent live in /sys/class/<classname>/ */
  485. if (dev->class && (!parent || parent->class != dev->class))
  486. return &dev->class->p->class_subsys.kobj;
  487. /* all other devices keep their parent */
  488. else if (parent)
  489. return &parent->kobj;
  490. return NULL;
  491. }
  492. static inline void cleanup_device_parent(struct device *dev) {}
  493. static inline void cleanup_glue_dir(struct device *dev,
  494. struct kobject *glue_dir) {}
  495. #else
  496. static struct kobject *virtual_device_parent(struct device *dev)
  497. {
  498. static struct kobject *virtual_dir = NULL;
  499. if (!virtual_dir)
  500. virtual_dir = kobject_create_and_add("virtual",
  501. &devices_kset->kobj);
  502. return virtual_dir;
  503. }
  504. static struct kobject *get_device_parent(struct device *dev,
  505. struct device *parent)
  506. {
  507. int retval;
  508. if (dev->class) {
  509. struct kobject *kobj = NULL;
  510. struct kobject *parent_kobj;
  511. struct kobject *k;
  512. /*
  513. * If we have no parent, we live in "virtual".
  514. * Class-devices with a non class-device as parent, live
  515. * in a "glue" directory to prevent namespace collisions.
  516. */
  517. if (parent == NULL)
  518. parent_kobj = virtual_device_parent(dev);
  519. else if (parent->class)
  520. return &parent->kobj;
  521. else
  522. parent_kobj = &parent->kobj;
  523. /* find our class-directory at the parent and reference it */
  524. spin_lock(&dev->class->p->class_dirs.list_lock);
  525. list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
  526. if (k->parent == parent_kobj) {
  527. kobj = kobject_get(k);
  528. break;
  529. }
  530. spin_unlock(&dev->class->p->class_dirs.list_lock);
  531. if (kobj)
  532. return kobj;
  533. /* or create a new class-directory at the parent device */
  534. k = kobject_create();
  535. if (!k)
  536. return NULL;
  537. k->kset = &dev->class->p->class_dirs;
  538. retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
  539. if (retval < 0) {
  540. kobject_put(k);
  541. return NULL;
  542. }
  543. /* do not emit an uevent for this simple "glue" directory */
  544. return k;
  545. }
  546. if (parent)
  547. return &parent->kobj;
  548. return NULL;
  549. }
  550. static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
  551. {
  552. /* see if we live in a "glue" directory */
  553. if (!glue_dir || !dev->class ||
  554. glue_dir->kset != &dev->class->p->class_dirs)
  555. return;
  556. kobject_put(glue_dir);
  557. }
  558. static void cleanup_device_parent(struct device *dev)
  559. {
  560. cleanup_glue_dir(dev, dev->kobj.parent);
  561. }
  562. #endif
  563. static void setup_parent(struct device *dev, struct device *parent)
  564. {
  565. struct kobject *kobj;
  566. kobj = get_device_parent(dev, parent);
  567. if (kobj)
  568. dev->kobj.parent = kobj;
  569. }
  570. static int device_add_class_symlinks(struct device *dev)
  571. {
  572. int error;
  573. if (!dev->class)
  574. return 0;
  575. error = sysfs_create_link(&dev->kobj,
  576. &dev->class->p->class_subsys.kobj,
  577. "subsystem");
  578. if (error)
  579. goto out;
  580. #ifdef CONFIG_SYSFS_DEPRECATED
  581. /* stacked class devices need a symlink in the class directory */
  582. if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
  583. device_is_not_partition(dev)) {
  584. error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
  585. &dev->kobj, dev_name(dev));
  586. if (error)
  587. goto out_subsys;
  588. }
  589. if (dev->parent && device_is_not_partition(dev)) {
  590. struct device *parent = dev->parent;
  591. char *class_name;
  592. /*
  593. * stacked class devices have the 'device' link
  594. * pointing to the bus device instead of the parent
  595. */
  596. while (parent->class && !parent->bus && parent->parent)
  597. parent = parent->parent;
  598. error = sysfs_create_link(&dev->kobj,
  599. &parent->kobj,
  600. "device");
  601. if (error)
  602. goto out_busid;
  603. class_name = make_class_name(dev->class->name,
  604. &dev->kobj);
  605. if (class_name)
  606. error = sysfs_create_link(&dev->parent->kobj,
  607. &dev->kobj, class_name);
  608. kfree(class_name);
  609. if (error)
  610. goto out_device;
  611. }
  612. return 0;
  613. out_device:
  614. if (dev->parent && device_is_not_partition(dev))
  615. sysfs_remove_link(&dev->kobj, "device");
  616. out_busid:
  617. if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
  618. device_is_not_partition(dev))
  619. sysfs_remove_link(&dev->class->p->class_subsys.kobj,
  620. dev_name(dev));
  621. #else
  622. /* link in the class directory pointing to the device */
  623. error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
  624. &dev->kobj, dev_name(dev));
  625. if (error)
  626. goto out_subsys;
  627. if (dev->parent && device_is_not_partition(dev)) {
  628. error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  629. "device");
  630. if (error)
  631. goto out_busid;
  632. }
  633. return 0;
  634. out_busid:
  635. sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
  636. #endif
  637. out_subsys:
  638. sysfs_remove_link(&dev->kobj, "subsystem");
  639. out:
  640. return error;
  641. }
  642. static void device_remove_class_symlinks(struct device *dev)
  643. {
  644. if (!dev->class)
  645. return;
  646. #ifdef CONFIG_SYSFS_DEPRECATED
  647. if (dev->parent && device_is_not_partition(dev)) {
  648. char *class_name;
  649. class_name = make_class_name(dev->class->name, &dev->kobj);
  650. if (class_name) {
  651. sysfs_remove_link(&dev->parent->kobj, class_name);
  652. kfree(class_name);
  653. }
  654. sysfs_remove_link(&dev->kobj, "device");
  655. }
  656. if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
  657. device_is_not_partition(dev))
  658. sysfs_remove_link(&dev->class->p->class_subsys.kobj,
  659. dev_name(dev));
  660. #else
  661. if (dev->parent && device_is_not_partition(dev))
  662. sysfs_remove_link(&dev->kobj, "device");
  663. sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
  664. #endif
  665. sysfs_remove_link(&dev->kobj, "subsystem");
  666. }
  667. /**
  668. * dev_set_name - set a device name
  669. * @dev: device
  670. * @fmt: format string for the device's name
  671. */
  672. int dev_set_name(struct device *dev, const char *fmt, ...)
  673. {
  674. va_list vargs;
  675. va_start(vargs, fmt);
  676. vsnprintf(dev->bus_id, sizeof(dev->bus_id), fmt, vargs);
  677. va_end(vargs);
  678. return 0;
  679. }
  680. EXPORT_SYMBOL_GPL(dev_set_name);
  681. /**
  682. * device_to_dev_kobj - select a /sys/dev/ directory for the device
  683. * @dev: device
  684. *
  685. * By default we select char/ for new entries. Setting class->dev_obj
  686. * to NULL prevents an entry from being created. class->dev_kobj must
  687. * be set (or cleared) before any devices are registered to the class
  688. * otherwise device_create_sys_dev_entry() and
  689. * device_remove_sys_dev_entry() will disagree about the the presence
  690. * of the link.
  691. */
  692. static struct kobject *device_to_dev_kobj(struct device *dev)
  693. {
  694. struct kobject *kobj;
  695. if (dev->class)
  696. kobj = dev->class->dev_kobj;
  697. else
  698. kobj = sysfs_dev_char_kobj;
  699. return kobj;
  700. }
  701. static int device_create_sys_dev_entry(struct device *dev)
  702. {
  703. struct kobject *kobj = device_to_dev_kobj(dev);
  704. int error = 0;
  705. char devt_str[15];
  706. if (kobj) {
  707. format_dev_t(devt_str, dev->devt);
  708. error = sysfs_create_link(kobj, &dev->kobj, devt_str);
  709. }
  710. return error;
  711. }
  712. static void device_remove_sys_dev_entry(struct device *dev)
  713. {
  714. struct kobject *kobj = device_to_dev_kobj(dev);
  715. char devt_str[15];
  716. if (kobj) {
  717. format_dev_t(devt_str, dev->devt);
  718. sysfs_remove_link(kobj, devt_str);
  719. }
  720. }
  721. /**
  722. * device_add - add device to device hierarchy.
  723. * @dev: device.
  724. *
  725. * This is part 2 of device_register(), though may be called
  726. * separately _iff_ device_initialize() has been called separately.
  727. *
  728. * This adds @dev to the kobject hierarchy via kobject_add(), adds it
  729. * to the global and sibling lists for the device, then
  730. * adds it to the other relevant subsystems of the driver model.
  731. *
  732. * NOTE: _Never_ directly free @dev after calling this function, even
  733. * if it returned an error! Always use put_device() to give up your
  734. * reference instead.
  735. */
  736. int device_add(struct device *dev)
  737. {
  738. struct device *parent = NULL;
  739. struct class_interface *class_intf;
  740. int error = -EINVAL;
  741. dev = get_device(dev);
  742. if (!dev)
  743. goto done;
  744. /* Temporarily support init_name if it is set.
  745. * It will override bus_id for now */
  746. if (dev->init_name)
  747. dev_set_name(dev, "%s", dev->init_name);
  748. if (!strlen(dev->bus_id))
  749. goto done;
  750. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  751. parent = get_device(dev->parent);
  752. setup_parent(dev, parent);
  753. /* use parent numa_node */
  754. if (parent)
  755. set_dev_node(dev, dev_to_node(parent));
  756. /* first, register with generic layer. */
  757. error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev_name(dev));
  758. if (error)
  759. goto Error;
  760. /* notify platform of device entry */
  761. if (platform_notify)
  762. platform_notify(dev);
  763. error = device_create_file(dev, &uevent_attr);
  764. if (error)
  765. goto attrError;
  766. if (MAJOR(dev->devt)) {
  767. error = device_create_file(dev, &devt_attr);
  768. if (error)
  769. goto ueventattrError;
  770. error = device_create_sys_dev_entry(dev);
  771. if (error)
  772. goto devtattrError;
  773. }
  774. error = device_add_class_symlinks(dev);
  775. if (error)
  776. goto SymlinkError;
  777. error = device_add_attrs(dev);
  778. if (error)
  779. goto AttrsError;
  780. error = bus_add_device(dev);
  781. if (error)
  782. goto BusError;
  783. error = dpm_sysfs_add(dev);
  784. if (error)
  785. goto DPMError;
  786. device_pm_add(dev);
  787. /* Notify clients of device addition. This call must come
  788. * after dpm_sysf_add() and before kobject_uevent().
  789. */
  790. if (dev->bus)
  791. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  792. BUS_NOTIFY_ADD_DEVICE, dev);
  793. kobject_uevent(&dev->kobj, KOBJ_ADD);
  794. bus_attach_device(dev);
  795. if (parent)
  796. klist_add_tail(&dev->knode_parent, &parent->klist_children);
  797. if (dev->class) {
  798. mutex_lock(&dev->class->p->class_mutex);
  799. /* tie the class to the device */
  800. klist_add_tail(&dev->knode_class,
  801. &dev->class->p->class_devices);
  802. /* notify any interfaces that the device is here */
  803. list_for_each_entry(class_intf,
  804. &dev->class->p->class_interfaces, node)
  805. if (class_intf->add_dev)
  806. class_intf->add_dev(dev, class_intf);
  807. mutex_unlock(&dev->class->p->class_mutex);
  808. }
  809. done:
  810. put_device(dev);
  811. return error;
  812. DPMError:
  813. bus_remove_device(dev);
  814. BusError:
  815. device_remove_attrs(dev);
  816. AttrsError:
  817. device_remove_class_symlinks(dev);
  818. SymlinkError:
  819. if (MAJOR(dev->devt))
  820. device_remove_sys_dev_entry(dev);
  821. devtattrError:
  822. if (MAJOR(dev->devt))
  823. device_remove_file(dev, &devt_attr);
  824. ueventattrError:
  825. device_remove_file(dev, &uevent_attr);
  826. attrError:
  827. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  828. kobject_del(&dev->kobj);
  829. Error:
  830. cleanup_device_parent(dev);
  831. if (parent)
  832. put_device(parent);
  833. goto done;
  834. }
  835. /**
  836. * device_register - register a device with the system.
  837. * @dev: pointer to the device structure
  838. *
  839. * This happens in two clean steps - initialize the device
  840. * and add it to the system. The two steps can be called
  841. * separately, but this is the easiest and most common.
  842. * I.e. you should only call the two helpers separately if
  843. * have a clearly defined need to use and refcount the device
  844. * before it is added to the hierarchy.
  845. *
  846. * NOTE: _Never_ directly free @dev after calling this function, even
  847. * if it returned an error! Always use put_device() to give up the
  848. * reference initialized in this function instead.
  849. */
  850. int device_register(struct device *dev)
  851. {
  852. device_initialize(dev);
  853. return device_add(dev);
  854. }
  855. /**
  856. * get_device - increment reference count for device.
  857. * @dev: device.
  858. *
  859. * This simply forwards the call to kobject_get(), though
  860. * we do take care to provide for the case that we get a NULL
  861. * pointer passed in.
  862. */
  863. struct device *get_device(struct device *dev)
  864. {
  865. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  866. }
  867. /**
  868. * put_device - decrement reference count.
  869. * @dev: device in question.
  870. */
  871. void put_device(struct device *dev)
  872. {
  873. /* might_sleep(); */
  874. if (dev)
  875. kobject_put(&dev->kobj);
  876. }
  877. /**
  878. * device_del - delete device from system.
  879. * @dev: device.
  880. *
  881. * This is the first part of the device unregistration
  882. * sequence. This removes the device from the lists we control
  883. * from here, has it removed from the other driver model
  884. * subsystems it was added to in device_add(), and removes it
  885. * from the kobject hierarchy.
  886. *
  887. * NOTE: this should be called manually _iff_ device_add() was
  888. * also called manually.
  889. */
  890. void device_del(struct device *dev)
  891. {
  892. struct device *parent = dev->parent;
  893. struct class_interface *class_intf;
  894. /* Notify clients of device removal. This call must come
  895. * before dpm_sysfs_remove().
  896. */
  897. if (dev->bus)
  898. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  899. BUS_NOTIFY_DEL_DEVICE, dev);
  900. device_pm_remove(dev);
  901. dpm_sysfs_remove(dev);
  902. if (parent)
  903. klist_del(&dev->knode_parent);
  904. if (MAJOR(dev->devt)) {
  905. device_remove_sys_dev_entry(dev);
  906. device_remove_file(dev, &devt_attr);
  907. }
  908. if (dev->class) {
  909. device_remove_class_symlinks(dev);
  910. mutex_lock(&dev->class->p->class_mutex);
  911. /* notify any interfaces that the device is now gone */
  912. list_for_each_entry(class_intf,
  913. &dev->class->p->class_interfaces, node)
  914. if (class_intf->remove_dev)
  915. class_intf->remove_dev(dev, class_intf);
  916. /* remove the device from the class list */
  917. klist_del(&dev->knode_class);
  918. mutex_unlock(&dev->class->p->class_mutex);
  919. }
  920. device_remove_file(dev, &uevent_attr);
  921. device_remove_attrs(dev);
  922. bus_remove_device(dev);
  923. /*
  924. * Some platform devices are driven without driver attached
  925. * and managed resources may have been acquired. Make sure
  926. * all resources are released.
  927. */
  928. devres_release_all(dev);
  929. /* Notify the platform of the removal, in case they
  930. * need to do anything...
  931. */
  932. if (platform_notify_remove)
  933. platform_notify_remove(dev);
  934. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  935. cleanup_device_parent(dev);
  936. kobject_del(&dev->kobj);
  937. put_device(parent);
  938. }
  939. /**
  940. * device_unregister - unregister device from system.
  941. * @dev: device going away.
  942. *
  943. * We do this in two parts, like we do device_register(). First,
  944. * we remove it from all the subsystems with device_del(), then
  945. * we decrement the reference count via put_device(). If that
  946. * is the final reference count, the device will be cleaned up
  947. * via device_release() above. Otherwise, the structure will
  948. * stick around until the final reference to the device is dropped.
  949. */
  950. void device_unregister(struct device *dev)
  951. {
  952. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  953. device_del(dev);
  954. put_device(dev);
  955. }
  956. static struct device *next_device(struct klist_iter *i)
  957. {
  958. struct klist_node *n = klist_next(i);
  959. return n ? container_of(n, struct device, knode_parent) : NULL;
  960. }
  961. /**
  962. * device_for_each_child - device child iterator.
  963. * @parent: parent struct device.
  964. * @data: data for the callback.
  965. * @fn: function to be called for each device.
  966. *
  967. * Iterate over @parent's child devices, and call @fn for each,
  968. * passing it @data.
  969. *
  970. * We check the return of @fn each time. If it returns anything
  971. * other than 0, we break out and return that value.
  972. */
  973. int device_for_each_child(struct device *parent, void *data,
  974. int (*fn)(struct device *dev, void *data))
  975. {
  976. struct klist_iter i;
  977. struct device *child;
  978. int error = 0;
  979. klist_iter_init(&parent->klist_children, &i);
  980. while ((child = next_device(&i)) && !error)
  981. error = fn(child, data);
  982. klist_iter_exit(&i);
  983. return error;
  984. }
  985. /**
  986. * device_find_child - device iterator for locating a particular device.
  987. * @parent: parent struct device
  988. * @data: Data to pass to match function
  989. * @match: Callback function to check device
  990. *
  991. * This is similar to the device_for_each_child() function above, but it
  992. * returns a reference to a device that is 'found' for later use, as
  993. * determined by the @match callback.
  994. *
  995. * The callback should return 0 if the device doesn't match and non-zero
  996. * if it does. If the callback returns non-zero and a reference to the
  997. * current device can be obtained, this function will return to the caller
  998. * and not iterate over any more devices.
  999. */
  1000. struct device *device_find_child(struct device *parent, void *data,
  1001. int (*match)(struct device *dev, void *data))
  1002. {
  1003. struct klist_iter i;
  1004. struct device *child;
  1005. if (!parent)
  1006. return NULL;
  1007. klist_iter_init(&parent->klist_children, &i);
  1008. while ((child = next_device(&i)))
  1009. if (match(child, data) && get_device(child))
  1010. break;
  1011. klist_iter_exit(&i);
  1012. return child;
  1013. }
  1014. int __init devices_init(void)
  1015. {
  1016. devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
  1017. if (!devices_kset)
  1018. return -ENOMEM;
  1019. dev_kobj = kobject_create_and_add("dev", NULL);
  1020. if (!dev_kobj)
  1021. goto dev_kobj_err;
  1022. sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
  1023. if (!sysfs_dev_block_kobj)
  1024. goto block_kobj_err;
  1025. sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
  1026. if (!sysfs_dev_char_kobj)
  1027. goto char_kobj_err;
  1028. return 0;
  1029. char_kobj_err:
  1030. kobject_put(sysfs_dev_block_kobj);
  1031. block_kobj_err:
  1032. kobject_put(dev_kobj);
  1033. dev_kobj_err:
  1034. kset_unregister(devices_kset);
  1035. return -ENOMEM;
  1036. }
  1037. EXPORT_SYMBOL_GPL(device_for_each_child);
  1038. EXPORT_SYMBOL_GPL(device_find_child);
  1039. EXPORT_SYMBOL_GPL(device_initialize);
  1040. EXPORT_SYMBOL_GPL(device_add);
  1041. EXPORT_SYMBOL_GPL(device_register);
  1042. EXPORT_SYMBOL_GPL(device_del);
  1043. EXPORT_SYMBOL_GPL(device_unregister);
  1044. EXPORT_SYMBOL_GPL(get_device);
  1045. EXPORT_SYMBOL_GPL(put_device);
  1046. EXPORT_SYMBOL_GPL(device_create_file);
  1047. EXPORT_SYMBOL_GPL(device_remove_file);
  1048. struct root_device
  1049. {
  1050. struct device dev;
  1051. struct module *owner;
  1052. };
  1053. #define to_root_device(dev) container_of(dev, struct root_device, dev)
  1054. static void root_device_release(struct device *dev)
  1055. {
  1056. kfree(to_root_device(dev));
  1057. }
  1058. /**
  1059. * __root_device_register - allocate and register a root device
  1060. * @name: root device name
  1061. * @owner: owner module of the root device, usually THIS_MODULE
  1062. *
  1063. * This function allocates a root device and registers it
  1064. * using device_register(). In order to free the returned
  1065. * device, use root_device_unregister().
  1066. *
  1067. * Root devices are dummy devices which allow other devices
  1068. * to be grouped under /sys/devices. Use this function to
  1069. * allocate a root device and then use it as the parent of
  1070. * any device which should appear under /sys/devices/{name}
  1071. *
  1072. * The /sys/devices/{name} directory will also contain a
  1073. * 'module' symlink which points to the @owner directory
  1074. * in sysfs.
  1075. *
  1076. * Note: You probably want to use root_device_register().
  1077. */
  1078. struct device *__root_device_register(const char *name, struct module *owner)
  1079. {
  1080. struct root_device *root;
  1081. int err = -ENOMEM;
  1082. root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
  1083. if (!root)
  1084. return ERR_PTR(err);
  1085. err = dev_set_name(&root->dev, name);
  1086. if (err) {
  1087. kfree(root);
  1088. return ERR_PTR(err);
  1089. }
  1090. root->dev.release = root_device_release;
  1091. err = device_register(&root->dev);
  1092. if (err) {
  1093. put_device(&root->dev);
  1094. return ERR_PTR(err);
  1095. }
  1096. #ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */
  1097. if (owner) {
  1098. struct module_kobject *mk = &owner->mkobj;
  1099. err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
  1100. if (err) {
  1101. device_unregister(&root->dev);
  1102. return ERR_PTR(err);
  1103. }
  1104. root->owner = owner;
  1105. }
  1106. #endif
  1107. return &root->dev;
  1108. }
  1109. EXPORT_SYMBOL_GPL(__root_device_register);
  1110. /**
  1111. * root_device_unregister - unregister and free a root device
  1112. * @root: device going away.
  1113. *
  1114. * This function unregisters and cleans up a device that was created by
  1115. * root_device_register().
  1116. */
  1117. void root_device_unregister(struct device *dev)
  1118. {
  1119. struct root_device *root = to_root_device(dev);
  1120. if (root->owner)
  1121. sysfs_remove_link(&root->dev.kobj, "module");
  1122. device_unregister(dev);
  1123. }
  1124. EXPORT_SYMBOL_GPL(root_device_unregister);
  1125. static void device_create_release(struct device *dev)
  1126. {
  1127. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  1128. kfree(dev);
  1129. }
  1130. /**
  1131. * device_create_vargs - creates a device and registers it with sysfs
  1132. * @class: pointer to the struct class that this device should be registered to
  1133. * @parent: pointer to the parent struct device of this new device, if any
  1134. * @devt: the dev_t for the char device to be added
  1135. * @drvdata: the data to be added to the device for callbacks
  1136. * @fmt: string for the device's name
  1137. * @args: va_list for the device's name
  1138. *
  1139. * This function can be used by char device classes. A struct device
  1140. * will be created in sysfs, registered to the specified class.
  1141. *
  1142. * A "dev" file will be created, showing the dev_t for the device, if
  1143. * the dev_t is not 0,0.
  1144. * If a pointer to a parent struct device is passed in, the newly created
  1145. * struct device will be a child of that device in sysfs.
  1146. * The pointer to the struct device will be returned from the call.
  1147. * Any further sysfs files that might be required can be created using this
  1148. * pointer.
  1149. *
  1150. * Note: the struct class passed to this function must have previously
  1151. * been created with a call to class_create().
  1152. */
  1153. struct device *device_create_vargs(struct class *class, struct device *parent,
  1154. dev_t devt, void *drvdata, const char *fmt,
  1155. va_list args)
  1156. {
  1157. struct device *dev = NULL;
  1158. int retval = -ENODEV;
  1159. if (class == NULL || IS_ERR(class))
  1160. goto error;
  1161. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1162. if (!dev) {
  1163. retval = -ENOMEM;
  1164. goto error;
  1165. }
  1166. dev->devt = devt;
  1167. dev->class = class;
  1168. dev->parent = parent;
  1169. dev->release = device_create_release;
  1170. dev_set_drvdata(dev, drvdata);
  1171. vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
  1172. retval = device_register(dev);
  1173. if (retval)
  1174. goto error;
  1175. return dev;
  1176. error:
  1177. put_device(dev);
  1178. return ERR_PTR(retval);
  1179. }
  1180. EXPORT_SYMBOL_GPL(device_create_vargs);
  1181. /**
  1182. * device_create - creates a device and registers it with sysfs
  1183. * @class: pointer to the struct class that this device should be registered to
  1184. * @parent: pointer to the parent struct device of this new device, if any
  1185. * @devt: the dev_t for the char device to be added
  1186. * @drvdata: the data to be added to the device for callbacks
  1187. * @fmt: string for the device's name
  1188. *
  1189. * This function can be used by char device classes. A struct device
  1190. * will be created in sysfs, registered to the specified class.
  1191. *
  1192. * A "dev" file will be created, showing the dev_t for the device, if
  1193. * the dev_t is not 0,0.
  1194. * If a pointer to a parent struct device is passed in, the newly created
  1195. * struct device will be a child of that device in sysfs.
  1196. * The pointer to the struct device will be returned from the call.
  1197. * Any further sysfs files that might be required can be created using this
  1198. * pointer.
  1199. *
  1200. * Note: the struct class passed to this function must have previously
  1201. * been created with a call to class_create().
  1202. */
  1203. struct device *device_create(struct class *class, struct device *parent,
  1204. dev_t devt, void *drvdata, const char *fmt, ...)
  1205. {
  1206. va_list vargs;
  1207. struct device *dev;
  1208. va_start(vargs, fmt);
  1209. dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
  1210. va_end(vargs);
  1211. return dev;
  1212. }
  1213. EXPORT_SYMBOL_GPL(device_create);
  1214. static int __match_devt(struct device *dev, void *data)
  1215. {
  1216. dev_t *devt = data;
  1217. return dev->devt == *devt;
  1218. }
  1219. /**
  1220. * device_destroy - removes a device that was created with device_create()
  1221. * @class: pointer to the struct class that this device was registered with
  1222. * @devt: the dev_t of the device that was previously registered
  1223. *
  1224. * This call unregisters and cleans up a device that was created with a
  1225. * call to device_create().
  1226. */
  1227. void device_destroy(struct class *class, dev_t devt)
  1228. {
  1229. struct device *dev;
  1230. dev = class_find_device(class, NULL, &devt, __match_devt);
  1231. if (dev) {
  1232. put_device(dev);
  1233. device_unregister(dev);
  1234. }
  1235. }
  1236. EXPORT_SYMBOL_GPL(device_destroy);
  1237. /**
  1238. * device_rename - renames a device
  1239. * @dev: the pointer to the struct device to be renamed
  1240. * @new_name: the new name of the device
  1241. *
  1242. * It is the responsibility of the caller to provide mutual
  1243. * exclusion between two different calls of device_rename
  1244. * on the same device to ensure that new_name is valid and
  1245. * won't conflict with other devices.
  1246. */
  1247. int device_rename(struct device *dev, char *new_name)
  1248. {
  1249. char *old_class_name = NULL;
  1250. char *new_class_name = NULL;
  1251. char *old_device_name = NULL;
  1252. int error;
  1253. dev = get_device(dev);
  1254. if (!dev)
  1255. return -EINVAL;
  1256. pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
  1257. __func__, new_name);
  1258. #ifdef CONFIG_SYSFS_DEPRECATED
  1259. if ((dev->class) && (dev->parent))
  1260. old_class_name = make_class_name(dev->class->name, &dev->kobj);
  1261. #endif
  1262. old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  1263. if (!old_device_name) {
  1264. error = -ENOMEM;
  1265. goto out;
  1266. }
  1267. strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE);
  1268. strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
  1269. error = kobject_rename(&dev->kobj, new_name);
  1270. if (error) {
  1271. strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
  1272. goto out;
  1273. }
  1274. #ifdef CONFIG_SYSFS_DEPRECATED
  1275. if (old_class_name) {
  1276. new_class_name = make_class_name(dev->class->name, &dev->kobj);
  1277. if (new_class_name) {
  1278. error = sysfs_create_link_nowarn(&dev->parent->kobj,
  1279. &dev->kobj,
  1280. new_class_name);
  1281. if (error)
  1282. goto out;
  1283. sysfs_remove_link(&dev->parent->kobj, old_class_name);
  1284. }
  1285. }
  1286. #else
  1287. if (dev->class) {
  1288. error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
  1289. &dev->kobj, dev_name(dev));
  1290. if (error)
  1291. goto out;
  1292. sysfs_remove_link(&dev->class->p->class_subsys.kobj,
  1293. old_device_name);
  1294. }
  1295. #endif
  1296. out:
  1297. put_device(dev);
  1298. kfree(new_class_name);
  1299. kfree(old_class_name);
  1300. kfree(old_device_name);
  1301. return error;
  1302. }
  1303. EXPORT_SYMBOL_GPL(device_rename);
  1304. static int device_move_class_links(struct device *dev,
  1305. struct device *old_parent,
  1306. struct device *new_parent)
  1307. {
  1308. int error = 0;
  1309. #ifdef CONFIG_SYSFS_DEPRECATED
  1310. char *class_name;
  1311. class_name = make_class_name(dev->class->name, &dev->kobj);
  1312. if (!class_name) {
  1313. error = -ENOMEM;
  1314. goto out;
  1315. }
  1316. if (old_parent) {
  1317. sysfs_remove_link(&dev->kobj, "device");
  1318. sysfs_remove_link(&old_parent->kobj, class_name);
  1319. }
  1320. if (new_parent) {
  1321. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1322. "device");
  1323. if (error)
  1324. goto out;
  1325. error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
  1326. class_name);
  1327. if (error)
  1328. sysfs_remove_link(&dev->kobj, "device");
  1329. } else
  1330. error = 0;
  1331. out:
  1332. kfree(class_name);
  1333. return error;
  1334. #else
  1335. if (old_parent)
  1336. sysfs_remove_link(&dev->kobj, "device");
  1337. if (new_parent)
  1338. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1339. "device");
  1340. return error;
  1341. #endif
  1342. }
  1343. /**
  1344. * device_move - moves a device to a new parent
  1345. * @dev: the pointer to the struct device to be moved
  1346. * @new_parent: the new parent of the device (can by NULL)
  1347. */
  1348. int device_move(struct device *dev, struct device *new_parent)
  1349. {
  1350. int error;
  1351. struct device *old_parent;
  1352. struct kobject *new_parent_kobj;
  1353. dev = get_device(dev);
  1354. if (!dev)
  1355. return -EINVAL;
  1356. new_parent = get_device(new_parent);
  1357. new_parent_kobj = get_device_parent(dev, new_parent);
  1358. pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
  1359. __func__, new_parent ? dev_name(new_parent) : "<NULL>");
  1360. error = kobject_move(&dev->kobj, new_parent_kobj);
  1361. if (error) {
  1362. cleanup_glue_dir(dev, new_parent_kobj);
  1363. put_device(new_parent);
  1364. goto out;
  1365. }
  1366. old_parent = dev->parent;
  1367. dev->parent = new_parent;
  1368. if (old_parent)
  1369. klist_remove(&dev->knode_parent);
  1370. if (new_parent) {
  1371. klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
  1372. set_dev_node(dev, dev_to_node(new_parent));
  1373. }
  1374. if (!dev->class)
  1375. goto out_put;
  1376. error = device_move_class_links(dev, old_parent, new_parent);
  1377. if (error) {
  1378. /* We ignore errors on cleanup since we're hosed anyway... */
  1379. device_move_class_links(dev, new_parent, old_parent);
  1380. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  1381. if (new_parent)
  1382. klist_remove(&dev->knode_parent);
  1383. dev->parent = old_parent;
  1384. if (old_parent) {
  1385. klist_add_tail(&dev->knode_parent,
  1386. &old_parent->klist_children);
  1387. set_dev_node(dev, dev_to_node(old_parent));
  1388. }
  1389. }
  1390. cleanup_glue_dir(dev, new_parent_kobj);
  1391. put_device(new_parent);
  1392. goto out;
  1393. }
  1394. out_put:
  1395. put_device(old_parent);
  1396. out:
  1397. put_device(dev);
  1398. return error;
  1399. }
  1400. EXPORT_SYMBOL_GPL(device_move);
  1401. /**
  1402. * device_shutdown - call ->shutdown() on each device to shutdown.
  1403. */
  1404. void device_shutdown(void)
  1405. {
  1406. struct device *dev, *devn;
  1407. list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
  1408. kobj.entry) {
  1409. if (dev->bus && dev->bus->shutdown) {
  1410. dev_dbg(dev, "shutdown\n");
  1411. dev->bus->shutdown(dev);
  1412. } else if (dev->driver && dev->driver->shutdown) {
  1413. dev_dbg(dev, "shutdown\n");
  1414. dev->driver->shutdown(dev);
  1415. }
  1416. }
  1417. kobject_put(sysfs_dev_char_kobj);
  1418. kobject_put(sysfs_dev_block_kobj);
  1419. kobject_put(dev_kobj);
  1420. }