devpts: Must release s_umount on error
[linux/fpc-iii.git] / drivers / net / phy / mdio_bus.c
blob811a637695caf4270247cbace802bca469bc03e0
1 /*
2 * drivers/net/phy/mdio_bus.c
4 * MDIO Bus interface
6 * Author: Andy Fleming
8 * Copyright (c) 2004 Freescale Semiconductor, Inc.
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/unistd.h>
20 #include <linux/slab.h>
21 #include <linux/interrupt.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/skbuff.h>
27 #include <linux/spinlock.h>
28 #include <linux/mm.h>
29 #include <linux/module.h>
30 #include <linux/mii.h>
31 #include <linux/ethtool.h>
32 #include <linux/phy.h>
34 #include <asm/io.h>
35 #include <asm/irq.h>
36 #include <asm/uaccess.h>
38 /**
39 * mdiobus_alloc - allocate a mii_bus structure
41 * Description: called by a bus driver to allocate an mii_bus
42 * structure to fill in.
44 struct mii_bus *mdiobus_alloc(void)
46 struct mii_bus *bus;
48 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
49 if (bus != NULL)
50 bus->state = MDIOBUS_ALLOCATED;
52 return bus;
54 EXPORT_SYMBOL(mdiobus_alloc);
56 /**
57 * mdiobus_release - mii_bus device release callback
58 * @d: the target struct device that contains the mii_bus
60 * Description: called when the last reference to an mii_bus is
61 * dropped, to free the underlying memory.
63 static void mdiobus_release(struct device *d)
65 struct mii_bus *bus = to_mii_bus(d);
66 BUG_ON(bus->state != MDIOBUS_RELEASED &&
67 /* for compatibility with error handling in drivers */
68 bus->state != MDIOBUS_ALLOCATED);
69 kfree(bus);
72 static struct class mdio_bus_class = {
73 .name = "mdio_bus",
74 .dev_release = mdiobus_release,
77 /**
78 * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
79 * @bus: target mii_bus
81 * Description: Called by a bus driver to bring up all the PHYs
82 * on a given bus, and attach them to the bus.
84 * Returns 0 on success or < 0 on error.
86 int mdiobus_register(struct mii_bus *bus)
88 int i, err;
90 if (NULL == bus || NULL == bus->name ||
91 NULL == bus->read ||
92 NULL == bus->write)
93 return -EINVAL;
95 BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
96 bus->state != MDIOBUS_UNREGISTERED);
98 bus->dev.parent = bus->parent;
99 bus->dev.class = &mdio_bus_class;
100 bus->dev.groups = NULL;
101 dev_set_name(&bus->dev, bus->id);
103 err = device_register(&bus->dev);
104 if (err) {
105 printk(KERN_ERR "mii_bus %s failed to register\n", bus->id);
106 return -EINVAL;
109 mutex_init(&bus->mdio_lock);
111 if (bus->reset)
112 bus->reset(bus);
114 for (i = 0; i < PHY_MAX_ADDR; i++) {
115 bus->phy_map[i] = NULL;
116 if ((bus->phy_mask & (1 << i)) == 0) {
117 struct phy_device *phydev;
119 phydev = mdiobus_scan(bus, i);
120 if (IS_ERR(phydev)) {
121 err = PTR_ERR(phydev);
122 goto error;
127 bus->state = MDIOBUS_REGISTERED;
128 pr_info("%s: probed\n", bus->name);
129 return 0;
131 error:
132 while (--i >= 0) {
133 if (bus->phy_map[i])
134 device_unregister(&bus->phy_map[i]->dev);
136 device_del(&bus->dev);
137 return err;
139 EXPORT_SYMBOL(mdiobus_register);
141 void mdiobus_unregister(struct mii_bus *bus)
143 int i;
145 BUG_ON(bus->state != MDIOBUS_REGISTERED);
146 bus->state = MDIOBUS_UNREGISTERED;
148 device_del(&bus->dev);
149 for (i = 0; i < PHY_MAX_ADDR; i++) {
150 if (bus->phy_map[i])
151 device_unregister(&bus->phy_map[i]->dev);
154 EXPORT_SYMBOL(mdiobus_unregister);
157 * mdiobus_free - free a struct mii_bus
158 * @bus: mii_bus to free
160 * This function releases the reference to the underlying device
161 * object in the mii_bus. If this is the last reference, the mii_bus
162 * will be freed.
164 void mdiobus_free(struct mii_bus *bus)
167 * For compatibility with error handling in drivers.
169 if (bus->state == MDIOBUS_ALLOCATED) {
170 kfree(bus);
171 return;
174 BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
175 bus->state = MDIOBUS_RELEASED;
177 put_device(&bus->dev);
179 EXPORT_SYMBOL(mdiobus_free);
181 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
183 struct phy_device *phydev;
184 int err;
186 phydev = get_phy_device(bus, addr);
187 if (IS_ERR(phydev) || phydev == NULL)
188 return phydev;
190 /* There's a PHY at this address
191 * We need to set:
192 * 1) IRQ
193 * 2) bus_id
194 * 3) parent
195 * 4) bus
196 * 5) mii_bus
197 * And, we need to register it */
199 phydev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
201 phydev->dev.parent = bus->parent;
202 phydev->dev.bus = &mdio_bus_type;
203 dev_set_name(&phydev->dev, PHY_ID_FMT, bus->id, addr);
205 phydev->bus = bus;
207 /* Run all of the fixups for this PHY */
208 phy_scan_fixups(phydev);
210 err = device_register(&phydev->dev);
211 if (err) {
212 printk(KERN_ERR "phy %d failed to register\n", addr);
213 phy_device_free(phydev);
214 phydev = NULL;
217 bus->phy_map[addr] = phydev;
219 return phydev;
221 EXPORT_SYMBOL(mdiobus_scan);
224 * mdiobus_read - Convenience function for reading a given MII mgmt register
225 * @bus: the mii_bus struct
226 * @addr: the phy address
227 * @regnum: register number to read
229 * NOTE: MUST NOT be called from interrupt context,
230 * because the bus read/write functions may wait for an interrupt
231 * to conclude the operation.
233 int mdiobus_read(struct mii_bus *bus, int addr, u16 regnum)
235 int retval;
237 BUG_ON(in_interrupt());
239 mutex_lock(&bus->mdio_lock);
240 retval = bus->read(bus, addr, regnum);
241 mutex_unlock(&bus->mdio_lock);
243 return retval;
245 EXPORT_SYMBOL(mdiobus_read);
248 * mdiobus_write - Convenience function for writing a given MII mgmt register
249 * @bus: the mii_bus struct
250 * @addr: the phy address
251 * @regnum: register number to write
252 * @val: value to write to @regnum
254 * NOTE: MUST NOT be called from interrupt context,
255 * because the bus read/write functions may wait for an interrupt
256 * to conclude the operation.
258 int mdiobus_write(struct mii_bus *bus, int addr, u16 regnum, u16 val)
260 int err;
262 BUG_ON(in_interrupt());
264 mutex_lock(&bus->mdio_lock);
265 err = bus->write(bus, addr, regnum, val);
266 mutex_unlock(&bus->mdio_lock);
268 return err;
270 EXPORT_SYMBOL(mdiobus_write);
273 * mdio_bus_match - determine if given PHY driver supports the given PHY device
274 * @dev: target PHY device
275 * @drv: given PHY driver
277 * Description: Given a PHY device, and a PHY driver, return 1 if
278 * the driver supports the device. Otherwise, return 0.
280 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
282 struct phy_device *phydev = to_phy_device(dev);
283 struct phy_driver *phydrv = to_phy_driver(drv);
285 return ((phydrv->phy_id & phydrv->phy_id_mask) ==
286 (phydev->phy_id & phydrv->phy_id_mask));
289 /* Suspend and resume. Copied from platform_suspend and
290 * platform_resume
292 static int mdio_bus_suspend(struct device * dev, pm_message_t state)
294 int ret = 0;
295 struct device_driver *drv = dev->driver;
296 struct phy_driver *phydrv = to_phy_driver(drv);
297 struct phy_device *phydev = to_phy_device(dev);
299 if (drv && phydrv->suspend && !device_may_wakeup(phydev->dev.parent))
300 ret = phydrv->suspend(phydev);
302 return ret;
305 static int mdio_bus_resume(struct device * dev)
307 int ret = 0;
308 struct device_driver *drv = dev->driver;
309 struct phy_driver *phydrv = to_phy_driver(drv);
310 struct phy_device *phydev = to_phy_device(dev);
312 if (drv && phydrv->resume && !device_may_wakeup(phydev->dev.parent))
313 ret = phydrv->resume(phydev);
315 return ret;
318 struct bus_type mdio_bus_type = {
319 .name = "mdio_bus",
320 .match = mdio_bus_match,
321 .suspend = mdio_bus_suspend,
322 .resume = mdio_bus_resume,
324 EXPORT_SYMBOL(mdio_bus_type);
326 int __init mdio_bus_init(void)
328 int ret;
330 ret = class_register(&mdio_bus_class);
331 if (!ret) {
332 ret = bus_register(&mdio_bus_type);
333 if (ret)
334 class_unregister(&mdio_bus_class);
337 return ret;
340 void mdio_bus_exit(void)
342 class_unregister(&mdio_bus_class);
343 bus_unregister(&mdio_bus_type);