core.c 46 KB

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