core.c 52 KB

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