core.c 34 KB

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