1 1.108 riastrad /* $NetBSD: usbdi.h,v 1.108 2022/08/20 11:32:20 riastradh Exp $ */ 2 1.34 augustss /* $FreeBSD: src/sys/dev/usb/usbdi.h,v 1.18 1999/11/17 22:33:49 n_hibma Exp $ */ 3 1.1 augustss 4 1.1 augustss /* 5 1.1 augustss * Copyright (c) 1998 The NetBSD Foundation, Inc. 6 1.1 augustss * All rights reserved. 7 1.1 augustss * 8 1.7 augustss * This code is derived from software contributed to The NetBSD Foundation 9 1.42 augustss * by Lennart Augustsson (lennart (at) augustsson.net) at 10 1.7 augustss * Carlstedt Research & Technology. 11 1.1 augustss * 12 1.1 augustss * Redistribution and use in source and binary forms, with or without 13 1.1 augustss * modification, are permitted provided that the following conditions 14 1.1 augustss * are met: 15 1.1 augustss * 1. Redistributions of source code must retain the above copyright 16 1.1 augustss * notice, this list of conditions and the following disclaimer. 17 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright 18 1.1 augustss * notice, this list of conditions and the following disclaimer in the 19 1.1 augustss * documentation and/or other materials provided with the distribution. 20 1.1 augustss * 21 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 1.1 augustss * POSSIBILITY OF SUCH DAMAGE. 32 1.1 augustss */ 33 1.1 augustss 34 1.74 drochner #ifndef _USBDI_H_ 35 1.74 drochner #define _USBDI_H_ 36 1.74 drochner 37 1.104 riastrad #include <sys/types.h> 38 1.104 riastrad 39 1.104 riastrad #include <dev/usb/usb.h> 40 1.104 riastrad 41 1.91 skrll struct usbd_bus; 42 1.91 skrll struct usbd_device; 43 1.91 skrll struct usbd_interface; 44 1.91 skrll struct usbd_pipe; 45 1.91 skrll struct usbd_xfer; 46 1.1 augustss 47 1.82 jakllsch typedef enum { /* keep in sync with usbd_error_strs */ 48 1.23 augustss USBD_NORMAL_COMPLETION = 0, /* must be 0 */ 49 1.58 augustss USBD_IN_PROGRESS, /* 1 */ 50 1.1 augustss /* errors */ 51 1.58 augustss USBD_PENDING_REQUESTS, /* 2 */ 52 1.58 augustss USBD_NOT_STARTED, /* 3 */ 53 1.58 augustss USBD_INVAL, /* 4 */ 54 1.58 augustss USBD_NOMEM, /* 5 */ 55 1.58 augustss USBD_CANCELLED, /* 6 */ 56 1.58 augustss USBD_BAD_ADDRESS, /* 7 */ 57 1.58 augustss USBD_IN_USE, /* 8 */ 58 1.58 augustss USBD_NO_ADDR, /* 9 */ 59 1.58 augustss USBD_SET_ADDR_FAILED, /* 10 */ 60 1.58 augustss USBD_NO_POWER, /* 11 */ 61 1.58 augustss USBD_TOO_DEEP, /* 12 */ 62 1.58 augustss USBD_IOERROR, /* 13 */ 63 1.58 augustss USBD_NOT_CONFIGURED, /* 14 */ 64 1.58 augustss USBD_TIMEOUT, /* 15 */ 65 1.58 augustss USBD_SHORT_XFER, /* 16 */ 66 1.58 augustss USBD_STALLED, /* 17 */ 67 1.58 augustss USBD_INTERRUPTED, /* 18 */ 68 1.1 augustss 69 1.52 lukem USBD_ERROR_MAX /* must be last */ 70 1.1 augustss } usbd_status; 71 1.1 augustss 72 1.91 skrll typedef void (*usbd_callback)(struct usbd_xfer *, void *, usbd_status); 73 1.91 skrll 74 1.91 skrll /* Use default (specified by ep. desc.) interval on interrupt pipe */ 75 1.91 skrll #define USBD_DEFAULT_INTERVAL (-1) 76 1.1 augustss 77 1.1 augustss /* Open flags */ 78 1.1 augustss #define USBD_EXCLUSIVE_USE 0x01 79 1.87 jmcneill #define USBD_MPSAFE 0x80 80 1.1 augustss 81 1.1 augustss /* Request flags */ 82 1.29 augustss #define USBD_SYNCHRONOUS 0x02 /* wait for completion */ 83 1.28 augustss /* in usb.h #define USBD_SHORT_XFER_OK 0x04*/ /* allow short reads */ 84 1.37 augustss #define USBD_FORCE_SHORT_XFER 0x08 /* force last short packet on write */ 85 1.83 mrg #define USBD_SYNCHRONOUS_SIG 0x10 /* if waiting for completion, 86 1.83 mrg * also take signals */ 87 1.1 augustss 88 1.1 augustss #define USBD_NO_TIMEOUT 0 89 1.1 augustss #define USBD_DEFAULT_TIMEOUT 5000 /* ms = 5 s */ 90 1.78 martin #define USBD_CONFIG_TIMEOUT (3*USBD_DEFAULT_TIMEOUT) 91 1.1 augustss 92 1.66 soren #define DEVINFOSIZE 1024 93 1.66 soren 94 1.91 skrll usbd_status usbd_open_pipe_intr(struct usbd_interface *, uint8_t, uint8_t, 95 1.91 skrll struct usbd_pipe **, void *, void *, uint32_t, usbd_callback, int); 96 1.91 skrll usbd_status usbd_open_pipe(struct usbd_interface *, uint8_t, uint8_t, 97 1.91 skrll struct usbd_pipe **); 98 1.106 riastrad void usbd_close_pipe(struct usbd_pipe *); 99 1.91 skrll 100 1.91 skrll usbd_status usbd_transfer(struct usbd_xfer *); 101 1.91 skrll 102 1.91 skrll void *usbd_get_buffer(struct usbd_xfer *); 103 1.91 skrll struct usbd_pipe *usbd_get_pipe0(struct usbd_device *); 104 1.91 skrll 105 1.91 skrll int usbd_create_xfer(struct usbd_pipe *, size_t, unsigned int, unsigned int, 106 1.91 skrll struct usbd_xfer **); 107 1.91 skrll void usbd_destroy_xfer(struct usbd_xfer *); 108 1.91 skrll 109 1.91 skrll void usbd_setup_xfer(struct usbd_xfer *, void *, void *, 110 1.91 skrll uint32_t, uint16_t, uint32_t, usbd_callback); 111 1.91 skrll 112 1.91 skrll void usbd_setup_default_xfer(struct usbd_xfer *, struct usbd_device *, 113 1.91 skrll void *, uint32_t, usb_device_request_t *, void *, 114 1.91 skrll uint32_t, uint16_t, usbd_callback); 115 1.91 skrll 116 1.91 skrll void usbd_setup_isoc_xfer(struct usbd_xfer *, void *, uint16_t *, 117 1.91 skrll uint32_t, uint16_t, usbd_callback); 118 1.91 skrll 119 1.91 skrll void usbd_get_xfer_status(struct usbd_xfer *, void **, 120 1.91 skrll void **, uint32_t *, usbd_status *); 121 1.91 skrll 122 1.1 augustss usb_endpoint_descriptor_t *usbd_interface2endpoint_descriptor 123 1.91 skrll (struct usbd_interface *, uint8_t); 124 1.91 skrll 125 1.105 riastrad void usbd_abort_pipe(struct usbd_pipe *); 126 1.105 riastrad void usbd_abort_default_pipe(struct usbd_device *); 127 1.91 skrll 128 1.107 riastrad void usbd_suspend_pipe(struct usbd_pipe *); 129 1.107 riastrad void usbd_resume_pipe(struct usbd_pipe *); 130 1.107 riastrad 131 1.91 skrll usbd_status usbd_clear_endpoint_stall(struct usbd_pipe *); 132 1.91 skrll void usbd_clear_endpoint_stall_async(struct usbd_pipe *); 133 1.91 skrll 134 1.91 skrll void usbd_clear_endpoint_toggle(struct usbd_pipe *); 135 1.91 skrll usbd_status usbd_endpoint_count(struct usbd_interface *, uint8_t *); 136 1.91 skrll 137 1.91 skrll usbd_status usbd_interface_count(struct usbd_device *, uint8_t *); 138 1.91 skrll 139 1.91 skrll void usbd_interface2device_handle(struct usbd_interface *, struct usbd_device **); 140 1.91 skrll usbd_status usbd_device2interface_handle(struct usbd_device *, 141 1.91 skrll uint8_t, struct usbd_interface **); 142 1.91 skrll 143 1.91 skrll struct usbd_device *usbd_pipe2device_handle(struct usbd_pipe *); 144 1.91 skrll 145 1.91 skrll usbd_status usbd_sync_transfer(struct usbd_xfer *); 146 1.91 skrll usbd_status usbd_sync_transfer_sig(struct usbd_xfer *); 147 1.91 skrll 148 1.91 skrll usbd_status usbd_do_request(struct usbd_device *, usb_device_request_t *, void *); 149 1.91 skrll usbd_status usbd_do_request_flags(struct usbd_device *, usb_device_request_t *, 150 1.91 skrll void *, uint16_t, int *, uint32_t); 151 1.97 mrg usbd_status usbd_do_request_len(struct usbd_device *dev, 152 1.97 mrg usb_device_request_t *req, size_t len, void *data, uint16_t flags, 153 1.97 mrg int *actlen, uint32_t timeout); 154 1.91 skrll 155 1.91 skrll usb_interface_descriptor_t * 156 1.91 skrll usbd_get_interface_descriptor(struct usbd_interface *); 157 1.91 skrll usb_endpoint_descriptor_t * 158 1.91 skrll usbd_get_endpoint_descriptor(struct usbd_interface *, uint8_t); 159 1.91 skrll 160 1.91 skrll usb_config_descriptor_t *usbd_get_config_descriptor(struct usbd_device *); 161 1.91 skrll usb_device_descriptor_t *usbd_get_device_descriptor(struct usbd_device *); 162 1.91 skrll 163 1.91 skrll usbd_status usbd_set_interface(struct usbd_interface *, int); 164 1.91 skrll usbd_status usbd_get_interface(struct usbd_interface *, uint8_t *); 165 1.91 skrll 166 1.43 augustss int usbd_get_no_alts(usb_config_descriptor_t *, int); 167 1.91 skrll 168 1.91 skrll void usbd_fill_deviceinfo(struct usbd_device *, struct usb_device_info *, int); 169 1.91 skrll int usbd_get_interface_altindex(struct usbd_interface *); 170 1.43 augustss 171 1.63 itojun usb_interface_descriptor_t *usbd_find_idesc(usb_config_descriptor_t *, 172 1.91 skrll int, int); 173 1.63 itojun usb_endpoint_descriptor_t *usbd_find_edesc(usb_config_descriptor_t *, 174 1.91 skrll int, int, int); 175 1.43 augustss 176 1.91 skrll void usbd_dopoll(struct usbd_interface *); 177 1.91 skrll void usbd_set_polling(struct usbd_device *, int); 178 1.23 augustss 179 1.63 itojun const char *usbd_errstr(usbd_status); 180 1.30 augustss 181 1.91 skrll void usbd_add_dev_event(int, struct usbd_device *); 182 1.91 skrll void usbd_add_drv_event(int, struct usbd_device *, device_t); 183 1.1 augustss 184 1.91 skrll char *usbd_devinfo_alloc(struct usbd_device *, int); 185 1.67 christos void usbd_devinfo_free(char *); 186 1.65 augustss 187 1.91 skrll const struct usbd_quirks *usbd_get_quirks(struct usbd_device *); 188 1.38 augustss 189 1.91 skrll usbd_status usbd_reload_device_desc(struct usbd_device *); 190 1.41 augustss 191 1.67 christos int usbd_ratecheck(struct timeval *); 192 1.48 augustss 193 1.91 skrll usbd_status usbd_get_string(struct usbd_device *, int, char *); 194 1.91 skrll usbd_status usbd_get_string0(struct usbd_device *, int, char *, int); 195 1.64 augustss 196 1.101 riastrad /* For use by HCI drivers, not USB device drivers */ 197 1.101 riastrad void usbd_xfer_schedule_timeout(struct usbd_xfer *); 198 1.101 riastrad bool usbd_xfer_trycomplete(struct usbd_xfer *); 199 1.101 riastrad void usbd_xfer_abort(struct usbd_xfer *); 200 1.101 riastrad 201 1.84 mrg /* Used to clear endpoint stalls from the softint */ 202 1.89 skrll void usbd_clear_endpoint_stall_task(void *); 203 1.84 mrg 204 1.48 augustss /* 205 1.48 augustss * The usb_task structs form a queue of things to run in the USB event 206 1.48 augustss * thread. Normally this is just device discovery when a connect/disconnect 207 1.48 augustss * has been detected. But it may also be used by drivers that need to 208 1.48 augustss * perform (short) tasks that must have a process context. 209 1.48 augustss */ 210 1.48 augustss struct usb_task { 211 1.49 augustss TAILQ_ENTRY(usb_task) next; 212 1.48 augustss void (*fun)(void *); 213 1.48 augustss void *arg; 214 1.90 riastrad volatile unsigned queue; 215 1.85 jmcneill int flags; 216 1.48 augustss }; 217 1.70 joerg #define USB_TASKQ_HC 0 218 1.70 joerg #define USB_TASKQ_DRIVER 1 219 1.70 joerg #define USB_NUM_TASKQS 2 220 1.70 joerg #define USB_TASKQ_NAMES {"usbtask-hc", "usbtask-dr"} 221 1.87 jmcneill #define USB_TASKQ_MPSAFE 0x80 222 1.48 augustss 223 1.91 skrll void usb_add_task(struct usbd_device *, struct usb_task *, int); 224 1.99 riastrad bool usb_rem_task(struct usbd_device *, struct usb_task *); 225 1.95 riastrad bool usb_rem_task_wait(struct usbd_device *, struct usb_task *, int, 226 1.95 riastrad kmutex_t *); 227 1.100 riastrad bool usb_task_pending(struct usbd_device *, struct usb_task *); 228 1.90 riastrad #define usb_init_task(t, f, a, fl) ((t)->fun = (f), (t)->arg = (a), (t)->queue = USB_NUM_TASKQS, (t)->flags = (fl)) 229 1.55 augustss 230 1.103 riastrad bool usb_in_event_thread(device_t); 231 1.103 riastrad 232 1.55 augustss struct usb_devno { 233 1.91 skrll uint16_t ud_vendor; 234 1.91 skrll uint16_t ud_product; 235 1.55 augustss }; 236 1.63 itojun const struct usb_devno *usb_match_device(const struct usb_devno *, 237 1.91 skrll u_int, u_int, uint16_t, uint16_t); 238 1.56 augustss #define usb_lookup(tbl, vendor, product) \ 239 1.91 skrll usb_match_device((const struct usb_devno *)(tbl), sizeof(tbl) / sizeof((tbl)[0]), sizeof((tbl)[0]), (vendor), (product)) 240 1.59 augustss #define USB_PRODUCT_ANY 0xffff 241 1.38 augustss 242 1.96 pgoyette /* compat callbacks */ 243 1.96 pgoyette void usbd_devinfo_vp(struct usbd_device *, char *, size_t, char *, size_t, 244 1.96 pgoyette int, int); 245 1.96 pgoyette int usbd_printBCD(char *, size_t, int); 246 1.96 pgoyette 247 1.96 pgoyette 248 1.1 augustss /* NetBSD attachment information */ 249 1.1 augustss 250 1.1 augustss /* Attach data */ 251 1.1 augustss struct usb_attach_arg { 252 1.91 skrll int uaa_port; 253 1.91 skrll int uaa_vendor; 254 1.91 skrll int uaa_product; 255 1.91 skrll int uaa_release; 256 1.91 skrll struct usbd_device * uaa_device; /* current device */ 257 1.91 skrll int uaa_class; 258 1.91 skrll int uaa_subclass; 259 1.91 skrll int uaa_proto; 260 1.91 skrll int uaa_usegeneric; 261 1.73 drochner }; 262 1.73 drochner 263 1.73 drochner struct usbif_attach_arg { 264 1.91 skrll int uiaa_port; 265 1.91 skrll int uiaa_configno; 266 1.91 skrll int uiaa_ifaceno; 267 1.91 skrll int uiaa_vendor; 268 1.91 skrll int uiaa_product; 269 1.91 skrll int uiaa_release; 270 1.91 skrll struct usbd_device * uiaa_device; /* current device */ 271 1.91 skrll 272 1.91 skrll struct usbd_interface * uiaa_iface; /* current interface */ 273 1.91 skrll int uiaa_class; 274 1.91 skrll int uiaa_subclass; 275 1.91 skrll int uiaa_proto; 276 1.73 drochner 277 1.73 drochner /* XXX need accounting for interfaces not matched to */ 278 1.73 drochner 279 1.91 skrll struct usbd_interface **uiaa_ifaces; /* all interfaces */ 280 1.91 skrll int uiaa_nifaces; /* number of interfaces */ 281 1.1 augustss }; 282 1.1 augustss 283 1.1 augustss /* Match codes. */ 284 1.69 augustss #define UMATCH_HIGHEST 15 285 1.1 augustss /* First five codes is for a whole device. */ 286 1.1 augustss #define UMATCH_VENDOR_PRODUCT_REV 14 287 1.1 augustss #define UMATCH_VENDOR_PRODUCT 13 288 1.1 augustss #define UMATCH_VENDOR_DEVCLASS_DEVPROTO 12 289 1.1 augustss #define UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO 11 290 1.1 augustss #define UMATCH_DEVCLASS_DEVSUBCLASS 10 291 1.1 augustss /* Next six codes are for interfaces. */ 292 1.1 augustss #define UMATCH_VENDOR_PRODUCT_REV_CONF_IFACE 9 293 1.1 augustss #define UMATCH_VENDOR_PRODUCT_CONF_IFACE 8 294 1.1 augustss #define UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO 7 295 1.1 augustss #define UMATCH_VENDOR_IFACESUBCLASS 6 296 1.1 augustss #define UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO 5 297 1.1 augustss #define UMATCH_IFACECLASS_IFACESUBCLASS 4 298 1.1 augustss #define UMATCH_IFACECLASS 3 299 1.1 augustss #define UMATCH_IFACECLASS_GENERIC 2 300 1.1 augustss /* Generic driver */ 301 1.1 augustss #define UMATCH_GENERIC 1 302 1.1 augustss /* No match */ 303 1.1 augustss #define UMATCH_NONE 0 304 1.12 augustss 305 1.88 skrll 306 1.88 skrll /* 307 1.88 skrll * IPL_USB is defined as IPL_VM for drivers that have not been made MP safe. 308 1.88 skrll * IPL_VM (currently) takes the kernel lock. 309 1.88 skrll * 310 1.88 skrll * Eventually, IPL_USB can/should be changed 311 1.88 skrll */ 312 1.92 skrll #define IPL_USB IPL_VM 313 1.88 skrll #define splhardusb splvm 314 1.92 skrll 315 1.92 skrll #define SOFTINT_USB SOFTINT_SERIAL 316 1.92 skrll #define IPL_SOFTUSB IPL_SOFTSERIAL 317 1.92 skrll #define splusb splsoftserial 318 1.74 drochner 319 1.74 drochner #endif /* _USBDI_H_ */ 320