core.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042
  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. "Attribute %s: write permission without 'store'\n",
  490. attr->attr.name);
  491. WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
  492. "Attribute %s: read permission without 'show'\n",
  493. attr->attr.name);
  494. error = sysfs_create_file(&dev->kobj, &attr->attr);
  495. }
  496. return error;
  497. }
  498. /**
  499. * device_remove_file - remove sysfs attribute file.
  500. * @dev: device.
  501. * @attr: device attribute descriptor.
  502. */
  503. void device_remove_file(struct device *dev,
  504. const struct device_attribute *attr)
  505. {
  506. if (dev)
  507. sysfs_remove_file(&dev->kobj, &attr->attr);
  508. }
  509. /**
  510. * device_create_bin_file - create sysfs binary attribute file for device.
  511. * @dev: device.
  512. * @attr: device binary attribute descriptor.
  513. */
  514. int device_create_bin_file(struct device *dev,
  515. const struct bin_attribute *attr)
  516. {
  517. int error = -EINVAL;
  518. if (dev)
  519. error = sysfs_create_bin_file(&dev->kobj, attr);
  520. return error;
  521. }
  522. EXPORT_SYMBOL_GPL(device_create_bin_file);
  523. /**
  524. * device_remove_bin_file - remove sysfs binary attribute file
  525. * @dev: device.
  526. * @attr: device binary attribute descriptor.
  527. */
  528. void device_remove_bin_file(struct device *dev,
  529. const struct bin_attribute *attr)
  530. {
  531. if (dev)
  532. sysfs_remove_bin_file(&dev->kobj, attr);
  533. }
  534. EXPORT_SYMBOL_GPL(device_remove_bin_file);
  535. /**
  536. * device_schedule_callback_owner - helper to schedule a callback for a device
  537. * @dev: device.
  538. * @func: callback function to invoke later.
  539. * @owner: module owning the callback routine
  540. *
  541. * Attribute methods must not unregister themselves or their parent device
  542. * (which would amount to the same thing). Attempts to do so will deadlock,
  543. * since unregistration is mutually exclusive with driver callbacks.
  544. *
  545. * Instead methods can call this routine, which will attempt to allocate
  546. * and schedule a workqueue request to call back @func with @dev as its
  547. * argument in the workqueue's process context. @dev will be pinned until
  548. * @func returns.
  549. *
  550. * This routine is usually called via the inline device_schedule_callback(),
  551. * which automatically sets @owner to THIS_MODULE.
  552. *
  553. * Returns 0 if the request was submitted, -ENOMEM if storage could not
  554. * be allocated, -ENODEV if a reference to @owner isn't available.
  555. *
  556. * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
  557. * underlying sysfs routine (since it is intended for use by attribute
  558. * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
  559. */
  560. int device_schedule_callback_owner(struct device *dev,
  561. void (*func)(struct device *), struct module *owner)
  562. {
  563. return sysfs_schedule_callback(&dev->kobj,
  564. (void (*)(void *)) func, dev, owner);
  565. }
  566. EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
  567. static void klist_children_get(struct klist_node *n)
  568. {
  569. struct device_private *p = to_device_private_parent(n);
  570. struct device *dev = p->device;
  571. get_device(dev);
  572. }
  573. static void klist_children_put(struct klist_node *n)
  574. {
  575. struct device_private *p = to_device_private_parent(n);
  576. struct device *dev = p->device;
  577. put_device(dev);
  578. }
  579. /**
  580. * device_initialize - init device structure.
  581. * @dev: device.
  582. *
  583. * This prepares the device for use by other layers by initializing
  584. * its fields.
  585. * It is the first half of device_register(), if called by
  586. * that function, though it can also be called separately, so one
  587. * may use @dev's fields. In particular, get_device()/put_device()
  588. * may be used for reference counting of @dev after calling this
  589. * function.
  590. *
  591. * All fields in @dev must be initialized by the caller to 0, except
  592. * for those explicitly set to some other value. The simplest
  593. * approach is to use kzalloc() to allocate the structure containing
  594. * @dev.
  595. *
  596. * NOTE: Use put_device() to give up your reference instead of freeing
  597. * @dev directly once you have called this function.
  598. */
  599. void device_initialize(struct device *dev)
  600. {
  601. dev->kobj.kset = devices_kset;
  602. kobject_init(&dev->kobj, &device_ktype);
  603. INIT_LIST_HEAD(&dev->dma_pools);
  604. mutex_init(&dev->mutex);
  605. lockdep_set_novalidate_class(&dev->mutex);
  606. spin_lock_init(&dev->devres_lock);
  607. INIT_LIST_HEAD(&dev->devres_head);
  608. device_pm_init(dev);
  609. set_dev_node(dev, -1);
  610. }
  611. struct kobject *virtual_device_parent(struct device *dev)
  612. {
  613. static struct kobject *virtual_dir = NULL;
  614. if (!virtual_dir)
  615. virtual_dir = kobject_create_and_add("virtual",
  616. &devices_kset->kobj);
  617. return virtual_dir;
  618. }
  619. struct class_dir {
  620. struct kobject kobj;
  621. struct class *class;
  622. };
  623. #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
  624. static void class_dir_release(struct kobject *kobj)
  625. {
  626. struct class_dir *dir = to_class_dir(kobj);
  627. kfree(dir);
  628. }
  629. static const
  630. struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
  631. {
  632. struct class_dir *dir = to_class_dir(kobj);
  633. return dir->class->ns_type;
  634. }
  635. static struct kobj_type class_dir_ktype = {
  636. .release = class_dir_release,
  637. .sysfs_ops = &kobj_sysfs_ops,
  638. .child_ns_type = class_dir_child_ns_type
  639. };
  640. static struct kobject *
  641. class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
  642. {
  643. struct class_dir *dir;
  644. int retval;
  645. dir = kzalloc(sizeof(*dir), GFP_KERNEL);
  646. if (!dir)
  647. return NULL;
  648. dir->class = class;
  649. kobject_init(&dir->kobj, &class_dir_ktype);
  650. dir->kobj.kset = &class->p->glue_dirs;
  651. retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
  652. if (retval < 0) {
  653. kobject_put(&dir->kobj);
  654. return NULL;
  655. }
  656. return &dir->kobj;
  657. }
  658. static struct kobject *get_device_parent(struct device *dev,
  659. struct device *parent)
  660. {
  661. if (dev->class) {
  662. static DEFINE_MUTEX(gdp_mutex);
  663. struct kobject *kobj = NULL;
  664. struct kobject *parent_kobj;
  665. struct kobject *k;
  666. #ifdef CONFIG_BLOCK
  667. /* block disks show up in /sys/block */
  668. if (sysfs_deprecated && dev->class == &block_class) {
  669. if (parent && parent->class == &block_class)
  670. return &parent->kobj;
  671. return &block_class.p->subsys.kobj;
  672. }
  673. #endif
  674. /*
  675. * If we have no parent, we live in "virtual".
  676. * Class-devices with a non class-device as parent, live
  677. * in a "glue" directory to prevent namespace collisions.
  678. */
  679. if (parent == NULL)
  680. parent_kobj = virtual_device_parent(dev);
  681. else if (parent->class && !dev->class->ns_type)
  682. return &parent->kobj;
  683. else
  684. parent_kobj = &parent->kobj;
  685. mutex_lock(&gdp_mutex);
  686. /* find our class-directory at the parent and reference it */
  687. spin_lock(&dev->class->p->glue_dirs.list_lock);
  688. list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
  689. if (k->parent == parent_kobj) {
  690. kobj = kobject_get(k);
  691. break;
  692. }
  693. spin_unlock(&dev->class->p->glue_dirs.list_lock);
  694. if (kobj) {
  695. mutex_unlock(&gdp_mutex);
  696. return kobj;
  697. }
  698. /* or create a new class-directory at the parent device */
  699. k = class_dir_create_and_add(dev->class, parent_kobj);
  700. /* do not emit an uevent for this simple "glue" directory */
  701. mutex_unlock(&gdp_mutex);
  702. return k;
  703. }
  704. /* subsystems can specify a default root directory for their devices */
  705. if (!parent && dev->bus && dev->bus->dev_root)
  706. return &dev->bus->dev_root->kobj;
  707. if (parent)
  708. return &parent->kobj;
  709. return NULL;
  710. }
  711. static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
  712. {
  713. /* see if we live in a "glue" directory */
  714. if (!glue_dir || !dev->class ||
  715. glue_dir->kset != &dev->class->p->glue_dirs)
  716. return;
  717. kobject_put(glue_dir);
  718. }
  719. static void cleanup_device_parent(struct device *dev)
  720. {
  721. cleanup_glue_dir(dev, dev->kobj.parent);
  722. }
  723. static int device_add_class_symlinks(struct device *dev)
  724. {
  725. int error;
  726. if (!dev->class)
  727. return 0;
  728. error = sysfs_create_link(&dev->kobj,
  729. &dev->class->p->subsys.kobj,
  730. "subsystem");
  731. if (error)
  732. goto out;
  733. if (dev->parent && device_is_not_partition(dev)) {
  734. error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  735. "device");
  736. if (error)
  737. goto out_subsys;
  738. }
  739. #ifdef CONFIG_BLOCK
  740. /* /sys/block has directories and does not need symlinks */
  741. if (sysfs_deprecated && dev->class == &block_class)
  742. return 0;
  743. #endif
  744. /* link in the class directory pointing to the device */
  745. error = sysfs_create_link(&dev->class->p->subsys.kobj,
  746. &dev->kobj, dev_name(dev));
  747. if (error)
  748. goto out_device;
  749. return 0;
  750. out_device:
  751. sysfs_remove_link(&dev->kobj, "device");
  752. out_subsys:
  753. sysfs_remove_link(&dev->kobj, "subsystem");
  754. out:
  755. return error;
  756. }
  757. static void device_remove_class_symlinks(struct device *dev)
  758. {
  759. if (!dev->class)
  760. return;
  761. if (dev->parent && device_is_not_partition(dev))
  762. sysfs_remove_link(&dev->kobj, "device");
  763. sysfs_remove_link(&dev->kobj, "subsystem");
  764. #ifdef CONFIG_BLOCK
  765. if (sysfs_deprecated && dev->class == &block_class)
  766. return;
  767. #endif
  768. sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
  769. }
  770. /**
  771. * dev_set_name - set a device name
  772. * @dev: device
  773. * @fmt: format string for the device's name
  774. */
  775. int dev_set_name(struct device *dev, const char *fmt, ...)
  776. {
  777. va_list vargs;
  778. int err;
  779. va_start(vargs, fmt);
  780. err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
  781. va_end(vargs);
  782. return err;
  783. }
  784. EXPORT_SYMBOL_GPL(dev_set_name);
  785. /**
  786. * device_to_dev_kobj - select a /sys/dev/ directory for the device
  787. * @dev: device
  788. *
  789. * By default we select char/ for new entries. Setting class->dev_obj
  790. * to NULL prevents an entry from being created. class->dev_kobj must
  791. * be set (or cleared) before any devices are registered to the class
  792. * otherwise device_create_sys_dev_entry() and
  793. * device_remove_sys_dev_entry() will disagree about the presence of
  794. * the link.
  795. */
  796. static struct kobject *device_to_dev_kobj(struct device *dev)
  797. {
  798. struct kobject *kobj;
  799. if (dev->class)
  800. kobj = dev->class->dev_kobj;
  801. else
  802. kobj = sysfs_dev_char_kobj;
  803. return kobj;
  804. }
  805. static int device_create_sys_dev_entry(struct device *dev)
  806. {
  807. struct kobject *kobj = device_to_dev_kobj(dev);
  808. int error = 0;
  809. char devt_str[15];
  810. if (kobj) {
  811. format_dev_t(devt_str, dev->devt);
  812. error = sysfs_create_link(kobj, &dev->kobj, devt_str);
  813. }
  814. return error;
  815. }
  816. static void device_remove_sys_dev_entry(struct device *dev)
  817. {
  818. struct kobject *kobj = device_to_dev_kobj(dev);
  819. char devt_str[15];
  820. if (kobj) {
  821. format_dev_t(devt_str, dev->devt);
  822. sysfs_remove_link(kobj, devt_str);
  823. }
  824. }
  825. int device_private_init(struct device *dev)
  826. {
  827. dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
  828. if (!dev->p)
  829. return -ENOMEM;
  830. dev->p->device = dev;
  831. klist_init(&dev->p->klist_children, klist_children_get,
  832. klist_children_put);
  833. INIT_LIST_HEAD(&dev->p->deferred_probe);
  834. return 0;
  835. }
  836. /**
  837. * device_add - add device to device hierarchy.
  838. * @dev: device.
  839. *
  840. * This is part 2 of device_register(), though may be called
  841. * separately _iff_ device_initialize() has been called separately.
  842. *
  843. * This adds @dev to the kobject hierarchy via kobject_add(), adds it
  844. * to the global and sibling lists for the device, then
  845. * adds it to the other relevant subsystems of the driver model.
  846. *
  847. * Do not call this routine or device_register() more than once for
  848. * any device structure. The driver model core is not designed to work
  849. * with devices that get unregistered and then spring back to life.
  850. * (Among other things, it's very hard to guarantee that all references
  851. * to the previous incarnation of @dev have been dropped.) Allocate
  852. * and register a fresh new struct device instead.
  853. *
  854. * NOTE: _Never_ directly free @dev after calling this function, even
  855. * if it returned an error! Always use put_device() to give up your
  856. * reference instead.
  857. */
  858. int device_add(struct device *dev)
  859. {
  860. struct device *parent = NULL;
  861. struct kobject *kobj;
  862. struct class_interface *class_intf;
  863. int error = -EINVAL;
  864. dev = get_device(dev);
  865. if (!dev)
  866. goto done;
  867. if (!dev->p) {
  868. error = device_private_init(dev);
  869. if (error)
  870. goto done;
  871. }
  872. /*
  873. * for statically allocated devices, which should all be converted
  874. * some day, we need to initialize the name. We prevent reading back
  875. * the name, and force the use of dev_name()
  876. */
  877. if (dev->init_name) {
  878. dev_set_name(dev, "%s", dev->init_name);
  879. dev->init_name = NULL;
  880. }
  881. /* subsystems can specify simple device enumeration */
  882. if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
  883. dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
  884. if (!dev_name(dev)) {
  885. error = -EINVAL;
  886. goto name_error;
  887. }
  888. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  889. parent = get_device(dev->parent);
  890. kobj = get_device_parent(dev, parent);
  891. if (kobj)
  892. dev->kobj.parent = kobj;
  893. /* use parent numa_node */
  894. if (parent)
  895. set_dev_node(dev, dev_to_node(parent));
  896. /* first, register with generic layer. */
  897. /* we require the name to be set before, and pass NULL */
  898. error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
  899. if (error)
  900. goto Error;
  901. /* notify platform of device entry */
  902. if (platform_notify)
  903. platform_notify(dev);
  904. error = device_create_file(dev, &uevent_attr);
  905. if (error)
  906. goto attrError;
  907. if (MAJOR(dev->devt)) {
  908. error = device_create_file(dev, &devt_attr);
  909. if (error)
  910. goto ueventattrError;
  911. error = device_create_sys_dev_entry(dev);
  912. if (error)
  913. goto devtattrError;
  914. devtmpfs_create_node(dev);
  915. }
  916. error = device_add_class_symlinks(dev);
  917. if (error)
  918. goto SymlinkError;
  919. error = device_add_attrs(dev);
  920. if (error)
  921. goto AttrsError;
  922. error = bus_add_device(dev);
  923. if (error)
  924. goto BusError;
  925. error = dpm_sysfs_add(dev);
  926. if (error)
  927. goto DPMError;
  928. device_pm_add(dev);
  929. /* Notify clients of device addition. This call must come
  930. * after dpm_sysfs_add() and before kobject_uevent().
  931. */
  932. if (dev->bus)
  933. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  934. BUS_NOTIFY_ADD_DEVICE, dev);
  935. kobject_uevent(&dev->kobj, KOBJ_ADD);
  936. bus_probe_device(dev);
  937. if (parent)
  938. klist_add_tail(&dev->p->knode_parent,
  939. &parent->p->klist_children);
  940. if (dev->class) {
  941. mutex_lock(&dev->class->p->mutex);
  942. /* tie the class to the device */
  943. klist_add_tail(&dev->knode_class,
  944. &dev->class->p->klist_devices);
  945. /* notify any interfaces that the device is here */
  946. list_for_each_entry(class_intf,
  947. &dev->class->p->interfaces, node)
  948. if (class_intf->add_dev)
  949. class_intf->add_dev(dev, class_intf);
  950. mutex_unlock(&dev->class->p->mutex);
  951. }
  952. done:
  953. put_device(dev);
  954. return error;
  955. DPMError:
  956. bus_remove_device(dev);
  957. BusError:
  958. device_remove_attrs(dev);
  959. AttrsError:
  960. device_remove_class_symlinks(dev);
  961. SymlinkError:
  962. if (MAJOR(dev->devt))
  963. devtmpfs_delete_node(dev);
  964. if (MAJOR(dev->devt))
  965. device_remove_sys_dev_entry(dev);
  966. devtattrError:
  967. if (MAJOR(dev->devt))
  968. device_remove_file(dev, &devt_attr);
  969. ueventattrError:
  970. device_remove_file(dev, &uevent_attr);
  971. attrError:
  972. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  973. kobject_del(&dev->kobj);
  974. Error:
  975. cleanup_device_parent(dev);
  976. if (parent)
  977. put_device(parent);
  978. name_error:
  979. kfree(dev->p);
  980. dev->p = NULL;
  981. goto done;
  982. }
  983. /**
  984. * device_register - register a device with the system.
  985. * @dev: pointer to the device structure
  986. *
  987. * This happens in two clean steps - initialize the device
  988. * and add it to the system. The two steps can be called
  989. * separately, but this is the easiest and most common.
  990. * I.e. you should only call the two helpers separately if
  991. * have a clearly defined need to use and refcount the device
  992. * before it is added to the hierarchy.
  993. *
  994. * For more information, see the kerneldoc for device_initialize()
  995. * and device_add().
  996. *
  997. * NOTE: _Never_ directly free @dev after calling this function, even
  998. * if it returned an error! Always use put_device() to give up the
  999. * reference initialized in this function instead.
  1000. */
  1001. int device_register(struct device *dev)
  1002. {
  1003. device_initialize(dev);
  1004. return device_add(dev);
  1005. }
  1006. /**
  1007. * get_device - increment reference count for device.
  1008. * @dev: device.
  1009. *
  1010. * This simply forwards the call to kobject_get(), though
  1011. * we do take care to provide for the case that we get a NULL
  1012. * pointer passed in.
  1013. */
  1014. struct device *get_device(struct device *dev)
  1015. {
  1016. return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
  1017. }
  1018. /**
  1019. * put_device - decrement reference count.
  1020. * @dev: device in question.
  1021. */
  1022. void put_device(struct device *dev)
  1023. {
  1024. /* might_sleep(); */
  1025. if (dev)
  1026. kobject_put(&dev->kobj);
  1027. }
  1028. /**
  1029. * device_del - delete device from system.
  1030. * @dev: device.
  1031. *
  1032. * This is the first part of the device unregistration
  1033. * sequence. This removes the device from the lists we control
  1034. * from here, has it removed from the other driver model
  1035. * subsystems it was added to in device_add(), and removes it
  1036. * from the kobject hierarchy.
  1037. *
  1038. * NOTE: this should be called manually _iff_ device_add() was
  1039. * also called manually.
  1040. */
  1041. void device_del(struct device *dev)
  1042. {
  1043. struct device *parent = dev->parent;
  1044. struct class_interface *class_intf;
  1045. /* Notify clients of device removal. This call must come
  1046. * before dpm_sysfs_remove().
  1047. */
  1048. if (dev->bus)
  1049. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  1050. BUS_NOTIFY_DEL_DEVICE, dev);
  1051. dpm_sysfs_remove(dev);
  1052. if (parent)
  1053. klist_del(&dev->p->knode_parent);
  1054. if (MAJOR(dev->devt)) {
  1055. devtmpfs_delete_node(dev);
  1056. device_remove_sys_dev_entry(dev);
  1057. device_remove_file(dev, &devt_attr);
  1058. }
  1059. if (dev->class) {
  1060. device_remove_class_symlinks(dev);
  1061. mutex_lock(&dev->class->p->mutex);
  1062. /* notify any interfaces that the device is now gone */
  1063. list_for_each_entry(class_intf,
  1064. &dev->class->p->interfaces, node)
  1065. if (class_intf->remove_dev)
  1066. class_intf->remove_dev(dev, class_intf);
  1067. /* remove the device from the class list */
  1068. klist_del(&dev->knode_class);
  1069. mutex_unlock(&dev->class->p->mutex);
  1070. }
  1071. device_remove_file(dev, &uevent_attr);
  1072. device_remove_attrs(dev);
  1073. bus_remove_device(dev);
  1074. device_pm_remove(dev);
  1075. driver_deferred_probe_del(dev);
  1076. /* Notify the platform of the removal, in case they
  1077. * need to do anything...
  1078. */
  1079. if (platform_notify_remove)
  1080. platform_notify_remove(dev);
  1081. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  1082. cleanup_device_parent(dev);
  1083. kobject_del(&dev->kobj);
  1084. put_device(parent);
  1085. }
  1086. /**
  1087. * device_unregister - unregister device from system.
  1088. * @dev: device going away.
  1089. *
  1090. * We do this in two parts, like we do device_register(). First,
  1091. * we remove it from all the subsystems with device_del(), then
  1092. * we decrement the reference count via put_device(). If that
  1093. * is the final reference count, the device will be cleaned up
  1094. * via device_release() above. Otherwise, the structure will
  1095. * stick around until the final reference to the device is dropped.
  1096. */
  1097. void device_unregister(struct device *dev)
  1098. {
  1099. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  1100. device_del(dev);
  1101. put_device(dev);
  1102. }
  1103. static struct device *next_device(struct klist_iter *i)
  1104. {
  1105. struct klist_node *n = klist_next(i);
  1106. struct device *dev = NULL;
  1107. struct device_private *p;
  1108. if (n) {
  1109. p = to_device_private_parent(n);
  1110. dev = p->device;
  1111. }
  1112. return dev;
  1113. }
  1114. /**
  1115. * device_get_devnode - path of device node file
  1116. * @dev: device
  1117. * @mode: returned file access mode
  1118. * @uid: returned file owner
  1119. * @gid: returned file group
  1120. * @tmp: possibly allocated string
  1121. *
  1122. * Return the relative path of a possible device node.
  1123. * Non-default names may need to allocate a memory to compose
  1124. * a name. This memory is returned in tmp and needs to be
  1125. * freed by the caller.
  1126. */
  1127. const char *device_get_devnode(struct device *dev,
  1128. umode_t *mode, kuid_t *uid, kgid_t *gid,
  1129. const char **tmp)
  1130. {
  1131. char *s;
  1132. *tmp = NULL;
  1133. /* the device type may provide a specific name */
  1134. if (dev->type && dev->type->devnode)
  1135. *tmp = dev->type->devnode(dev, mode, uid, gid);
  1136. if (*tmp)
  1137. return *tmp;
  1138. /* the class may provide a specific name */
  1139. if (dev->class && dev->class->devnode)
  1140. *tmp = dev->class->devnode(dev, mode);
  1141. if (*tmp)
  1142. return *tmp;
  1143. /* return name without allocation, tmp == NULL */
  1144. if (strchr(dev_name(dev), '!') == NULL)
  1145. return dev_name(dev);
  1146. /* replace '!' in the name with '/' */
  1147. *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
  1148. if (!*tmp)
  1149. return NULL;
  1150. while ((s = strchr(*tmp, '!')))
  1151. s[0] = '/';
  1152. return *tmp;
  1153. }
  1154. /**
  1155. * device_for_each_child - device child iterator.
  1156. * @parent: parent struct device.
  1157. * @fn: function to be called for each device.
  1158. * @data: data for the callback.
  1159. *
  1160. * Iterate over @parent's child devices, and call @fn for each,
  1161. * passing it @data.
  1162. *
  1163. * We check the return of @fn each time. If it returns anything
  1164. * other than 0, we break out and return that value.
  1165. */
  1166. int device_for_each_child(struct device *parent, void *data,
  1167. int (*fn)(struct device *dev, void *data))
  1168. {
  1169. struct klist_iter i;
  1170. struct device *child;
  1171. int error = 0;
  1172. if (!parent->p)
  1173. return 0;
  1174. klist_iter_init(&parent->p->klist_children, &i);
  1175. while ((child = next_device(&i)) && !error)
  1176. error = fn(child, data);
  1177. klist_iter_exit(&i);
  1178. return error;
  1179. }
  1180. /**
  1181. * device_find_child - device iterator for locating a particular device.
  1182. * @parent: parent struct device
  1183. * @match: Callback function to check device
  1184. * @data: Data to pass to match function
  1185. *
  1186. * This is similar to the device_for_each_child() function above, but it
  1187. * returns a reference to a device that is 'found' for later use, as
  1188. * determined by the @match callback.
  1189. *
  1190. * The callback should return 0 if the device doesn't match and non-zero
  1191. * if it does. If the callback returns non-zero and a reference to the
  1192. * current device can be obtained, this function will return to the caller
  1193. * and not iterate over any more devices.
  1194. *
  1195. * NOTE: you will need to drop the reference with put_device() after use.
  1196. */
  1197. struct device *device_find_child(struct device *parent, void *data,
  1198. int (*match)(struct device *dev, void *data))
  1199. {
  1200. struct klist_iter i;
  1201. struct device *child;
  1202. if (!parent)
  1203. return NULL;
  1204. klist_iter_init(&parent->p->klist_children, &i);
  1205. while ((child = next_device(&i)))
  1206. if (match(child, data) && get_device(child))
  1207. break;
  1208. klist_iter_exit(&i);
  1209. return child;
  1210. }
  1211. int __init devices_init(void)
  1212. {
  1213. devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
  1214. if (!devices_kset)
  1215. return -ENOMEM;
  1216. dev_kobj = kobject_create_and_add("dev", NULL);
  1217. if (!dev_kobj)
  1218. goto dev_kobj_err;
  1219. sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
  1220. if (!sysfs_dev_block_kobj)
  1221. goto block_kobj_err;
  1222. sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
  1223. if (!sysfs_dev_char_kobj)
  1224. goto char_kobj_err;
  1225. return 0;
  1226. char_kobj_err:
  1227. kobject_put(sysfs_dev_block_kobj);
  1228. block_kobj_err:
  1229. kobject_put(dev_kobj);
  1230. dev_kobj_err:
  1231. kset_unregister(devices_kset);
  1232. return -ENOMEM;
  1233. }
  1234. EXPORT_SYMBOL_GPL(device_for_each_child);
  1235. EXPORT_SYMBOL_GPL(device_find_child);
  1236. EXPORT_SYMBOL_GPL(device_initialize);
  1237. EXPORT_SYMBOL_GPL(device_add);
  1238. EXPORT_SYMBOL_GPL(device_register);
  1239. EXPORT_SYMBOL_GPL(device_del);
  1240. EXPORT_SYMBOL_GPL(device_unregister);
  1241. EXPORT_SYMBOL_GPL(get_device);
  1242. EXPORT_SYMBOL_GPL(put_device);
  1243. EXPORT_SYMBOL_GPL(device_create_file);
  1244. EXPORT_SYMBOL_GPL(device_remove_file);
  1245. struct root_device {
  1246. struct device dev;
  1247. struct module *owner;
  1248. };
  1249. static inline struct root_device *to_root_device(struct device *d)
  1250. {
  1251. return container_of(d, struct root_device, dev);
  1252. }
  1253. static void root_device_release(struct device *dev)
  1254. {
  1255. kfree(to_root_device(dev));
  1256. }
  1257. /**
  1258. * __root_device_register - allocate and register a root device
  1259. * @name: root device name
  1260. * @owner: owner module of the root device, usually THIS_MODULE
  1261. *
  1262. * This function allocates a root device and registers it
  1263. * using device_register(). In order to free the returned
  1264. * device, use root_device_unregister().
  1265. *
  1266. * Root devices are dummy devices which allow other devices
  1267. * to be grouped under /sys/devices. Use this function to
  1268. * allocate a root device and then use it as the parent of
  1269. * any device which should appear under /sys/devices/{name}
  1270. *
  1271. * The /sys/devices/{name} directory will also contain a
  1272. * 'module' symlink which points to the @owner directory
  1273. * in sysfs.
  1274. *
  1275. * Returns &struct device pointer on success, or ERR_PTR() on error.
  1276. *
  1277. * Note: You probably want to use root_device_register().
  1278. */
  1279. struct device *__root_device_register(const char *name, struct module *owner)
  1280. {
  1281. struct root_device *root;
  1282. int err = -ENOMEM;
  1283. root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
  1284. if (!root)
  1285. return ERR_PTR(err);
  1286. err = dev_set_name(&root->dev, "%s", name);
  1287. if (err) {
  1288. kfree(root);
  1289. return ERR_PTR(err);
  1290. }
  1291. root->dev.release = root_device_release;
  1292. err = device_register(&root->dev);
  1293. if (err) {
  1294. put_device(&root->dev);
  1295. return ERR_PTR(err);
  1296. }
  1297. #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
  1298. if (owner) {
  1299. struct module_kobject *mk = &owner->mkobj;
  1300. err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
  1301. if (err) {
  1302. device_unregister(&root->dev);
  1303. return ERR_PTR(err);
  1304. }
  1305. root->owner = owner;
  1306. }
  1307. #endif
  1308. return &root->dev;
  1309. }
  1310. EXPORT_SYMBOL_GPL(__root_device_register);
  1311. /**
  1312. * root_device_unregister - unregister and free a root device
  1313. * @dev: device going away
  1314. *
  1315. * This function unregisters and cleans up a device that was created by
  1316. * root_device_register().
  1317. */
  1318. void root_device_unregister(struct device *dev)
  1319. {
  1320. struct root_device *root = to_root_device(dev);
  1321. if (root->owner)
  1322. sysfs_remove_link(&root->dev.kobj, "module");
  1323. device_unregister(dev);
  1324. }
  1325. EXPORT_SYMBOL_GPL(root_device_unregister);
  1326. static void device_create_release(struct device *dev)
  1327. {
  1328. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  1329. kfree(dev);
  1330. }
  1331. /**
  1332. * device_create_vargs - creates a device and registers it with sysfs
  1333. * @class: pointer to the struct class that this device should be registered to
  1334. * @parent: pointer to the parent struct device of this new device, if any
  1335. * @devt: the dev_t for the char device to be added
  1336. * @drvdata: the data to be added to the device for callbacks
  1337. * @fmt: string for the device's name
  1338. * @args: va_list for the device's name
  1339. *
  1340. * This function can be used by char device classes. A struct device
  1341. * will be created in sysfs, registered to the specified class.
  1342. *
  1343. * A "dev" file will be created, showing the dev_t for the device, if
  1344. * the dev_t is not 0,0.
  1345. * If a pointer to a parent struct device is passed in, the newly created
  1346. * struct device will be a child of that device in sysfs.
  1347. * The pointer to the struct device will be returned from the call.
  1348. * Any further sysfs files that might be required can be created using this
  1349. * pointer.
  1350. *
  1351. * Returns &struct device pointer on success, or ERR_PTR() on error.
  1352. *
  1353. * Note: the struct class passed to this function must have previously
  1354. * been created with a call to class_create().
  1355. */
  1356. struct device *device_create_vargs(struct class *class, struct device *parent,
  1357. dev_t devt, void *drvdata, const char *fmt,
  1358. va_list args)
  1359. {
  1360. struct device *dev = NULL;
  1361. int retval = -ENODEV;
  1362. if (class == NULL || IS_ERR(class))
  1363. goto error;
  1364. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1365. if (!dev) {
  1366. retval = -ENOMEM;
  1367. goto error;
  1368. }
  1369. dev->devt = devt;
  1370. dev->class = class;
  1371. dev->parent = parent;
  1372. dev->release = device_create_release;
  1373. dev_set_drvdata(dev, drvdata);
  1374. retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
  1375. if (retval)
  1376. goto error;
  1377. retval = device_register(dev);
  1378. if (retval)
  1379. goto error;
  1380. return dev;
  1381. error:
  1382. put_device(dev);
  1383. return ERR_PTR(retval);
  1384. }
  1385. EXPORT_SYMBOL_GPL(device_create_vargs);
  1386. /**
  1387. * device_create - creates a device and registers it with sysfs
  1388. * @class: pointer to the struct class that this device should be registered to
  1389. * @parent: pointer to the parent struct device of this new device, if any
  1390. * @devt: the dev_t for the char device to be added
  1391. * @drvdata: the data to be added to the device for callbacks
  1392. * @fmt: string for the device's name
  1393. *
  1394. * This function can be used by char device classes. A struct device
  1395. * will be created in sysfs, registered to the specified class.
  1396. *
  1397. * A "dev" file will be created, showing the dev_t for the device, if
  1398. * the dev_t is not 0,0.
  1399. * If a pointer to a parent struct device is passed in, the newly created
  1400. * struct device will be a child of that device in sysfs.
  1401. * The pointer to the struct device will be returned from the call.
  1402. * Any further sysfs files that might be required can be created using this
  1403. * pointer.
  1404. *
  1405. * Returns &struct device pointer on success, or ERR_PTR() on error.
  1406. *
  1407. * Note: the struct class passed to this function must have previously
  1408. * been created with a call to class_create().
  1409. */
  1410. struct device *device_create(struct class *class, struct device *parent,
  1411. dev_t devt, void *drvdata, const char *fmt, ...)
  1412. {
  1413. va_list vargs;
  1414. struct device *dev;
  1415. va_start(vargs, fmt);
  1416. dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
  1417. va_end(vargs);
  1418. return dev;
  1419. }
  1420. EXPORT_SYMBOL_GPL(device_create);
  1421. static int __match_devt(struct device *dev, const void *data)
  1422. {
  1423. const dev_t *devt = data;
  1424. return dev->devt == *devt;
  1425. }
  1426. /**
  1427. * device_destroy - removes a device that was created with device_create()
  1428. * @class: pointer to the struct class that this device was registered with
  1429. * @devt: the dev_t of the device that was previously registered
  1430. *
  1431. * This call unregisters and cleans up a device that was created with a
  1432. * call to device_create().
  1433. */
  1434. void device_destroy(struct class *class, dev_t devt)
  1435. {
  1436. struct device *dev;
  1437. dev = class_find_device(class, NULL, &devt, __match_devt);
  1438. if (dev) {
  1439. put_device(dev);
  1440. device_unregister(dev);
  1441. }
  1442. }
  1443. EXPORT_SYMBOL_GPL(device_destroy);
  1444. /**
  1445. * device_rename - renames a device
  1446. * @dev: the pointer to the struct device to be renamed
  1447. * @new_name: the new name of the device
  1448. *
  1449. * It is the responsibility of the caller to provide mutual
  1450. * exclusion between two different calls of device_rename
  1451. * on the same device to ensure that new_name is valid and
  1452. * won't conflict with other devices.
  1453. *
  1454. * Note: Don't call this function. Currently, the networking layer calls this
  1455. * function, but that will change. The following text from Kay Sievers offers
  1456. * some insight:
  1457. *
  1458. * Renaming devices is racy at many levels, symlinks and other stuff are not
  1459. * replaced atomically, and you get a "move" uevent, but it's not easy to
  1460. * connect the event to the old and new device. Device nodes are not renamed at
  1461. * all, there isn't even support for that in the kernel now.
  1462. *
  1463. * In the meantime, during renaming, your target name might be taken by another
  1464. * driver, creating conflicts. Or the old name is taken directly after you
  1465. * renamed it -- then you get events for the same DEVPATH, before you even see
  1466. * the "move" event. It's just a mess, and nothing new should ever rely on
  1467. * kernel device renaming. Besides that, it's not even implemented now for
  1468. * other things than (driver-core wise very simple) network devices.
  1469. *
  1470. * We are currently about to change network renaming in udev to completely
  1471. * disallow renaming of devices in the same namespace as the kernel uses,
  1472. * because we can't solve the problems properly, that arise with swapping names
  1473. * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
  1474. * be allowed to some other name than eth[0-9]*, for the aforementioned
  1475. * reasons.
  1476. *
  1477. * Make up a "real" name in the driver before you register anything, or add
  1478. * some other attributes for userspace to find the device, or use udev to add
  1479. * symlinks -- but never rename kernel devices later, it's a complete mess. We
  1480. * don't even want to get into that and try to implement the missing pieces in
  1481. * the core. We really have other pieces to fix in the driver core mess. :)
  1482. */
  1483. int device_rename(struct device *dev, const char *new_name)
  1484. {
  1485. char *old_device_name = NULL;
  1486. int error;
  1487. dev = get_device(dev);
  1488. if (!dev)
  1489. return -EINVAL;
  1490. pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
  1491. __func__, new_name);
  1492. old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
  1493. if (!old_device_name) {
  1494. error = -ENOMEM;
  1495. goto out;
  1496. }
  1497. if (dev->class) {
  1498. error = sysfs_rename_link(&dev->class->p->subsys.kobj,
  1499. &dev->kobj, old_device_name, new_name);
  1500. if (error)
  1501. goto out;
  1502. }
  1503. error = kobject_rename(&dev->kobj, new_name);
  1504. if (error)
  1505. goto out;
  1506. out:
  1507. put_device(dev);
  1508. kfree(old_device_name);
  1509. return error;
  1510. }
  1511. EXPORT_SYMBOL_GPL(device_rename);
  1512. static int device_move_class_links(struct device *dev,
  1513. struct device *old_parent,
  1514. struct device *new_parent)
  1515. {
  1516. int error = 0;
  1517. if (old_parent)
  1518. sysfs_remove_link(&dev->kobj, "device");
  1519. if (new_parent)
  1520. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1521. "device");
  1522. return error;
  1523. }
  1524. /**
  1525. * device_move - moves a device to a new parent
  1526. * @dev: the pointer to the struct device to be moved
  1527. * @new_parent: the new parent of the device (can by NULL)
  1528. * @dpm_order: how to reorder the dpm_list
  1529. */
  1530. int device_move(struct device *dev, struct device *new_parent,
  1531. enum dpm_order dpm_order)
  1532. {
  1533. int error;
  1534. struct device *old_parent;
  1535. struct kobject *new_parent_kobj;
  1536. dev = get_device(dev);
  1537. if (!dev)
  1538. return -EINVAL;
  1539. device_pm_lock();
  1540. new_parent = get_device(new_parent);
  1541. new_parent_kobj = get_device_parent(dev, new_parent);
  1542. pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
  1543. __func__, new_parent ? dev_name(new_parent) : "<NULL>");
  1544. error = kobject_move(&dev->kobj, new_parent_kobj);
  1545. if (error) {
  1546. cleanup_glue_dir(dev, new_parent_kobj);
  1547. put_device(new_parent);
  1548. goto out;
  1549. }
  1550. old_parent = dev->parent;
  1551. dev->parent = new_parent;
  1552. if (old_parent)
  1553. klist_remove(&dev->p->knode_parent);
  1554. if (new_parent) {
  1555. klist_add_tail(&dev->p->knode_parent,
  1556. &new_parent->p->klist_children);
  1557. set_dev_node(dev, dev_to_node(new_parent));
  1558. }
  1559. if (dev->class) {
  1560. error = device_move_class_links(dev, old_parent, new_parent);
  1561. if (error) {
  1562. /* We ignore errors on cleanup since we're hosed anyway... */
  1563. device_move_class_links(dev, new_parent, old_parent);
  1564. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  1565. if (new_parent)
  1566. klist_remove(&dev->p->knode_parent);
  1567. dev->parent = old_parent;
  1568. if (old_parent) {
  1569. klist_add_tail(&dev->p->knode_parent,
  1570. &old_parent->p->klist_children);
  1571. set_dev_node(dev, dev_to_node(old_parent));
  1572. }
  1573. }
  1574. cleanup_glue_dir(dev, new_parent_kobj);
  1575. put_device(new_parent);
  1576. goto out;
  1577. }
  1578. }
  1579. switch (dpm_order) {
  1580. case DPM_ORDER_NONE:
  1581. break;
  1582. case DPM_ORDER_DEV_AFTER_PARENT:
  1583. device_pm_move_after(dev, new_parent);
  1584. break;
  1585. case DPM_ORDER_PARENT_BEFORE_DEV:
  1586. device_pm_move_before(new_parent, dev);
  1587. break;
  1588. case DPM_ORDER_DEV_LAST:
  1589. device_pm_move_last(dev);
  1590. break;
  1591. }
  1592. put_device(old_parent);
  1593. out:
  1594. device_pm_unlock();
  1595. put_device(dev);
  1596. return error;
  1597. }
  1598. EXPORT_SYMBOL_GPL(device_move);
  1599. /**
  1600. * device_shutdown - call ->shutdown() on each device to shutdown.
  1601. */
  1602. void device_shutdown(void)
  1603. {
  1604. struct device *dev;
  1605. spin_lock(&devices_kset->list_lock);
  1606. /*
  1607. * Walk the devices list backward, shutting down each in turn.
  1608. * Beware that device unplug events may also start pulling
  1609. * devices offline, even as the system is shutting down.
  1610. */
  1611. while (!list_empty(&devices_kset->list)) {
  1612. dev = list_entry(devices_kset->list.prev, struct device,
  1613. kobj.entry);
  1614. /*
  1615. * hold reference count of device's parent to
  1616. * prevent it from being freed because parent's
  1617. * lock is to be held
  1618. */
  1619. get_device(dev->parent);
  1620. get_device(dev);
  1621. /*
  1622. * Make sure the device is off the kset list, in the
  1623. * event that dev->*->shutdown() doesn't remove it.
  1624. */
  1625. list_del_init(&dev->kobj.entry);
  1626. spin_unlock(&devices_kset->list_lock);
  1627. /* hold lock to avoid race with probe/release */
  1628. if (dev->parent)
  1629. device_lock(dev->parent);
  1630. device_lock(dev);
  1631. /* Don't allow any more runtime suspends */
  1632. pm_runtime_get_noresume(dev);
  1633. pm_runtime_barrier(dev);
  1634. if (dev->bus && dev->bus->shutdown) {
  1635. if (initcall_debug)
  1636. dev_info(dev, "shutdown\n");
  1637. dev->bus->shutdown(dev);
  1638. } else if (dev->driver && dev->driver->shutdown) {
  1639. if (initcall_debug)
  1640. dev_info(dev, "shutdown\n");
  1641. dev->driver->shutdown(dev);
  1642. }
  1643. device_unlock(dev);
  1644. if (dev->parent)
  1645. device_unlock(dev->parent);
  1646. put_device(dev);
  1647. put_device(dev->parent);
  1648. spin_lock(&devices_kset->list_lock);
  1649. }
  1650. spin_unlock(&devices_kset->list_lock);
  1651. async_synchronize_full();
  1652. }
  1653. /*
  1654. * Device logging functions
  1655. */
  1656. #ifdef CONFIG_PRINTK
  1657. static int
  1658. create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
  1659. {
  1660. const char *subsys;
  1661. size_t pos = 0;
  1662. if (dev->class)
  1663. subsys = dev->class->name;
  1664. else if (dev->bus)
  1665. subsys = dev->bus->name;
  1666. else
  1667. return 0;
  1668. pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
  1669. /*
  1670. * Add device identifier DEVICE=:
  1671. * b12:8 block dev_t
  1672. * c127:3 char dev_t
  1673. * n8 netdev ifindex
  1674. * +sound:card0 subsystem:devname
  1675. */
  1676. if (MAJOR(dev->devt)) {
  1677. char c;
  1678. if (strcmp(subsys, "block") == 0)
  1679. c = 'b';
  1680. else
  1681. c = 'c';
  1682. pos++;
  1683. pos += snprintf(hdr + pos, hdrlen - pos,
  1684. "DEVICE=%c%u:%u",
  1685. c, MAJOR(dev->devt), MINOR(dev->devt));
  1686. } else if (strcmp(subsys, "net") == 0) {
  1687. struct net_device *net = to_net_dev(dev);
  1688. pos++;
  1689. pos += snprintf(hdr + pos, hdrlen - pos,
  1690. "DEVICE=n%u", net->ifindex);
  1691. } else {
  1692. pos++;
  1693. pos += snprintf(hdr + pos, hdrlen - pos,
  1694. "DEVICE=+%s:%s", subsys, dev_name(dev));
  1695. }
  1696. return pos;
  1697. }
  1698. EXPORT_SYMBOL(create_syslog_header);
  1699. int dev_vprintk_emit(int level, const struct device *dev,
  1700. const char *fmt, va_list args)
  1701. {
  1702. char hdr[128];
  1703. size_t hdrlen;
  1704. hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
  1705. return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
  1706. }
  1707. EXPORT_SYMBOL(dev_vprintk_emit);
  1708. int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
  1709. {
  1710. va_list args;
  1711. int r;
  1712. va_start(args, fmt);
  1713. r = dev_vprintk_emit(level, dev, fmt, args);
  1714. va_end(args);
  1715. return r;
  1716. }
  1717. EXPORT_SYMBOL(dev_printk_emit);
  1718. static int __dev_printk(const char *level, const struct device *dev,
  1719. struct va_format *vaf)
  1720. {
  1721. if (!dev)
  1722. return printk("%s(NULL device *): %pV", level, vaf);
  1723. return dev_printk_emit(level[1] - '0', dev,
  1724. "%s %s: %pV",
  1725. dev_driver_string(dev), dev_name(dev), vaf);
  1726. }
  1727. int dev_printk(const char *level, const struct device *dev,
  1728. const char *fmt, ...)
  1729. {
  1730. struct va_format vaf;
  1731. va_list args;
  1732. int r;
  1733. va_start(args, fmt);
  1734. vaf.fmt = fmt;
  1735. vaf.va = &args;
  1736. r = __dev_printk(level, dev, &vaf);
  1737. va_end(args);
  1738. return r;
  1739. }
  1740. EXPORT_SYMBOL(dev_printk);
  1741. #define define_dev_printk_level(func, kern_level) \
  1742. int func(const struct device *dev, const char *fmt, ...) \
  1743. { \
  1744. struct va_format vaf; \
  1745. va_list args; \
  1746. int r; \
  1747. \
  1748. va_start(args, fmt); \
  1749. \
  1750. vaf.fmt = fmt; \
  1751. vaf.va = &args; \
  1752. \
  1753. r = __dev_printk(kern_level, dev, &vaf); \
  1754. \
  1755. va_end(args); \
  1756. \
  1757. return r; \
  1758. } \
  1759. EXPORT_SYMBOL(func);
  1760. define_dev_printk_level(dev_emerg, KERN_EMERG);
  1761. define_dev_printk_level(dev_alert, KERN_ALERT);
  1762. define_dev_printk_level(dev_crit, KERN_CRIT);
  1763. define_dev_printk_level(dev_err, KERN_ERR);
  1764. define_dev_printk_level(dev_warn, KERN_WARNING);
  1765. define_dev_printk_level(dev_notice, KERN_NOTICE);
  1766. define_dev_printk_level(_dev_info, KERN_INFO);
  1767. #endif