linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Sylvain Munaut <tnt@246tNt.com>
To: Paul Mackerras <paulus@samba.org>
Cc: Linux PPC Dev ML <linuxppc-dev@ozlabs.org>,
	Sylvain Munaut <tnt@246tNt.com>
Subject: [PATCH 07/10] powerpc: Add a unified uevent handler for bus based on of_device
Date: Mon, 12 Feb 2007 23:13:25 +0100	[thread overview]
Message-ID: <11713184171985-git-send-email-tnt@246tNt.com> (raw)
In-Reply-To: <11713184152189-git-send-email-tnt@246tNt.com>

This common uevent handler allow the several bus types based on
of_device to generate the uevent properly and avoiding
code duplication.

This handlers take a struct device as argument and can therefore
be used as the uevent call directly if no special treatment is
needed for the bus.

Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
---
 arch/powerpc/kernel/of_device.c |  112 +++++++++++++++++++++++++++++++++++++++
 include/asm-powerpc/of_device.h |    3 +
 2 files changed, 115 insertions(+), 0 deletions(-)

diff --git a/arch/powerpc/kernel/of_device.c b/arch/powerpc/kernel/of_device.c
index e921514..e8aa1f3 100644
--- a/arch/powerpc/kernel/of_device.c
+++ b/arch/powerpc/kernel/of_device.c
@@ -120,6 +120,117 @@ void of_device_unregister(struct of_devi
 }
 
 
+static ssize_t of_device_get_modalias(struct of_device *ofdev,
+					char *str, ssize_t len)
+{
+	const char *compat;
+	int cplen, i;
+	ssize_t tsize, csize, repend;
+
+	/* Name & Type */
+	csize = snprintf(str, len, "of:N%sT%s",
+				ofdev->node->name, ofdev->node->type);
+
+	/* Get compatible property if any */
+	compat = get_property(ofdev->node, "compatible", &cplen);
+	if (!compat)
+		return csize;
+
+	/* Find true end (we tolerate multiple \0 at the end */
+	for (i=(cplen-1); i>=0 && !compat[i]; i--)
+		cplen--;
+	if (!cplen)
+		return csize;
+	cplen++;
+
+	/* Check space (need cplen+1 chars including final \0) */
+	tsize = csize + cplen;
+	repend = tsize;
+
+	if (csize>=len)		/* @ the limit, all is already filled */
+		return tsize;
+
+	if (tsize>=len) {		/* limit compat list */
+		cplen = len-csize-1;
+		repend = len;
+	}
+
+	/* Copy and do char replacement */
+	memcpy(&str[csize+1], compat, cplen);
+	for (i=csize; i<repend; i++) {
+		char c = str[i];
+		if (c=='\0')
+			str[i] = 'C';
+		else if (c==' ')
+			str[i] = '_';
+	}
+
+	return tsize;
+}
+
+int of_device_uevent(struct device *dev,
+		char **envp, int num_envp, char *buffer, int buffer_size)
+{
+	struct of_device *ofdev;
+	const char *compat;
+	int i = 0, length = 0, seen = 0, cplen, sl;
+
+	if (!dev)
+		return -ENODEV;
+
+	ofdev = to_of_device(dev);
+
+	if (add_uevent_var(envp, num_envp, &i,
+			   buffer, buffer_size, &length,
+			   "OF_NAME=%s", ofdev->node->name))
+		return -ENOMEM;
+
+	if (add_uevent_var(envp, num_envp, &i,
+			   buffer, buffer_size, &length,
+			   "OF_TYPE=%s", ofdev->node->type))
+		return -ENOMEM;
+
+        /* Since the compatible field can contain pretty much anything
+         * it's not really legal to split it out with commas. We split it
+         * up using a number of environment variables instead. */
+
+	compat = get_property(ofdev->node, "compatible", &cplen);
+	while (compat && *compat && cplen > 0) {
+		if (add_uevent_var(envp, num_envp, &i,
+				   buffer, buffer_size, &length,
+				   "OF_COMPATIBLE_%d=%s", seen, compat))
+			return -ENOMEM;
+
+		sl = strlen (compat) + 1;
+		compat += sl;
+		cplen -= sl;
+		seen++;
+	}
+
+	if (add_uevent_var(envp, num_envp, &i,
+			   buffer, buffer_size, &length,
+			   "OF_COMPATIBLE_N=%d", seen))
+		return -ENOMEM;
+
+	/* modalias is trickier, we add it in 2 steps */
+	if (add_uevent_var(envp, num_envp, &i,
+			   buffer, buffer_size, &length,
+			   "MODALIAS="))
+		return -ENOMEM;
+
+	sl = of_device_get_modalias(ofdev, &buffer[length-1],
+					buffer_size-length);
+	if (sl >= (buffer_size-length))
+		return -ENOMEM;
+
+	length += sl;
+
+	envp[i] = NULL;
+
+	return 0;
+}
+
+
 EXPORT_SYMBOL(of_match_node);
 EXPORT_SYMBOL(of_match_device);
 EXPORT_SYMBOL(of_device_register);
