usbdivar.h revision 1.136 1 1.136 riastrad /* $NetBSD: usbdivar.h,v 1.136 2022/03/13 11:28:42 riastradh Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.97 mrg * Copyright (c) 1998, 2012 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.8 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.56 augustss * by Lennart Augustsson (lennart (at) augustsson.net) at
9 1.97 mrg * Carlstedt Research & Technology and Matthew R. Green (mrg (at) eterna.com.au).
10 1.1 augustss *
11 1.1 augustss * Redistribution and use in source and binary forms, with or without
12 1.1 augustss * modification, are permitted provided that the following conditions
13 1.1 augustss * are met:
14 1.1 augustss * 1. Redistributions of source code must retain the above copyright
15 1.1 augustss * notice, this list of conditions and the following disclaimer.
16 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 augustss * notice, this list of conditions and the following disclaimer in the
18 1.1 augustss * documentation and/or other materials provided with the distribution.
19 1.1 augustss *
20 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
31 1.1 augustss */
32 1.1 augustss
33 1.131 riastrad #ifndef _DEV_USB_USBDIVAR_H_
34 1.131 riastrad #define _DEV_USB_USBDIVAR_H_
35 1.131 riastrad
36 1.97 mrg /*
37 1.97 mrg * Discussion about locking in the USB code:
38 1.97 mrg *
39 1.104 skrll * The host controller presents one lock at IPL_SOFTUSB (aka IPL_SOFTNET).
40 1.97 mrg *
41 1.99 mrg * List of hardware interface methods, and whether the lock is held
42 1.99 mrg * when each is called by this module:
43 1.97 mrg *
44 1.113 skrll * BUS METHOD LOCK NOTES
45 1.97 mrg * ----------------------- ------- -------------------------
46 1.113 skrll * ubm_open - might want to take lock?
47 1.133 riastrad * ubm_softint x may release/reacquire lock
48 1.113 skrll * ubm_dopoll - might want to take lock?
49 1.113 skrll * ubm_allocx -
50 1.113 skrll * ubm_freex -
51 1.133 riastrad * ubm_abortx x must not release/reacquire lock
52 1.113 skrll * ubm_getlock - Called at attach time
53 1.113 skrll * ubm_newdev - Will take lock
54 1.135 riastrad * ubm_rhctrl -
55 1.97 mrg *
56 1.113 skrll * PIPE METHOD LOCK NOTES
57 1.97 mrg * ----------------------- ------- -------------------------
58 1.133 riastrad * upm_init -
59 1.133 riastrad * upm_fini -
60 1.134 riastrad * upm_transfer x
61 1.134 riastrad * upm_start x
62 1.113 skrll * upm_abort x
63 1.113 skrll * upm_close x
64 1.113 skrll * upm_cleartoggle -
65 1.113 skrll * upm_done x
66 1.97 mrg *
67 1.97 mrg * The above semantics are likely to change. Little performance
68 1.97 mrg * evaluation has been done on this code and the locking strategy.
69 1.109 skrll *
70 1.97 mrg * USB functions known to expect the lock taken include (this list is
71 1.97 mrg * probably not exhaustive):
72 1.97 mrg * usb_transfer_complete()
73 1.97 mrg * usb_start_next()
74 1.97 mrg *
75 1.97 mrg */
76 1.97 mrg
77 1.48 thorpej #include <sys/callout.h>
78 1.97 mrg #include <sys/mutex.h>
79 1.96 mrg #include <sys/bus.h>
80 1.48 thorpej
81 1.29 augustss /* From usb_mem.h */
82 1.89 dyoung struct usb_dma_block;
83 1.89 dyoung typedef struct {
84 1.113 skrll struct usb_dma_block *udma_block;
85 1.113 skrll u_int udma_offs;
86 1.89 dyoung } usb_dma_t;
87 1.29 augustss
88 1.40 augustss struct usbd_xfer;
89 1.1 augustss struct usbd_pipe;
90 1.105 jakllsch struct usbd_port;
91 1.1 augustss
92 1.1 augustss struct usbd_endpoint {
93 1.113 skrll usb_endpoint_descriptor_t *ue_edesc;
94 1.113 skrll int ue_refcnt;
95 1.113 skrll int ue_toggle;
96 1.1 augustss };
97 1.1 augustss
98 1.29 augustss struct usbd_bus_methods {
99 1.113 skrll usbd_status (*ubm_open)(struct usbd_pipe *);
100 1.113 skrll void (*ubm_softint)(void *);
101 1.113 skrll void (*ubm_dopoll)(struct usbd_bus *);
102 1.113 skrll struct usbd_xfer *(*ubm_allocx)(struct usbd_bus *, unsigned int);
103 1.113 skrll void (*ubm_freex)(struct usbd_bus *, struct usbd_xfer *);
104 1.122 riastrad void (*ubm_abortx)(struct usbd_xfer *);
105 1.122 riastrad bool (*ubm_dying)(struct usbd_bus *);
106 1.113 skrll void (*ubm_getlock)(struct usbd_bus *, kmutex_t **);
107 1.113 skrll usbd_status (*ubm_newdev)(device_t, struct usbd_bus *, int,
108 1.105 jakllsch int, int, struct usbd_port *);
109 1.113 skrll
110 1.113 skrll int (*ubm_rhctrl)(struct usbd_bus *,
111 1.113 skrll usb_device_request_t *, void *, int);
112 1.29 augustss };
113 1.29 augustss
114 1.29 augustss struct usbd_pipe_methods {
115 1.113 skrll int (*upm_init)(struct usbd_xfer *);
116 1.113 skrll void (*upm_fini)(struct usbd_xfer *);
117 1.113 skrll usbd_status (*upm_transfer)(struct usbd_xfer *);
118 1.113 skrll usbd_status (*upm_start)(struct usbd_xfer *);
119 1.113 skrll void (*upm_abort)(struct usbd_xfer *);
120 1.113 skrll void (*upm_close)(struct usbd_pipe *);
121 1.113 skrll void (*upm_cleartoggle)(struct usbd_pipe *);
122 1.113 skrll void (*upm_done)(struct usbd_xfer *);
123 1.1 augustss };
124 1.1 augustss
125 1.72 augustss struct usbd_tt {
126 1.113 skrll struct usbd_hub *utt_hub;
127 1.72 augustss };
128 1.72 augustss
129 1.1 augustss struct usbd_port {
130 1.113 skrll usb_port_status_t up_status;
131 1.113 skrll uint16_t up_power; /* mA of current on port */
132 1.113 skrll uint8_t up_portno;
133 1.113 skrll uint8_t up_restartcnt;
134 1.11 augustss #define USBD_RESTART_MAX 5
135 1.113 skrll uint8_t up_reattach;
136 1.113 skrll struct usbd_device *up_dev; /* Connected device */
137 1.113 skrll struct usbd_device *up_parent; /* The ports hub */
138 1.113 skrll struct usbd_tt *up_tt; /* Transaction translator (if any) */
139 1.1 augustss };
140 1.1 augustss
141 1.1 augustss struct usbd_hub {
142 1.113 skrll usbd_status (*uh_explore)(struct usbd_device *hub);
143 1.113 skrll void *uh_hubsoftc;
144 1.113 skrll usb_hub_descriptor_t uh_hubdesc;
145 1.113 skrll struct usbd_port uh_ports[1];
146 1.1 augustss };
147 1.1 augustss
148 1.1 augustss /*****/
149 1.114 skrll /* 0, root, and 1->127 */
150 1.114 skrll #define USB_ROOTHUB_INDEX 1
151 1.114 skrll #define USB_TOTAL_DEVICES (USB_MAX_DEVICES + 1)
152 1.1 augustss
153 1.1 augustss struct usbd_bus {
154 1.1 augustss /* Filled by HC driver */
155 1.113 skrll void *ub_hcpriv;
156 1.113 skrll int ub_revision; /* USB revision */
157 1.42 augustss #define USBREV_UNKNOWN 0
158 1.42 augustss #define USBREV_PRE_1_0 1
159 1.42 augustss #define USBREV_1_0 2
160 1.42 augustss #define USBREV_1_1 3
161 1.60 augustss #define USBREV_2_0 4
162 1.108 skrll #define USBREV_3_0 5
163 1.116 msaitoh #define USBREV_3_1 6
164 1.116 msaitoh #define USBREV_STR { "unknown", "pre 1.0", "1.0", "1.1", "2.0", "3.0", "3.1" }
165 1.123 maxv int ub_hctype;
166 1.123 maxv #define USBHCTYPE_UNKNOWN 0
167 1.123 maxv #define USBHCTYPE_MOTG 1
168 1.123 maxv #define USBHCTYPE_OHCI 2
169 1.123 maxv #define USBHCTYPE_UHCI 3
170 1.123 maxv #define USBHCTYPE_EHCI 4
171 1.123 maxv #define USBHCTYPE_XHCI 5
172 1.123 maxv #define USBHCTYPE_VHCI 6
173 1.124 maxv int ub_busnum;
174 1.113 skrll const struct usbd_bus_methods
175 1.113 skrll *ub_methods;
176 1.113 skrll uint32_t ub_pipesize; /* size of a pipe struct */
177 1.113 skrll bool ub_usedma; /* Does this HC support DMA */
178 1.113 skrll int ub_dmaflags;
179 1.113 skrll bus_dma_tag_t ub_dmatag; /* DMA tag */
180 1.113 skrll
181 1.113 skrll /* Filled by usb driver */
182 1.113 skrll kmutex_t *ub_lock;
183 1.113 skrll struct usbd_device *ub_roothub;
184 1.113 skrll uint8_t ub_rhaddr; /* roothub address */
185 1.113 skrll uint8_t ub_rhconf; /* roothub configuration */
186 1.114 skrll struct usbd_device *ub_devices[USB_TOTAL_DEVICES];
187 1.113 skrll kcondvar_t ub_needsexplore_cv;
188 1.113 skrll char ub_needsexplore;/* a hub a signalled a change */
189 1.113 skrll char ub_usepolling;
190 1.113 skrll device_t ub_usbctl;
191 1.113 skrll struct usb_device_stats ub_stats;
192 1.113 skrll
193 1.113 skrll void *ub_soft; /* soft interrupt cookie */
194 1.1 augustss };
195 1.1 augustss
196 1.1 augustss struct usbd_device {
197 1.113 skrll struct usbd_bus *ud_bus; /* our controller */
198 1.113 skrll struct usbd_pipe *ud_pipe0; /* pipe 0 */
199 1.129 andvar uint8_t ud_addr; /* device address */
200 1.113 skrll uint8_t ud_config; /* current configuration # */
201 1.113 skrll uint8_t ud_depth; /* distance from root hub */
202 1.113 skrll uint8_t ud_speed; /* low/full/high speed */
203 1.113 skrll uint8_t ud_selfpowered; /* flag for self powered */
204 1.113 skrll uint16_t ud_power; /* mA the device uses */
205 1.113 skrll int16_t ud_langid; /* language for strings */
206 1.13 augustss #define USBD_NOLANG (-1)
207 1.113 skrll usb_event_cookie_t ud_cookie; /* unique connection id */
208 1.113 skrll struct usbd_port *ud_powersrc; /* upstream hub port, or 0 */
209 1.113 skrll struct usbd_device *ud_myhub; /* upstream hub */
210 1.113 skrll struct usbd_port *ud_myhsport; /* closest high speed port */
211 1.113 skrll struct usbd_endpoint ud_ep0; /* for pipe 0 */
212 1.113 skrll usb_endpoint_descriptor_t
213 1.113 skrll ud_ep0desc; /* for pipe 0 */
214 1.113 skrll struct usbd_interface *ud_ifaces; /* array of all interfaces */
215 1.113 skrll usb_device_descriptor_t ud_ddesc; /* device descriptor */
216 1.113 skrll usb_config_descriptor_t *ud_cdesc; /* full config descr */
217 1.113 skrll usb_bos_descriptor_t *ud_bdesc; /* full BOS descr */
218 1.113 skrll const struct usbd_quirks
219 1.113 skrll *ud_quirks; /* device quirks, always set */
220 1.113 skrll struct usbd_hub *ud_hub; /* only if this is a hub */
221 1.119 christos u_int ud_subdevlen; /* array length of following */
222 1.113 skrll device_t *ud_subdevs; /* sub-devices */
223 1.113 skrll int ud_nifaces_claimed; /* number of ifaces in use */
224 1.113 skrll void *ud_hcpriv;
225 1.111 skrll
226 1.111 skrll char *ud_serial; /* serial number, can be NULL */
227 1.111 skrll char *ud_vendor; /* vendor string, can be NULL */
228 1.111 skrll char *ud_product; /* product string can be NULL */
229 1.1 augustss };
230 1.1 augustss
231 1.1 augustss struct usbd_interface {
232 1.113 skrll struct usbd_device *ui_dev;
233 1.113 skrll usb_interface_descriptor_t
234 1.113 skrll *ui_idesc;
235 1.113 skrll int ui_index;
236 1.113 skrll int ui_altindex;
237 1.113 skrll struct usbd_endpoint *ui_endpoints;
238 1.128 riastrad int64_t ui_busy; /* #pipes, or -1 if setting */
239 1.1 augustss };
240 1.1 augustss
241 1.1 augustss struct usbd_pipe {
242 1.113 skrll struct usbd_interface *up_iface;
243 1.113 skrll struct usbd_device *up_dev;
244 1.113 skrll struct usbd_endpoint *up_endpoint;
245 1.113 skrll char up_running;
246 1.113 skrll char up_aborting;
247 1.113 skrll bool up_serialise;
248 1.113 skrll SIMPLEQ_HEAD(, usbd_xfer)
249 1.113 skrll up_queue;
250 1.113 skrll struct usb_task up_async_task;
251 1.113 skrll
252 1.113 skrll struct usbd_xfer *up_intrxfer; /* used for repeating requests */
253 1.113 skrll char up_repeat;
254 1.113 skrll int up_interval;
255 1.113 skrll uint8_t up_flags;
256 1.52 augustss
257 1.130 riastrad struct usbd_xfer *up_callingxfer; /* currently in callback */
258 1.130 riastrad kcondvar_t up_callingcv;
259 1.130 riastrad
260 1.136 riastrad struct lwp *up_abortlwp; /* lwp currently aborting */
261 1.136 riastrad
262 1.1 augustss /* Filled by HC driver. */
263 1.113 skrll const struct usbd_pipe_methods
264 1.113 skrll *up_methods;
265 1.1 augustss };
266 1.1 augustss
267 1.40 augustss struct usbd_xfer {
268 1.113 skrll struct usbd_pipe *ux_pipe;
269 1.113 skrll void *ux_priv;
270 1.113 skrll void *ux_buffer;
271 1.113 skrll kcondvar_t ux_cv;
272 1.113 skrll uint32_t ux_length;
273 1.113 skrll uint32_t ux_actlen;
274 1.113 skrll uint16_t ux_flags;
275 1.113 skrll uint32_t ux_timeout;
276 1.113 skrll usbd_status ux_status;
277 1.113 skrll usbd_callback ux_callback;
278 1.113 skrll volatile uint8_t ux_done;
279 1.113 skrll uint8_t ux_state; /* used for DIAGNOSTIC */
280 1.77 chs #define XFER_FREE 0x46
281 1.77 chs #define XFER_BUSY 0x55
282 1.77 chs #define XFER_ONQU 0x9e
283 1.1 augustss
284 1.29 augustss /* For control pipe */
285 1.113 skrll usb_device_request_t ux_request;
286 1.29 augustss
287 1.29 augustss /* For isoc */
288 1.113 skrll uint16_t *ux_frlengths;
289 1.113 skrll int ux_nframes;
290 1.113 skrll
291 1.113 skrll const struct usbd_pipe_methods *ux_methods;
292 1.29 augustss
293 1.113 skrll /* For memory allocation and softc */
294 1.113 skrll struct usbd_bus *ux_bus;
295 1.113 skrll usb_dma_t ux_dmabuf;
296 1.113 skrll void *ux_buf;
297 1.113 skrll uint32_t ux_bufsize;
298 1.29 augustss
299 1.113 skrll uint8_t ux_rqflags;
300 1.29 augustss #define URQ_REQUEST 0x01
301 1.1 augustss
302 1.113 skrll SIMPLEQ_ENTRY(usbd_xfer)
303 1.113 skrll ux_next;
304 1.1 augustss
305 1.113 skrll void *ux_hcpriv; /* private use by the HC driver */
306 1.11 augustss
307 1.117 mrg struct usb_task ux_aborttask;
308 1.113 skrll struct callout ux_callout;
309 1.121 riastrad
310 1.121 riastrad /*
311 1.121 riastrad * Protected by bus lock.
312 1.121 riastrad *
313 1.121 riastrad * - ux_timeout_set: The timeout is scheduled as a callout or
314 1.121 riastrad * usb task, and has not yet acquired the bus lock.
315 1.121 riastrad *
316 1.121 riastrad * - ux_timeout_reset: The xfer completed, and was resubmitted
317 1.121 riastrad * before the callout or task was able to acquire the bus
318 1.121 riastrad * lock, so one or the other needs to schedule a new callout.
319 1.121 riastrad */
320 1.121 riastrad bool ux_timeout_set;
321 1.121 riastrad bool ux_timeout_reset;
322 1.1 augustss };
323 1.1 augustss
324 1.57 augustss void usbd_init(void);
325 1.57 augustss void usbd_finish(void);
326 1.64 augustss
327 1.113 skrll #if defined(USB_DEBUG)
328 1.113 skrll void usbd_dump_iface(struct usbd_interface *);
329 1.113 skrll void usbd_dump_device(struct usbd_device *);
330 1.113 skrll void usbd_dump_endpoint(struct usbd_endpoint *);
331 1.113 skrll void usbd_dump_queue(struct usbd_pipe *);
332 1.113 skrll void usbd_dump_pipe(struct usbd_pipe *);
333 1.67 augustss #endif
334 1.1 augustss
335 1.1 augustss /* Routines from usb_subr.c */
336 1.57 augustss int usbctlprint(void *, const char *);
337 1.111 skrll void usbd_get_device_strings(struct usbd_device *);
338 1.113 skrll void usb_delay_ms_locked(struct usbd_bus *, u_int, kmutex_t *);
339 1.113 skrll void usb_delay_ms(struct usbd_bus *, u_int);
340 1.113 skrll void usbd_delay_ms_locked(struct usbd_device *, u_int, kmutex_t *);
341 1.113 skrll void usbd_delay_ms(struct usbd_device *, u_int);
342 1.113 skrll usbd_status usbd_reset_port(struct usbd_device *, int, usb_port_status_t *);
343 1.113 skrll usbd_status usbd_setup_pipe(struct usbd_device *,
344 1.113 skrll struct usbd_interface *,
345 1.57 augustss struct usbd_endpoint *, int,
346 1.113 skrll struct usbd_pipe **);
347 1.113 skrll usbd_status usbd_setup_pipe_flags(struct usbd_device *,
348 1.113 skrll struct usbd_interface *,
349 1.101 jmcneill struct usbd_endpoint *, int,
350 1.113 skrll struct usbd_pipe **,
351 1.113 skrll uint8_t);
352 1.113 skrll usbd_status usbd_new_device(device_t, struct usbd_bus *, int, int, int,
353 1.102 skrll struct usbd_port *);
354 1.113 skrll usbd_status usbd_reattach_device(device_t, struct usbd_device *,
355 1.102 skrll int, const int *);
356 1.88 kent
357 1.113 skrll void usbd_remove_device(struct usbd_device *, struct usbd_port *);
358 1.128 riastrad bool usbd_iface_locked(struct usbd_interface *);
359 1.128 riastrad usbd_status usbd_iface_lock(struct usbd_interface *);
360 1.128 riastrad void usbd_iface_unlock(struct usbd_interface *);
361 1.128 riastrad usbd_status usbd_iface_piperef(struct usbd_interface *);
362 1.128 riastrad void usbd_iface_pipeunref(struct usbd_interface *);
363 1.113 skrll usbd_status usbd_fill_iface_data(struct usbd_device *, int, int);
364 1.113 skrll void usb_free_device(struct usbd_device *);
365 1.57 augustss
366 1.113 skrll void usb_transfer_complete(struct usbd_xfer *);
367 1.91 dyoung int usb_disconnect_port(struct usbd_port *, device_t, int);
368 1.1 augustss
369 1.125 riastrad usbd_status usbd_endpoint_acquire(struct usbd_device *,
370 1.125 riastrad struct usbd_endpoint *, int);
371 1.125 riastrad void usbd_endpoint_release(struct usbd_device *,
372 1.125 riastrad struct usbd_endpoint *);
373 1.125 riastrad
374 1.113 skrll void usbd_kill_pipe(struct usbd_pipe *);
375 1.113 skrll usbd_status usbd_attach_roothub(device_t, struct usbd_device *);
376 1.113 skrll usbd_status usbd_probe_and_attach(device_t, struct usbd_device *, int, int);
377 1.105 jakllsch
378 1.1 augustss /* Routines from usb.c */
379 1.113 skrll void usb_needs_explore(struct usbd_device *);
380 1.113 skrll void usb_needs_reattach(struct usbd_device *);
381 1.57 augustss void usb_schedsoftintr(struct usbd_bus *);
382 1.97 mrg
383 1.115 christos static __inline int
384 1.110 skrll usbd_xfer_isread(struct usbd_xfer *xfer)
385 1.110 skrll {
386 1.113 skrll if (xfer->ux_rqflags & URQ_REQUEST)
387 1.113 skrll return xfer->ux_request.bmRequestType & UT_READ;
388 1.110 skrll
389 1.113 skrll return xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress &
390 1.113 skrll UE_DIR_IN;
391 1.110 skrll }
392 1.110 skrll
393 1.115 christos static __inline size_t
394 1.114 skrll usb_addr2dindex(int addr)
395 1.114 skrll {
396 1.114 skrll
397 1.114 skrll return USB_ROOTHUB_INDEX + addr;
398 1.114 skrll }
399 1.114 skrll
400 1.97 mrg /*
401 1.107 skrll * These macros reflect the current locking scheme. They might change.
402 1.97 mrg */
403 1.97 mrg
404 1.113 skrll #define usbd_lock_pipe(p) mutex_enter((p)->up_dev->ud_bus->ub_lock)
405 1.113 skrll #define usbd_unlock_pipe(p) mutex_exit((p)->up_dev->ud_bus->ub_lock)
406 1.131 riastrad
407 1.131 riastrad #endif /* _DEV_USB_USBDIVAR_H_ */
408