blob: c8571405146d394bfd8df9c20146df39ba1da74c [file] [log] [blame]
David Vrabelda389ea2008-09-17 16:34:12 +01001/*
2 * Bus for UWB Multi-interface Controller capabilities.
3 *
4 * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
5 *
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>
Paul Gortmakerc7394efc2011-07-17 16:40:36 -040011#include <linux/module.h>
David Vrabelda389ea2008-09-17 16:34:12 +010012#include <linux/uwb/umc.h>
13#include <linux/pci.h>
14
David Vrabel307ba6d2008-11-07 17:37:33 +000015static int umc_bus_pre_reset_helper(struct device *dev, void *data)
David Vrabelda389ea2008-09-17 16:34:12 +010016{
David Vrabel307ba6d2008-11-07 17:37:33 +000017 int ret = 0;
David Vrabelda389ea2008-09-17 16:34:12 +010018
David Vrabel307ba6d2008-11-07 17:37:33 +000019 if (dev->driver) {
20 struct umc_dev *umc = to_umc_dev(dev);
21 struct umc_driver *umc_drv = to_umc_driver(dev->driver);
22
23 if (umc_drv->pre_reset)
24 ret = umc_drv->pre_reset(umc);
25 else
26 device_release_driver(dev);
27 }
28 return ret;
29}
30
31static int umc_bus_post_reset_helper(struct device *dev, void *data)
32{
33 int ret = 0;
34
35 if (dev->driver) {
36 struct umc_dev *umc = to_umc_dev(dev);
37 struct umc_driver *umc_drv = to_umc_driver(dev->driver);
38
39 if (umc_drv->post_reset)
40 ret = umc_drv->post_reset(umc);
41 } else
42 ret = device_attach(dev);
43
44 return ret;
David Vrabelda389ea2008-09-17 16:34:12 +010045}
46
47/**
48 * umc_controller_reset - reset the whole UMC controller
49 * @umc: the UMC device for the radio controller.
50 *
David Vrabel307ba6d2008-11-07 17:37:33 +000051 * 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.
54 *
55 * Radio controllers must provide pre_reset and post_reset methods and
56 * reset the hardware in their start method.
David Vrabelda389ea2008-09-17 16:34:12 +010057 *
58 * If this is called while a probe() or remove() is in progress it
59 * will return -EAGAIN and not perform the reset.
60 */
61int umc_controller_reset(struct umc_dev *umc)
62{
63 struct device *parent = umc->dev.parent;
David Vrabel307ba6d2008-11-07 17:37:33 +000064 int ret = 0;
David Vrabelda389ea2008-09-17 16:34:12 +010065
Claudio Scordinob7177272012-10-09 12:21:17 +020066 if (!device_trylock(parent))
David Vrabelda389ea2008-09-17 16:34:12 +010067 return -EAGAIN;
David Vrabel307ba6d2008-11-07 17:37:33 +000068 ret = device_for_each_child(parent, parent, umc_bus_pre_reset_helper);
69 if (ret >= 0)
David Vrabel0396c212009-08-25 16:41:06 +010070 ret = device_for_each_child(parent, parent, umc_bus_post_reset_helper);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -080071 device_unlock(parent);
David Vrabelda389ea2008-09-17 16:34:12 +010072
73 return ret;
74}
75EXPORT_SYMBOL_GPL(umc_controller_reset);
76
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.
82 */
83int umc_match_pci_id(struct umc_driver *umc_drv, struct umc_dev *umc)
84{
85 const struct pci_device_id *id_table = umc_drv->match_data;
86 struct pci_dev *pci;
87
Yijing Wang0d4e5be2013-12-05 19:21:42 +080088 if (!dev_is_pci(umc->dev.parent))
David Vrabelda389ea2008-09-17 16:34:12 +010089 return 0;
90
91 pci = to_pci_dev(umc->dev.parent);
92 return pci_match_id(id_table, pci) != NULL;
93}
94EXPORT_SYMBOL_GPL(umc_match_pci_id);
95
96static int umc_bus_rescan_helper(struct device *dev, void *data)
97{
98 int ret = 0;
99
100 if (!dev->driver)
101 ret = device_attach(dev);
102
David Vrabel307ba6d2008-11-07 17:37:33 +0000103 return ret;
David Vrabelda389ea2008-09-17 16:34:12 +0100104}
105
David Vrabel307ba6d2008-11-07 17:37:33 +0000106static void umc_bus_rescan(struct device *parent)
David Vrabelda389ea2008-09-17 16:34:12 +0100107{
108 int err;
109
110 /*
111 * We can't use bus_rescan_devices() here as it deadlocks when
112 * it tries to retake the dev->parent semaphore.
113 */
David Vrabel307ba6d2008-11-07 17:37:33 +0000114 err = device_for_each_child(parent, NULL, umc_bus_rescan_helper);
David Vrabelda389ea2008-09-17 16:34:12 +0100115 if (err < 0)
116 printk(KERN_WARNING "%s: rescan of bus failed: %d\n",
117 KBUILD_MODNAME, err);
118}
119
120static int umc_bus_match(struct device *dev, struct device_driver *drv)
121{
122 struct umc_dev *umc = to_umc_dev(dev);
123 struct umc_driver *umc_driver = to_umc_driver(drv);
124
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;
130 }
131 return 0;
132}
133
134static int umc_device_probe(struct device *dev)
135{
136 struct umc_dev *umc;
137 struct umc_driver *umc_driver;
138 int err;
139
140 umc_driver = to_umc_driver(dev->driver);
141 umc = to_umc_dev(dev);
142
143 get_device(dev);
144 err = umc_driver->probe(umc);
145 if (err)
146 put_device(dev);
147 else
David Vrabel307ba6d2008-11-07 17:37:33 +0000148 umc_bus_rescan(dev->parent);
David Vrabelda389ea2008-09-17 16:34:12 +0100149
150 return err;
151}
152
153static int umc_device_remove(struct device *dev)
154{
155 struct umc_dev *umc;
156 struct umc_driver *umc_driver;
157
158 umc_driver = to_umc_driver(dev->driver);
159 umc = to_umc_dev(dev);
160
161 umc_driver->remove(umc);
162 put_device(dev);
163 return 0;
164}
165
David Vrabelda389ea2008-09-17 16:34:12 +0100166static ssize_t capability_id_show(struct device *dev, struct device_attribute *attr, char *buf)
167{
168 struct umc_dev *umc = to_umc_dev(dev);
169
170 return sprintf(buf, "0x%02x\n", umc->cap_id);
171}
Greg Kroah-Hartman1b604032013-10-06 23:55:44 -0700172static DEVICE_ATTR_RO(capability_id);
David Vrabelda389ea2008-09-17 16:34:12 +0100173
174static ssize_t version_show(struct device *dev, struct device_attribute *attr, char *buf)
175{
176 struct umc_dev *umc = to_umc_dev(dev);
177
178 return sprintf(buf, "0x%04x\n", umc->version);
179}
Greg Kroah-Hartman1b604032013-10-06 23:55:44 -0700180static DEVICE_ATTR_RO(version);
David Vrabelda389ea2008-09-17 16:34:12 +0100181
Greg Kroah-Hartman1b604032013-10-06 23:55:44 -0700182static struct attribute *umc_dev_attrs[] = {
183 &dev_attr_capability_id.attr,
184 &dev_attr_version.attr,
185 NULL,
David Vrabelda389ea2008-09-17 16:34:12 +0100186};
Greg Kroah-Hartman1b604032013-10-06 23:55:44 -0700187ATTRIBUTE_GROUPS(umc_dev);
David Vrabelda389ea2008-09-17 16:34:12 +0100188
189struct bus_type umc_bus_type = {
190 .name = "umc",
191 .match = umc_bus_match,
192 .probe = umc_device_probe,
193 .remove = umc_device_remove,
Greg Kroah-Hartman1b604032013-10-06 23:55:44 -0700194 .dev_groups = umc_dev_groups,
David Vrabelda389ea2008-09-17 16:34:12 +0100195};
196EXPORT_SYMBOL_GPL(umc_bus_type);
197
198static int __init umc_bus_init(void)
199{
200 return bus_register(&umc_bus_type);
201}
202module_init(umc_bus_init);
203
204static void __exit umc_bus_exit(void)
205{
206 bus_unregister(&umc_bus_type);
207}
208module_exit(umc_bus_exit);
209
210MODULE_DESCRIPTION("UWB Multi-interface Controller capability bus");
211MODULE_AUTHOR("Cambridge Silicon Radio Ltd.");
212MODULE_LICENSE("GPL");