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