uwb: add the umc bus
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / uwb / umc-bus.c
blob2d8d62d9f53e5d2a11624df6bcbc79247d195811
1 /*
2 * Bus for UWB Multi-interface Controller capabilities.
4 * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
6 * This file is released under the GNU GPL v2.
7 */
8 #include <linux/kernel.h>
9 #include <linux/sysfs.h>
10 #include <linux/workqueue.h>
11 #include <linux/uwb/umc.h>
12 #include <linux/pci.h>
14 static int umc_bus_unbind_helper(struct device *dev, void *data)
16 struct device *parent = data;
18 if (dev->parent == parent && dev->driver)
19 device_release_driver(dev);
20 return 0;
23 /**
24 * umc_controller_reset - reset the whole UMC controller
25 * @umc: the UMC device for the radio controller.
27 * Drivers will be unbound from all UMC devices belonging to the
28 * controller and then the radio controller will be rebound. The
29 * radio controller is expected to do a full hardware reset when it is
30 * probed.
32 * If this is called while a probe() or remove() is in progress it
33 * will return -EAGAIN and not perform the reset.
35 int umc_controller_reset(struct umc_dev *umc)
37 struct device *parent = umc->dev.parent;
38 int ret;
40 if (down_trylock(&parent->sem))
41 return -EAGAIN;
42 bus_for_each_dev(&umc_bus_type, NULL, parent, umc_bus_unbind_helper);
43 ret = device_attach(&umc->dev);
44 if (ret == 1)
45 ret = 0;
46 up(&parent->sem);
48 return ret;
50 EXPORT_SYMBOL_GPL(umc_controller_reset);
52 /**
53 * umc_match_pci_id - match a UMC driver to a UMC device's parent PCI device.
54 * @umc_drv: umc driver with match_data pointing to a zero-terminated
55 * table of pci_device_id's.
56 * @umc: umc device whose parent is to be matched.
58 int umc_match_pci_id(struct umc_driver *umc_drv, struct umc_dev *umc)
60 const struct pci_device_id *id_table = umc_drv->match_data;
61 struct pci_dev *pci;
63 if (umc->dev.parent->bus != &pci_bus_type)
64 return 0;
66 pci = to_pci_dev(umc->dev.parent);
67 return pci_match_id(id_table, pci) != NULL;
69 EXPORT_SYMBOL_GPL(umc_match_pci_id);
71 static int umc_bus_rescan_helper(struct device *dev, void *data)
73 int ret = 0;
75 if (!dev->driver)
76 ret = device_attach(dev);
78 return ret < 0 ? ret : 0;
81 static void umc_bus_rescan(void)
83 int err;
86 * We can't use bus_rescan_devices() here as it deadlocks when
87 * it tries to retake the dev->parent semaphore.
89 err = bus_for_each_dev(&umc_bus_type, NULL, NULL, umc_bus_rescan_helper);
90 if (err < 0)
91 printk(KERN_WARNING "%s: rescan of bus failed: %d\n",
92 KBUILD_MODNAME, err);
95 static int umc_bus_match(struct device *dev, struct device_driver *drv)
97 struct umc_dev *umc = to_umc_dev(dev);
98 struct umc_driver *umc_driver = to_umc_driver(drv);
100 if (umc->cap_id == umc_driver->cap_id) {
101 if (umc_driver->match)
102 return umc_driver->match(umc_driver, umc);
103 else
104 return 1;
106 return 0;
109 static int umc_device_probe(struct device *dev)
111 struct umc_dev *umc;
112 struct umc_driver *umc_driver;
113 int err;
115 umc_driver = to_umc_driver(dev->driver);
116 umc = to_umc_dev(dev);
118 get_device(dev);
119 err = umc_driver->probe(umc);
120 if (err)
121 put_device(dev);
122 else
123 umc_bus_rescan();
125 return err;
128 static int umc_device_remove(struct device *dev)
130 struct umc_dev *umc;
131 struct umc_driver *umc_driver;
133 umc_driver = to_umc_driver(dev->driver);
134 umc = to_umc_dev(dev);
136 umc_driver->remove(umc);
137 put_device(dev);
138 return 0;
141 static int umc_device_suspend(struct device *dev, pm_message_t state)
143 struct umc_dev *umc;
144 struct umc_driver *umc_driver;
145 int err = 0;
147 umc = to_umc_dev(dev);
149 if (dev->driver) {
150 umc_driver = to_umc_driver(dev->driver);
151 if (umc_driver->suspend)
152 err = umc_driver->suspend(umc, state);
154 return err;
157 static int umc_device_resume(struct device *dev)
159 struct umc_dev *umc;
160 struct umc_driver *umc_driver;
161 int err = 0;
163 umc = to_umc_dev(dev);
165 if (dev->driver) {
166 umc_driver = to_umc_driver(dev->driver);
167 if (umc_driver->resume)
168 err = umc_driver->resume(umc);
170 return err;
173 static ssize_t capability_id_show(struct device *dev, struct device_attribute *attr, char *buf)
175 struct umc_dev *umc = to_umc_dev(dev);
177 return sprintf(buf, "0x%02x\n", umc->cap_id);
180 static ssize_t version_show(struct device *dev, struct device_attribute *attr, char *buf)
182 struct umc_dev *umc = to_umc_dev(dev);
184 return sprintf(buf, "0x%04x\n", umc->version);
187 static struct device_attribute umc_dev_attrs[] = {
188 __ATTR_RO(capability_id),
189 __ATTR_RO(version),
190 __ATTR_NULL,
193 struct bus_type umc_bus_type = {
194 .name = "umc",
195 .match = umc_bus_match,
196 .probe = umc_device_probe,
197 .remove = umc_device_remove,
198 .suspend = umc_device_suspend,
199 .resume = umc_device_resume,
200 .dev_attrs = umc_dev_attrs,
202 EXPORT_SYMBOL_GPL(umc_bus_type);
204 static int __init umc_bus_init(void)
206 return bus_register(&umc_bus_type);
208 module_init(umc_bus_init);
210 static void __exit umc_bus_exit(void)
212 bus_unregister(&umc_bus_type);
214 module_exit(umc_bus_exit);
216 MODULE_DESCRIPTION("UWB Multi-interface Controller capability bus");
217 MODULE_AUTHOR("Cambridge Silicon Radio Ltd.");
218 MODULE_LICENSE("GPL");