core.c 46 KB

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