1 | #include <machine/rtems-bsd-kernel-space.h> |
---|
2 | |
---|
3 | /* $OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $ */ |
---|
4 | |
---|
5 | /*- |
---|
6 | * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr> |
---|
7 | * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org> |
---|
8 | * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org> |
---|
9 | * |
---|
10 | * Permission to use, copy, modify, and distribute this software for any |
---|
11 | * purpose with or without fee is hereby granted, provided that the above |
---|
12 | * copyright notice and this permission notice appear in all copies. |
---|
13 | * |
---|
14 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
---|
15 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
16 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
---|
17 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
18 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
19 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
---|
20 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
21 | */ |
---|
22 | |
---|
23 | #include <sys/cdefs.h> |
---|
24 | __FBSDID("$FreeBSD$"); |
---|
25 | |
---|
26 | #include <rtems/bsd/sys/param.h> |
---|
27 | #include <rtems/bsd/sys/lock.h> |
---|
28 | #include <sys/mutex.h> |
---|
29 | #include <sys/mbuf.h> |
---|
30 | #include <sys/kernel.h> |
---|
31 | #include <sys/socket.h> |
---|
32 | #include <sys/systm.h> |
---|
33 | #include <sys/malloc.h> |
---|
34 | #include <sys/queue.h> |
---|
35 | #include <sys/taskqueue.h> |
---|
36 | #include <sys/bus.h> |
---|
37 | #include <sys/endian.h> |
---|
38 | |
---|
39 | #include <net/if.h> |
---|
40 | #include <net/ethernet.h> |
---|
41 | #include <net/if_media.h> |
---|
42 | |
---|
43 | #include <net80211/ieee80211_var.h> |
---|
44 | #include <net80211/ieee80211_radiotap.h> |
---|
45 | |
---|
46 | #include <dev/usb/usb.h> |
---|
47 | #include <dev/usb/usbdi.h> |
---|
48 | |
---|
49 | #include <dev/rtwn/if_rtwnvar.h> |
---|
50 | #include <dev/rtwn/if_rtwn_debug.h> |
---|
51 | |
---|
52 | #include <dev/rtwn/usb/rtwn_usb_var.h> |
---|
53 | #include <dev/rtwn/usb/rtwn_usb_reg.h> |
---|
54 | |
---|
55 | static int rtwn_do_request(struct rtwn_softc *, |
---|
56 | struct usb_device_request *, void *); |
---|
57 | static int rtwn_usb_read_region_1(struct rtwn_softc *, |
---|
58 | uint16_t, uint8_t *, int); |
---|
59 | |
---|
60 | /* USB Requests. */ |
---|
61 | #define R92C_REQ_REGS 0x05 |
---|
62 | |
---|
63 | |
---|
64 | static int |
---|
65 | rtwn_do_request(struct rtwn_softc *sc, struct usb_device_request *req, |
---|
66 | void *data) |
---|
67 | { |
---|
68 | struct rtwn_usb_softc *uc = RTWN_USB_SOFTC(sc); |
---|
69 | usb_error_t err; |
---|
70 | int ntries = 10; |
---|
71 | |
---|
72 | RTWN_ASSERT_LOCKED(sc); |
---|
73 | |
---|
74 | while (ntries--) { |
---|
75 | err = usbd_do_request_flags(uc->uc_udev, &sc->sc_mtx, |
---|
76 | req, data, 0, NULL, 250 /* ms */); |
---|
77 | if (err == USB_ERR_NORMAL_COMPLETION) |
---|
78 | return (0); |
---|
79 | |
---|
80 | RTWN_DPRINTF(sc, RTWN_DEBUG_USB, |
---|
81 | "%s: control request failed, %s (retries left: %d)\n", |
---|
82 | __func__, usbd_errstr(err), ntries); |
---|
83 | if (err == USB_ERR_NOT_CONFIGURED) |
---|
84 | return (ENXIO); |
---|
85 | |
---|
86 | usb_pause_mtx(&sc->sc_mtx, hz / 100); |
---|
87 | } |
---|
88 | return (EIO); |
---|
89 | } |
---|
90 | |
---|
91 | /* export for rtwn_fw_write_block() */ |
---|
92 | int |
---|
93 | rtwn_usb_write_region_1(struct rtwn_softc *sc, uint16_t addr, uint8_t *buf, |
---|
94 | int len) |
---|
95 | { |
---|
96 | usb_device_request_t req; |
---|
97 | |
---|
98 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; |
---|
99 | req.bRequest = R92C_REQ_REGS; |
---|
100 | USETW(req.wValue, addr); |
---|
101 | USETW(req.wIndex, 0); |
---|
102 | USETW(req.wLength, len); |
---|
103 | return (rtwn_do_request(sc, &req, buf)); |
---|
104 | } |
---|
105 | |
---|
106 | int |
---|
107 | rtwn_usb_write_1(struct rtwn_softc *sc, uint16_t addr, uint8_t val) |
---|
108 | { |
---|
109 | return (rtwn_usb_write_region_1(sc, addr, &val, sizeof(val))); |
---|
110 | } |
---|
111 | |
---|
112 | int |
---|
113 | rtwn_usb_write_2(struct rtwn_softc *sc, uint16_t addr, uint16_t val) |
---|
114 | { |
---|
115 | val = htole16(val); |
---|
116 | return (rtwn_usb_write_region_1(sc, addr, (uint8_t *)&val, sizeof(val))); |
---|
117 | } |
---|
118 | |
---|
119 | int |
---|
120 | rtwn_usb_write_4(struct rtwn_softc *sc, uint16_t addr, uint32_t val) |
---|
121 | { |
---|
122 | val = htole32(val); |
---|
123 | return (rtwn_usb_write_region_1(sc, addr, (uint8_t *)&val, sizeof(val))); |
---|
124 | } |
---|
125 | |
---|
126 | static int |
---|
127 | rtwn_usb_read_region_1(struct rtwn_softc *sc, uint16_t addr, uint8_t *buf, |
---|
128 | int len) |
---|
129 | { |
---|
130 | usb_device_request_t req; |
---|
131 | |
---|
132 | req.bmRequestType = UT_READ_VENDOR_DEVICE; |
---|
133 | req.bRequest = R92C_REQ_REGS; |
---|
134 | USETW(req.wValue, addr); |
---|
135 | USETW(req.wIndex, 0); |
---|
136 | USETW(req.wLength, len); |
---|
137 | return (rtwn_do_request(sc, &req, buf)); |
---|
138 | } |
---|
139 | |
---|
140 | uint8_t |
---|
141 | rtwn_usb_read_1(struct rtwn_softc *sc, uint16_t addr) |
---|
142 | { |
---|
143 | uint8_t val; |
---|
144 | |
---|
145 | if (rtwn_usb_read_region_1(sc, addr, &val, 1) != 0) |
---|
146 | return (0xff); |
---|
147 | return (val); |
---|
148 | } |
---|
149 | |
---|
150 | uint16_t |
---|
151 | rtwn_usb_read_2(struct rtwn_softc *sc, uint16_t addr) |
---|
152 | { |
---|
153 | uint16_t val; |
---|
154 | |
---|
155 | if (rtwn_usb_read_region_1(sc, addr, (uint8_t *)&val, 2) != 0) |
---|
156 | return (0xffff); |
---|
157 | return (le16toh(val)); |
---|
158 | } |
---|
159 | |
---|
160 | uint32_t |
---|
161 | rtwn_usb_read_4(struct rtwn_softc *sc, uint16_t addr) |
---|
162 | { |
---|
163 | uint32_t val; |
---|
164 | |
---|
165 | if (rtwn_usb_read_region_1(sc, addr, (uint8_t *)&val, 4) != 0) |
---|
166 | return (0xffffffff); |
---|
167 | return (le32toh(val)); |
---|
168 | } |
---|
169 | |
---|
170 | void |
---|
171 | rtwn_usb_delay(struct rtwn_softc *sc, int usec) |
---|
172 | { |
---|
173 | |
---|
174 | /* 1ms delay as default is too big. */ |
---|
175 | if (usec < 1000) |
---|
176 | DELAY(usec); |
---|
177 | else { |
---|
178 | usb_pause_mtx(&sc->sc_mtx, |
---|
179 | MAX(msecs_to_ticks(usec / 1000), 1)); |
---|
180 | } |
---|
181 | } |
---|