3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Including this file is mandatory for all USB related c-files in the
35 /* Allow defines in "opt_usb.h" to override configuration */
40 /* Default USB configuration */
43 * The following macro defines if the code shall use cv_xxx() instead
44 * of msleep() and wakeup().
46 #ifndef USB_HAVE_CONDVAR
47 #define USB_HAVE_CONDVAR 0
51 * The following macro defines if the code shall support
55 #define USB_HAVE_UGEN 1
59 * The following macro defines if the code shall support any forms of
62 #ifndef USB_HAVE_STRINGS
63 #define USB_HAVE_STRINGS 1
67 * The following macro defines if the code shall support BUS-DMA.
69 #ifndef USB_HAVE_BUSDMA
70 #define USB_HAVE_BUSDMA 1
74 * The following macro defines if the code shall support the Linux
75 * compatibility layer.
77 #ifndef USB_HAVE_COMPAT_LINUX
78 #define USB_HAVE_COMPAT_LINUX 1
82 * The following macro defines if the code shall support
83 * userland data transfer via copyin() and copyout()
85 #ifndef USB_HAVE_USER_IO
86 #define USB_HAVE_USER_IO 1
90 * The following macro defines if the code shall support copy in via
94 #define USB_HAVE_MBUF 1
98 * The following macro defines if the code shall compile a table
99 * describing USB vendor and product IDs.
102 #define USB_VERBOSE 1
106 * The following macro defines if USB debugging support shall be
107 * compiled for the USB core and all drivers.
113 #ifndef USB_TD_GET_PROC
114 #define USB_TD_GET_PROC(td) (td)->td_proc
117 #ifndef USB_PROC_GET_GID
118 #define USB_PROC_GET_GID(td) (td)->p_pgid
123 #include <sys/stdint.h>
124 #include <sys/stddef.h>
125 #include <sys/param.h>
126 #include <sys/queue.h>
127 #include <sys/types.h>
128 #include <sys/systm.h>
129 #include <sys/kernel.h>
131 #include <sys/linker_set.h>
132 #include <sys/module.h>
133 #include <sys/lock.h>
134 #include <sys/mutex.h>
135 #include <sys/condvar.h>
136 #include <sys/sysctl.h>
138 #include <sys/unistd.h>
139 #include <sys/callout.h>
140 #include <sys/malloc.h>
141 #include <sys/priv.h>
143 #include <dev/usb/usb_mfunc.h>
144 #include <dev/usb/usb_revision.h>
148 #define USB_STACK_VERSION 2000 /* 2.0 */
150 #define USB_HOST_ALIGN 8 /* bytes, must be power of two */
152 #define USB_ISOC_TIME_MAX 128 /* ms */
153 #define USB_FS_ISOC_UFRAME_MAX 4 /* exclusive unit */
155 #if (USB_FS_ISOC_UFRAME_MAX > 6)
156 #error "USB_FS_ISOC_UFRAME_MAX cannot be set higher than 6"
159 #define USB_MAX_FS_ISOC_FRAMES_PER_XFER (120) /* units */
160 #define USB_MAX_HS_ISOC_FRAMES_PER_XFER (8*120) /* units */
162 #define USB_MAX_IPACKET 8 /* maximum size of the initial USB
164 #define USB_HUB_MAX_DEPTH 5
166 /* USB transfer states */
168 #define USB_ST_SETUP 0
169 #define USB_ST_TRANSFERRED 1
170 #define USB_ST_ERROR 2
173 * The following macro will return the current state of an USB
174 * transfer like defined by the "USB_ST_XXX" enums.
176 #define USB_GET_STATE(xfer) ((xfer)->usb2_state)
179 * The following macro will tell if an USB transfer is currently
180 * receiving or transferring data.
182 #define USB_GET_DATA_ISREAD(xfer) (((xfer)->flags_int.usb2_mode == \
183 USB_MODE_DEVICE) ? ((xfer->endpoint & UE_DIR_IN) ? 0 : 1) : \
184 ((xfer->endpoint & UE_DIR_IN) ? 1 : 0))
187 * The following macros are used used to convert milliseconds into
188 * HZ. We use 1024 instead of 1000 milliseconds per second to save a
191 #define USB_MS_HZ 1024
193 #define USB_MS_TO_TICKS(ms) \
194 (((uint32_t)((((uint32_t)(ms)) * ((uint32_t)(hz))) + USB_MS_HZ - 1)) / USB_MS_HZ)
198 #define usb2_callout_init_mtx(c,m,f) callout_init_mtx(&(c)->co,m,f)
199 #define usb2_callout_reset(c,t,f,d) callout_reset(&(c)->co,t,f,d)
200 #define usb2_callout_stop(c) callout_stop(&(c)->co)
201 #define usb2_callout_drain(c) callout_drain(&(c)->co)
202 #define usb2_callout_pending(c) callout_pending(&(c)->co)
204 #define USB_BUS_LOCK(_b) mtx_lock(&(_b)->bus_mtx)
205 #define USB_BUS_UNLOCK(_b) mtx_unlock(&(_b)->bus_mtx)
206 #define USB_BUS_LOCK_ASSERT(_b, _t) mtx_assert(&(_b)->bus_mtx, _t)
207 #define USB_XFER_LOCK(_x) mtx_lock((_x)->xroot->xfer_mtx)
208 #define USB_XFER_UNLOCK(_x) mtx_unlock((_x)->xroot->xfer_mtx)
209 #define USB_XFER_LOCK_ASSERT(_x, _t) mtx_assert((_x)->xroot->xfer_mtx, _t)
210 /* structure prototypes */
216 struct usb2_page_cache;
218 struct usb2_xfer_root;
222 typedef void (usb2_callback_t)(struct usb2_xfer *);
224 #ifndef USB_HAVE_USB_ERROR_T
225 typedef uint8_t usb2_error_t; /* see "USB_ERR_XXX" */
228 #ifndef USB_HAVE_TIMEOUT_T
229 typedef uint32_t usb2_timeout_t; /* milliseconds */
232 #ifndef USB_HAVE_LENGTH_T
233 typedef uint32_t usb2_length_t; /* bytes */
236 #ifndef USB_HAVE_FRAMES_T
237 typedef uint32_t usb2_frames_t; /* units */
240 #ifndef USB_HAVE_INTERVAL_T
241 typedef uint32_t usb2_interval_t; /* milliseconds */
244 #ifndef USB_HAVE_SIZE_T
245 typedef uint32_t usb2_size_t; /* bytes */
248 #ifndef USB_HAVE_REFS_T
249 typedef uint32_t usb2_refs_t; /* units */
252 #ifndef USB_HAVE_TICKS_T
253 typedef uint32_t usb2_ticks_t; /* system defined */
259 * Common queue structure for USB transfers.
261 struct usb2_xfer_queue {
262 TAILQ_HEAD(, usb2_xfer) head;
263 struct usb2_xfer *curr; /* current USB transfer processed */
264 void (*command) (struct usb2_xfer_queue *pq);
270 * The following is a wrapper for the callout structure to ease
271 * porting the code to other platforms.
273 struct usb2_callout {
278 * The following structure defines a set of USB transfer flags.
280 struct usb2_xfer_flags {
281 uint8_t force_short_xfer:1; /* force a short transmit transfer
283 uint8_t short_xfer_ok:1; /* allow short receive transfers */
284 uint8_t short_frames_ok:1; /* allow short frames */
285 uint8_t pipe_bof:1; /* block pipe on failure */
286 uint8_t proxy_buffer:1; /* makes buffer size a factor of
287 * "max_frame_size" */
288 uint8_t ext_buffer:1; /* uses external DMA buffer */
289 uint8_t manual_status:1; /* non automatic status stage on
290 * control transfers */
291 uint8_t no_pipe_ok:1; /* set if "USB_ERR_NO_PIPE" error can
293 uint8_t stall_pipe:1; /* set if the endpoint belonging to
294 * this USB transfer should be stalled
295 * before starting this transfer! */
299 * The following structure defines a set of internal USB transfer
302 struct usb2_xfer_flags_int {
303 uint16_t control_rem; /* remainder in bytes */
305 uint8_t open:1; /* set if USB pipe has been opened */
306 uint8_t transferring:1; /* set if an USB transfer is in
308 uint8_t did_dma_delay:1; /* set if we waited for HW DMA */
309 uint8_t did_close:1; /* set if we closed the USB transfer */
310 uint8_t draining:1; /* set if we are draining an USB
312 uint8_t started:1; /* keeps track of started or stopped */
313 uint8_t bandwidth_reclaimed:1;
314 uint8_t control_xfr:1; /* set if control transfer */
315 uint8_t control_hdr:1; /* set if control header should be
317 uint8_t control_act:1; /* set if control transfer is active */
319 uint8_t short_frames_ok:1; /* filtered version */
320 uint8_t short_xfer_ok:1; /* filtered version */
322 uint8_t bdma_enable:1; /* filtered version (only set if
323 * hardware supports DMA) */
324 uint8_t bdma_no_post_sync:1; /* set if the USB callback wrapper
325 * should not do the BUS-DMA post sync
327 uint8_t bdma_setup:1; /* set if BUS-DMA has been setup */
329 uint8_t isochronous_xfr:1; /* set if isochronous transfer */
330 uint8_t usb2_mode:1; /* shadow copy of "udev->usb2_mode" */
331 uint8_t curr_dma_set:1; /* used by USB HC/DC driver */
332 uint8_t can_cancel_immed:1; /* set if USB transfer can be
333 * cancelled immediately */
337 * The following structure defines the symmetric part of an USB config
340 struct usb2_config_sub {
341 usb2_callback_t *callback; /* USB transfer callback */
342 uint32_t bufsize; /* total pipe buffer size in bytes */
343 uint32_t frames; /* maximum number of USB frames */
344 uint16_t interval; /* interval in milliseconds */
345 #define USB_DEFAULT_INTERVAL 0
346 uint16_t timeout; /* transfer timeout in milliseconds */
347 struct usb2_xfer_flags flags; /* transfer flags */
351 * The following structure define an USB configuration, that basically
352 * is used when setting up an USB transfer.
355 struct usb2_config_sub mh; /* parameters for USB_MODE_HOST */
356 struct usb2_config_sub md; /* parameters for USB_MODE_DEVICE */
357 uint8_t type; /* pipe type */
358 uint8_t endpoint; /* pipe number */
359 uint8_t direction; /* pipe direction */
360 uint8_t ep_index; /* pipe index match to use */
361 uint8_t if_index; /* "ifaces" index to use */
365 * The following structure defines an USB transfer.
368 struct usb2_callout timeout_handle;
369 TAILQ_ENTRY(usb2_xfer) wait_entry; /* used at various places */
371 struct usb2_page_cache *buf_fixup; /* fixup buffer(s) */
372 struct usb2_xfer_queue *wait_queue; /* pointer to queue that we
374 struct usb2_page *dma_page_ptr;
375 struct usb2_pipe *pipe; /* our USB pipe */
376 struct usb2_xfer_root *xroot; /* used by HC driver */
377 void *qh_start[2]; /* used by HC driver */
378 void *td_start[2]; /* used by HC driver */
379 void *td_transfer_first; /* used by HC driver */
380 void *td_transfer_last; /* used by HC driver */
381 void *td_transfer_cache; /* used by HC driver */
382 void *priv_sc; /* device driver data pointer 1 */
383 void *priv_fifo; /* device driver data pointer 2 */
386 struct usb2_page_cache *frbuffers;
387 usb2_callback_t *callback;
389 uint32_t max_usb2_frame_size;
390 uint32_t max_data_length;
391 uint32_t sumlen; /* sum of all lengths in bytes */
392 uint32_t actlen; /* actual length in bytes */
393 uint32_t timeout; /* milliseconds */
394 #define USB_NO_TIMEOUT 0
395 #define USB_DEFAULT_TIMEOUT 5000 /* 5000 ms = 5 seconds */
397 uint32_t max_frame_count; /* initial value of "nframes" after
399 uint32_t nframes; /* number of USB frames to transfer */
400 uint32_t aframes; /* actual number of USB frames
403 uint16_t max_packet_size;
404 uint16_t max_frame_size;
406 uint16_t isoc_time_complete; /* in ms */
407 uint16_t interval; /* milliseconds */
409 uint8_t address; /* physical USB address */
410 uint8_t endpoint; /* physical USB endpoint */
411 uint8_t max_packet_count;
419 struct usb2_xfer_flags flags;
420 struct usb2_xfer_flags_int flags_int;
424 * The following structure keeps information that is used to match
425 * against an array of "usb2_device_id" elements.
427 struct usb2_lookup_info {
431 uint8_t bDeviceClass;
432 uint8_t bDeviceSubClass;
433 uint8_t bDeviceProtocol;
434 uint8_t bInterfaceClass;
435 uint8_t bInterfaceSubClass;
436 uint8_t bInterfaceProtocol;
439 uint8_t bConfigIndex;
443 /* Structure used by probe and attach */
445 struct usb2_attach_arg {
446 struct usb2_lookup_info info;
447 device_t temp_dev; /* for internal use */
448 const void *driver_info; /* for internal use */
449 struct usb2_device *device; /* current device */
450 struct usb2_interface *iface; /* current interface */
451 uint8_t usb2_mode; /* see USB_MODE_XXX */
453 uint8_t use_generic; /* hint for generic drivers */
456 /* external variables */
458 MALLOC_DECLARE(M_USB);
459 MALLOC_DECLARE(M_USBDEV);
460 MALLOC_DECLARE(M_USBHC);
462 extern struct mtx usb2_ref_lock;
466 typedef struct malloc_type *usb2_malloc_type;
470 const char *usb2_errstr(usb2_error_t error);
471 struct usb2_config_descriptor *usb2_get_config_descriptor(
472 struct usb2_device *udev);
473 struct usb2_device_descriptor *usb2_get_device_descriptor(
474 struct usb2_device *udev);
475 struct usb2_interface *usb2_get_iface(struct usb2_device *udev,
476 uint8_t iface_index);
477 struct usb2_interface_descriptor *usb2_get_interface_descriptor(
478 struct usb2_interface *iface);
479 uint8_t usb2_clear_stall_callback(struct usb2_xfer *xfer1,
480 struct usb2_xfer *xfer2);
481 uint8_t usb2_get_interface_altindex(struct usb2_interface *iface);
482 usb2_error_t usb2_set_alt_interface_index(struct usb2_device *udev,
483 uint8_t iface_index, uint8_t alt_index);
484 uint8_t usb2_get_mode(struct usb2_device *udev);
485 uint8_t usb2_get_speed(struct usb2_device *udev);
486 uint32_t usb2_get_isoc_fps(struct usb2_device *udev);
487 usb2_error_t usb2_transfer_setup(struct usb2_device *udev,
488 const uint8_t *ifaces, struct usb2_xfer **pxfer,
489 const struct usb2_config *setup_start, uint16_t n_setup,
490 void *priv_sc, struct mtx *priv_mtx);
491 void usb2_set_frame_data(struct usb2_xfer *xfer, void *ptr,
493 void usb2_set_frame_offset(struct usb2_xfer *xfer, uint32_t offset,
495 void usb2_start_hardware(struct usb2_xfer *xfer);
496 void usb2_transfer_clear_stall(struct usb2_xfer *xfer);
497 void usb2_transfer_drain(struct usb2_xfer *xfer);
498 void usb2_transfer_set_stall(struct usb2_xfer *xfer);
499 uint8_t usb2_transfer_pending(struct usb2_xfer *xfer);
500 void usb2_transfer_start(struct usb2_xfer *xfer);
501 void usb2_transfer_stop(struct usb2_xfer *xfer);
502 void usb2_transfer_unsetup(struct usb2_xfer **pxfer, uint16_t n_setup);
503 void usb2_set_parent_iface(struct usb2_device *udev, uint8_t iface_index,
504 uint8_t parent_index);
505 uint8_t usb2_get_bus_index(struct usb2_device *udev);
506 uint8_t usb2_get_device_index(struct usb2_device *udev);
507 void usb2_set_power_mode(struct usb2_device *udev, uint8_t power_mode);
509 #endif /* _USB2_CORE_H_ */