core.c 30 KB

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