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