i2c: acorn: is tristate and should use module.h
[linux-2.6/btrfs-unstable.git] / drivers / xen / xenbus / xenbus_probe_backend.c
blob5125dce11a6083da92fc5942a09c2aa786f181d8
1 /******************************************************************************
2 * Talks to Xen Store to figure out what devices we have (backend half).
4 * Copyright (C) 2005 Rusty Russell, IBM Corporation
5 * Copyright (C) 2005 Mike Wray, Hewlett-Packard
6 * Copyright (C) 2005, 2006 XenSource Ltd
7 * Copyright (C) 2007 Solarflare Communications, Inc.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation; or, when distributed
12 * separately from the Linux kernel or incorporated into other
13 * software packages, subject to the following license:
15 * Permission is hereby granted, free of charge, to any person obtaining a copy
16 * of this source file (the "Software"), to deal in the Software without
17 * restriction, including without limitation the rights to use, copy, modify,
18 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19 * and to permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31 * IN THE SOFTWARE.
34 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36 #define DPRINTK(fmt, ...) \
37 pr_debug("(%s:%d) " fmt "\n", \
38 __func__, __LINE__, ##__VA_ARGS__)
40 #include <linux/kernel.h>
41 #include <linux/err.h>
42 #include <linux/string.h>
43 #include <linux/ctype.h>
44 #include <linux/fcntl.h>
45 #include <linux/mm.h>
46 #include <linux/notifier.h>
47 #include <linux/export.h>
49 #include <asm/page.h>
50 #include <asm/pgtable.h>
51 #include <asm/xen/hypervisor.h>
52 #include <asm/hypervisor.h>
53 #include <xen/xenbus.h>
54 #include <xen/features.h>
56 #include "xenbus_comms.h"
57 #include "xenbus_probe.h"
59 /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
60 static int backend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
62 int domid, err;
63 const char *devid, *type, *frontend;
64 unsigned int typelen;
66 type = strchr(nodename, '/');
67 if (!type)
68 return -EINVAL;
69 type++;
70 typelen = strcspn(type, "/");
71 if (!typelen || type[typelen] != '/')
72 return -EINVAL;
74 devid = strrchr(nodename, '/') + 1;
76 err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid,
77 "frontend", NULL, &frontend,
78 NULL);
79 if (err)
80 return err;
81 if (strlen(frontend) == 0)
82 err = -ERANGE;
83 if (!err && !xenbus_exists(XBT_NIL, frontend, ""))
84 err = -ENOENT;
85 kfree(frontend);
87 if (err)
88 return err;
90 if (snprintf(bus_id, XEN_BUS_ID_SIZE, "%.*s-%i-%s",
91 typelen, type, domid, devid) >= XEN_BUS_ID_SIZE)
92 return -ENOSPC;
93 return 0;
96 static int xenbus_uevent_backend(struct device *dev,
97 struct kobj_uevent_env *env)
99 struct xenbus_device *xdev;
100 struct xenbus_driver *drv;
101 struct xen_bus_type *bus;
103 DPRINTK("");
105 if (dev == NULL)
106 return -ENODEV;
108 xdev = to_xenbus_device(dev);
109 bus = container_of(xdev->dev.bus, struct xen_bus_type, bus);
111 if (add_uevent_var(env, "MODALIAS=xen-backend:%s", xdev->devicetype))
112 return -ENOMEM;
114 /* stuff we want to pass to /sbin/hotplug */
115 if (add_uevent_var(env, "XENBUS_TYPE=%s", xdev->devicetype))
116 return -ENOMEM;
118 if (add_uevent_var(env, "XENBUS_PATH=%s", xdev->nodename))
119 return -ENOMEM;
121 if (add_uevent_var(env, "XENBUS_BASE_PATH=%s", bus->root))
122 return -ENOMEM;
124 if (dev->driver) {
125 drv = to_xenbus_driver(dev->driver);
126 if (drv && drv->uevent)
127 return drv->uevent(xdev, env);
130 return 0;
133 /* backend/<typename>/<frontend-uuid>/<name> */
134 static int xenbus_probe_backend_unit(struct xen_bus_type *bus,
135 const char *dir,
136 const char *type,
137 const char *name)
139 char *nodename;
140 int err;
142 nodename = kasprintf(GFP_KERNEL, "%s/%s", dir, name);
143 if (!nodename)
144 return -ENOMEM;
146 DPRINTK("%s\n", nodename);
148 err = xenbus_probe_node(bus, type, nodename);
149 kfree(nodename);
150 return err;
153 /* backend/<typename>/<frontend-domid> */
154 static int xenbus_probe_backend(struct xen_bus_type *bus, const char *type,
155 const char *domid)
157 char *nodename;
158 int err = 0;
159 char **dir;
160 unsigned int i, dir_n = 0;
162 DPRINTK("");
164 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, domid);
165 if (!nodename)
166 return -ENOMEM;
168 dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n);
169 if (IS_ERR(dir)) {
170 kfree(nodename);
171 return PTR_ERR(dir);
174 for (i = 0; i < dir_n; i++) {
175 err = xenbus_probe_backend_unit(bus, nodename, type, dir[i]);
176 if (err)
177 break;
179 kfree(dir);
180 kfree(nodename);
181 return err;
184 static void frontend_changed(struct xenbus_watch *watch,
185 const char **vec, unsigned int len)
187 xenbus_otherend_changed(watch, vec, len, 0);
190 static struct xen_bus_type xenbus_backend = {
191 .root = "backend",
192 .levels = 3, /* backend/type/<frontend>/<id> */
193 .get_bus_id = backend_bus_id,
194 .probe = xenbus_probe_backend,
195 .otherend_changed = frontend_changed,
196 .bus = {
197 .name = "xen-backend",
198 .match = xenbus_match,
199 .uevent = xenbus_uevent_backend,
200 .probe = xenbus_dev_probe,
201 .remove = xenbus_dev_remove,
202 .shutdown = xenbus_dev_shutdown,
203 .dev_groups = xenbus_dev_groups,
207 static void backend_changed(struct xenbus_watch *watch,
208 const char **vec, unsigned int len)
210 DPRINTK("");
212 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
215 static struct xenbus_watch be_watch = {
216 .node = "backend",
217 .callback = backend_changed,
220 static int read_frontend_details(struct xenbus_device *xendev)
222 return xenbus_read_otherend_details(xendev, "frontend-id", "frontend");
225 int xenbus_dev_is_online(struct xenbus_device *dev)
227 int rc, val;
229 rc = xenbus_scanf(XBT_NIL, dev->nodename, "online", "%d", &val);
230 if (rc != 1)
231 val = 0; /* no online node present */
233 return val;
235 EXPORT_SYMBOL_GPL(xenbus_dev_is_online);
237 int xenbus_register_backend(struct xenbus_driver *drv)
239 drv->read_otherend_details = read_frontend_details;
241 return xenbus_register_driver_common(drv, &xenbus_backend);
243 EXPORT_SYMBOL_GPL(xenbus_register_backend);
245 static int backend_probe_and_watch(struct notifier_block *notifier,
246 unsigned long event,
247 void *data)
249 /* Enumerate devices in xenstore and watch for changes. */
250 xenbus_probe_devices(&xenbus_backend);
251 register_xenbus_watch(&be_watch);
253 return NOTIFY_DONE;
256 static int __init xenbus_probe_backend_init(void)
258 static struct notifier_block xenstore_notifier = {
259 .notifier_call = backend_probe_and_watch
261 int err;
263 DPRINTK("");
265 /* Register ourselves with the kernel bus subsystem */
266 err = bus_register(&xenbus_backend.bus);
267 if (err)
268 return err;
270 register_xenstore_notifier(&xenstore_notifier);
272 return 0;
274 subsys_initcall(xenbus_probe_backend_init);