switch: ROBO Switch Gigabit MII Support
[openwrt.git] / package / switch / src / switch-robo.c
1 /*
2  * Broadcom BCM5325E/536x switch configuration module
3  *
4  * Copyright (C) 2005 Felix Fietkau <nbd@nbd.name>
5  * Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>
6  * Based on 'robocfg' by Oleg I. Vdovikin
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
21  * 02110-1301, USA.
22  */
23
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/if.h>
27 #include <linux/if_arp.h>
28 #include <linux/sockios.h>
29 #include <linux/ethtool.h>
30 #include <linux/mii.h>
31 #include <linux/delay.h>
32 #include <asm/uaccess.h>
33
34 #include "switch-core.h"
35 #include "etc53xx.h"
36
37 #ifdef CONFIG_BCM47XX
38 #include <bcm47xx_nvram.h>
39 #endif
40
41 #define DRIVER_NAME             "bcm53xx"
42 #define DRIVER_VERSION          "0.02"
43 #define PFX                     "roboswitch: "
44
45 #define ROBO_PHY_ADDR           0x1E    /* robo switch phy address */
46 #define ROBO_PHY_ADDR_TG3       0x01    /* Tigon3 PHY address */
47 #define ROBO_PHY_ADDR_BCM63XX   0x00    /* BCM63XX PHY address */
48
49 /* MII registers */
50 #define REG_MII_PAGE    0x10    /* MII Page register */
51 #define REG_MII_ADDR    0x11    /* MII Address register */
52 #define REG_MII_DATA0   0x18    /* MII Data register 0 */
53
54 #define REG_MII_PAGE_ENABLE     1
55 #define REG_MII_ADDR_WRITE      1
56 #define REG_MII_ADDR_READ       2
57
58 /* Robo device ID register (in ROBO_MGMT_PAGE) */
59 #define ROBO_DEVICE_ID          0x30
60 #define  ROBO_DEVICE_ID_5325    0x25 /* Faked */
61 #define  ROBO_DEVICE_ID_5395    0x95
62 #define  ROBO_DEVICE_ID_5397    0x97
63 #define  ROBO_DEVICE_ID_5398    0x98
64 #define  ROBO_DEVICE_ID_53115   0x3115
65
66 /* Private et.o ioctls */
67 #define SIOCGETCPHYRD           (SIOCDEVPRIVATE + 9)
68 #define SIOCSETCPHYWR           (SIOCDEVPRIVATE + 10)
69
70 /* Data structure for a Roboswitch device. */
71 struct robo_switch {
72         char *device;                   /* The device name string (ethX) */
73         u16 devid;                      /* ROBO_DEVICE_ID_53xx */
74         bool is_5350;
75         u8 gmii;                        /* gigabit mii */
76         struct ifreq ifr;
77         struct net_device *dev;
78         unsigned char port[6];
79 };
80
81 /* Currently we can only have one device in the system. */
82 static struct robo_switch robo;
83
84
85 static int do_ioctl(int cmd)
86 {
87         mm_segment_t old_fs = get_fs();
88         int ret;
89
90         set_fs(KERNEL_DS);
91         ret = robo.dev->netdev_ops->ndo_do_ioctl(robo.dev, &robo.ifr, cmd);
92         set_fs(old_fs);
93
94         return ret;
95 }
96
97 static u16 mdio_read(__u16 phy_id, __u8 reg)
98 {
99         struct mii_ioctl_data *mii = if_mii(&robo.ifr);
100         int err;
101
102         mii->phy_id = phy_id;
103         mii->reg_num = reg;
104
105         err = do_ioctl(SIOCGMIIREG);
106         if (err < 0) {
107                 printk(KERN_ERR PFX "failed to read mdio reg %i with err %i.\n", reg, err);
108
109                 return 0xffff;
110         }
111
112         return mii->val_out;
113 }
114
115 static void mdio_write(__u16 phy_id, __u8 reg, __u16 val)
116 {
117         struct mii_ioctl_data *mii = if_mii(&robo.ifr);
118         int err;
119
120         mii->phy_id = phy_id;
121         mii->reg_num = reg;
122         mii->val_in = val;
123
124         err = do_ioctl(SIOCSMIIREG);
125         if (err < 0) {
126                 printk(KERN_ERR PFX "failed to write mdio reg: %i with err %i.\n", reg, err);
127                 return;
128         }
129 }
130
131 static int robo_reg(__u8 page, __u8 reg, __u8 op)
132 {
133         int i = 3;
134
135         /* set page number */
136         mdio_write(ROBO_PHY_ADDR, REG_MII_PAGE,
137                 (page << 8) | REG_MII_PAGE_ENABLE);
138
139         /* set register address */
140         mdio_write(ROBO_PHY_ADDR, REG_MII_ADDR,
141                 (reg << 8) | op);
142
143         /* check if operation completed */
144         while (i--) {
145                 if ((mdio_read(ROBO_PHY_ADDR, REG_MII_ADDR) & 3) == 0)
146                         return 0;
147         }
148
149         printk(KERN_ERR PFX "timeout in robo_reg on page %i and reg %i with op %i.\n", page, reg, op);
150
151         return 1;
152 }
153
154 /*
155 static void robo_read(__u8 page, __u8 reg, __u16 *val, int count)
156 {
157         int i;
158
159         robo_reg(page, reg, REG_MII_ADDR_READ);
160
161         for (i = 0; i < count; i++)
162                 val[i] = mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + i);
163 }
164 */
165
166 static __u16 robo_read16(__u8 page, __u8 reg)
167 {
168         robo_reg(page, reg, REG_MII_ADDR_READ);
169
170         return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0);
171 }
172
173 static __u32 robo_read32(__u8 page, __u8 reg)
174 {
175         robo_reg(page, reg, REG_MII_ADDR_READ);
176
177         return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0) |
178                 (mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + 1) << 16);
179 }
180
181 static void robo_write16(__u8 page, __u8 reg, __u16 val16)
182 {
183         /* write data */
184         mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val16);
185
186         robo_reg(page, reg, REG_MII_ADDR_WRITE);
187 }
188
189 static void robo_write32(__u8 page, __u8 reg, __u32 val32)
190 {
191         /* write data */
192         mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val32 & 0xFFFF);
193         mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0 + 1, val32 >> 16);
194
195         robo_reg(page, reg, REG_MII_ADDR_WRITE);
196 }
197
198 /* checks that attached switch is 5325/5352/5354/5356/5357/53115 */
199 static int robo_vlan5350(__u32 phyid)
200 {
201         /* set vlan access id to 15 and read it back */
202         __u16 val16 = 15;
203         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
204
205         /* 5365 will refuse this as it does not have this reg */
206         if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350) != val16)
207                 return 0;
208         /* gigabit ? */
209         if (mdio_read(0, ROBO_MII_STAT) & 0x0100)
210                 robo.gmii = ((mdio_read(0, 0x0f) & 0xf000) != 0);
211         /* 53115 ? */
212         if (robo.gmii && robo_read32(ROBO_STAT_PAGE, ROBO_LSA_IM_PORT) != 0) {
213                 robo_write16(ROBO_ARLIO_PAGE, ROBO_VTBL_INDX_5395, val16);
214                 robo_write16(ROBO_ARLIO_PAGE, ROBO_VTBL_ACCESS_5395,
215                                          (1 << 7) /* start */ | 1 /* read */);
216                 if (robo_read16(ROBO_ARLIO_PAGE, ROBO_VTBL_ACCESS_5395) == 1 &&
217                     robo_read16(ROBO_ARLIO_PAGE, ROBO_VTBL_INDX_5395) == val16)
218                         return 4;
219         }
220         /* dirty trick for 5356/5357 */
221         if ((phyid & 0xfff0ffff ) == 0x5da00362 ||
222             (phyid & 0xfff0ffff ) == 0x5e000362)
223                 return 3;
224         /* 5325/5352/5354*/
225         return 1;
226 }
227
228 static int robo_switch_enable(void)
229 {
230         unsigned int i, last_port;
231         u16 val;
232 #ifdef CONFIG_BCM47XX
233         char buf[20];
234 #endif
235
236         val = robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE);
237         if (!(val & (1 << 1))) {
238                 /* Unmanaged mode */
239                 val &= ~(1 << 0);
240                 /* With forwarding */
241                 val |= (1 << 1);
242                 robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, val);
243                 val = robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE);
244                 if (!(val & (1 << 1))) {
245                         printk(KERN_ERR PFX "Failed to enable switch\n");
246                         return -EBUSY;
247                 }
248
249                 last_port = (robo.devid == ROBO_DEVICE_ID_5398) ?
250                                 ROBO_PORT6_CTRL : ROBO_PORT3_CTRL;
251                 for (i = ROBO_PORT0_CTRL; i < last_port + 1; i++)
252                         robo_write16(ROBO_CTRL_PAGE, i, 0);
253         }
254
255 #ifdef CONFIG_BCM47XX
256         /* WAN port LED, except for Netgear WGT634U */
257         if (bcm47xx_nvram_getenv("nvram_type", buf, sizeof(buf)) >= 0) {
258                 if (strcmp(buf, "cfe") != 0)
259                         robo_write16(ROBO_CTRL_PAGE, 0x16, 0x1F);
260         }
261 #endif
262         return 0;
263 }
264
265 static void robo_switch_reset(void)
266 {
267         if ((robo.devid == ROBO_DEVICE_ID_5395) ||
268             (robo.devid == ROBO_DEVICE_ID_5397) ||
269             (robo.devid == ROBO_DEVICE_ID_5398)) {
270                 /* Trigger a software reset. */
271                 robo_write16(ROBO_CTRL_PAGE, 0x79, 0x83);
272                 mdelay(500);
273                 robo_write16(ROBO_CTRL_PAGE, 0x79, 0);
274         }
275 }
276
277 static int robo_probe(char *devname)
278 {
279         __u32 phyid;
280         unsigned int i;
281         int err = 1;
282         struct mii_ioctl_data *mii;
283
284         printk(KERN_INFO PFX "Probing device '%s'\n", devname);
285         strcpy(robo.ifr.ifr_name, devname);
286
287         if ((robo.dev = dev_get_by_name(&init_net, devname)) == NULL) {
288                 printk(KERN_ERR PFX "No such device\n");
289                 return 1;
290         }
291         if (!robo.dev->netdev_ops || !robo.dev->netdev_ops->ndo_do_ioctl) {
292                 printk(KERN_ERR PFX "ndo_do_ioctl not implemented in ethernet driver\n");
293                 return 1;
294         }
295
296         robo.device = devname;
297         for (i = 0; i < 5; i++)
298                 robo.port[i] = i;
299         robo.port[5] = 8;
300
301         /* try access using MII ioctls - get phy address */
302         err = do_ioctl(SIOCGMIIPHY);
303         if (err < 0) {
304                 printk(KERN_ERR PFX "error (%i) while accessing MII phy registers with ioctls\n", err);
305                 goto done;
306         }
307
308         /* got phy address check for robo address */
309         mii = if_mii(&robo.ifr);
310         if ((mii->phy_id != ROBO_PHY_ADDR) &&
311             (mii->phy_id != ROBO_PHY_ADDR_BCM63XX) &&
312             (mii->phy_id != ROBO_PHY_ADDR_TG3)) {
313                 printk(KERN_ERR PFX "Invalid phy address (%d)\n", mii->phy_id);
314                 goto done;
315         }
316
317         phyid = mdio_read(ROBO_PHY_ADDR, 0x2) | 
318                 (mdio_read(ROBO_PHY_ADDR, 0x3) << 16);
319
320         if (phyid == 0xffffffff || phyid == 0x55210022) {
321                 printk(KERN_ERR PFX "No Robo switch in managed mode found, phy_id = 0x%08x\n", phyid);
322                 goto done;
323         }
324
325         /* Get the device ID */
326         for (i = 0; i < 10; i++) {
327                 robo.devid = robo_read16(ROBO_MGMT_PAGE, ROBO_DEVICE_ID);
328                 if (robo.devid)
329                         break;
330                 udelay(10);
331         }
332         if (!robo.devid)
333                 robo.devid = ROBO_DEVICE_ID_5325; /* Fake it */
334         robo.is_5350 = robo_vlan5350(phyid);
335
336         robo_switch_reset();
337         err = robo_switch_enable();
338         if (err)
339                 goto done;
340         err = 0;
341
342         printk(KERN_INFO PFX "found a 5%s%x!%s at %s\n", robo.devid & 0xff00 ? "" : "3", robo.devid,
343                 robo.is_5350 ? " It's a 5350." : "", devname);
344
345 done:
346         if (err) {
347                 dev_put(robo.dev);
348                 robo.dev = NULL;
349         }
350         return err;
351 }
352
353
354 static int handle_vlan_port_read(void *driver, char *buf, int nr)
355 {
356         __u16 val16;
357         int len = 0;
358         int j;
359
360         val16 = (nr) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
361
362         if (robo.is_5350) {
363                 u32 val32;
364                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
365                 /* actual read */
366                 val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
367                 if ((val32 & (1 << 20)) /* valid */) {
368                         for (j = 0; j < 6; j++) {
369                                 if (val32 & (1 << j)) {
370                                         len += sprintf(buf + len, "%d", j);
371                                         if (val32 & (1 << (j + 6))) {
372                                                 if (j == 5) buf[len++] = 'u';
373                                         } else {
374                                                 buf[len++] = 't';
375                                                 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
376                                                         buf[len++] = '*';
377                                         }
378                                         buf[len++] = '\t';
379                                 }
380                         }
381                         len += sprintf(buf + len, "\n");
382                 }
383         } else {
384                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
385                 /* actual read */
386                 val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
387                 if ((val16 & (1 << 14)) /* valid */) {
388                         for (j = 0; j < 6; j++) {
389                                 if (val16 & (1 << j)) {
390                                         len += sprintf(buf + len, "%d", j);
391                                         if (val16 & (1 << (j + 7))) {
392                                                 if (j == 5) buf[len++] = 'u';
393                                         } else {
394                                                 buf[len++] = 't';
395                                                 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
396                                                         buf[len++] = '*';
397                                         }
398                                         buf[len++] = '\t';
399                                 }
400                         }
401                         len += sprintf(buf + len, "\n");
402                 }
403         }
404
405         buf[len] = '\0';
406
407         return len;
408 }
409
410 static int handle_vlan_port_write(void *driver, char *buf, int nr)
411 {
412         switch_driver *d = (switch_driver *) driver;
413         switch_vlan_config *c = switch_parse_vlan(d, buf);
414         int j;
415         __u16 val16;
416
417         if (c == NULL)
418                 return -EINVAL;
419
420         for (j = 0; j < d->ports; j++) {
421                 if ((c->untag | c->pvid) & (1 << j))
422                         /* change default vlan tag */
423                         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), nr);
424         }
425
426         /* write config now */
427
428         if (robo.devid != ROBO_DEVICE_ID_5325) {
429                 __u8 regoff = ((robo.devid == ROBO_DEVICE_ID_5395) ||
430                         (robo.devid == ROBO_DEVICE_ID_53115)) ? 0x20 : 0;
431
432                 robo_write32(ROBO_ARLIO_PAGE, 0x63 + regoff, (c->untag << 9) | c->port);
433                 robo_write16(ROBO_ARLIO_PAGE, 0x61 + regoff, nr);
434                 robo_write16(ROBO_ARLIO_PAGE, 0x60 + regoff, 1 << 7);
435                 kfree(c);
436                 return 0;
437         }
438
439         val16 = (nr) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
440         if (robo.is_5350) {
441                 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350,
442                         (1 << 20) /* valid */ | (c->untag << 6) | c->port);
443                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
444         } else {
445                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE,
446                         (1 << 14)  /* valid */ | (c->untag << 7) | c->port);
447                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
448         }
449
450         kfree(c);
451         return 0;
452 }
453
454 #define set_switch(state) \
455         robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, (robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & ~2) | (state ? 2 : 0));
456
457 static int handle_enable_read(void *driver, char *buf, int nr)
458 {
459         return sprintf(buf, "%d\n", (((robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & 2) == 2) ? 1 : 0));
460 }
461
462 static int handle_enable_write(void *driver, char *buf, int nr)
463 {
464         set_switch(buf[0] == '1');
465
466         return 0;
467 }
468
469 static int handle_port_enable_read(void *driver, char *buf, int nr)
470 {
471         return sprintf(buf, "%d\n", ((robo_read16(ROBO_CTRL_PAGE, robo.port[nr]) & 3) == 3 ? 0 : 1));
472 }
473
474 static int handle_port_enable_write(void *driver, char *buf, int nr)
475 {
476         u16 val16;
477
478         if (buf[0] == '0')
479                 val16 = 3; /* disabled */
480         else if (buf[0] == '1')
481                 val16 = 0; /* enabled */
482         else
483                 return -EINVAL;
484
485         robo_write16(ROBO_CTRL_PAGE, robo.port[nr],
486                 (robo_read16(ROBO_CTRL_PAGE, robo.port[nr]) & ~3) | val16);
487
488         return 0;
489 }
490
491 static int handle_port_media_read(void *driver, char *buf, int nr)
492 {
493         u16 bmcr = mdio_read(robo.port[nr], MII_BMCR);
494         int media, len;
495
496         if (bmcr & BMCR_ANENABLE)
497                 media = SWITCH_MEDIA_AUTO;
498         else {
499                 if (bmcr & BMCR_SPEED1000)
500                         media = SWITCH_MEDIA_1000;
501                 else if (bmcr & BMCR_SPEED100)
502                         media = SWITCH_MEDIA_100;
503                 else
504                         media = 0;
505
506                 if (bmcr & BMCR_FULLDPLX)
507                         media |= SWITCH_MEDIA_FD;
508         }
509
510         len = switch_print_media(buf, media);
511         return len + sprintf(buf + len, "\n");
512 }
513
514 static int handle_port_media_write(void *driver, char *buf, int nr)
515 {
516         int media = switch_parse_media(buf);
517         u16 bmcr, bmcr_mask;
518
519         if (media & SWITCH_MEDIA_AUTO)
520                 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
521         else {
522                 if (media & SWITCH_MEDIA_1000) {
523                         if (!robo.gmii)
524                                 return -EINVAL;
525                         bmcr = BMCR_SPEED1000;
526                 }
527                 else if (media & SWITCH_MEDIA_100)
528                         bmcr = BMCR_SPEED100;
529                 else
530                         bmcr = 0;
531
532                 if (media & SWITCH_MEDIA_FD)
533                         bmcr |= BMCR_FULLDPLX;
534         }
535
536         bmcr_mask = ~(BMCR_SPEED1000 | BMCR_SPEED100 | BMCR_FULLDPLX | BMCR_ANENABLE | BMCR_ANRESTART);
537         mdio_write(robo.port[nr], MII_BMCR, 
538                 (mdio_read(robo.port[nr], MII_BMCR) & bmcr_mask) | bmcr);
539
540         return 0;
541 }
542
543 static int handle_enable_vlan_read(void *driver, char *buf, int nr)
544 {
545         return sprintf(buf, "%d\n", (((robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0) & (1 << 7)) == (1 << 7)) ? 1 : 0));
546 }
547
548 static int handle_enable_vlan_write(void *driver, char *buf, int nr)
549 {
550         int disable = ((buf[0] != '1') ? 1 : 0);
551
552         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0, disable ? 0 :
553                 (1 << 7) /* 802.1Q VLAN */ | (3 << 5) /* mac check and hash */);
554         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL1, disable ? 0 :
555                 (robo.devid == ROBO_DEVICE_ID_5325 ? (1 << 1) :
556                 0) | (1 << 2) | (1 << 3)); /* RSV multicast */
557
558         if (robo.devid != ROBO_DEVICE_ID_5325)
559                 return 0;
560
561         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL4, disable ? 0 :
562                 (1 << 6) /* drop invalid VID frames */);
563         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL5, disable ? 0 :
564                 (1 << 3) /* drop miss V table frames */);
565
566         return 0;
567 }
568
569 static int handle_reset(void *driver, char *buf, int nr)
570 {
571         switch_driver *d = (switch_driver *) driver;
572         int j;
573         __u16 val16;
574
575         /* disable switching */
576         set_switch(0);
577
578         /* reset vlans */
579         for (j = 0; j <= ((robo.is_5350) ? VLAN_ID_MAX5350 : VLAN_ID_MAX); j++) {
580                 /* write config now */
581                 val16 = (j) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
582                 if (robo.is_5350)
583                         robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350, 0);
584                 else
585                         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE, 0);
586                 robo_write16(ROBO_VLAN_PAGE, robo.is_5350 ? ROBO_VLAN_TABLE_ACCESS_5350 :
587                                                             ROBO_VLAN_TABLE_ACCESS,
588                              val16);
589         }
590
591         /* reset ports to a known good state */
592         for (j = 0; j < d->ports; j++) {
593                 robo_write16(ROBO_CTRL_PAGE, robo.port[j], 0x0000);
594                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), 0);
595         }
596
597         /* enable switching */
598         set_switch(1);
599
600         /* enable vlans */
601         handle_enable_vlan_write(driver, "1", 0);
602
603         return 0;
604 }
605
606 static int __init robo_init(void)
607 {
608         int notfound = 1;
609         char *device;
610
611         device = strdup("ethX");
612         for (device[3] = '0'; (device[3] <= '3') && notfound; device[3]++) {
613                 if (! switch_device_registered (device))
614                         notfound = robo_probe(device);
615         }
616         device[3]--;
617
618         if (notfound) {
619                 kfree(device);
620                 return -ENODEV;
621         } else {
622                 static const switch_config cfg[] = {
623                         {
624                                 .name   = "enable",
625                                 .read   = handle_enable_read,
626                                 .write  = handle_enable_write
627                         }, {
628                                 .name   = "enable_vlan",
629                                 .read   = handle_enable_vlan_read,
630                                 .write  = handle_enable_vlan_write
631                         }, {
632                                 .name   = "reset",
633                                 .read   = NULL,
634                                 .write  = handle_reset
635                         }, { NULL, },
636                 };
637                 static const switch_config port[] = {
638                         {
639                                 .name   = "enable",
640                                 .read   = handle_port_enable_read,
641                                 .write  = handle_port_enable_write
642                         }, {
643                                 .name   = "media",
644                                 .read   = handle_port_media_read,
645                                 .write  = handle_port_media_write
646                         }, { NULL, },
647                 };
648                 static const switch_config vlan[] = {
649                         {
650                                 .name   = "ports",
651                                 .read   = handle_vlan_port_read,
652                                 .write  = handle_vlan_port_write
653                         }, { NULL, },
654                 };
655                 switch_driver driver = {
656                         .name                   = DRIVER_NAME,
657                         .version                = DRIVER_VERSION,
658                         .interface              = device,
659                         .cpuport                = 5,
660                         .ports                  = 6,
661                         .vlans                  = 16,
662                         .driver_handlers        = cfg,
663                         .port_handlers          = port,
664                         .vlan_handlers          = vlan,
665                 };
666                 if (robo.devid != ROBO_DEVICE_ID_5325) {
667                         driver.ports = 9;
668                         driver.cpuport = 8;
669                 }
670
671                 return switch_register_driver(&driver);
672         }
673 }
674
675 static void __exit robo_exit(void)
676 {
677         switch_unregister_driver(DRIVER_NAME);
678         if (robo.dev)
679                 dev_put(robo.dev);
680         kfree(robo.device);
681 }
682
683
684 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
685 MODULE_LICENSE("GPL");
686
687 module_init(robo_init);
688 module_exit(robo_exit);