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