core.c 50 KB

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