1 /* $OpenBSD: usbdi.h,v 1.12 2001/05/03 02:20:34 aaron Exp $ */ 2 /* $NetBSD: usbdi.h,v 1.52 2001/05/01 16:43:44 lukem Exp $ */ 3 /* $FreeBSD: src/sys/dev/usb/usbdi.h,v 1.18 1999/11/17 22:33:49 n_hibma Exp $ */ 4 5 /* 6 * Copyright (c) 1998 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Lennart Augustsson (lennart@augustsson.net) at 11 * Carlstedt Research & Technology. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the NetBSD 24 * Foundation, Inc. and its contributors. 25 * 4. Neither the name of The NetBSD Foundation nor the names of its 26 * contributors may be used to endorse or promote products derived 27 * from this software without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 39 * POSSIBILITY OF SUCH DAMAGE. 40 */ 41 42 typedef struct usbd_bus *usbd_bus_handle; 43 typedef struct usbd_device *usbd_device_handle; 44 typedef struct usbd_interface *usbd_interface_handle; 45 typedef struct usbd_pipe *usbd_pipe_handle; 46 typedef struct usbd_xfer *usbd_xfer_handle; 47 typedef void *usbd_private_handle; 48 49 typedef enum { /* keep in sync with usbd_status_msgs */ 50 USBD_NORMAL_COMPLETION = 0, /* must be 0 */ 51 USBD_IN_PROGRESS, 52 /* errors */ 53 USBD_PENDING_REQUESTS, 54 USBD_NOT_STARTED, 55 USBD_INVAL, 56 USBD_NOMEM, 57 USBD_CANCELLED, 58 USBD_BAD_ADDRESS, 59 USBD_IN_USE, 60 USBD_NO_ADDR, 61 USBD_SET_ADDR_FAILED, 62 USBD_NO_POWER, 63 USBD_TOO_DEEP, 64 USBD_IOERROR, 65 USBD_NOT_CONFIGURED, 66 USBD_TIMEOUT, 67 USBD_SHORT_XFER, 68 USBD_STALLED, 69 USBD_INTERRUPTED, 70 71 USBD_ERROR_MAX /* must be last */ 72 } usbd_status; 73 74 typedef void (*usbd_callback)(usbd_xfer_handle, usbd_private_handle, 75 usbd_status); 76 77 /* Open flags */ 78 #define USBD_EXCLUSIVE_USE 0x01 79 80 /* Use default (specified by ep. desc.) interval on interrupt pipe */ 81 #define USBD_DEFAULT_INTERVAL (-1) 82 83 /* Request flags */ 84 #define USBD_NO_COPY 0x01 /* do not copy data to DMA buffer */ 85 #define USBD_SYNCHRONOUS 0x02 /* wait for completion */ 86 /* in usb.h #define USBD_SHORT_XFER_OK 0x04*/ /* allow short reads */ 87 #define USBD_FORCE_SHORT_XFER 0x08 /* force last short packet on write */ 88 89 #define USBD_NO_TIMEOUT 0 90 #define USBD_DEFAULT_TIMEOUT 5000 /* ms = 5 s */ 91 92 #if defined(__FreeBSD__) 93 #define USB_CDEV_MAJOR 108 94 #endif 95 96 usbd_status usbd_open_pipe(usbd_interface_handle iface, u_int8_t address, 97 u_int8_t flags, usbd_pipe_handle *pipe); 98 usbd_status usbd_close_pipe(usbd_pipe_handle pipe); 99 usbd_status usbd_transfer(usbd_xfer_handle req); 100 usbd_xfer_handle usbd_alloc_xfer(usbd_device_handle); 101 usbd_status usbd_free_xfer(usbd_xfer_handle xfer); 102 void usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe, 103 usbd_private_handle priv, void *buffer, 104 u_int32_t length, u_int16_t flags, u_int32_t timeout, 105 usbd_callback); 106 void usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev, 107 usbd_private_handle priv, u_int32_t timeout, 108 usb_device_request_t *req, void *buffer, 109 u_int32_t length, u_int16_t flags, usbd_callback); 110 void usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe, 111 usbd_private_handle priv, u_int16_t *frlengths, 112 u_int32_t nframes, u_int16_t flags, usbd_callback); 113 void usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv, 114 void **buffer, u_int32_t *count, usbd_status *status); 115 usb_endpoint_descriptor_t *usbd_interface2endpoint_descriptor 116 (usbd_interface_handle iface, u_int8_t address); 117 usbd_status usbd_abort_pipe(usbd_pipe_handle pipe); 118 usbd_status usbd_clear_endpoint_stall(usbd_pipe_handle pipe); 119 usbd_status usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe); 120 usbd_status usbd_endpoint_count(usbd_interface_handle dev, u_int8_t *count); 121 usbd_status usbd_interface_count(usbd_device_handle dev, u_int8_t *count); 122 usbd_status usbd_interface2device_handle(usbd_interface_handle iface, 123 usbd_device_handle *dev); 124 usbd_status usbd_device2interface_handle(usbd_device_handle dev, 125 u_int8_t ifaceno, usbd_interface_handle *iface); 126 127 usbd_device_handle usbd_pipe2device_handle(usbd_pipe_handle); 128 void *usbd_alloc_buffer(usbd_xfer_handle req, u_int32_t size); 129 void usbd_free_buffer(usbd_xfer_handle req); 130 void *usbd_get_buffer(usbd_xfer_handle xfer); 131 usbd_status usbd_sync_transfer(usbd_xfer_handle req); 132 usbd_status usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address, 133 u_int8_t flags, usbd_pipe_handle *pipe, 134 usbd_private_handle priv, void *buffer, 135 u_int32_t length, usbd_callback, int); 136 usbd_status usbd_do_request(usbd_device_handle pipe, usb_device_request_t *req, 137 void *data); 138 usbd_status usbd_do_request_async(usbd_device_handle pipe, 139 usb_device_request_t *req, void *data); 140 usbd_status usbd_do_request_flags(usbd_device_handle pipe, 141 usb_device_request_t *req, 142 void *data, u_int16_t flags, int *); 143 usbd_status usbd_do_request_flags_pipe( 144 usbd_device_handle dev, usbd_pipe_handle pipe, 145 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen); 146 usb_interface_descriptor_t *usbd_get_interface_descriptor 147 (usbd_interface_handle iface); 148 usb_config_descriptor_t *usbd_get_config_descriptor(usbd_device_handle dev); 149 usb_device_descriptor_t *usbd_get_device_descriptor(usbd_device_handle dev); 150 usbd_status usbd_set_interface(usbd_interface_handle, int); 151 int usbd_get_no_alts(usb_config_descriptor_t *, int); 152 usbd_status usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface); 153 void usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di, int); 154 int usbd_get_interface_altindex(usbd_interface_handle iface); 155 156 usb_interface_descriptor_t *usbd_find_idesc(usb_config_descriptor_t *cd, 157 int iindex, int ano); 158 usb_endpoint_descriptor_t *usbd_find_edesc(usb_config_descriptor_t *cd, 159 int ifaceidx, int altidx, 160 int endptidx); 161 162 void usbd_dopoll(usbd_interface_handle); 163 void usbd_set_polling(usbd_device_handle iface, int on); 164 165 const char *usbd_errstr(usbd_status err); 166 167 void usbd_add_dev_event(int, usbd_device_handle); 168 void usbd_add_drv_event(int, usbd_device_handle, device_ptr_t); 169 170 void usbd_devinfo(usbd_device_handle, int, char *); 171 const struct usbd_quirks *usbd_get_quirks(usbd_device_handle); 172 usb_endpoint_descriptor_t *usbd_get_endpoint_descriptor 173 (usbd_interface_handle iface, u_int8_t address); 174 175 usbd_status usbd_reload_device_desc(usbd_device_handle); 176 177 int usbd_ratecheck(struct timeval *last); 178 179 /* 180 * The usb_task structs form a queue of things to run in the USB event 181 * thread. Normally this is just device discovery when a connect/disconnect 182 * has been detected. But it may also be used by drivers that need to 183 * perform (short) tasks that must have a process context. 184 */ 185 struct usb_task { 186 TAILQ_ENTRY(usb_task) next; 187 void (*fun)(void *); 188 void *arg; 189 char onqueue; 190 }; 191 192 void usb_add_task(usbd_device_handle dev, struct usb_task *task); 193 void usb_rem_task(usbd_device_handle dev, struct usb_task *task); 194 #define usb_init_task(t, f, a) ((t)->fun = (f), (t)->arg = (a), (t)->onqueue = 0) 195 196 /* NetBSD attachment information */ 197 198 /* Attach data */ 199 struct usb_attach_arg { 200 int port; 201 int configno; 202 int ifaceno; 203 int vendor; 204 int product; 205 int release; 206 usbd_device_handle device; /* current device */ 207 usbd_interface_handle iface; /* current interface */ 208 int usegeneric; 209 usbd_interface_handle *ifaces; /* all interfaces */ 210 int nifaces; /* number of interfaces */ 211 }; 212 213 #if defined(__NetBSD__) || defined(__OpenBSD__) 214 /* Match codes. */ 215 /* First five codes is for a whole device. */ 216 #define UMATCH_VENDOR_PRODUCT_REV 14 217 #define UMATCH_VENDOR_PRODUCT 13 218 #define UMATCH_VENDOR_DEVCLASS_DEVPROTO 12 219 #define UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO 11 220 #define UMATCH_DEVCLASS_DEVSUBCLASS 10 221 /* Next six codes are for interfaces. */ 222 #define UMATCH_VENDOR_PRODUCT_REV_CONF_IFACE 9 223 #define UMATCH_VENDOR_PRODUCT_CONF_IFACE 8 224 #define UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO 7 225 #define UMATCH_VENDOR_IFACESUBCLASS 6 226 #define UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO 5 227 #define UMATCH_IFACECLASS_IFACESUBCLASS 4 228 #define UMATCH_IFACECLASS 3 229 #define UMATCH_IFACECLASS_GENERIC 2 230 /* Generic driver */ 231 #define UMATCH_GENERIC 1 232 /* No match */ 233 #define UMATCH_NONE 0 234 235 #elif defined(__FreeBSD__) 236 /* FreeBSD needs values less than zero */ 237 #define UMATCH_VENDOR_PRODUCT_REV (-10) 238 #define UMATCH_VENDOR_PRODUCT (-20) 239 #define UMATCH_VENDOR_DEVCLASS_DEVPROTO (-30) 240 #define UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO (-40) 241 #define UMATCH_DEVCLASS_DEVSUBCLASS (-50) 242 #define UMATCH_VENDOR_PRODUCT_REV_CONF_IFACE (-60) 243 #define UMATCH_VENDOR_PRODUCT_CONF_IFACE (-70) 244 #define UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO (-80) 245 #define UMATCH_VENDOR_IFACESUBCLASS (-90) 246 #define UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO (-100) 247 #define UMATCH_IFACECLASS_IFACESUBCLASS (-110) 248 #define UMATCH_IFACECLASS (-120) 249 #define UMATCH_IFACECLASS_GENERIC (-130) 250 #define UMATCH_GENERIC (-140) 251 #define UMATCH_NONE (ENXIO) 252 253 #endif 254 255 #if defined(__FreeBSD__) 256 int usbd_driver_load(module_t mod, int what, void *arg); 257 #endif 258 259 /* 260 * XXX 261 * splusb MUST be the lowest level interrupt so that within USB callbacks 262 * the level can be raised the appropriate level. 263 * XXX Should probably use a softsplusb. 264 */ 265 /* XXX */ 266 #define splusb splbio 267 #define splhardusb splbio 268 #define IPL_USB IPL_BIO 269 /* XXX */ 270