Home | History | Annotate | Line # | Download | only in usb
usbdi.h revision 1.73
      1 /*	$NetBSD: usbdi.h,v 1.73 2007/03/13 13:51:56 drochner Exp $	*/
      2 /*	$FreeBSD: src/sys/dev/usb/usbdi.h,v 1.18 1999/11/17 22:33:49 n_hibma Exp $	*/
      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 (at) 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 typedef struct usbd_bus		*usbd_bus_handle;
     42 typedef struct usbd_device	*usbd_device_handle;
     43 typedef struct usbd_interface	*usbd_interface_handle;
     44 typedef struct usbd_pipe	*usbd_pipe_handle;
     45 typedef struct usbd_xfer	*usbd_xfer_handle;
     46 typedef void			*usbd_private_handle;
     47 
     48 typedef enum {		/* keep in sync with usbd_status_msgs */
     49 	USBD_NORMAL_COMPLETION = 0, /* must be 0 */
     50 	USBD_IN_PROGRESS,	/* 1 */
     51 	/* errors */
     52 	USBD_PENDING_REQUESTS,	/* 2 */
     53 	USBD_NOT_STARTED,	/* 3 */
     54 	USBD_INVAL,		/* 4 */
     55 	USBD_NOMEM,		/* 5 */
     56 	USBD_CANCELLED,		/* 6 */
     57 	USBD_BAD_ADDRESS,	/* 7 */
     58 	USBD_IN_USE,		/* 8 */
     59 	USBD_NO_ADDR,		/* 9 */
     60 	USBD_SET_ADDR_FAILED,	/* 10 */
     61 	USBD_NO_POWER,		/* 11 */
     62 	USBD_TOO_DEEP,		/* 12 */
     63 	USBD_IOERROR,		/* 13 */
     64 	USBD_NOT_CONFIGURED,	/* 14 */
     65 	USBD_TIMEOUT,		/* 15 */
     66 	USBD_SHORT_XFER,	/* 16 */
     67 	USBD_STALLED,		/* 17 */
     68 	USBD_INTERRUPTED,	/* 18 */
     69 
     70 	USBD_ERROR_MAX		/* must be last */
     71 } usbd_status;
     72 
     73 typedef void (*usbd_callback)(usbd_xfer_handle, usbd_private_handle,
     74 			      usbd_status);
     75 
     76 /* Open flags */
     77 #define USBD_EXCLUSIVE_USE	0x01
     78 
     79 /* Use default (specified by ep. desc.) interval on interrupt pipe */
     80 #define USBD_DEFAULT_INTERVAL	(-1)
     81 
     82 /* Request flags */
     83 #define USBD_NO_COPY		0x01	/* do not copy data to DMA buffer */
     84 #define USBD_SYNCHRONOUS	0x02	/* wait for completion */
     85 /* in usb.h #define USBD_SHORT_XFER_OK	0x04*/	/* allow short reads */
     86 #define USBD_FORCE_SHORT_XFER	0x08	/* force last short packet on write */
     87 
     88 #define USBD_NO_TIMEOUT 0
     89 #define USBD_DEFAULT_TIMEOUT 5000 /* ms = 5 s */
     90 
     91 #if defined(__FreeBSD__)
     92 #define USB_CDEV_MAJOR 108
     93 #endif
     94 
     95 #define DEVINFOSIZE 1024
     96 
     97 usbd_status usbd_open_pipe(usbd_interface_handle, u_int8_t,
     98 			   u_int8_t, usbd_pipe_handle *);
     99 usbd_status usbd_close_pipe(usbd_pipe_handle);
    100 usbd_status usbd_transfer(usbd_xfer_handle);
    101 usbd_xfer_handle usbd_alloc_xfer(usbd_device_handle);
    102 usbd_status usbd_free_xfer(usbd_xfer_handle);
    103 void usbd_setup_xfer(usbd_xfer_handle, usbd_pipe_handle,
    104 		     usbd_private_handle, void *,
    105 		     u_int32_t, u_int16_t, u_int32_t,
    106 		     usbd_callback);
    107 void usbd_setup_default_xfer(usbd_xfer_handle, usbd_device_handle,
    108 			     usbd_private_handle, u_int32_t,
    109 			     usb_device_request_t *, void *,
    110 			     u_int32_t, u_int16_t, usbd_callback);
    111 void usbd_setup_isoc_xfer(usbd_xfer_handle, usbd_pipe_handle,
    112 			  usbd_private_handle, u_int16_t *,
    113 			  u_int32_t, u_int16_t, usbd_callback);
    114 void usbd_get_xfer_status(usbd_xfer_handle, usbd_private_handle *,
    115 			  void **, u_int32_t *, usbd_status *);
    116 usb_endpoint_descriptor_t *usbd_interface2endpoint_descriptor
    117 			(usbd_interface_handle, u_int8_t);
    118 usbd_status usbd_abort_pipe(usbd_pipe_handle);
    119 usbd_status usbd_clear_endpoint_stall(usbd_pipe_handle);
    120 usbd_status usbd_clear_endpoint_stall_async(usbd_pipe_handle);
    121 void usbd_clear_endpoint_toggle(usbd_pipe_handle);
    122 usbd_status usbd_endpoint_count(usbd_interface_handle, u_int8_t *);
    123 usbd_status usbd_interface_count(usbd_device_handle, u_int8_t *);
    124 void usbd_interface2device_handle(usbd_interface_handle,
    125 					 usbd_device_handle *);
    126 usbd_status usbd_device2interface_handle(usbd_device_handle,
    127 			      u_int8_t, usbd_interface_handle *);
    128 
    129 usbd_device_handle usbd_pipe2device_handle(usbd_pipe_handle);
    130 void *usbd_alloc_buffer(usbd_xfer_handle, u_int32_t);
    131 void usbd_free_buffer(usbd_xfer_handle);
    132 void *usbd_get_buffer(usbd_xfer_handle);
    133 usbd_status usbd_sync_transfer(usbd_xfer_handle);
    134 usbd_status usbd_open_pipe_intr(usbd_interface_handle, u_int8_t,
    135 				u_int8_t, usbd_pipe_handle *,
    136 				usbd_private_handle, void *,
    137 				u_int32_t, usbd_callback, int);
    138 usbd_status usbd_do_request(usbd_device_handle, usb_device_request_t *, void *);
    139 usbd_status usbd_do_request_async(usbd_device_handle,
    140 				  usb_device_request_t *, void *);
    141 usbd_status usbd_do_request_flags(usbd_device_handle, usb_device_request_t *,
    142 				  void *, u_int16_t, int*, u_int32_t);
    143 usbd_status usbd_do_request_flags_pipe(usbd_device_handle, usbd_pipe_handle,
    144 	usb_device_request_t *, void *, u_int16_t, int *, u_int32_t);
    145 usb_interface_descriptor_t *usbd_get_interface_descriptor
    146 				(usbd_interface_handle);
    147 usb_config_descriptor_t *usbd_get_config_descriptor(usbd_device_handle);
    148 usb_device_descriptor_t *usbd_get_device_descriptor(usbd_device_handle);
    149 usbd_status usbd_set_interface(usbd_interface_handle, int);
    150 int usbd_get_no_alts(usb_config_descriptor_t *, int);
    151 usbd_status  usbd_get_interface(usbd_interface_handle, u_int8_t *);
    152 void usbd_fill_deviceinfo(usbd_device_handle, struct usb_device_info *, int);
    153 #ifdef COMPAT_30
    154 void usbd_fill_deviceinfo_old(usbd_device_handle, struct usb_device_info_old *,
    155     int);
    156 #endif
    157 int usbd_get_interface_altindex(usbd_interface_handle);
    158 
    159 usb_interface_descriptor_t *usbd_find_idesc(usb_config_descriptor_t *,
    160 					    int, int);
    161 usb_endpoint_descriptor_t *usbd_find_edesc(usb_config_descriptor_t *,
    162 					   int, int, int);
    163 
    164 void usbd_dopoll(usbd_interface_handle);
    165 void usbd_set_polling(usbd_device_handle, int);
    166 
    167 const char *usbd_errstr(usbd_status);
    168 
    169 void usbd_add_dev_event(int, usbd_device_handle);
    170 void usbd_add_drv_event(int, usbd_device_handle, device_ptr_t);
    171 
    172 char *usbd_devinfo_alloc(usbd_device_handle, int);
    173 void usbd_devinfo_free(char *);
    174 
    175 const struct usbd_quirks *usbd_get_quirks(usbd_device_handle);
    176 usb_endpoint_descriptor_t *usbd_get_endpoint_descriptor
    177 			(usbd_interface_handle, u_int8_t);
    178 
    179 usbd_status usbd_reload_device_desc(usbd_device_handle);
    180 
    181 int usbd_ratecheck(struct timeval *);
    182 
    183 usbd_status usbd_get_string(usbd_device_handle, int, char *);
    184 usbd_status usbd_get_string0(usbd_device_handle, int, char *, int);
    185 
    186 /* An iterator for descriptors. */
    187 typedef struct {
    188 	const uByte *cur;
    189 	const uByte *end;
    190 } usbd_desc_iter_t;
    191 void usb_desc_iter_init(usbd_device_handle, usbd_desc_iter_t *);
    192 const usb_descriptor_t *usb_desc_iter_next(usbd_desc_iter_t *);
    193 
    194 /*
    195  * The usb_task structs form a queue of things to run in the USB event
    196  * thread.  Normally this is just device discovery when a connect/disconnect
    197  * has been detected.  But it may also be used by drivers that need to
    198  * perform (short) tasks that must have a process context.
    199  */
    200 struct usb_task {
    201 	TAILQ_ENTRY(usb_task) next;
    202 	void (*fun)(void *);
    203 	void *arg;
    204 	int queue;
    205 };
    206 #define	USB_TASKQ_HC		0
    207 #define	USB_TASKQ_DRIVER	1
    208 #define	USB_NUM_TASKQS		2
    209 #define	USB_TASKQ_NAMES		{"usbtask-hc", "usbtask-dr"}
    210 
    211 void usb_add_task(usbd_device_handle, struct usb_task *, int);
    212 void usb_rem_task(usbd_device_handle, struct usb_task *);
    213 #define usb_init_task(t, f, a) ((t)->fun = (f), (t)->arg = (a), (t)->queue = -1)
    214 
    215 struct usb_devno {
    216 	u_int16_t ud_vendor;
    217 	u_int16_t ud_product;
    218 };
    219 const struct usb_devno *usb_match_device(const struct usb_devno *,
    220 	u_int, u_int, u_int16_t, u_int16_t);
    221 #define usb_lookup(tbl, vendor, product) \
    222 	usb_match_device((const struct usb_devno *)(tbl), sizeof (tbl) / sizeof ((tbl)[0]), sizeof ((tbl)[0]), (vendor), (product))
    223 #define	USB_PRODUCT_ANY		0xffff
    224 
    225 /* NetBSD attachment information */
    226 
    227 /* Attach data */
    228 struct usb_attach_arg {
    229 	int			port;
    230 	int			vendor;
    231 	int			product;
    232 	int			release;
    233 	usbd_device_handle	device;	/* current device */
    234 	int			class, subclass, proto;
    235 	int			usegeneric;
    236 };
    237 
    238 struct usbif_attach_arg {
    239 	int			port;
    240 	int			configno;
    241 	int			ifaceno;
    242 	int			vendor;
    243 	int			product;
    244 	int			release;
    245 	usbd_device_handle	device;	/* current device */
    246 
    247 	usbd_interface_handle	iface; /* current interface */
    248 	int			class, subclass, proto;
    249 
    250 	/* XXX need accounting for interfaces not matched to */
    251 
    252 	usbd_interface_handle  *ifaces;	/* all interfaces */
    253 	int			nifaces; /* number of interfaces */
    254 };
    255 
    256 #if defined(__NetBSD__) || defined(__OpenBSD__)
    257 /* Match codes. */
    258 #define UMATCH_HIGHEST					15
    259 /* First five codes is for a whole device. */
    260 #define UMATCH_VENDOR_PRODUCT_REV			14
    261 #define UMATCH_VENDOR_PRODUCT				13
    262 #define UMATCH_VENDOR_DEVCLASS_DEVPROTO			12
    263 #define UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO		11
    264 #define UMATCH_DEVCLASS_DEVSUBCLASS			10
    265 /* Next six codes are for interfaces. */
    266 #define UMATCH_VENDOR_PRODUCT_REV_CONF_IFACE		 9
    267 #define UMATCH_VENDOR_PRODUCT_CONF_IFACE		 8
    268 #define UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO		 7
    269 #define UMATCH_VENDOR_IFACESUBCLASS			 6
    270 #define UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO	 5
    271 #define UMATCH_IFACECLASS_IFACESUBCLASS			 4
    272 #define UMATCH_IFACECLASS				 3
    273 #define UMATCH_IFACECLASS_GENERIC			 2
    274 /* Generic driver */
    275 #define UMATCH_GENERIC					 1
    276 /* No match */
    277 #define UMATCH_NONE					 0
    278 
    279 #elif defined(__FreeBSD__)
    280 /* FreeBSD needs values less than zero */
    281 #define UMATCH_VENDOR_PRODUCT_REV			(-10)
    282 #define UMATCH_VENDOR_PRODUCT				(-20)
    283 #define UMATCH_VENDOR_DEVCLASS_DEVPROTO			(-30)
    284 #define UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO		(-40)
    285 #define UMATCH_DEVCLASS_DEVSUBCLASS			(-50)
    286 #define UMATCH_VENDOR_PRODUCT_REV_CONF_IFACE		(-60)
    287 #define UMATCH_VENDOR_PRODUCT_CONF_IFACE		(-70)
    288 #define UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO		(-80)
    289 #define UMATCH_VENDOR_IFACESUBCLASS			(-90)
    290 #define UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO	(-100)
    291 #define UMATCH_IFACECLASS_IFACESUBCLASS			(-110)
    292 #define UMATCH_IFACECLASS				(-120)
    293 #define UMATCH_IFACECLASS_GENERIC			(-130)
    294 #define UMATCH_GENERIC					(-140)
    295 #define UMATCH_NONE					(ENXIO)
    296 
    297 #endif
    298 
    299 #if defined(__FreeBSD__)
    300 int usbd_driver_load(module_t mod, int what, void *arg);
    301 #endif
    302 
    303 /* XXX Perhaps USB should have its own levels? */
    304 #ifdef USB_USE_SOFTINTR
    305 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    306 #define splusb splsoftnet
    307 #else
    308 #define	splusb splsoftclock
    309 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
    310 #else
    311 #define splusb splbio
    312 #endif /* USB_USE_SOFTINTR */
    313 #define splhardusb splbio
    314 #define IPL_USB IPL_BIO
    315