]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/net/phy/mdio_bus.c
d206691f15d89cc23c144e618da25c68ea380d02
2 * drivers/net/phy/mdio_bus.c
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>
29 #include <linux/module.h>
30 #include <linux/mii.h>
31 #include <linux/ethtool.h>
32 #include <linux/phy.h>
36 #include <asm/uaccess.h>
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 return kzalloc(sizeof(struct mii_bus
), GFP_KERNEL
);
48 EXPORT_SYMBOL(mdiobus_alloc
);
51 * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
52 * @bus: target mii_bus
54 * Description: Called by a bus driver to bring up all the PHYs
55 * on a given bus, and attach them to the bus.
57 * Returns 0 on success or < 0 on error.
59 int mdiobus_register(struct mii_bus
*bus
)
64 if (NULL
== bus
|| NULL
== bus
->name
||
69 mutex_init(&bus
->mdio_lock
);
74 for (i
= 0; i
< PHY_MAX_ADDR
; i
++) {
75 bus
->phy_map
[i
] = NULL
;
76 if ((bus
->phy_mask
& (1 << i
)) == 0) {
77 struct phy_device
*phydev
;
79 phydev
= mdiobus_scan(bus
, i
);
81 err
= PTR_ERR(phydev
);
85 pr_info("%s: probed\n", bus
->name
);
89 EXPORT_SYMBOL(mdiobus_register
);
91 void mdiobus_unregister(struct mii_bus
*bus
)
95 for (i
= 0; i
< PHY_MAX_ADDR
; i
++) {
97 device_unregister(&bus
->phy_map
[i
]->dev
);
100 EXPORT_SYMBOL(mdiobus_unregister
);
103 * mdiobus_free - free a struct mii_bus
104 * @bus: mii_bus to free
106 * This function frees the mii_bus.
108 void mdiobus_free(struct mii_bus
*bus
)
112 EXPORT_SYMBOL(mdiobus_free
);
114 struct phy_device
*mdiobus_scan(struct mii_bus
*bus
, int addr
)
116 struct phy_device
*phydev
;
119 phydev
= get_phy_device(bus
, addr
);
120 if (IS_ERR(phydev
) || phydev
== NULL
)
123 /* There's a PHY at this address
130 * And, we need to register it */
132 phydev
->irq
= bus
->irq
!= NULL
? bus
->irq
[addr
] : PHY_POLL
;
134 phydev
->dev
.parent
= bus
->parent
;
135 phydev
->dev
.bus
= &mdio_bus_type
;
136 snprintf(phydev
->dev
.bus_id
, BUS_ID_SIZE
, PHY_ID_FMT
, bus
->id
, addr
);
140 /* Run all of the fixups for this PHY */
141 phy_scan_fixups(phydev
);
143 err
= device_register(&phydev
->dev
);
145 printk(KERN_ERR
"phy %d failed to register\n", addr
);
146 phy_device_free(phydev
);
150 bus
->phy_map
[addr
] = phydev
;
154 EXPORT_SYMBOL(mdiobus_scan
);
157 * mdio_bus_match - determine if given PHY driver supports the given PHY device
158 * @dev: target PHY device
159 * @drv: given PHY driver
161 * Description: Given a PHY device, and a PHY driver, return 1 if
162 * the driver supports the device. Otherwise, return 0.
164 static int mdio_bus_match(struct device
*dev
, struct device_driver
*drv
)
166 struct phy_device
*phydev
= to_phy_device(dev
);
167 struct phy_driver
*phydrv
= to_phy_driver(drv
);
169 return ((phydrv
->phy_id
& phydrv
->phy_id_mask
) ==
170 (phydev
->phy_id
& phydrv
->phy_id_mask
));
173 /* Suspend and resume. Copied from platform_suspend and
176 static int mdio_bus_suspend(struct device
* dev
, pm_message_t state
)
179 struct device_driver
*drv
= dev
->driver
;
181 if (drv
&& drv
->suspend
)
182 ret
= drv
->suspend(dev
, state
);
187 static int mdio_bus_resume(struct device
* dev
)
190 struct device_driver
*drv
= dev
->driver
;
192 if (drv
&& drv
->resume
)
193 ret
= drv
->resume(dev
);
198 struct bus_type mdio_bus_type
= {
200 .match
= mdio_bus_match
,
201 .suspend
= mdio_bus_suspend
,
202 .resume
= mdio_bus_resume
,
204 EXPORT_SYMBOL(mdio_bus_type
);
206 int __init
mdio_bus_init(void)
208 return bus_register(&mdio_bus_type
);
211 void mdio_bus_exit(void)
213 bus_unregister(&mdio_bus_type
);