Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Begin to consolidate of_device.c

This moves all the common parts for the Sparc, Sparc64 and PowerPC
of_device.c files into drivers/of/device.c.

Apart from the simple move, Sparc gains of_match_node() and a call to
of_node_put in of_release_dev(). PowerPC gains better recovery if
device_create_file() fails in of_device_register().

Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au>
Acked-by: Paul Mackerras <paulus@samba.org>
Acked-by: David S. Miller <davem@davemloft.net>

+142 -340
+1 -121
arch/powerpc/kernel/of_device.c
··· 1 1 #include <linux/string.h> 2 2 #include <linux/kernel.h> 3 + #include <linux/of.h> 3 4 #include <linux/init.h> 4 5 #include <linux/module.h> 5 6 #include <linux/mod_devicetable.h> ··· 8 7 9 8 #include <asm/errno.h> 10 9 #include <asm/of_device.h> 11 - 12 - /** 13 - * of_match_node - Tell if an device_node has a matching of_match structure 14 - * @ids: array of of device match structures to search in 15 - * @node: the of device structure to match against 16 - * 17 - * Low level utility function used by device matching. 18 - */ 19 - const struct of_device_id *of_match_node(const struct of_device_id *matches, 20 - const struct device_node *node) 21 - { 22 - while (matches->name[0] || matches->type[0] || matches->compatible[0]) { 23 - int match = 1; 24 - if (matches->name[0]) 25 - match &= node->name 26 - && !strcmp(matches->name, node->name); 27 - if (matches->type[0]) 28 - match &= node->type 29 - && !strcmp(matches->type, node->type); 30 - if (matches->compatible[0]) 31 - match &= of_device_is_compatible(node, 32 - matches->compatible); 33 - if (match) 34 - return matches; 35 - matches++; 36 - } 37 - return NULL; 38 - } 39 - 40 - /** 41 - * of_match_device - Tell if an of_device structure has a matching 42 - * of_match structure 43 - * @ids: array of of device match structures to search in 44 - * @dev: the of device structure to match against 45 - * 46 - * Used by a driver to check whether an of_device present in the 47 - * system is in its list of supported devices. 48 - */ 49 - const struct of_device_id *of_match_device(const struct of_device_id *matches, 50 - const struct of_device *dev) 51 - { 52 - if (!dev->node) 53 - return NULL; 54 - return of_match_node(matches, dev->node); 55 - } 56 - 57 - struct of_device *of_dev_get(struct of_device *dev) 58 - { 59 - struct device *tmp; 60 - 61 - if (!dev) 62 - return NULL; 63 - tmp = get_device(&dev->dev); 64 - if (tmp) 65 - return to_of_device(tmp); 66 - else 67 - return NULL; 68 - } 69 - 70 - void of_dev_put(struct of_device *dev) 71 - { 72 - if (dev) 73 - put_device(&dev->dev); 74 - } 75 - 76 - static ssize_t dev_show_devspec(struct device *dev, 77 - struct device_attribute *attr, char *buf) 78 - { 79 - struct of_device *ofdev; 80 - 81 - ofdev = to_of_device(dev); 82 - return sprintf(buf, "%s", ofdev->node->full_name); 83 - } 84 - 85 - static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL); 86 - 87 - /** 88 - * of_release_dev - free an of device structure when all users of it are finished. 89 - * @dev: device that's been disconnected 90 - * 91 - * Will be called only by the device core when all users of this of device are 92 - * done. 93 - */ 94 - void of_release_dev(struct device *dev) 95 - { 96 - struct of_device *ofdev; 97 - 98 - ofdev = to_of_device(dev); 99 - of_node_put(ofdev->node); 100 - kfree(ofdev); 101 - } 102 - 103 - int of_device_register(struct of_device *ofdev) 104 - { 105 - int rc; 106 - 107 - BUG_ON(ofdev->node == NULL); 108 - 109 - rc = device_register(&ofdev->dev); 110 - if (rc) 111 - return rc; 112 - 113 - return device_create_file(&ofdev->dev, &dev_attr_devspec); 114 - } 115 - 116 - void of_device_unregister(struct of_device *ofdev) 117 - { 118 - device_remove_file(&ofdev->dev, &dev_attr_devspec); 119 - 120 - device_unregister(&ofdev->dev); 121 - } 122 - 123 10 124 11 ssize_t of_device_get_modalias(struct of_device *ofdev, 125 12 char *str, ssize_t len) ··· 118 229 119 230 return 0; 120 231 } 121 - 122 - 123 - EXPORT_SYMBOL(of_match_node); 124 - EXPORT_SYMBOL(of_match_device); 125 - EXPORT_SYMBOL(of_device_register); 126 - EXPORT_SYMBOL(of_device_unregister); 127 - EXPORT_SYMBOL(of_dev_get); 128 - EXPORT_SYMBOL(of_dev_put); 129 - EXPORT_SYMBOL(of_release_dev); 130 232 EXPORT_SYMBOL(of_device_uevent); 131 233 EXPORT_SYMBOL(of_device_get_modalias);
+1 -108
arch/sparc/kernel/of_device.c
··· 1 1 #include <linux/string.h> 2 2 #include <linux/kernel.h> 3 + #include <linux/of.h> 3 4 #include <linux/init.h> 4 5 #include <linux/module.h> 5 6 #include <linux/mod_devicetable.h> ··· 8 7 9 8 #include <asm/errno.h> 10 9 #include <asm/of_device.h> 11 - 12 - /** 13 - * of_match_device - Tell if an of_device structure has a matching 14 - * of_match structure 15 - * @ids: array of of device match structures to search in 16 - * @dev: the of device structure to match against 17 - * 18 - * Used by a driver to check whether an of_device present in the 19 - * system is in its list of supported devices. 20 - */ 21 - const struct of_device_id *of_match_device(const struct of_device_id *matches, 22 - const struct of_device *dev) 23 - { 24 - if (!dev->node) 25 - return NULL; 26 - while (matches->name[0] || matches->type[0] || matches->compatible[0]) { 27 - int match = 1; 28 - if (matches->name[0]) 29 - match &= dev->node->name 30 - && !strcmp(matches->name, dev->node->name); 31 - if (matches->type[0]) 32 - match &= dev->node->type 33 - && !strcmp(matches->type, dev->node->type); 34 - if (matches->compatible[0]) 35 - match &= of_device_is_compatible(dev->node, 36 - matches->compatible); 37 - if (match) 38 - return matches; 39 - matches++; 40 - } 41 - return NULL; 42 - } 43 10 44 11 static int of_platform_bus_match(struct device *dev, struct device_driver *drv) 45 12 { ··· 20 51 21 52 return of_match_device(matches, of_dev) != NULL; 22 53 } 23 - 24 - struct of_device *of_dev_get(struct of_device *dev) 25 - { 26 - struct device *tmp; 27 - 28 - if (!dev) 29 - return NULL; 30 - tmp = get_device(&dev->dev); 31 - if (tmp) 32 - return to_of_device(tmp); 33 - else 34 - return NULL; 35 - } 36 - 37 - void of_dev_put(struct of_device *dev) 38 - { 39 - if (dev) 40 - put_device(&dev->dev); 41 - } 42 - 43 54 44 55 static int of_device_probe(struct device *dev) 45 56 { ··· 684 735 driver_unregister(&drv->driver); 685 736 } 686 737 687 - 688 - static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf) 689 - { 690 - struct of_device *ofdev; 691 - 692 - ofdev = to_of_device(dev); 693 - return sprintf(buf, "%s", ofdev->node->full_name); 694 - } 695 - 696 - static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL); 697 - 698 - /** 699 - * of_release_dev - free an of device structure when all users of it are finished. 700 - * @dev: device that's been disconnected 701 - * 702 - * Will be called only by the device core when all users of this of device are 703 - * done. 704 - */ 705 - void of_release_dev(struct device *dev) 706 - { 707 - struct of_device *ofdev; 708 - 709 - ofdev = to_of_device(dev); 710 - 711 - kfree(ofdev); 712 - } 713 - 714 - int of_device_register(struct of_device *ofdev) 715 - { 716 - int rc; 717 - 718 - BUG_ON(ofdev->node == NULL); 719 - 720 - rc = device_register(&ofdev->dev); 721 - if (rc) 722 - return rc; 723 - 724 - rc = device_create_file(&ofdev->dev, &dev_attr_devspec); 725 - if (rc) 726 - device_unregister(&ofdev->dev); 727 - 728 - return rc; 729 - } 730 - 731 - void of_device_unregister(struct of_device *ofdev) 732 - { 733 - device_remove_file(&ofdev->dev, &dev_attr_devspec); 734 - device_unregister(&ofdev->dev); 735 - } 736 - 737 738 struct of_device* of_platform_device_create(struct device_node *np, 738 739 const char *bus_id, 739 740 struct device *parent, ··· 709 810 return dev; 710 811 } 711 812 712 - EXPORT_SYMBOL(of_match_device); 713 813 EXPORT_SYMBOL(of_register_driver); 714 814 EXPORT_SYMBOL(of_unregister_driver); 715 - EXPORT_SYMBOL(of_device_register); 716 - EXPORT_SYMBOL(of_device_unregister); 717 - EXPORT_SYMBOL(of_dev_get); 718 - EXPORT_SYMBOL(of_dev_put); 719 815 EXPORT_SYMBOL(of_platform_device_create); 720 - EXPORT_SYMBOL(of_release_dev);
+3 -111
arch/sparc64/kernel/of_device.c
··· 1 1 #include <linux/string.h> 2 2 #include <linux/kernel.h> 3 + #include <linux/of.h> 3 4 #include <linux/init.h> 4 5 #include <linux/module.h> 5 6 #include <linux/mod_devicetable.h> ··· 8 7 9 8 #include <asm/errno.h> 10 9 #include <asm/of_device.h> 11 - 12 - /** 13 - * of_match_device - Tell if an of_device structure has a matching 14 - * of_match structure 15 - * @ids: array of of device match structures to search in 16 - * @dev: the of device structure to match against 17 - * 18 - * Used by a driver to check whether an of_device present in the 19 - * system is in its list of supported devices. 20 - */ 21 - const struct of_device_id *of_match_device(const struct of_device_id *matches, 22 - const struct of_device *dev) 23 - { 24 - if (!dev->node) 25 - return NULL; 26 - while (matches->name[0] || matches->type[0] || matches->compatible[0]) { 27 - int match = 1; 28 - if (matches->name[0]) 29 - match &= dev->node->name 30 - && !strcmp(matches->name, dev->node->name); 31 - if (matches->type[0]) 32 - match &= dev->node->type 33 - && !strcmp(matches->type, dev->node->type); 34 - if (matches->compatible[0]) 35 - match &= of_device_is_compatible(dev->node, 36 - matches->compatible); 37 - if (match) 38 - return matches; 39 - matches++; 40 - } 41 - return NULL; 42 - } 43 10 44 11 static int of_platform_bus_match(struct device *dev, struct device_driver *drv) 45 12 { ··· 20 51 21 52 return of_match_device(matches, of_dev) != NULL; 22 53 } 23 - 24 - struct of_device *of_dev_get(struct of_device *dev) 25 - { 26 - struct device *tmp; 27 - 28 - if (!dev) 29 - return NULL; 30 - tmp = get_device(&dev->dev); 31 - if (tmp) 32 - return to_of_device(tmp); 33 - else 34 - return NULL; 35 - } 36 - 37 - void of_dev_put(struct of_device *dev) 38 - { 39 - if (dev) 40 - put_device(&dev->dev); 41 - } 42 - 43 54 44 55 static int of_device_probe(struct device *dev) 45 56 { ··· 969 1020 /* register with core */ 970 1021 return driver_register(&drv->driver); 971 1022 } 1023 + EXPORT_SYMBOL(of_register_driver); 972 1024 973 1025 void of_unregister_driver(struct of_platform_driver *drv) 974 1026 { 975 1027 driver_unregister(&drv->driver); 976 1028 } 977 - 978 - 979 - static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf) 980 - { 981 - struct of_device *ofdev; 982 - 983 - ofdev = to_of_device(dev); 984 - return sprintf(buf, "%s", ofdev->node->full_name); 985 - } 986 - 987 - static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL); 988 - 989 - /** 990 - * of_release_dev - free an of device structure when all users of it are finished. 991 - * @dev: device that's been disconnected 992 - * 993 - * Will be called only by the device core when all users of this of device are 994 - * done. 995 - */ 996 - void of_release_dev(struct device *dev) 997 - { 998 - struct of_device *ofdev; 999 - 1000 - ofdev = to_of_device(dev); 1001 - 1002 - kfree(ofdev); 1003 - } 1004 - 1005 - int of_device_register(struct of_device *ofdev) 1006 - { 1007 - int rc; 1008 - 1009 - BUG_ON(ofdev->node == NULL); 1010 - 1011 - rc = device_register(&ofdev->dev); 1012 - if (rc) 1013 - return rc; 1014 - 1015 - rc = device_create_file(&ofdev->dev, &dev_attr_devspec); 1016 - if (rc) 1017 - device_unregister(&ofdev->dev); 1018 - 1019 - return rc; 1020 - } 1021 - 1022 - void of_device_unregister(struct of_device *ofdev) 1023 - { 1024 - device_remove_file(&ofdev->dev, &dev_attr_devspec); 1025 - device_unregister(&ofdev->dev); 1026 - } 1029 + EXPORT_SYMBOL(of_unregister_driver); 1027 1030 1028 1031 struct of_device* of_platform_device_create(struct device_node *np, 1029 1032 const char *bus_id, ··· 1001 1100 1002 1101 return dev; 1003 1102 } 1004 - 1005 - EXPORT_SYMBOL(of_match_device); 1006 - EXPORT_SYMBOL(of_register_driver); 1007 - EXPORT_SYMBOL(of_unregister_driver); 1008 - EXPORT_SYMBOL(of_device_register); 1009 - EXPORT_SYMBOL(of_device_unregister); 1010 - EXPORT_SYMBOL(of_dev_get); 1011 - EXPORT_SYMBOL(of_dev_put); 1012 1103 EXPORT_SYMBOL(of_platform_device_create); 1013 - EXPORT_SYMBOL(of_release_dev);
+2
drivers/Kconfig
··· 8 8 9 9 source "drivers/mtd/Kconfig" 10 10 11 + source "drivers/of/Kconfig" 12 + 11 13 source "drivers/parport/Kconfig" 12 14 13 15 source "drivers/pnp/Kconfig"
+3
drivers/of/Kconfig
··· 1 + config OF_DEVICE 2 + def_bool y 3 + depends on OF && (SPARC || PPC_OF)
+1
drivers/of/Makefile
··· 1 1 obj-y = base.o 2 + obj-$(CONFIG_OF_DEVICE) += device.o
+131
drivers/of/device.c
··· 1 + #include <linux/string.h> 2 + #include <linux/kernel.h> 3 + #include <linux/of.h> 4 + #include <linux/init.h> 5 + #include <linux/module.h> 6 + #include <linux/mod_devicetable.h> 7 + #include <linux/slab.h> 8 + 9 + #include <asm/errno.h> 10 + #include <asm/of_device.h> 11 + 12 + /** 13 + * of_match_node - Tell if an device_node has a matching of_match structure 14 + * @ids: array of of device match structures to search in 15 + * @node: the of device structure to match against 16 + * 17 + * Low level utility function used by device matching. 18 + */ 19 + const struct of_device_id *of_match_node(const struct of_device_id *matches, 20 + const struct device_node *node) 21 + { 22 + while (matches->name[0] || matches->type[0] || matches->compatible[0]) { 23 + int match = 1; 24 + if (matches->name[0]) 25 + match &= node->name 26 + && !strcmp(matches->name, node->name); 27 + if (matches->type[0]) 28 + match &= node->type 29 + && !strcmp(matches->type, node->type); 30 + if (matches->compatible[0]) 31 + match &= of_device_is_compatible(node, 32 + matches->compatible); 33 + if (match) 34 + return matches; 35 + matches++; 36 + } 37 + return NULL; 38 + } 39 + EXPORT_SYMBOL(of_match_node); 40 + 41 + /** 42 + * of_match_device - Tell if an of_device structure has a matching 43 + * of_match structure 44 + * @ids: array of of device match structures to search in 45 + * @dev: the of device structure to match against 46 + * 47 + * Used by a driver to check whether an of_device present in the 48 + * system is in its list of supported devices. 49 + */ 50 + const struct of_device_id *of_match_device(const struct of_device_id *matches, 51 + const struct of_device *dev) 52 + { 53 + if (!dev->node) 54 + return NULL; 55 + return of_match_node(matches, dev->node); 56 + } 57 + EXPORT_SYMBOL(of_match_device); 58 + 59 + struct of_device *of_dev_get(struct of_device *dev) 60 + { 61 + struct device *tmp; 62 + 63 + if (!dev) 64 + return NULL; 65 + tmp = get_device(&dev->dev); 66 + if (tmp) 67 + return to_of_device(tmp); 68 + else 69 + return NULL; 70 + } 71 + EXPORT_SYMBOL(of_dev_get); 72 + 73 + void of_dev_put(struct of_device *dev) 74 + { 75 + if (dev) 76 + put_device(&dev->dev); 77 + } 78 + EXPORT_SYMBOL(of_dev_put); 79 + 80 + static ssize_t dev_show_devspec(struct device *dev, 81 + struct device_attribute *attr, char *buf) 82 + { 83 + struct of_device *ofdev; 84 + 85 + ofdev = to_of_device(dev); 86 + return sprintf(buf, "%s", ofdev->node->full_name); 87 + } 88 + 89 + static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL); 90 + 91 + /** 92 + * of_release_dev - free an of device structure when all users of it are finished. 93 + * @dev: device that's been disconnected 94 + * 95 + * Will be called only by the device core when all users of this of device are 96 + * done. 97 + */ 98 + void of_release_dev(struct device *dev) 99 + { 100 + struct of_device *ofdev; 101 + 102 + ofdev = to_of_device(dev); 103 + of_node_put(ofdev->node); 104 + kfree(ofdev); 105 + } 106 + EXPORT_SYMBOL(of_release_dev); 107 + 108 + int of_device_register(struct of_device *ofdev) 109 + { 110 + int rc; 111 + 112 + BUG_ON(ofdev->node == NULL); 113 + 114 + rc = device_register(&ofdev->dev); 115 + if (rc) 116 + return rc; 117 + 118 + rc = device_create_file(&ofdev->dev, &dev_attr_devspec); 119 + if (rc) 120 + device_unregister(&ofdev->dev); 121 + 122 + return rc; 123 + } 124 + EXPORT_SYMBOL(of_device_register); 125 + 126 + void of_device_unregister(struct of_device *ofdev) 127 + { 128 + device_remove_file(&ofdev->dev, &dev_attr_devspec); 129 + device_unregister(&ofdev->dev); 130 + } 131 + EXPORT_SYMBOL(of_device_unregister);