usb_device.h revision 197553
152419Sjulian/* $FreeBSD: head/sys/dev/usb/usb_device.h 197553 2009-09-28 07:01:54Z thompsa $ */ 252419Sjulian/*- 352419Sjulian * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 452419Sjulian * 559882Sarchie * Redistribution and use in source and binary forms, with or without 652419Sjulian * modification, are permitted provided that the following conditions 752419Sjulian * are met: 852419Sjulian * 1. Redistributions of source code must retain the above copyright 952419Sjulian * notice, this list of conditions and the following disclaimer. 1052419Sjulian * 2. Redistributions in binary form must reproduce the above copyright 1152419Sjulian * notice, this list of conditions and the following disclaimer in the 1252419Sjulian * documentation and/or other materials provided with the distribution. 1352419Sjulian * 1452419Sjulian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1552419Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1652419Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1752419Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1852419Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1952419Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2052419Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2152419Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2252419Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2352419Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2452419Sjulian * SUCH DAMAGE. 2552419Sjulian */ 2652419Sjulian 2752419Sjulian#ifndef _USB_DEVICE_H_ 2852419Sjulian#define _USB_DEVICE_H_ 2952419Sjulian 3052419Sjulianstruct usb_symlink; /* UGEN */ 3152419Sjulianstruct usb_device; /* linux compat */ 3252419Sjulian 3352419Sjulian#define USB_DEFAULT_XFER_MAX 2 3452419Sjulian 3552419Sjulian/* "usb_parse_config()" commands */ 3652419Sjulian 3766775Sarchie#define USB_CFG_ALLOC 0 3852419Sjulian#define USB_CFG_FREE 1 3952419Sjulian#define USB_CFG_INIT 2 4052752Sjulian 4152419Sjulian/* "usb_unconfigure()" flags */ 4252419Sjulian 4352419Sjulian#define USB_UNCFG_FLAG_NONE 0x00 4452639Sarchie#define USB_UNCFG_FLAG_FREE_EP0 0x02 /* endpoint zero is freed */ 4552419Sjulian 4652419Sjulianstruct usb_clear_stall_msg { 4752419Sjulian struct usb_proc_msg hdr; 4852419Sjulian struct usb_device *udev; 4952419Sjulian}; 5059882Sarchie 5152419Sjulian/* The following four structures makes up a tree, where we have the 5252419Sjulian * leaf structure, "usb_host_endpoint", first, and the root structure, 5352419Sjulian * "usb_device", last. The four structures below mirror the structure 5452843Sphk * of the USB descriptors belonging to an USB configuration. Please 5552419Sjulian * refer to the USB specification for a definition of "endpoints" and 5659882Sarchie * "interfaces". 5759882Sarchie */ 5852419Sjulianstruct usb_host_endpoint { 5952419Sjulian struct usb_endpoint_descriptor desc; 6053913Sarchie TAILQ_HEAD(, urb) bsd_urb_list; 6152419Sjulian struct usb_xfer *bsd_xfer[2]; 6252639Sarchie uint8_t *extra; /* Extra descriptors */ 6352419Sjulian usb_frlength_t fbsd_buf_size; 6470870Sjulian uint16_t extralen; 6570870Sjulian uint8_t bsd_iface_index; 6670870Sjulian} __aligned(USB_HOST_ALIGN); 6770870Sjulian 6870870Sjulianstruct usb_host_interface { 6970870Sjulian struct usb_interface_descriptor desc; 7052419Sjulian /* the following array has size "desc.bNumEndpoint" */ 7152816Sarchie struct usb_host_endpoint *endpoint; 7252419Sjulian const char *string; /* iInterface string, if present */ 7352639Sarchie uint8_t *extra; /* Extra descriptors */ 7452639Sarchie uint16_t extralen; 7552639Sarchie uint8_t bsd_iface_index; 7652639Sarchie} __aligned(USB_HOST_ALIGN); 7752639Sarchie 7859882Sarchie/* 7952816Sarchie * The following structure defines the USB device flags. 8053075Sarchie */ 8152639Sarchiestruct usb_device_flags { 8252639Sarchie enum usb_hc_mode usb_mode; /* host or device mode */ 8352639Sarchie uint8_t self_powered:1; /* set if USB device is self powered */ 8452419Sjulian uint8_t no_strings:1; /* set if USB device does not support 8552639Sarchie * strings */ 8652639Sarchie uint8_t remote_wakeup:1; /* set if remote wakeup is enabled */ 8752639Sarchie uint8_t uq_bus_powered:1; /* set if BUS powered quirk is present */ 8852639Sarchie 8952639Sarchie /* 9052639Sarchie * NOTE: Although the flags below will reach the same value 9152639Sarchie * over time, but the instant values may differ, and 9252639Sarchie * consequently the flags cannot be merged into one! 9352639Sarchie */ 9452639Sarchie uint8_t peer_suspended:1; /* set if peer is suspended */ 9552639Sarchie uint8_t self_suspended:1; /* set if self is suspended */ 9652639Sarchie}; 9752639Sarchie 9852639Sarchie/* 9952639Sarchie * The following structure is used for power-save purposes. The data 10066775Sarchie * in this structure is protected by the USB BUS lock. 10159882Sarchie */ 10252639Sarchiestruct usb_power_save { 10366775Sarchie usb_ticks_t last_xfer_time; /* copy of "ticks" */ 10466775Sarchie usb_size_t type_refs[4]; /* transfer reference count */ 10566775Sarchie usb_size_t read_refs; /* data read references */ 10666775Sarchie usb_size_t write_refs; /* data write references */ 10766775Sarchie}; 10866775Sarchie 10952639Sarchie/* 11066775Sarchie * The following structure defines an USB device. There exists one of 11166775Sarchie * these structures for every USB device. 11266775Sarchie */ 11366775Sarchiestruct usb_device { 11452639Sarchie struct usb_clear_stall_msg cs_msg[2]; /* generic clear stall 11566775Sarchie * messages */ 11666775Sarchie struct sx default_sx[2]; 11766775Sarchie struct mtx default_mtx[1]; 11852639Sarchie struct cv default_cv[2]; 11952639Sarchie struct usb_interface *ifaces; 12066775Sarchie struct usb_endpoint default_ep; /* Control Endpoint 0 */ 12166775Sarchie struct usb_endpoint *endpoints; 12266775Sarchie struct usb_power_save pwr_save;/* power save data */ 12366775Sarchie struct usb_bus *bus; /* our USB BUS */ 12459882Sarchie device_t parent_dev; /* parent device */ 12559882Sarchie struct usb_device *parent_hub; 12659882Sarchie struct usb_device *parent_hs_hub; /* high-speed parent HUB */ 12759882Sarchie struct usb_config_descriptor *cdesc; /* full config descr */ 12859882Sarchie struct usb_hub *hub; /* only if this is a hub */ 12959882Sarchie struct usb_xfer *default_xfer[USB_DEFAULT_XFER_MAX]; 13059882Sarchie struct usb_temp_data *usb_template_ptr; 13159882Sarchie struct usb_endpoint *ep_curr; /* current clear stall endpoint */ 13259882Sarchie#if USB_HAVE_UGEN 13359882Sarchie struct usb_fifo *fifo[USB_FIFO_MAX]; 13452639Sarchie struct usb_symlink *ugen_symlink; /* our generic symlink */ 13552639Sarchie struct cdev *default_dev; /* Control Endpoint 0 device node */ 13659882Sarchie LIST_HEAD(,usb_fs_privdata) pd_list; 13759882Sarchie char ugen_name[20]; /* name of ugenX.X device */ 13859882Sarchie#endif 13959882Sarchie usb_ticks_t plugtime; /* copy of "ticks" */ 14059882Sarchie 14159882Sarchie enum usb_dev_state state; 14268761Smckusick enum usb_dev_speed speed; 14352639Sarchie uint16_t refcount; 14452639Sarchie#define USB_DEV_REF_MAX 0xffff 14552639Sarchie 14652639Sarchie uint16_t power; /* mA the device uses */ 14752639Sarchie uint16_t langid; /* language for strings */ 14852639Sarchie 14952639Sarchie uint8_t address; /* device addess */ 15052639Sarchie uint8_t device_index; /* device index in "bus->devices" */ 15152639Sarchie uint8_t curr_config_index; /* current configuration index */ 15252639Sarchie uint8_t curr_config_no; /* current configuration number */ 15352639Sarchie uint8_t depth; /* distance from root HUB */ 15452639Sarchie uint8_t port_index; /* parent HUB port index */ 15552639Sarchie uint8_t port_no; /* parent HUB port number */ 15652639Sarchie uint8_t hs_hub_addr; /* high-speed HUB address */ 15752639Sarchie uint8_t hs_port_no; /* high-speed HUB port number */ 15852639Sarchie uint8_t driver_added_refcount; /* our driver added generation count */ 15952639Sarchie uint8_t power_mode; /* see USB_POWER_XXX */ 16052639Sarchie uint8_t ifaces_max; /* number of interfaces present */ 16152639Sarchie uint8_t endpoints_max; /* number of endpoints present */ 16252639Sarchie 16352639Sarchie /* the "flags" field is write-protected by "bus->mtx" */ 16452639Sarchie 16552639Sarchie struct usb_device_flags flags; 16652639Sarchie 16752639Sarchie struct usb_endpoint_descriptor default_ep_desc; /* for endpoint 0 */ 16852639Sarchie struct usb_device_descriptor ddesc; /* device descriptor */ 16952639Sarchie 17052639Sarchie char *serial; /* serial number */ 17159882Sarchie char *manufacturer; /* manufacturer string */ 17259882Sarchie char *product; /* product string */ 17352639Sarchie 17459882Sarchie#if USB_HAVE_COMPAT_LINUX 17552639Sarchie /* Linux compat */ 17652639Sarchie struct usb_device_descriptor descriptor; 17752639Sarchie struct usb_host_endpoint ep0; 17852639Sarchie struct usb_interface *linux_iface_start; 17970784Sjulian struct usb_interface *linux_iface_end; 18070784Sjulian struct usb_host_endpoint *linux_endpoint_start; 18152639Sarchie struct usb_host_endpoint *linux_endpoint_end; 18270784Sjulian uint16_t devnum; 18352639Sarchie#endif 18459882Sarchie}; 18559882Sarchie 18659882Sarchie/* globals */ 18759882Sarchie 18866775Sarchieextern int usb_template; 18959882Sarchie 19059882Sarchie/* function prototypes */ 19159882Sarchie 19259882Sarchieconst char *usb_statestr(enum usb_dev_state state); 19359882Sarchiestruct usb_device *usb_alloc_device(device_t parent_dev, struct usb_bus *bus, 19459882Sarchie struct usb_device *parent_hub, uint8_t depth, 19553406Sarchie uint8_t port_index, uint8_t port_no, 19659882Sarchie enum usb_dev_speed speed, enum usb_hc_mode mode); 19759882Sarchieusb_error_t usb_probe_and_attach(struct usb_device *udev, 19859882Sarchie uint8_t iface_index); 19966775Sarchieusb_error_t usb_reset_iface_endpoints(struct usb_device *udev, 20066775Sarchie uint8_t iface_index); 20159882Sarchieusb_error_t usbd_set_config_index(struct usb_device *udev, uint8_t index); 20259882Sarchieusb_error_t usbd_set_endpoint_stall(struct usb_device *udev, 20359882Sarchie struct usb_endpoint *ep, uint8_t do_stall); 20459882Sarchieusb_error_t usb_suspend_resume(struct usb_device *udev, 20559882Sarchie uint8_t do_suspend); 20659882Sarchievoid usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len); 20759882Sarchievoid usb_free_device(struct usb_device *, uint8_t); 20868761Smckusickvoid usb_linux_free_device(struct usb_device *dev); 20959882Sarchieuint8_t usb_peer_can_wakeup(struct usb_device *udev); 21059882Sarchiestruct usb_endpoint *usb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep); 21159882Sarchievoid usb_set_device_state(struct usb_device *udev, 21252419Sjulian enum usb_dev_state state); 21353406Sarchievoid usbd_enum_lock(struct usb_device *); 21452419Sjulianvoid usbd_enum_unlock(struct usb_device *); 21552419Sjulianuint8_t usbd_enum_is_locked(struct usb_device *); 21652752Sjulian 21752752Sjulian#endif /* _USB_DEVICE_H_ */ 21870700Sjulian