@@ -127,3 +238,4 @@ EXPORT_SYMBOL(of_device_unregister);
 EXPORT_SYMBOL(of_dev_get);
 EXPORT_SYMBOL(of_dev_put);
 EXPORT_SYMBOL(of_release_dev);
+EXPORT_SYMBOL(of_device_uevent);
diff --git a/include/asm-powerpc/of_device.h b/include/asm-powerpc/of_device.h
index a889b20..4f1aabe 100644
--- a/include/asm-powerpc/of_device.h
+++ b/include/asm-powerpc/of_device.h
@@ -32,5 +32,8 @@ extern int of_device_register(struct of_
 extern void of_device_unregister(struct of_device *ofdev);
 extern void of_release_dev(struct device *dev);
 
+extern int of_device_uevent(struct device *dev,
+	char **envp, int num_envp, char *buffer, int buffer_size);
+
 #endif /* __KERNEL__ */
 #endif /* _ASM_POWERPC_OF_DEVICE_H */
-- 
1.4.2

  reply	other threads:[~2007-02-12 22:14 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-12 22:13 [PATCH 00/10] Mainly 52xx patches for 2.6.21 (but not only) Sylvain Munaut
2007-02-12 22:13 ` [PATCH 01/10] powerpc/serial: Dispose irq mapping when done in mpc52xx_serial.c Sylvain Munaut
2007-02-12 22:13   ` [PATCH 02/10] powerpc: Add device tree fixups for the EFIKA Sylvain Munaut
2007-02-12 22:13     ` [PATCH 03/10] powerpc: Restore 'proper' link order in platform Sylvain Munaut
2007-02-12 22:13       ` [PATCH 04/10] powerpc: Use common 52xx of_platform probe code for EFIKA Sylvain Munaut
2007-02-12 22:13         ` [PATCH 05/10] powerpc: Fix unbalanced of_node_{get, put} in efika-setup.c Sylvain Munaut
2007-02-12 22:13           ` [PATCH 06/10] powerpc: Small cleanup of EFIKA platform Sylvain Munaut
2007-02-12 22:13             ` Sylvain Munaut [this message]
2007-02-12 22:13               ` [PATCH 08/10] macintosh: Use the new of_device common uevent handler Sylvain Munaut
2007-02-12 22:13                 ` [PATCH 09/10] powerpc: Add uevent handler for of_platform_bus Sylvain Munaut
2007-02-12 22:13                   ` [PATCH 10/10] powerpc: Add uevent handler for ibmebus Sylvain Munaut
2007-03-29 14:46                     ` Olaf Hering
2007-03-30 11:27                       ` Hoang-Nam Nguyen
2007-03-30 11:45                         ` Sylvain Munaut
2007-03-27  6:30                 ` [PATCH 08/10] macintosh: Use the new of_device common uevent handler Benjamin Herrenschmidt
2007-03-27  7:11                   ` Sylvain Munaut
2007-03-28 10:29                     ` Johannes Berg
2007-03-28 10:58                       ` Sylvain Munaut
2007-03-28 11:10                         ` Johannes Berg
2007-02-12 23:06     ` [PATCH 02/10] powerpc: Add device tree fixups for the EFIKA Kumar Gala
2007-02-12 23:11       ` Sylvain Munaut
2007-02-12 23:18       ` Benjamin Herrenschmidt
2007-02-12 23:12     ` Benjamin Herrenschmidt
2007-02-12 23:25       ` Sylvain Munaut
2007-02-13  0:20         ` Benjamin Herrenschmidt
2007-02-16 16:44           ` Segher Boessenkool
2007-02-12 23:10   ` [PATCH 01/10] powerpc/serial: Dispose irq mapping when done in mpc52xx_serial.c Benjamin Herrenschmidt
2007-02-15 22:18 ` [PATCH] " Sylvain Munaut

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=11713184171985-git-send-email-tnt@246tNt.com \
    --to=tnt@246tnt.com \
    --cc=linuxppc-dev@ozlabs.org \
    --cc=paulus@samba.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).