usbdi.h revision 1.106 1 1.106 riastrad /* $NetBSD: usbdi.h,v 1.106 2022/03/03 06:06:52 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.91 skrll usbd_status usbd_clear_endpoint_stall(struct usbd_pipe *);
129 1.91 skrll void usbd_clear_endpoint_stall_async(struct usbd_pipe *);
130 1.91 skrll
131 1.91 skrll void usbd_clear_endpoint_toggle(struct usbd_pipe *);
132 1.91 skrll usbd_status usbd_endpoint_count(struct usbd_interface *, uint8_t *);
133 1.91 skrll
134 1.91 skrll usbd_status usbd_interface_count(struct usbd_device *, uint8_t *);
135 1.91 skrll
136 1.91 skrll void usbd_interface2device_handle(struct usbd_interface *, struct usbd_device **);
137 1.91 skrll usbd_status usbd_device2interface_handle(struct usbd_device *,
138 1.91 skrll uint8_t, struct usbd_interface **);
139 1.91 skrll
140 1.91 skrll struct usbd_device *usbd_pipe2device_handle(struct usbd_pipe *);
141 1.91 skrll
142 1.91 skrll usbd_status usbd_sync_transfer(struct usbd_xfer *);
143 1.91 skrll usbd_status usbd_sync_transfer_sig(struct usbd_xfer *);
144 1.91 skrll
145 1.91 skrll usbd_status usbd_do_request(struct usbd_device *, usb_device_request_t *, void *);
146 1.94 khorben usbd_status usbd_request_async(struct usbd_device *, struct usbd_xfer *,
147 1.94 khorben usb_device_request_t *, void *, usbd_callback);
148 1.91 skrll usbd_status usbd_do_request_flags(struct usbd_device *, usb_device_request_t *,
149 1.91 skrll void *, uint16_t, int *, uint32_t);
150 1.97 mrg usbd_status usbd_do_request_len(struct usbd_device *dev,
151 1.97 mrg usb_device_request_t *req, size_t len, void *data, uint16_t flags,
152 1.97 mrg int *actlen, uint32_t timeout);
153 1.91 skrll
154 1.91 skrll usb_interface_descriptor_t *
155 1.91 skrll usbd_get_interface_descriptor(struct usbd_interface *);
156 1.91 skrll usb_endpoint_descriptor_t *
157 1.91 skrll usbd_get_endpoint_descriptor(struct usbd_interface *, uint8_t);
158 1.91 skrll
159 1.91 skrll usb_config_descriptor_t *usbd_get_config_descriptor(struct usbd_device *);
160 1.91 skrll usb_device_descriptor_t *usbd_get_device_descriptor(struct usbd_device *);
161 1.91 skrll
162 1.91 skrll usbd_status usbd_set_interface(struct usbd_interface *, int);
163 1.91 skrll usbd_status usbd_get_interface(struct usbd_interface *, uint8_t *);
164 1.91 skrll
165 1.43 augustss int usbd_get_no_alts(usb_config_descriptor_t *, int);
166 1.91 skrll
167 1.91 skrll void usbd_fill_deviceinfo(struct usbd_device *, struct usb_device_info *, int);
168 1.91 skrll int usbd_get_interface_altindex(struct usbd_interface *);
169 1.43 augustss
170 1.63 itojun usb_interface_descriptor_t *usbd_find_idesc(usb_config_descriptor_t *,
171 1.91 skrll int, int);
172 1.63 itojun usb_endpoint_descriptor_t *usbd_find_edesc(usb_config_descriptor_t *,
173 1.91 skrll int, int, int);
174 1.43 augustss
175 1.91 skrll void usbd_dopoll(struct usbd_interface *);
176 1.91 skrll void usbd_set_polling(struct usbd_device *, int);
177 1.23 augustss
178 1.63 itojun const char *usbd_errstr(usbd_status);
179 1.30 augustss
180 1.91 skrll void usbd_add_dev_event(int, struct usbd_device *);
181 1.91 skrll void usbd_add_drv_event(int, struct usbd_device *, device_t);
182 1.1 augustss
183 1.91 skrll char *usbd_devinfo_alloc(struct usbd_device *, int);
184 1.67 christos void usbd_devinfo_free(char *);
185 1.65 augustss
186 1.91 skrll const struct usbd_quirks *usbd_get_quirks(struct usbd_device *);
187 1.38 augustss
188 1.91 skrll usbd_status usbd_reload_device_desc(struct usbd_device *);
189 1.41 augustss
190 1.67 christos int usbd_ratecheck(struct timeval *);
191 1.48 augustss
192 1.91 skrll usbd_status usbd_get_string(struct usbd_device *, int, char *);
193 1.91 skrll usbd_status usbd_get_string0(struct usbd_device *, int, char *, int);
194 1.64 augustss
195 1.101 riastrad /* For use by HCI drivers, not USB device drivers */
196 1.101 riastrad void usbd_xfer_schedule_timeout(struct usbd_xfer *);
197 1.101 riastrad bool usbd_xfer_trycomplete(struct usbd_xfer *);
198 1.101 riastrad void usbd_xfer_abort(struct usbd_xfer *);
199 1.101 riastrad
200 1.84 mrg /* Used to clear endpoint stalls from the softint */
201 1.89 skrll void usbd_clear_endpoint_stall_task(void *);
202 1.84 mrg
203 1.48 augustss /*
204 1.48 augustss * The usb_task structs form a queue of things to run in the USB event
205 1.48 augustss * thread. Normally this is just device discovery when a connect/disconnect
206 1.48 augustss * has been detected. But it may also be used by drivers that need to
207 1.48 augustss * perform (short) tasks that must have a process context.
208 1.48 augustss */
209 1.48 augustss struct usb_task {
210 1.49 augustss TAILQ_ENTRY(usb_task) next;
211 1.48 augustss void (*fun)(void *);
212 1.48 augustss void *arg;
213 1.90 riastrad volatile unsigned queue;
214 1.85 jmcneill int flags;
215 1.48 augustss };
216 1.70 joerg #define USB_TASKQ_HC 0
217 1.70 joerg #define USB_TASKQ_DRIVER 1
218 1.70 joerg #define USB_NUM_TASKQS 2
219 1.70 joerg #define USB_TASKQ_NAMES {"usbtask-hc", "usbtask-dr"}
220 1.87 jmcneill #define USB_TASKQ_MPSAFE 0x80
221 1.48 augustss
222 1.91 skrll void usb_add_task(struct usbd_device *, struct usb_task *, int);
223 1.99 riastrad bool usb_rem_task(struct usbd_device *, struct usb_task *);
224 1.95 riastrad bool usb_rem_task_wait(struct usbd_device *, struct usb_task *, int,
225 1.95 riastrad kmutex_t *);
226 1.100 riastrad bool usb_task_pending(struct usbd_device *, struct usb_task *);
227 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))
228 1.55 augustss
229 1.103 riastrad bool usb_in_event_thread(device_t);
230 1.103 riastrad
231 1.55 augustss struct usb_devno {
232 1.91 skrll uint16_t ud_vendor;
233 1.91 skrll uint16_t ud_product;
234 1.55 augustss };
235 1.63 itojun const struct usb_devno *usb_match_device(const struct usb_devno *,
236 1.91 skrll u_int, u_int, uint16_t, uint16_t);
237 1.56 augustss #define usb_lookup(tbl, vendor, product) \
238 1.91 skrll usb_match_device((const struct usb_devno *)(tbl), sizeof(tbl) / sizeof((tbl)[0]), sizeof((tbl)[0]), (vendor), (product))
239 1.59 augustss #define USB_PRODUCT_ANY 0xffff
240 1.38 augustss
241 1.96 pgoyette /* compat callbacks */
242 1.96 pgoyette void usbd_devinfo_vp(struct usbd_device *, char *, size_t, char *, size_t,
243 1.96 pgoyette int, int);
244 1.96 pgoyette int usbd_printBCD(char *, size_t, int);
245 1.96 pgoyette
246 1.96 pgoyette
247 1.1 augustss /* NetBSD attachment information */
248 1.1 augustss
249 1.1 augustss /* Attach data */
250 1.1 augustss struct usb_attach_arg {
251 1.91 skrll int uaa_port;
252 1.91 skrll int uaa_vendor;
253 1.91 skrll int uaa_product;
254 1.91 skrll int uaa_release;
255 1.91 skrll struct usbd_device * uaa_device; /* current device */
256 1.91 skrll int uaa_class;
257 1.91 skrll int uaa_subclass;
258 1.91 skrll int uaa_proto;
259 1.91 skrll int uaa_usegeneric;
260 1.73 drochner };
261 1.73 drochner
262 1.73 drochner struct usbif_attach_arg {
263 1.91 skrll int uiaa_port;
264 1.91 skrll int uiaa_configno;
265 1.91 skrll int uiaa_ifaceno;
266 1.91 skrll int uiaa_vendor;
267 1.91 skrll int uiaa_product;
268 1.91 skrll int uiaa_release;
269 1.91 skrll struct usbd_device * uiaa_device; /* current device */
270 1.91 skrll
271 1.91 skrll struct usbd_interface * uiaa_iface; /* current interface */
272 1.91 skrll int uiaa_class;
273 1.91 skrll int uiaa_subclass;
274 1.91 skrll int uiaa_proto;
275 1.73 drochner
276 1.73 drochner /* XXX need accounting for interfaces not matched to */
277 1.73 drochner
278 1.91 skrll struct usbd_interface **uiaa_ifaces; /* all interfaces */
279 1.91 skrll int uiaa_nifaces; /* number of interfaces */
280 1.1 augustss };
281 1.1 augustss
282 1.1 augustss /* Match codes. */
283 1.69 augustss #define UMATCH_HIGHEST 15
284 1.1 augustss /* First five codes is for a whole device. */
285 1.1 augustss #define UMATCH_VENDOR_PRODUCT_REV 14
286 1.1 augustss #define UMATCH_VENDOR_PRODUCT 13
287 1.1 augustss #define UMATCH_VENDOR_DEVCLASS_DEVPROTO 12
288 1.1 augustss #define UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO 11
289 1.1 augustss #define UMATCH_DEVCLASS_DEVSUBCLASS 10
290 1.1 augustss /* Next six codes are for interfaces. */
291 1.1 augustss #define UMATCH_VENDOR_PRODUCT_REV_CONF_IFACE 9
292 1.1 augustss #define UMATCH_VENDOR_PRODUCT_CONF_IFACE 8
293 1.1 augustss #define UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO 7
294 1.1 augustss #define UMATCH_VENDOR_IFACESUBCLASS 6
295 1.1 augustss #define UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO 5
296 1.1 augustss #define UMATCH_IFACECLASS_IFACESUBCLASS 4
297 1.1 augustss #define UMATCH_IFACECLASS 3
298 1.1 augustss #define UMATCH_IFACECLASS_GENERIC 2
299 1.1 augustss /* Generic driver */
300 1.1 augustss #define UMATCH_GENERIC 1
301 1.1 augustss /* No match */
302 1.1 augustss #define UMATCH_NONE 0
303 1.12 augustss
304 1.88 skrll
305 1.88 skrll /*
306 1.88 skrll * IPL_USB is defined as IPL_VM for drivers that have not been made MP safe.
307 1.88 skrll * IPL_VM (currently) takes the kernel lock.
308 1.88 skrll *
309 1.88 skrll * Eventually, IPL_USB can/should be changed
310 1.88 skrll */
311 1.92 skrll #define IPL_USB IPL_VM
312 1.88 skrll #define splhardusb splvm
313 1.92 skrll
314 1.92 skrll #define SOFTINT_USB SOFTINT_SERIAL
315 1.92 skrll #define IPL_SOFTUSB IPL_SOFTSERIAL
316 1.92 skrll #define splusb splsoftserial
317 1.74 drochner
318 1.74 drochner #endif /* _USBDI_H_ */
319