xref: /openbsd/sys/dev/usb/usbdi.h (revision 36bb8c29)
1 /*	$OpenBSD: usbdi.h,v 1.73 2023/10/01 15:58:12 krw Exp $ */
2 /*	$NetBSD: usbdi.h,v 1.62 2002/07/11 21:14:35 augustss 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  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef _USBDI_H_
36 #define _USBDI_H_
37 
38 struct usbd_bus;
39 struct usbd_device;
40 struct usbd_interface;
41 struct usbd_pipe;
42 struct usbd_xfer;
43 
44 typedef enum {
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)(struct usbd_xfer *, void *, usbd_status);
70 
71 /* Open flags */
72 #define USBD_EXCLUSIVE_USE	0x01
73 
74 /* Use default (specified by ep. desc.) interval on interrupt pipe */
75 #define USBD_DEFAULT_INTERVAL	(-1)
76 
77 /* Request flags */
78 #define USBD_NO_COPY		0x01	/* do not copy data to DMA buffer */
79 #define USBD_SYNCHRONOUS	0x02	/* wait for completion */
80 /* in usb.h #define USBD_SHORT_XFER_OK	0x04*/	/* allow short reads */
81 #define USBD_FORCE_SHORT_XFER	0x08	/* force last short packet on write */
82 #define USBD_CATCH		0x10	/* catch signals while sleeping */
83 
84 #define USBD_NO_TIMEOUT 0
85 #define USBD_DEFAULT_TIMEOUT 5000 /* ms = 5 s */
86 
87 #define DEVINFOSIZE 1024
88 
89 usbd_status usbd_open_pipe(struct usbd_interface *iface, u_int8_t address,
90     u_int8_t flags, struct usbd_pipe **pipe);
91 usbd_status usbd_close_pipe(struct usbd_pipe *pipe);
92 usbd_status usbd_transfer(struct usbd_xfer *req);
93 struct usbd_xfer *usbd_alloc_xfer(struct usbd_device *);
94 void usbd_free_xfer(struct usbd_xfer *xfer);
95 void usbd_setup_xfer(struct usbd_xfer *xfer, struct usbd_pipe *pipe,
96     void *priv, void *buffer, u_int32_t length, u_int16_t flags,
97     u_int32_t timeout, usbd_callback);
98 void usbd_setup_default_xfer(struct usbd_xfer *xfer, struct usbd_device *dev,
99     void *priv, u_int32_t timeout, usb_device_request_t *req,
100     void *buffer, u_int32_t length, u_int16_t flags, usbd_callback);
101 void usbd_setup_isoc_xfer(struct usbd_xfer *xfer, struct usbd_pipe *pipe,
102     void *priv, u_int16_t *frlengths, u_int32_t nframes,
103     u_int16_t flags, usbd_callback);
104 void usbd_get_xfer_status(struct usbd_xfer *xfer, void **priv,
105     void **buffer, u_int32_t *count, usbd_status *status);
106 usb_endpoint_descriptor_t *usbd_interface2endpoint_descriptor(
107     struct usbd_interface *iface, u_int8_t address);
108 void usbd_abort_pipe(struct usbd_pipe *pipe);
109 usbd_status usbd_clear_endpoint_stall(struct usbd_pipe *pipe);
110 usbd_status usbd_clear_endpoint_stall_async(struct usbd_pipe *pipe);
111 void usbd_clear_endpoint_toggle(struct usbd_pipe *pipe);
112 usbd_status usbd_device2interface_handle(struct usbd_device *dev,
113     u_int8_t ifaceno, struct usbd_interface **iface);
114 
115 void *usbd_alloc_buffer(struct usbd_xfer *xfer, u_int32_t size);
116 void usbd_free_buffer(struct usbd_xfer *xfer);
117 usbd_status usbd_open_pipe_intr(struct usbd_interface *iface, u_int8_t address,
118     u_int8_t flags, struct usbd_pipe **pipe, void *priv,
119     void *buffer, u_int32_t length, usbd_callback, int);
120 usbd_status usbd_do_request(struct usbd_device *pipe, usb_device_request_t *req,
121     void *data);
122 usbd_status usbd_request_async(struct usbd_xfer *xfer,
123     usb_device_request_t *req, void *priv, usbd_callback callback);
124 usbd_status usbd_do_request_flags(struct usbd_device *pipe,
125     usb_device_request_t *req, void *data, u_int16_t flags, int*, u_int32_t);
126 usb_interface_descriptor_t *usbd_get_interface_descriptor(
127     struct usbd_interface *iface);
128 usb_config_descriptor_t *usbd_get_config_descriptor(struct usbd_device *dev);
129 usb_device_descriptor_t *usbd_get_device_descriptor(struct usbd_device *dev);
130 usbd_status usbd_set_interface(struct usbd_interface *, int);
131 int usbd_get_no_alts(usb_config_descriptor_t *, int);
132 void usbd_fill_deviceinfo(struct usbd_device *, struct usb_device_info *);
133 int usbd_get_routestring(struct usbd_device *, uint32_t *);
134 int usbd_get_location(struct usbd_device *, struct usbd_interface *, uint8_t *,
135     uint32_t *, uint8_t *);
136 usb_config_descriptor_t *usbd_get_cdesc(struct usbd_device *, int, u_int *);
137 int usbd_get_interface_altindex(struct usbd_interface *iface);
138 
139 usb_interface_descriptor_t *usbd_find_idesc(usb_config_descriptor_t *cd,
140     int iindex, int ano);
141 usb_endpoint_descriptor_t *usbd_find_edesc(usb_config_descriptor_t *cd,
142     int ifaceno, int altno, int endptidx);
143 
144 void usbd_dopoll(struct usbd_device *);
145 void usbd_set_polling(struct usbd_device *iface, int on);
146 
147 const char *usbd_errstr(usbd_status err);
148 
149 const struct usbd_quirks *usbd_get_quirks(struct usbd_device *);
150 usb_endpoint_descriptor_t *usbd_get_endpoint_descriptor(
151     struct usbd_interface *iface, u_int8_t address);
152 
153 usbd_status usbd_reload_device_desc(struct usbd_device *);
154 
155 int usbd_ratecheck(struct timeval *last);
156 
157 int usbd_get_devcnt(struct usbd_device *);
158 void usbd_claim_iface(struct usbd_device *, int);
159 int usbd_iface_claimed(struct usbd_device *, int);
160 
161 int usbd_is_dying(struct usbd_device *);
162 void usbd_deactivate(struct usbd_device *);
163 
164 void usbd_ref_incr(struct usbd_device *);
165 void usbd_ref_decr(struct usbd_device *);
166 void usbd_ref_wait(struct usbd_device *);
167 
168 /* An iterator for descriptors. */
169 struct usbd_desc_iter {
170 	const uByte *cur;
171 	const uByte *end;
172 };
173 void usbd_desc_iter_init(struct usbd_device *, struct usbd_desc_iter *);
174 const usb_descriptor_t *usbd_desc_iter_next(struct usbd_desc_iter *);
175 
176 int usbd_str(usb_string_descriptor_t *, int, const char *);
177 
178 /*
179  * The usb_task structs form a queue of things to run in the USB task
180  * threads.  Normally this is just device discovery when a connect/disconnect
181  * has been detected.  But it may also be used by drivers that need to
182  * perform (short) tasks that must have a process context.
183  */
184 struct usb_task {
185 	TAILQ_ENTRY(usb_task) next;
186 	struct usbd_device *dev;
187 	void (*fun)(void *);
188 	void *arg;
189 	char type;
190 #define	USB_TASK_TYPE_GENERIC	0
191 #define USB_TASK_TYPE_EXPLORE	1
192 #define USB_TASK_TYPE_ABORT	2
193 	u_int state;
194 #define	USB_TASK_STATE_NONE	0x0
195 #define	USB_TASK_STATE_ONQ	0x1
196 #define	USB_TASK_STATE_RUN	0x2
197 
198 };
199 
200 void usb_add_task(struct usbd_device *, struct usb_task *);
201 void usb_rem_task(struct usbd_device *, struct usb_task *);
202 void usb_wait_task(struct usbd_device *, struct usb_task *);
203 void usb_rem_wait_task(struct usbd_device *, struct usb_task *);
204 #define usb_init_task(t, f, a, y) \
205 	((t)->fun = (f),	\
206 	(t)->arg = (a),		\
207 	(t)->type = (y),	\
208 	(t)->state = USB_TASK_STATE_NONE)
209 
210 struct usb_devno {
211 	u_int16_t ud_vendor;
212 	u_int16_t ud_product;
213 };
214 const struct usb_devno *usbd_match_device(const struct usb_devno *tbl,
215     u_int nentries, u_int sz, u_int16_t vendor, u_int16_t product);
216 #define usb_lookup(tbl, vendor, product) \
217 	usbd_match_device((const struct usb_devno *)(tbl), sizeof (tbl) / sizeof ((tbl)[0]), sizeof ((tbl)[0]), (vendor), (product))
218 #define	USB_PRODUCT_ANY		0xffff
219 
220 /* Attach data */
221 struct usb_attach_arg {
222 	int			port;
223 	int			configno;
224 	int			ifaceno;
225 	int			vendor;
226 	int			product;
227 	int			release;
228 	struct usbd_device	*device;	/* current device */
229 	struct usbd_interface	*iface; /* current interface */
230 	int			usegeneric;
231 	struct usbd_interface	**ifaces;/* all interfaces */
232 	int			nifaces; /* number of interfaces */
233 	void			*cookie;
234 };
235 
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 #define	IPL_USB		IPL_BIO
258 #define	IPL_SOFTUSB	IPL_SOFTNET
259 
260 #define splusb()	splraise(IPL_SOFTUSB)
261 #define splhardusb()	splraise(IPL_USB)
262 
263 
264 #endif /* _USBDI_H_ */
265