43
43
*/
44
44
struct mii_bus * mdiobus_alloc (void )
45
45
{
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 ;
47
53
}
48
54
EXPORT_SYMBOL (mdiobus_alloc );
49
55
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
+
50
74
/**
51
75
* mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
52
76
* @bus: target mii_bus
@@ -66,6 +90,22 @@ int mdiobus_register(struct mii_bus *bus)
66
90
NULL == bus -> write )
67
91
return - EINVAL ;
68
92
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
+
69
109
mutex_init (& bus -> mdio_lock );
70
110
71
111
if (bus -> reset )
@@ -92,6 +132,10 @@ void mdiobus_unregister(struct mii_bus *bus)
92
132
{
93
133
int i ;
94
134
135
+ BUG_ON (bus -> state != MDIOBUS_REGISTERED );
136
+ bus -> state = MDIOBUS_UNREGISTERED ;
137
+
138
+ device_unregister (& bus -> dev );
95
139
for (i = 0 ; i < PHY_MAX_ADDR ; i ++ ) {
96
140
if (bus -> phy_map [i ])
97
141
device_unregister (& bus -> phy_map [i ]-> dev );
@@ -103,11 +147,24 @@ EXPORT_SYMBOL(mdiobus_unregister);
103
147
* mdiobus_free - free a struct mii_bus
104
148
* @bus: mii_bus to free
105
149
*
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.
107
153
*/
108
154
void mdiobus_free (struct mii_bus * bus )
109
155
{
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 );
111
168
}
112
169
EXPORT_SYMBOL (mdiobus_free );
113
170
@@ -205,10 +262,20 @@ EXPORT_SYMBOL(mdio_bus_type);
205
262
206
263
int __init mdio_bus_init (void )
207
264
{
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 ;
209
275
}
210
276
211
277
void mdio_bus_exit (void )
212
278
{
279
+ class_unregister (& mdio_bus_class );
213
280
bus_unregister (& mdio_bus_type );
214
281
}
0 commit comments