core.c 42 KB

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