Home | History | Annotate | Line # | Download | only in usb
usbdi.h revision 1.84
      1 /*	$NetBSD: usbdi.h,v 1.84 2012/07/15 21:13:31 mrg 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  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #ifndef _USBDI_H_
     35 #define _USBDI_H_
     36 
     37 typedef struct usbd_bus		*usbd_bus_handle;
     38 typedef struct usbd_device	*usbd_device_handle;
     39 typedef struct usbd_interface	*usbd_interface_handle;
     40 typedef struct usbd_pipe	*usbd_pipe_handle;
     41 typedef struct usbd_xfer	*usbd_xfer_handle;
     42 typedef void			*usbd_private_handle;
     43 
     44 typedef enum {		/* keep in sync with usbd_error_strs */
     45 	USBD_NORMAL_COMPLETION = 0, /* must be 0 */
     46 	USBD_IN_PROGRESS,	/* 1 */
     47 	/* errors */
     48 	USBD_PENDING_REQUESTS,	/* 2 */
     49 	USBD_NOT_STARTED,	/* 3 */
     50 	USBD_INVAL,		/* 4 */
     51 	USBD_NOMEM,		/* 5 */
     52 	USBD_CANCELLED,		/* 6 */
     53 	USBD_BAD_ADDRESS,	/* 7 */
     54 	USBD_IN_USE,		/* 8 */
     55 	USBD_NO_ADDR,		/* 9 */
     56 	USBD_SET_ADDR_FAILED,	/* 10 */
     57 	USBD_NO_POWER,		/* 11 */
     58 	USBD_TOO_DEEP,		/* 12 */
     59 	USBD_IOERROR,		/* 13 */
     60 	USBD_NOT_CONFIGURED,	/* 14 */
     61 	USBD_TIMEOUT,		/* 15 */
     62 	USBD_SHORT_XFER,	/* 16 */
     63 	USBD_STALLED,		/* 17 */
     64 	USBD_INTERRUPTED,	/* 18 */
     65 
     66 	USBD_ERROR_MAX		/* must be last */
     67 } usbd_status;
     68 
     69 typedef void (*usbd_callback)(usbd_xfer_handle, usbd_private_handle,
     70 			      usbd_status);
     71 
     72 /* Open flags */
     73 #define USBD_EXCLUSIVE_USE	0x01
     74 
     75 /* Use default (specified by ep. desc.) interval on interrupt pipe */
     76 #define USBD_DEFAULT_INTERVAL	(-1)
     77 
     78 /* Request flags */
     79 #define USBD_NO_COPY		0x01	/* do not copy data to DMA buffer */
     80 #define USBD_SYNCHRONOUS	0x02	/* wait for completion */
     81 /* in usb.h #define USBD_SHORT_XFER_OK	0x04*/	/* allow short reads */
     82 #define USBD_FORCE_SHORT_XFER	0x08	/* force last short packet on write */
     83 #define USBD_SYNCHRONOUS_SIG	0x10	/* if waiting for completion,
     84 					 * also take signals */
     85 
     86 #define USBD_NO_TIMEOUT 0
     87 #define USBD_DEFAULT_TIMEOUT 5000 /* ms = 5 s */
     88 #define	USBD_CONFIG_TIMEOUT  (3*USBD_DEFAULT_TIMEOUT)
     89 
     90 #define DEVINFOSIZE 1024
     91 
     92 usbd_status usbd_open_pipe(usbd_interface_handle, u_int8_t,
     93 			   u_int8_t, usbd_pipe_handle *);
     94 usbd_status usbd_close_pipe(usbd_pipe_handle);
     95 usbd_status usbd_transfer(usbd_xfer_handle);
     96 usbd_xfer_handle usbd_alloc_xfer(usbd_device_handle);
     97 usbd_status usbd_free_xfer(usbd_xfer_handle);
     98 void usbd_setup_xfer(usbd_xfer_handle, usbd_pipe_handle,
     99 		     usbd_private_handle, void *,
    100 		     u_int32_t, u_int16_t, u_int32_t,
    101 		     usbd_callback);
    102 void usbd_setup_default_xfer(usbd_xfer_handle, usbd_device_handle,
    103 			     usbd_private_handle, u_int32_t,
    104 			     usb_device_request_t *, void *,
    105 			     u_int32_t, u_int16_t, usbd_callback);
    106 void usbd_setup_isoc_xfer(usbd_xfer_handle, usbd_pipe_handle,
    107 			  usbd_private_handle, u_int16_t *,
    108 			  u_int32_t, u_int16_t, usbd_callback);
    109 void usbd_get_xfer_status(usbd_xfer_handle, usbd_private_handle *,
    110 			  void **, u_int32_t *, usbd_status *);
    111 usb_endpoint_descriptor_t *usbd_interface2endpoint_descriptor
    112 			(usbd_interface_handle, u_int8_t);
    113 usbd_status usbd_abort_pipe(usbd_pipe_handle);
    114 usbd_status usbd_abort_default_pipe(usbd_device_handle);
    115 usbd_status usbd_clear_endpoint_stall(usbd_pipe_handle);
    116 void usbd_clear_endpoint_stall_async(usbd_pipe_handle);
    117 void usbd_clear_endpoint_toggle(usbd_pipe_handle);
    118 usbd_status usbd_endpoint_count(usbd_interface_handle, u_int8_t *);
    119 usbd_status usbd_interface_count(usbd_device_handle, u_int8_t *);
    120 void usbd_interface2device_handle(usbd_interface_handle,
    121 				  usbd_device_handle *);
    122 usbd_status usbd_device2interface_handle(usbd_device_handle,
    123 			      u_int8_t, usbd_interface_handle *);
    124 
    125 usbd_device_handle usbd_pipe2device_handle(usbd_pipe_handle);
    126 void *usbd_alloc_buffer(usbd_xfer_handle, u_int32_t);
    127 void usbd_free_buffer(usbd_xfer_handle);
    128 void *usbd_get_buffer(usbd_xfer_handle);
    129 usbd_status usbd_sync_transfer(usbd_xfer_handle);
    130 usbd_status usbd_sync_transfer_sig(usbd_xfer_handle);
    131 usbd_status usbd_open_pipe_intr(usbd_interface_handle, u_int8_t,
    132 				u_int8_t, usbd_pipe_handle *,
    133 				usbd_private_handle, void *,
    134 				u_int32_t, usbd_callback, int);
    135 usbd_status usbd_do_request(usbd_device_handle, usb_device_request_t *, void *);
    136 usbd_status usbd_do_request_async(usbd_device_handle,
    137 				  usb_device_request_t *, void *);
    138 usbd_status usbd_do_request_flags(usbd_device_handle, usb_device_request_t *,
    139 				  void *, u_int16_t, int*, u_int32_t);
    140 usbd_status usbd_do_request_flags_pipe(usbd_device_handle, usbd_pipe_handle,
    141 	usb_device_request_t *, void *, u_int16_t, int *, u_int32_t);
    142 usb_interface_descriptor_t *usbd_get_interface_descriptor
    143 				(usbd_interface_handle);
    144 usb_config_descriptor_t *usbd_get_config_descriptor(usbd_device_handle);
    145 usb_device_descriptor_t *usbd_get_device_descriptor(usbd_device_handle);
    146 usbd_status usbd_set_interface(usbd_interface_handle, int);
    147 int usbd_get_no_alts(usb_config_descriptor_t *, int);
    148 usbd_status  usbd_get_interface(usbd_interface_handle, u_int8_t *);
    149 void usbd_fill_deviceinfo(usbd_device_handle, struct usb_device_info *, int);
    150 #ifdef COMPAT_30
    151 void usbd_fill_deviceinfo_old(usbd_device_handle, struct usb_device_info_old *,
    152     int);
    153 #endif
    154 int usbd_get_interface_altindex(usbd_interface_handle);
    155 
    156 usb_interface_descriptor_t *usbd_find_idesc(usb_config_descriptor_t *,
    157 					    int, int);
    158 usb_endpoint_descriptor_t *usbd_find_edesc(usb_config_descriptor_t *,
    159 					   int, int, int);
    160 
    161 void usbd_dopoll(usbd_interface_handle);
    162 void usbd_set_polling(usbd_device_handle, int);
    163 
    164 const char *usbd_errstr(usbd_status);
    165 
    166 void usbd_add_dev_event(int, usbd_device_handle);
    167 void usbd_add_drv_event(int, usbd_device_handle, device_t);
    168 
    169 char *usbd_devinfo_alloc(usbd_device_handle, int);
    170 void usbd_devinfo_free(char *);
    171 
    172 const struct usbd_quirks *usbd_get_quirks(usbd_device_handle);
    173 usb_endpoint_descriptor_t *usbd_get_endpoint_descriptor
    174 			(usbd_interface_handle, u_int8_t);
    175 
    176 usbd_status usbd_reload_device_desc(usbd_device_handle);
    177 
    178 int usbd_ratecheck(struct timeval *);
    179 
    180 usbd_status usbd_get_string(usbd_device_handle, int, char *);
    181 usbd_status usbd_get_string0(usbd_device_handle, int, char *, int);
    182 
    183 /* An iterator for descriptors. */
    184 typedef struct {
    185 	const uByte *cur;
    186 	const uByte *end;
    187 } usbd_desc_iter_t;
    188 void usb_desc_iter_init(usbd_device_handle, usbd_desc_iter_t *);
    189 const usb_descriptor_t *usb_desc_iter_next(usbd_desc_iter_t *);
    190 
    191 /* Used to clear endpoint stalls from the softint */
    192 void usbd_clear_endpoint_stall_async_cb(void *);
    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 /* Match codes. */
    257 #define UMATCH_HIGHEST					15
    258 /* First five codes is for a whole device. */
    259 #define UMATCH_VENDOR_PRODUCT_REV			14
    260 #define UMATCH_VENDOR_PRODUCT				13
    261 #define UMATCH_VENDOR_DEVCLASS_DEVPROTO			12
    262 #define UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO		11
    263 #define UMATCH_DEVCLASS_DEVSUBCLASS			10
    264 /* Next six codes are for interfaces. */
    265 #define UMATCH_VENDOR_PRODUCT_REV_CONF_IFACE		 9
    266 #define UMATCH_VENDOR_PRODUCT_CONF_IFACE		 8
    267 #define UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO		 7
    268 #define UMATCH_VENDOR_IFACESUBCLASS			 6
    269 #define UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO	 5
    270 #define UMATCH_IFACECLASS_IFACESUBCLASS			 4
    271 #define UMATCH_IFACECLASS				 3
    272 #define UMATCH_IFACECLASS_GENERIC			 2
    273 /* Generic driver */
    274 #define UMATCH_GENERIC					 1
    275 /* No match */
    276 #define UMATCH_NONE					 0
    277 
    278 #define splusb splsoftnet
    279 #define splhardusb splbio
    280 #define IPL_USB IPL_BIO
    281 #define IPL_SOFTUSB IPL_SOFTNET
    282 
    283 #endif /* _USBDI_H_ */
    284