core.c 35 KB

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