Home | History | Annotate | Line # | Download | only in usb
usbdi.h revision 1.101
      1 /*	$NetBSD: usbdi.h,v 1.101 2020/02/12 16:01:00 riastradh 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 struct usbd_bus;
     38 struct usbd_device;
     39 struct usbd_interface;
     40 struct usbd_pipe;
     41 struct usbd_xfer;
     42 
     43 typedef enum {		/* keep in sync with usbd_error_strs */
     44 	USBD_NORMAL_COMPLETION = 0, /* must be 0 */
     45 	USBD_IN_PROGRESS,	/* 1 */
     46 	/* errors */
     47 	USBD_PENDING_REQUESTS,	/* 2 */
     48 	USBD_NOT_STARTED,	/* 3 */
     49 	USBD_INVAL,		/* 4 */
     50 	USBD_NOMEM,		/* 5 */
     51 	USBD_CANCELLED,		/* 6 */
     52 	USBD_BAD_ADDRESS,	/* 7 */
     53 	USBD_IN_USE,		/* 8 */
     54 	USBD_NO_ADDR,		/* 9 */
     55 	USBD_SET_ADDR_FAILED,	/* 10 */
     56 	USBD_NO_POWER,		/* 11 */
     57 	USBD_TOO_DEEP,		/* 12 */
     58 	USBD_IOERROR,		/* 13 */
     59 	USBD_NOT_CONFIGURED,	/* 14 */
     60 	USBD_TIMEOUT,		/* 15 */
     61 	USBD_SHORT_XFER,	/* 16 */
     62 	USBD_STALLED,		/* 17 */
     63 	USBD_INTERRUPTED,	/* 18 */
     64 
     65 	USBD_ERROR_MAX		/* must be last */
     66 } usbd_status;
     67 
     68 typedef void (*usbd_callback)(struct usbd_xfer *, void *, usbd_status);
     69 
     70 /* Use default (specified by ep. desc.) interval on interrupt pipe */
     71 #define USBD_DEFAULT_INTERVAL	(-1)
     72 
     73 /* Open flags */
     74 #define USBD_EXCLUSIVE_USE	0x01
     75 #define USBD_MPSAFE		0x80
     76 
     77 /* Request flags */
     78 #define USBD_SYNCHRONOUS	0x02	/* wait for completion */
     79 /* in usb.h #define USBD_SHORT_XFER_OK	0x04*/	/* allow short reads */
     80 #define USBD_FORCE_SHORT_XFER	0x08	/* force last short packet on write */
     81 #define USBD_SYNCHRONOUS_SIG	0x10	/* if waiting for completion,
     82 					 * also take signals */
     83 
     84 #define USBD_NO_TIMEOUT 0
     85 #define USBD_DEFAULT_TIMEOUT 5000 /* ms = 5 s */
     86 #define	USBD_CONFIG_TIMEOUT  (3*USBD_DEFAULT_TIMEOUT)
     87 
     88 #define DEVINFOSIZE 1024
     89 
     90 usbd_status usbd_open_pipe_intr(struct usbd_interface *, uint8_t, uint8_t,
     91     struct usbd_pipe **, void *, void *, uint32_t, usbd_callback, int);
     92 usbd_status usbd_open_pipe(struct usbd_interface *, uint8_t, uint8_t,
     93      struct usbd_pipe **);
     94 usbd_status usbd_close_pipe(struct usbd_pipe *);
     95 
     96 usbd_status usbd_transfer(struct usbd_xfer *);
     97 
     98 void *usbd_get_buffer(struct usbd_xfer *);
     99 struct usbd_pipe *usbd_get_pipe0(struct usbd_device *);
    100 
    101 int usbd_create_xfer(struct usbd_pipe *, size_t, unsigned int, unsigned int,
    102     struct usbd_xfer **);
    103 void usbd_destroy_xfer(struct usbd_xfer *);
    104 
    105 void usbd_setup_xfer(struct usbd_xfer *, void *, void *,
    106     uint32_t, uint16_t, uint32_t, usbd_callback);
    107 
    108 void usbd_setup_default_xfer(struct usbd_xfer *, struct usbd_device *,
    109     void *, uint32_t, usb_device_request_t *, void *,
    110     uint32_t, uint16_t, usbd_callback);
    111 
    112 void usbd_setup_isoc_xfer(struct usbd_xfer *, void *, uint16_t *,
    113     uint32_t, uint16_t, usbd_callback);
    114 
    115 void usbd_get_xfer_status(struct usbd_xfer *, void **,
    116     void **, uint32_t *, usbd_status *);
    117 
    118 usb_endpoint_descriptor_t *usbd_interface2endpoint_descriptor
    119     (struct usbd_interface *, uint8_t);
    120 
    121 usbd_status usbd_abort_pipe(struct usbd_pipe *);
    122 usbd_status usbd_abort_default_pipe(struct usbd_device *);
    123 
    124 usbd_status usbd_clear_endpoint_stall(struct usbd_pipe *);
    125 void usbd_clear_endpoint_stall_async(struct usbd_pipe *);
    126 
    127 void usbd_clear_endpoint_toggle(struct usbd_pipe *);
    128 usbd_status usbd_endpoint_count(struct usbd_interface *, uint8_t *);
    129 
    130 usbd_status usbd_interface_count(struct usbd_device *, uint8_t *);
    131 
    132 void usbd_interface2device_handle(struct usbd_interface *, struct usbd_device **);
    133 usbd_status usbd_device2interface_handle(struct usbd_device *,
    134     uint8_t, struct usbd_interface **);
    135 
    136 struct usbd_device *usbd_pipe2device_handle(struct usbd_pipe *);
    137 
    138 usbd_status usbd_sync_transfer(struct usbd_xfer *);
    139 usbd_status usbd_sync_transfer_sig(struct usbd_xfer *);
    140 
    141 usbd_status usbd_do_request(struct usbd_device *, usb_device_request_t *, void *);
    142 usbd_status usbd_request_async(struct usbd_device *, struct usbd_xfer *,
    143     usb_device_request_t *, void *, usbd_callback);
    144 usbd_status usbd_do_request_flags(struct usbd_device *, usb_device_request_t *,
    145     void *, uint16_t, int *, uint32_t);
    146 usbd_status usbd_do_request_len(struct usbd_device *dev,
    147     usb_device_request_t *req, size_t len, void *data, uint16_t flags,
    148     int *actlen, uint32_t timeout);
    149 
    150 usb_interface_descriptor_t *
    151     usbd_get_interface_descriptor(struct usbd_interface *);
    152 usb_endpoint_descriptor_t *
    153     usbd_get_endpoint_descriptor(struct usbd_interface *, uint8_t);
    154 
    155 usb_config_descriptor_t *usbd_get_config_descriptor(struct usbd_device *);
    156 usb_device_descriptor_t *usbd_get_device_descriptor(struct usbd_device *);
    157 
    158 usbd_status usbd_set_interface(struct usbd_interface *, int);
    159 usbd_status usbd_get_interface(struct usbd_interface *, uint8_t *);
    160 
    161 int usbd_get_no_alts(usb_config_descriptor_t *, int);
    162 
    163 void usbd_fill_deviceinfo(struct usbd_device *, struct usb_device_info *, int);
    164 int usbd_get_interface_altindex(struct usbd_interface *);
    165 
    166 usb_interface_descriptor_t *usbd_find_idesc(usb_config_descriptor_t *,
    167     int, int);
    168 usb_endpoint_descriptor_t *usbd_find_edesc(usb_config_descriptor_t *,
    169     int, int, int);
    170 
    171 void usbd_dopoll(struct usbd_interface *);
    172 void usbd_set_polling(struct usbd_device *, int);
    173 
    174 const char *usbd_errstr(usbd_status);
    175 
    176 void usbd_add_dev_event(int, struct usbd_device *);
    177 void usbd_add_drv_event(int, struct usbd_device *, device_t);
    178 
    179 char *usbd_devinfo_alloc(struct usbd_device *, int);
    180 void usbd_devinfo_free(char *);
    181 
    182 const struct usbd_quirks *usbd_get_quirks(struct usbd_device *);
    183 
    184 usbd_status usbd_reload_device_desc(struct usbd_device *);
    185 
    186 int usbd_ratecheck(struct timeval *);
    187 
    188 usbd_status usbd_get_string(struct usbd_device *, int, char *);
    189 usbd_status usbd_get_string0(struct usbd_device *, int, char *, int);
    190 
    191 /* For use by HCI drivers, not USB device drivers */
    192 void usbd_xfer_schedule_timeout(struct usbd_xfer *);
    193 bool usbd_xfer_trycomplete(struct usbd_xfer *);
    194 void usbd_xfer_abort(struct usbd_xfer *);
    195 
    196 /* An iterator for descriptors. */
    197 typedef struct {
    198 	const uByte *cur;
    199 	const uByte *end;
    200 } usbd_desc_iter_t;
    201 void usb_desc_iter_init(struct usbd_device *, usbd_desc_iter_t *);
    202 const usb_descriptor_t *usb_desc_iter_peek(usbd_desc_iter_t *);
    203 const usb_descriptor_t *usb_desc_iter_next(usbd_desc_iter_t *);
    204 const usb_interface_descriptor_t *usb_desc_iter_next_interface(usbd_desc_iter_t *);
    205 const usb_descriptor_t *usb_desc_iter_next_non_interface(usbd_desc_iter_t *);
    206 
    207 /* Used to clear endpoint stalls from the softint */
    208 void usbd_clear_endpoint_stall_task(void *);
    209 
    210 /*
    211  * The usb_task structs form a queue of things to run in the USB event
    212  * thread.  Normally this is just device discovery when a connect/disconnect
    213  * has been detected.  But it may also be used by drivers that need to
    214  * perform (short) tasks that must have a process context.
    215  */
    216 struct usb_task {
    217 	TAILQ_ENTRY(usb_task) next;
    218 	void (*fun)(void *);
    219 	void *arg;
    220 	volatile unsigned queue;
    221 	int flags;
    222 };
    223 #define	USB_TASKQ_HC		0
    224 #define	USB_TASKQ_DRIVER	1
    225 #define	USB_NUM_TASKQS		2
    226 #define	USB_TASKQ_NAMES		{"usbtask-hc", "usbtask-dr"}
    227 #define	USB_TASKQ_MPSAFE	0x80
    228 
    229 void usb_add_task(struct usbd_device *, struct usb_task *, int);
    230 bool usb_rem_task(struct usbd_device *, struct usb_task *);
    231 bool usb_rem_task_wait(struct usbd_device *, struct usb_task *, int,
    232     kmutex_t *);
    233 bool usb_task_pending(struct usbd_device *, struct usb_task *);
    234 #define usb_init_task(t, f, a, fl) ((t)->fun = (f), (t)->arg = (a), (t)->queue = USB_NUM_TASKQS, (t)->flags = (fl))
    235 
    236 struct usb_devno {
    237 	uint16_t ud_vendor;
    238 	uint16_t ud_product;
    239 };
    240 const struct usb_devno *usb_match_device(const struct usb_devno *,
    241 	u_int, u_int, uint16_t, uint16_t);
    242 #define usb_lookup(tbl, vendor, product) \
    243 	usb_match_device((const struct usb_devno *)(tbl), sizeof(tbl) / sizeof((tbl)[0]), sizeof((tbl)[0]), (vendor), (product))
    244 #define	USB_PRODUCT_ANY		0xffff
    245 
    246 /* compat callbacks */
    247 void usbd_devinfo_vp(struct usbd_device *, char *, size_t, char *, size_t,
    248     int, int);
    249 int usbd_printBCD(char *, size_t, int);
    250 
    251 
    252 /* NetBSD attachment information */
    253 
    254 /* Attach data */
    255 struct usb_attach_arg {
    256 	int			uaa_port;
    257 	int			uaa_vendor;
    258 	int			uaa_product;
    259 	int			uaa_release;
    260 	struct usbd_device *	uaa_device;	/* current device */
    261 	int			uaa_class;
    262 	int			uaa_subclass;
    263 	int			uaa_proto;
    264 	int			uaa_usegeneric;
    265 };
    266 
    267 struct usbif_attach_arg {
    268 	int			uiaa_port;
    269 	int			uiaa_configno;
    270 	int			uiaa_ifaceno;
    271 	int			uiaa_vendor;
    272 	int			uiaa_product;
    273 	int			uiaa_release;
    274 	struct usbd_device *	uiaa_device;	/* current device */
    275 
    276 	struct usbd_interface *	uiaa_iface;	/* current interface */
    277 	int			uiaa_class;
    278 	int			uiaa_subclass;
    279 	int			uiaa_proto;
    280 
    281 	/* XXX need accounting for interfaces not matched to */
    282 
    283 	struct usbd_interface **uiaa_ifaces;	/* all interfaces */
    284 	int			uiaa_nifaces;	/* number of interfaces */
    285 };
    286 
    287 /* Match codes. */
    288 #define UMATCH_HIGHEST					15
    289 /* First five codes is for a whole device. */
    290 #define UMATCH_VENDOR_PRODUCT_REV			14
    291 #define UMATCH_VENDOR_PRODUCT				13
    292 #define UMATCH_VENDOR_DEVCLASS_DEVPROTO			12
    293 #define UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO		11
    294 #define UMATCH_DEVCLASS_DEVSUBCLASS			10
    295 /* Next six codes are for interfaces. */
    296 #define UMATCH_VENDOR_PRODUCT_REV_CONF_IFACE		 9
    297 #define UMATCH_VENDOR_PRODUCT_CONF_IFACE		 8
    298 #define UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO		 7
    299 #define UMATCH_VENDOR_IFACESUBCLASS			 6
    300 #define UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO	 5
    301 #define UMATCH_IFACECLASS_IFACESUBCLASS			 4
    302 #define UMATCH_IFACECLASS				 3
    303 #define UMATCH_IFACECLASS_GENERIC			 2
    304 /* Generic driver */
    305 #define UMATCH_GENERIC					 1
    306 /* No match */
    307 #define UMATCH_NONE					 0
    308 
    309 
    310 /*
    311  * IPL_USB is defined as IPL_VM for drivers that have not been made MP safe.
    312  * IPL_VM (currently) takes the kernel lock.
    313  *
    314  * Eventually, IPL_USB can/should be changed
    315  */
    316 #define IPL_USB IPL_VM
    317 #define splhardusb splvm
    318 
    319 #define SOFTINT_USB SOFTINT_SERIAL
    320 #define IPL_SOFTUSB IPL_SOFTSERIAL
    321 #define splusb splsoftserial
    322 
    323 #endif /* _USBDI_H_ */
    324