core.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  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 "base.h"
  25. #include "power/power.h"
  26. int (*platform_notify)(struct device *dev) = NULL;
  27. int (*platform_notify_remove)(struct device *dev) = NULL;
  28. static struct kobject *dev_kobj;
  29. struct kobject *sysfs_dev_char_kobj;
  30. struct kobject *sysfs_dev_block_kobj;
  31. #ifdef CONFIG_BLOCK
  32. static inline int device_is_not_partition(struct device *dev)
  33. {
  34. return !(dev->type == &part_type);
  35. }
  36. #else
  37. static inline int device_is_not_partition(struct device *dev)
  38. {
  39. return 1;
  40. }
  41. #endif
  42. /**
  43. * dev_driver_string - Return a device's driver name, if at all possible
  44. * @dev: struct device to get the name of
  45. *
  46. * Will return the device's driver's name if it is bound to a device. If
  47. * the device is not bound to a device, it will return the name of the bus
  48. * it is attached to. If it is not attached to a bus either, an empty
  49. * string will be returned.
  50. */
  51. const char *dev_driver_string(struct device *dev)
  52. {
  53. return dev->driver ? dev->driver->name :
  54. (dev->bus ? dev->bus->name :
  55. (dev->class ? dev->class->name : ""));
  56. }
  57. EXPORT_SYMBOL(dev_driver_string);
  58. #define to_dev(obj) container_of(obj, struct device, kobj)
  59. #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
  60. static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
  61. char *buf)
  62. {
  63. struct device_attribute *dev_attr = to_dev_attr(attr);
  64. struct device *dev = to_dev(kobj);
  65. ssize_t ret = -EIO;
  66. if (dev_attr->show)
  67. ret = dev_attr->show(dev, dev_attr, buf);
  68. if (ret >= (ssize_t)PAGE_SIZE) {
  69. print_symbol("dev_attr_show: %s returned bad count\n",
  70. (unsigned long)dev_attr->show);
  71. }
  72. return ret;
  73. }
  74. static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
  75. const char *buf, size_t count)
  76. {
  77. struct device_attribute *dev_attr = to_dev_attr(attr);
  78. struct device *dev = to_dev(kobj);
  79. ssize_t ret = -EIO;
  80. if (dev_attr->store)
  81. ret = dev_attr->store(dev, dev_attr, buf, count);
  82. return ret;
  83. }
  84. static struct sysfs_ops dev_sysfs_ops = {
  85. .show = dev_attr_show,
  86. .store = dev_attr_store,
  87. };
  88. /**
  89. * device_release - free device structure.
  90. * @kobj: device's kobject.
  91. *
  92. * This is called once the reference count for the object
  93. * reaches 0. We forward the call to the device's release
  94. * method, which should handle actually freeing the structure.
  95. */
  96. static void device_release(struct kobject *kobj)
  97. {
  98. struct device *dev = to_dev(kobj);
  99. if (dev->release)
  100. dev->release(dev);
  101. else if (dev->type && dev->type->release)
  102. dev->type->release(dev);
  103. else if (dev->class && dev->class->dev_release)
  104. dev->class->dev_release(dev);
  105. else {
  106. printk(KERN_ERR "Device '%s' does not have a release() "
  107. "function, it is broken and must be fixed.\n",
  108. dev->bus_id);
  109. WARN_ON(1);
  110. }
  111. }
  112. static struct kobj_type device_ktype = {
  113. .release = device_release,
  114. .sysfs_ops = &dev_sysfs_ops,
  115. };
  116. static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
  117. {
  118. struct kobj_type *ktype = get_ktype(kobj);
  119. if (ktype == &device_ktype) {
  120. struct device *dev = to_dev(kobj);
  121. if (dev->uevent_suppress)
  122. return 0;
  123. if (dev->bus)
  124. return 1;
  125. if (dev->class)
  126. return 1;
  127. }
  128. return 0;
  129. }
  130. static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
  131. {
  132. struct device *dev = to_dev(kobj);
  133. if (dev->bus)
  134. return dev->bus->name;
  135. if (dev->class)
  136. return dev->class->name;
  137. return NULL;
  138. }
  139. static int dev_uevent(struct kset *kset, struct kobject *kobj,
  140. struct kobj_uevent_env *env)
  141. {
  142. struct device *dev = to_dev(kobj);
  143. int retval = 0;
  144. /* add the major/minor if present */
  145. if (MAJOR(dev->devt)) {
  146. add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
  147. add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
  148. }
  149. if (dev->type && dev->type->name)
  150. add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
  151. if (dev->driver)
  152. add_uevent_var(env, "DRIVER=%s", dev->driver->name);
  153. #ifdef CONFIG_SYSFS_DEPRECATED
  154. if (dev->class) {
  155. struct device *parent = dev->parent;
  156. /* find first bus device in parent chain */
  157. while (parent && !parent->bus)
  158. parent = parent->parent;
  159. if (parent && parent->bus) {
  160. const char *path;
  161. path = kobject_get_path(&parent->kobj, GFP_KERNEL);
  162. if (path) {
  163. add_uevent_var(env, "PHYSDEVPATH=%s", path);
  164. kfree(path);
  165. }
  166. add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
  167. if (parent->driver)
  168. add_uevent_var(env, "PHYSDEVDRIVER=%s",
  169. parent->driver->name);
  170. }
  171. } else if (dev->bus) {
  172. add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
  173. if (dev->driver)
  174. add_uevent_var(env, "PHYSDEVDRIVER=%s",
  175. dev->driver->name);
  176. }
  177. #endif
  178. /* have the bus specific function add its stuff */
  179. if (dev->bus && dev->bus->uevent) {
  180. retval = dev->bus->uevent(dev, env);
  181. if (retval)
  182. pr_debug("device: '%s': %s: bus uevent() returned %d\n",
  183. dev->bus_id, __func__, retval);
  184. }
  185. /* have the class specific function add its stuff */
  186. if (dev->class && dev->class->dev_uevent) {
  187. retval = dev->class->dev_uevent(dev, env);
  188. if (retval)
  189. pr_debug("device: '%s': %s: class uevent() "
  190. "returned %d\n", dev->bus_id,
  191. __func__, retval);
  192. }
  193. /* have the device type specific fuction add its stuff */
  194. if (dev->type && dev->type->uevent) {
  195. retval = dev->type->uevent(dev, env);
  196. if (retval)
  197. pr_debug("device: '%s': %s: dev_type uevent() "
  198. "returned %d\n", dev->bus_id,
  199. __func__, retval);
  200. }
  201. return retval;
  202. }
  203. static struct kset_uevent_ops device_uevent_ops = {
  204. .filter = dev_uevent_filter,
  205. .name = dev_uevent_name,
  206. .uevent = dev_uevent,
  207. };
  208. static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
  209. char *buf)
  210. {
  211. struct kobject *top_kobj;
  212. struct kset *kset;
  213. struct kobj_uevent_env *env = NULL;
  214. int i;
  215. size_t count = 0;
  216. int retval;
  217. /* search the kset, the device belongs to */
  218. top_kobj = &dev->kobj;
  219. while (!top_kobj->kset && top_kobj->parent)
  220. top_kobj = top_kobj->parent;
  221. if (!top_kobj->kset)
  222. goto out;
  223. kset = top_kobj->kset;
  224. if (!kset->uevent_ops || !kset->uevent_ops->uevent)
  225. goto out;
  226. /* respect filter */
  227. if (kset->uevent_ops && kset->uevent_ops->filter)
  228. if (!kset->uevent_ops->filter(kset, &dev->kobj))
  229. goto out;
  230. env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  231. if (!env)
  232. return -ENOMEM;
  233. /* let the kset specific function add its keys */
  234. retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
  235. if (retval)
  236. goto out;
  237. /* copy keys to file */
  238. for (i = 0; i < env->envp_idx; i++)
  239. count += sprintf(&buf[count], "%s\n", env->envp[i]);
  240. out:
  241. kfree(env);
  242. return count;
  243. }
  244. static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
  245. const char *buf, size_t count)
  246. {
  247. enum kobject_action action;
  248. if (kobject_action_type(buf, count, &action) == 0) {
  249. kobject_uevent(&dev->kobj, action);
  250. goto out;
  251. }
  252. dev_err(dev, "uevent: unsupported action-string; this will "
  253. "be ignored in a future kernel version\n");
  254. kobject_uevent(&dev->kobj, KOBJ_ADD);
  255. out:
  256. return count;
  257. }
  258. static struct device_attribute uevent_attr =
  259. __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
  260. static int device_add_attributes(struct device *dev,
  261. struct device_attribute *attrs)
  262. {
  263. int error = 0;
  264. int i;
  265. if (attrs) {
  266. for (i = 0; attr_name(attrs[i]); i++) {
  267. error = device_create_file(dev, &attrs[i]);
  268. if (error)
  269. break;
  270. }
  271. if (error)
  272. while (--i >= 0)
  273. device_remove_file(dev, &attrs[i]);
  274. }
  275. return error;
  276. }
  277. static void device_remove_attributes(struct device *dev,
  278. struct device_attribute *attrs)
  279. {
  280. int i;
  281. if (attrs)
  282. for (i = 0; attr_name(attrs[i]); i++)
  283. device_remove_file(dev, &attrs[i]);
  284. }
  285. static int device_add_groups(struct device *dev,
  286. struct attribute_group **groups)
  287. {
  288. int error = 0;
  289. int i;
  290. if (groups) {
  291. for (i = 0; groups[i]; i++) {
  292. error = sysfs_create_group(&dev->kobj, groups[i]);
  293. if (error) {
  294. while (--i >= 0)
  295. sysfs_remove_group(&dev->kobj,
  296. groups[i]);
  297. break;
  298. }
  299. }
  300. }
  301. return error;
  302. }
  303. static void device_remove_groups(struct device *dev,
  304. struct attribute_group **groups)
  305. {
  306. int i;
  307. if (groups)
  308. for (i = 0; groups[i]; i++)
  309. sysfs_remove_group(&dev->kobj, groups[i]);
  310. }
  311. static int device_add_attrs(struct device *dev)
  312. {
  313. struct class *class = dev->class;
  314. struct device_type *type = dev->type;
  315. int error;
  316. if (class) {
  317. error = device_add_attributes(dev, class->dev_attrs);
  318. if (error)
  319. return error;
  320. }
  321. if (type) {
  322. error = device_add_groups(dev, type->groups);
  323. if (error)
  324. goto err_remove_class_attrs;
  325. }
  326. error = device_add_groups(dev, dev->groups);
  327. if (error)
  328. goto err_remove_type_groups;
  329. return 0;
  330. err_remove_type_groups:
  331. if (type)
  332. device_remove_groups(dev, type->groups);
  333. err_remove_class_attrs:
  334. if (class)
  335. device_remove_attributes(dev, class->dev_attrs);
  336. return error;
  337. }
  338. static void device_remove_attrs(struct device *dev)
  339. {
  340. struct class *class = dev->class;
  341. struct device_type *type = dev->type;
  342. device_remove_groups(dev, dev->groups);
  343. if (type)
  344. device_remove_groups(dev, type->groups);
  345. if (class)
  346. device_remove_attributes(dev, class->dev_attrs);
  347. }
  348. static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
  349. char *buf)
  350. {
  351. return print_dev_t(buf, dev->devt);
  352. }
  353. static struct device_attribute devt_attr =
  354. __ATTR(dev, S_IRUGO, show_dev, NULL);
  355. /* kset to create /sys/devices/ */
  356. struct kset *devices_kset;
  357. /**
  358. * device_create_file - create sysfs attribute file for device.
  359. * @dev: device.
  360. * @attr: device attribute descriptor.
  361. */
  362. int device_create_file(struct device *dev, struct device_attribute *attr)
  363. {
  364. int error = 0;
  365. if (dev)
  366. error = sysfs_create_file(&dev->kobj, &attr->attr);
  367. return error;
  368. }
  369. /**
  370. * device_remove_file - remove sysfs attribute file.
  371. * @dev: device.
  372. * @attr: device attribute descriptor.
  373. */
  374. void device_remove_file(struct device *dev, struct device_attribute *attr)
  375. {
  376. if (dev)
  377. sysfs_remove_file(&dev->kobj, &attr->attr);
  378. }
  379. /**
  380. * device_create_bin_file - create sysfs binary attribute file for device.
  381. * @dev: device.
  382. * @attr: device binary attribute descriptor.
  383. */
  384. int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
  385. {
  386. int error = -EINVAL;
  387. if (dev)
  388. error = sysfs_create_bin_file(&dev->kobj, attr);
  389. return error;
  390. }
  391. EXPORT_SYMBOL_GPL(device_create_bin_file);
  392. /**
  393. * device_remove_bin_file - remove sysfs binary attribute file
  394. * @dev: device.
  395. * @attr: device binary attribute descriptor.
  396. */
  397. void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
  398. {
  399. if (dev)
  400. sysfs_remove_bin_file(&dev->kobj, attr);
  401. }
  402. EXPORT_SYMBOL_GPL(device_remove_bin_file);
  403. /**
  404. * device_schedule_callback_owner - helper to schedule a callback for a device
  405. * @dev: device.
  406. * @func: callback function to invoke later.
  407. * @owner: module owning the callback routine
  408. *
  409. * Attribute methods must not unregister themselves or their parent device
  410. * (which would amount to the same thing). Attempts to do so will deadlock,
  411. * since unregistration is mutually exclusive with driver callbacks.
  412. *
  413. * Instead methods can call this routine, which will attempt to allocate
  414. * and schedule a workqueue request to call back @func with @dev as its
  415. * argument in the workqueue's process context. @dev will be pinned until
  416. * @func returns.
  417. *
  418. * This routine is usually called via the inline device_schedule_callback(),
  419. * which automatically sets @owner to THIS_MODULE.
  420. *
  421. * Returns 0 if the request was submitted, -ENOMEM if storage could not
  422. * be allocated, -ENODEV if a reference to @owner isn't available.
  423. *
  424. * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
  425. * underlying sysfs routine (since it is intended for use by attribute
  426. * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
  427. */
  428. int device_schedule_callback_owner(struct device *dev,
  429. void (*func)(struct device *), struct module *owner)
  430. {
  431. return sysfs_schedule_callback(&dev->kobj,
  432. (void (*)(void *)) func, dev, owner);
  433. }
  434. EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
  435. static void klist_children_get(struct klist_node *n)
  436. {
  437. struct device *dev = container_of(n, struct device, knode_parent);
  438. get_device(dev);
  439. }
  440. static void klist_children_put(struct klist_node *n)
  441. {
  442. struct device *dev = container_of(n, struct device, knode_parent);
  443. put_device(dev);
  444. }
  445. /**
  446. * device_initialize - init device structure.
  447. * @dev: device.
  448. *
  449. * This prepares the device for use by other layers,
  450. * including adding it to the device hierarchy.
  451. * It is the first half of device_register(), if called by
  452. * that, though it can also be called separately, so one
  453. * may use @dev's fields (e.g. the refcount).
  454. */
  455. void device_initialize(struct device *dev)
  456. {
  457. dev->kobj.kset = devices_kset;
  458. kobject_init(&dev->kobj, &device_ktype);
  459. klist_init(&dev->klist_children, klist_children_get,
  460. klist_children_put);
  461. INIT_LIST_HEAD(&dev->dma_pools);
  462. INIT_LIST_HEAD(&dev->node);
  463. init_MUTEX(&dev->sem);
  464. spin_lock_init(&dev->devres_lock);
  465. INIT_LIST_HEAD(&dev->devres_head);
  466. device_init_wakeup(dev, 0);
  467. set_dev_node(dev, -1);
  468. }
  469. #ifdef CONFIG_SYSFS_DEPRECATED
  470. static struct kobject *get_device_parent(struct device *dev,
  471. struct device *parent)
  472. {
  473. /* class devices without a parent live in /sys/class/<classname>/ */
  474. if (dev->class && (!parent || parent->class != dev->class))
  475. return &dev->class->p->class_subsys.kobj;
  476. /* all other devices keep their parent */
  477. else if (parent)
  478. return &parent->kobj;
  479. return NULL;
  480. }
  481. static inline void cleanup_device_parent(struct device *dev) {}
  482. static inline void cleanup_glue_dir(struct device *dev,
  483. struct kobject *glue_dir) {}
  484. #else
  485. static struct kobject *virtual_device_parent(struct device *dev)
  486. {
  487. static struct kobject *virtual_dir = NULL;
  488. if (!virtual_dir)
  489. virtual_dir = kobject_create_and_add("virtual",
  490. &devices_kset->kobj);
  491. return virtual_dir;
  492. }
  493. static struct kobject *get_device_parent(struct device *dev,
  494. struct device *parent)
  495. {
  496. int retval;
  497. if (dev->class) {
  498. struct kobject *kobj = NULL;
  499. struct kobject *parent_kobj;
  500. struct kobject *k;
  501. /*
  502. * If we have no parent, we live in "virtual".
  503. * Class-devices with a non class-device as parent, live
  504. * in a "glue" directory to prevent namespace collisions.
  505. */
  506. if (parent == NULL)
  507. parent_kobj = virtual_device_parent(dev);
  508. else if (parent->class)
  509. return &parent->kobj;
  510. else
  511. parent_kobj = &parent->kobj;
  512. /* find our class-directory at the parent and reference it */
  513. spin_lock(&dev->class->p->class_dirs.list_lock);
  514. list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
  515. if (k->parent == parent_kobj) {
  516. kobj = kobject_get(k);
  517. break;
  518. }
  519. spin_unlock(&dev->class->p->class_dirs.list_lock);
  520. if (kobj)
  521. return kobj;
  522. /* or create a new class-directory at the parent device */
  523. k = kobject_create();
  524. if (!k)
  525. return NULL;
  526. k->kset = &dev->class->p->class_dirs;
  527. retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
  528. if (retval < 0) {
  529. kobject_put(k);
  530. return NULL;
  531. }
  532. /* do not emit an uevent for this simple "glue" directory */
  533. return k;
  534. }
  535. if (parent)
  536. return &parent->kobj;
  537. return NULL;
  538. }
  539. static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
  540. {
  541. /* see if we live in a "glue" directory */
  542. if (!glue_dir || !dev->class ||
  543. glue_dir->kset != &dev->class->p->class_dirs)
  544. return;
  545. kobject_put(glue_dir);
  546. }
  547. static void cleanup_device_parent(struct device *dev)
  548. {
  549. cleanup_glue_dir(dev, dev->kobj.parent);
  550. }
  551. #endif
  552. static void setup_parent(struct device *dev, struct device *parent)
  553. {
  554. struct kobject *kobj;
  555. kobj = get_device_parent(dev, parent);
  556. if (kobj)
  557. dev->kobj.parent = kobj;
  558. }
  559. static int device_add_class_symlinks(struct device *dev)
  560. {
  561. int error;
  562. if (!dev->class)
  563. return 0;
  564. error = sysfs_create_link(&dev->kobj,
  565. &dev->class->p->class_subsys.kobj,
  566. "subsystem");
  567. if (error)
  568. goto out;
  569. #ifdef CONFIG_SYSFS_DEPRECATED
  570. /* stacked class devices need a symlink in the class directory */
  571. if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
  572. device_is_not_partition(dev)) {
  573. error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
  574. &dev->kobj, dev->bus_id);
  575. if (error)
  576. goto out_subsys;
  577. }
  578. if (dev->parent && device_is_not_partition(dev)) {
  579. struct device *parent = dev->parent;
  580. char *class_name;
  581. /*
  582. * stacked class devices have the 'device' link
  583. * pointing to the bus device instead of the parent
  584. */
  585. while (parent->class && !parent->bus && parent->parent)
  586. parent = parent->parent;
  587. error = sysfs_create_link(&dev->kobj,
  588. &parent->kobj,
  589. "device");
  590. if (error)
  591. goto out_busid;
  592. class_name = make_class_name(dev->class->name,
  593. &dev->kobj);
  594. if (class_name)
  595. error = sysfs_create_link(&dev->parent->kobj,
  596. &dev->kobj, class_name);
  597. kfree(class_name);
  598. if (error)
  599. goto out_device;
  600. }
  601. return 0;
  602. out_device:
  603. if (dev->parent && device_is_not_partition(dev))
  604. sysfs_remove_link(&dev->kobj, "device");
  605. out_busid:
  606. if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
  607. device_is_not_partition(dev))
  608. sysfs_remove_link(&dev->class->p->class_subsys.kobj,
  609. dev->bus_id);
  610. #else
  611. /* link in the class directory pointing to the device */
  612. error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
  613. &dev->kobj, dev->bus_id);
  614. if (error)
  615. goto out_subsys;
  616. if (dev->parent && device_is_not_partition(dev)) {
  617. error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  618. "device");
  619. if (error)
  620. goto out_busid;
  621. }
  622. return 0;
  623. out_busid:
  624. sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev->bus_id);
  625. #endif
  626. out_subsys:
  627. sysfs_remove_link(&dev->kobj, "subsystem");
  628. out:
  629. return error;
  630. }
  631. static void device_remove_class_symlinks(struct device *dev)
  632. {
  633. if (!dev->class)
  634. return;
  635. #ifdef CONFIG_SYSFS_DEPRECATED
  636. if (dev->parent && device_is_not_partition(dev)) {
  637. char *class_name;
  638. class_name = make_class_name(dev->class->name, &dev->kobj);
  639. if (class_name) {
  640. sysfs_remove_link(&dev->parent->kobj, class_name);
  641. kfree(class_name);
  642. }
  643. sysfs_remove_link(&dev->kobj, "device");
  644. }
  645. if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
  646. device_is_not_partition(dev))
  647. sysfs_remove_link(&dev->class->p->class_subsys.kobj,
  648. dev->bus_id);
  649. #else
  650. if (dev->parent && device_is_not_partition(dev))
  651. sysfs_remove_link(&dev->kobj, "device");
  652. sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev->bus_id);
  653. #endif
  654. sysfs_remove_link(&dev->kobj, "subsystem");
  655. }
  656. /**
  657. * dev_set_name - set a device name
  658. * @dev: device
  659. * @fmt: format string for the device's name
  660. */
  661. int dev_set_name(struct device *dev, const char *fmt, ...)
  662. {
  663. va_list vargs;
  664. va_start(vargs, fmt);
  665. vsnprintf(dev->bus_id, sizeof(dev->bus_id), fmt, vargs);
  666. va_end(vargs);
  667. return 0;
  668. }
  669. EXPORT_SYMBOL_GPL(dev_set_name);
  670. /**
  671. * device_to_dev_kobj - select a /sys/dev/ directory for the device
  672. * @dev: device
  673. *
  674. * By default we select char/ for new entries. Setting class->dev_obj
  675. * to NULL prevents an entry from being created. class->dev_kobj must
  676. * be set (or cleared) before any devices are registered to the class
  677. * otherwise device_create_sys_dev_entry() and
  678. * device_remove_sys_dev_entry() will disagree about the the presence
  679. * of the link.
  680. */
  681. static struct kobject *device_to_dev_kobj(struct device *dev)
  682. {
  683. struct kobject *kobj;
  684. if (dev->class)
  685. kobj = dev->class->dev_kobj;
  686. else
  687. kobj = sysfs_dev_char_kobj;
  688. return kobj;
  689. }
  690. static int device_create_sys_dev_entry(struct device *dev)
  691. {
  692. struct kobject *kobj = device_to_dev_kobj(dev);
  693. int error = 0;
  694. char devt_str[15];
  695. if (kobj) {
  696. format_dev_t(devt_str, dev->devt);
  697. error = sysfs_create_link(kobj, &dev->kobj, devt_str);
  698. }
  699. return error;
  700. }
  701. static void device_remove_sys_dev_entry(struct device *dev)
  702. {
  703. struct kobject *kobj = device_to_dev_kobj(dev);
  704. char devt_str[15];
  705. if (kobj) {
  706. format_dev_t(devt_str, dev->devt);
  707. sysfs_remove_link(kobj, devt_str);
  708. }
  709. }
  710. /**
  711. * device_add - add device to device hierarchy.
  712. * @dev: device.
  713. *
  714. * This is part 2 of device_register(), though may be called
  715. * separately _iff_ device_initialize() has been called separately.
  716. *
  717. * This adds it to the kobject hierarchy via kobject_add(), adds it
  718. * to the global and sibling lists for the device, then
  719. * adds it to the other relevant subsystems of the driver model.
  720. */
  721. int device_add(struct device *dev)
  722. {
  723. struct device *parent = NULL;
  724. struct class_interface *class_intf;
  725. int error;
  726. dev = get_device(dev);
  727. if (!dev || !strlen(dev->bus_id)) {
  728. error = -EINVAL;
  729. goto Done;
  730. }
  731. pr_debug("device: '%s': %s\n", dev->bus_id, __func__);
  732. parent = get_device(dev->parent);
  733. setup_parent(dev, parent);
  734. /* use parent numa_node */
  735. if (parent)
  736. set_dev_node(dev, dev_to_node(parent));
  737. /* first, register with generic layer. */
  738. error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev->bus_id);
  739. if (error)
  740. goto Error;
  741. /* notify platform of device entry */
  742. if (platform_notify)
  743. platform_notify(dev);
  744. /* notify clients of device entry (new way) */
  745. if (dev->bus)
  746. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  747. BUS_NOTIFY_ADD_DEVICE, dev);
  748. error = device_create_file(dev, &uevent_attr);
  749. if (error)
  750. goto attrError;
  751. if (MAJOR(dev->devt)) {
  752. error = device_create_file(dev, &devt_attr);
  753. if (error)
  754. goto ueventattrError;
  755. error = device_create_sys_dev_entry(dev);
  756. if (error)
  757. goto devtattrError;
  758. }
  759. error = device_add_class_symlinks(dev);
  760. if (error)
  761. goto SymlinkError;
  762. error = device_add_attrs(dev);
  763. if (error)
  764. goto AttrsError;
  765. error = bus_add_device(dev);
  766. if (error)
  767. goto BusError;
  768. error = device_pm_add(dev);
  769. if (error)
  770. goto PMError;
  771. kobject_uevent(&dev->kobj, KOBJ_ADD);
  772. bus_attach_device(dev);
  773. if (parent)
  774. klist_add_tail(&dev->knode_parent, &parent->klist_children);
  775. if (dev->class) {
  776. mutex_lock(&dev->class->p->class_mutex);
  777. /* tie the class to the device */
  778. list_add_tail(&dev->node, &dev->class->p->class_devices);
  779. /* notify any interfaces that the device is here */
  780. list_for_each_entry(class_intf,
  781. &dev->class->p->class_interfaces, node)
  782. if (class_intf->add_dev)
  783. class_intf->add_dev(dev, class_intf);
  784. mutex_unlock(&dev->class->p->class_mutex);
  785. }
  786. Done:
  787. put_device(dev);
  788. return error;
  789. PMError:
  790. bus_remove_device(dev);
  791. BusError:
  792. if (dev->bus)
  793. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  794. BUS_NOTIFY_DEL_DEVICE, dev);
  795. device_remove_attrs(dev);
  796. AttrsError:
  797. device_remove_class_symlinks(dev);
  798. SymlinkError:
  799. if (MAJOR(dev->devt))
  800. device_remove_sys_dev_entry(dev);
  801. devtattrError:
  802. if (MAJOR(dev->devt))
  803. device_remove_file(dev, &devt_attr);
  804. ueventattrError:
  805. device_remove_file(dev, &uevent_attr);
  806. attrError:
  807. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  808. kobject_del(&dev->kobj);
  809. Error:
  810. cleanup_device_parent(dev);
  811. if (parent)
  812. put_device(parent);
  813. goto Done;
  814. }
  815. /**
  816. * device_register - register a device with the system.
  817. * @dev: pointer to the device structure
  818. *
  819. * This happens in two clean steps - initialize the device
  820. * and add it to the system. The two steps can be called
  821. * separately, but this is the easiest and most common.
  822. * I.e. you should only call the two helpers separately if
  823. * have a clearly defined need to use and refcount the device
  824. * before it is added to the hierarchy.
  825. */
  826. int device_register(struct device *dev)
  827. {
  828. device_initialize(dev);
  829. return device_add(dev);
  830. }
  831. /**
  832. * get_device - increment reference count for device.
  833. * @dev: device.
  834. *
  835. * This simply forwards the call to kobject_get(), though
  836. * we do take care to provide for the case that we get a NULL
  837. * pointer passed in.
  838. */
  839. struct device *get_device(struct device *dev)
  840. {
  841. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  842. }
  843. /**
  844. * put_device - decrement reference count.
  845. * @dev: device in question.
  846. */
  847. void put_device(struct device *dev)
  848. {
  849. /* might_sleep(); */
  850. if (dev)
  851. kobject_put(&dev->kobj);
  852. }
  853. /**
  854. * device_del - delete device from system.
  855. * @dev: device.
  856. *
  857. * This is the first part of the device unregistration
  858. * sequence. This removes the device from the lists we control
  859. * from here, has it removed from the other driver model
  860. * subsystems it was added to in device_add(), and removes it
  861. * from the kobject hierarchy.
  862. *
  863. * NOTE: this should be called manually _iff_ device_add() was
  864. * also called manually.
  865. */
  866. void device_del(struct device *dev)
  867. {
  868. struct device *parent = dev->parent;
  869. struct class_interface *class_intf;
  870. device_pm_remove(dev);
  871. if (parent)
  872. klist_del(&dev->knode_parent);
  873. if (MAJOR(dev->devt)) {
  874. device_remove_sys_dev_entry(dev);
  875. device_remove_file(dev, &devt_attr);
  876. }
  877. if (dev->class) {
  878. device_remove_class_symlinks(dev);
  879. mutex_lock(&dev->class->p->class_mutex);
  880. /* notify any interfaces that the device is now gone */
  881. list_for_each_entry(class_intf,
  882. &dev->class->p->class_interfaces, node)
  883. if (class_intf->remove_dev)
  884. class_intf->remove_dev(dev, class_intf);
  885. /* remove the device from the class list */
  886. list_del_init(&dev->node);
  887. mutex_unlock(&dev->class->p->class_mutex);
  888. }
  889. device_remove_file(dev, &uevent_attr);
  890. device_remove_attrs(dev);
  891. bus_remove_device(dev);
  892. /*
  893. * Some platform devices are driven without driver attached
  894. * and managed resources may have been acquired. Make sure
  895. * all resources are released.
  896. */
  897. devres_release_all(dev);
  898. /* Notify the platform of the removal, in case they
  899. * need to do anything...
  900. */
  901. if (platform_notify_remove)
  902. platform_notify_remove(dev);
  903. if (dev->bus)
  904. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  905. BUS_NOTIFY_DEL_DEVICE, dev);
  906. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  907. cleanup_device_parent(dev);
  908. kobject_del(&dev->kobj);
  909. put_device(parent);
  910. }
  911. /**
  912. * device_unregister - unregister device from system.
  913. * @dev: device going away.
  914. *
  915. * We do this in two parts, like we do device_register(). First,
  916. * we remove it from all the subsystems with device_del(), then
  917. * we decrement the reference count via put_device(). If that
  918. * is the final reference count, the device will be cleaned up
  919. * via device_release() above. Otherwise, the structure will
  920. * stick around until the final reference to the device is dropped.
  921. */
  922. void device_unregister(struct device *dev)
  923. {
  924. pr_debug("device: '%s': %s\n", dev->bus_id, __func__);
  925. device_del(dev);
  926. put_device(dev);
  927. }
  928. static struct device *next_device(struct klist_iter *i)
  929. {
  930. struct klist_node *n = klist_next(i);
  931. return n ? container_of(n, struct device, knode_parent) : NULL;
  932. }
  933. /**
  934. * device_for_each_child - device child iterator.
  935. * @parent: parent struct device.
  936. * @data: data for the callback.
  937. * @fn: function to be called for each device.
  938. *
  939. * Iterate over @parent's child devices, and call @fn for each,
  940. * passing it @data.
  941. *
  942. * We check the return of @fn each time. If it returns anything
  943. * other than 0, we break out and return that value.
  944. */
  945. int device_for_each_child(struct device *parent, void *data,
  946. int (*fn)(struct device *dev, void *data))
  947. {
  948. struct klist_iter i;
  949. struct device *child;
  950. int error = 0;
  951. klist_iter_init(&parent->klist_children, &i);
  952. while ((child = next_device(&i)) && !error)
  953. error = fn(child, data);
  954. klist_iter_exit(&i);
  955. return error;
  956. }
  957. /**
  958. * device_find_child - device iterator for locating a particular device.
  959. * @parent: parent struct device
  960. * @data: Data to pass to match function
  961. * @match: Callback function to check device
  962. *
  963. * This is similar to the device_for_each_child() function above, but it
  964. * returns a reference to a device that is 'found' for later use, as
  965. * determined by the @match callback.
  966. *
  967. * The callback should return 0 if the device doesn't match and non-zero
  968. * if it does. If the callback returns non-zero and a reference to the
  969. * current device can be obtained, this function will return to the caller
  970. * and not iterate over any more devices.
  971. */
  972. struct device *device_find_child(struct device *parent, void *data,
  973. int (*match)(struct device *dev, void *data))
  974. {
  975. struct klist_iter i;
  976. struct device *child;
  977. if (!parent)
  978. return NULL;
  979. klist_iter_init(&parent->klist_children, &i);
  980. while ((child = next_device(&i)))
  981. if (match(child, data) && get_device(child))
  982. break;
  983. klist_iter_exit(&i);
  984. return child;
  985. }
  986. int __init devices_init(void)
  987. {
  988. devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
  989. if (!devices_kset)
  990. return -ENOMEM;
  991. dev_kobj = kobject_create_and_add("dev", NULL);
  992. if (!dev_kobj)
  993. goto dev_kobj_err;
  994. sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
  995. if (!sysfs_dev_block_kobj)
  996. goto block_kobj_err;
  997. sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
  998. if (!sysfs_dev_char_kobj)
  999. goto char_kobj_err;
  1000. return 0;
  1001. char_kobj_err:
  1002. kobject_put(sysfs_dev_block_kobj);
  1003. block_kobj_err:
  1004. kobject_put(dev_kobj);
  1005. dev_kobj_err:
  1006. kset_unregister(devices_kset);
  1007. return -ENOMEM;
  1008. }
  1009. EXPORT_SYMBOL_GPL(device_for_each_child);
  1010. EXPORT_SYMBOL_GPL(device_find_child);
  1011. EXPORT_SYMBOL_GPL(device_initialize);
  1012. EXPORT_SYMBOL_GPL(device_add);
  1013. EXPORT_SYMBOL_GPL(device_register);
  1014. EXPORT_SYMBOL_GPL(device_del);
  1015. EXPORT_SYMBOL_GPL(device_unregister);
  1016. EXPORT_SYMBOL_GPL(get_device);
  1017. EXPORT_SYMBOL_GPL(put_device);
  1018. EXPORT_SYMBOL_GPL(device_create_file);
  1019. EXPORT_SYMBOL_GPL(device_remove_file);
  1020. static void device_create_release(struct device *dev)
  1021. {
  1022. pr_debug("device: '%s': %s\n", dev->bus_id, __func__);
  1023. kfree(dev);
  1024. }
  1025. /**
  1026. * device_create_vargs - creates a device and registers it with sysfs
  1027. * @class: pointer to the struct class that this device should be registered to
  1028. * @parent: pointer to the parent struct device of this new device, if any
  1029. * @devt: the dev_t for the char device to be added
  1030. * @drvdata: the data to be added to the device for callbacks
  1031. * @fmt: string for the device's name
  1032. * @args: va_list for the device's name
  1033. *
  1034. * This function can be used by char device classes. A struct device
  1035. * will be created in sysfs, registered to the specified class.
  1036. *
  1037. * A "dev" file will be created, showing the dev_t for the device, if
  1038. * the dev_t is not 0,0.
  1039. * If a pointer to a parent struct device is passed in, the newly created
  1040. * struct device will be a child of that device in sysfs.
  1041. * The pointer to the struct device will be returned from the call.
  1042. * Any further sysfs files that might be required can be created using this
  1043. * pointer.
  1044. *
  1045. * Note: the struct class passed to this function must have previously
  1046. * been created with a call to class_create().
  1047. */
  1048. struct device *device_create_vargs(struct class *class, struct device *parent,
  1049. dev_t devt, void *drvdata, const char *fmt,
  1050. va_list args)
  1051. {
  1052. struct device *dev = NULL;
  1053. int retval = -ENODEV;
  1054. if (class == NULL || IS_ERR(class))
  1055. goto error;
  1056. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1057. if (!dev) {
  1058. retval = -ENOMEM;
  1059. goto error;
  1060. }
  1061. dev->devt = devt;
  1062. dev->class = class;
  1063. dev->parent = parent;
  1064. dev->release = device_create_release;
  1065. dev_set_drvdata(dev, drvdata);
  1066. vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
  1067. retval = device_register(dev);
  1068. if (retval)
  1069. goto error;
  1070. return dev;
  1071. error:
  1072. kfree(dev);
  1073. return ERR_PTR(retval);
  1074. }
  1075. EXPORT_SYMBOL_GPL(device_create_vargs);
  1076. /**
  1077. * device_create - creates a device and registers it with sysfs
  1078. * @class: pointer to the struct class that this device should be registered to
  1079. * @parent: pointer to the parent struct device of this new device, if any
  1080. * @devt: the dev_t for the char device to be added
  1081. * @drvdata: the data to be added to the device for callbacks
  1082. * @fmt: string for the device's name
  1083. *
  1084. * This function can be used by char device classes. A struct device
  1085. * will be created in sysfs, registered to the specified class.
  1086. *
  1087. * A "dev" file will be created, showing the dev_t for the device, if
  1088. * the dev_t is not 0,0.
  1089. * If a pointer to a parent struct device is passed in, the newly created
  1090. * struct device will be a child of that device in sysfs.
  1091. * The pointer to the struct device will be returned from the call.
  1092. * Any further sysfs files that might be required can be created using this
  1093. * pointer.
  1094. *
  1095. * Note: the struct class passed to this function must have previously
  1096. * been created with a call to class_create().
  1097. */
  1098. struct device *device_create(struct class *class, struct device *parent,
  1099. dev_t devt, void *drvdata, const char *fmt, ...)
  1100. {
  1101. va_list vargs;
  1102. struct device *dev;
  1103. va_start(vargs, fmt);
  1104. dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
  1105. va_end(vargs);
  1106. return dev;
  1107. }
  1108. EXPORT_SYMBOL_GPL(device_create);
  1109. static int __match_devt(struct device *dev, void *data)
  1110. {
  1111. dev_t *devt = data;
  1112. return dev->devt == *devt;
  1113. }
  1114. /**
  1115. * device_destroy - removes a device that was created with device_create()
  1116. * @class: pointer to the struct class that this device was registered with
  1117. * @devt: the dev_t of the device that was previously registered
  1118. *
  1119. * This call unregisters and cleans up a device that was created with a
  1120. * call to device_create().
  1121. */
  1122. void device_destroy(struct class *class, dev_t devt)
  1123. {
  1124. struct device *dev;
  1125. dev = class_find_device(class, NULL, &devt, __match_devt);
  1126. if (dev) {
  1127. put_device(dev);
  1128. device_unregister(dev);
  1129. }
  1130. }
  1131. EXPORT_SYMBOL_GPL(device_destroy);
  1132. /**
  1133. * device_rename - renames a device
  1134. * @dev: the pointer to the struct device to be renamed
  1135. * @new_name: the new name of the device
  1136. */
  1137. int device_rename(struct device *dev, char *new_name)
  1138. {
  1139. char *old_class_name = NULL;
  1140. char *new_class_name = NULL;
  1141. char *old_device_name = NULL;
  1142. int error;
  1143. dev = get_device(dev);
  1144. if (!dev)
  1145. return -EINVAL;
  1146. pr_debug("device: '%s': %s: renaming to '%s'\n", dev->bus_id,
  1147. __func__, new_name);
  1148. #ifdef CONFIG_SYSFS_DEPRECATED
  1149. if ((dev->class) && (dev->parent))
  1150. old_class_name = make_class_name(dev->class->name, &dev->kobj);
  1151. #endif
  1152. old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  1153. if (!old_device_name) {
  1154. error = -ENOMEM;
  1155. goto out;
  1156. }
  1157. strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE);
  1158. strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
  1159. error = kobject_rename(&dev->kobj, new_name);
  1160. if (error) {
  1161. strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
  1162. goto out;
  1163. }
  1164. #ifdef CONFIG_SYSFS_DEPRECATED
  1165. if (old_class_name) {
  1166. new_class_name = make_class_name(dev->class->name, &dev->kobj);
  1167. if (new_class_name) {
  1168. error = sysfs_create_link_nowarn(&dev->parent->kobj,
  1169. &dev->kobj,
  1170. new_class_name);
  1171. if (error)
  1172. goto out;
  1173. sysfs_remove_link(&dev->parent->kobj, old_class_name);
  1174. }
  1175. }
  1176. #else
  1177. if (dev->class) {
  1178. error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
  1179. &dev->kobj, dev->bus_id);
  1180. if (error)
  1181. goto out;
  1182. sysfs_remove_link(&dev->class->p->class_subsys.kobj,
  1183. old_device_name);
  1184. }
  1185. #endif
  1186. out:
  1187. put_device(dev);
  1188. kfree(new_class_name);
  1189. kfree(old_class_name);
  1190. kfree(old_device_name);
  1191. return error;
  1192. }
  1193. EXPORT_SYMBOL_GPL(device_rename);
  1194. static int device_move_class_links(struct device *dev,
  1195. struct device *old_parent,
  1196. struct device *new_parent)
  1197. {
  1198. int error = 0;
  1199. #ifdef CONFIG_SYSFS_DEPRECATED
  1200. char *class_name;
  1201. class_name = make_class_name(dev->class->name, &dev->kobj);
  1202. if (!class_name) {
  1203. error = -ENOMEM;
  1204. goto out;
  1205. }
  1206. if (old_parent) {
  1207. sysfs_remove_link(&dev->kobj, "device");
  1208. sysfs_remove_link(&old_parent->kobj, class_name);
  1209. }
  1210. if (new_parent) {
  1211. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1212. "device");
  1213. if (error)
  1214. goto out;
  1215. error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
  1216. class_name);
  1217. if (error)
  1218. sysfs_remove_link(&dev->kobj, "device");
  1219. } else
  1220. error = 0;
  1221. out:
  1222. kfree(class_name);
  1223. return error;
  1224. #else
  1225. if (old_parent)
  1226. sysfs_remove_link(&dev->kobj, "device");
  1227. if (new_parent)
  1228. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1229. "device");
  1230. return error;
  1231. #endif
  1232. }
  1233. /**
  1234. * device_move - moves a device to a new parent
  1235. * @dev: the pointer to the struct device to be moved
  1236. * @new_parent: the new parent of the device (can by NULL)
  1237. */
  1238. int device_move(struct device *dev, struct device *new_parent)
  1239. {
  1240. int error;
  1241. struct device *old_parent;
  1242. struct kobject *new_parent_kobj;
  1243. dev = get_device(dev);
  1244. if (!dev)
  1245. return -EINVAL;
  1246. new_parent = get_device(new_parent);
  1247. new_parent_kobj = get_device_parent(dev, new_parent);
  1248. pr_debug("device: '%s': %s: moving to '%s'\n", dev->bus_id,
  1249. __func__, new_parent ? new_parent->bus_id : "<NULL>");
  1250. error = kobject_move(&dev->kobj, new_parent_kobj);
  1251. if (error) {
  1252. cleanup_glue_dir(dev, new_parent_kobj);
  1253. put_device(new_parent);
  1254. goto out;
  1255. }
  1256. old_parent = dev->parent;
  1257. dev->parent = new_parent;
  1258. if (old_parent)
  1259. klist_remove(&dev->knode_parent);
  1260. if (new_parent) {
  1261. klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
  1262. set_dev_node(dev, dev_to_node(new_parent));
  1263. }
  1264. if (!dev->class)
  1265. goto out_put;
  1266. error = device_move_class_links(dev, old_parent, new_parent);
  1267. if (error) {
  1268. /* We ignore errors on cleanup since we're hosed anyway... */
  1269. device_move_class_links(dev, new_parent, old_parent);
  1270. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  1271. if (new_parent)
  1272. klist_remove(&dev->knode_parent);
  1273. dev->parent = old_parent;
  1274. if (old_parent) {
  1275. klist_add_tail(&dev->knode_parent,
  1276. &old_parent->klist_children);
  1277. set_dev_node(dev, dev_to_node(old_parent));
  1278. }
  1279. }
  1280. cleanup_glue_dir(dev, new_parent_kobj);
  1281. put_device(new_parent);
  1282. goto out;
  1283. }
  1284. out_put:
  1285. put_device(old_parent);
  1286. out:
  1287. put_device(dev);
  1288. return error;
  1289. }
  1290. EXPORT_SYMBOL_GPL(device_move);
  1291. /**
  1292. * device_shutdown - call ->shutdown() on each device to shutdown.
  1293. */
  1294. void device_shutdown(void)
  1295. {
  1296. struct device *dev, *devn;
  1297. list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
  1298. kobj.entry) {
  1299. if (dev->bus && dev->bus->shutdown) {
  1300. dev_dbg(dev, "shutdown\n");
  1301. dev->bus->shutdown(dev);
  1302. } else if (dev->driver && dev->driver->shutdown) {
  1303. dev_dbg(dev, "shutdown\n");
  1304. dev->driver->shutdown(dev);
  1305. }
  1306. }
  1307. kobject_put(sysfs_dev_char_kobj);
  1308. kobject_put(sysfs_dev_block_kobj);
  1309. kobject_put(dev_kobj);
  1310. }