1 /* 2 * Copyright 2022, Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT license. 4 */ 5 /*- 6 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 7 * 8 * Copyright (c) 2009 Andrew Thompson 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 #ifndef _FBSD_COMPAT_USB_USBDI_H_ 31 #define _FBSD_COMPAT_USB_USBDI_H_ 32 33 #define usb_device freebsd_usb_device 34 #define usb_interface freebsd_usb_interface 35 36 struct usb_device; 37 struct usb_interface; 38 struct usb_xfer; 39 struct usb_attach_arg; 40 struct usb_endpoint; 41 struct usb_page_cache; 42 struct usb_page_search; 43 struct usb_process; 44 struct usb_mbuf; 45 struct usb_fs_privdata; 46 struct mbuf; 47 48 typedef enum { /* keep in sync with usb_errstr_table */ 49 USB_ERR_NORMAL_COMPLETION = 0, 50 USB_ERR_PENDING_REQUESTS, /* 1 */ 51 USB_ERR_NOT_STARTED, /* 2 */ 52 USB_ERR_INVAL, /* 3 */ 53 USB_ERR_NOMEM, /* 4 */ 54 USB_ERR_CANCELLED, /* 5 */ 55 USB_ERR_BAD_ADDRESS, /* 6 */ 56 USB_ERR_BAD_BUFSIZE, /* 7 */ 57 USB_ERR_BAD_FLAG, /* 8 */ 58 USB_ERR_NO_CALLBACK, /* 9 */ 59 USB_ERR_IN_USE, /* 10 */ 60 USB_ERR_NO_ADDR, /* 11 */ 61 USB_ERR_NO_PIPE, /* 12 */ 62 USB_ERR_ZERO_NFRAMES, /* 13 */ 63 USB_ERR_ZERO_MAXP, /* 14 */ 64 USB_ERR_SET_ADDR_FAILED, /* 15 */ 65 USB_ERR_NO_POWER, /* 16 */ 66 USB_ERR_TOO_DEEP, /* 17 */ 67 USB_ERR_IOERROR, /* 18 */ 68 USB_ERR_NOT_CONFIGURED, /* 19 */ 69 USB_ERR_TIMEOUT, /* 20 */ 70 USB_ERR_SHORT_XFER, /* 21 */ 71 USB_ERR_STALLED, /* 22 */ 72 USB_ERR_INTERRUPTED, /* 23 */ 73 USB_ERR_DMA_LOAD_FAILED, /* 24 */ 74 USB_ERR_BAD_CONTEXT, /* 25 */ 75 USB_ERR_NO_ROOT_HUB, /* 26 */ 76 USB_ERR_NO_INTR_THREAD, /* 27 */ 77 USB_ERR_NOT_LOCKED, /* 28 */ 78 USB_ERR_MAX 79 } usb_error_t; 80 81 /* 82 * Flags for transfers 83 */ 84 #define USB_FORCE_SHORT_XFER 0x0001 /* force a short transmit last */ 85 #define USB_SHORT_XFER_OK 0x0004 /* allow short reads */ 86 #define USB_DELAY_STATUS_STAGE 0x0010 /* insert delay before STATUS stage */ 87 #define USB_USER_DATA_PTR 0x0020 /* internal flag */ 88 #define USB_MULTI_SHORT_OK 0x0040 /* allow multiple short frames */ 89 #define USB_MANUAL_STATUS 0x0080 /* manual ctrl status */ 90 91 #define USB_NO_TIMEOUT 0 92 #define USB_DEFAULT_TIMEOUT 5000 /* 5000 ms = 5 seconds */ 93 94 #if defined(_KERNEL) 95 /* typedefs */ 96 97 typedef void (usb_callback_t)(struct usb_xfer *, usb_error_t); 98 99 /* 100 * The following macros are used used to convert milliseconds into 101 * HZ. We use 1024 instead of 1000 milliseconds per second to save a 102 * full division. 103 */ 104 #define USB_MS_HZ 1024 105 106 #define USB_MS_TO_TICKS(ms) \ 107 (((uint32_t)((((uint32_t)(ms)) * ((uint32_t)(hz))) + USB_MS_HZ - 1)) / USB_MS_HZ) 108 109 /* 110 * The following structure defines a USB endpoint. 111 */ 112 struct usb_endpoint { 113 struct usb_endpoint_descriptor* edesc; 114 uint8_t iface_index; /* not used by "default endpoint" */ 115 }; 116 117 /* 118 * The following structure defines a set of USB transfer flags. 119 */ 120 struct usb_xfer_flags { 121 uint8_t force_short_xfer:1; 122 /* force a short transmit transfer last */ 123 uint8_t short_xfer_ok:1; /* allow short receive transfers */ 124 uint8_t short_frames_ok:1; /* allow short frames */ 125 uint8_t pipe_bof:1; /* block pipe on failure */ 126 uint8_t proxy_buffer:1; 127 /* makes buffer size a factor of "max_frame_size" */ 128 uint8_t ext_buffer:1; /* uses external DMA buffer */ 129 uint8_t manual_status:1; 130 /* non automatic status stage on control transfers */ 131 uint8_t no_pipe_ok:1; 132 /* set if "USB_ERR_NO_PIPE" error can be ignored */ 133 uint8_t stall_pipe:1; 134 /* set if the endpoint belonging to this USB transfer 135 * should be stalled before starting this transfer! */ 136 }; 137 138 struct usb_config { 139 usb_callback_t *callback; /* USB transfer callback */ 140 usb_frlength_t bufsize; /* total pipe buffer size in bytes */ 141 usb_frcount_t frames; /* maximum number of USB frames */ 142 usb_timeout_t interval; /* interval in milliseconds */ 143 #define USB_DEFAULT_INTERVAL 0 144 usb_timeout_t timeout; /* transfer timeout in milliseconds */ 145 struct usb_xfer_flags flags; /* transfer flags */ 146 usb_stream_t stream_id; /* USB3.0 specific */ 147 enum usb_hc_mode usb_mode; /* host or device mode */ 148 uint8_t type; /* pipe type */ 149 uint8_t endpoint; /* pipe number */ 150 uint8_t direction; /* pipe direction */ 151 uint8_t ep_index; /* pipe index match to use */ 152 uint8_t if_index; /* "ifaces" index to use */ 153 }; 154 155 #if USB_HAVE_ID_SECTION 156 #define STRUCT_USB_HOST_ID \ 157 struct usb_device_id __section("usb_host_id") 158 #define STRUCT_USB_DEVICE_ID \ 159 struct usb_device_id __section("usb_device_id") 160 #define STRUCT_USB_DUAL_ID \ 161 struct usb_device_id __section("usb_dual_id") 162 #else 163 #define STRUCT_USB_HOST_ID \ 164 struct usb_device_id 165 #define STRUCT_USB_DEVICE_ID \ 166 struct usb_device_id 167 #define STRUCT_USB_DUAL_ID \ 168 struct usb_device_id 169 #endif /* USB_HAVE_ID_SECTION */ 170 171 struct usb_device_id { 172 /* Select which fields to match against */ 173 #if BYTE_ORDER == LITTLE_ENDIAN 174 uint16_t 175 match_flag_vendor:1, 176 match_flag_product:1, 177 match_flag_dev_lo:1, 178 match_flag_dev_hi:1, 179 180 match_flag_dev_class:1, 181 match_flag_dev_subclass:1, 182 match_flag_dev_protocol:1, 183 match_flag_int_class:1, 184 185 match_flag_int_subclass:1, 186 match_flag_int_protocol:1, 187 match_flag_unused:6; 188 #else 189 uint16_t 190 match_flag_unused:6, 191 match_flag_int_protocol:1, 192 match_flag_int_subclass:1, 193 194 match_flag_int_class:1, 195 match_flag_dev_protocol:1, 196 match_flag_dev_subclass:1, 197 match_flag_dev_class:1, 198 199 match_flag_dev_hi:1, 200 match_flag_dev_lo:1, 201 match_flag_product:1, 202 match_flag_vendor:1; 203 #endif 204 205 /* Used for product specific matches; the BCD range is inclusive */ 206 uint16_t idVendor; 207 uint16_t idProduct; 208 uint16_t bcdDevice_lo; 209 uint16_t bcdDevice_hi; 210 211 /* Used for device class matches */ 212 uint8_t bDeviceClass; 213 uint8_t bDeviceSubClass; 214 uint8_t bDeviceProtocol; 215 216 /* Used for interface class matches */ 217 uint8_t bInterfaceClass; 218 uint8_t bInterfaceSubClass; 219 uint8_t bInterfaceProtocol; 220 221 /* Hook for driver specific information */ 222 unsigned long driver_info; 223 } __aligned(32); 224 225 #define USB_PNP_HOST_INFO(table) 226 #define USB_PNP_DEVICE_INFO(table) 227 #define USB_PNP_DUAL_INFO(table) 228 229 /* check that the size of the structure above is correct */ 230 extern char usb_device_id_assert[(sizeof(struct usb_device_id) == 32) ? 1 : -1]; 231 232 #define USB_VENDOR(vend) \ 233 .match_flag_vendor = 1, .idVendor = (vend) 234 235 #define USB_PRODUCT(prod) \ 236 .match_flag_product = 1, .idProduct = (prod) 237 238 #define USB_VP(vend,prod) \ 239 USB_VENDOR(vend), USB_PRODUCT(prod) 240 241 #define USB_VPI(vend,prod,info) \ 242 USB_VENDOR(vend), USB_PRODUCT(prod), USB_DRIVER_INFO(info) 243 244 #define USB_DEV_BCD_GTEQ(lo) /* greater than or equal */ \ 245 .match_flag_dev_lo = 1, .bcdDevice_lo = (lo) 246 247 #define USB_DEV_BCD_LTEQ(hi) /* less than or equal */ \ 248 .match_flag_dev_hi = 1, .bcdDevice_hi = (hi) 249 250 #define USB_DEV_CLASS(dc) \ 251 .match_flag_dev_class = 1, .bDeviceClass = (dc) 252 253 #define USB_DEV_SUBCLASS(dsc) \ 254 .match_flag_dev_subclass = 1, .bDeviceSubClass = (dsc) 255 256 #define USB_DEV_PROTOCOL(dp) \ 257 .match_flag_dev_protocol = 1, .bDeviceProtocol = (dp) 258 259 #define USB_IFACE_CLASS(ic) \ 260 .match_flag_int_class = 1, .bInterfaceClass = (ic) 261 262 #define USB_IFACE_SUBCLASS(isc) \ 263 .match_flag_int_subclass = 1, .bInterfaceSubClass = (isc) 264 265 #define USB_IFACE_PROTOCOL(ip) \ 266 .match_flag_int_protocol = 1, .bInterfaceProtocol = (ip) 267 268 #define USB_IF_CSI(class,subclass,info) \ 269 USB_IFACE_CLASS(class), USB_IFACE_SUBCLASS(subclass), USB_DRIVER_INFO(info) 270 271 #define USB_DRIVER_INFO(n) \ 272 .driver_info = (n) 273 274 #define USB_GET_DRIVER_INFO(did) \ 275 (did)->driver_info 276 277 /* 278 * The following structure keeps information that is used to match 279 * against an array of "usb_device_id" elements. 280 */ 281 struct usbd_lookup_info { 282 uint16_t idVendor; 283 uint16_t idProduct; 284 uint16_t bcdDevice; 285 uint8_t bDeviceClass; 286 uint8_t bDeviceSubClass; 287 uint8_t bDeviceProtocol; 288 uint8_t bInterfaceClass; 289 uint8_t bInterfaceSubClass; 290 uint8_t bInterfaceProtocol; 291 uint8_t bIfaceIndex; 292 uint8_t bIfaceNum; 293 uint8_t bConfigIndex; 294 uint8_t bConfigNum; 295 }; 296 297 /* Structure used by probe and attach */ 298 struct usb_attach_arg { 299 struct usbd_lookup_info info; 300 device_t temp_dev; /* for internal use */ 301 unsigned long driver_info; /* for internal use */ 302 void *driver_ivar; 303 struct usb_device *device; /* current device */ 304 struct usb_interface *iface; /* current interface */ 305 enum usb_hc_mode usb_mode; /* host or device mode */ 306 uint8_t port; 307 uint8_t dev_state; 308 #define UAA_DEV_READY 0 309 #define UAA_DEV_DISABLED 1 310 #define UAA_DEV_EJECTING 2 311 }; 312 313 /* 314 * The following is a wrapper for the callout structure to ease 315 * porting the code to other platforms. 316 */ 317 struct usb_callout { 318 struct callout co; 319 }; 320 #define usb_callout_init_mtx(c,m,f) callout_init_mtx(&(c)->co,m,f) 321 #define usb_callout_reset(c,t,f,d) callout_reset(&(c)->co,t,f,d) 322 #define usb_callout_stop(c) callout_stop(&(c)->co) 323 #define usb_callout_drain(c) callout_drain(&(c)->co) 324 #define usb_callout_pending(c) callout_pending(&(c)->co) 325 326 /* USB transfer states */ 327 #define USB_ST_SETUP 0 328 #define USB_ST_TRANSFERRED 1 329 #define USB_ST_ERROR 2 330 331 /* 332 * The following macro will return the current state of an USB 333 * transfer like defined by the "USB_ST_XXX" enums. 334 */ 335 #define USB_GET_STATE(xfer) (usbd_xfer_state(xfer)) 336 337 int usbd_lookup_id_by_uaa(const struct usb_device_id *id, 338 usb_size_t sizeof_id, struct usb_attach_arg *uaa); 339 340 void device_set_usb_desc(device_t dev); 341 342 const char *usbd_errstr(usb_error_t error); 343 void usb_pause_mtx(struct mtx *mtx, int _ticks); 344 345 usb_error_t usbd_do_request_flags(struct usb_device* udev, struct mtx *mtx, 346 struct usb_device_request* req, void* data, uint16_t flags, 347 uint16_t *actlen, usb_timeout_t timeout); 348 #define usbd_do_request(u,m,r,d) \ 349 usbd_do_request_flags(u,m,r,d,0,NULL,USB_DEFAULT_TIMEOUT) 350 351 void usb_pause_mtx(struct mtx *mtx, int _ticks); 352 353 enum usb_dev_speed usbd_get_speed(struct usb_device* udev); 354 355 void* usbd_xfer_softc(struct usb_xfer *xfer); 356 void* usbd_xfer_get_priv(struct usb_xfer* xfer); 357 void usbd_xfer_set_priv(struct usb_xfer* xfer, void* ptr); 358 uint8_t usbd_xfer_state(struct usb_xfer *xfer); 359 usb_frlength_t usbd_xfer_max_len(struct usb_xfer *xfer); 360 struct usb_page_cache *usbd_xfer_get_frame(struct usb_xfer *, usb_frcount_t); 361 void usbd_xfer_set_frames(struct usb_xfer *xfer, usb_frcount_t n); 362 void usbd_xfer_set_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex, 363 void *ptr, usb_frlength_t len); 364 void usbd_xfer_set_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex, 365 usb_frlength_t len); 366 void usbd_xfer_set_stall(struct usb_xfer *xfer); 367 void usbd_xfer_status(struct usb_xfer *xfer, int *actlen, int *sumlen, 368 int *aframes, int *nframes); 369 370 void usbd_frame_zero(struct usb_page_cache *cache, usb_frlength_t offset, 371 usb_frlength_t len); 372 void usbd_copy_in(struct usb_page_cache *cache, usb_frlength_t offset, 373 const void *ptr, usb_frlength_t len); 374 void usbd_copy_out(struct usb_page_cache *cache, usb_frlength_t offset, 375 void *ptr, usb_frlength_t len); 376 void usbd_m_copy_in(struct usb_page_cache *cache, usb_frlength_t dst_offset, 377 struct mbuf *m, usb_size_t src_offset, usb_frlength_t src_len); 378 379 void usbd_transfer_submit(struct usb_xfer *xfer); 380 void usbd_transfer_start(struct usb_xfer *xfer); 381 void usbd_transfer_stop(struct usb_xfer *xfer); 382 void usbd_transfer_drain(struct usb_xfer* xfer); 383 usb_error_t usbd_transfer_setup(struct usb_device *udev, 384 const uint8_t *ifaces, struct usb_xfer **pxfer, 385 const struct usb_config *setup_start, uint16_t n_setup, 386 void *priv_sc, struct mtx *priv_mtx); 387 void usbd_transfer_unsetup(struct usb_xfer **pxfer, uint16_t n_setup); 388 389 #endif /* _KERNEL */ 390 #endif /* _FBSD_COMPAT_USB_USBDI_H_ */ 391