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 #include <sys/param.h> 23 #include <sys/lock.h> 24 #include <sys/mutex.h> 25 #include <sys/mbuf.h> 26 #include <sys/kernel.h> 27 #include <sys/socket.h> 28 #include <sys/systm.h> 29 #include <sys/malloc.h> 30 #include <sys/queue.h> 31 #include <sys/taskqueue.h> 32 #include <sys/bus.h> 33 #include <sys/endian.h> 34 35 #include <net/if.h> 36 #include <net/ethernet.h> 37 #include <net/if_media.h> 38 39 #include <net80211/ieee80211_var.h> 40 #include <net80211/ieee80211_radiotap.h> 41 42 #include <dev/usb/usb.h> 43 #include <dev/usb/usbdi.h> 44 45 #include <dev/rtwn/if_rtwnvar.h> 46 #include <dev/rtwn/if_rtwn_debug.h> 47 48 #include <dev/rtwn/usb/rtwn_usb_var.h> 49 #include <dev/rtwn/usb/rtwn_usb_reg.h> 50 51 static int rtwn_do_request(struct rtwn_softc *, 52 struct usb_device_request *, void *); 53 static int rtwn_usb_read_region_1(struct rtwn_softc *, 54 uint16_t, uint8_t *, int); 55 56 /* USB Requests. */ 57 #define R92C_REQ_REGS 0x05 58 59 static int 60 rtwn_do_request(struct rtwn_softc *sc, struct usb_device_request *req, 61 void *data) 62 { 63 struct rtwn_usb_softc *uc = RTWN_USB_SOFTC(sc); 64 usb_error_t err; 65 int ntries = 10; 66 67 RTWN_ASSERT_LOCKED(sc); 68 69 while (ntries--) { 70 err = usbd_do_request_flags(uc->uc_udev, &sc->sc_mtx, 71 req, data, 0, NULL, 250 /* ms */); 72 if (err == USB_ERR_NORMAL_COMPLETION) 73 return (0); 74 75 RTWN_DPRINTF(sc, RTWN_DEBUG_USB, 76 "%s: control request failed, %s (retries left: %d)\n", 77 __func__, usbd_errstr(err), ntries); 78 if (err == USB_ERR_NOT_CONFIGURED) 79 return (ENXIO); 80 81 usb_pause_mtx(&sc->sc_mtx, hz / 100); 82 } 83 return (EIO); 84 } 85 86 /* export for rtwn_fw_write_block() */ 87 int 88 rtwn_usb_write_region_1(struct rtwn_softc *sc, uint16_t addr, uint8_t *buf, 89 int len) 90 { 91 usb_device_request_t req; 92 93 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 94 req.bRequest = R92C_REQ_REGS; 95 USETW(req.wValue, addr); 96 USETW(req.wIndex, 0); 97 USETW(req.wLength, len); 98 return (rtwn_do_request(sc, &req, buf)); 99 } 100 101 int 102 rtwn_usb_write_1(struct rtwn_softc *sc, uint16_t addr, uint8_t val) 103 { 104 return (rtwn_usb_write_region_1(sc, addr, &val, sizeof(val))); 105 } 106 107 int 108 rtwn_usb_write_2(struct rtwn_softc *sc, uint16_t addr, uint16_t val) 109 { 110 val = htole16(val); 111 return (rtwn_usb_write_region_1(sc, addr, (uint8_t *)&val, sizeof(val))); 112 } 113 114 int 115 rtwn_usb_write_4(struct rtwn_softc *sc, uint16_t addr, uint32_t val) 116 { 117 val = htole32(val); 118 return (rtwn_usb_write_region_1(sc, addr, (uint8_t *)&val, sizeof(val))); 119 } 120 121 static int 122 rtwn_usb_read_region_1(struct rtwn_softc *sc, uint16_t addr, uint8_t *buf, 123 int len) 124 { 125 usb_device_request_t req; 126 127 req.bmRequestType = UT_READ_VENDOR_DEVICE; 128 req.bRequest = R92C_REQ_REGS; 129 USETW(req.wValue, addr); 130 USETW(req.wIndex, 0); 131 USETW(req.wLength, len); 132 return (rtwn_do_request(sc, &req, buf)); 133 } 134 135 uint8_t 136 rtwn_usb_read_1(struct rtwn_softc *sc, uint16_t addr) 137 { 138 uint8_t val; 139 140 if (rtwn_usb_read_region_1(sc, addr, &val, 1) != 0) 141 return (0xff); 142 return (val); 143 } 144 145 uint16_t 146 rtwn_usb_read_2(struct rtwn_softc *sc, uint16_t addr) 147 { 148 uint16_t val; 149 150 if (rtwn_usb_read_region_1(sc, addr, (uint8_t *)&val, 2) != 0) 151 return (0xffff); 152 return (le16toh(val)); 153 } 154 155 uint32_t 156 rtwn_usb_read_4(struct rtwn_softc *sc, uint16_t addr) 157 { 158 uint32_t val; 159 160 if (rtwn_usb_read_region_1(sc, addr, (uint8_t *)&val, 4) != 0) 161 return (0xffffffff); 162 return (le32toh(val)); 163 } 164 165 void 166 rtwn_usb_delay(struct rtwn_softc *sc, int usec) 167 { 168 169 /* 1ms delay as default is too big. */ 170 if (usec < 1000) 171 DELAY(usec); 172 else 173 usb_pause_mtx(&sc->sc_mtx, msecs_to_ticks(usec / 1000)); 174 } 175