usbdivar.h revision 1.109.2.4 1 /* $NetBSD: usbdivar.h,v 1.109.2.4 2014/12/02 09:00:34 skrll 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 /*
34 * Discussion about locking in the USB code:
35 *
36 * The host controller presents one lock at IPL_SOFTUSB (aka IPL_SOFTNET).
37 *
38 * List of hardware interface methods, and whether the lock is held
39 * when each is called by this module:
40 *
41 * BUS METHOD LOCK NOTES
42 * ----------------------- ------- -------------------------
43 * open_pipe - might want to take lock?
44 * soft_intr x
45 * do_poll - might want to take lock?
46 * allocx -
47 * freex -
48 * get_lock - Called at attach time
49 * new_device
50 *
51 * PIPE METHOD LOCK NOTES
52 * ----------------------- ------- -------------------------
53 * transfer -
54 * start - might want to take lock?
55 * abort x
56 * close x
57 * cleartoggle -
58 * done x
59 *
60 * The above semantics are likely to change. Little performance
61 * evaluation has been done on this code and the locking strategy.
62 *
63 * USB functions known to expect the lock taken include (this list is
64 * probably not exhaustive):
65 * usb_transfer_complete()
66 * usb_insert_transfer()
67 * usb_start_next()
68 *
69 */
70
71 #include <sys/callout.h>
72 #include <sys/mutex.h>
73 #include <sys/bus.h>
74
75 /* From usb_mem.h */
76 struct usb_dma_block;
77 typedef struct {
78 struct usb_dma_block *block;
79 u_int offs;
80 } usb_dma_t;
81
82 struct usbd_xfer;
83 struct usbd_pipe;
84 struct usbd_port;
85
86 struct usbd_endpoint {
87 usb_endpoint_descriptor_t *edesc;
88 int refcnt;
89 int datatoggle;
90 };
91
92 struct usbd_bus_methods {
93 usbd_status (*ubm_open)(struct usbd_pipe *pipe);
94 void (*ubm_softint)(void *);
95 void (*ubm_dopoll)(struct usbd_bus *);
96 struct usbd_xfer * (*ubm_allocx)(struct usbd_bus *);
97 void (*ubm_freex)(struct usbd_bus *, struct usbd_xfer *);
98 void (*ubm_getlock)(struct usbd_bus *, kmutex_t **);
99 usbd_status (*ubm_newdev)(device_t, usbd_bus_handle, int,
100 int, int, struct usbd_port *);
101 };
102
103 struct usbd_pipe_methods {
104 usbd_status (*upm_transfer)(usbd_xfer_handle);
105 usbd_status (*upm_start)(usbd_xfer_handle);
106 void (*upm_abort)(usbd_xfer_handle);
107 void (*upm_close)(usbd_pipe_handle);
108 void (*upm_cleartoggle)(usbd_pipe_handle);
109 void (*upm_done)(usbd_xfer_handle);
110 };
111
112 #if 0 /* notyet */
113 struct usbd_tt {
114 struct usbd_hub *hub;
115 };
116 #endif
117
118 struct usbd_port {
119 usb_port_status_t status;
120 uint16_t power; /* mA of current on port */
121 uint8_t portno;
122 uint8_t restartcnt;
123 #define USBD_RESTART_MAX 5
124 uint8_t reattach;
125 struct usbd_device *device; /* Connected device */
126 struct usbd_device *parent; /* The ports hub */
127 #if 0
128 struct usbd_tt *tt; /* Transaction translator (if any) */
129 #endif
130 };
131
132 struct usbd_hub {
133 usbd_status (*explore)(usbd_device_handle hub);
134 void *hubsoftc;
135 usb_hub_descriptor_t hubdesc;
136 struct usbd_port ports[1];
137 };
138
139 /*****/
140
141 struct usbd_bus {
142 /* Filled by HC driver */
143 void *hci_private;
144 const struct usbd_bus_methods *methods;
145 uint32_t pipe_size; /* size of a pipe struct */
146 /* Filled by usb driver */
147 kmutex_t *lock;
148 struct usbd_device *root_hub;
149 usbd_device_handle devices[USB_MAX_DEVICES];
150 kcondvar_t needs_explore_cv;
151 char needs_explore;/* a hub a signalled a change */
152 char use_polling;
153 device_t usbctl;
154 struct usb_device_stats stats;
155 int usbrev; /* USB revision */
156 #define USBREV_UNKNOWN 0
157 #define USBREV_PRE_1_0 1
158 #define USBREV_1_0 2
159 #define USBREV_1_1 3
160 #define USBREV_2_0 4
161 #define USBREV_3_0 5
162 #define USBREV_STR { "unknown", "pre 1.0", "1.0", "1.1", "2.0", "3.0" }
163
164 void *soft; /* soft interrupt cookie */
165
166 bool usedma; /* Does this HC support DMA */
167 int dmaflags;
168 bus_dma_tag_t dmatag; /* DMA tag */
169 };
170
171 struct usbd_device {
172 struct usbd_bus *bus; /* our controller */
173 struct usbd_pipe *default_pipe; /* pipe 0 */
174 uint8_t address; /* device addess */
175 uint8_t config; /* current configuration # */
176 uint8_t depth; /* distance from root hub */
177 uint8_t speed; /* low/full/high speed */
178 uint8_t self_powered; /* flag for self powered */
179 uint16_t power; /* mA the device uses */
180 int16_t langid; /* language for strings */
181 #define USBD_NOLANG (-1)
182 usb_event_cookie_t cookie; /* unique connection id */
183 struct usbd_port *powersrc; /* upstream hub port, or 0 */
184 struct usbd_device *myhub; /* upstream hub */
185 struct usbd_port *myhsport; /* closest high speed port */
186 struct usbd_endpoint def_ep; /* for pipe 0 */
187 usb_endpoint_descriptor_t def_ep_desc; /* for pipe 0 */
188 struct usbd_interface *ifaces; /* array of all interfaces */
189 usb_device_descriptor_t ddesc; /* device descriptor */
190 usb_config_descriptor_t *cdesc; /* full config descr */
191 const struct usbd_quirks *quirks; /* device quirks, always set */
192 struct usbd_hub *hub; /* only if this is a hub */
193 int subdevlen; /* array length of following */
194 device_t *subdevs; /* sub-devices */
195 int nifaces_claimed; /* number of ifaces in use */
196 void *hci_private;
197 };
198
199 struct usbd_interface {
200 struct usbd_device *device;
201 usb_interface_descriptor_t *idesc;
202 int index;
203 int altindex;
204 struct usbd_endpoint *endpoints;
205 void *priv;
206 LIST_HEAD(, usbd_pipe) pipes;
207 };
208
209 struct usbd_pipe {
210 struct usbd_interface *iface;
211 struct usbd_device *device;
212 struct usbd_endpoint *endpoint;
213 int refcnt;
214 char running;
215 char aborting;
216 SIMPLEQ_HEAD(, usbd_xfer) queue;
217 LIST_ENTRY(usbd_pipe) next;
218 struct usb_task async_task;
219
220 usbd_xfer_handle intrxfer; /* used for repeating requests */
221 char repeat;
222 int interval;
223 uint8_t flags;
224
225 /* Filled by HC driver. */
226 const struct usbd_pipe_methods *methods;
227 };
228
229 struct usbd_xfer {
230 struct usbd_pipe *pipe;
231 void *priv;
232 void *buffer;
233 kcondvar_t cv;
234 uint32_t length;
235 uint32_t actlen;
236 uint16_t flags;
237 uint32_t timeout;
238 usbd_status status;
239 usbd_callback callback;
240 volatile uint8_t done;
241 uint8_t busy_free; /* used for DIAGNOSTIC */
242 #define XFER_FREE 0x46
243 #define XFER_BUSY 0x55
244 #define XFER_ONQU 0x9e
245
246 /* For control pipe */
247 usb_device_request_t request;
248
249 /* For isoc */
250 uint16_t *frlengths;
251 int nframes;
252
253 /* For memory allocation */
254 struct usbd_device *device;
255 usb_dma_t dmabuf;
256 void *buf;
257 uint32_t bufsize;
258
259 uint8_t rqflags;
260 #define URQ_REQUEST 0x01
261 #define URQ_AUTO_BUFFER 0x10
262
263 SIMPLEQ_ENTRY(usbd_xfer) next;
264
265 void *hcpriv; /* private use by the HC driver */
266 uint8_t hcflags; /* private use by the HC driver */
267 #define UXFER_ABORTING 0x01 /* xfer is aborting. */
268 #define UXFER_ABORTWAIT 0x02 /* abort completion is being awaited. */
269 kcondvar_t hccv; /* private use by the HC driver */
270
271 struct callout timeout_handle;
272 };
273
274 void usbd_init(void);
275 void usbd_finish(void);
276
277 #if defined(USB_DEBUG) || defined(EHCI_DEBUG) || defined(OHCI_DEBUG)
278 void usbd_dump_iface(struct usbd_interface *iface);
279 void usbd_dump_device(struct usbd_device *dev);
280 void usbd_dump_endpoint(struct usbd_endpoint *endp);
281 void usbd_dump_queue(usbd_pipe_handle pipe);
282 void usbd_dump_pipe(usbd_pipe_handle pipe);
283 #endif
284
285 /* Routines from usb_subr.c */
286 int usbctlprint(void *, const char *);
287 void usb_delay_ms_locked(usbd_bus_handle, u_int, kmutex_t *);
288 void usb_delay_ms(usbd_bus_handle, u_int);
289 void usbd_delay_ms_locked(usbd_device_handle, u_int, kmutex_t *);
290 void usbd_delay_ms(usbd_device_handle, u_int);
291 usbd_status usbd_reset_port(usbd_device_handle, int, usb_port_status_t *);
292 usbd_status usbd_setup_pipe(usbd_device_handle dev,
293 usbd_interface_handle iface,
294 struct usbd_endpoint *, int,
295 usbd_pipe_handle *pipe);
296 usbd_status usbd_setup_pipe_flags(usbd_device_handle dev,
297 usbd_interface_handle iface,
298 struct usbd_endpoint *, int,
299 usbd_pipe_handle *pipe,
300 uint8_t flags);
301 usbd_status usbd_new_device(device_t, usbd_bus_handle, int, int, int,
302 struct usbd_port *);
303 usbd_status usbd_reattach_device(device_t, usbd_device_handle,
304 int, const int *);
305
306 void usbd_remove_device(usbd_device_handle, struct usbd_port *);
307 int usbd_printBCD(char *, size_t, int);
308 usbd_status usbd_fill_iface_data(usbd_device_handle, int, int);
309 void usb_free_device(usbd_device_handle);
310
311 usbd_status usb_insert_transfer(usbd_xfer_handle);
312 void usb_transfer_complete(usbd_xfer_handle);
313 int usb_disconnect_port(struct usbd_port *, device_t, int);
314
315 void usbd_kill_pipe(usbd_pipe_handle);
316 usbd_status usbd_attach_roothub(device_t, usbd_device_handle);
317 usbd_status usbd_probe_and_attach(device_t, usbd_device_handle, int, int);
318 usbd_status usbd_get_initial_ddesc(usbd_device_handle,
319 usb_device_descriptor_t *);
320
321 /* Routines from usb.c */
322 void usb_needs_explore(usbd_device_handle);
323 void usb_needs_reattach(usbd_device_handle);
324 void usb_schedsoftintr(struct usbd_bus *);
325
326 /*
327 * These macros reflect the current locking scheme. They might change.
328 */
329
330 #define usbd_lock_pipe(p) mutex_enter((p)->device->bus->lock)
331 #define usbd_unlock_pipe(p) mutex_exit((p)->device->bus->lock)
332