core.c 51 KB

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