core.c 43 KB

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