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