bnx2x: fixed ethtool statistics for MF modes
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / uwb / umc-bus.c
blob82a84d53120f27f2a1734fcef5ab5774d5fbc0a3
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/module.h>
12 #include <linux/uwb/umc.h>
13 #include <linux/pci.h>
15 static int umc_bus_pre_reset_helper(struct device *dev, void *data)
17 int ret = 0;
19 if (dev->driver) {
20 struct umc_dev *umc = to_umc_dev(dev);
21 struct umc_driver *umc_drv = to_umc_driver(dev->driver);
23 if (umc_drv->pre_reset)
24 ret = umc_drv->pre_reset(umc);
25 else
26 device_release_driver(dev);
28 return ret;
31 static int umc_bus_post_reset_helper(struct device *dev, void *data)
33 int ret = 0;
35 if (dev->driver) {
36 struct umc_dev *umc = to_umc_dev(dev);
37 struct umc_driver *umc_drv = to_umc_driver(dev->driver);
39 if (umc_drv->post_reset)
40 ret = umc_drv->post_reset(umc);
41 } else
42 ret = device_attach(dev);
44 return ret;
47 /**
48 * umc_controller_reset - reset the whole UMC controller
49 * @umc: the UMC device for the radio controller.
51 * Drivers or all capabilities of the controller will have their
52 * pre_reset methods called or be unbound from their device. Then all
53 * post_reset methods will be called or the drivers will be rebound.
55 * Radio controllers must provide pre_reset and post_reset methods and
56 * reset the hardware in their start method.
58 * If this is called while a probe() or remove() is in progress it
59 * will return -EAGAIN and not perform the reset.
61 int umc_controller_reset(struct umc_dev *umc)
63 struct device *parent = umc->dev.parent;
64 int ret = 0;
66 if (device_trylock(parent))
67 return -EAGAIN;
68 ret = device_for_each_child(parent, parent, umc_bus_pre_reset_helper);
69 if (ret >= 0)
70 ret = device_for_each_child(parent, parent, umc_bus_post_reset_helper);
71 device_unlock(parent);
73 return ret;
75 EXPORT_SYMBOL_GPL(umc_controller_reset);
77 /**
78 * umc_match_pci_id - match a UMC driver to a UMC device's parent PCI device.
79 * @umc_drv: umc driver with match_data pointing to a zero-terminated
80 * table of pci_device_id's.
81 * @umc: umc device whose parent is to be matched.
83 int umc_match_pci_id(struct umc_driver *umc_drv, struct umc_dev *umc)
85 const struct pci_device_id *id_table = umc_drv->match_data;
86 struct pci_dev *pci;
88 if (umc->dev.parent->bus != &pci_bus_type)
89 return 0;
91 pci = to_pci_dev(umc->dev.parent);
92 return pci_match_id(id_table, pci) != NULL;
94 EXPORT_SYMBOL_GPL(umc_match_pci_id);
96 static int umc_bus_rescan_helper(struct device *dev, void *data)
98 int ret = 0;
100 if (!dev->driver)
101 ret = device_attach(dev);
103 return ret;
106 static void umc_bus_rescan(struct device *parent)
108 int err;
111 * We can't use bus_rescan_devices() here as it deadlocks when
112 * it tries to retake the dev->parent semaphore.
114 err = device_for_each_child(parent, NULL, umc_bus_rescan_helper);
115 if (err < 0)
116 printk(KERN_WARNING "%s: rescan of bus failed: %d\n",
117 KBUILD_MODNAME, err);
120 static int umc_bus_match(struct device *dev, struct device_driver *drv)
122 struct umc_dev *umc = to_umc_dev(dev);
123 struct umc_driver *umc_driver = to_umc_driver(drv);
125 if (umc->cap_id == umc_driver->cap_id) {
126 if (umc_driver->match)
127 return umc_driver->match(umc_driver, umc);
128 else
129 return 1;
131 return 0;
134 static int umc_device_probe(struct device *dev)
136 struct umc_dev *umc;
137 struct umc_driver *umc_driver;
138 int err;
140 umc_driver = to_umc_driver(dev->driver);
141 umc = to_umc_dev(dev);
143 get_device(dev);
144 err = umc_driver->probe(umc);
145 if (err)
146 put_device(dev);
147 else
148 umc_bus_rescan(dev->parent);
150 return err;
153 static int umc_device_remove(struct device *dev)
155 struct umc_dev *umc;
156 struct umc_driver *umc_driver;
158 umc_driver = to_umc_driver(dev->driver);
159 umc = to_umc_dev(dev);
161 umc_driver->remove(umc);
162 put_device(dev);
163 return 0;
166 static int umc_device_suspend(struct device *dev, pm_message_t state)
168 struct umc_dev *umc;
169 struct umc_driver *umc_driver;
170 int err = 0;
172 umc = to_umc_dev(dev);
174 if (dev->driver) {
175 umc_driver = to_umc_driver(dev->driver);
176 if (umc_driver->suspend)
177 err = umc_driver->suspend(umc, state);
179 return err;
182 static int umc_device_resume(struct device *dev)
184 struct umc_dev *umc;
185 struct umc_driver *umc_driver;
186 int err = 0;
188 umc = to_umc_dev(dev);
190 if (dev->driver) {
191 umc_driver = to_umc_driver(dev->driver);
192 if (umc_driver->resume)
193 err = umc_driver->resume(umc);
195 return err;
198 static ssize_t capability_id_show(struct device *dev, struct device_attribute *attr, char *buf)
200 struct umc_dev *umc = to_umc_dev(dev);
202 return sprintf(buf, "0x%02x\n", umc->cap_id);
205 static ssize_t version_show(struct device *dev, struct device_attribute *attr, char *buf)
207 struct umc_dev *umc = to_umc_dev(dev);
209 return sprintf(buf, "0x%04x\n", umc->version);
212 static struct device_attribute umc_dev_attrs[] = {
213 __ATTR_RO(capability_id),
214 __ATTR_RO(version),
215 __ATTR_NULL,
218 struct bus_type umc_bus_type = {
219 .name = "umc",
220 .match = umc_bus_match,
221 .probe = umc_device_probe,
222 .remove = umc_device_remove,
223 .suspend = umc_device_suspend,
224 .resume = umc_device_resume,
225 .dev_attrs = umc_dev_attrs,
227 EXPORT_SYMBOL_GPL(umc_bus_type);
229 static int __init umc_bus_init(void)
231 return bus_register(&umc_bus_type);
233 module_init(umc_bus_init);
235 static void __exit umc_bus_exit(void)
237 bus_unregister(&umc_bus_type);
239 module_exit(umc_bus_exit);
241 MODULE_DESCRIPTION("UWB Multi-interface Controller capability bus");
242 MODULE_AUTHOR("Cambridge Silicon Radio Ltd.");
243 MODULE_LICENSE("GPL");