1 /* $NetBSD: usbdivar.h,v 1.140 2025/10/05 20:04:30 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 * ud_config above holds a value of bConfigurationValue from 236 * the config descriptor, or USB_UNCONFIG_NO=0 -- which may 237 * _also_ be a value of bConfigurationValue. 238 * 239 * ud_configidx below holds an index in [0, bNumConfigurations) 240 * into the list of configuration descriptors, or 241 * USB_UNCONFIG_INDEX=-1 to denote that the interface is 242 * unconfigured. Note that ud_config may be USB_UNCONFIG_NO 243 * even if ud_configidx is not USB_UNCONFIG_INDEX, if a screwy 244 * device has a config descriptor with bConfigurationValue=0. 245 * 246 * This goes at the end, rather than next to ud_config where it 247 * might properly belong, so the change preserves ABI for 248 * pullup to release branches. 249 */ 250 int16_t ud_configidx; 251 }; 252 253 struct usbd_interface { 254 struct usbd_device *ui_dev; 255 usb_interface_descriptor_t 256 *ui_idesc; 257 int ui_index; 258 int ui_altindex; 259 struct usbd_endpoint *ui_endpoints; 260 int64_t ui_busy; /* #pipes, or -1 if setting */ 261 }; 262 263 struct usbd_pipe { 264 struct usbd_interface *up_iface; 265 struct usbd_device *up_dev; 266 struct usbd_endpoint *up_endpoint; 267 char up_running; 268 char up_aborting; 269 bool up_serialise; 270 SIMPLEQ_HEAD(, usbd_xfer) 271 up_queue; 272 struct usb_task up_async_task; 273 274 struct usbd_xfer *up_intrxfer; /* used for repeating requests */ 275 char up_repeat; 276 int up_interval; 277 uint8_t up_flags; 278 279 struct usbd_xfer *up_callingxfer; /* currently in callback */ 280 kcondvar_t up_callingcv; 281 282 struct lwp *up_abortlwp; /* lwp currently aborting */ 283 284 /* Filled by HC driver. */ 285 const struct usbd_pipe_methods 286 *up_methods; 287 }; 288 289 struct usbd_xfer { 290 struct usbd_pipe *ux_pipe; 291 void *ux_priv; 292 void *ux_buffer; 293 kcondvar_t ux_cv; 294 uint32_t ux_length; 295 uint32_t ux_actlen; 296 uint16_t ux_flags; 297 uint32_t ux_timeout; 298 usbd_status ux_status; 299 usbd_callback ux_callback; 300 volatile uint8_t ux_done; 301 uint8_t ux_state; /* used for DIAGNOSTIC */ 302 #define XFER_FREE 0x46 303 #define XFER_BUSY 0x55 304 #define XFER_ONQU 0x9e 305 306 /* For control pipe */ 307 usb_device_request_t ux_request; 308 309 /* For isoc */ 310 uint16_t *ux_frlengths; 311 int ux_nframes; 312 313 const struct usbd_pipe_methods *ux_methods; 314 315 /* For memory allocation and softc */ 316 struct usbd_bus *ux_bus; 317 usb_dma_t ux_dmabuf; 318 void *ux_buf; 319 uint32_t ux_bufsize; 320 321 uint8_t ux_rqflags; 322 #define URQ_REQUEST 0x01 323 324 SIMPLEQ_ENTRY(usbd_xfer) 325 ux_next; 326 327 void *ux_hcpriv; /* private use by the HC driver */ 328 329 struct usb_task ux_aborttask; 330 struct callout ux_callout; 331 332 /* 333 * Protected by bus lock. 334 * 335 * - ux_timeout_set: The timeout is scheduled as a callout or 336 * usb task, and has not yet acquired the bus lock. 337 * 338 * - ux_timeout_reset: The xfer completed, and was resubmitted 339 * before the callout or task was able to acquire the bus 340 * lock, so one or the other needs to schedule a new callout. 341 */ 342 bool ux_timeout_set; 343 bool ux_timeout_reset; 344 }; 345 346 void usbd_init(void); 347 void usbd_finish(void); 348 349 #if defined(USB_DEBUG) 350 void usbd_dump_iface(struct usbd_interface *); 351 void usbd_dump_device(struct usbd_device *); 352 void usbd_dump_endpoint(struct usbd_endpoint *); 353 void usbd_dump_queue(struct usbd_pipe *); 354 void usbd_dump_pipe(struct usbd_pipe *); 355 #endif 356 357 /* Routines from usb_subr.c */ 358 int usbctlprint(void *, const char *); 359 void usbd_get_device_strings(struct usbd_device *); 360 void usb_delay_ms_locked(struct usbd_bus *, u_int, kmutex_t *); 361 void usb_delay_ms(struct usbd_bus *, u_int); 362 void usbd_delay_ms_locked(struct usbd_device *, u_int, kmutex_t *); 363 void usbd_delay_ms(struct usbd_device *, u_int); 364 usbd_status usbd_reset_port(struct usbd_device *, int, usb_port_status_t *); 365 usbd_status usbd_setup_pipe(struct usbd_device *, 366 struct usbd_interface *, 367 struct usbd_endpoint *, int, 368 struct usbd_pipe **); 369 usbd_status usbd_setup_pipe_flags(struct usbd_device *, 370 struct usbd_interface *, 371 struct usbd_endpoint *, int, 372 struct usbd_pipe **, 373 uint8_t); 374 usbd_status usbd_new_device(device_t, struct usbd_bus *, int, int, int, 375 struct usbd_port *); 376 usbd_status usbd_reattach_device(device_t, struct usbd_device *, 377 int, const int *); 378 379 void usbd_remove_device(struct usbd_device *, struct usbd_port *); 380 bool usbd_iface_locked(struct usbd_interface *); 381 usbd_status usbd_iface_lock(struct usbd_interface *); 382 void usbd_iface_unlock(struct usbd_interface *); 383 usbd_status usbd_iface_piperef(struct usbd_interface *); 384 void usbd_iface_pipeunref(struct usbd_interface *); 385 usbd_status usbd_fill_iface_data(struct usbd_device *, int, int); 386 void usb_free_device(struct usbd_device *); 387 388 void usb_transfer_complete(struct usbd_xfer *); 389 int usb_disconnect_port(struct usbd_port *, device_t, int); 390 391 usbd_status usbd_endpoint_acquire(struct usbd_device *, 392 struct usbd_endpoint *, int); 393 void usbd_endpoint_release(struct usbd_device *, 394 struct usbd_endpoint *); 395 396 void usbd_kill_pipe(struct usbd_pipe *); 397 usbd_status usbd_attach_roothub(device_t, struct usbd_device *); 398 usbd_status usbd_probe_and_attach(device_t, struct usbd_device *, int, int); 399 400 /* Routines from usb.c */ 401 void usb_needs_explore(struct usbd_device *); 402 void usb_needs_reattach(struct usbd_device *); 403 void usb_schedsoftintr(struct usbd_bus *); 404 405 static __inline int 406 usbd_xfer_isread(struct usbd_xfer *xfer) 407 { 408 if (xfer->ux_rqflags & URQ_REQUEST) 409 return xfer->ux_request.bmRequestType & UT_READ; 410 411 return xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress & 412 UE_DIR_IN; 413 } 414 415 static __inline size_t 416 usb_addr2dindex(int addr) 417 { 418 419 return USB_ROOTHUB_INDEX + addr; 420 } 421 422 /* 423 * These macros reflect the current locking scheme. They might change. 424 */ 425 426 #define usbd_lock_pipe(p) mutex_enter((p)->up_dev->ud_bus->ub_lock) 427 #define usbd_unlock_pipe(p) mutex_exit((p)->up_dev->ud_bus->ub_lock) 428 429 #endif /* _DEV_USB_USBDIVAR_H_ */ 430