core.c 44 KB

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