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