usbdi.h revision 1.103 1 1.103 riastrad /* $NetBSD: usbdi.h,v 1.103 2021/06/13 14:48:10 riastradh Exp $ */
2 1.34 augustss /* $FreeBSD: src/sys/dev/usb/usbdi.h,v 1.18 1999/11/17 22:33:49 n_hibma Exp $ */
3 1.1 augustss
4 1.1 augustss /*
5 1.1 augustss * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 1.1 augustss * All rights reserved.
7 1.1 augustss *
8 1.7 augustss * This code is derived from software contributed to The NetBSD Foundation
9 1.42 augustss * by Lennart Augustsson (lennart (at) augustsson.net) at
10 1.7 augustss * Carlstedt Research & Technology.
11 1.1 augustss *
12 1.1 augustss * Redistribution and use in source and binary forms, with or without
13 1.1 augustss * modification, are permitted provided that the following conditions
14 1.1 augustss * are met:
15 1.1 augustss * 1. Redistributions of source code must retain the above copyright
16 1.1 augustss * notice, this list of conditions and the following disclaimer.
17 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 augustss * notice, this list of conditions and the following disclaimer in the
19 1.1 augustss * documentation and/or other materials provided with the distribution.
20 1.1 augustss *
21 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
32 1.1 augustss */
33 1.1 augustss
34 1.74 drochner #ifndef _USBDI_H_
35 1.74 drochner #define _USBDI_H_
36 1.74 drochner
37 1.91 skrll struct usbd_bus;
38 1.91 skrll struct usbd_device;
39 1.91 skrll struct usbd_interface;
40 1.91 skrll struct usbd_pipe;
41 1.91 skrll struct usbd_xfer;
42 1.1 augustss
43 1.82 jakllsch typedef enum { /* keep in sync with usbd_error_strs */
44 1.23 augustss USBD_NORMAL_COMPLETION = 0, /* must be 0 */
45 1.58 augustss USBD_IN_PROGRESS, /* 1 */
46 1.1 augustss /* errors */
47 1.58 augustss USBD_PENDING_REQUESTS, /* 2 */
48 1.58 augustss USBD_NOT_STARTED, /* 3 */
49 1.58 augustss USBD_INVAL, /* 4 */
50 1.58 augustss USBD_NOMEM, /* 5 */
51 1.58 augustss USBD_CANCELLED, /* 6 */
52 1.58 augustss USBD_BAD_ADDRESS, /* 7 */
53 1.58 augustss USBD_IN_USE, /* 8 */
54 1.58 augustss USBD_NO_ADDR, /* 9 */
55 1.58 augustss USBD_SET_ADDR_FAILED, /* 10 */
56 1.58 augustss USBD_NO_POWER, /* 11 */
57 1.58 augustss USBD_TOO_DEEP, /* 12 */
58 1.58 augustss USBD_IOERROR, /* 13 */
59 1.58 augustss USBD_NOT_CONFIGURED, /* 14 */
60 1.58 augustss USBD_TIMEOUT, /* 15 */
61 1.58 augustss USBD_SHORT_XFER, /* 16 */
62 1.58 augustss USBD_STALLED, /* 17 */
63 1.58 augustss USBD_INTERRUPTED, /* 18 */
64 1.1 augustss
65 1.52 lukem USBD_ERROR_MAX /* must be last */
66 1.1 augustss } usbd_status;
67 1.1 augustss
68 1.91 skrll typedef void (*usbd_callback)(struct usbd_xfer *, void *, usbd_status);
69 1.91 skrll
70 1.91 skrll /* Use default (specified by ep. desc.) interval on interrupt pipe */
71 1.91 skrll #define USBD_DEFAULT_INTERVAL (-1)
72 1.1 augustss
73 1.1 augustss /* Open flags */
74 1.1 augustss #define USBD_EXCLUSIVE_USE 0x01
75 1.87 jmcneill #define USBD_MPSAFE 0x80
76 1.1 augustss
77 1.1 augustss /* Request flags */
78 1.29 augustss #define USBD_SYNCHRONOUS 0x02 /* wait for completion */
79 1.28 augustss /* in usb.h #define USBD_SHORT_XFER_OK 0x04*/ /* allow short reads */
80 1.37 augustss #define USBD_FORCE_SHORT_XFER 0x08 /* force last short packet on write */
81 1.83 mrg #define USBD_SYNCHRONOUS_SIG 0x10 /* if waiting for completion,
82 1.83 mrg * also take signals */
83 1.1 augustss
84 1.1 augustss #define USBD_NO_TIMEOUT 0
85 1.1 augustss #define USBD_DEFAULT_TIMEOUT 5000 /* ms = 5 s */
86 1.78 martin #define USBD_CONFIG_TIMEOUT (3*USBD_DEFAULT_TIMEOUT)
87 1.1 augustss
88 1.66 soren #define DEVINFOSIZE 1024
89 1.66 soren
90 1.91 skrll usbd_status usbd_open_pipe_intr(struct usbd_interface *, uint8_t, uint8_t,
91 1.91 skrll struct usbd_pipe **, void *, void *, uint32_t, usbd_callback, int);
92 1.91 skrll usbd_status usbd_open_pipe(struct usbd_interface *, uint8_t, uint8_t,
93 1.91 skrll struct usbd_pipe **);
94 1.91 skrll usbd_status usbd_close_pipe(struct usbd_pipe *);
95 1.91 skrll
96 1.91 skrll usbd_status usbd_transfer(struct usbd_xfer *);
97 1.91 skrll
98 1.91 skrll void *usbd_get_buffer(struct usbd_xfer *);
99 1.91 skrll struct usbd_pipe *usbd_get_pipe0(struct usbd_device *);
100 1.91 skrll
101 1.91 skrll int usbd_create_xfer(struct usbd_pipe *, size_t, unsigned int, unsigned int,
102 1.91 skrll struct usbd_xfer **);
103 1.91 skrll void usbd_destroy_xfer(struct usbd_xfer *);
104 1.91 skrll
105 1.91 skrll void usbd_setup_xfer(struct usbd_xfer *, void *, void *,
106 1.91 skrll uint32_t, uint16_t, uint32_t, usbd_callback);
107 1.91 skrll
108 1.91 skrll void usbd_setup_default_xfer(struct usbd_xfer *, struct usbd_device *,
109 1.91 skrll void *, uint32_t, usb_device_request_t *, void *,
110 1.91 skrll uint32_t, uint16_t, usbd_callback);
111 1.91 skrll
112 1.91 skrll void usbd_setup_isoc_xfer(struct usbd_xfer *, void *, uint16_t *,
113 1.91 skrll uint32_t, uint16_t, usbd_callback);
114 1.91 skrll
115 1.91 skrll void usbd_get_xfer_status(struct usbd_xfer *, void **,
116 1.91 skrll void **, uint32_t *, usbd_status *);
117 1.91 skrll
118 1.1 augustss usb_endpoint_descriptor_t *usbd_interface2endpoint_descriptor
119 1.91 skrll (struct usbd_interface *, uint8_t);
120 1.91 skrll
121 1.91 skrll usbd_status usbd_abort_pipe(struct usbd_pipe *);
122 1.91 skrll usbd_status usbd_abort_default_pipe(struct usbd_device *);
123 1.91 skrll
124 1.91 skrll usbd_status usbd_clear_endpoint_stall(struct usbd_pipe *);
125 1.91 skrll void usbd_clear_endpoint_stall_async(struct usbd_pipe *);
126 1.91 skrll
127 1.91 skrll void usbd_clear_endpoint_toggle(struct usbd_pipe *);
128 1.91 skrll usbd_status usbd_endpoint_count(struct usbd_interface *, uint8_t *);
129 1.91 skrll
130 1.91 skrll usbd_status usbd_interface_count(struct usbd_device *, uint8_t *);
131 1.91 skrll
132 1.91 skrll void usbd_interface2device_handle(struct usbd_interface *, struct usbd_device **);
133 1.91 skrll usbd_status usbd_device2interface_handle(struct usbd_device *,
134 1.91 skrll uint8_t, struct usbd_interface **);
135 1.91 skrll
136 1.91 skrll struct usbd_device *usbd_pipe2device_handle(struct usbd_pipe *);
137 1.91 skrll
138 1.91 skrll usbd_status usbd_sync_transfer(struct usbd_xfer *);
139 1.91 skrll usbd_status usbd_sync_transfer_sig(struct usbd_xfer *);
140 1.91 skrll
141 1.91 skrll usbd_status usbd_do_request(struct usbd_device *, usb_device_request_t *, void *);
142 1.94 khorben usbd_status usbd_request_async(struct usbd_device *, struct usbd_xfer *,
143 1.94 khorben usb_device_request_t *, void *, usbd_callback);
144 1.91 skrll usbd_status usbd_do_request_flags(struct usbd_device *, usb_device_request_t *,
145 1.91 skrll void *, uint16_t, int *, uint32_t);
146 1.97 mrg usbd_status usbd_do_request_len(struct usbd_device *dev,
147 1.97 mrg usb_device_request_t *req, size_t len, void *data, uint16_t flags,
148 1.97 mrg int *actlen, uint32_t timeout);
149 1.91 skrll
150 1.91 skrll usb_interface_descriptor_t *
151 1.91 skrll usbd_get_interface_descriptor(struct usbd_interface *);
152 1.91 skrll usb_endpoint_descriptor_t *
153 1.91 skrll usbd_get_endpoint_descriptor(struct usbd_interface *, uint8_t);
154 1.91 skrll
155 1.91 skrll usb_config_descriptor_t *usbd_get_config_descriptor(struct usbd_device *);
156 1.91 skrll usb_device_descriptor_t *usbd_get_device_descriptor(struct usbd_device *);
157 1.91 skrll
158 1.91 skrll usbd_status usbd_set_interface(struct usbd_interface *, int);
159 1.91 skrll usbd_status usbd_get_interface(struct usbd_interface *, uint8_t *);
160 1.91 skrll
161 1.43 augustss int usbd_get_no_alts(usb_config_descriptor_t *, int);
162 1.91 skrll
163 1.91 skrll void usbd_fill_deviceinfo(struct usbd_device *, struct usb_device_info *, int);
164 1.91 skrll int usbd_get_interface_altindex(struct usbd_interface *);
165 1.43 augustss
166 1.63 itojun usb_interface_descriptor_t *usbd_find_idesc(usb_config_descriptor_t *,
167 1.91 skrll int, int);
168 1.63 itojun usb_endpoint_descriptor_t *usbd_find_edesc(usb_config_descriptor_t *,
169 1.91 skrll int, int, int);
170 1.43 augustss
171 1.91 skrll void usbd_dopoll(struct usbd_interface *);
172 1.91 skrll void usbd_set_polling(struct usbd_device *, int);
173 1.23 augustss
174 1.63 itojun const char *usbd_errstr(usbd_status);
175 1.30 augustss
176 1.91 skrll void usbd_add_dev_event(int, struct usbd_device *);
177 1.91 skrll void usbd_add_drv_event(int, struct usbd_device *, device_t);
178 1.1 augustss
179 1.91 skrll char *usbd_devinfo_alloc(struct usbd_device *, int);
180 1.67 christos void usbd_devinfo_free(char *);
181 1.65 augustss
182 1.91 skrll const struct usbd_quirks *usbd_get_quirks(struct usbd_device *);
183 1.38 augustss
184 1.91 skrll usbd_status usbd_reload_device_desc(struct usbd_device *);
185 1.41 augustss
186 1.67 christos int usbd_ratecheck(struct timeval *);
187 1.48 augustss
188 1.91 skrll usbd_status usbd_get_string(struct usbd_device *, int, char *);
189 1.91 skrll usbd_status usbd_get_string0(struct usbd_device *, int, char *, int);
190 1.64 augustss
191 1.101 riastrad /* For use by HCI drivers, not USB device drivers */
192 1.101 riastrad void usbd_xfer_schedule_timeout(struct usbd_xfer *);
193 1.101 riastrad bool usbd_xfer_trycomplete(struct usbd_xfer *);
194 1.101 riastrad void usbd_xfer_abort(struct usbd_xfer *);
195 1.101 riastrad
196 1.84 mrg /* Used to clear endpoint stalls from the softint */
197 1.89 skrll void usbd_clear_endpoint_stall_task(void *);
198 1.84 mrg
199 1.48 augustss /*
200 1.48 augustss * The usb_task structs form a queue of things to run in the USB event
201 1.48 augustss * thread. Normally this is just device discovery when a connect/disconnect
202 1.48 augustss * has been detected. But it may also be used by drivers that need to
203 1.48 augustss * perform (short) tasks that must have a process context.
204 1.48 augustss */
205 1.48 augustss struct usb_task {
206 1.49 augustss TAILQ_ENTRY(usb_task) next;
207 1.48 augustss void (*fun)(void *);
208 1.48 augustss void *arg;
209 1.90 riastrad volatile unsigned queue;
210 1.85 jmcneill int flags;
211 1.48 augustss };
212 1.70 joerg #define USB_TASKQ_HC 0
213 1.70 joerg #define USB_TASKQ_DRIVER 1
214 1.70 joerg #define USB_NUM_TASKQS 2
215 1.70 joerg #define USB_TASKQ_NAMES {"usbtask-hc", "usbtask-dr"}
216 1.87 jmcneill #define USB_TASKQ_MPSAFE 0x80
217 1.48 augustss
218 1.91 skrll void usb_add_task(struct usbd_device *, struct usb_task *, int);
219 1.99 riastrad bool usb_rem_task(struct usbd_device *, struct usb_task *);
220 1.95 riastrad bool usb_rem_task_wait(struct usbd_device *, struct usb_task *, int,
221 1.95 riastrad kmutex_t *);
222 1.100 riastrad bool usb_task_pending(struct usbd_device *, struct usb_task *);
223 1.90 riastrad #define usb_init_task(t, f, a, fl) ((t)->fun = (f), (t)->arg = (a), (t)->queue = USB_NUM_TASKQS, (t)->flags = (fl))
224 1.55 augustss
225 1.103 riastrad bool usb_in_event_thread(device_t);
226 1.103 riastrad
227 1.55 augustss struct usb_devno {
228 1.91 skrll uint16_t ud_vendor;
229 1.91 skrll uint16_t ud_product;
230 1.55 augustss };
231 1.63 itojun const struct usb_devno *usb_match_device(const struct usb_devno *,
232 1.91 skrll u_int, u_int, uint16_t, uint16_t);
233 1.56 augustss #define usb_lookup(tbl, vendor, product) \
234 1.91 skrll usb_match_device((const struct usb_devno *)(tbl), sizeof(tbl) / sizeof((tbl)[0]), sizeof((tbl)[0]), (vendor), (product))
235 1.59 augustss #define USB_PRODUCT_ANY 0xffff
236 1.38 augustss
237 1.96 pgoyette /* compat callbacks */
238 1.96 pgoyette void usbd_devinfo_vp(struct usbd_device *, char *, size_t, char *, size_t,
239 1.96 pgoyette int, int);
240 1.96 pgoyette int usbd_printBCD(char *, size_t, int);
241 1.96 pgoyette
242 1.96 pgoyette
243 1.1 augustss /* NetBSD attachment information */
244 1.1 augustss
245 1.1 augustss /* Attach data */
246 1.1 augustss struct usb_attach_arg {
247 1.91 skrll int uaa_port;
248 1.91 skrll int uaa_vendor;
249 1.91 skrll int uaa_product;
250 1.91 skrll int uaa_release;
251 1.91 skrll struct usbd_device * uaa_device; /* current device */
252 1.91 skrll int uaa_class;
253 1.91 skrll int uaa_subclass;
254 1.91 skrll int uaa_proto;
255 1.91 skrll int uaa_usegeneric;
256 1.73 drochner };
257 1.73 drochner
258 1.73 drochner struct usbif_attach_arg {
259 1.91 skrll int uiaa_port;
260 1.91 skrll int uiaa_configno;
261 1.91 skrll int uiaa_ifaceno;
262 1.91 skrll int uiaa_vendor;
263 1.91 skrll int uiaa_product;
264 1.91 skrll int uiaa_release;
265 1.91 skrll struct usbd_device * uiaa_device; /* current device */
266 1.91 skrll
267 1.91 skrll struct usbd_interface * uiaa_iface; /* current interface */
268 1.91 skrll int uiaa_class;
269 1.91 skrll int uiaa_subclass;
270 1.91 skrll int uiaa_proto;
271 1.73 drochner
272 1.73 drochner /* XXX need accounting for interfaces not matched to */
273 1.73 drochner
274 1.91 skrll struct usbd_interface **uiaa_ifaces; /* all interfaces */
275 1.91 skrll int uiaa_nifaces; /* number of interfaces */
276 1.1 augustss };
277 1.1 augustss
278 1.1 augustss /* Match codes. */
279 1.69 augustss #define UMATCH_HIGHEST 15
280 1.1 augustss /* First five codes is for a whole device. */
281 1.1 augustss #define UMATCH_VENDOR_PRODUCT_REV 14
282 1.1 augustss #define UMATCH_VENDOR_PRODUCT 13
283 1.1 augustss #define UMATCH_VENDOR_DEVCLASS_DEVPROTO 12
284 1.1 augustss #define UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO 11
285 1.1 augustss #define UMATCH_DEVCLASS_DEVSUBCLASS 10
286 1.1 augustss /* Next six codes are for interfaces. */
287 1.1 augustss #define UMATCH_VENDOR_PRODUCT_REV_CONF_IFACE 9
288 1.1 augustss #define UMATCH_VENDOR_PRODUCT_CONF_IFACE 8
289 1.1 augustss #define UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO 7
290 1.1 augustss #define UMATCH_VENDOR_IFACESUBCLASS 6
291 1.1 augustss #define UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO 5
292 1.1 augustss #define UMATCH_IFACECLASS_IFACESUBCLASS 4
293 1.1 augustss #define UMATCH_IFACECLASS 3
294 1.1 augustss #define UMATCH_IFACECLASS_GENERIC 2
295 1.1 augustss /* Generic driver */
296 1.1 augustss #define UMATCH_GENERIC 1
297 1.1 augustss /* No match */
298 1.1 augustss #define UMATCH_NONE 0
299 1.12 augustss
300 1.88 skrll
301 1.88 skrll /*
302 1.88 skrll * IPL_USB is defined as IPL_VM for drivers that have not been made MP safe.
303 1.88 skrll * IPL_VM (currently) takes the kernel lock.
304 1.88 skrll *
305 1.88 skrll * Eventually, IPL_USB can/should be changed
306 1.88 skrll */
307 1.92 skrll #define IPL_USB IPL_VM
308 1.88 skrll #define splhardusb splvm
309 1.92 skrll
310 1.92 skrll #define SOFTINT_USB SOFTINT_SERIAL
311 1.92 skrll #define IPL_SOFTUSB IPL_SOFTSERIAL
312 1.92 skrll #define splusb splsoftserial
313 1.74 drochner
314 1.74 drochner #endif /* _USBDI_H_ */
315