core.c 43 KB

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