This client driver allows you to use a GPIO pin as a source for PPS
[linux-2.6/next.git] / drivers / xen / xenbus / xenbus_probe_frontend.c
blobbebb2183dc887418407e8cba8b48f3f62cf7b4fd
1 #define DPRINTK(fmt, args...) \
2 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
3 __func__, __LINE__, ##args)
5 #include <linux/kernel.h>
6 #include <linux/err.h>
7 #include <linux/string.h>
8 #include <linux/ctype.h>
9 #include <linux/fcntl.h>
10 #include <linux/mm.h>
11 #include <linux/proc_fs.h>
12 #include <linux/notifier.h>
13 #include <linux/kthread.h>
14 #include <linux/mutex.h>
15 #include <linux/io.h>
16 #include <linux/module.h>
18 #include <asm/page.h>
19 #include <asm/pgtable.h>
20 #include <asm/xen/hypervisor.h>
21 #include <xen/xenbus.h>
22 #include <xen/events.h>
23 #include <xen/page.h>
25 #include <xen/platform_pci.h>
27 #include "xenbus_comms.h"
28 #include "xenbus_probe.h"
31 /* device/<type>/<id> => <type>-<id> */
32 static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
34 nodename = strchr(nodename, '/');
35 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
36 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
37 return -EINVAL;
40 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
41 if (!strchr(bus_id, '/')) {
42 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
43 return -EINVAL;
45 *strchr(bus_id, '/') = '-';
46 return 0;
49 /* device/<typename>/<name> */
50 static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type,
51 const char *name)
53 char *nodename;
54 int err;
56 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name);
57 if (!nodename)
58 return -ENOMEM;
60 DPRINTK("%s", nodename);
62 err = xenbus_probe_node(bus, type, nodename);
63 kfree(nodename);
64 return err;
67 static int xenbus_uevent_frontend(struct device *_dev,
68 struct kobj_uevent_env *env)
70 struct xenbus_device *dev = to_xenbus_device(_dev);
72 if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
73 return -ENOMEM;
75 return 0;
79 static void backend_changed(struct xenbus_watch *watch,
80 const char **vec, unsigned int len)
82 xenbus_otherend_changed(watch, vec, len, 1);
85 static const struct dev_pm_ops xenbus_pm_ops = {
86 .suspend = xenbus_dev_suspend,
87 .resume = xenbus_dev_resume,
88 .freeze = xenbus_dev_suspend,
89 .thaw = xenbus_dev_cancel,
90 .restore = xenbus_dev_resume,
93 static struct xen_bus_type xenbus_frontend = {
94 .root = "device",
95 .levels = 2, /* device/type/<id> */
96 .get_bus_id = frontend_bus_id,
97 .probe = xenbus_probe_frontend,
98 .otherend_changed = backend_changed,
99 .bus = {
100 .name = "xen",
101 .match = xenbus_match,
102 .uevent = xenbus_uevent_frontend,
103 .probe = xenbus_dev_probe,
104 .remove = xenbus_dev_remove,
105 .shutdown = xenbus_dev_shutdown,
106 .dev_attrs = xenbus_dev_attrs,
108 .pm = &xenbus_pm_ops,
112 static void frontend_changed(struct xenbus_watch *watch,
113 const char **vec, unsigned int len)
115 DPRINTK("");
117 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
121 /* We watch for devices appearing and vanishing. */
122 static struct xenbus_watch fe_watch = {
123 .node = "device",
124 .callback = frontend_changed,
127 static int read_backend_details(struct xenbus_device *xendev)
129 return xenbus_read_otherend_details(xendev, "backend-id", "backend");
132 static int is_device_connecting(struct device *dev, void *data)
134 struct xenbus_device *xendev = to_xenbus_device(dev);
135 struct device_driver *drv = data;
136 struct xenbus_driver *xendrv;
139 * A device with no driver will never connect. We care only about
140 * devices which should currently be in the process of connecting.
142 if (!dev->driver)
143 return 0;
145 /* Is this search limited to a particular driver? */
146 if (drv && (dev->driver != drv))
147 return 0;
149 xendrv = to_xenbus_driver(dev->driver);
150 return (xendev->state < XenbusStateConnected ||
151 (xendev->state == XenbusStateConnected &&
152 xendrv->is_ready && !xendrv->is_ready(xendev)));
155 static int exists_connecting_device(struct device_driver *drv)
157 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
158 is_device_connecting);
161 static int print_device_status(struct device *dev, void *data)
163 struct xenbus_device *xendev = to_xenbus_device(dev);
164 struct device_driver *drv = data;
166 /* Is this operation limited to a particular driver? */
167 if (drv && (dev->driver != drv))
168 return 0;
170 if (!dev->driver) {
171 /* Information only: is this too noisy? */
172 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
173 xendev->nodename);
174 } else if (xendev->state < XenbusStateConnected) {
175 enum xenbus_state rstate = XenbusStateUnknown;
176 if (xendev->otherend)
177 rstate = xenbus_read_driver_state(xendev->otherend);
178 printk(KERN_WARNING "XENBUS: Timeout connecting "
179 "to device: %s (local state %d, remote state %d)\n",
180 xendev->nodename, xendev->state, rstate);
183 return 0;
186 /* We only wait for device setup after most initcalls have run. */
187 static int ready_to_wait_for_devices;
190 * On a 5-minute timeout, wait for all devices currently configured. We need
191 * to do this to guarantee that the filesystems and / or network devices
192 * needed for boot are available, before we can allow the boot to proceed.
194 * This needs to be on a late_initcall, to happen after the frontend device
195 * drivers have been initialised, but before the root fs is mounted.
197 * A possible improvement here would be to have the tools add a per-device
198 * flag to the store entry, indicating whether it is needed at boot time.
199 * This would allow people who knew what they were doing to accelerate their
200 * boot slightly, but of course needs tools or manual intervention to set up
201 * those flags correctly.
203 static void wait_for_devices(struct xenbus_driver *xendrv)
205 unsigned long start = jiffies;
206 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
207 unsigned int seconds_waited = 0;
209 if (!ready_to_wait_for_devices || !xen_domain())
210 return;
212 while (exists_connecting_device(drv)) {
213 if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
214 if (!seconds_waited)
215 printk(KERN_WARNING "XENBUS: Waiting for "
216 "devices to initialise: ");
217 seconds_waited += 5;
218 printk("%us...", 300 - seconds_waited);
219 if (seconds_waited == 300)
220 break;
223 schedule_timeout_interruptible(HZ/10);
226 if (seconds_waited)
227 printk("\n");
229 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
230 print_device_status);
233 int __xenbus_register_frontend(struct xenbus_driver *drv,
234 struct module *owner, const char *mod_name)
236 int ret;
238 drv->read_otherend_details = read_backend_details;
240 ret = xenbus_register_driver_common(drv, &xenbus_frontend,
241 owner, mod_name);
242 if (ret)
243 return ret;
245 /* If this driver is loaded as a module wait for devices to attach. */
246 wait_for_devices(drv);
248 return 0;
250 EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
252 static int frontend_probe_and_watch(struct notifier_block *notifier,
253 unsigned long event,
254 void *data)
256 /* Enumerate devices in xenstore and watch for changes. */
257 xenbus_probe_devices(&xenbus_frontend);
258 register_xenbus_watch(&fe_watch);
260 return NOTIFY_DONE;
264 static int __init xenbus_probe_frontend_init(void)
266 static struct notifier_block xenstore_notifier = {
267 .notifier_call = frontend_probe_and_watch
269 int err;
271 DPRINTK("");
273 /* Register ourselves with the kernel bus subsystem */
274 err = bus_register(&xenbus_frontend.bus);
275 if (err)
276 return err;
278 register_xenstore_notifier(&xenstore_notifier);
280 return 0;
282 subsys_initcall(xenbus_probe_frontend_init);
284 #ifndef MODULE
285 static int __init boot_wait_for_devices(void)
287 if (xen_hvm_domain() && !xen_platform_pci_unplug)
288 return -ENODEV;
290 ready_to_wait_for_devices = 1;
291 wait_for_devices(NULL);
292 return 0;
295 late_initcall(boot_wait_for_devices);
296 #endif
298 MODULE_LICENSE("GPL");