core.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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. static struct kobject *virtual_dir = NULL;
  415. if (!virtual_dir)
  416. virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
  417. return virtual_dir;
  418. }
  419. static struct kobject * get_device_parent(struct device *dev,
  420. struct device *parent)
  421. {
  422. if (dev->class) {
  423. struct kobject *kobj = NULL;
  424. struct kobject *parent_kobj;
  425. struct kobject *k;
  426. /*
  427. * If we have no parent, we live in "virtual".
  428. * Class-devices with a bus-device as parent, live
  429. * in a class-directory to prevent namespace collisions.
  430. */
  431. if (parent == NULL)
  432. parent_kobj = virtual_device_parent(dev);
  433. else if (parent->class)
  434. return &parent->kobj;
  435. else
  436. parent_kobj = &parent->kobj;
  437. /* find our class-directory at the parent and reference it */
  438. spin_lock(&dev->class->class_dirs.list_lock);
  439. list_for_each_entry(k, &dev->class->class_dirs.list, entry)
  440. if (k->parent == parent_kobj) {
  441. kobj = kobject_get(k);
  442. break;
  443. }
  444. spin_unlock(&dev->class->class_dirs.list_lock);
  445. if (kobj)
  446. return kobj;
  447. /* or create a new class-directory at the parent device */
  448. return kobject_kset_add_dir(&dev->class->class_dirs,
  449. parent_kobj, dev->class->name);
  450. }
  451. if (parent)
  452. return &parent->kobj;
  453. return NULL;
  454. }
  455. #endif
  456. static int setup_parent(struct device *dev, struct device *parent)
  457. {
  458. struct kobject *kobj;
  459. kobj = get_device_parent(dev, parent);
  460. if (IS_ERR(kobj))
  461. return PTR_ERR(kobj);
  462. if (kobj)
  463. dev->kobj.parent = kobj;
  464. return 0;
  465. }
  466. /**
  467. * device_add - add device to device hierarchy.
  468. * @dev: device.
  469. *
  470. * This is part 2 of device_register(), though may be called
  471. * separately _iff_ device_initialize() has been called separately.
  472. *
  473. * This adds it to the kobject hierarchy via kobject_add(), adds it
  474. * to the global and sibling lists for the device, then
  475. * adds it to the other relevant subsystems of the driver model.
  476. */
  477. int device_add(struct device *dev)
  478. {
  479. struct device *parent = NULL;
  480. char *class_name = NULL;
  481. struct class_interface *class_intf;
  482. int error = -EINVAL;
  483. dev = get_device(dev);
  484. if (!dev || !strlen(dev->bus_id))
  485. goto Error;
  486. pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
  487. parent = get_device(dev->parent);
  488. error = setup_parent(dev, parent);
  489. if (error)
  490. goto Error;
  491. /* first, register with generic layer. */
  492. kobject_set_name(&dev->kobj, "%s", dev->bus_id);
  493. error = kobject_add(&dev->kobj);
  494. if (error)
  495. goto Error;
  496. /* notify platform of device entry */
  497. if (platform_notify)
  498. platform_notify(dev);
  499. /* notify clients of device entry (new way) */
  500. if (dev->bus)
  501. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  502. BUS_NOTIFY_ADD_DEVICE, dev);
  503. dev->uevent_attr.attr.name = "uevent";
  504. dev->uevent_attr.attr.mode = S_IWUSR;
  505. if (dev->driver)
  506. dev->uevent_attr.attr.owner = dev->driver->owner;
  507. dev->uevent_attr.store = store_uevent;
  508. error = device_create_file(dev, &dev->uevent_attr);
  509. if (error)
  510. goto attrError;
  511. if (MAJOR(dev->devt)) {
  512. struct device_attribute *attr;
  513. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  514. if (!attr) {
  515. error = -ENOMEM;
  516. goto ueventattrError;
  517. }
  518. attr->attr.name = "dev";
  519. attr->attr.mode = S_IRUGO;
  520. if (dev->driver)
  521. attr->attr.owner = dev->driver->owner;
  522. attr->show = show_dev;
  523. error = device_create_file(dev, attr);
  524. if (error) {
  525. kfree(attr);
  526. goto ueventattrError;
  527. }
  528. dev->devt_attr = attr;
  529. }
  530. if (dev->class) {
  531. sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
  532. "subsystem");
  533. /* If this is not a "fake" compatible device, then create the
  534. * symlink from the class to the device. */
  535. if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
  536. sysfs_create_link(&dev->class->subsys.kset.kobj,
  537. &dev->kobj, dev->bus_id);
  538. if (parent) {
  539. sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  540. "device");
  541. #ifdef CONFIG_SYSFS_DEPRECATED
  542. class_name = make_class_name(dev->class->name,
  543. &dev->kobj);
  544. if (class_name)
  545. sysfs_create_link(&dev->parent->kobj,
  546. &dev->kobj, class_name);
  547. #endif
  548. }
  549. }
  550. if ((error = device_add_attrs(dev)))
  551. goto AttrsError;
  552. if ((error = device_add_groups(dev)))
  553. goto GroupError;
  554. if ((error = device_pm_add(dev)))
  555. goto PMError;
  556. if ((error = bus_add_device(dev)))
  557. goto BusError;
  558. if (!dev->uevent_suppress)
  559. kobject_uevent(&dev->kobj, KOBJ_ADD);
  560. if ((error = bus_attach_device(dev)))
  561. goto AttachError;
  562. if (parent)
  563. klist_add_tail(&dev->knode_parent, &parent->klist_children);
  564. if (dev->class) {
  565. down(&dev->class->sem);
  566. /* tie the class to the device */
  567. list_add_tail(&dev->node, &dev->class->devices);
  568. /* notify any interfaces that the device is here */
  569. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  570. if (class_intf->add_dev)
  571. class_intf->add_dev(dev, class_intf);
  572. up(&dev->class->sem);
  573. }
  574. Done:
  575. kfree(class_name);
  576. put_device(dev);
  577. return error;
  578. AttachError:
  579. bus_remove_device(dev);
  580. BusError:
  581. device_pm_remove(dev);
  582. PMError:
  583. if (dev->bus)
  584. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  585. BUS_NOTIFY_DEL_DEVICE, dev);
  586. device_remove_groups(dev);
  587. GroupError:
  588. device_remove_attrs(dev);
  589. AttrsError:
  590. if (dev->devt_attr) {
  591. device_remove_file(dev, dev->devt_attr);
  592. kfree(dev->devt_attr);
  593. }
  594. if (dev->class) {
  595. sysfs_remove_link(&dev->kobj, "subsystem");
  596. /* If this is not a "fake" compatible device, remove the
  597. * symlink from the class to the device. */
  598. if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
  599. sysfs_remove_link(&dev->class->subsys.kset.kobj,
  600. dev->bus_id);
  601. if (parent) {
  602. #ifdef CONFIG_SYSFS_DEPRECATED
  603. char *class_name = make_class_name(dev->class->name,
  604. &dev->kobj);
  605. if (class_name)
  606. sysfs_remove_link(&dev->parent->kobj,
  607. class_name);
  608. kfree(class_name);
  609. #endif
  610. sysfs_remove_link(&dev->kobj, "device");
  611. }
  612. }
  613. ueventattrError:
  614. device_remove_file(dev, &dev->uevent_attr);
  615. attrError:
  616. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  617. kobject_del(&dev->kobj);
  618. Error:
  619. if (parent)
  620. put_device(parent);
  621. goto Done;
  622. }
  623. /**
  624. * device_register - register a device with the system.
  625. * @dev: pointer to the device structure
  626. *
  627. * This happens in two clean steps - initialize the device
  628. * and add it to the system. The two steps can be called
  629. * separately, but this is the easiest and most common.
  630. * I.e. you should only call the two helpers separately if
  631. * have a clearly defined need to use and refcount the device
  632. * before it is added to the hierarchy.
  633. */
  634. int device_register(struct device *dev)
  635. {
  636. device_initialize(dev);
  637. return device_add(dev);
  638. }
  639. /**
  640. * get_device - increment reference count for device.
  641. * @dev: device.
  642. *
  643. * This simply forwards the call to kobject_get(), though
  644. * we do take care to provide for the case that we get a NULL
  645. * pointer passed in.
  646. */
  647. struct device * get_device(struct device * dev)
  648. {
  649. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  650. }
  651. /**
  652. * put_device - decrement reference count.
  653. * @dev: device in question.
  654. */
  655. void put_device(struct device * dev)
  656. {
  657. if (dev)
  658. kobject_put(&dev->kobj);
  659. }
  660. /**
  661. * device_del - delete device from system.
  662. * @dev: device.
  663. *
  664. * This is the first part of the device unregistration
  665. * sequence. This removes the device from the lists we control
  666. * from here, has it removed from the other driver model
  667. * subsystems it was added to in device_add(), and removes it
  668. * from the kobject hierarchy.
  669. *
  670. * NOTE: this should be called manually _iff_ device_add() was
  671. * also called manually.
  672. */
  673. void device_del(struct device * dev)
  674. {
  675. struct device * parent = dev->parent;
  676. struct class_interface *class_intf;
  677. if (parent)
  678. klist_del(&dev->knode_parent);
  679. if (dev->devt_attr) {
  680. device_remove_file(dev, dev->devt_attr);
  681. kfree(dev->devt_attr);
  682. }
  683. if (dev->class) {
  684. sysfs_remove_link(&dev->kobj, "subsystem");
  685. /* If this is not a "fake" compatible device, remove the
  686. * symlink from the class to the device. */
  687. if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
  688. sysfs_remove_link(&dev->class->subsys.kset.kobj,
  689. dev->bus_id);
  690. if (parent) {
  691. #ifdef CONFIG_SYSFS_DEPRECATED
  692. char *class_name = make_class_name(dev->class->name,
  693. &dev->kobj);
  694. if (class_name)
  695. sysfs_remove_link(&dev->parent->kobj,
  696. class_name);
  697. kfree(class_name);
  698. #endif
  699. sysfs_remove_link(&dev->kobj, "device");
  700. }
  701. down(&dev->class->sem);
  702. /* notify any interfaces that the device is now gone */
  703. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  704. if (class_intf->remove_dev)
  705. class_intf->remove_dev(dev, class_intf);
  706. /* remove the device from the class list */
  707. list_del_init(&dev->node);
  708. up(&dev->class->sem);
  709. /* If we live in a parent class-directory, unreference it */
  710. if (dev->kobj.parent->kset == &dev->class->class_dirs) {
  711. struct device *d;
  712. int other = 0;
  713. /*
  714. * if we are the last child of our class, delete
  715. * our class-directory at this parent
  716. */
  717. down(&dev->class->sem);
  718. list_for_each_entry(d, &dev->class->devices, node) {
  719. if (d == dev)
  720. continue;
  721. if (d->kobj.parent == dev->kobj.parent) {
  722. other = 1;
  723. break;
  724. }
  725. }
  726. if (!other)
  727. kobject_del(dev->kobj.parent);
  728. kobject_put(dev->kobj.parent);
  729. up(&dev->class->sem);
  730. }
  731. }
  732. device_remove_file(dev, &dev->uevent_attr);
  733. device_remove_groups(dev);
  734. device_remove_attrs(dev);
  735. bus_remove_device(dev);
  736. /*
  737. * Some platform devices are driven without driver attached
  738. * and managed resources may have been acquired. Make sure
  739. * all resources are released.
  740. */
  741. devres_release_all(dev);
  742. /* Notify the platform of the removal, in case they
  743. * need to do anything...
  744. */
  745. if (platform_notify_remove)
  746. platform_notify_remove(dev);
  747. if (dev->bus)
  748. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  749. BUS_NOTIFY_DEL_DEVICE, dev);
  750. device_pm_remove(dev);
  751. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  752. kobject_del(&dev->kobj);
  753. if (parent)
  754. put_device(parent);
  755. }
  756. /**
  757. * device_unregister - unregister device from system.
  758. * @dev: device going away.
  759. *
  760. * We do this in two parts, like we do device_register(). First,
  761. * we remove it from all the subsystems with device_del(), then
  762. * we decrement the reference count via put_device(). If that
  763. * is the final reference count, the device will be cleaned up
  764. * via device_release() above. Otherwise, the structure will
  765. * stick around until the final reference to the device is dropped.
  766. */
  767. void device_unregister(struct device * dev)
  768. {
  769. pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
  770. device_del(dev);
  771. put_device(dev);
  772. }
  773. static struct device * next_device(struct klist_iter * i)
  774. {
  775. struct klist_node * n = klist_next(i);
  776. return n ? container_of(n, struct device, knode_parent) : NULL;
  777. }
  778. /**
  779. * device_for_each_child - device child iterator.
  780. * @parent: parent struct device.
  781. * @data: data for the callback.
  782. * @fn: function to be called for each device.
  783. *
  784. * Iterate over @parent's child devices, and call @fn for each,
  785. * passing it @data.
  786. *
  787. * We check the return of @fn each time. If it returns anything
  788. * other than 0, we break out and return that value.
  789. */
  790. int device_for_each_child(struct device * parent, void * data,
  791. int (*fn)(struct device *, void *))
  792. {
  793. struct klist_iter i;
  794. struct device * child;
  795. int error = 0;
  796. klist_iter_init(&parent->klist_children, &i);
  797. while ((child = next_device(&i)) && !error)
  798. error = fn(child, data);
  799. klist_iter_exit(&i);
  800. return error;
  801. }
  802. /**
  803. * device_find_child - device iterator for locating a particular device.
  804. * @parent: parent struct device
  805. * @data: Data to pass to match function
  806. * @match: Callback function to check device
  807. *
  808. * This is similar to the device_for_each_child() function above, but it
  809. * returns a reference to a device that is 'found' for later use, as
  810. * determined by the @match callback.
  811. *
  812. * The callback should return 0 if the device doesn't match and non-zero
  813. * if it does. If the callback returns non-zero and a reference to the
  814. * current device can be obtained, this function will return to the caller
  815. * and not iterate over any more devices.
  816. */
  817. struct device * device_find_child(struct device *parent, void *data,
  818. int (*match)(struct device *, void *))
  819. {
  820. struct klist_iter i;
  821. struct device *child;
  822. if (!parent)
  823. return NULL;
  824. klist_iter_init(&parent->klist_children, &i);
  825. while ((child = next_device(&i)))
  826. if (match(child, data) && get_device(child))
  827. break;
  828. klist_iter_exit(&i);
  829. return child;
  830. }
  831. int __init devices_init(void)
  832. {
  833. return subsystem_register(&devices_subsys);
  834. }
  835. EXPORT_SYMBOL_GPL(device_for_each_child);
  836. EXPORT_SYMBOL_GPL(device_find_child);
  837. EXPORT_SYMBOL_GPL(device_initialize);
  838. EXPORT_SYMBOL_GPL(device_add);
  839. EXPORT_SYMBOL_GPL(device_register);
  840. EXPORT_SYMBOL_GPL(device_del);
  841. EXPORT_SYMBOL_GPL(device_unregister);
  842. EXPORT_SYMBOL_GPL(get_device);
  843. EXPORT_SYMBOL_GPL(put_device);
  844. EXPORT_SYMBOL_GPL(device_create_file);
  845. EXPORT_SYMBOL_GPL(device_remove_file);
  846. static void device_create_release(struct device *dev)
  847. {
  848. pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
  849. kfree(dev);
  850. }
  851. /**
  852. * device_create - creates a device and registers it with sysfs
  853. * @class: pointer to the struct class that this device should be registered to
  854. * @parent: pointer to the parent struct device of this new device, if any
  855. * @devt: the dev_t for the char device to be added
  856. * @fmt: string for the device's name
  857. *
  858. * This function can be used by char device classes. A struct device
  859. * will be created in sysfs, registered to the specified class.
  860. *
  861. * A "dev" file will be created, showing the dev_t for the device, if
  862. * the dev_t is not 0,0.
  863. * If a pointer to a parent struct device is passed in, the newly created
  864. * struct device will be a child of that device in sysfs.
  865. * The pointer to the struct device will be returned from the call.
  866. * Any further sysfs files that might be required can be created using this
  867. * pointer.
  868. *
  869. * Note: the struct class passed to this function must have previously
  870. * been created with a call to class_create().
  871. */
  872. struct device *device_create(struct class *class, struct device *parent,
  873. dev_t devt, const char *fmt, ...)
  874. {
  875. va_list args;
  876. struct device *dev = NULL;
  877. int retval = -ENODEV;
  878. if (class == NULL || IS_ERR(class))
  879. goto error;
  880. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  881. if (!dev) {
  882. retval = -ENOMEM;
  883. goto error;
  884. }
  885. dev->devt = devt;
  886. dev->class = class;
  887. dev->parent = parent;
  888. dev->release = device_create_release;
  889. va_start(args, fmt);
  890. vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
  891. va_end(args);
  892. retval = device_register(dev);
  893. if (retval)
  894. goto error;
  895. return dev;
  896. error:
  897. kfree(dev);
  898. return ERR_PTR(retval);
  899. }
  900. EXPORT_SYMBOL_GPL(device_create);
  901. /**
  902. * device_destroy - removes a device that was created with device_create()
  903. * @class: pointer to the struct class that this device was registered with
  904. * @devt: the dev_t of the device that was previously registered
  905. *
  906. * This call unregisters and cleans up a device that was created with a
  907. * call to device_create().
  908. */
  909. void device_destroy(struct class *class, dev_t devt)
  910. {
  911. struct device *dev = NULL;
  912. struct device *dev_tmp;
  913. down(&class->sem);
  914. list_for_each_entry(dev_tmp, &class->devices, node) {
  915. if (dev_tmp->devt == devt) {
  916. dev = dev_tmp;
  917. break;
  918. }
  919. }
  920. up(&class->sem);
  921. if (dev)
  922. device_unregister(dev);
  923. }
  924. EXPORT_SYMBOL_GPL(device_destroy);
  925. /**
  926. * device_rename - renames a device
  927. * @dev: the pointer to the struct device to be renamed
  928. * @new_name: the new name of the device
  929. */
  930. int device_rename(struct device *dev, char *new_name)
  931. {
  932. char *old_class_name = NULL;
  933. char *new_class_name = NULL;
  934. char *old_symlink_name = NULL;
  935. int error;
  936. dev = get_device(dev);
  937. if (!dev)
  938. return -EINVAL;
  939. pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
  940. #ifdef CONFIG_SYSFS_DEPRECATED
  941. if ((dev->class) && (dev->parent))
  942. old_class_name = make_class_name(dev->class->name, &dev->kobj);
  943. #endif
  944. if (dev->class) {
  945. old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  946. if (!old_symlink_name) {
  947. error = -ENOMEM;
  948. goto out_free_old_class;
  949. }
  950. strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
  951. }
  952. strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
  953. error = kobject_rename(&dev->kobj, new_name);
  954. #ifdef CONFIG_SYSFS_DEPRECATED
  955. if (old_class_name) {
  956. new_class_name = make_class_name(dev->class->name, &dev->kobj);
  957. if (new_class_name) {
  958. sysfs_create_link(&dev->parent->kobj, &dev->kobj,
  959. new_class_name);
  960. sysfs_remove_link(&dev->parent->kobj, old_class_name);
  961. }
  962. }
  963. #endif
  964. if (dev->class) {
  965. sysfs_remove_link(&dev->class->subsys.kset.kobj,
  966. old_symlink_name);
  967. sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
  968. dev->bus_id);
  969. }
  970. put_device(dev);
  971. kfree(new_class_name);
  972. kfree(old_symlink_name);
  973. out_free_old_class:
  974. kfree(old_class_name);
  975. return error;
  976. }
  977. EXPORT_SYMBOL_GPL(device_rename);
  978. static int device_move_class_links(struct device *dev,
  979. struct device *old_parent,
  980. struct device *new_parent)
  981. {
  982. int error = 0;
  983. #ifdef CONFIG_SYSFS_DEPRECATED
  984. char *class_name;
  985. class_name = make_class_name(dev->class->name, &dev->kobj);
  986. if (!class_name) {
  987. error = -ENOMEM;
  988. goto out;
  989. }
  990. if (old_parent) {
  991. sysfs_remove_link(&dev->kobj, "device");
  992. sysfs_remove_link(&old_parent->kobj, class_name);
  993. }
  994. if (new_parent) {
  995. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  996. "device");
  997. if (error)
  998. goto out;
  999. error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
  1000. class_name);
  1001. if (error)
  1002. sysfs_remove_link(&dev->kobj, "device");
  1003. }
  1004. else
  1005. error = 0;
  1006. out:
  1007. kfree(class_name);
  1008. return error;
  1009. #else
  1010. if (old_parent)
  1011. sysfs_remove_link(&dev->kobj, "device");
  1012. if (new_parent)
  1013. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1014. "device");
  1015. return error;
  1016. #endif
  1017. }
  1018. /**
  1019. * device_move - moves a device to a new parent
  1020. * @dev: the pointer to the struct device to be moved
  1021. * @new_parent: the new parent of the device (can by NULL)
  1022. */
  1023. int device_move(struct device *dev, struct device *new_parent)
  1024. {
  1025. int error;
  1026. struct device *old_parent;
  1027. struct kobject *new_parent_kobj;
  1028. dev = get_device(dev);
  1029. if (!dev)
  1030. return -EINVAL;
  1031. new_parent = get_device(new_parent);
  1032. new_parent_kobj = get_device_parent (dev, new_parent);
  1033. if (IS_ERR(new_parent_kobj)) {
  1034. error = PTR_ERR(new_parent_kobj);
  1035. put_device(new_parent);
  1036. goto out;
  1037. }
  1038. pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
  1039. new_parent ? new_parent->bus_id : "<NULL>");
  1040. error = kobject_move(&dev->kobj, new_parent_kobj);
  1041. if (error) {
  1042. put_device(new_parent);
  1043. goto out;
  1044. }
  1045. old_parent = dev->parent;
  1046. dev->parent = new_parent;
  1047. if (old_parent)
  1048. klist_remove(&dev->knode_parent);
  1049. if (new_parent)
  1050. klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
  1051. if (!dev->class)
  1052. goto out_put;
  1053. error = device_move_class_links(dev, old_parent, new_parent);
  1054. if (error) {
  1055. /* We ignore errors on cleanup since we're hosed anyway... */
  1056. device_move_class_links(dev, new_parent, old_parent);
  1057. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  1058. if (new_parent)
  1059. klist_remove(&dev->knode_parent);
  1060. if (old_parent)
  1061. klist_add_tail(&dev->knode_parent,
  1062. &old_parent->klist_children);
  1063. }
  1064. put_device(new_parent);
  1065. goto out;
  1066. }
  1067. out_put:
  1068. put_device(old_parent);
  1069. out:
  1070. put_device(dev);
  1071. return error;
  1072. }
  1073. EXPORT_SYMBOL_GPL(device_move);