core.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038
  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. struct device *device_find_child(struct device *parent, void *data,
  1194. int (*match)(struct device *dev, void *data))
  1195. {
  1196. struct klist_iter i;
  1197. struct device *child;
  1198. if (!parent)
  1199. return NULL;
  1200. klist_iter_init(&parent->p->klist_children, &i);
  1201. while ((child = next_device(&i)))
  1202. if (match(child, data) && get_device(child))
  1203. break;
  1204. klist_iter_exit(&i);
  1205. return child;
  1206. }
  1207. int __init devices_init(void)
  1208. {
  1209. devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
  1210. if (!devices_kset)
  1211. return -ENOMEM;
  1212. dev_kobj = kobject_create_and_add("dev", NULL);
  1213. if (!dev_kobj)
  1214. goto dev_kobj_err;
  1215. sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
  1216. if (!sysfs_dev_block_kobj)
  1217. goto block_kobj_err;
  1218. sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
  1219. if (!sysfs_dev_char_kobj)
  1220. goto char_kobj_err;
  1221. return 0;
  1222. char_kobj_err:
  1223. kobject_put(sysfs_dev_block_kobj);
  1224. block_kobj_err:
  1225. kobject_put(dev_kobj);
  1226. dev_kobj_err:
  1227. kset_unregister(devices_kset);
  1228. return -ENOMEM;
  1229. }
  1230. EXPORT_SYMBOL_GPL(device_for_each_child);
  1231. EXPORT_SYMBOL_GPL(device_find_child);
  1232. EXPORT_SYMBOL_GPL(device_initialize);
  1233. EXPORT_SYMBOL_GPL(device_add);
  1234. EXPORT_SYMBOL_GPL(device_register);
  1235. EXPORT_SYMBOL_GPL(device_del);
  1236. EXPORT_SYMBOL_GPL(device_unregister);
  1237. EXPORT_SYMBOL_GPL(get_device);
  1238. EXPORT_SYMBOL_GPL(put_device);
  1239. EXPORT_SYMBOL_GPL(device_create_file);
  1240. EXPORT_SYMBOL_GPL(device_remove_file);
  1241. struct root_device {
  1242. struct device dev;
  1243. struct module *owner;
  1244. };
  1245. static inline struct root_device *to_root_device(struct device *d)
  1246. {
  1247. return container_of(d, struct root_device, dev);
  1248. }
  1249. static void root_device_release(struct device *dev)
  1250. {
  1251. kfree(to_root_device(dev));
  1252. }
  1253. /**
  1254. * __root_device_register - allocate and register a root device
  1255. * @name: root device name
  1256. * @owner: owner module of the root device, usually THIS_MODULE
  1257. *
  1258. * This function allocates a root device and registers it
  1259. * using device_register(). In order to free the returned
  1260. * device, use root_device_unregister().
  1261. *
  1262. * Root devices are dummy devices which allow other devices
  1263. * to be grouped under /sys/devices. Use this function to
  1264. * allocate a root device and then use it as the parent of
  1265. * any device which should appear under /sys/devices/{name}
  1266. *
  1267. * The /sys/devices/{name} directory will also contain a
  1268. * 'module' symlink which points to the @owner directory
  1269. * in sysfs.
  1270. *
  1271. * Returns &struct device pointer on success, or ERR_PTR() on error.
  1272. *
  1273. * Note: You probably want to use root_device_register().
  1274. */
  1275. struct device *__root_device_register(const char *name, struct module *owner)
  1276. {
  1277. struct root_device *root;
  1278. int err = -ENOMEM;
  1279. root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
  1280. if (!root)
  1281. return ERR_PTR(err);
  1282. err = dev_set_name(&root->dev, "%s", name);
  1283. if (err) {
  1284. kfree(root);
  1285. return ERR_PTR(err);
  1286. }
  1287. root->dev.release = root_device_release;
  1288. err = device_register(&root->dev);
  1289. if (err) {
  1290. put_device(&root->dev);
  1291. return ERR_PTR(err);
  1292. }
  1293. #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
  1294. if (owner) {
  1295. struct module_kobject *mk = &owner->mkobj;
  1296. err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
  1297. if (err) {
  1298. device_unregister(&root->dev);
  1299. return ERR_PTR(err);
  1300. }
  1301. root->owner = owner;
  1302. }
  1303. #endif
  1304. return &root->dev;
  1305. }
  1306. EXPORT_SYMBOL_GPL(__root_device_register);
  1307. /**
  1308. * root_device_unregister - unregister and free a root device
  1309. * @dev: device going away
  1310. *
  1311. * This function unregisters and cleans up a device that was created by
  1312. * root_device_register().
  1313. */
  1314. void root_device_unregister(struct device *dev)
  1315. {
  1316. struct root_device *root = to_root_device(dev);
  1317. if (root->owner)
  1318. sysfs_remove_link(&root->dev.kobj, "module");
  1319. device_unregister(dev);
  1320. }
  1321. EXPORT_SYMBOL_GPL(root_device_unregister);
  1322. static void device_create_release(struct device *dev)
  1323. {
  1324. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  1325. kfree(dev);
  1326. }
  1327. /**
  1328. * device_create_vargs - creates a device and registers it with sysfs
  1329. * @class: pointer to the struct class that this device should be registered to
  1330. * @parent: pointer to the parent struct device of this new device, if any
  1331. * @devt: the dev_t for the char device to be added
  1332. * @drvdata: the data to be added to the device for callbacks
  1333. * @fmt: string for the device's name
  1334. * @args: va_list for the device's name
  1335. *
  1336. * This function can be used by char device classes. A struct device
  1337. * will be created in sysfs, registered to the specified class.
  1338. *
  1339. * A "dev" file will be created, showing the dev_t for the device, if
  1340. * the dev_t is not 0,0.
  1341. * If a pointer to a parent struct device is passed in, the newly created
  1342. * struct device will be a child of that device in sysfs.
  1343. * The pointer to the struct device will be returned from the call.
  1344. * Any further sysfs files that might be required can be created using this
  1345. * pointer.
  1346. *
  1347. * Returns &struct device pointer on success, or ERR_PTR() on error.
  1348. *
  1349. * Note: the struct class passed to this function must have previously
  1350. * been created with a call to class_create().
  1351. */
  1352. struct device *device_create_vargs(struct class *class, struct device *parent,
  1353. dev_t devt, void *drvdata, const char *fmt,
  1354. va_list args)
  1355. {
  1356. struct device *dev = NULL;
  1357. int retval = -ENODEV;
  1358. if (class == NULL || IS_ERR(class))
  1359. goto error;
  1360. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1361. if (!dev) {
  1362. retval = -ENOMEM;
  1363. goto error;
  1364. }
  1365. dev->devt = devt;
  1366. dev->class = class;
  1367. dev->parent = parent;
  1368. dev->release = device_create_release;
  1369. dev_set_drvdata(dev, drvdata);
  1370. retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
  1371. if (retval)
  1372. goto error;
  1373. retval = device_register(dev);
  1374. if (retval)
  1375. goto error;
  1376. return dev;
  1377. error:
  1378. put_device(dev);
  1379. return ERR_PTR(retval);
  1380. }
  1381. EXPORT_SYMBOL_GPL(device_create_vargs);
  1382. /**
  1383. * device_create - creates a device and registers it with sysfs
  1384. * @class: pointer to the struct class that this device should be registered to
  1385. * @parent: pointer to the parent struct device of this new device, if any
  1386. * @devt: the dev_t for the char device to be added
  1387. * @drvdata: the data to be added to the device for callbacks
  1388. * @fmt: string for the device's name
  1389. *
  1390. * This function can be used by char device classes. A struct device
  1391. * will be created in sysfs, registered to the specified class.
  1392. *
  1393. * A "dev" file will be created, showing the dev_t for the device, if
  1394. * the dev_t is not 0,0.
  1395. * If a pointer to a parent struct device is passed in, the newly created
  1396. * struct device will be a child of that device in sysfs.
  1397. * The pointer to the struct device will be returned from the call.
  1398. * Any further sysfs files that might be required can be created using this
  1399. * pointer.
  1400. *
  1401. * Returns &struct device pointer on success, or ERR_PTR() on error.
  1402. *
  1403. * Note: the struct class passed to this function must have previously
  1404. * been created with a call to class_create().
  1405. */
  1406. struct device *device_create(struct class *class, struct device *parent,
  1407. dev_t devt, void *drvdata, const char *fmt, ...)
  1408. {
  1409. va_list vargs;
  1410. struct device *dev;
  1411. va_start(vargs, fmt);
  1412. dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
  1413. va_end(vargs);
  1414. return dev;
  1415. }
  1416. EXPORT_SYMBOL_GPL(device_create);
  1417. static int __match_devt(struct device *dev, const void *data)
  1418. {
  1419. const dev_t *devt = data;
  1420. return dev->devt == *devt;
  1421. }
  1422. /**
  1423. * device_destroy - removes a device that was created with device_create()
  1424. * @class: pointer to the struct class that this device was registered with
  1425. * @devt: the dev_t of the device that was previously registered
  1426. *
  1427. * This call unregisters and cleans up a device that was created with a
  1428. * call to device_create().
  1429. */
  1430. void device_destroy(struct class *class, dev_t devt)
  1431. {
  1432. struct device *dev;
  1433. dev = class_find_device(class, NULL, &devt, __match_devt);
  1434. if (dev) {
  1435. put_device(dev);
  1436. device_unregister(dev);
  1437. }
  1438. }
  1439. EXPORT_SYMBOL_GPL(device_destroy);
  1440. /**
  1441. * device_rename - renames a device
  1442. * @dev: the pointer to the struct device to be renamed
  1443. * @new_name: the new name of the device
  1444. *
  1445. * It is the responsibility of the caller to provide mutual
  1446. * exclusion between two different calls of device_rename
  1447. * on the same device to ensure that new_name is valid and
  1448. * won't conflict with other devices.
  1449. *
  1450. * Note: Don't call this function. Currently, the networking layer calls this
  1451. * function, but that will change. The following text from Kay Sievers offers
  1452. * some insight:
  1453. *
  1454. * Renaming devices is racy at many levels, symlinks and other stuff are not
  1455. * replaced atomically, and you get a "move" uevent, but it's not easy to
  1456. * connect the event to the old and new device. Device nodes are not renamed at
  1457. * all, there isn't even support for that in the kernel now.
  1458. *
  1459. * In the meantime, during renaming, your target name might be taken by another
  1460. * driver, creating conflicts. Or the old name is taken directly after you
  1461. * renamed it -- then you get events for the same DEVPATH, before you even see
  1462. * the "move" event. It's just a mess, and nothing new should ever rely on
  1463. * kernel device renaming. Besides that, it's not even implemented now for
  1464. * other things than (driver-core wise very simple) network devices.
  1465. *
  1466. * We are currently about to change network renaming in udev to completely
  1467. * disallow renaming of devices in the same namespace as the kernel uses,
  1468. * because we can't solve the problems properly, that arise with swapping names
  1469. * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
  1470. * be allowed to some other name than eth[0-9]*, for the aforementioned
  1471. * reasons.
  1472. *
  1473. * Make up a "real" name in the driver before you register anything, or add
  1474. * some other attributes for userspace to find the device, or use udev to add
  1475. * symlinks -- but never rename kernel devices later, it's a complete mess. We
  1476. * don't even want to get into that and try to implement the missing pieces in
  1477. * the core. We really have other pieces to fix in the driver core mess. :)
  1478. */
  1479. int device_rename(struct device *dev, const char *new_name)
  1480. {
  1481. char *old_device_name = NULL;
  1482. int error;
  1483. dev = get_device(dev);
  1484. if (!dev)
  1485. return -EINVAL;
  1486. pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
  1487. __func__, new_name);
  1488. old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
  1489. if (!old_device_name) {
  1490. error = -ENOMEM;
  1491. goto out;
  1492. }
  1493. if (dev->class) {
  1494. error = sysfs_rename_link(&dev->class->p->subsys.kobj,
  1495. &dev->kobj, old_device_name, new_name);
  1496. if (error)
  1497. goto out;
  1498. }
  1499. error = kobject_rename(&dev->kobj, new_name);
  1500. if (error)
  1501. goto out;
  1502. out:
  1503. put_device(dev);
  1504. kfree(old_device_name);
  1505. return error;
  1506. }
  1507. EXPORT_SYMBOL_GPL(device_rename);
  1508. static int device_move_class_links(struct device *dev,
  1509. struct device *old_parent,
  1510. struct device *new_parent)
  1511. {
  1512. int error = 0;
  1513. if (old_parent)
  1514. sysfs_remove_link(&dev->kobj, "device");
  1515. if (new_parent)
  1516. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1517. "device");
  1518. return error;
  1519. }
  1520. /**
  1521. * device_move - moves a device to a new parent
  1522. * @dev: the pointer to the struct device to be moved
  1523. * @new_parent: the new parent of the device (can by NULL)
  1524. * @dpm_order: how to reorder the dpm_list
  1525. */
  1526. int device_move(struct device *dev, struct device *new_parent,
  1527. enum dpm_order dpm_order)
  1528. {
  1529. int error;
  1530. struct device *old_parent;
  1531. struct kobject *new_parent_kobj;
  1532. dev = get_device(dev);
  1533. if (!dev)
  1534. return -EINVAL;
  1535. device_pm_lock();
  1536. new_parent = get_device(new_parent);
  1537. new_parent_kobj = get_device_parent(dev, new_parent);
  1538. pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
  1539. __func__, new_parent ? dev_name(new_parent) : "<NULL>");
  1540. error = kobject_move(&dev->kobj, new_parent_kobj);
  1541. if (error) {
  1542. cleanup_glue_dir(dev, new_parent_kobj);
  1543. put_device(new_parent);
  1544. goto out;
  1545. }
  1546. old_parent = dev->parent;
  1547. dev->parent = new_parent;
  1548. if (old_parent)
  1549. klist_remove(&dev->p->knode_parent);
  1550. if (new_parent) {
  1551. klist_add_tail(&dev->p->knode_parent,
  1552. &new_parent->p->klist_children);
  1553. set_dev_node(dev, dev_to_node(new_parent));
  1554. }
  1555. if (dev->class) {
  1556. error = device_move_class_links(dev, old_parent, new_parent);
  1557. if (error) {
  1558. /* We ignore errors on cleanup since we're hosed anyway... */
  1559. device_move_class_links(dev, new_parent, old_parent);
  1560. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  1561. if (new_parent)
  1562. klist_remove(&dev->p->knode_parent);
  1563. dev->parent = old_parent;
  1564. if (old_parent) {
  1565. klist_add_tail(&dev->p->knode_parent,
  1566. &old_parent->p->klist_children);
  1567. set_dev_node(dev, dev_to_node(old_parent));
  1568. }
  1569. }
  1570. cleanup_glue_dir(dev, new_parent_kobj);
  1571. put_device(new_parent);
  1572. goto out;
  1573. }
  1574. }
  1575. switch (dpm_order) {
  1576. case DPM_ORDER_NONE:
  1577. break;
  1578. case DPM_ORDER_DEV_AFTER_PARENT:
  1579. device_pm_move_after(dev, new_parent);
  1580. break;
  1581. case DPM_ORDER_PARENT_BEFORE_DEV:
  1582. device_pm_move_before(new_parent, dev);
  1583. break;
  1584. case DPM_ORDER_DEV_LAST:
  1585. device_pm_move_last(dev);
  1586. break;
  1587. }
  1588. put_device(old_parent);
  1589. out:
  1590. device_pm_unlock();
  1591. put_device(dev);
  1592. return error;
  1593. }
  1594. EXPORT_SYMBOL_GPL(device_move);
  1595. /**
  1596. * device_shutdown - call ->shutdown() on each device to shutdown.
  1597. */
  1598. void device_shutdown(void)
  1599. {
  1600. struct device *dev;
  1601. spin_lock(&devices_kset->list_lock);
  1602. /*
  1603. * Walk the devices list backward, shutting down each in turn.
  1604. * Beware that device unplug events may also start pulling
  1605. * devices offline, even as the system is shutting down.
  1606. */
  1607. while (!list_empty(&devices_kset->list)) {
  1608. dev = list_entry(devices_kset->list.prev, struct device,
  1609. kobj.entry);
  1610. /*
  1611. * hold reference count of device's parent to
  1612. * prevent it from being freed because parent's
  1613. * lock is to be held
  1614. */
  1615. get_device(dev->parent);
  1616. get_device(dev);
  1617. /*
  1618. * Make sure the device is off the kset list, in the
  1619. * event that dev->*->shutdown() doesn't remove it.
  1620. */
  1621. list_del_init(&dev->kobj.entry);
  1622. spin_unlock(&devices_kset->list_lock);
  1623. /* hold lock to avoid race with probe/release */
  1624. if (dev->parent)
  1625. device_lock(dev->parent);
  1626. device_lock(dev);
  1627. /* Don't allow any more runtime suspends */
  1628. pm_runtime_get_noresume(dev);
  1629. pm_runtime_barrier(dev);
  1630. if (dev->bus && dev->bus->shutdown) {
  1631. if (initcall_debug)
  1632. dev_info(dev, "shutdown\n");
  1633. dev->bus->shutdown(dev);
  1634. } else if (dev->driver && dev->driver->shutdown) {
  1635. if (initcall_debug)
  1636. dev_info(dev, "shutdown\n");
  1637. dev->driver->shutdown(dev);
  1638. }
  1639. device_unlock(dev);
  1640. if (dev->parent)
  1641. device_unlock(dev->parent);
  1642. put_device(dev);
  1643. put_device(dev->parent);
  1644. spin_lock(&devices_kset->list_lock);
  1645. }
  1646. spin_unlock(&devices_kset->list_lock);
  1647. async_synchronize_full();
  1648. }
  1649. /*
  1650. * Device logging functions
  1651. */
  1652. #ifdef CONFIG_PRINTK
  1653. static int
  1654. create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
  1655. {
  1656. const char *subsys;
  1657. size_t pos = 0;
  1658. if (dev->class)
  1659. subsys = dev->class->name;
  1660. else if (dev->bus)
  1661. subsys = dev->bus->name;
  1662. else
  1663. return 0;
  1664. pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
  1665. /*
  1666. * Add device identifier DEVICE=:
  1667. * b12:8 block dev_t
  1668. * c127:3 char dev_t
  1669. * n8 netdev ifindex
  1670. * +sound:card0 subsystem:devname
  1671. */
  1672. if (MAJOR(dev->devt)) {
  1673. char c;
  1674. if (strcmp(subsys, "block") == 0)
  1675. c = 'b';
  1676. else
  1677. c = 'c';
  1678. pos++;
  1679. pos += snprintf(hdr + pos, hdrlen - pos,
  1680. "DEVICE=%c%u:%u",
  1681. c, MAJOR(dev->devt), MINOR(dev->devt));
  1682. } else if (strcmp(subsys, "net") == 0) {
  1683. struct net_device *net = to_net_dev(dev);
  1684. pos++;
  1685. pos += snprintf(hdr + pos, hdrlen - pos,
  1686. "DEVICE=n%u", net->ifindex);
  1687. } else {
  1688. pos++;
  1689. pos += snprintf(hdr + pos, hdrlen - pos,
  1690. "DEVICE=+%s:%s", subsys, dev_name(dev));
  1691. }
  1692. return pos;
  1693. }
  1694. EXPORT_SYMBOL(create_syslog_header);
  1695. int dev_vprintk_emit(int level, const struct device *dev,
  1696. const char *fmt, va_list args)
  1697. {
  1698. char hdr[128];
  1699. size_t hdrlen;
  1700. hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
  1701. return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
  1702. }
  1703. EXPORT_SYMBOL(dev_vprintk_emit);
  1704. int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
  1705. {
  1706. va_list args;
  1707. int r;
  1708. va_start(args, fmt);
  1709. r = dev_vprintk_emit(level, dev, fmt, args);
  1710. va_end(args);
  1711. return r;
  1712. }
  1713. EXPORT_SYMBOL(dev_printk_emit);
  1714. static int __dev_printk(const char *level, const struct device *dev,
  1715. struct va_format *vaf)
  1716. {
  1717. if (!dev)
  1718. return printk("%s(NULL device *): %pV", level, vaf);
  1719. return dev_printk_emit(level[1] - '0', dev,
  1720. "%s %s: %pV",
  1721. dev_driver_string(dev), dev_name(dev), vaf);
  1722. }
  1723. int dev_printk(const char *level, const struct device *dev,
  1724. const char *fmt, ...)
  1725. {
  1726. struct va_format vaf;
  1727. va_list args;
  1728. int r;
  1729. va_start(args, fmt);
  1730. vaf.fmt = fmt;
  1731. vaf.va = &args;
  1732. r = __dev_printk(level, dev, &vaf);
  1733. va_end(args);
  1734. return r;
  1735. }
  1736. EXPORT_SYMBOL(dev_printk);
  1737. #define define_dev_printk_level(func, kern_level) \
  1738. int func(const struct device *dev, const char *fmt, ...) \
  1739. { \
  1740. struct va_format vaf; \
  1741. va_list args; \
  1742. int r; \
  1743. \
  1744. va_start(args, fmt); \
  1745. \
  1746. vaf.fmt = fmt; \
  1747. vaf.va = &args; \
  1748. \
  1749. r = __dev_printk(kern_level, dev, &vaf); \
  1750. \
  1751. va_end(args); \
  1752. \
  1753. return r; \
  1754. } \
  1755. EXPORT_SYMBOL(func);
  1756. define_dev_printk_level(dev_emerg, KERN_EMERG);
  1757. define_dev_printk_level(dev_alert, KERN_ALERT);
  1758. define_dev_printk_level(dev_crit, KERN_CRIT);
  1759. define_dev_printk_level(dev_err, KERN_ERR);
  1760. define_dev_printk_level(dev_warn, KERN_WARNING);
  1761. define_dev_printk_level(dev_notice, KERN_NOTICE);
  1762. define_dev_printk_level(_dev_info, KERN_INFO);
  1763. #endif