core.c 31 KB

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