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