core.c 43 KB

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