linux/3.2: allow to set platform_data for phy devices
[openwrt/svn-archive/archive.git] / target / linux / generic / patches-3.2 / 710-phy-add-mdio_register_board_info.patch
1 --- a/drivers/net/phy/mdio_bus.c
2 +++ b/drivers/net/phy/mdio_bus.c
3 @@ -36,6 +36,44 @@
4 #include <asm/irq.h>
5 #include <asm/uaccess.h>
6
7 +struct mdio_board_entry {
8 + struct list_head list;
9 + struct mdio_board_info board_info;
10 +};
11 +
12 +static LIST_HEAD(mdio_board_list);
13 +static DEFINE_MUTEX(mdio_board_lock);
14 +
15 +/**
16 + * mdio_register_board_info - register PHY devices for a given board
17 + * @info: array of chip descriptors
18 + * @n: how many descriptors are provided
19 + * Context: can sleep
20 + *
21 + * The board info passed can safely be __initdata ... but be careful of
22 + * any embedded pointers (platform_data, etc), they're copied as-is.
23 + */
24 +int __init
25 +mdiobus_register_board_info(struct mdio_board_info const *info, unsigned n)
26 +{
27 + struct mdio_board_entry *be;
28 + int i;
29 +
30 + be = kzalloc(n * sizeof(*be), GFP_KERNEL);
31 + if (!be)
32 + return -ENOMEM;
33 +
34 + for (i = 0; i < n; i++, be++, info++) {
35 + memcpy(&be->board_info, info, sizeof(*info));
36 + mutex_lock(&mdio_board_lock);
37 + list_add_tail(&be->list, &mdio_board_list);
38 + mutex_unlock(&mdio_board_lock);
39 + }
40 +
41 + return 0;
42 +}
43 +
44 +
45 /**
46 * mdiobus_alloc - allocate a mii_bus structure
47 *
48 @@ -179,15 +217,31 @@ void mdiobus_free(struct mii_bus *bus)
49 }
50 EXPORT_SYMBOL(mdiobus_free);
51
52 +static void mdiobus_setup_phydev_from_boardinfo(struct mii_bus *bus,
53 + struct phy_device *phydev,
54 + struct mdio_board_info *bi)
55 +{
56 + if (strcmp(bus->id, bi->bus_id) ||
57 + bi->phy_addr != phydev->addr)
58 + return;
59 +
60 + phydev->dev.platform_data = (void *) bi->platform_data;
61 +}
62 +
63 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
64 {
65 struct phy_device *phydev;
66 + struct mdio_board_entry *be;
67 int err;
68
69 phydev = get_phy_device(bus, addr);
70 if (IS_ERR(phydev) || phydev == NULL)
71 return phydev;
72
73 + list_for_each_entry(be, &mdio_board_list, list)
74 + mdiobus_setup_phydev_from_boardinfo(bus, phydev,
75 + &be->board_info);
76 +
77 err = phy_device_register(phydev);
78 if (err) {
79 phy_device_free(phydev);
80 --- a/include/linux/phy.h
81 +++ b/include/linux/phy.h
82 @@ -394,8 +394,8 @@ struct phy_driver {
83 /* Determines the negotiated speed and duplex */
84 int (*read_status)(struct phy_device *phydev);
85
86 - /*
87 - * Update the value in phydev->link to reflect the
88 + /*
89 + * Update the value in phydev->link to reflect the
90 * current link value
91 */
92 int (*update_link)(struct phy_device *phydev);
93 @@ -538,4 +538,22 @@ int __init mdio_bus_init(void);
94 void mdio_bus_exit(void);
95
96 extern struct bus_type mdio_bus_type;
97 +
98 +struct mdio_board_info {
99 + const char *bus_id;
100 + int phy_addr;
101 +
102 + const void *platform_data;
103 +};
104 +
105 +#ifdef CONFIG_PHYLIB
106 +int mdiobus_register_board_info(struct mdio_board_info const *info, unsigned n);
107 +#else
108 +static inline int
109 +mdiobus_register_board_info(struct mdio_board_info const *info, unsigned n)
110 +{
111 + return 0;
112 +}
113 +#endif
114 +
115 #endif /* __PHY_H */