1 /*- 2 * Copyright (c) 2011-2012 Stefan Bethke. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/bus.h> 31 #include <sys/conf.h> 32 #include <sys/fcntl.h> 33 #include <sys/kernel.h> 34 #include <sys/malloc.h> 35 #include <sys/module.h> 36 #include <sys/socket.h> 37 #include <sys/sx.h> 38 #include <sys/systm.h> 39 #include <sys/uio.h> 40 41 #include <net/if.h> 42 43 #include <dev/etherswitch/etherswitch.h> 44 45 #include "etherswitch_if.h" 46 47 struct etherswitch_softc { 48 device_t sc_dev; 49 struct cdev *sc_devnode; 50 }; 51 52 static int etherswitch_probe(device_t); 53 static int etherswitch_attach(device_t); 54 static int etherswitch_detach(device_t); 55 static void etherswitch_identify(driver_t *driver, device_t parent); 56 57 devclass_t etherswitch_devclass; 58 59 static device_method_t etherswitch_methods[] = { 60 /* device interface */ 61 DEVMETHOD(device_identify, etherswitch_identify), 62 DEVMETHOD(device_probe, etherswitch_probe), 63 DEVMETHOD(device_attach, etherswitch_attach), 64 DEVMETHOD(device_detach, etherswitch_detach), 65 66 DEVMETHOD_END 67 }; 68 69 driver_t etherswitch_driver = { 70 "etherswitch", 71 etherswitch_methods, 72 sizeof(struct etherswitch_softc), 73 }; 74 75 static d_ioctl_t etherswitchioctl; 76 77 static struct cdevsw etherswitch_cdevsw = { 78 .d_version = D_VERSION, 79 .d_flags = D_TRACKCLOSE, 80 .d_ioctl = etherswitchioctl, 81 .d_name = "etherswitch", 82 }; 83 84 static void 85 etherswitch_identify(driver_t *driver, device_t parent) 86 { 87 if (device_find_child(parent, "etherswitch", -1) == NULL) 88 BUS_ADD_CHILD(parent, 0, "etherswitch", -1); 89 } 90 91 static int 92 etherswitch_probe(device_t dev) 93 { 94 device_set_desc(dev, "Switch controller"); 95 96 return (0); 97 } 98 99 static int 100 etherswitch_attach(device_t dev) 101 { 102 struct etherswitch_softc *sc = (struct etherswitch_softc *)device_get_softc(dev); 103 104 sc->sc_dev = dev; 105 sc->sc_devnode = make_dev(ðerswitch_cdevsw, device_get_unit(dev), 106 UID_ROOT, GID_WHEEL, 107 0600, "etherswitch%d", device_get_unit(dev)); 108 if (sc->sc_devnode == NULL) { 109 device_printf(dev, "failed to create character device\n"); 110 return (ENXIO); 111 } 112 sc->sc_devnode->si_drv1 = sc; 113 114 return (0); 115 } 116 117 static int 118 etherswitch_detach(device_t dev) 119 { 120 struct etherswitch_softc *sc = (struct etherswitch_softc *)device_get_softc(dev); 121 122 if (sc->sc_devnode) 123 destroy_dev(sc->sc_devnode); 124 125 return (0); 126 } 127 128 static int 129 etherswitchioctl(struct cdev *cdev, u_long cmd, caddr_t data, int flags, struct thread *td) 130 { 131 struct etherswitch_softc *sc = cdev->si_drv1; 132 device_t dev = sc->sc_dev; 133 device_t etherswitch = device_get_parent(dev); 134 etherswitch_conf_t conf; 135 etherswitch_info_t *info; 136 etherswitch_reg_t *reg; 137 etherswitch_phyreg_t *phyreg; 138 int error = 0; 139 140 switch (cmd) { 141 case IOETHERSWITCHGETINFO: 142 info = ETHERSWITCH_GETINFO(etherswitch); 143 bcopy(info, data, sizeof(etherswitch_info_t)); 144 break; 145 146 case IOETHERSWITCHGETREG: 147 reg = (etherswitch_reg_t *)data; 148 ETHERSWITCH_LOCK(etherswitch); 149 reg->val = ETHERSWITCH_READREG(etherswitch, reg->reg); 150 ETHERSWITCH_UNLOCK(etherswitch); 151 break; 152 153 case IOETHERSWITCHSETREG: 154 reg = (etherswitch_reg_t *)data; 155 ETHERSWITCH_LOCK(etherswitch); 156 error = ETHERSWITCH_WRITEREG(etherswitch, reg->reg, reg->val); 157 ETHERSWITCH_UNLOCK(etherswitch); 158 break; 159 160 case IOETHERSWITCHGETPORT: 161 error = ETHERSWITCH_GETPORT(etherswitch, (etherswitch_port_t *)data); 162 break; 163 164 case IOETHERSWITCHSETPORT: 165 error = ETHERSWITCH_SETPORT(etherswitch, (etherswitch_port_t *)data); 166 break; 167 168 case IOETHERSWITCHGETVLANGROUP: 169 error = ETHERSWITCH_GETVGROUP(etherswitch, (etherswitch_vlangroup_t *)data); 170 break; 171 172 case IOETHERSWITCHSETVLANGROUP: 173 error = ETHERSWITCH_SETVGROUP(etherswitch, (etherswitch_vlangroup_t *)data); 174 break; 175 176 case IOETHERSWITCHGETPHYREG: 177 phyreg = (etherswitch_phyreg_t *)data; 178 phyreg->val = ETHERSWITCH_READPHYREG(etherswitch, phyreg->phy, phyreg->reg); 179 break; 180 181 case IOETHERSWITCHSETPHYREG: 182 phyreg = (etherswitch_phyreg_t *)data; 183 error = ETHERSWITCH_WRITEPHYREG(etherswitch, phyreg->phy, phyreg->reg, phyreg->val); 184 break; 185 186 case IOETHERSWITCHGETCONF: 187 bzero(&conf, sizeof(etherswitch_conf_t)); 188 error = ETHERSWITCH_GETCONF(etherswitch, &conf); 189 bcopy(&conf, data, sizeof(etherswitch_conf_t)); 190 break; 191 192 case IOETHERSWITCHSETCONF: 193 error = ETHERSWITCH_SETCONF(etherswitch, (etherswitch_conf_t *)data); 194 break; 195 196 default: 197 error = ENOTTY; 198 } 199 200 return (error); 201 } 202 203 MODULE_VERSION(etherswitch, 1); 204