1 /*-
2 * Copyright (c) 2015 Landon Fuller <landon@landonf.org>
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 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
15 *
16 * NO WARRANTY
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
28 */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_bwn.h"
34 #include "opt_wlan.h"
35
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/systm.h>
40
41 #include <machine/bus.h>
42 #include <sys/rman.h>
43 #include <machine/resource.h>
44
45 #include <dev/bhnd/bhnd.h>
46 #include <dev/bhnd/bhnd_ids.h>
47
48 #include "bhnd_nvram_map.h"
49
50 static const struct resource_spec bwn_rspec[] = {
51 { SYS_RES_MEMORY, 0, RF_ACTIVE },
52 { -1, -1, 0 }
53 };
54
55 #define RSPEC_LEN (sizeof(bwn_rspec)/sizeof(bwn_rspec[0]))
56
57 struct bwn_softc {
58 struct resource_spec rspec[RSPEC_LEN];
59 struct bhnd_resource *res[RSPEC_LEN-1];
60 };
61
62 static const struct bwn_device {
63 uint16_t vendor;
64 uint16_t device;
65 } bwn_devices[] = {
66 { BHND_MFGID_BCM, BHND_COREID_D11 },
67 { BHND_MFGID_INVALID, BHND_COREID_INVALID }
68 };
69
70 static int
bwn_probe(device_t dev)71 bwn_probe(device_t dev)
72 {
73 const struct bwn_device *id;
74
75 for (id = bwn_devices; id->device != BHND_COREID_INVALID; id++)
76 {
77 if (bhnd_get_vendor(dev) == id->vendor &&
78 bhnd_get_device(dev) == id->device)
79 {
80 device_set_desc(dev, bhnd_get_device_name(dev));
81 return (BUS_PROBE_DEFAULT);
82 }
83 }
84
85 return (ENXIO);
86 }
87
88 static int
bwn_attach(device_t dev)89 bwn_attach(device_t dev)
90 {
91 struct bwn_softc *sc;
92 struct bhnd_resource *r;
93 int error;
94
95 sc = device_get_softc(dev);
96
97 memcpy(sc->rspec, bwn_rspec, sizeof(bwn_rspec));
98 if ((error = bhnd_alloc_resources(dev, sc->rspec, sc->res)))
99 return (error);
100
101 // XXX TODO
102 r = sc->res[0];
103 device_printf(dev, "got rid=%d res=%p\n", sc->rspec[0].rid, r);
104
105 uint8_t macaddr[6];
106 error = bhnd_nvram_getvar(dev, BHND_NVAR_MACADDR, macaddr,
107 sizeof(macaddr));
108 if (error)
109 return (error);
110
111 device_printf(dev, "got macaddr %6D\n", macaddr, ":");
112
113 return (0);
114 }
115
116 static int
bwn_detach(device_t dev)117 bwn_detach(device_t dev)
118 {
119 struct bwn_softc *sc;
120
121 sc = device_get_softc(dev);
122 bhnd_release_resources(dev, sc->rspec, sc->res);
123
124 return (0);
125 }
126
127 static int
bwn_suspend(device_t dev)128 bwn_suspend(device_t dev)
129 {
130 return (0);
131 }
132
133 static int
bwn_resume(device_t dev)134 bwn_resume(device_t dev)
135 {
136 return (0);
137 }
138
139 static device_method_t bwn_methods[] = {
140 /* Device interface */
141 DEVMETHOD(device_probe, bwn_probe),
142 DEVMETHOD(device_attach, bwn_attach),
143 DEVMETHOD(device_detach, bwn_detach),
144 DEVMETHOD(device_suspend, bwn_suspend),
145 DEVMETHOD(device_resume, bwn_resume),
146 DEVMETHOD_END
147 };
148
149 static devclass_t bwn_devclass;
150
151 DEFINE_CLASS_0(bwn, bwn_driver, bwn_methods, sizeof(struct bwn_softc));
152 DRIVER_MODULE(bwn_mac, bhnd, bwn_driver, bwn_devclass, 0, 0);
153 MODULE_DEPEND(bwn_mac, bhnd, 1, 1, 1);
154 MODULE_VERSION(bwn_mac, 1);
155