xref: /openbsd/sys/dev/usb/dwc2/dwc2var.h (revision f6fab45d)
1 /*	$OpenBSD: dwc2var.h,v 1.24 2022/09/10 08:13:16 mglocker Exp $	*/
2 /*	$NetBSD: dwc2var.h,v 1.3 2013/10/22 12:57:40 skrll 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	_DWC2VAR_H_
34 #define	_DWC2VAR_H_
35 
36 #include <sys/pool.h>
37 #include <sys/task.h>
38 
39 struct dwc2_hsotg;
40 struct dwc2_qtd;
41 
42 struct dwc2_xfer {
43 	struct usbd_xfer xfer;			/* Needs to be first */
44 
45 	struct dwc2_hcd_urb *urb;
46 
47 	TAILQ_ENTRY(dwc2_xfer) xnext;		/* list of complete xfers */
48 	usbd_status intr_status;
49 };
50 
51 struct dwc2_pipe {
52 	struct usbd_pipe pipe;		/* Must be first */
53 
54 	/* Current transfer */
55 	void *priv;			/* QH */
56 
57 	 /* DMA buffer for control endpoint requests */
58 	struct usb_dma req_dma;
59 };
60 
61 
62 #define	DWC2_BUS2SC(bus)	((void *)(bus))
63 #define	DWC2_PIPE2SC(pipe)	DWC2_BUS2SC((pipe)->device->bus)
64 #define	DWC2_XFER2SC(xfer)	DWC2_PIPE2SC((xfer)->pipe)
65 #define	DWC2_DPIPE2SC(d)	DWC2_BUS2SC((d)->pipe.device->bus)
66 
67 #define	DWC2_XFER2DXFER(x)	(struct dwc2_xfer *)(x)
68 
69 #define	DWC2_XFER2DPIPE(x)	(struct dwc2_pipe *)(x)->pipe;
70 #define	DWC2_PIPE2DPIPE(p)	(struct dwc2_pipe *)(p)
71 
72 
73 typedef struct dwc2_softc {
74 	struct usbd_bus		sc_bus;
75 
76  	bus_space_tag_t		sc_iot;
77  	bus_space_handle_t	sc_ioh;
78 	struct dwc2_core_params *sc_params;
79 	int			(*sc_set_dma_addr)(struct device *, bus_addr_t, int);
80 
81 	/*
82 	 * Private
83 	 */
84 
85 	struct dwc2_hsotg *sc_hsotg;
86 
87 	struct mutex sc_lock;
88 
89 	bool sc_hcdenabled;
90 	void *sc_rhc_si;
91 
92 	struct usbd_xfer *sc_intrxfer;
93 
94 	struct device *sc_child;	/* /dev/usb# device */
95 
96 	char sc_vendor[32];		/* vendor string for root hub */
97 
98 	TAILQ_HEAD(, dwc2_xfer) sc_complete;	/* complete transfers */
99 
100 	struct pool sc_xferpool;
101 	struct pool sc_qhpool;
102 	struct pool sc_qtdpool;
103 
104 	uint8_t sc_addr;		/* device address */
105 	uint8_t sc_conf;		/* device configuration */
106 
107 } dwc2_softc_t;
108 
109 int		dwc2_init(struct dwc2_softc *);
110 int		dwc2_intr(void *);
111 int		dwc2_detach(dwc2_softc_t *, int);
112 
113 void		dwc2_worker(struct task *, void *);
114 
115 void		dwc2_host_complete(struct dwc2_hsotg *, struct dwc2_qtd *,
116 				   int);
117 
118 static inline void
dwc2_root_intr(dwc2_softc_t * sc)119 dwc2_root_intr(dwc2_softc_t *sc)
120 {
121 
122 	softintr_schedule(sc->sc_rhc_si);
123 }
124 
125 /*
126  * XXX Compat
127  */
128 #define USB_MAXCHILDREN		31	/* XXX: Include in to our USB stack */
129 #define ENOSR			90
130 #define device_xname(d)		((d)->dv_xname)
131 #define jiffies			hardclock_ticks
132 #define msecs_to_jiffies(x)	(((uint64_t)(x)) * hz / 1000)
133 #define IS_ENABLED(option)	(option)
134 #define DIV_ROUND_UP(x, y)	(((x) + ((y) - 1)) / (y))
135 #define NS_TO_US(ns)		DIV_ROUND_UP(ns, 1000L)
136 #define BitTime(bytecount)	(7 * 8 * bytecount / 6)
137 #define BITS_PER_LONG		64
138 #define unlikely(x)		 __builtin_expect(!!(x), 0)
139 
140 #define USB2_HOST_DELAY		5
141 #define HS_NSECS(bytes)		(((55 * 8 * 2083)		\
142 	+ (2083UL * (3 + BitTime(bytes))))/1000			\
143 	+ USB2_HOST_DELAY)
144 #define HS_NSECS_ISO(bytes)	(((38 * 8 * 2083)		\
145 	+ (2083UL * (3 + BitTime(bytes))))/1000			\
146 	+ USB2_HOST_DELAY)
147 #define HS_USECS(bytes)		NS_TO_US(HS_NSECS(bytes))
148 #define HS_USECS_ISO(bytes)	NS_TO_US(HS_NSECS_ISO(bytes))
149 
150 #define min_t(t, a, b) ({					\
151 	t __min_a = (a);					\
152 	t __min_b = (b);					\
153 	__min_a < __min_b ? __min_a : __min_b; })
154 #define max_t(t, a, b) ({					\
155         t __max_a = (a);					\
156         t __max_b = (b);					\
157         __max_a > __max_b ? __max_a : __max_b; })
158 
159 #define _WARN_STR(x)		#x
160 #define WARN_ON(condition) ({					\
161 	int __ret = !!(condition);				\
162 	if (__ret)						\
163 		printf("WARNING %s failed at %s:%d\n",		\
164 		    _WARN_STR(condition), __FILE__, __LINE__);	\
165 	unlikely(__ret);					\
166 })
167 #define WARN_ON_ONCE(condition) ({				\
168 	static int __warned;					\
169 	int __ret = !!(condition);				\
170 	if (__ret && !__warned) {				\
171 		printf("WARNING %s failed at %s:%d\n",		\
172 		    _WARN_STR(condition), __FILE__, __LINE__);	\
173 		__warned = 1;					\
174 	}							\
175 	unlikely(__ret);					\
176 })
177 
178 #endif	/* _DWC_OTGVAR_H_ */
179