core.c 44 KB

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