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