usbdivar.h revision 1.109.2.8 1 /* $NetBSD: usbdivar.h,v 1.109.2.8 2014/12/06 08:27:23 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 *udma_block;
79 u_int udma_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 *ue_edesc;
88 int ue_refcnt;
89 int ue_toggle;
90 };
91
92 struct usbd_bus_methods {
93 usbd_status (*ubm_open)(struct usbd_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 int (*ubm_rhctrl)(struct usbd_bus *,
103 usb_device_request_t *, void *, int);
104 };
105
106 struct usbd_pipe_methods {
107 usbd_status (*upm_transfer)(usbd_xfer_handle);
108 usbd_status (*upm_start)(usbd_xfer_handle);
109 void (*upm_abort)(usbd_xfer_handle);
110 void (*upm_close)(usbd_pipe_handle);
111 void (*upm_cleartoggle)(usbd_pipe_handle);
112 void (*upm_done)(usbd_xfer_handle);
113 };
114
115 #if 0 /* notyet */
116 struct usbd_tt {
117 struct usbd_hub *hub;
118 };
119 #endif
120
121 struct usbd_port {
122 usb_port_status_t up_status;
123 uint16_t up_power; /* mA of current on port */
124 uint8_t up_portno;
125 uint8_t up_restartcnt;
126 #define USBD_RESTART_MAX 5
127 uint8_t up_reattach;
128 struct usbd_device *up_dev; /* Connected device */
129 struct usbd_device *up_parent; /* The ports hub */
130 #if 0
131 struct usbd_tt *up_tt; /* Transaction translator (if any) */
132 #endif
133 };
134
135 struct usbd_hub {
136 usbd_status (*uh_explore)(usbd_device_handle hub);
137 void *uh_hubsoftc;
138 usb_hub_descriptor_t uh_hubdesc;
139 struct usbd_port uh_ports[1];
140 };
141
142 /*****/
143
144 struct usbd_bus {
145 /* Filled by HC driver */
146 void *ub_hcpriv;
147 int ub_revision; /* USB revision */
148 #define USBREV_UNKNOWN 0
149 #define USBREV_PRE_1_0 1
150 #define USBREV_1_0 2
151 #define USBREV_1_1 3
152 #define USBREV_2_0 4
153 #define USBREV_3_0 5
154 #define USBREV_STR { "unknown", "pre 1.0", "1.0", "1.1", "2.0", "3.0" }
155
156 const struct usbd_bus_methods *ub_methods;
157 uint32_t ub_pipesize; /* size of a pipe struct */
158 bool ub_usedma; /* Does this HC support DMA */
159 int ub_dmaflags;
160 bus_dma_tag_t ub_dmatag; /* DMA tag */
161
162 /* Filled by usb driver */
163 kmutex_t *ub_lock;
164 struct usbd_device *ub_roothub;
165 uint8_t ub_rhaddr; /* roothub address */
166 uint8_t ub_rhconf; /* roothub configuration */
167 usbd_device_handle ub_devices[USB_MAX_DEVICES];
168 kcondvar_t ub_needsexplore_cv;
169 char ub_needsexplore;/* a hub a signalled a change */
170 char ub_usepolling;
171 device_t ub_usbctl;
172 struct usb_device_stats ub_stats;
173
174 void *ub_soft; /* soft interrupt cookie */
175 };
176
177 struct usbd_device {
178 struct usbd_bus *ud_bus; /* our controller */
179 struct usbd_pipe *ud_pipe0; /* pipe 0 */
180 uint8_t ud_addr; /* device addess */
181 uint8_t ud_config; /* current configuration # */
182 uint8_t ud_depth; /* distance from root hub */
183 uint8_t ud_speed; /* low/full/high speed */
184 uint8_t ud_selfpowered; /* flag for self powered */
185 uint16_t ud_power; /* mA the device uses */
186 int16_t ud_langid; /* language for strings */
187 #define USBD_NOLANG (-1)
188 usb_event_cookie_t ud_cookie; /* unique connection id */
189 struct usbd_port *ud_powersrc; /* upstream hub port, or 0 */
190 struct usbd_device *ud_myhub; /* upstream hub */
191 struct usbd_port *ud_myhsport; /* closest high speed port */
192 struct usbd_endpoint ud_ep0; /* for pipe 0 */
193 usb_endpoint_descriptor_t ud_ep0desc; /* for pipe 0 */
194 struct usbd_interface *ud_ifaces; /* array of all interfaces */
195 usb_device_descriptor_t ud_ddesc; /* device descriptor */
196 usb_config_descriptor_t *ud_cdesc; /* full config descr */
197 const struct usbd_quirks *ud_quirks;/* device quirks, always set */
198 struct usbd_hub *ud_hub; /* only if this is a hub */
199 int ud_subdevlen; /* array length of following */
200 device_t *ud_subdevs; /* sub-devices */
201 int ud_nifaces_claimed; /* number of ifaces in use */
202 void *ud_hcpriv;
203 };
204
205 struct usbd_interface {
206 struct usbd_device *ui_dev;
207 usb_interface_descriptor_t *ui_idesc;
208 int ui_index;
209 int ui_altindex;
210 struct usbd_endpoint *ui_endpoints;
211 void *ui_priv;
212 LIST_HEAD(, usbd_pipe) ui_pipes;
213 };
214
215 struct usbd_pipe {
216 struct usbd_interface *up_iface;
217 struct usbd_device *up_dev;
218 struct usbd_endpoint *up_endpoint;
219 int up_refcnt;
220 char up_running;
221 char up_aborting;
222 SIMPLEQ_HEAD(, usbd_xfer) up_queue;
223 LIST_ENTRY(usbd_pipe) up_next;
224 struct usb_task up_async_task;
225
226 usbd_xfer_handle up_intrxfer; /* used for repeating requests */
227 char up_repeat;
228 int up_interval;
229 uint8_t up_flags;
230
231 /* Filled by HC driver. */
232 const struct usbd_pipe_methods *up_methods;
233 };
234
235 struct usbd_xfer {
236 struct usbd_pipe *ux_pipe;
237 void *ux_priv;
238 void *ux_buffer;
239 kcondvar_t ux_cv;
240 uint32_t ux_length;
241 uint32_t ux_actlen;
242 uint16_t ux_flags;
243 uint32_t ux_timeout;
244 usbd_status ux_status;
245 usbd_callback ux_callback;
246 volatile uint8_t ux_done;
247 uint8_t ux_state; /* used for DIAGNOSTIC */
248 #define XFER_FREE 0x46
249 #define XFER_BUSY 0x55
250 #define XFER_ONQU 0x9e
251
252 /* For control pipe */
253 usb_device_request_t ux_request;
254
255 /* For isoc */
256 uint16_t *ux_frlengths;
257 int ux_nframes;
258
259 /* For memory allocation */
260 struct usbd_device *ux_dev;
261 usb_dma_t ux_dmabuf;
262 void *ux_buf;
263 uint32_t ux_bufsize;
264
265 uint8_t ux_rqflags;
266 #define URQ_REQUEST 0x01
267 #define URQ_AUTO_BUFFER 0x10
268
269 SIMPLEQ_ENTRY(usbd_xfer) ux_next;
270
271 void *ux_hcpriv; /* private use by the HC driver */
272 uint8_t ux_hcflags; /* private use by the HC driver */
273 #define UXFER_ABORTING 0x01 /* xfer is aborting. */
274 #define UXFER_ABORTWAIT 0x02 /* abort completion is being awaited. */
275 kcondvar_t ux_hccv; /* private use by the HC driver */
276
277 struct callout ux_callout;
278 };
279
280 void usbd_init(void);
281 void usbd_finish(void);
282
283 #if defined(USB_DEBUG) || defined(EHCI_DEBUG) || defined(OHCI_DEBUG)
284 void usbd_dump_iface(struct usbd_interface *);
285 void usbd_dump_device(struct usbd_device *);
286 void usbd_dump_endpoint(struct usbd_endpoint *);
287 void usbd_dump_queue(usbd_pipe_handle);
288 void usbd_dump_pipe(usbd_pipe_handle);
289 #endif
290
291 /* Routines from usb_subr.c */
292 int usbctlprint(void *, const char *);
293 void usb_delay_ms_locked(usbd_bus_handle, u_int, kmutex_t *);
294 void usb_delay_ms(usbd_bus_handle, u_int);
295 void usbd_delay_ms_locked(usbd_device_handle, u_int, kmutex_t *);
296 void usbd_delay_ms(usbd_device_handle, u_int);
297 usbd_status usbd_reset_port(usbd_device_handle, int, usb_port_status_t *);
298 usbd_status usbd_setup_pipe(usbd_device_handle,
299 usbd_interface_handle,
300 struct usbd_endpoint *, int,
301 usbd_pipe_handle *);
302 usbd_status usbd_setup_pipe_flags(usbd_device_handle,
303 usbd_interface_handle,
304 struct usbd_endpoint *, int,
305 usbd_pipe_handle *,
306 uint8_t);
307 usbd_status usbd_new_device(device_t, usbd_bus_handle, int, int, int,
308 struct usbd_port *);
309 usbd_status usbd_reattach_device(device_t, usbd_device_handle,
310 int, const int *);
311
312 void usbd_remove_device(usbd_device_handle, struct usbd_port *);
313 int usbd_printBCD(char *, size_t, int);
314 usbd_status usbd_fill_iface_data(usbd_device_handle, int, int);
315 void usb_free_device(usbd_device_handle);
316
317 usbd_status usb_insert_transfer(usbd_xfer_handle);
318 void usb_transfer_complete(usbd_xfer_handle);
319 int usb_disconnect_port(struct usbd_port *, device_t, int);
320
321 void usbd_kill_pipe(usbd_pipe_handle);
322 usbd_status usbd_attach_roothub(device_t, usbd_device_handle);
323 usbd_status usbd_probe_and_attach(device_t, usbd_device_handle, int, int);
324 usbd_status usbd_get_initial_ddesc(usbd_device_handle,
325 usb_device_descriptor_t *);
326
327 /* Routines from usb.c */
328 void usb_needs_explore(usbd_device_handle);
329 void usb_needs_reattach(usbd_device_handle);
330 void usb_schedsoftintr(struct usbd_bus *);
331
332 /*
333 * These macros reflect the current locking scheme. They might change.
334 */
335
336 #define usbd_lock_pipe(p) mutex_enter((p)->up_dev->ud_bus->ub_lock)
337 #define usbd_unlock_pipe(p) mutex_exit((p)->up_dev->ud_bus->ub_lock)
338