usbdivar.h revision 1.139 1 1.139 riastrad /* $NetBSD: usbdivar.h,v 1.139 2025/03/31 14:45:14 riastradh Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.97 mrg * Copyright (c) 1998, 2012 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.8 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.56 augustss * by Lennart Augustsson (lennart (at) augustsson.net) at
9 1.138 mrg * Carlstedt Research & Technology and Matthew R. Green (mrg (at) eterna23.net).
10 1.1 augustss *
11 1.1 augustss * Redistribution and use in source and binary forms, with or without
12 1.1 augustss * modification, are permitted provided that the following conditions
13 1.1 augustss * are met:
14 1.1 augustss * 1. Redistributions of source code must retain the above copyright
15 1.1 augustss * notice, this list of conditions and the following disclaimer.
16 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 augustss * notice, this list of conditions and the following disclaimer in the
18 1.1 augustss * documentation and/or other materials provided with the distribution.
19 1.1 augustss *
20 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
31 1.1 augustss */
32 1.1 augustss
33 1.131 riastrad #ifndef _DEV_USB_USBDIVAR_H_
34 1.131 riastrad #define _DEV_USB_USBDIVAR_H_
35 1.131 riastrad
36 1.97 mrg /*
37 1.97 mrg * Discussion about locking in the USB code:
38 1.97 mrg *
39 1.104 skrll * The host controller presents one lock at IPL_SOFTUSB (aka IPL_SOFTNET).
40 1.97 mrg *
41 1.99 mrg * List of hardware interface methods, and whether the lock is held
42 1.99 mrg * when each is called by this module:
43 1.97 mrg *
44 1.113 skrll * BUS METHOD LOCK NOTES
45 1.97 mrg * ----------------------- ------- -------------------------
46 1.113 skrll * ubm_open - might want to take lock?
47 1.133 riastrad * ubm_softint x may release/reacquire lock
48 1.113 skrll * ubm_dopoll - might want to take lock?
49 1.113 skrll * ubm_allocx -
50 1.113 skrll * ubm_freex -
51 1.133 riastrad * ubm_abortx x must not release/reacquire lock
52 1.113 skrll * ubm_getlock - Called at attach time
53 1.113 skrll * ubm_newdev - Will take lock
54 1.135 riastrad * ubm_rhctrl -
55 1.97 mrg *
56 1.113 skrll * PIPE METHOD LOCK NOTES
57 1.97 mrg * ----------------------- ------- -------------------------
58 1.133 riastrad * upm_init -
59 1.133 riastrad * upm_fini -
60 1.134 riastrad * upm_transfer x
61 1.134 riastrad * upm_start x
62 1.113 skrll * upm_abort x
63 1.113 skrll * upm_close x
64 1.113 skrll * upm_cleartoggle -
65 1.113 skrll * upm_done x
66 1.97 mrg *
67 1.97 mrg * The above semantics are likely to change. Little performance
68 1.97 mrg * evaluation has been done on this code and the locking strategy.
69 1.109 skrll *
70 1.97 mrg * USB functions known to expect the lock taken include (this list is
71 1.97 mrg * probably not exhaustive):
72 1.97 mrg * usb_transfer_complete()
73 1.97 mrg * usb_start_next()
74 1.97 mrg *
75 1.97 mrg */
76 1.97 mrg
77 1.139 riastrad #include <sys/bus.h>
78 1.48 thorpej #include <sys/callout.h>
79 1.97 mrg #include <sys/mutex.h>
80 1.139 riastrad
81 1.139 riastrad #include <dev/usb/usbdi.h>
82 1.48 thorpej
83 1.29 augustss /* From usb_mem.h */
84 1.89 dyoung struct usb_dma_block;
85 1.89 dyoung typedef struct {
86 1.113 skrll struct usb_dma_block *udma_block;
87 1.113 skrll u_int udma_offs;
88 1.89 dyoung } usb_dma_t;
89 1.29 augustss
90 1.40 augustss struct usbd_xfer;
91 1.1 augustss struct usbd_pipe;
92 1.105 jakllsch struct usbd_port;
93 1.1 augustss
94 1.1 augustss struct usbd_endpoint {
95 1.113 skrll usb_endpoint_descriptor_t *ue_edesc;
96 1.113 skrll int ue_refcnt;
97 1.113 skrll int ue_toggle;
98 1.1 augustss };
99 1.1 augustss
100 1.29 augustss struct usbd_bus_methods {
101 1.113 skrll usbd_status (*ubm_open)(struct usbd_pipe *);
102 1.113 skrll void (*ubm_softint)(void *);
103 1.113 skrll void (*ubm_dopoll)(struct usbd_bus *);
104 1.113 skrll struct usbd_xfer *(*ubm_allocx)(struct usbd_bus *, unsigned int);
105 1.113 skrll void (*ubm_freex)(struct usbd_bus *, struct usbd_xfer *);
106 1.122 riastrad void (*ubm_abortx)(struct usbd_xfer *);
107 1.122 riastrad bool (*ubm_dying)(struct usbd_bus *);
108 1.113 skrll void (*ubm_getlock)(struct usbd_bus *, kmutex_t **);
109 1.113 skrll usbd_status (*ubm_newdev)(device_t, struct usbd_bus *, int,
110 1.105 jakllsch int, int, struct usbd_port *);
111 1.113 skrll
112 1.113 skrll int (*ubm_rhctrl)(struct usbd_bus *,
113 1.113 skrll usb_device_request_t *, void *, int);
114 1.29 augustss };
115 1.29 augustss
116 1.29 augustss struct usbd_pipe_methods {
117 1.113 skrll int (*upm_init)(struct usbd_xfer *);
118 1.113 skrll void (*upm_fini)(struct usbd_xfer *);
119 1.113 skrll usbd_status (*upm_transfer)(struct usbd_xfer *);
120 1.113 skrll usbd_status (*upm_start)(struct usbd_xfer *);
121 1.113 skrll void (*upm_abort)(struct usbd_xfer *);
122 1.113 skrll void (*upm_close)(struct usbd_pipe *);
123 1.113 skrll void (*upm_cleartoggle)(struct usbd_pipe *);
124 1.113 skrll void (*upm_done)(struct usbd_xfer *);
125 1.1 augustss };
126 1.1 augustss
127 1.72 augustss struct usbd_tt {
128 1.113 skrll struct usbd_hub *utt_hub;
129 1.72 augustss };
130 1.72 augustss
131 1.1 augustss struct usbd_port {
132 1.113 skrll usb_port_status_t up_status;
133 1.113 skrll uint16_t up_power; /* mA of current on port */
134 1.113 skrll uint8_t up_portno;
135 1.113 skrll uint8_t up_restartcnt;
136 1.11 augustss #define USBD_RESTART_MAX 5
137 1.113 skrll uint8_t up_reattach;
138 1.113 skrll struct usbd_device *up_dev; /* Connected device */
139 1.113 skrll struct usbd_device *up_parent; /* The ports hub */
140 1.113 skrll struct usbd_tt *up_tt; /* Transaction translator (if any) */
141 1.1 augustss };
142 1.1 augustss
143 1.1 augustss struct usbd_hub {
144 1.113 skrll usbd_status (*uh_explore)(struct usbd_device *hub);
145 1.113 skrll void *uh_hubsoftc;
146 1.113 skrll usb_hub_descriptor_t uh_hubdesc;
147 1.113 skrll struct usbd_port uh_ports[1];
148 1.1 augustss };
149 1.1 augustss
150 1.1 augustss /*****/
151 1.114 skrll /* 0, root, and 1->127 */
152 1.114 skrll #define USB_ROOTHUB_INDEX 1
153 1.114 skrll #define USB_TOTAL_DEVICES (USB_MAX_DEVICES + 1)
154 1.1 augustss
155 1.1 augustss struct usbd_bus {
156 1.1 augustss /* Filled by HC driver */
157 1.113 skrll void *ub_hcpriv;
158 1.113 skrll int ub_revision; /* USB revision */
159 1.42 augustss #define USBREV_UNKNOWN 0
160 1.42 augustss #define USBREV_PRE_1_0 1
161 1.42 augustss #define USBREV_1_0 2
162 1.42 augustss #define USBREV_1_1 3
163 1.60 augustss #define USBREV_2_0 4
164 1.108 skrll #define USBREV_3_0 5
165 1.116 msaitoh #define USBREV_3_1 6
166 1.116 msaitoh #define USBREV_STR { "unknown", "pre 1.0", "1.0", "1.1", "2.0", "3.0", "3.1" }
167 1.123 maxv int ub_hctype;
168 1.123 maxv #define USBHCTYPE_UNKNOWN 0
169 1.123 maxv #define USBHCTYPE_MOTG 1
170 1.123 maxv #define USBHCTYPE_OHCI 2
171 1.123 maxv #define USBHCTYPE_UHCI 3
172 1.123 maxv #define USBHCTYPE_EHCI 4
173 1.123 maxv #define USBHCTYPE_XHCI 5
174 1.123 maxv #define USBHCTYPE_VHCI 6
175 1.124 maxv int ub_busnum;
176 1.113 skrll const struct usbd_bus_methods
177 1.113 skrll *ub_methods;
178 1.113 skrll uint32_t ub_pipesize; /* size of a pipe struct */
179 1.113 skrll bool ub_usedma; /* Does this HC support DMA */
180 1.113 skrll int ub_dmaflags;
181 1.113 skrll bus_dma_tag_t ub_dmatag; /* DMA tag */
182 1.113 skrll
183 1.113 skrll /* Filled by usb driver */
184 1.113 skrll kmutex_t *ub_lock;
185 1.113 skrll struct usbd_device *ub_roothub;
186 1.137 riastrad struct usbd_xfer *ub_rhxfer; /* roothub xfer in progress */
187 1.137 riastrad kcondvar_t ub_rhxfercv;
188 1.113 skrll uint8_t ub_rhaddr; /* roothub address */
189 1.113 skrll uint8_t ub_rhconf; /* roothub configuration */
190 1.114 skrll struct usbd_device *ub_devices[USB_TOTAL_DEVICES];
191 1.113 skrll kcondvar_t ub_needsexplore_cv;
192 1.113 skrll char ub_needsexplore;/* a hub a signalled a change */
193 1.113 skrll char ub_usepolling;
194 1.113 skrll device_t ub_usbctl;
195 1.113 skrll struct usb_device_stats ub_stats;
196 1.113 skrll
197 1.113 skrll void *ub_soft; /* soft interrupt cookie */
198 1.1 augustss };
199 1.1 augustss
200 1.1 augustss struct usbd_device {
201 1.113 skrll struct usbd_bus *ud_bus; /* our controller */
202 1.113 skrll struct usbd_pipe *ud_pipe0; /* pipe 0 */
203 1.129 andvar uint8_t ud_addr; /* device address */
204 1.113 skrll uint8_t ud_config; /* current configuration # */
205 1.113 skrll uint8_t ud_depth; /* distance from root hub */
206 1.113 skrll uint8_t ud_speed; /* low/full/high speed */
207 1.113 skrll uint8_t ud_selfpowered; /* flag for self powered */
208 1.113 skrll uint16_t ud_power; /* mA the device uses */
209 1.113 skrll int16_t ud_langid; /* language for strings */
210 1.13 augustss #define USBD_NOLANG (-1)
211 1.113 skrll usb_event_cookie_t ud_cookie; /* unique connection id */
212 1.113 skrll struct usbd_port *ud_powersrc; /* upstream hub port, or 0 */
213 1.113 skrll struct usbd_device *ud_myhub; /* upstream hub */
214 1.113 skrll struct usbd_port *ud_myhsport; /* closest high speed port */
215 1.113 skrll struct usbd_endpoint ud_ep0; /* for pipe 0 */
216 1.113 skrll usb_endpoint_descriptor_t
217 1.113 skrll ud_ep0desc; /* for pipe 0 */
218 1.113 skrll struct usbd_interface *ud_ifaces; /* array of all interfaces */
219 1.113 skrll usb_device_descriptor_t ud_ddesc; /* device descriptor */
220 1.113 skrll usb_config_descriptor_t *ud_cdesc; /* full config descr */
221 1.113 skrll usb_bos_descriptor_t *ud_bdesc; /* full BOS descr */
222 1.113 skrll const struct usbd_quirks
223 1.113 skrll *ud_quirks; /* device quirks, always set */
224 1.113 skrll struct usbd_hub *ud_hub; /* only if this is a hub */
225 1.119 christos u_int ud_subdevlen; /* array length of following */
226 1.113 skrll device_t *ud_subdevs; /* sub-devices */
227 1.113 skrll int ud_nifaces_claimed; /* number of ifaces in use */
228 1.113 skrll void *ud_hcpriv;
229 1.111 skrll
230 1.111 skrll char *ud_serial; /* serial number, can be NULL */
231 1.111 skrll char *ud_vendor; /* vendor string, can be NULL */
232 1.111 skrll char *ud_product; /* product string can be NULL */
233 1.1 augustss };
234 1.1 augustss
235 1.1 augustss struct usbd_interface {
236 1.113 skrll struct usbd_device *ui_dev;
237 1.113 skrll usb_interface_descriptor_t
238 1.113 skrll *ui_idesc;
239 1.113 skrll int ui_index;
240 1.113 skrll int ui_altindex;
241 1.113 skrll struct usbd_endpoint *ui_endpoints;
242 1.128 riastrad int64_t ui_busy; /* #pipes, or -1 if setting */
243 1.1 augustss };
244 1.1 augustss
245 1.1 augustss struct usbd_pipe {
246 1.113 skrll struct usbd_interface *up_iface;
247 1.113 skrll struct usbd_device *up_dev;
248 1.113 skrll struct usbd_endpoint *up_endpoint;
249 1.113 skrll char up_running;
250 1.113 skrll char up_aborting;
251 1.113 skrll bool up_serialise;
252 1.113 skrll SIMPLEQ_HEAD(, usbd_xfer)
253 1.113 skrll up_queue;
254 1.113 skrll struct usb_task up_async_task;
255 1.113 skrll
256 1.113 skrll struct usbd_xfer *up_intrxfer; /* used for repeating requests */
257 1.113 skrll char up_repeat;
258 1.113 skrll int up_interval;
259 1.113 skrll uint8_t up_flags;
260 1.52 augustss
261 1.130 riastrad struct usbd_xfer *up_callingxfer; /* currently in callback */
262 1.130 riastrad kcondvar_t up_callingcv;
263 1.130 riastrad
264 1.136 riastrad struct lwp *up_abortlwp; /* lwp currently aborting */
265 1.136 riastrad
266 1.1 augustss /* Filled by HC driver. */
267 1.113 skrll const struct usbd_pipe_methods
268 1.113 skrll *up_methods;
269 1.1 augustss };
270 1.1 augustss
271 1.40 augustss struct usbd_xfer {
272 1.113 skrll struct usbd_pipe *ux_pipe;
273 1.113 skrll void *ux_priv;
274 1.113 skrll void *ux_buffer;
275 1.113 skrll kcondvar_t ux_cv;
276 1.113 skrll uint32_t ux_length;
277 1.113 skrll uint32_t ux_actlen;
278 1.113 skrll uint16_t ux_flags;
279 1.113 skrll uint32_t ux_timeout;
280 1.113 skrll usbd_status ux_status;
281 1.113 skrll usbd_callback ux_callback;
282 1.113 skrll volatile uint8_t ux_done;
283 1.113 skrll uint8_t ux_state; /* used for DIAGNOSTIC */
284 1.77 chs #define XFER_FREE 0x46
285 1.77 chs #define XFER_BUSY 0x55
286 1.77 chs #define XFER_ONQU 0x9e
287 1.1 augustss
288 1.29 augustss /* For control pipe */
289 1.113 skrll usb_device_request_t ux_request;
290 1.29 augustss
291 1.29 augustss /* For isoc */
292 1.113 skrll uint16_t *ux_frlengths;
293 1.113 skrll int ux_nframes;
294 1.113 skrll
295 1.113 skrll const struct usbd_pipe_methods *ux_methods;
296 1.29 augustss
297 1.113 skrll /* For memory allocation and softc */
298 1.113 skrll struct usbd_bus *ux_bus;
299 1.113 skrll usb_dma_t ux_dmabuf;
300 1.113 skrll void *ux_buf;
301 1.113 skrll uint32_t ux_bufsize;
302 1.29 augustss
303 1.113 skrll uint8_t ux_rqflags;
304 1.29 augustss #define URQ_REQUEST 0x01
305 1.1 augustss
306 1.113 skrll SIMPLEQ_ENTRY(usbd_xfer)
307 1.113 skrll ux_next;
308 1.1 augustss
309 1.113 skrll void *ux_hcpriv; /* private use by the HC driver */
310 1.11 augustss
311 1.117 mrg struct usb_task ux_aborttask;
312 1.113 skrll struct callout ux_callout;
313 1.121 riastrad
314 1.121 riastrad /*
315 1.121 riastrad * Protected by bus lock.
316 1.121 riastrad *
317 1.121 riastrad * - ux_timeout_set: The timeout is scheduled as a callout or
318 1.121 riastrad * usb task, and has not yet acquired the bus lock.
319 1.121 riastrad *
320 1.121 riastrad * - ux_timeout_reset: The xfer completed, and was resubmitted
321 1.121 riastrad * before the callout or task was able to acquire the bus
322 1.121 riastrad * lock, so one or the other needs to schedule a new callout.
323 1.121 riastrad */
324 1.121 riastrad bool ux_timeout_set;
325 1.121 riastrad bool ux_timeout_reset;
326 1.1 augustss };
327 1.1 augustss
328 1.57 augustss void usbd_init(void);
329 1.57 augustss void usbd_finish(void);
330 1.64 augustss
331 1.113 skrll #if defined(USB_DEBUG)
332 1.113 skrll void usbd_dump_iface(struct usbd_interface *);
333 1.113 skrll void usbd_dump_device(struct usbd_device *);
334 1.113 skrll void usbd_dump_endpoint(struct usbd_endpoint *);
335 1.113 skrll void usbd_dump_queue(struct usbd_pipe *);
336 1.113 skrll void usbd_dump_pipe(struct usbd_pipe *);
337 1.67 augustss #endif
338 1.1 augustss
339 1.1 augustss /* Routines from usb_subr.c */
340 1.57 augustss int usbctlprint(void *, const char *);
341 1.111 skrll void usbd_get_device_strings(struct usbd_device *);
342 1.113 skrll void usb_delay_ms_locked(struct usbd_bus *, u_int, kmutex_t *);
343 1.113 skrll void usb_delay_ms(struct usbd_bus *, u_int);
344 1.113 skrll void usbd_delay_ms_locked(struct usbd_device *, u_int, kmutex_t *);
345 1.113 skrll void usbd_delay_ms(struct usbd_device *, u_int);
346 1.113 skrll usbd_status usbd_reset_port(struct usbd_device *, int, usb_port_status_t *);
347 1.113 skrll usbd_status usbd_setup_pipe(struct usbd_device *,
348 1.113 skrll struct usbd_interface *,
349 1.57 augustss struct usbd_endpoint *, int,
350 1.113 skrll struct usbd_pipe **);
351 1.113 skrll usbd_status usbd_setup_pipe_flags(struct usbd_device *,
352 1.113 skrll struct usbd_interface *,
353 1.101 jmcneill struct usbd_endpoint *, int,
354 1.113 skrll struct usbd_pipe **,
355 1.113 skrll uint8_t);
356 1.113 skrll usbd_status usbd_new_device(device_t, struct usbd_bus *, int, int, int,
357 1.102 skrll struct usbd_port *);
358 1.113 skrll usbd_status usbd_reattach_device(device_t, struct usbd_device *,
359 1.102 skrll int, const int *);
360 1.88 kent
361 1.113 skrll void usbd_remove_device(struct usbd_device *, struct usbd_port *);
362 1.128 riastrad bool usbd_iface_locked(struct usbd_interface *);
363 1.128 riastrad usbd_status usbd_iface_lock(struct usbd_interface *);
364 1.128 riastrad void usbd_iface_unlock(struct usbd_interface *);
365 1.128 riastrad usbd_status usbd_iface_piperef(struct usbd_interface *);
366 1.128 riastrad void usbd_iface_pipeunref(struct usbd_interface *);
367 1.113 skrll usbd_status usbd_fill_iface_data(struct usbd_device *, int, int);
368 1.113 skrll void usb_free_device(struct usbd_device *);
369 1.57 augustss
370 1.113 skrll void usb_transfer_complete(struct usbd_xfer *);
371 1.91 dyoung int usb_disconnect_port(struct usbd_port *, device_t, int);
372 1.1 augustss
373 1.125 riastrad usbd_status usbd_endpoint_acquire(struct usbd_device *,
374 1.125 riastrad struct usbd_endpoint *, int);
375 1.125 riastrad void usbd_endpoint_release(struct usbd_device *,
376 1.125 riastrad struct usbd_endpoint *);
377 1.125 riastrad
378 1.113 skrll void usbd_kill_pipe(struct usbd_pipe *);
379 1.113 skrll usbd_status usbd_attach_roothub(device_t, struct usbd_device *);
380 1.113 skrll usbd_status usbd_probe_and_attach(device_t, struct usbd_device *, int, int);
381 1.105 jakllsch
382 1.1 augustss /* Routines from usb.c */
383 1.113 skrll void usb_needs_explore(struct usbd_device *);
384 1.113 skrll void usb_needs_reattach(struct usbd_device *);
385 1.57 augustss void usb_schedsoftintr(struct usbd_bus *);
386 1.97 mrg
387 1.115 christos static __inline int
388 1.110 skrll usbd_xfer_isread(struct usbd_xfer *xfer)
389 1.110 skrll {
390 1.113 skrll if (xfer->ux_rqflags & URQ_REQUEST)
391 1.113 skrll return xfer->ux_request.bmRequestType & UT_READ;
392 1.110 skrll
393 1.113 skrll return xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress &
394 1.113 skrll UE_DIR_IN;
395 1.110 skrll }
396 1.110 skrll
397 1.115 christos static __inline size_t
398 1.114 skrll usb_addr2dindex(int addr)
399 1.114 skrll {
400 1.114 skrll
401 1.114 skrll return USB_ROOTHUB_INDEX + addr;
402 1.114 skrll }
403 1.114 skrll
404 1.97 mrg /*
405 1.107 skrll * These macros reflect the current locking scheme. They might change.
406 1.97 mrg */
407 1.97 mrg
408 1.113 skrll #define usbd_lock_pipe(p) mutex_enter((p)->up_dev->ud_bus->ub_lock)
409 1.113 skrll #define usbd_unlock_pipe(p) mutex_exit((p)->up_dev->ud_bus->ub_lock)
410 1.131 riastrad
411 1.131 riastrad #endif /* _DEV_USB_USBDIVAR_H_ */
412