core.c 29 KB

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