Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/of_address.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/of_iommu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/dma-direct.h> /* for bus_dma_region */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/dma-map-ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/mod_devicetable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <asm/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "of_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * of_match_device - Tell if a struct device matches an of_device_id list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * @matches: array of of device match structures to search in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * @dev: the of device structure to match against
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * Used by a driver to check whether an platform_device present in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * system is in its list of supported devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) const struct of_device_id *of_match_device(const struct of_device_id *matches,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 					   const struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	if ((!matches) || (!dev->of_node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	return of_match_node(matches, dev->of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) EXPORT_SYMBOL(of_match_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) struct platform_device *of_dev_get(struct platform_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct device *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	tmp = get_device(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	if (tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		return to_platform_device(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) EXPORT_SYMBOL(of_dev_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) void of_dev_put(struct platform_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	if (dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		put_device(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) EXPORT_SYMBOL(of_dev_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) int of_device_add(struct platform_device *ofdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	BUG_ON(ofdev->dev.of_node == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	/* name and id have to be set so that the platform bus doesn't get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	 * confused on matching */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	ofdev->name = dev_name(&ofdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	ofdev->id = PLATFORM_DEVID_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	 * If this device has not binding numa node in devicetree, that is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	 * of_node_to_nid returns NUMA_NO_NODE. device_add will assume that this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	 * device is on the same node as the parent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	return device_add(&ofdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  * of_dma_configure - Setup DMA configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  * @dev:	Device to apply DMA configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  * @np:		Pointer to OF node having DMA configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  * @force_dma:  Whether device is to be set up by of_dma_configure() even if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)  *		DMA capability is not explicitly described by firmware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  * @id:		Optional const pointer value input id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  * Try to get devices's DMA configuration from DT and update it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  * accordingly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * If platform code needs to use its own special DMA configuration, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  * can use a platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  * to fix up DMA configuration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) int of_dma_configure_id(struct device *dev, struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			bool force_dma, const u32 *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	const struct iommu_ops *iommu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	const struct bus_dma_region *map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	u64 dma_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	u64 mask, end, size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	bool coherent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	ret = of_dma_get_range(np, &map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		 * For legacy reasons, we have to assume some devices need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		 * DMA configuration regardless of whether "dma-ranges" is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		 * correctly specified or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		if (!force_dma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			return ret == -ENODEV ? 0 : ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		const struct bus_dma_region *r = map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		u64 dma_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		/* Determine the overall bounds of all DMA regions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		for (dma_start = ~0; r->size; r++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			/* Take lower and upper limits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			if (r->dma_start < dma_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 				dma_start = r->dma_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			if (r->dma_start + r->size > dma_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 				dma_end = r->dma_start + r->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		size = dma_end - dma_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		 * Add a work around to treat the size as mask + 1 in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		 * it is defined in DT as a mask.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		if (size & 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			dev_warn(dev, "Invalid size 0x%llx for dma-range(s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 				 size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			size = size + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		if (!size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			dev_err(dev, "Adjusted size 0x%llx invalid\n", size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			kfree(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	 * If @dev is expected to be DMA-capable then the bus code that created
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	 * it should have initialised its dma_mask pointer by this point. For
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	 * now, we'll continue the legacy behaviour of coercing it to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	 * coherent mask if not, but we'll no longer do so quietly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	if (!dev->dma_mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		dev_warn(dev, "DMA mask not set\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		dev->dma_mask = &dev->coherent_dma_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	if (!size && dev->coherent_dma_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	else if (!size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		size = 1ULL << 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	 * Limit coherent and dma mask based on size and default mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	 * set by the driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	end = dma_start + size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	mask = DMA_BIT_MASK(ilog2(end) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	dev->coherent_dma_mask &= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	*dev->dma_mask &= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	/* ...but only set bus limit and range map if we found valid dma-ranges earlier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		dev->bus_dma_limit = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		dev->dma_range_map = map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	coherent = of_dma_is_coherent(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	dev_dbg(dev, "device is%sdma coherent\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		coherent ? " " : " not ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	iommu = of_iommu_configure(dev, np, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (PTR_ERR(iommu) == -EPROBE_DEFER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		/* Don't touch range map if it wasn't set from a valid dma-ranges */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			dev->dma_range_map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		kfree(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	dev_dbg(dev, "device is%sbehind an iommu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		iommu ? " " : " not ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	arch_setup_dma_ops(dev, dma_start, size, iommu, coherent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) EXPORT_SYMBOL_GPL(of_dma_configure_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) int of_device_register(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	device_initialize(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	return of_device_add(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) EXPORT_SYMBOL(of_device_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) void of_device_unregister(struct platform_device *ofdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	device_unregister(&ofdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) EXPORT_SYMBOL(of_device_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) const void *of_device_get_match_data(const struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	const struct of_device_id *match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	match = of_match_device(dev->driver->of_match_table, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (!match)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	return match->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) EXPORT_SYMBOL(of_device_get_match_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	const char *compat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	char *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	struct property *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	ssize_t csize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	ssize_t tsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	if ((!dev) || (!dev->of_node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	/* Name & Type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	/* %p eats all alphanum characters, so %c must be used here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	csize = snprintf(str, len, "of:N%pOFn%c%s", dev->of_node, 'T',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			 of_node_get_device_type(dev->of_node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	tsize = csize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	len -= csize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		str += csize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	of_property_for_each_string(dev->of_node, "compatible", p, compat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		csize = strlen(compat) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		tsize += csize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		if (csize > len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		csize = snprintf(str, len, "C%s", compat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		for (c = str; c; ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			c = strchr(c, ' ');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			if (c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 				*c++ = '_';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		len -= csize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		str += csize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	return tsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) int of_device_request_module(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	char *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	ssize_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	size = of_device_get_modalias(dev, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (size < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	str = kmalloc(size + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	if (!str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	of_device_get_modalias(dev, str, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	str[size] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	ret = request_module(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	kfree(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) EXPORT_SYMBOL_GPL(of_device_request_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)  * of_device_modalias - Fill buffer with newline terminated modalias string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) ssize_t of_device_modalias(struct device *dev, char *str, ssize_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	ssize_t sl = of_device_get_modalias(dev, str, len - 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	if (sl < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		return sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	if (sl > len - 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	str[sl++] = '\n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	str[sl] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	return sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) EXPORT_SYMBOL_GPL(of_device_modalias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)  * of_device_uevent - Display OF related uevent information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) void of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	const char *compat, *type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	struct alias_prop *app;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	struct property *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	int seen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	if ((!dev) || (!dev->of_node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	add_uevent_var(env, "OF_NAME=%pOFn", dev->of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	add_uevent_var(env, "OF_FULLNAME=%pOF", dev->of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	type = of_node_get_device_type(dev->of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	if (type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		add_uevent_var(env, "OF_TYPE=%s", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	/* Since the compatible field can contain pretty much anything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	 * it's not really legal to split it out with commas. We split it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	 * up using a number of environment variables instead. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	of_property_for_each_string(dev->of_node, "compatible", p, compat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		seen++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	seen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	mutex_lock(&of_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	list_for_each_entry(app, &aliases_lookup, link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		if (dev->of_node == app->np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			add_uevent_var(env, "OF_ALIAS_%d=%s", seen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 				       app->alias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			seen++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	mutex_unlock(&of_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	int sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	if ((!dev) || (!dev->of_node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	/* Devicetree modalias is tricky, we add it in 2 steps */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	if (add_uevent_var(env, "MODALIAS="))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	sl = of_device_get_modalias(dev, &env->buf[env->buflen-1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 				    sizeof(env->buf) - env->buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	if (sl >= (sizeof(env->buf) - env->buflen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	env->buflen += sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) EXPORT_SYMBOL_GPL(of_device_uevent_modalias);