]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/bwn/bwn_mac.c
Merge libc++ trunk r300890, and update build glue.
[FreeBSD/FreeBSD.git] / sys / dev / bwn / bwn_mac.c
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 struct bwn_softc {
51         int                      mem_rid;
52         struct bhnd_resource    *mem_res;
53
54         int                      intr_rid;
55         struct resource         *intr_res;
56 };
57
58 static const struct bwn_device {
59         uint16_t         vendor;
60         uint16_t         device;
61 } bwn_devices[] = {
62         { BHND_MFGID_BCM,       BHND_COREID_D11 },
63         { BHND_MFGID_INVALID,   BHND_COREID_INVALID }
64 };
65
66 static int
67 bwn_probe(device_t dev)
68 {
69         const struct bwn_device *id;
70
71         for (id = bwn_devices; id->device != BHND_COREID_INVALID; id++)
72         {
73                 if (bhnd_get_vendor(dev) == id->vendor &&
74                     bhnd_get_device(dev) == id->device)
75                 {
76                         device_set_desc(dev, bhnd_get_device_name(dev));
77                         return (BUS_PROBE_DEFAULT);
78                 }
79         }
80
81         return (ENXIO);
82 }
83
84 static int
85 bwn_attach(device_t dev)
86 {
87         struct bwn_softc        *sc;
88         int                      error;
89
90         sc = device_get_softc(dev);
91
92         /* Allocate device resources */
93         sc->mem_rid = 0;
94         sc->mem_res = bhnd_alloc_resource_any(dev, SYS_RES_MEMORY,
95             &sc->mem_rid, RF_ACTIVE);
96         if (sc->mem_res == NULL) {
97                 device_printf(dev, "failed to allocate device registers\n");
98                 error = ENXIO;
99                 goto cleanup;
100         }
101
102         sc->intr_rid = 0;
103         sc->intr_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->intr_rid,
104             RF_ACTIVE|RF_SHAREABLE);
105         if (sc->intr_res == NULL) {
106                 device_printf(dev, "failed to allocate device interrupt\n");
107                 error = ENXIO;
108                 goto cleanup;
109         }
110
111         // TODO
112         uint8_t macaddr[6];
113         error = bhnd_nvram_getvar_array(dev, BHND_NVAR_MACADDR, macaddr,
114             sizeof(macaddr), BHND_NVRAM_TYPE_UINT8_ARRAY);
115         if (error)
116                 device_printf(dev, "error fetching macaddr: %d\n", error);
117         else
118                 device_printf(dev, "got macaddr %6D\n", macaddr, ":");
119
120         return (0);
121
122 cleanup:
123         if (sc->mem_res != NULL)
124                 bhnd_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid,
125                     sc->mem_res);
126
127         if (sc->intr_res != NULL)
128                 bus_release_resource(dev, SYS_RES_IRQ, sc->intr_rid,
129                     sc->intr_res);
130
131         return (error);
132 }
133
134 static int
135 bwn_detach(device_t dev)
136 {
137         struct bwn_softc        *sc;
138
139         sc = device_get_softc(dev);
140
141         bhnd_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem_res);
142         bus_release_resource(dev, SYS_RES_IRQ, sc->intr_rid, sc->intr_res);
143
144         return (0);
145 }
146
147 static int
148 bwn_suspend(device_t dev)
149 {
150         return (0);
151 }
152
153 static int
154 bwn_resume(device_t dev)
155 {
156         return (0);
157 }
158
159 static device_method_t bwn_methods[] = {
160         /* Device interface */
161         DEVMETHOD(device_probe,         bwn_probe),
162         DEVMETHOD(device_attach,        bwn_attach),
163         DEVMETHOD(device_detach,        bwn_detach),
164         DEVMETHOD(device_suspend,       bwn_suspend),
165         DEVMETHOD(device_resume,        bwn_resume),
166         DEVMETHOD_END
167 };
168
169 static devclass_t bwn_devclass;
170
171 DEFINE_CLASS_0(bwn, bwn_driver, bwn_methods, sizeof(struct bwn_softc));
172 DRIVER_MODULE(bwn_mac, bhnd, bwn_driver, bwn_devclass, 0, 0);
173 MODULE_DEPEND(bwn_mac, bhnd, 1, 1, 1);
174 MODULE_VERSION(bwn_mac, 1);