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