core.c 44 KB

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