OSDN Git Service

4394ddb69ce5f7f94e2f73603b323cd2e2fef42d
[android-x86/external-wpa_supplicant_6.git] / src / drivers / driver_roboswitch.c
1 /*
2  * WPA Supplicant - roboswitch driver interface
3  * Copyright (c) 2008-2009 Jouke Witteveen
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16 #include <sys/ioctl.h>
17 #include <linux/if.h>
18 #include <linux/sockios.h>
19 #include <linux/mii.h>
20
21 #include "common.h"
22 #include "driver.h"
23
24 #define ROBO_PHY_ADDR           0x1E    /* RoboSwitch PHY address */
25
26 /* MII access registers */
27 #define ROBO_MII_PAGE           0x10    /* MII page register */
28 #define ROBO_MII_ADDR           0x11    /* MII address register */
29 #define ROBO_MII_DATA_OFFSET    0x18    /* Start of MII data registers */
30
31 #define ROBO_MII_PAGE_ENABLE    0x01    /* MII page op code */
32 #define ROBO_MII_ADDR_WRITE     0x01    /* MII address write op code */
33 #define ROBO_MII_ADDR_READ      0x02    /* MII address read op code */
34 #define ROBO_MII_DATA_MAX          4    /* Consecutive MII data registers */
35 #define ROBO_MII_RETRY_MAX        10    /* Read attempts before giving up */
36
37 /* Page numbers */
38 #define ROBO_ARLCTRL_PAGE       0x04    /* ARL control page */
39 #define ROBO_VLAN_PAGE          0x34    /* VLAN page */
40
41 /* ARL control page registers */
42 #define ROBO_ARLCTRL_CONF       0x00    /* ARL configuration register */
43 #define ROBO_ARLCTRL_ADDR_1     0x10    /* Multiport address 1 */
44 #define ROBO_ARLCTRL_VEC_1      0x16    /* Multiport vector 1 */
45 #define ROBO_ARLCTRL_ADDR_2     0x20    /* Multiport address 2 */
46 #define ROBO_ARLCTRL_VEC_2      0x26    /* Multiport vector 2 */
47
48 /* VLAN page registers */
49 #define ROBO_VLAN_ACCESS        0x06    /* VLAN table Access register */
50 #define ROBO_VLAN_ACCESS_5365   0x08    /* VLAN table Access register (5365) */
51 #define ROBO_VLAN_READ          0x0C    /* VLAN read register */
52 #define ROBO_VLAN_MAX           0xFF    /* Maximum number of VLANs */
53
54
55 static const u8 pae_group_addr[ETH_ALEN] =
56 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
57
58
59 struct wpa_driver_roboswitch_data {
60         void *ctx;
61         char ifname[IFNAMSIZ + 1];
62         struct ifreq ifr;
63         int fd;
64         u16 ports;
65 };
66
67
68 /* Copied from the kernel-only part of mii.h. */
69 static inline struct mii_ioctl_data *if_mii(struct ifreq *rq)
70 {
71         return (struct mii_ioctl_data *) &rq->ifr_ifru;
72 }
73
74
75 static u16 wpa_driver_roboswitch_mdio_read(
76         struct wpa_driver_roboswitch_data *drv, u8 reg)
77 {
78         struct mii_ioctl_data *mii = if_mii(&drv->ifr);
79
80         mii->phy_id = ROBO_PHY_ADDR;
81         mii->reg_num = reg;
82
83         if (ioctl(drv->fd, SIOCGMIIREG, &drv->ifr) < 0) {
84                 perror("ioctl[SIOCGMIIREG]");
85                 return 0x00;
86         }
87         return mii->val_out;
88 }
89
90
91 static void wpa_driver_roboswitch_mdio_write(
92         struct wpa_driver_roboswitch_data *drv, u8 reg, u16 val)
93 {
94         struct mii_ioctl_data *mii = if_mii(&drv->ifr);
95
96         mii->phy_id = ROBO_PHY_ADDR;
97         mii->reg_num = reg;
98         mii->val_in = val;
99
100         if (ioctl(drv->fd, SIOCSMIIREG, &drv->ifr) < 0) {
101                 perror("ioctl[SIOCSMIIREG");
102         }
103 }
104
105
106 static int wpa_driver_roboswitch_reg(struct wpa_driver_roboswitch_data *drv,
107                                      u8 page, u8 reg, u8 op)
108 {
109         int i;
110
111         /* set page number */
112         wpa_driver_roboswitch_mdio_write(drv, ROBO_MII_PAGE,
113                                          (page << 8) | ROBO_MII_PAGE_ENABLE);
114         /* set register address */
115         wpa_driver_roboswitch_mdio_write(drv, ROBO_MII_ADDR, (reg << 8) | op);
116
117         /* check if operation completed */
118         for (i = 0; i < ROBO_MII_RETRY_MAX; ++i) {
119                 if ((wpa_driver_roboswitch_mdio_read(drv, ROBO_MII_ADDR) & 3)
120                     == 0)
121                         return 0;
122         }
123         /* timeout */
124         return -1;
125 }
126
127
128 static int wpa_driver_roboswitch_read(struct wpa_driver_roboswitch_data *drv,
129                                       u8 page, u8 reg, u16 *val, int len)
130 {
131         int i;
132
133         if (len > ROBO_MII_DATA_MAX ||
134             wpa_driver_roboswitch_reg(drv, page, reg, ROBO_MII_ADDR_READ) < 0)
135                 return -1;
136
137         for (i = 0; i < len; ++i) {
138                 val[i] = wpa_driver_roboswitch_mdio_read(
139                         drv, ROBO_MII_DATA_OFFSET + i);
140         }
141
142         return 0;
143 }
144
145
146 static int wpa_driver_roboswitch_write(struct wpa_driver_roboswitch_data *drv,
147                                        u8 page, u8 reg, u16 *val, int len)
148 {
149         int i;
150
151         if (len > ROBO_MII_DATA_MAX) return -1;
152         for (i = 0; i < len; ++i) {
153                 wpa_driver_roboswitch_mdio_write(drv, ROBO_MII_DATA_OFFSET + i,
154                                                  val[i]);
155         }
156         return wpa_driver_roboswitch_reg(drv, page, reg, ROBO_MII_ADDR_WRITE);
157 }
158
159
160 static int wpa_driver_roboswitch_get_ssid(void *priv, u8 *ssid)
161 {
162         ssid[0] = 0;
163         return 0;
164 }
165
166
167 static int wpa_driver_roboswitch_get_bssid(void *priv, u8 *bssid)
168 {
169         /* Report PAE group address as the "BSSID" for wired connection. */
170         os_memcpy(bssid, pae_group_addr, ETH_ALEN);
171         return 0;
172 }
173
174
175 static const char * wpa_driver_roboswitch_get_ifname(void *priv)
176 {
177         struct wpa_driver_roboswitch_data *drv = priv;
178         return drv->ifname;
179 }
180
181
182 static int wpa_driver_roboswitch_join(struct wpa_driver_roboswitch_data *drv,
183                                       const u8 *addr)
184 {
185         int i;
186         u16 _read, zero = 0;
187         /* For reasons of simplicity we assume ETH_ALEN is even. */
188         u16 addr_word[ETH_ALEN / 2];
189         /* RoboSwitch uses 16-bit Big Endian addresses.                 */
190         /* The ordering of the words is reversed in the MII registers.  */
191         for (i = 0; i < ETH_ALEN; i += 2)
192                 addr_word[(ETH_ALEN - i) / 2 - 1] = WPA_GET_BE16(addr + i);
193
194         /* check if multiport addresses are not yet enabled */
195         if (wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
196                                        ROBO_ARLCTRL_CONF, &_read, 1) < 0)
197                 return -1;
198
199         if (!(_read & (1 << 4))) {
200                 _read |= 1 << 4;
201                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
202                                             ROBO_ARLCTRL_ADDR_1, addr_word, 3);
203                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
204                                             ROBO_ARLCTRL_VEC_1, &drv->ports,
205                                             1);
206                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
207                                             ROBO_ARLCTRL_VEC_2, &zero, 1);
208                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
209                                             ROBO_ARLCTRL_CONF, &_read, 1);
210                 return 0;
211         }
212
213         /* check if multiport address 1 is free */
214         wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE, ROBO_ARLCTRL_VEC_1,
215                                    &_read, 1);
216         if (_read == 0) {
217                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
218                                             ROBO_ARLCTRL_ADDR_1, addr_word, 3);
219                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
220                                             ROBO_ARLCTRL_VEC_1, &drv->ports,
221                                             1);
222                 return 0;
223         }
224         /* check if multiport address 2 is free */
225         wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE, ROBO_ARLCTRL_VEC_2,
226                                    &_read, 1);
227         if (_read == 0) {
228                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
229                                             ROBO_ARLCTRL_ADDR_2, addr_word, 3);
230                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
231                                             ROBO_ARLCTRL_VEC_2, &drv->ports,
232                                             1);
233                 return 0;
234         }
235
236         /* out of free multiport addresses */
237         return -1;
238 }
239
240
241 static int wpa_driver_roboswitch_leave(struct wpa_driver_roboswitch_data *drv,
242                                        const u8 *addr)
243 {
244         int i;
245         u16 _read[3], zero = 0;
246         u16 addr_word[ETH_ALEN / 2]; /* same as at join */
247
248         for (i = 0; i < ETH_ALEN; i += 2)
249                 addr_word[(ETH_ALEN - i) / 2 - 1] = WPA_GET_BE16(addr + i);
250
251         /* check if multiport address 1 was used */
252         wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE, ROBO_ARLCTRL_VEC_1,
253                                    _read, 1);
254         if (_read[0] == drv->ports) {
255                 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
256                                            ROBO_ARLCTRL_ADDR_1, _read, 3);
257                 if (os_memcmp(_read, addr_word, 6) == 0) {
258                         wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
259                                                     ROBO_ARLCTRL_VEC_1, &zero,
260                                                     1);
261                         goto clean_up;
262                 }
263         }
264
265         /* check if multiport address 2 was used */
266         wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE, ROBO_ARLCTRL_VEC_2,
267                                    _read, 1);
268         if (_read[0] == drv->ports) {
269                 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
270                                            ROBO_ARLCTRL_ADDR_2, _read, 3);
271                 if (os_memcmp(_read, addr_word, 6) == 0) {
272                         wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
273                                                     ROBO_ARLCTRL_VEC_2, &zero,
274                                                     1);
275                         goto clean_up;
276                 }
277         }
278
279         /* used multiport address not found */
280         return -1;
281
282 clean_up:
283         /* leave the multiport registers in a sane state */
284         wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE, ROBO_ARLCTRL_VEC_1,
285                                    _read, 1);
286         if (_read[0] == 0) {
287                 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
288                                            ROBO_ARLCTRL_VEC_2, _read, 1);
289                 if (_read[0] == 0) {
290                         wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
291                                                    ROBO_ARLCTRL_CONF, _read,
292                                                    1);
293                         _read[0] &= ~(1 << 4);
294                         wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
295                                                     ROBO_ARLCTRL_CONF, _read,
296                                                     1);
297                 } else {
298                         wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
299                                                    ROBO_ARLCTRL_ADDR_2, _read,
300                                                    3);
301                         wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
302                                                     ROBO_ARLCTRL_ADDR_1, _read,
303                                                     3);
304                         wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
305                                                    ROBO_ARLCTRL_VEC_2, _read,
306                                                    1);
307                         wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
308                                                     ROBO_ARLCTRL_VEC_1, _read,
309                                                     1);
310                         wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
311                                                     ROBO_ARLCTRL_VEC_2, &zero,
312                                                     1);
313                 }
314         }
315         return 0;
316 }
317
318
319 static void * wpa_driver_roboswitch_init(void *ctx, const char *ifname)
320 {
321         struct wpa_driver_roboswitch_data *drv;
322         int len = -1, sep = -1;
323         u16 vlan_max = ROBO_VLAN_MAX, vlan = 0, vlan_read[2];
324
325         drv = os_zalloc(sizeof(*drv));
326         if (drv == NULL) return NULL;
327         drv->ctx = ctx;
328
329         while (ifname[++len]) {
330                 if (ifname[len] == '.')
331                         sep = len;
332         }
333         if (sep < 0 || sep >= len - 1) {
334                 wpa_printf(MSG_INFO, "%s: No <interface>.<vlan> pair in "
335                            "interface name %s", __func__, ifname);
336                 os_free(drv);
337                 return NULL;
338         }
339         if (sep > IFNAMSIZ) {
340                 wpa_printf(MSG_INFO, "%s: Interface name %s is too long",
341                            __func__, ifname);
342                 os_free(drv);
343                 return NULL;
344         }
345         os_memcpy(drv->ifname, ifname, sep);
346         drv->ifname[sep] = '\0';
347         while (++sep < len) {
348                 if (ifname[sep] < '0' || ifname[sep] > '9') {
349                         wpa_printf(MSG_INFO, "%s: Invalid vlan specification "
350                                    "in interface name %s", __func__, ifname);
351                         os_free(drv);
352                         return NULL;
353                 }
354                 vlan *= 10;
355                 vlan += ifname[sep] - '0';
356                 if (vlan > ROBO_VLAN_MAX) {
357                         wpa_printf(MSG_INFO, "%s: VLAN out of range in "
358                                    "interface name %s", __func__, ifname);
359                         os_free(drv);
360                         return NULL;
361                 }
362         }
363
364         drv->fd = socket(PF_INET, SOCK_DGRAM, 0);
365         if (drv->fd < 0) {
366                 wpa_printf(MSG_INFO, "%s: Unable to create socket", __func__);
367                 os_free(drv);
368                 return NULL;
369         }
370
371         os_memset(&drv->ifr, 0, sizeof(drv->ifr));
372         os_strlcpy(drv->ifr.ifr_name, drv->ifname, IFNAMSIZ);
373         if (ioctl(drv->fd, SIOCGMIIPHY, &drv->ifr) < 0) {
374                 perror("ioctl[SIOCGMIIPHY]");
375                 os_free(drv);
376                 return NULL;
377         }
378         if (if_mii(&drv->ifr)->phy_id != ROBO_PHY_ADDR) {
379                 wpa_printf(MSG_INFO, "%s: Invalid phy address (not a "
380                            "RoboSwitch?)", __func__);
381                 os_free(drv);
382                 return NULL;
383         }
384
385         /* set the read bit */
386         vlan |= 1 << 13;
387         /* set and read back to see if the register can be used */
388         wpa_driver_roboswitch_write(drv, ROBO_VLAN_PAGE, ROBO_VLAN_ACCESS,
389                                     &vlan_max, 1);
390         wpa_driver_roboswitch_read(drv, ROBO_VLAN_PAGE, ROBO_VLAN_ACCESS,
391                                    &vlan_max, 1);
392         if (vlan_max == ROBO_VLAN_MAX) /* pre-5365 */
393                 wpa_driver_roboswitch_write(drv, ROBO_VLAN_PAGE,
394                                             ROBO_VLAN_ACCESS, &vlan, 1);
395         else /* 5365 uses a different register */
396                 wpa_driver_roboswitch_write(drv, ROBO_VLAN_PAGE,
397                                             ROBO_VLAN_ACCESS_5365, &vlan, 1);
398         wpa_driver_roboswitch_read(drv, ROBO_VLAN_PAGE, ROBO_VLAN_READ,
399                                    vlan_read, 2);
400         if (!(vlan_read[1] & (1 << 4))) {
401                 wpa_printf(MSG_INFO, "%s: Could not get port information for "
402                                      "VLAN %d", __func__, vlan & ~(1 << 13));
403                 os_free(drv);
404                 return NULL;
405         }
406         drv->ports = vlan_read[0] & 0x001F;
407         /* add the MII port */
408         drv->ports |= 1 << 8;
409         if (wpa_driver_roboswitch_join(drv, pae_group_addr) < 0) {
410                 wpa_printf(MSG_INFO, "%s: Unable to join PAE group", __func__);
411                 os_free(drv);
412                 return NULL;
413         } else {
414                 wpa_printf(MSG_DEBUG, "%s: Added PAE group address to "
415                            "RoboSwitch ARL", __func__);
416         }
417
418         return drv;
419 }
420
421
422 static void wpa_driver_roboswitch_deinit(void *priv)
423 {
424         struct wpa_driver_roboswitch_data *drv = priv;
425
426         if (wpa_driver_roboswitch_leave(drv, pae_group_addr) < 0) {
427                 wpa_printf(MSG_DEBUG, "%s: Unable to leave PAE group",
428                            __func__);
429         }
430
431         close(drv->fd);
432         os_free(drv);
433 }
434
435
436 const struct wpa_driver_ops wpa_driver_roboswitch_ops = {
437         .name = "roboswitch",
438         .desc = "wpa_supplicant roboswitch driver",
439         .get_ssid = wpa_driver_roboswitch_get_ssid,
440         .get_bssid = wpa_driver_roboswitch_get_bssid,
441         .init = wpa_driver_roboswitch_init,
442         .deinit = wpa_driver_roboswitch_deinit,
443         .get_ifname = wpa_driver_roboswitch_get_ifname,
444 };