core.c 48 KB

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