/****************************************************************************** * Talks to Xen Store to figure out what devices we have. * * Copyright (C) 2005 Rusty Russell, IBM Corporation * Copyright (C) 2005 Mike Wray, Hewlett-Packard * Copyright (C) 2005, 2006 XenSource Ltd * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation; or, when distributed * separately from the Linux kernel or incorporated into other * software packages, subject to the following license: * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this source file (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, copy, modify, * merge, publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. */ #define DPRINTK(fmt, args...) \ pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \ __func__, __LINE__, ##args) #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "xenbus_comms.h" #include "xenbus_probe.h" int xen_store_evtchn; EXPORT_SYMBOL(xen_store_evtchn); struct xenstore_domain_interface *xen_store_interface; static unsigned long xen_store_mfn; static BLOCKING_NOTIFIER_HEAD(xenstore_chain); static void wait_for_devices(struct xenbus_driver *xendrv); static int xenbus_probe_frontend(const char *type, const char *name); static void xenbus_dev_shutdown(struct device *_dev); static int xenbus_dev_suspend(struct device *dev, pm_message_t state); static int xenbus_dev_resume(struct device *dev); /* If something in array of ids matches this device, return it. */ static const struct xenbus_device_id * match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev) { for (; *arr->devicetype != '\0'; arr++) { if (!strcmp(arr->devicetype, dev->devicetype)) return arr; } return NULL; } int xenbus_match(struct device *_dev, struct device_driver *_drv) { struct xenbus_driver *drv = to_xenbus_driver(_drv); if (!drv->ids) return 0; return match_device(drv->ids, to_xenbus_device(_dev)) != NULL; } static int xenbus_uevent(struct device *_dev, struct kobj_uevent_env *env) { struct xenbus_device *dev = to_xenbus_device(_dev); if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype)) return -ENOMEM; return 0; } /* device// => - */ static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename) { nodename = strchr(nodename, '/'); if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) { printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename); return -EINVAL; } strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE); if (!strchr(bus_id, '/')) { printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id); return -EINVAL; } *strchr(bus_id, '/') = '-'; return 0; } static void free_otherend_details(struct xenbus_device *dev) { kfree(dev->otherend); dev->otherend = NULL; } static void free_otherend_watch(struct xenbus_device *dev) { if (dev->otherend_watch.node) { unregister_xenbus_watch(&dev->otherend_watch); kfree(dev->otherend_watch.node); dev->otherend_watch.node = NULL; } } int read_otherend_details(struct xenbus_device *xendev, char *id_node, char *path_node) { int err = xenbus_gather(XBT_NIL, xendev->nodename, id_node, "%i", &xendev->otherend_id, path_node, NULL, &xendev->otherend, NULL); if (err) { xenbus_dev_fatal(xendev, err, "reading other end details from %s", xendev->nodename); return err; } if (strlen(xendev->otherend) == 0 || !xenbus_exists(XBT_NIL, xendev->otherend, "")) { xenbus_dev_fatal(xendev, -ENOENT, "unable to read other end from %s. " "missing or inaccessible.", xendev->nodename); free_otherend_details(xendev); return -ENOENT; } return 0; } static int read_backend_details(struct xenbus_device *xendev) { return read_otherend_details(xendev, "backend-id", "backend"); } static struct device_attribute xenbus_dev_attrs[] = { __ATTR_NULL }; /* Bus type for frontend drivers. */ static struct xen_bus_type xenbus_frontend = { .root = "device", .levels = 2, /* device/type/ */ .get_bus_id = frontend_bus_id, .probe = xenbus_probe_frontend, .bus = { .name = "xen", .match = xenbus_match, .uevent = xenbus_uevent, .probe = xenbus_dev_probe, .remove = xenbus_dev_remove, .shutdown = xenbus_dev_shutdown, .dev_attrs = xenbus_dev_attrs, .suspend = xenbus_dev_suspend, .resume = xenbus_dev_resume, }, }; static void otherend_changed(struct xenbus_watch *watch, const char **vec, unsigned int len) { struct xenbus_device *dev = container_of(watch, struct xenbus_device, otherend_watch); struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver); enum xenbus_state state; /* Protect us against watches firing on old details when the otherend details change, say immediately after a resume. */ if (!dev->otherend || strncmp(dev->otherend, vec[XS_WATCH_PATH], strlen(dev->otherend))) { dev_dbg(&dev->dev, "Ignoring watch at %s\n", vec[XS_WATCH_PATH]); return; } state = xenbus_read_driver_state(dev->otherend); dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n", state, xenbus_strstate(state), dev->otherend_watch.node, vec[XS_WATCH_PATH]); /* * Ignore xenbus transitions during shutdown. This prevents us doing * work that can fail e.g., when the rootfs is gone. */ if (system_state > SYSTEM_RUNNING) { struct xen_bus_type *bus = bus; bus = container_of(dev->dev.bus, struct xen_bus_type, bus); /* If we're frontend, drive the state machine to Closed. */ /* This should cause the backend to release our resources. */ if ((bus == &xenbus_frontend) && (state == XenbusStateClosing)) xenbus_frontend_closed(dev); return; } if (drv->otherend_changed) drv->otherend_changed(dev, state); } static int talk_to_otherend(struct xenbus_device *dev) { struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver); free_otherend_watch(dev); free_otherend_details(dev); return drv->read_otherend_details(dev); } static int watch_otherend(struct xenbus_device *dev) { return xenbus_watch_pathfmt(dev, &dev->otherend_watch, otherend_changed, "%s/%s", dev->otherend, "state"); } int xenbus_dev_probe(struct device *_dev) { struct xenbus_device *dev = to_xenbus_device(_dev); struct xenbus_driver *drv = to_xenbus_driver(_dev->driver); const struct xenbus_device_id *id; int err; DPRINTK("%s", dev->nodename); if (!drv->probe) { err = -ENODEV; goto fail; } id = match_device(drv->ids, dev); if (!id) { err = -ENODEV; goto fail; } err = talk_to_otherend(dev); if (err) { dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n", dev->nodename); return err; } err = drv->probe(dev, id); if (err) goto fail; err = watch_otherend(dev); if (err) { dev_warn(&dev->dev, "watch_otherend on %s failed.\n", dev->nodename); return err; } return 0; fail: xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename); xenbus_switch_state(dev, XenbusStateClosed); return -ENODEV; } int xenbus_dev_remove(struct device *_dev) { struct xenbus_device *dev = to_xenbus_device(_dev); struct xenbus_driver *drv = to_xenbus_driver(_dev->driver); DPRINTK("%s", dev->nodename); free_otherend_watch(dev); free_otherend_details(dev); if (drv->remove) drv->remove(dev); xenbus_switch_state(dev, XenbusStateClosed); return 0; } static void xenbus_dev_shutdown(struct device *_dev) { struct xenbus_device *dev = to_xenbus_device(_dev); unsigned long timeout = 5*HZ; DPRINTK("%s", dev->nodename); get_device(&dev->dev); if (dev->state != XenbusStateConnected) { printk(KERN_INFO "%s: %s: %s != Connected, skipping\n", __func__, dev->nodename, xenbus_strstate(dev->state)); goto out; } xenbus_switch_state(dev, XenbusStateClosing); timeout = wait_for_completion_timeout(&dev->down, timeout); if (!timeout) printk(KERN_INFO "%s: %s timeout closing device\n", __func__, dev->nodename); out: put_device(&dev->dev); } int xenbus_register_driver_common(struct xenbus_driver *drv, struct xen_bus_type *bus, struct module *owner, const char *mod_name) { drv->driver.name = drv->name; drv->driver.bus = &bus->bus; drv->driver.owner = owner; drv->driver.mod_name = mod_name; return driver_register(&drv->driver); } int __xenbus_register_frontend(struct xenbus_driver *drv, struct module *owner, const char *mod_name) { int ret; drv->read_otherend_details = read_backend_details; ret = xenbus_register_driver_common(drv, &xenbus_frontend, owner, mod_name); if (ret) return ret; /* If this driver is loaded as a module wait for devices to attach. */ wait_for_devices(drv); return 0; } EXPORT_SYMBOL_GPL(__xenbus_register_frontend); void xenbus_unregister_driver(struct xenbus_driver *drv) { driver_unregister(&drv->driver); } EXPORT_SYMBOL_GPL(xenbus_unregister_driver); struct xb_find_info { struct xenbus_device *dev; const char *nodename; }; static int cmp_dev(struct device *dev, void *data) { struct xenbus_device *xendev = to_xenbus_device(dev); struct xb_find_info *info = data; if (!strcmp(xendev->nodename, info->nodename)) { info->dev = xendev; get_device(dev); return 1; } return 0; } struct xenbus_device *xenbus_device_find(const char *nodename, struct bus_type *bus) { struct xb_find_info info = { .dev = NULL, .nodename = nodename }; bus_for_each_dev(bus, NULL, &info, cmp_dev); return info.dev; } static int cleanup_dev(struct device *dev, void *data) { struct xenbus_device *xendev = to_xenbus_device(dev); struct xb_find_info *info = data; int len = strlen(info->nodename); DPRINTK("%s", info->nodename); /* Match the info->nodename path, or any subdirectory of that path. */ if (strncmp(xendev->nodename, info->nodename, len)) return 0; /* If the node name is longer, ensure it really is a subdirectory. */ if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/')) return 0; info->dev = xendev; get_device(dev); return 1; } static void xenbus_cleanup_devices(const char *path, struct bus_type *bus) { struct xb_find_info info = { .nodename = path }; do { info.dev = NULL; bus_for_each_dev(bus, NULL, &info, cleanup_dev); if (info.dev) { device_unregister(&info.dev->dev); put_device(&info.dev->dev); } } while (info.dev); } static void xenbus_dev_release(struct device *dev) { if (dev) kfree(to_xenbus_device(dev)); } static ssize_t xendev_show_nodename(struct device *dev, struct device_attribute *attr, char *buf) { return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename); } static DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL); static ssize_t xendev_show_devtype(struct device *dev, struct device_attribute *attr, char *buf) { return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype); } static DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL); static ssize_t xendev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf) { return sprintf(buf, "xen:%s\n", to_xenbus_device(dev)->devicetype); } static DEVICE_ATTR(modalias, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_modalias, NULL); int xenbus_probe_node(struct xen_bus_type *bus, const char *type, const char *nodename) { char devname[XEN_BUS_ID_SIZE]; int err; struct xenbus_device *xendev; size_t stringlen; char *tmpstring; enum xenbus_state state = xenbus_read_driver_state(nodename); if (state != XenbusStateInitialising) { /* Device is not new, so ignore it. This can happen if a device is going away after switching to Closed. */ return 0; } stringlen = strlen(nodename) + 1 + strlen(type) + 1; xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL); if (!xendev) return -ENOMEM; xendev->state = XenbusStateInitialising; /* Copy the strings into the extra space. */ tmpstring = (char *)(xendev + 1); strcpy(tmpstring, nodename); xendev->nodename = tmpstring; tmpstring += strlen(tmpstring) + 1; strcpy(tmpstring, type); xendev->devicetype = tmpstring; init_completion(&xendev->down); xendev->dev.bus = &bus->bus; xendev->dev.release = xenbus_dev_release; err = bus->get_bus_id(devname, xendev->nodename); if (err) goto fail; dev_set_name(&xendev->dev, devname); /* Register with generic device framework. */ err = device_register(&xendev->dev); if (err) goto fail; err = device_create_file(&xendev->dev, &dev_attr_nodename); if (err) goto fail_unregister; err = device_create_file(&xendev->dev, &dev_attr_devtype); if (err) goto fail_remove_nodename; err = device_create_file(&xendev->dev, &dev_attr_modalias); if (err) goto fail_remove_devtype; return 0; fail_remove_devtype: device_remove_file(&xendev->dev, &dev_attr_devtype); fail_remove_nodename: device_remove_file(&xendev->dev, &dev_attr_nodename); fail_unregister: device_unregister(&xendev->dev); fail: kfree(xendev); return err; } /* device// */ static int xenbus_probe_frontend(const char *type, const char *name) { char *nodename; int err; nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", xenbus_frontend.root, type, name); if (!nodename) return -ENOMEM; DPRINTK("%s", nodename); err = xenbus_probe_node(&xenbus_frontend, type, nodename); kfree(nodename); return err; } static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type) { int err = 0; char **dir; unsigned int dir_n = 0; int i; dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n); if (IS_ERR(dir)) return PTR_ERR(dir); for (i = 0; i < dir_n; i++) { err = bus->probe(type, dir[i]); if (err) break; } kfree(dir); return err; } int xenbus_probe_devices(struct xen_bus_type *bus) { int err = 0; char **dir; unsigned int i, dir_n; dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n); if (IS_ERR(dir)) return PTR_ERR(dir); for (i = 0; i < dir_n; i++) { err = xenbus_probe_device_type(bus, dir[i]); if (err) break; } kfree(dir); return err; } static unsigned int char_count(const char *str, char c) { unsigned int/* * Driver for ST M41T94 SPI RTC * * Copyright (C) 2008 Kim B. Heino * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */ #include <linux/module.h> #include <linux/kernel.h> #include <linux/platform_device.h> #include <linux/rtc.h> #include <linux/spi/spi.h> #include <linux/bcd.h> #define M41T94_REG_SECONDS 0x01 #define M41T94_REG_MINUTES 0x02 #define M41T94_REG_HOURS 0x03 #define M41T94_REG_WDAY 0x04 #define M41T94_REG_DAY 0x05 #define M41T94_REG_MONTH 0x06 #define M41T94_REG_YEAR 0x07 #define M41T94_REG_HT 0x0c #define M41T94_BIT_HALT 0x40 #define M41T94_BIT_STOP 0x80 #define M41T94_BIT_CB 0x40 #define M41T94_BIT_CEB 0x80 static int m41t94_set_time(struct device *dev, struct rtc_time *tm) { struct spi_device *spi = to_spi_device(dev); u8 buf[8]; /* write cmd + 7 registers */ dev_dbg(dev, "%s secs=%d, mins=%d, " "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", "write", tm->tm_sec, tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); buf[0] = 0x80 | M41T94_REG_SECONDS; /* write time + date */ buf[M41T94_REG_SECONDS] = bin2bcd(tm->tm_sec); buf[M41T94_REG_MINUTES] = bin2bcd(tm->tm_min); buf[M41T94_REG_HOURS] = bin2bcd(tm->tm_hour); buf[M41T94_REG_WDAY] = bin2bcd(tm->tm_wday + 1); buf[M41T94_REG_DAY] = bin2bcd(tm->tm_mday); buf[M41T94_REG_MONTH] = bin2bcd(tm->tm_mon + 1); buf[M41T94_REG_HOURS] |= M41T94_BIT_CEB; if (tm->tm_year >= 100) buf[M41T94_REG_HOURS] |= M41T94_BIT_CB; buf[M41T94_REG_YEAR] = bin2bcd(tm->tm_year % 100); return spi_write(spi, buf, 8); } static int m41t94_read_time(struct device *dev, struct rtc_time *tm) { struct spi_device *spi = to_spi_device(dev); u8 buf[2]; int ret, hour; /* clear halt update bit */ ret = spi_w8r8(spi, M41T94_REG_HT); if (ret < 0) return ret; if (ret & M41T94_BIT_HALT) { buf[0] = 0x80 | M41T94_REG_HT; buf[1] = ret & ~M41T94_BIT_HALT; spi_write(spi, buf, 2); } /* clear stop bit */ ret = spi_w8r8(spi, M41T94_REG_SECONDS); if (ret < 0)