2 * Copyright (c) 2011-2012 Stefan Bethke.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
32 #include <sys/fcntl.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/socket.h>
39 #include <sys/systm.h>
44 #include <dev/etherswitch/etherswitch.h>
46 #include "etherswitch_if.h"
50 struct etherswitch_softc {
54 struct cdev *sc_devnode;
58 #define SWITCH_LOCK(sc) sx_xlock(&(sc)->sc_lock)
59 #define SWITCH_UNLOCK(sc) sx_xunlock(&(sc)->sc_lock)
61 static int etherswitch_probe(device_t);
62 static int etherswitch_attach(device_t);
63 static int etherswitch_detach(device_t);
64 static void etherswitch_identify(driver_t *driver, device_t parent);
66 devclass_t etherswitch_devclass;
68 static device_method_t etherswitch_methods[] = {
69 /* device interface */
70 DEVMETHOD(device_identify, etherswitch_identify),
71 DEVMETHOD(device_probe, etherswitch_probe),
72 DEVMETHOD(device_attach, etherswitch_attach),
73 DEVMETHOD(device_detach, etherswitch_detach),
78 driver_t etherswitch_driver = {
81 sizeof(struct etherswitch_softc),
84 static d_open_t etherswitchopen;
85 static d_close_t etherswitchclose;
86 static d_write_t etherswitchwrite;
87 static d_read_t etherswitchread;
88 static d_ioctl_t etherswitchioctl;
90 static struct cdevsw etherswitch_cdevsw = {
91 .d_version = D_VERSION,
92 .d_flags = D_TRACKCLOSE,
93 .d_open = etherswitchopen,
94 .d_close = etherswitchclose,
95 .d_read = etherswitchread,
96 .d_write = etherswitchwrite,
97 .d_ioctl = etherswitchioctl,
98 .d_name = "etherswitch",
102 etherswitch_identify(driver_t *driver, device_t parent)
104 if (device_find_child(parent, "etherswitch", -1) == NULL)
105 BUS_ADD_CHILD(parent, 0, "etherswitch", -1);
109 etherswitch_probe(device_t dev)
111 device_set_desc(dev, "Switch controller");
117 etherswitch_attach(device_t dev)
119 struct etherswitch_softc *sc = (struct etherswitch_softc *)device_get_softc(dev);
122 sx_init(&sc->sc_lock, "etherswitch");
123 sc->sc_devnode = make_dev(ðerswitch_cdevsw, device_get_unit(dev),
125 0600, "etherswitch%d", device_get_unit(dev));
126 if (sc->sc_devnode == NULL) {
127 device_printf(dev, "failed to create character device\n");
128 sx_destroy(&sc->sc_lock);
131 sc->sc_devnode->si_drv1 = sc;
137 etherswitch_detach(device_t dev)
139 struct etherswitch_softc *sc = (struct etherswitch_softc *)device_get_softc(dev);
142 destroy_dev(sc->sc_devnode);
143 sx_destroy(&sc->sc_lock);
149 etherswitchopen(struct cdev *dev, int flags, int fmt, struct thread *td)
151 struct etherswitch_softc *sc = dev->si_drv1;
154 if (sc->sc_count > 0) {
166 etherswitchclose(struct cdev *dev, int flags, int fmt, struct thread *td)
168 struct etherswitch_softc *sc = dev->si_drv1;
171 if (sc->sc_count == 0) {
178 if (sc->sc_count < 0)
179 panic("%s: etherswitch_count < 0!", __func__);
186 etherswitchwrite(struct cdev *dev, struct uio * uio, int ioflag)
192 etherswitchread(struct cdev *dev, struct uio * uio, int ioflag)
198 etherswitchioctl(struct cdev *cdev, u_long cmd, caddr_t data, int flags, struct thread *td)
200 struct etherswitch_softc *sc = cdev->si_drv1;
201 device_t dev = sc->sc_dev;
202 device_t etherswitch = device_get_parent(dev);
203 etherswitch_conf_t conf;
204 etherswitch_info_t *info;
205 etherswitch_reg_t *reg;
206 etherswitch_phyreg_t *phyreg;
210 case IOETHERSWITCHGETINFO:
211 info = ETHERSWITCH_GETINFO(etherswitch);
212 bcopy(info, data, sizeof(etherswitch_info_t));
215 case IOETHERSWITCHGETREG:
216 reg = (etherswitch_reg_t *)data;
217 ETHERSWITCH_LOCK(etherswitch);
218 reg->val = ETHERSWITCH_READREG(etherswitch, reg->reg);
219 ETHERSWITCH_UNLOCK(etherswitch);
222 case IOETHERSWITCHSETREG:
223 reg = (etherswitch_reg_t *)data;
224 ETHERSWITCH_LOCK(etherswitch);
225 error = ETHERSWITCH_WRITEREG(etherswitch, reg->reg, reg->val);
226 ETHERSWITCH_UNLOCK(etherswitch);
229 case IOETHERSWITCHGETPORT:
230 error = ETHERSWITCH_GETPORT(etherswitch, (etherswitch_port_t *)data);
233 case IOETHERSWITCHSETPORT:
234 error = ETHERSWITCH_SETPORT(etherswitch, (etherswitch_port_t *)data);
237 case IOETHERSWITCHGETVLANGROUP:
238 error = ETHERSWITCH_GETVGROUP(etherswitch, (etherswitch_vlangroup_t *)data);
241 case IOETHERSWITCHSETVLANGROUP:
242 error = ETHERSWITCH_SETVGROUP(etherswitch, (etherswitch_vlangroup_t *)data);
245 case IOETHERSWITCHGETPHYREG:
246 phyreg = (etherswitch_phyreg_t *)data;
247 phyreg->val = ETHERSWITCH_READPHYREG(etherswitch, phyreg->phy, phyreg->reg);
250 case IOETHERSWITCHSETPHYREG:
251 phyreg = (etherswitch_phyreg_t *)data;
252 error = ETHERSWITCH_WRITEPHYREG(etherswitch, phyreg->phy, phyreg->reg, phyreg->val);
255 case IOETHERSWITCHGETCONF:
256 bzero(&conf, sizeof(etherswitch_conf_t));
257 error = ETHERSWITCH_GETCONF(etherswitch, &conf);
258 bcopy(&conf, data, sizeof(etherswitch_conf_t));
261 case IOETHERSWITCHSETCONF:
262 error = ETHERSWITCH_SETCONF(etherswitch, (etherswitch_conf_t *)data);
272 MODULE_VERSION(etherswitch, 1);