Skip to content

Commit 46abc02

Browse files
Lennert Buytenhekdavem330
authored andcommitted
phylib: give mdio buses a device tree presence
Introduce the mdio_bus class, and give each 'struct mii_bus' its own 'struct device', so that mii_bus objects are represented in the device tree and can be found by querying the device tree. Signed-off-by: Lennert Buytenhek <[email protected]> Acked-by: Andy Fleming <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 298cf9b commit 46abc02

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

drivers/net/phy/mdio_bus.c

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,34 @@
4343
*/
4444
struct mii_bus *mdiobus_alloc(void)
4545
{
46-
return kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
46+
struct mii_bus *bus;
47+
48+
bus = kzalloc(sizeof(*bus), GFP_KERNEL);
49+
if (bus != NULL)
50+
bus->state = MDIOBUS_ALLOCATED;
51+
52+
return bus;
4753
}
4854
EXPORT_SYMBOL(mdiobus_alloc);
4955

56+
/**
57+
* mdiobus_release - mii_bus device release callback
58+
*
59+
* Description: called when the last reference to an mii_bus is
60+
* dropped, to free the underlying memory.
61+
*/
62+
static void mdiobus_release(struct device *d)
63+
{
64+
struct mii_bus *bus = to_mii_bus(d);
65+
BUG_ON(bus->state != MDIOBUS_RELEASED);
66+
kfree(bus);
67+
}
68+
69+
static struct class mdio_bus_class = {
70+
.name = "mdio_bus",
71+
.dev_release = mdiobus_release,
72+
};
73+
5074
/**
5175
* mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
5276
* @bus: target mii_bus
@@ -66,6 +90,22 @@ int mdiobus_register(struct mii_bus *bus)
6690
NULL == bus->write)
6791
return -EINVAL;
6892

93+
BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
94+
bus->state != MDIOBUS_UNREGISTERED);
95+
96+
bus->dev.parent = bus->parent;
97+
bus->dev.class = &mdio_bus_class;
98+
bus->dev.groups = NULL;
99+
memcpy(bus->dev.bus_id, bus->id, MII_BUS_ID_SIZE);
100+
101+
err = device_register(&bus->dev);
102+
if (err) {
103+
printk(KERN_ERR "mii_bus %s failed to register\n", bus->id);
104+
return -EINVAL;
105+
}
106+
107+
bus->state = MDIOBUS_REGISTERED;
108+
69109
mutex_init(&bus->mdio_lock);
70110

71111
if (bus->reset)
@@ -92,6 +132,10 @@ void mdiobus_unregister(struct mii_bus *bus)
92132
{
93133
int i;
94134

135+
BUG_ON(bus->state != MDIOBUS_REGISTERED);
136+
bus->state = MDIOBUS_UNREGISTERED;
137+
138+
device_unregister(&bus->dev);
95139
for (i = 0; i < PHY_MAX_ADDR; i++) {
96140
if (bus->phy_map[i])
97141
device_unregister(&bus->phy_map[i]->dev);
@@ -103,11 +147,24 @@ EXPORT_SYMBOL(mdiobus_unregister);
103147
* mdiobus_free - free a struct mii_bus
104148
* @bus: mii_bus to free
105149
*
106-
* This function frees the mii_bus.
150+
* This function releases the reference to the underlying device
151+
* object in the mii_bus. If this is the last reference, the mii_bus
152+
* will be freed.
107153
*/
108154
void mdiobus_free(struct mii_bus *bus)
109155
{
110-
kfree(bus);
156+
/*
157+
* For compatibility with error handling in drivers.
158+
*/
159+
if (bus->state == MDIOBUS_ALLOCATED) {
160+
kfree(bus);
161+
return;
162+
}
163+
164+
BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
165+
bus->state = MDIOBUS_RELEASED;
166+
167+
put_device(&bus->dev);
111168
}
112169
EXPORT_SYMBOL(mdiobus_free);
113170

@@ -205,10 +262,20 @@ EXPORT_SYMBOL(mdio_bus_type);
205262

206263
int __init mdio_bus_init(void)
207264
{
208-
return bus_register(&mdio_bus_type);
265+
int ret;
266+
267+
ret = class_register(&mdio_bus_class);
268+
if (!ret) {
269+
ret = bus_register(&mdio_bus_type);
270+
if (ret)
271+
class_unregister(&mdio_bus_class);
272+
}
273+
274+
return ret;
209275
}
210276

211277
void mdio_bus_exit(void)
212278
{
279+
class_unregister(&mdio_bus_class);
213280
bus_unregister(&mdio_bus_type);
214281
}

include/linux/phy.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ struct mii_bus {
100100
struct mutex mdio_lock;
101101

102102
struct device *parent;
103+
enum {
104+
MDIOBUS_ALLOCATED = 1,
105+
MDIOBUS_REGISTERED,
106+
MDIOBUS_UNREGISTERED,
107+
MDIOBUS_RELEASED,
108+
} state;
109+
struct device dev;
103110

104111
/* list of all PHYs on bus */
105112
struct phy_device *phy_map[PHY_MAX_ADDR];
@@ -113,6 +120,7 @@ struct mii_bus {
113120
*/
114121
int *irq;
115122
};
123+
#define to_mii_bus(d) container_of(d, struct mii_bus, dev)
116124

117125
#define PHY_INTERRUPT_DISABLED 0x0
118126
#define PHY_INTERRUPT_ENABLED 0x80000000

0 commit comments

Comments
 (0)