core.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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. #ifdef CONFIG_SYSFS_DEPRECATED
  503. if (parent) {
  504. sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  505. "device");
  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. }
  512. #endif
  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. #ifdef CONFIG_SYSFS_DEPRECATED
  566. if (parent) {
  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. sysfs_remove_link(&dev->kobj, "device");
  574. }
  575. #endif
  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. #ifdef CONFIG_SYSFS_DEPRECATED
  663. if (parent) {
  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. sysfs_remove_link(&dev->kobj, "device");
  671. }
  672. #endif
  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. /* Notify the platform of the removal, in case they
  687. * need to do anything...
  688. */
  689. if (platform_notify_remove)
  690. platform_notify_remove(dev);
  691. if (dev->bus)
  692. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  693. BUS_NOTIFY_DEL_DEVICE, dev);
  694. device_pm_remove(dev);
  695. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  696. kobject_del(&dev->kobj);
  697. if (parent)
  698. put_device(parent);
  699. }
  700. /**
  701. * device_unregister - unregister device from system.
  702. * @dev: device going away.
  703. *
  704. * We do this in two parts, like we do device_register(). First,
  705. * we remove it from all the subsystems with device_del(), then
  706. * we decrement the reference count via put_device(). If that
  707. * is the final reference count, the device will be cleaned up
  708. * via device_release() above. Otherwise, the structure will
  709. * stick around until the final reference to the device is dropped.
  710. */
  711. void device_unregister(struct device * dev)
  712. {
  713. pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
  714. device_del(dev);
  715. put_device(dev);
  716. }
  717. static struct device * next_device(struct klist_iter * i)
  718. {
  719. struct klist_node * n = klist_next(i);
  720. return n ? container_of(n, struct device, knode_parent) : NULL;
  721. }
  722. /**
  723. * device_for_each_child - device child iterator.
  724. * @parent: parent struct device.
  725. * @data: data for the callback.
  726. * @fn: function to be called for each device.
  727. *
  728. * Iterate over @parent's child devices, and call @fn for each,
  729. * passing it @data.
  730. *
  731. * We check the return of @fn each time. If it returns anything
  732. * other than 0, we break out and return that value.
  733. */
  734. int device_for_each_child(struct device * parent, void * data,
  735. int (*fn)(struct device *, void *))
  736. {
  737. struct klist_iter i;
  738. struct device * child;
  739. int error = 0;
  740. klist_iter_init(&parent->klist_children, &i);
  741. while ((child = next_device(&i)) && !error)
  742. error = fn(child, data);
  743. klist_iter_exit(&i);
  744. return error;
  745. }
  746. /**
  747. * device_find_child - device iterator for locating a particular device.
  748. * @parent: parent struct device
  749. * @data: Data to pass to match function
  750. * @match: Callback function to check device
  751. *
  752. * This is similar to the device_for_each_child() function above, but it
  753. * returns a reference to a device that is 'found' for later use, as
  754. * determined by the @match callback.
  755. *
  756. * The callback should return 0 if the device doesn't match and non-zero
  757. * if it does. If the callback returns non-zero and a reference to the
  758. * current device can be obtained, this function will return to the caller
  759. * and not iterate over any more devices.
  760. */
  761. struct device * device_find_child(struct device *parent, void *data,
  762. int (*match)(struct device *, void *))
  763. {
  764. struct klist_iter i;
  765. struct device *child;
  766. if (!parent)
  767. return NULL;
  768. klist_iter_init(&parent->klist_children, &i);
  769. while ((child = next_device(&i)))
  770. if (match(child, data) && get_device(child))
  771. break;
  772. klist_iter_exit(&i);
  773. return child;
  774. }
  775. int __init devices_init(void)
  776. {
  777. return subsystem_register(&devices_subsys);
  778. }
  779. EXPORT_SYMBOL_GPL(device_for_each_child);
  780. EXPORT_SYMBOL_GPL(device_find_child);
  781. EXPORT_SYMBOL_GPL(device_initialize);
  782. EXPORT_SYMBOL_GPL(device_add);
  783. EXPORT_SYMBOL_GPL(device_register);
  784. EXPORT_SYMBOL_GPL(device_del);
  785. EXPORT_SYMBOL_GPL(device_unregister);
  786. EXPORT_SYMBOL_GPL(get_device);
  787. EXPORT_SYMBOL_GPL(put_device);
  788. EXPORT_SYMBOL_GPL(device_create_file);
  789. EXPORT_SYMBOL_GPL(device_remove_file);
  790. static void device_create_release(struct device *dev)
  791. {
  792. pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
  793. kfree(dev);
  794. }
  795. /**
  796. * device_create - creates a device and registers it with sysfs
  797. * @class: pointer to the struct class that this device should be registered to
  798. * @parent: pointer to the parent struct device of this new device, if any
  799. * @devt: the dev_t for the char device to be added
  800. * @fmt: string for the device's name
  801. *
  802. * This function can be used by char device classes. A struct device
  803. * will be created in sysfs, registered to the specified class.
  804. *
  805. * A "dev" file will be created, showing the dev_t for the device, if
  806. * the dev_t is not 0,0.
  807. * If a pointer to a parent struct device is passed in, the newly created
  808. * struct device will be a child of that device in sysfs.
  809. * The pointer to the struct device will be returned from the call.
  810. * Any further sysfs files that might be required can be created using this
  811. * pointer.
  812. *
  813. * Note: the struct class passed to this function must have previously
  814. * been created with a call to class_create().
  815. */
  816. struct device *device_create(struct class *class, struct device *parent,
  817. dev_t devt, const char *fmt, ...)
  818. {
  819. va_list args;
  820. struct device *dev = NULL;
  821. int retval = -ENODEV;
  822. if (class == NULL || IS_ERR(class))
  823. goto error;
  824. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  825. if (!dev) {
  826. retval = -ENOMEM;
  827. goto error;
  828. }
  829. dev->devt = devt;
  830. dev->class = class;
  831. dev->parent = parent;
  832. dev->release = device_create_release;
  833. va_start(args, fmt);
  834. vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
  835. va_end(args);
  836. retval = device_register(dev);
  837. if (retval)
  838. goto error;
  839. return dev;
  840. error:
  841. kfree(dev);
  842. return ERR_PTR(retval);
  843. }
  844. EXPORT_SYMBOL_GPL(device_create);
  845. /**
  846. * device_destroy - removes a device that was created with device_create()
  847. * @class: pointer to the struct class that this device was registered with
  848. * @devt: the dev_t of the device that was previously registered
  849. *
  850. * This call unregisters and cleans up a device that was created with a
  851. * call to device_create().
  852. */
  853. void device_destroy(struct class *class, dev_t devt)
  854. {
  855. struct device *dev = NULL;
  856. struct device *dev_tmp;
  857. down(&class->sem);
  858. list_for_each_entry(dev_tmp, &class->devices, node) {
  859. if (dev_tmp->devt == devt) {
  860. dev = dev_tmp;
  861. break;
  862. }
  863. }
  864. up(&class->sem);
  865. if (dev)
  866. device_unregister(dev);
  867. }
  868. EXPORT_SYMBOL_GPL(device_destroy);
  869. /**
  870. * device_rename - renames a device
  871. * @dev: the pointer to the struct device to be renamed
  872. * @new_name: the new name of the device
  873. */
  874. int device_rename(struct device *dev, char *new_name)
  875. {
  876. char *old_class_name = NULL;
  877. char *new_class_name = NULL;
  878. char *old_symlink_name = NULL;
  879. int error;
  880. dev = get_device(dev);
  881. if (!dev)
  882. return -EINVAL;
  883. pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
  884. #ifdef CONFIG_SYSFS_DEPRECATED
  885. if ((dev->class) && (dev->parent))
  886. old_class_name = make_class_name(dev->class->name, &dev->kobj);
  887. #endif
  888. if (dev->class) {
  889. old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  890. if (!old_symlink_name) {
  891. error = -ENOMEM;
  892. goto out_free_old_class;
  893. }
  894. strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
  895. }
  896. strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
  897. error = kobject_rename(&dev->kobj, new_name);
  898. #ifdef CONFIG_SYSFS_DEPRECATED
  899. if (old_class_name) {
  900. new_class_name = make_class_name(dev->class->name, &dev->kobj);
  901. if (new_class_name) {
  902. sysfs_create_link(&dev->parent->kobj, &dev->kobj,
  903. new_class_name);
  904. sysfs_remove_link(&dev->parent->kobj, old_class_name);
  905. }
  906. }
  907. #endif
  908. if (dev->class) {
  909. sysfs_remove_link(&dev->class->subsys.kset.kobj,
  910. old_symlink_name);
  911. sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
  912. dev->bus_id);
  913. }
  914. put_device(dev);
  915. kfree(new_class_name);
  916. kfree(old_symlink_name);
  917. out_free_old_class:
  918. kfree(old_class_name);
  919. return error;
  920. }
  921. static int device_move_class_links(struct device *dev,
  922. struct device *old_parent,
  923. struct device *new_parent)
  924. {
  925. #ifdef CONFIG_SYSFS_DEPRECATED
  926. int error;
  927. char *class_name;
  928. class_name = make_class_name(dev->class->name, &dev->kobj);
  929. if (!class_name) {
  930. error = -ENOMEM;
  931. goto out;
  932. }
  933. if (old_parent) {
  934. sysfs_remove_link(&dev->kobj, "device");
  935. sysfs_remove_link(&old_parent->kobj, class_name);
  936. }
  937. if (new_parent) {
  938. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  939. "device");
  940. if (error)
  941. goto out;
  942. error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
  943. class_name);
  944. if (error)
  945. sysfs_remove_link(&dev->kobj, "device");
  946. }
  947. else
  948. error = 0;
  949. out:
  950. kfree(class_name);
  951. return error;
  952. #else
  953. return 0;
  954. #endif
  955. }
  956. /**
  957. * device_move - moves a device to a new parent
  958. * @dev: the pointer to the struct device to be moved
  959. * @new_parent: the new parent of the device (can by NULL)
  960. */
  961. int device_move(struct device *dev, struct device *new_parent)
  962. {
  963. int error;
  964. struct device *old_parent;
  965. struct kobject *new_parent_kobj;
  966. dev = get_device(dev);
  967. if (!dev)
  968. return -EINVAL;
  969. new_parent = get_device(new_parent);
  970. new_parent_kobj = get_device_parent (dev, new_parent);
  971. if (IS_ERR(new_parent_kobj)) {
  972. error = PTR_ERR(new_parent_kobj);
  973. put_device(new_parent);
  974. goto out;
  975. }
  976. pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
  977. new_parent ? new_parent->bus_id : "<NULL>");
  978. error = kobject_move(&dev->kobj, new_parent_kobj);
  979. if (error) {
  980. put_device(new_parent);
  981. goto out;
  982. }
  983. old_parent = dev->parent;
  984. dev->parent = new_parent;
  985. if (old_parent)
  986. klist_remove(&dev->knode_parent);
  987. if (new_parent)
  988. klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
  989. if (!dev->class)
  990. goto out_put;
  991. error = device_move_class_links(dev, old_parent, new_parent);
  992. if (error) {
  993. /* We ignore errors on cleanup since we're hosed anyway... */
  994. device_move_class_links(dev, new_parent, old_parent);
  995. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  996. if (new_parent)
  997. klist_remove(&dev->knode_parent);
  998. if (old_parent)
  999. klist_add_tail(&dev->knode_parent,
  1000. &old_parent->klist_children);
  1001. }
  1002. put_device(new_parent);
  1003. goto out;
  1004. }
  1005. out_put:
  1006. put_device(old_parent);
  1007. out:
  1008. put_device(dev);
  1009. return error;
  1010. }
  1011. EXPORT_SYMBOL_GPL(device_move);