1 /* $OpenBSD: dwc2.h,v 1.14 2017/02/15 14:49:13 visa Exp $ */ 2 /* $NetBSD: dwc2.h,v 1.4 2014/12/23 16:20:06 macallan Exp $ */ 3 4 /*- 5 * Copyright (c) 2013 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Nick Hudson 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #ifndef _EXTERNAL_BSD_DWC2_DWC2_H_ 34 #define _EXTERNAL_BSD_DWC2_DWC2_H_ 35 36 #include <sys/param.h> 37 #include <sys/kernel.h> 38 39 #include <sys/task.h> 40 #include <sys/timeout.h> 41 42 #include <lib/libkern/libkern.h> 43 44 #if 0 45 #include "opt_usb.h" 46 #endif 47 48 #define STATIC_INLINE static inline 49 #define STATIC 50 51 // #define VERBOSE_DEBUG 52 // #define DWC2_DUMP_FRREM 53 // #define CONFIG_USB_DWC2_TRACK_MISSED_SOFS 54 55 typedef int irqreturn_t; 56 #define IRQ_NONE 0 57 #define IRQ_HANDLED 1 58 59 #define u8 uint8_t 60 #define u16 uint16_t 61 #define s16 int16_t 62 #define u32 uint32_t 63 #define u64 uint64_t 64 65 #define dma_addr_t bus_addr_t 66 67 #ifdef DWC2_DEBUG 68 extern int dwc2debug; 69 #define WARN_ON(x) KASSERT(!(x)) 70 71 #define dev_info(d,fmt,...) do { \ 72 printf("%s: " fmt, device_xname(d), \ 73 ## __VA_ARGS__); \ 74 } while (0) 75 #define dev_warn(d,fmt,...) do { \ 76 printf("%s: " fmt, device_xname(d), \ 77 ## __VA_ARGS__); \ 78 } while (0) 79 #define dev_err(d,fmt,...) do { \ 80 printf("%s: " fmt, device_xname(d), \ 81 ## __VA_ARGS__); \ 82 } while (0) 83 #define dev_dbg(d,fmt,...) do { \ 84 if (dwc2debug >= 1) { \ 85 printf("%s: " fmt, device_xname(d), \ 86 ## __VA_ARGS__); \ 87 } \ 88 } while (0) 89 #define dev_vdbg(d,fmt,...) do { \ 90 if (dwc2debug >= 2) { \ 91 printf("%s: " fmt, device_xname(d), \ 92 ## __VA_ARGS__); \ 93 } \ 94 } while (0) 95 #else 96 #define WARN_ON(x) 97 #define dev_info(...) do { } while (0) 98 #define dev_warn(...) do { } while (0) 99 #define dev_err(...) do { } while (0) 100 #define dev_dbg(...) do { } while (0) 101 #define dev_vdbg(...) do { } while (0) 102 #endif 103 104 #define jiffies hardclock_ticks 105 #define msecs_to_jiffies mstohz 106 107 #define gfp_t int 108 #define GFP_KERNEL M_WAITOK 109 #define GFP_ATOMIC M_NOWAIT 110 111 enum usb_otg_state { 112 OTG_STATE_RESERVED = 0, 113 114 OTG_STATE_A_HOST, 115 OTG_STATE_A_PERIPHERAL, 116 OTG_STATE_A_SUSPEND, 117 OTG_STATE_B_HOST, 118 OTG_STATE_B_PERIPHERAL, 119 }; 120 121 #define usleep_range(l, u) do { DELAY(u); } while (0) 122 123 #define spinlock_t struct mutex 124 #define spin_lock_init(lock) mtx_init(lock, IPL_USB) 125 #define spin_lock(l) do { mtx_enter(l); } while (0) 126 #define spin_unlock(l) do { mtx_leave(l); } while (0) 127 128 #define spin_lock_irqsave(l, f) \ 129 do { mtx_enter(l); (void)(f); } while (0) 130 131 #define spin_unlock_irqrestore(l, f) \ 132 do { mtx_leave(l); (void)(f); } while (0) 133 134 #define IRQ_RETVAL(r) (r) 135 136 #define USB_ENDPOINT_XFER_CONTROL UE_CONTROL /* 0 */ 137 #define USB_ENDPOINT_XFER_ISOC UE_ISOCHRONOUS /* 1 */ 138 #define USB_ENDPOINT_XFER_BULK UE_BULK /* 2 */ 139 #define USB_ENDPOINT_XFER_INT UE_INTERRUPT /* 3 */ 140 141 #define USB_DIR_IN UE_DIR_IN 142 #define USB_DIR_OUT UE_DIR_OUT 143 144 #define USB_PORT_FEAT_CONNECTION UHF_PORT_CONNECTION 145 #define USB_PORT_FEAT_ENABLE UHF_PORT_ENABLE 146 #define USB_PORT_FEAT_SUSPEND UHF_PORT_SUSPEND 147 #define USB_PORT_FEAT_OVER_CURRENT UHF_PORT_OVER_CURRENT 148 #define USB_PORT_FEAT_RESET UHF_PORT_RESET 149 // #define USB_PORT_FEAT_L1 5 /* L1 suspend */ 150 #define USB_PORT_FEAT_POWER UHF_PORT_POWER 151 #define USB_PORT_FEAT_LOWSPEED UHF_PORT_LOW_SPEED 152 #define USB_PORT_FEAT_C_CONNECTION UHF_C_PORT_CONNECTION 153 #define USB_PORT_FEAT_C_ENABLE UHF_C_PORT_ENABLE 154 #define USB_PORT_FEAT_C_SUSPEND UHF_C_PORT_SUSPEND 155 #define USB_PORT_FEAT_C_OVER_CURRENT UHF_C_PORT_OVER_CURRENT 156 #define USB_PORT_FEAT_C_RESET UHF_C_PORT_RESET 157 #define USB_PORT_FEAT_TEST UHF_PORT_TEST 158 #define USB_PORT_FEAT_INDICATOR UHF_PORT_INDICATOR 159 #define USB_PORT_FEAT_C_PORT_L1 UHF_C_PORT_L1 160 161 #define C_HUB_LOCAL_POWER UHF_C_HUB_LOCAL_POWER 162 #define C_HUB_OVER_CURRENT UHF_C_HUB_OVER_CURRENT 163 164 #define USB_REQ_GET_STATUS UR_GET_STATUS 165 #define USB_REQ_CLEAR_FEATURE UR_CLEAR_FEATURE 166 #define USB_REQ_SET_FEATURE UR_SET_FEATURE 167 #define USB_REQ_GET_DESCRIPTOR UR_GET_DESCRIPTOR 168 169 #define ClearHubFeature ((UT_WRITE_CLASS_DEVICE << 8) | USB_REQ_CLEAR_FEATURE) 170 #define ClearPortFeature ((UT_WRITE_CLASS_OTHER << 8) | USB_REQ_CLEAR_FEATURE) 171 #define GetHubDescriptor ((UT_READ_CLASS_DEVICE << 8) | USB_REQ_GET_DESCRIPTOR) 172 #define GetHubStatus ((UT_READ_CLASS_DEVICE << 8) | USB_REQ_GET_STATUS) 173 #define GetPortStatus ((UT_READ_CLASS_OTHER << 8) | USB_REQ_GET_STATUS) 174 #define SetHubFeature ((UT_WRITE_CLASS_DEVICE << 8) | USB_REQ_SET_FEATURE) 175 #define SetPortFeature ((UT_WRITE_CLASS_OTHER << 8) | USB_REQ_SET_FEATURE) 176 177 #define USB_PORT_STAT_CONNECTION UPS_CURRENT_CONNECT_STATUS 178 #define USB_PORT_STAT_ENABLE UPS_PORT_ENABLED 179 #define USB_PORT_STAT_SUSPEND UPS_SUSPEND 180 #define USB_PORT_STAT_OVERCURRENT UPS_OVERCURRENT_INDICATOR 181 #define USB_PORT_STAT_RESET UPS_RESET 182 #define USB_PORT_STAT_L1 UPS_PORT_L1 183 #define USB_PORT_STAT_POWER UPS_PORT_POWER 184 #define USB_PORT_STAT_LOW_SPEED UPS_LOW_SPEED 185 #define USB_PORT_STAT_HIGH_SPEED UPS_HIGH_SPEED 186 #define USB_PORT_STAT_TEST UPS_PORT_TEST 187 #define USB_PORT_STAT_INDICATOR UPS_PORT_INDICATOR 188 189 #define USB_PORT_STAT_C_CONNECTION UPS_C_CONNECT_STATUS 190 #define USB_PORT_STAT_C_ENABLE UPS_C_PORT_ENABLED 191 #define USB_PORT_STAT_C_SUSPEND UPS_C_SUSPEND 192 #define USB_PORT_STAT_C_OVERCURRENT UPS_C_OVERCURRENT_INDICATOR 193 #define USB_PORT_STAT_C_RESET UPS_C_PORT_RESET 194 #define USB_PORT_STAT_C_L1 UPS_C_PORT_L1 195 196 STATIC_INLINE void 197 udelay(unsigned long usecs) 198 { 199 DELAY(usecs); 200 } 201 202 #define EREMOTEIO EIO 203 #define ECOMM EIO 204 205 #define NS_TO_US(ns) ((ns + 500L) / 1000L) 206 207 void dw_timeout(void *); 208 void dwc2_worker(struct task *, void *); 209 210 struct delayed_work { 211 struct task work; 212 struct timeout dw_timer; 213 214 struct taskq *dw_wq; 215 void (*dw_fn)(void *); 216 void *dw_arg; 217 }; 218 219 STATIC_INLINE void 220 INIT_DELAYED_WORK(struct delayed_work *dw, void (*fn)(void *), void *arg) 221 { 222 dw->dw_fn = fn; 223 dw->dw_arg = arg; 224 timeout_set(&dw->dw_timer, dw_timeout, dw); 225 } 226 227 STATIC_INLINE void 228 queue_delayed_work(struct taskq *wq, struct delayed_work *dw, int j) 229 { 230 dw->dw_wq = wq; 231 timeout_add(&dw->dw_timer, j); 232 } 233 234 #endif 235