usb_device.h revision 193045
1184610Salfred/* $FreeBSD: head/sys/dev/usb/usb_device.h 193045 2009-05-29 18:46:57Z thompsa $ */ 2184610Salfred/*- 3184610Salfred * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4184610Salfred * 5184610Salfred * Redistribution and use in source and binary forms, with or without 6184610Salfred * modification, are permitted provided that the following conditions 7184610Salfred * are met: 8184610Salfred * 1. Redistributions of source code must retain the above copyright 9184610Salfred * notice, this list of conditions and the following disclaimer. 10184610Salfred * 2. Redistributions in binary form must reproduce the above copyright 11184610Salfred * notice, this list of conditions and the following disclaimer in the 12184610Salfred * documentation and/or other materials provided with the distribution. 13184610Salfred * 14184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17184610Salfred * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24184610Salfred * SUCH DAMAGE. 25184610Salfred */ 26184610Salfred 27184610Salfred#ifndef _USB2_DEVICE_H_ 28184610Salfred#define _USB2_DEVICE_H_ 29184610Salfred 30192984Sthompsastruct usb_symlink; /* UGEN */ 31190180Sthompsastruct usb_device; /* linux compat */ 32184610Salfred 33184610Salfred#define USB_DEFAULT_XFER_MAX 2 34184610Salfred 35190730Sthompsa/* "usb2_parse_config()" commands */ 36190730Sthompsa 37190730Sthompsa#define USB_CFG_ALLOC 0 38190730Sthompsa#define USB_CFG_FREE 1 39190730Sthompsa#define USB_CFG_INIT 2 40190730Sthompsa 41190730Sthompsa/* "usb2_unconfigure()" flags */ 42190730Sthompsa 43190730Sthompsa#define USB_UNCFG_FLAG_NONE 0x00 44190730Sthompsa#define USB_UNCFG_FLAG_FREE_SUBDEV 0x01 /* subdevices are freed */ 45190730Sthompsa#define USB_UNCFG_FLAG_FREE_EP0 0x02 /* endpoint zero is freed */ 46190730Sthompsa 47192984Sthompsastruct usb_clear_stall_msg { 48192984Sthompsa struct usb_proc_msg hdr; 49192984Sthompsa struct usb_device *udev; 50184610Salfred}; 51184610Salfred 52192984Sthompsa/* The following four structures makes up a tree, where we have the 53192984Sthompsa * leaf structure, "usb_host_endpoint", first, and the root structure, 54192984Sthompsa * "usb_device", last. The four structures below mirror the structure 55192984Sthompsa * of the USB descriptors belonging to an USB configuration. Please 56192984Sthompsa * refer to the USB specification for a definition of "endpoints" and 57192984Sthompsa * "interfaces". 58192984Sthompsa */ 59192984Sthompsastruct usb_host_endpoint { 60192984Sthompsa struct usb_endpoint_descriptor desc; 61192984Sthompsa TAILQ_HEAD(, urb) bsd_urb_list; 62192984Sthompsa struct usb_xfer *bsd_xfer[2]; 63192984Sthompsa uint8_t *extra; /* Extra descriptors */ 64193045Sthompsa usb_frlength_t fbsd_buf_size; 65192984Sthompsa uint16_t extralen; 66192984Sthompsa uint8_t bsd_iface_index; 67192984Sthompsa} __aligned(USB_HOST_ALIGN); 68192984Sthompsa 69192984Sthompsastruct usb_host_interface { 70192984Sthompsa struct usb_interface_descriptor desc; 71192984Sthompsa /* the following array has size "desc.bNumEndpoint" */ 72192984Sthompsa struct usb_host_endpoint *endpoint; 73192984Sthompsa const char *string; /* iInterface string, if present */ 74192984Sthompsa uint8_t *extra; /* Extra descriptors */ 75192984Sthompsa uint16_t extralen; 76192984Sthompsa uint8_t bsd_iface_index; 77192984Sthompsa} __aligned(USB_HOST_ALIGN); 78192984Sthompsa 79184610Salfred/* 80184610Salfred * The following structure defines an USB pipe which is equal to an 81184610Salfred * USB endpoint. 82184610Salfred */ 83192984Sthompsastruct usb_pipe { 84192984Sthompsa struct usb_xfer_queue pipe_q; /* queue of USB transfers */ 85184610Salfred 86192984Sthompsa struct usb_endpoint_descriptor *edesc; 87192984Sthompsa struct usb_pipe_methods *methods; /* set by HC driver */ 88184610Salfred 89184610Salfred uint16_t isoc_next; 90184610Salfred uint16_t refcount; 91184610Salfred 92184610Salfred uint8_t toggle_next:1; /* next data toggle value */ 93184610Salfred uint8_t is_stalled:1; /* set if pipe is stalled */ 94184610Salfred uint8_t is_synced:1; /* set if we a synchronised */ 95184610Salfred uint8_t unused:5; 96184610Salfred uint8_t iface_index; /* not used by "default pipe" */ 97184610Salfred}; 98184610Salfred 99184610Salfred/* 100184610Salfred * The following structure defines an USB interface. 101184610Salfred */ 102192984Sthompsastruct usb_interface { 103192984Sthompsa struct usb_interface_descriptor *idesc; 104184610Salfred device_t subdev; 105184610Salfred uint8_t alt_index; 106184610Salfred uint8_t parent_iface_index; 107192984Sthompsa 108192984Sthompsa /* Linux compat */ 109192984Sthompsa struct usb_host_interface *altsetting; 110192984Sthompsa struct usb_host_interface *cur_altsetting; 111192984Sthompsa struct usb_device *linux_udev; 112192984Sthompsa void *bsd_priv_sc; /* device specific information */ 113192984Sthompsa uint8_t num_altsetting; /* number of alternate settings */ 114192984Sthompsa uint8_t bsd_iface_index; 115184610Salfred}; 116184610Salfred 117184610Salfred/* 118184610Salfred * The following structure defines the USB device flags. 119184610Salfred */ 120192984Sthompsastruct usb_device_flags { 121192499Sthompsa enum usb_hc_mode usb_mode; /* host or device mode */ 122184610Salfred uint8_t self_powered:1; /* set if USB device is self powered */ 123184610Salfred uint8_t no_strings:1; /* set if USB device does not support 124184610Salfred * strings */ 125184610Salfred uint8_t remote_wakeup:1; /* set if remote wakeup is enabled */ 126184610Salfred uint8_t uq_bus_powered:1; /* set if BUS powered quirk is present */ 127191824Sthompsa 128191824Sthompsa /* 129191824Sthompsa * NOTE: Although the flags below will reach the same value 130191824Sthompsa * over time, but the instant values may differ, and 131191824Sthompsa * consequently the flags cannot be merged into one! 132191824Sthompsa */ 133191824Sthompsa uint8_t peer_suspended:1; /* set if peer is suspended */ 134191824Sthompsa uint8_t self_suspended:1; /* set if self is suspended */ 135184610Salfred}; 136184610Salfred 137184610Salfred/* 138186730Salfred * The following structure is used for power-save purposes. The data 139186730Salfred * in this structure is protected by the USB BUS lock. 140186730Salfred */ 141192984Sthompsastruct usb_power_save { 142193045Sthompsa usb_ticks_t last_xfer_time; /* copy of "ticks" */ 143193045Sthompsa size_t type_refs[4]; /* transfer reference count */ 144193045Sthompsa size_t read_refs; /* data read references */ 145193045Sthompsa size_t write_refs; /* data write references */ 146186730Salfred}; 147186730Salfred 148186730Salfred/* 149184610Salfred * The following structure defines an USB device. There exists one of 150184610Salfred * these structures for every USB device. 151184610Salfred */ 152192984Sthompsastruct usb_device { 153192984Sthompsa struct usb_clear_stall_msg cs_msg[2]; /* generic clear stall 154184610Salfred * messages */ 155184610Salfred struct sx default_sx[2]; 156184610Salfred struct mtx default_mtx[1]; 157184610Salfred struct cv default_cv[2]; 158192984Sthompsa struct usb_interface *ifaces; 159192984Sthompsa struct usb_pipe default_pipe; /* Control Endpoint 0 */ 160192984Sthompsa struct usb_pipe *pipes; 161192984Sthompsa struct usb_power_save pwr_save;/* power save data */ 162192984Sthompsa struct usb_bus *bus; /* our USB BUS */ 163184610Salfred device_t parent_dev; /* parent device */ 164192984Sthompsa struct usb_device *parent_hub; 165192984Sthompsa struct usb_device *parent_hs_hub; /* high-speed parent HUB */ 166192984Sthompsa struct usb_config_descriptor *cdesc; /* full config descr */ 167192984Sthompsa struct usb_hub *hub; /* only if this is a hub */ 168192984Sthompsa struct usb_xfer *default_xfer[USB_DEFAULT_XFER_MAX]; 169192984Sthompsa struct usb_temp_data *usb2_template_ptr; 170192984Sthompsa struct usb_pipe *pipe_curr; /* current clear stall pipe */ 171190191Sthompsa#if USB_HAVE_UGEN 172192984Sthompsa struct usb_fifo *fifo[USB_FIFO_MAX]; 173192984Sthompsa struct usb_symlink *ugen_symlink; /* our generic symlink */ 174190730Sthompsa struct cdev *default_dev; /* Control Endpoint 0 device node */ 175192984Sthompsa LIST_HEAD(,usb_fs_privdata) pd_list; 176190191Sthompsa char ugen_name[20]; /* name of ugenX.X device */ 177190191Sthompsa#endif 178193045Sthompsa usb_ticks_t plugtime; /* copy of "ticks" */ 179184610Salfred 180192500Sthompsa enum usb_dev_state state; 181192500Sthompsa enum usb_dev_speed speed; 182184610Salfred uint16_t refcount; 183184610Salfred#define USB_DEV_REF_MAX 0xffff 184184610Salfred 185184610Salfred uint16_t power; /* mA the device uses */ 186184610Salfred uint16_t langid; /* language for strings */ 187184610Salfred 188184610Salfred uint8_t address; /* device addess */ 189184610Salfred uint8_t device_index; /* device index in "bus->devices" */ 190184610Salfred uint8_t curr_config_index; /* current configuration index */ 191184610Salfred uint8_t curr_config_no; /* current configuration number */ 192184610Salfred uint8_t depth; /* distance from root HUB */ 193184610Salfred uint8_t port_index; /* parent HUB port index */ 194184610Salfred uint8_t port_no; /* parent HUB port number */ 195184610Salfred uint8_t hs_hub_addr; /* high-speed HUB address */ 196184610Salfred uint8_t hs_port_no; /* high-speed HUB port number */ 197184610Salfred uint8_t driver_added_refcount; /* our driver added generation count */ 198184610Salfred uint8_t power_mode; /* see USB_POWER_XXX */ 199190730Sthompsa uint8_t ifaces_max; /* number of interfaces present */ 200190730Sthompsa uint8_t pipes_max; /* number of pipes present */ 201184610Salfred 202184610Salfred /* the "flags" field is write-protected by "bus->mtx" */ 203184610Salfred 204192984Sthompsa struct usb_device_flags flags; 205184610Salfred 206192984Sthompsa struct usb_endpoint_descriptor default_ep_desc; /* for pipe 0 */ 207192984Sthompsa struct usb_device_descriptor ddesc; /* device descriptor */ 208184610Salfred 209192938Sthompsa char *serial; /* serial number */ 210192938Sthompsa char *manufacturer; /* manufacturer string */ 211192938Sthompsa char *product; /* product string */ 212192984Sthompsa 213192984Sthompsa#if USB_HAVE_COMPAT_LINUX 214192984Sthompsa /* Linux compat */ 215192984Sthompsa struct usb_device_descriptor descriptor; 216192984Sthompsa struct usb_host_endpoint ep0; 217192984Sthompsa struct usb_interface *linux_iface_start; 218192984Sthompsa struct usb_interface *linux_iface_end; 219192984Sthompsa struct usb_host_endpoint *linux_endpoint_start; 220192984Sthompsa struct usb_host_endpoint *linux_endpoint_end; 221192984Sthompsa uint16_t devnum; 222192984Sthompsa#endif 223184610Salfred}; 224184610Salfred 225188987Sthompsa/* globals */ 226188987Sthompsa 227188987Sthompsaextern int usb2_template; 228188987Sthompsa 229184610Salfred/* function prototypes */ 230184610Salfred 231192984Sthompsastruct usb_device *usb2_alloc_device(device_t parent_dev, struct usb_bus *bus, 232192984Sthompsa struct usb_device *parent_hub, uint8_t depth, 233192500Sthompsa uint8_t port_index, uint8_t port_no, 234192500Sthompsa enum usb_dev_speed speed, enum usb_hc_mode mode); 235192984Sthompsastruct usb_pipe *usb2_get_pipe(struct usb_device *udev, uint8_t iface_index, 236192984Sthompsa const struct usb_config *setup); 237192984Sthompsastruct usb_pipe *usb2_get_pipe_by_addr(struct usb_device *udev, uint8_t ea_val); 238193045Sthompsausb_error_t usb2_interface_count(struct usb_device *udev, uint8_t *count); 239193045Sthompsausb_error_t usb2_probe_and_attach(struct usb_device *udev, 240185948Sthompsa uint8_t iface_index); 241193045Sthompsausb_error_t usb2_reset_iface_endpoints(struct usb_device *udev, 242185948Sthompsa uint8_t iface_index); 243193045Sthompsausb_error_t usb2_set_config_index(struct usb_device *udev, uint8_t index); 244193045Sthompsausb_error_t usb2_set_endpoint_stall(struct usb_device *udev, 245192984Sthompsa struct usb_pipe *pipe, uint8_t do_stall); 246193045Sthompsausb_error_t usb2_suspend_resume(struct usb_device *udev, 247185948Sthompsa uint8_t do_suspend); 248192984Sthompsavoid usb2_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len); 249192984Sthompsavoid usb2_free_device(struct usb_device *, uint8_t); 250192984Sthompsavoid *usb2_find_descriptor(struct usb_device *udev, void *id, 251185948Sthompsa uint8_t iface_index, uint8_t type, uint8_t type_mask, 252185948Sthompsa uint8_t subtype, uint8_t subtype_mask); 253184610Salfredvoid usb_linux_free_device(struct usb_device *dev); 254192984Sthompsauint8_t usb2_peer_can_wakeup(struct usb_device *udev); 255192984Sthompsastruct usb_pipe *usb2_pipe_foreach(struct usb_device *udev, struct usb_pipe *pipe); 256192984Sthompsavoid usb2_set_device_state(struct usb_device *udev, 257192500Sthompsa enum usb_dev_state state); 258184610Salfred 259184610Salfred#endif /* _USB2_DEVICE_H_ */ 260