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