aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/include
diff options
context:
space:
mode:
authorFabien Poussin <fabien.poussin@gmail.com>2016-02-15 23:34:25 +0100
committerFabien Poussin <fabien.poussin@gmail.com>2016-02-15 23:34:25 +0100
commit771feb098db86458340ab2665dfb23bef970ace6 (patch)
treef44218022dc6e96b669138c2bda65ad1f0f7e608 /os/hal/include
parent1548bca80f5af0fe3f79331ae5a3d51b18f1c077 (diff)
downloadChibiOS-Contrib-771feb098db86458340ab2665dfb23bef970ace6.tar.gz
ChibiOS-Contrib-771feb098db86458340ab2665dfb23bef970ace6.tar.bz2
ChibiOS-Contrib-771feb098db86458340ab2665dfb23bef970ace6.zip
USB-Host: Initial commit
Diffstat (limited to 'os/hal/include')
-rw-r--r--os/hal/include/hal_community.h1
-rw-r--r--os/hal/include/usbh.h439
-rw-r--r--os/hal/include/usbh/debug.h44
-rw-r--r--os/hal/include/usbh/defs.h160
-rw-r--r--os/hal/include/usbh/desciter.h63
-rw-r--r--os/hal/include/usbh/dev/ftdi.h154
-rw-r--r--os/hal/include/usbh/dev/hub.h138
-rw-r--r--os/hal/include/usbh/dev/msd.h125
-rw-r--r--os/hal/include/usbh/internal.h148
-rw-r--r--os/hal/include/usbh/list.h598
10 files changed, 1870 insertions, 0 deletions
diff --git a/os/hal/include/hal_community.h b/os/hal/include/hal_community.h
index 426fadd..8a17765 100644
--- a/os/hal/include/hal_community.h
+++ b/os/hal/include/hal_community.h
@@ -32,6 +32,7 @@
/* Normal drivers.*/
#include "nand.h"
#include "eicu.h"
+#include "usbh.h"
/* Complex drivers.*/
#include "onewire.h"
diff --git a/os/hal/include/usbh.h b/os/hal/include/usbh.h
new file mode 100644
index 0000000..2f8f3dd
--- /dev/null
+++ b/os/hal/include/usbh.h
@@ -0,0 +1,439 @@
+/*
+ ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
+ Copyright (C) 2015 Diego Ismirlian, TISA, (dismirlian (at) google's mail)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+#ifndef USBH_H_
+#define USBH_H_
+
+#include "hal.h"
+
+#ifndef HAL_USE_USBH
+#define HAL_USE_USBH FALSE
+#endif
+
+#ifndef HAL_USBH_USE_FTDI
+#define HAL_USBH_USE_FTDI FALSE
+#endif
+
+#ifndef HAL_USBH_USE_HUB
+#define HAL_USBH_USE_HUB FALSE
+#endif
+
+#ifndef HAL_USBH_USE_MSD
+#define HAL_USBH_USE_MSD FALSE
+#endif
+
+#ifndef HAL_USBH_USE_UVC
+#define HAL_USBH_USE_UVC FALSE
+#endif
+
+#if HAL_USE_USBH
+
+#include "osal.h"
+#include "usbh/list.h"
+#include "usbh/defs.h"
+
+/* TODO:
+ *
+ * - Integrate VBUS power switching functionality to the API.
+ *
+ */
+
+
+/*===========================================================================*/
+/* Derived constants and error checks. */
+/*===========================================================================*/
+
+#if !HAL_USBH_USE_HUB
+#define USBH_MAX_ADDRESSES 1
+#else
+#define USBH_MAX_ADDRESSES (HAL_USBHHUB_MAX_PORTS + 1)
+#endif
+
+enum usbh_status {
+ USBH_STATUS_STOPPED = 0,
+ USBH_STATUS_STARTED,
+ USBH_STATUS_SUSPENDED,
+};
+
+enum usbh_devstatus {
+ USBH_DEVSTATUS_DISCONNECTED = 0,
+ USBH_DEVSTATUS_ATTACHED,
+ USBH_DEVSTATUS_CONNECTED,
+ USBH_DEVSTATUS_DEFAULT,
+ USBH_DEVSTATUS_ADDRESS,
+ USBH_DEVSTATUS_CONFIGURED,
+};
+
+enum usbh_devspeed {
+ USBH_DEVSPEED_LOW = 0,
+ USBH_DEVSPEED_FULL,
+ USBH_DEVSPEED_HIGH,
+};
+
+enum usbh_epdir {
+ USBH_EPDIR_IN = 0x80,
+ USBH_EPDIR_OUT = 0
+};
+
+enum usbh_eptype {
+ USBH_EPTYPE_CTRL = 0,
+ USBH_EPTYPE_ISO = 1,
+ USBH_EPTYPE_BULK = 2,
+ USBH_EPTYPE_INT = 3,
+};
+
+enum usbh_epstatus {
+ USBH_EPSTATUS_UNINITIALIZED = 0,
+ USBH_EPSTATUS_CLOSED,
+ USBH_EPSTATUS_OPEN,
+ USBH_EPSTATUS_HALTED,
+};
+
+enum usbh_urbstatus {
+ USBH_URBSTATUS_UNINITIALIZED = 0,
+ USBH_URBSTATUS_INITIALIZED,
+ USBH_URBSTATUS_PENDING,
+// USBH_URBSTATUS_QUEUED,
+ USBH_URBSTATUS_ERROR,
+ USBH_URBSTATUS_TIMEOUT,
+ USBH_URBSTATUS_CANCELLED,
+ USBH_URBSTATUS_STALL,
+ USBH_URBSTATUS_DISCONNECTED,
+// USBH_URBSTATUS_EPCLOSED,
+ USBH_URBSTATUS_OK,
+};
+
+/*===========================================================================*/
+/* Driver data structures and types. */
+/*===========================================================================*/
+
+/* forward declarations */
+typedef struct USBHDriver USBHDriver;
+typedef struct usbh_port usbh_port_t;
+typedef struct usbh_device usbh_device_t;
+typedef struct usbh_ep usbh_ep_t;
+typedef struct usbh_urb usbh_urb_t;
+typedef struct usbh_baseclassdriver usbh_baseclassdriver_t;
+typedef struct usbh_classdriverinfo usbh_classdriverinfo_t;
+#if HAL_USBH_USE_HUB
+typedef struct USBHHubDriver USBHHubDriver;
+#endif
+
+/* typedefs */
+typedef enum usbh_status usbh_status_t;
+typedef enum usbh_devspeed usbh_devspeed_t;
+typedef enum usbh_devstatus usbh_devstatus_t;
+typedef enum usbh_epdir usbh_epdir_t;
+typedef enum usbh_eptype usbh_eptype_t;
+typedef enum usbh_epstatus usbh_epstatus_t;
+typedef enum usbh_urbstatus usbh_urbstatus_t;
+typedef uint16_t usbh_portstatus_t;
+typedef uint16_t usbh_portcstatus_t;
+typedef void (*usbh_completion_cb)(usbh_urb_t *);
+
+/* include the low level driver; the required definitions are above */
+#include "usbh_lld.h"
+
+#define USBH_DEFINE_BUFFER(type, name) USBH_LLD_DEFINE_BUFFER(type, name)
+
+struct usbh_urb {
+ usbh_ep_t *ep;
+
+ void *userData;
+ usbh_completion_cb callback;
+
+ const void *setup_buff;
+ void *buff;
+ uint32_t requestedLength;
+ uint32_t actualLength;
+
+ usbh_urbstatus_t status;
+
+ thread_reference_t waitingThread;
+ thread_reference_t abortingThread;
+
+ /* Low level part */
+ _usbh_urb_ll_data
+};
+
+struct usbh_ep {
+ usbh_device_t *device;
+ usbh_ep_t *next;
+
+ usbh_epstatus_t status;
+ uint8_t address;
+ bool in;
+ usbh_eptype_t type;
+ uint16_t wMaxPacketSize;
+ uint8_t bInterval;
+
+ /* debug */
+ const char *name;
+
+ /* Low-level part */
+ _usbh_ep_ll_data
+};
+
+struct usbh_device {
+ USBHDriver *host; /* shortcut to host */
+
+ usbh_ep_t ctrl;
+ usbh_ep_t *endpoints;
+
+ usbh_baseclassdriver_t *drivers;
+
+ uint16_t langID0;
+
+ usbh_devstatus_t status;
+ usbh_devspeed_t speed;
+
+ USBH_DEFINE_BUFFER(usbh_device_descriptor_t, devDesc);
+ unsigned char align_bytes[2];
+ USBH_DEFINE_BUFFER(usbh_config_descriptor_t, basicConfigDesc);
+
+ uint8_t *fullConfigurationDescriptor;
+ uint8_t keepFullCfgDesc;
+
+ uint8_t address;
+ uint8_t bConfiguration;
+
+ /* Low level part */
+ _usbh_device_ll_data
+};
+
+
+struct usbh_port {
+#if HAL_USBH_USE_HUB
+ USBHHubDriver *hub;
+#endif
+
+ usbh_portstatus_t status;
+ usbh_portcstatus_t c_status;
+
+ usbh_port_t *next;
+
+ uint8_t number;
+
+ usbh_device_t device;
+
+ /* Low level part */
+ _usbh_port_ll_data
+};
+
+struct USBHDriver {
+ usbh_status_t status;
+ uint8_t address_bitmap[(USBH_MAX_ADDRESSES + 7) / 8];
+
+ usbh_port_t rootport;
+
+#if HAL_USBH_USE_HUB
+ struct list_head hubs;
+#endif
+
+ /* Low level part */
+ _usbhdriver_ll_data
+
+#if USBH_DEBUG_ENABLE
+ /* debug */
+ uint8_t dbg_buff[USBH_DEBUG_BUFFER];
+ THD_WORKING_AREA(waDebug, 512);
+ input_queue_t iq;
+#endif
+};
+
+
+
+/*===========================================================================*/
+/* External declarations. */
+/*===========================================================================*/
+
+#if STM32_USBH_USE_OTG1
+extern USBHDriver USBHD1;
+#endif
+
+#if STM32_USBH_USE_OTG2
+extern USBHDriver USBHD2;
+#endif
+
+
+/*===========================================================================*/
+/* Main driver API. */
+/*===========================================================================*/
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+ /* Main functions */
+ void usbhObjectInit(USBHDriver *usbh);
+ void usbhInit(void);
+ void usbhStart(USBHDriver *usbh);
+ void usbhStop(USBHDriver *usbh);
+ void usbhSuspend(USBHDriver *usbh);
+ void usbhResume(USBHDriver *usbh);
+
+ /* Device-related */
+#if USBH_DEBUG_ENABLE && USBH_DEBUG_ENABLE_INFO
+ void usbhDevicePrintInfo(usbh_device_t *dev);
+ void usbhDevicePrintConfiguration(const uint8_t *descriptor, uint16_t rem);
+#else
+# define usbhDevicePrintInfo(dev) do {} while(0)
+# define usbhDevicePrintConfiguration(descriptor, rem) do {} while(0)
+#endif
+ bool usbhDeviceReadString(usbh_device_t *dev, char *dest, uint8_t size,
+ uint8_t index, uint16_t langID);
+ static inline usbh_port_t *usbhDeviceGetPort(usbh_device_t *dev) {
+ return container_of(dev, usbh_port_t, device);
+ }
+
+ /* Synchronous API */
+ usbh_urbstatus_t usbhBulkTransfer(usbh_ep_t *ep,
+ void *data,
+ uint32_t len,
+ uint32_t *actual_len,
+ systime_t timeout);
+ usbh_urbstatus_t usbhControlRequest(usbh_device_t *dev,
+ uint8_t bmRequestType,
+ uint8_t bRequest,
+ uint16_t wValue,
+ uint16_t wIndex,
+ uint16_t wLength,
+ uint8_t *buff);
+ usbh_urbstatus_t usbhControlRequestExtended(usbh_device_t *dev,
+ const usbh_control_request_t *req,
+ uint8_t *buff,
+ uint32_t *actual_len,
+ systime_t timeout);
+
+ /* Standard request helpers */
+ bool usbhStdReqGetDeviceDescriptor(usbh_device_t *dev,
+ uint16_t wLength,
+ uint8_t *buf);
+ bool usbhStdReqGetConfigurationDescriptor(usbh_device_t *dev,
+ uint8_t index,
+ uint16_t wLength,
+ uint8_t *buf);
+ bool usbhStdReqGetStringDescriptor(usbh_device_t *dev,
+ uint8_t index,
+ uint16_t langID,
+ uint16_t wLength,
+ uint8_t *buf);
+ bool usbhStdReqSetInterface(usbh_device_t *dev,
+ uint8_t bInterfaceNumber,
+ uint8_t bAlternateSetting);
+ bool usbhStdReqGetInterface(usbh_device_t *dev,
+ uint8_t bInterfaceNumber,
+ uint8_t *bAlternateSetting);
+
+ /* Endpoint/pipe management */
+ void usbhEPObjectInit(usbh_ep_t *ep, usbh_device_t *dev, const usbh_endpoint_descriptor_t *desc);
+ static inline void usbhEPOpen(usbh_ep_t *ep) {
+ osalDbgCheck(ep != 0);
+ osalSysLock();
+ osalDbgAssert(ep->status == USBH_EPSTATUS_CLOSED, "invalid state");
+ usbh_lld_ep_open(ep);
+ ep->next = ep->device->endpoints;
+ ep->device->endpoints = ep;
+ osalSysUnlock();
+ }
+ static inline void usbhEPCloseS(usbh_ep_t *ep) {
+ osalDbgCheck(ep != 0);
+ osalDbgCheckClassS();
+ osalDbgAssert(ep->status != USBH_EPSTATUS_UNINITIALIZED, "invalid state");
+ if (ep->status == USBH_EPSTATUS_CLOSED) {
+ osalOsRescheduleS();
+ return;
+ }
+ usbh_lld_ep_close(ep);
+ }
+ static inline void usbhEPClose(usbh_ep_t *ep) {
+ osalSysLock();
+ usbhEPCloseS(ep);
+ osalSysUnlock();
+ }
+ static inline void usbhEPResetI(usbh_ep_t *ep) {
+ osalDbgCheckClassI();
+ osalDbgCheck(ep != NULL);
+ usbh_lld_epreset(ep);
+ }
+ static inline bool usbhEPIsPeriodic(usbh_ep_t *ep) {
+ osalDbgCheck(ep != NULL);
+ return (ep->type & 1) != 0;
+ }
+ static inline bool usbhURBIsBusy(usbh_urb_t *urb) {
+ osalDbgCheck(urb != NULL);
+ return (urb->status == USBH_URBSTATUS_PENDING);
+ }
+ static inline void usbhEPSetName(usbh_ep_t *ep, const char *name) {
+ ep->name = name;
+ }
+
+ /* URB management */
+ void usbhURBObjectInit(usbh_urb_t *urb, usbh_ep_t *ep, usbh_completion_cb callback,
+ void *user, void *buff, uint32_t len);
+ void usbhURBObjectResetI(usbh_urb_t *urb);
+ void usbhURBSubmitI(usbh_urb_t *urb);
+ bool usbhURBCancelI(usbh_urb_t *urb);
+ msg_t usbhURBSubmitAndWaitS(usbh_urb_t *urb, systime_t timeout);
+ void usbhURBCancelAndWaitS(usbh_urb_t *urb);
+ msg_t usbhURBWaitTimeoutS(usbh_urb_t *urb, systime_t timeout);
+
+ /* Main loop */
+ void usbhMainLoop(USBHDriver *usbh);
+
+#ifdef __cplusplus
+}
+#endif
+
+
+/*===========================================================================*/
+/* Class driver definitions and API. */
+/*===========================================================================*/
+
+typedef struct usbh_classdriver_vmt usbh_classdriver_vmt_t;
+struct usbh_classdriver_vmt {
+ usbh_baseclassdriver_t *(*load)(usbh_device_t *dev, const uint8_t *descriptor, uint16_t rem);
+ void (*unload)(usbh_baseclassdriver_t *drv);
+};
+
+struct usbh_classdriverinfo {
+ int16_t class;
+ int16_t subclass;
+ int16_t protocol;
+ const char *name;
+ const usbh_classdriver_vmt_t *vmt;
+};
+
+#define _usbh_base_classdriver_data \
+ const usbh_classdriverinfo_t *info; \
+ usbh_device_t *dev; \
+ usbh_baseclassdriver_t *next;
+
+struct usbh_baseclassdriver {
+ _usbh_base_classdriver_data
+};
+
+
+/*===========================================================================*/
+/* Helper functions. */
+/*===========================================================================*/
+#include <usbh/desciter.h> /* descriptor iterators */
+#include <usbh/debug.h> /* debug */
+
+#endif
+
+#endif /* USBH_H_ */
diff --git a/os/hal/include/usbh/debug.h b/os/hal/include/usbh/debug.h
new file mode 100644
index 0000000..219eeb1
--- /dev/null
+++ b/os/hal/include/usbh/debug.h
@@ -0,0 +1,44 @@
+/*
+ ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
+ Copyright (C) 2015 Diego Ismirlian, TISA, (dismirlian (at) google's mail)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+
+#ifndef USBH_DEBUG_H_
+#define USBH_DEBUG_H_
+
+#include "usbh.h"
+
+#if HAL_USE_USBH
+
+//TODO: Debug is only for USBHD1, make it generic.
+
+#if USBH_DEBUG_ENABLE
+ void usbDbgPrintf(const char *fmt, ...);
+ void usbDbgPuts(const char *s);
+ void usbDbgInit(USBHDriver *host);
+ void usbDbgReset(void);
+ void usbDbgSystemHalted(void);
+#else
+#define usbDbgPrintf(fmt, ...) do {} while(0)
+#define usbDbgPuts(s) do {} while(0)
+#define usbDbgInit(host) do {} while(0)
+#define usbDbgReset() do {} while(0)
+#define usbDbgSystemHalted() do {} while(0)
+#endif
+
+#endif
+
+#endif /* USBH_DEBUG_H_ */
diff --git a/os/hal/include/usbh/defs.h b/os/hal/include/usbh/defs.h
new file mode 100644
index 0000000..c3d8a9a
--- /dev/null
+++ b/os/hal/include/usbh/defs.h
@@ -0,0 +1,160 @@
+/*
+ ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
+ Copyright (C) 2015 Diego Ismirlian, TISA, (dismirlian (at) google's mail)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+#ifndef USBH_DEFS_H_
+#define USBH_DEFS_H_
+
+#include "hal.h"
+
+#if HAL_USE_USBH
+
+#include "osal.h"
+
+#ifdef __IAR_SYSTEMS_ICC__
+#define PACKED_STRUCT typedef PACKED_VAR struct
+#else
+#define PACKED_STRUCT typedef struct PACKED_VAR
+#endif
+
+PACKED_STRUCT {
+ uint8_t bLength;
+ uint8_t bDescriptorType;
+ uint16_t bcdUSB;
+ uint8_t bDeviceClass;
+ uint8_t bDeviceSubClass;
+ uint8_t bDeviceProtocol;
+ uint8_t bMaxPacketSize0;
+ uint16_t idVendor;
+ uint16_t idProduct;
+ uint16_t bcdDevice;
+ uint8_t iManufacturer;
+ uint8_t iProduct;
+ uint8_t iSerialNumber;
+ uint8_t bNumConfigurations;
+} usbh_device_descriptor_t;
+#define USBH_DT_DEVICE 0x01
+#define USBH_DT_DEVICE_SIZE 18
+
+PACKED_STRUCT {
+ uint8_t bLength;
+ uint8_t bDescriptorType;
+ uint16_t wTotalLength;
+ uint8_t bNumInterfaces;
+ uint8_t bConfigurationValue;
+ uint8_t iConfiguration;
+ uint8_t bmAttributes;
+ uint8_t bMaxPower;
+} usbh_config_descriptor_t;
+#define USBH_DT_CONFIG 0x02
+#define USBH_DT_CONFIG_SIZE 9
+
+PACKED_STRUCT {
+ uint8_t bLength;
+ uint8_t bDescriptorType;
+ uint16_t wData[1];
+} usbh_string_descriptor_t;
+#define USBH_DT_STRING 0x03
+#define USBH_DT_STRING_SIZE 2
+
+PACKED_STRUCT {
+ uint8_t bLength;
+ uint8_t bDescriptorType;
+ uint8_t bInterfaceNumber;
+ uint8_t bAlternateSetting;
+ uint8_t bNumEndpoints;
+ uint8_t bInterfaceClass;
+ uint8_t bInterfaceSubClass;
+ uint8_t bInterfaceProtocol;
+ uint8_t iInterface;
+} usbh_interface_descriptor_t;
+#define USBH_DT_INTERFACE 0x04
+#define USBH_DT_INTERFACE_SIZE 9
+
+PACKED_STRUCT {
+ uint8_t bLength;
+ uint8_t bDescriptorType;
+ uint8_t bEndpointAddress;
+ uint8_t bmAttributes;
+ uint16_t wMaxPacketSize;
+ uint8_t bInterval;
+} usbh_endpoint_descriptor_t;
+#define USBH_DT_ENDPOINT 0x05
+#define USBH_DT_ENDPOINT_SIZE 7
+
+PACKED_STRUCT {
+ uint8_t bLength;
+ uint8_t bDescriptorType;
+ uint8_t bFirstInterface;
+ uint8_t bInterfaceCount;
+ uint8_t bFunctionClass;
+ uint8_t bFunctionSubClass;
+ uint8_t bFunctionProtocol;
+ uint8_t iFunction;
+} usbh_ia_descriptor_t;
+#define USBH_DT_INTERFACE_ASSOCIATION 0x0b
+#define USBH_DT_INTERFACE_ASSOCIATION_SIZE 8
+
+PACKED_STRUCT {
+ uint8_t bDescLength;
+ uint8_t bDescriptorType;
+ uint8_t bNbrPorts;
+ uint16_t wHubCharacteristics;
+ uint8_t bPwrOn2PwrGood;
+ uint8_t bHubContrCurrent;
+ uint32_t DeviceRemovable;
+} usbh_hub_descriptor_t;
+#define USBH_DT_HUB 0x29
+#define USBH_DT_HUB_SIZE (7 + 4)
+
+PACKED_STRUCT {
+ uint8_t bmRequestType;
+ uint8_t bRequest;
+ uint16_t wValue;
+ uint16_t wIndex;
+ uint16_t wLength;
+} usbh_control_request_t;
+
+
+#define USBH_REQ_GET_STATUS 0x00
+#define USBH_REQ_CLEAR_FEATURE 0x01
+#define USBH_REQ_SET_FEATURE 0x03
+#define USBH_REQ_SET_ADDRESS 0x05
+#define USBH_REQ_GET_DESCRIPTOR 0x06
+#define USBH_REQ_SET_DESCRIPTOR 0x07
+#define USBH_REQ_GET_CONFIGURATION 0x08
+#define USBH_REQ_SET_CONFIGURATION 0x09
+#define USBH_REQ_GET_INTERFACE 0x0A
+#define USBH_REQ_SET_INTERFACE 0x0B
+#define USBH_REQ_SYNCH_FRAME 0x0C
+
+
+#define USBH_REQTYPE_IN 0x80
+#define USBH_REQTYPE_OUT 0x00
+
+#define USBH_REQTYPE_STANDARD 0x00
+#define USBH_REQTYPE_CLASS 0x20
+#define USBH_REQTYPE_VENDOR 0x40
+
+#define USBH_REQTYPE_DEVICE 0x00
+#define USBH_REQTYPE_INTERFACE 0x01
+#define USBH_REQTYPE_ENDPOINT 0x02
+#define USBH_REQTYPE_OTHER 0x03
+
+#endif
+
+
+#endif /* USBH_DEFS_H_ */
diff --git a/os/hal/include/usbh/desciter.h b/os/hal/include/usbh/desciter.h
new file mode 100644
index 0000000..52b0c98
--- /dev/null
+++ b/os/hal/include/usbh/desciter.h
@@ -0,0 +1,63 @@
+/*
+ ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
+ Copyright (C) 2015 Diego Ismirlian, TISA, (dismirlian (at) google's mail)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+
+#ifndef USBH_DESCITER_H_
+#define USBH_DESCITER_H_
+
+#include "hal.h"
+
+#if HAL_USE_USBH
+
+#include "usbh/defs.h"
+
+
+/* DESCRIPTOR PARSING */
+#define _generic_iterator_fields \
+ const uint8_t *curr; \
+ uint16_t rem; \
+ bool valid;
+
+typedef struct {
+ _generic_iterator_fields
+} generic_iterator_t;
+
+typedef struct {
+ _generic_iterator_fields
+ const usbh_ia_descriptor_t *iad;
+} if_iterator_t;
+
+void cfg_iter_init(generic_iterator_t *icfg, const uint8_t *buff, uint16_t rem);
+void if_iter_init(if_iterator_t *iif, const generic_iterator_t *icfg);
+void ep_iter_init(generic_iterator_t *iep, const if_iterator_t *iif);
+void cs_iter_init(generic_iterator_t *ics, const generic_iterator_t *iter);
+void if_iter_next(if_iterator_t *iif);
+void ep_iter_next(generic_iterator_t *iep);
+void cs_iter_next(generic_iterator_t *ics);
+static inline const usbh_config_descriptor_t *cfg_get(generic_iterator_t *icfg) {
+ return (const usbh_config_descriptor_t *)icfg->curr;
+}
+static inline const usbh_interface_descriptor_t *if_get(if_iterator_t *iif) {
+ return (const usbh_interface_descriptor_t *)iif->curr;
+}
+static inline const usbh_endpoint_descriptor_t *ep_get(generic_iterator_t *iep) {
+ return (const usbh_endpoint_descriptor_t *)iep->curr;
+}
+
+#endif
+
+#endif /* USBH_DESCITER_H_ */
diff --git a/os/hal/include/usbh/dev/ftdi.h b/os/hal/include/usbh/dev/ftdi.h
new file mode 100644
index 0000000..678a521
--- /dev/null
+++ b/os/hal/include/usbh/dev/ftdi.h
@@ -0,0 +1,154 @@
+/*
+ ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
+ Copyright (C) 2015 Diego Ismirlian, TISA, (dismirlian (at) google's mail)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+#ifndef USBH_FTDI_H_
+#define USBH_FTDI_H_
+
+#include "usbh.h"
+
+#if HAL_USE_USBH && HAL_USBH_USE_FTDI
+
+/*===========================================================================*/
+/* Driver pre-compile time settings. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Derived constants and error checks. */
+/*===========================================================================*/
+#define USBHFTDI_FRAMING_DATABITS_7 (0x7 << 0)
+#define USBHFTDI_FRAMING_DATABITS_8 (0x8 << 0)
+#define USBHFTDI_FRAMING_PARITY_NONE (0x0 << 8)
+#define USBHFTDI_FRAMING_PARITY_NONE (0x0 << 8)
+#define USBHFTDI_FRAMING_PARITY_ODD (0x1 << 8)
+#define USBHFTDI_FRAMING_PARITY_EVEN (0x2 << 8)
+#define USBHFTDI_FRAMING_PARITY_MARK (0x3 << 8)
+#define USBHFTDI_FRAMING_PARITY_SPACE (0x4 << 8)
+#define USBHFTDI_FRAMING_STOP_BITS_1 (0x0 << 11)
+#define USBHFTDI_FRAMING_STOP_BITS_15 (0x1 << 11)
+#define USBHFTDI_FRAMING_STOP_BITS_2 (0x2 << 11)
+
+#define USBHFTDI_HANDSHAKE_NONE (0x0)
+#define USBHFTDI_HANDSHAKE_RTS_CTS (0x1)
+#define USBHFTDI_HANDSHAKE_DTR_DSR (0x2)
+#define USBHFTDI_HANDSHAKE_XON_XOFF (0x4)
+
+
+
+/*===========================================================================*/
+/* Driver data structures and types. */
+/*===========================================================================*/
+typedef struct {
+ uint32_t speed;
+ uint16_t framing;
+ uint8_t handshake;
+ uint8_t xon_character;
+ uint8_t xoff_character;
+} USBHFTDIPortConfig;
+
+typedef enum {
+ USBHFTDI_TYPE_A,
+ USBHFTDI_TYPE_B,
+ USBHFTDI_TYPE_H,
+} usbhftdi_type_t;
+
+typedef enum {
+ USBHFTDIP_STATE_UNINIT = 0,
+ USBHFTDIP_STATE_STOP = 1,
+ USBHFTDIP_STATE_ACTIVE = 2,
+ USBHFTDIP_STATE_READY = 3
+} usbhftdip_state_t;
+
+
+#define _ftdi_port_driver_methods \
+ _base_asynchronous_channel_methods
+
+struct FTDIPortDriverVMT {
+ _ftdi_port_driver_methods
+};
+
+typedef struct USBHFTDIPortDriver USBHFTDIPortDriver;
+typedef struct USBHFTDIDriver USBHFTDIDriver;
+
+struct USBHFTDIPortDriver {
+ /* inherited from abstract asyncrhonous channel driver */
+ const struct FTDIPortDriverVMT *vmt;
+ _base_asynchronous_channel_data
+
+ USBHFTDIDriver *ftdip;
+
+ usbhftdip_state_t state;
+
+ usbh_ep_t epin;
+ usbh_urb_t iq_urb;
+ threads_queue_t iq_waiting;
+ uint32_t iq_counter;
+ USBH_DEFINE_BUFFER(uint8_t, iq_buff[64]);
+ uint8_t *iq_ptr;
+
+
+ usbh_ep_t epout;
+ usbh_urb_t oq_urb;
+ threads_queue_t oq_waiting;
+ uint32_t oq_counter;
+ USBH_DEFINE_BUFFER(uint8_t, oq_buff[64]);
+ uint8_t *oq_ptr;
+
+ virtual_timer_t vt;
+ uint8_t ifnum;
+
+ USBHFTDIPortDriver *next;
+};
+
+typedef struct USBHFTDIDriver {
+ /* inherited from abstract class driver */
+ _usbh_base_classdriver_data
+
+ usbhftdi_type_t type;
+ USBHFTDIPortDriver *ports;
+
+ mutex_t mtx;
+} USBHFTDIDriver;
+
+/*===========================================================================*/
+/* Driver macros. */
+/*===========================================================================*/
+
+
+/*===========================================================================*/
+/* External declarations. */
+/*===========================================================================*/
+extern USBHFTDIDriver USBHFTDID[HAL_USBHFTDI_MAX_INSTANCES];
+extern USBHFTDIPortDriver FTDIPD[HAL_USBHFTDI_MAX_PORTS];
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+ /* FTDI device driver */
+ void usbhftdiObjectInit(USBHFTDIDriver *ftdip);
+
+ /* FTDI port driver */
+ void usbhftdipObjectInit(USBHFTDIPortDriver *ftdipp);
+ void usbhftdipStart(USBHFTDIPortDriver *ftdipp, const USBHFTDIPortConfig *config);
+ void usbhftdipStop(USBHFTDIPortDriver *ftdipp);
+#ifdef __cplusplus
+}
+#endif
+
+
+#endif
+
+#endif /* USBH_FTDI_H_ */
diff --git a/os/hal/include/usbh/dev/hub.h b/os/hal/include/usbh/dev/hub.h
new file mode 100644
index 0000000..28a0681
--- /dev/null
+++ b/os/hal/include/usbh/dev/hub.h
@@ -0,0 +1,138 @@
+/*
+ ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
+ Copyright (C) 2015 Diego Ismirlian, TISA, (dismirlian (at) google's mail)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+#ifndef USBH_HUB_H_
+#define USBH_HUB_H_
+
+#include "usbh.h"
+
+#if HAL_USE_USBH
+#if HAL_USBH_USE_HUB
+
+typedef struct USBHHubDriver {
+ /* inherited from abstract class driver */
+ _usbh_base_classdriver_data
+
+ struct list_head node;
+
+ usbh_ep_t epint;
+ usbh_urb_t urb;
+
+ USBH_DEFINE_BUFFER(uint8_t, scbuff[4]);
+ volatile uint32_t statuschange;
+ uint16_t status;
+ uint16_t c_status;
+
+ usbh_port_t *ports;
+
+ USBH_DEFINE_BUFFER(usbh_hub_descriptor_t, hubDesc);
+
+ /* Low level part */
+ _usbh_hub_ll_data
+
+} USBHHubDriver;
+
+extern USBHHubDriver USBHHUBD[HAL_USBHHUB_MAX_INSTANCES];
+
+
+usbh_urbstatus_t usbhhubControlRequest(USBHDriver *host, USBHHubDriver *hub,
+ uint8_t bmRequestType,
+ uint8_t bRequest,
+ uint16_t wValue,
+ uint16_t wIndex,
+ uint16_t wLength,
+ uint8_t *buf);
+
+
+static inline usbh_urbstatus_t usbhhubClearFeaturePort(usbh_port_t *port, uint8_t feature) {
+ return usbhhubControlRequest(port->device.host, port->hub,
+ USBH_REQTYPE_OUT | USBH_REQTYPE_CLASS | USBH_REQTYPE_OTHER,
+ USBH_REQ_CLEAR_FEATURE,
+ feature,
+ port->number,
+ 0,
+ 0);
+}
+
+static inline usbh_urbstatus_t usbhhubClearFeatureHub(USBHDriver *host, USBHHubDriver *hub, uint8_t feature) {
+ return usbhhubControlRequest(host, hub,
+ USBH_REQTYPE_OUT | USBH_REQTYPE_CLASS | USBH_REQTYPE_DEVICE,
+ USBH_REQ_CLEAR_FEATURE,
+ feature,
+ 0,
+ 0,
+ 0);
+}
+
+static inline usbh_urbstatus_t usbhhubSetFeaturePort(usbh_port_t *port, uint8_t feature) {
+ return usbhhubControlRequest(port->device.host, port->hub,
+ USBH_REQTYPE_OUT | USBH_REQTYPE_CLASS | USBH_REQTYPE_OTHER,
+ USBH_REQ_SET_FEATURE,
+ feature,
+ port->number,
+ 0,
+ 0);
+}
+
+void usbhhubObjectInit(USBHHubDriver *hubdp);
+#else
+
+static inline usbh_urbstatus_t usbhhubControlRequest(USBHDriver *host,
+ uint8_t bmRequestType,
+ uint8_t bRequest,
+ uint16_t wValue,
+ uint16_t wIndex,
+ uint16_t wLength,
+ uint8_t *buf) {
+ return usbh_lld_root_hub_request(host, bmRequestType, bRequest, wValue, wIndex, wLength, buf);
+}
+
+static inline usbh_urbstatus_t usbhhubClearFeaturePort(usbh_port_t *port, uint8_t feature) {
+ return usbhhubControlRequest(port->device.host,
+ USBH_REQTYPE_OUT | USBH_REQTYPE_CLASS | USBH_REQTYPE_OTHER,
+ USBH_REQ_CLEAR_FEATURE,
+ feature,
+ port->number,
+ 0,
+ 0);
+}
+
+static inline usbh_urbstatus_t usbhhubClearFeatureHub(USBHDriver *host, uint8_t feature) {
+ return usbhhubControlRequest(host,
+ USBH_REQTYPE_OUT | USBH_REQTYPE_CLASS | USBH_REQTYPE_DEVICE,
+ USBH_REQ_CLEAR_FEATURE,
+ feature,
+ 0,
+ 0,
+ 0);
+}
+
+static inline usbh_urbstatus_t usbhhubSetFeaturePort(usbh_port_t *port, uint8_t feature) {
+ return usbhhubControlRequest(port->device.host,
+ USBH_REQTYPE_OUT | USBH_REQTYPE_CLASS | USBH_REQTYPE_OTHER,
+ USBH_REQ_SET_FEATURE,
+ feature,
+ port->number,
+ 0,
+ 0);
+}
+
+#endif
+
+#endif
+
+#endif /* USBH_HUB_H_ */
diff --git a/os/hal/include/usbh/dev/msd.h b/os/hal/include/usbh/dev/msd.h
new file mode 100644
index 0000000..2ca8817
--- /dev/null
+++ b/os/hal/include/usbh/dev/msd.h
@@ -0,0 +1,125 @@
+/*
+ ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
+ Copyright (C) 2015 Diego Ismirlian, TISA, (dismirlian (at) google's mail)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+#ifndef USBH_MSD_H_
+#define USBH_MSD_H_
+
+#include "usbh.h"
+
+#if HAL_USE_USBH && HAL_USBH_USE_MSD
+
+/* TODO:
+ *
+ * - Implement of conditional compilation of multiple-luns per instance.
+ * - Implement error checking and recovery when commands fail.
+ *
+ */
+
+
+/*===========================================================================*/
+/* Driver pre-compile time settings. */
+/*===========================================================================*/
+
+
+/*===========================================================================*/
+/* Derived constants and error checks. */
+/*===========================================================================*/
+
+
+/*===========================================================================*/
+/* Driver data structures and types. */
+/*===========================================================================*/
+
+#define _usbhmsd_driver_methods \
+ _base_block_device_methods
+
+struct USBHMassStorageDriverVMT {
+ _usbhmsd_driver_methods
+};
+
+typedef struct USBHMassStorageLUNDriver USBHMassStorageLUNDriver;
+typedef struct USBHMassStorageDriver USBHMassStorageDriver;
+
+struct USBHMassStorageLUNDriver {
+ /* inherited from abstract block driver */
+ const struct USBHMassStorageDriverVMT *vmt;
+ _base_block_device_data
+
+ BlockDeviceInfo info;
+ USBHMassStorageDriver *msdp;
+
+ USBHMassStorageLUNDriver *next;
+};
+
+typedef struct USBHMassStorageDriver {
+ /* inherited from abstract class driver */
+ _usbh_base_classdriver_data
+
+ /* for LUN request serialization, can be removed
+ * if the driver is configured to support only one LUN
+ * per USBHMassStorageDriver instance */
+ mutex_t mtx;
+
+ usbh_ep_t epin;
+ usbh_ep_t epout;
+ uint8_t ifnum;
+ uint8_t max_lun;
+ uint32_t tag;
+
+ USBHMassStorageLUNDriver *luns;
+} USBHMassStorageDriver;
+
+
+/*===========================================================================*/
+/* Driver macros. */
+/*===========================================================================*/
+
+
+/*===========================================================================*/
+/* External declarations. */
+/*===========================================================================*/
+
+extern USBHMassStorageLUNDriver MSBLKD[HAL_USBHMSD_MAX_LUNS];
+extern USBHMassStorageDriver USBHMSD[HAL_USBHMSD_MAX_INSTANCES];
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+ /* Mass Storage Driver */
+ void usbhmsdObjectInit(USBHMassStorageDriver *msdp);
+
+ /* Mass Storage LUN Driver (block driver) */
+ void usbhmsdLUNObjectInit(USBHMassStorageLUNDriver *lunp);
+ void usbhmsdLUNStart(USBHMassStorageLUNDriver *lunp);
+ void usbhmsdLUNStop(USBHMassStorageLUNDriver *lunp);
+ bool usbhmsdLUNConnect(USBHMassStorageLUNDriver *lunp);
+ bool usbhmsdLUNDisconnect(USBHMassStorageLUNDriver *lunp);
+ bool usbhmsdLUNRead(USBHMassStorageLUNDriver *lunp, uint32_t startblk,
+ uint8_t *buffer, uint32_t n);
+ bool usbhmsdLUNWrite(USBHMassStorageLUNDriver *lunp, uint32_t startblk,
+ const uint8_t *buffer, uint32_t n);
+ bool usbhmsdLUNSync(USBHMassStorageLUNDriver *lunp);
+ bool usbhmsdLUNGetInfo(USBHMassStorageLUNDriver *lunp, BlockDeviceInfo *bdip);
+ bool usbhmsdLUNIsInserted(USBHMassStorageLUNDriver *lunp);
+ bool usbhmsdLUNIsProtected(USBHMassStorageLUNDriver *lunp);
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
+#endif /* USBH_MSD_H_ */
diff --git a/os/hal/include/usbh/internal.h b/os/hal/include/usbh/internal.h
new file mode 100644
index 0000000..6811e20
--- /dev/null
+++ b/os/hal/include/usbh/internal.h
@@ -0,0 +1,148 @@
+/*
+ ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
+ Copyright (C) 2015 Diego Ismirlian, TISA, (dismirlian (at) google's mail)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+#ifndef USBH_INTERNAL_H_
+#define USBH_INTERNAL_H_
+
+#include "usbh.h"
+
+#if HAL_USE_USBH
+
+/*===========================================================================*/
+/* These declarations are not part of the public API. */
+/*===========================================================================*/
+
+#if HAL_USBH_USE_FTDI
+extern const usbh_classdriverinfo_t usbhftdiClassDriverInfo;
+#endif
+#if HAL_USBH_USE_MSD
+extern const usbh_classdriverinfo_t usbhmsdClassDriverInfo;
+#endif
+#if HAL_USBH_USE_UVC
+extern const usbh_classdriverinfo_t usbhuvcClassDriverInfo;
+#endif
+#if HAL_USBH_USE_HUB
+extern const usbh_classdriverinfo_t usbhhubClassDriverInfo;
+void _usbhub_port_object_init(usbh_port_t *port, USBHDriver *usbh,
+ USBHHubDriver *hub, uint8_t number);
+#else
+void _usbhub_port_object_init(usbh_port_t *port, USBHDriver *usbh, uint8_t number);
+#endif
+
+void _usbh_port_disconnected(usbh_port_t *port);
+void _usbh_urb_completeI(usbh_urb_t *urb, usbh_urbstatus_t status);
+bool _usbh_urb_abortI(usbh_urb_t *urb, usbh_urbstatus_t status);
+void _usbh_urb_abort_and_waitS(usbh_urb_t *urb, usbh_urbstatus_t status);
+
+
+#define USBH_CLASSIN(type, req, value, index) \
+ (USBH_REQTYPE_IN | type | USBH_REQTYPE_CLASS), \
+ req, \
+ value, \
+ index
+
+#define USBH_CLASSOUT(type, req, value, index) \
+ (USBH_REQTYPE_OUT | type | USBH_REQTYPE_CLASS), \
+ req, \
+ value, \
+ index
+
+#define USBH_STANDARDIN(type, req, value, index) \
+ (USBH_REQTYPE_IN | type | USBH_REQTYPE_STANDARD), \
+ req, \
+ value, \
+ index
+
+#define USBH_STANDARDOUT(type, req, value, index) \
+ (USBH_REQTYPE_OUT | type | USBH_REQTYPE_STANDARD), \
+ req, \
+ value, \
+ index
+
+
+#define USBH_PID_DATA0 0
+#define USBH_PID_DATA2 1
+#define USBH_PID_DATA1 2
+#define USBH_PID_MDATA 3
+#define USBH_PID_SETUP 3
+
+
+/* GetBusState and SetHubDescriptor are optional, omitted */
+#define ClearHubFeature (((USBH_REQTYPE_OUT | USBH_REQTYPE_CLASS | USBH_REQTYPE_DEVICE) << 8) \
+ | USBH_REQ_CLEAR_FEATURE)
+#define SetHubFeature (((USBH_REQTYPE_OUT | USBH_REQTYPE_CLASS | USBH_REQTYPE_DEVICE) << 8) \
+ | USBH_REQ_SET_FEATURE)
+#define ClearPortFeature (((USBH_REQTYPE_OUT | USBH_REQTYPE_CLASS | USBH_REQTYPE_OTHER) << 8) \
+ | USBH_REQ_CLEAR_FEATURE)
+#define SetPortFeature (((USBH_REQTYPE_OUT | USBH_REQTYPE_CLASS | USBH_REQTYPE_OTHER) << 8) \
+ | USBH_REQ_SET_FEATURE)
+#define GetHubDescriptor (((USBH_REQTYPE_IN | USBH_REQTYPE_CLASS | USBH_REQTYPE_DEVICE) << 8) \
+ | USBH_REQ_GET_DESCRIPTOR)
+#define GetHubStatus (((USBH_REQTYPE_IN | USBH_REQTYPE_CLASS | USBH_REQTYPE_DEVICE) << 8) \
+ | USBH_REQ_GET_STATUS)
+#define GetPortStatus (((USBH_REQTYPE_IN | USBH_REQTYPE_CLASS | USBH_REQTYPE_OTHER) << 8) \
+ | USBH_REQ_GET_STATUS)
+
+
+#define USBH_PORTSTATUS_CONNECTION 0x0001
+#define USBH_PORTSTATUS_ENABLE 0x0002
+#define USBH_PORTSTATUS_SUSPEND 0x0004
+#define USBH_PORTSTATUS_OVERCURRENT 0x0008
+#define USBH_PORTSTATUS_RESET 0x0010
+/* bits 5 to 7 are reserved */
+#define USBH_PORTSTATUS_POWER 0x0100
+#define USBH_PORTSTATUS_LOW_SPEED 0x0200
+#define USBH_PORTSTATUS_HIGH_SPEED 0x0400
+#define USBH_PORTSTATUS_TEST 0x0800
+#define USBH_PORTSTATUS_INDICATOR 0x1000
+/* bits 13 to 15 are reserved */
+
+#define USBH_PORTSTATUS_C_CONNECTION 0x0001
+#define USBH_PORTSTATUS_C_ENABLE 0x0002
+#define USBH_PORTSTATUS_C_SUSPEND 0x0004
+#define USBH_PORTSTATUS_C_OVERCURRENT 0x0008
+#define USBH_PORTSTATUS_C_RESET 0x0010
+
+#define USBH_HUBSTATUS_C_HUB_LOCAL_POWER 0x0001
+#define USBH_HUBSTATUS_C_HUB_OVER_CURRENT 0x0002
+
+/*
+ * Port feature numbers
+ * See USB 2.0 spec Table 11-17
+ */
+#define USBH_HUB_FEAT_C_HUB_LOCAL_POWER 0
+#define USBH_HUB_FEAT_C_HUB_OVER_CURRENT 1
+#define USBH_PORT_FEAT_CONNECTION 0
+#define USBH_PORT_FEAT_ENABLE 1
+#define USBH_PORT_FEAT_SUSPEND 2
+#define USBH_PORT_FEAT_OVERCURRENT 3
+#define USBH_PORT_FEAT_RESET 4
+#define USBH_PORT_FEAT_POWER 8
+#define USBH_PORT_FEAT_LOWSPEED 9
+#define USBH_PORT_FEAT_C_CONNECTION 16
+#define USBH_PORT_FEAT_C_ENABLE 17
+#define USBH_PORT_FEAT_C_SUSPEND 18
+#define USBH_PORT_FEAT_C_OVERCURRENT 19
+#define USBH_PORT_FEAT_C_RESET 20
+#define USBH_PORT_FEAT_TEST 21
+#define USBH_PORT_FEAT_INDICATOR 22
+
+#define sizeof_array(x) (sizeof(x)/sizeof(*(x)))
+
+#endif
+
+#endif /* USBH_INTERNAL_H_ */
diff --git a/os/hal/include/usbh/list.h b/os/hal/include/usbh/list.h
new file mode 100644
index 0000000..4eceacd
--- /dev/null
+++ b/os/hal/include/usbh/list.h
@@ -0,0 +1,598 @@
+#ifndef USBH_LIST_H_
+#define USBH_LIST_H_
+
+/* TODO: re-write this file; stolen from linux */
+
+#ifndef offsetof
+#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
+#endif
+
+#define container_of(ptr, type, member) ((type *)(void *)((char *)(ptr) - offsetof(type, member)))
+#ifndef container_of
+#define container_of(ptr, type, member) ({ \
+ const typeof(((type *)0)->member) * __mptr = (ptr); \
+ (type *)((char *)__mptr - offsetof(type, member)); })
+#endif
+
+/*
+ * Simple doubly linked list implementation.
+ *
+ * Some of the internal functions ("__xxx") are useful when
+ * manipulating whole lists rather than single entries, as
+ * sometimes we already know the next/prev entries and we can
+ * generate better code by using them directly rather than
+ * using the generic single-entry routines.
+ */
+struct list_head {
+ struct list_head *next, *prev;
+};
+
+#define LIST_HEAD_INIT(name) { &(name), &(name) }
+
+#define LIST_HEAD(name) \
+ struct list_head name = LIST_HEAD_INIT(name)
+
+static inline void INIT_LIST_HEAD(struct list_head *list)
+{
+ list->next = list;
+ list->prev = list;
+}
+
+/*
+ * Insert a new entry between two known consecutive entries.
+ *
+ * This is only for internal list manipulation where we know
+ * the prev/next entries already!
+ */
+#ifndef CONFIG_DEBUG_LIST
+static inline void __list_add(struct list_head *new,
+ struct list_head *prev,
+ struct list_head *next)
+{
+ next->prev = new;
+ new->next = next;
+ new->prev = prev;
+ prev->next = new;
+}
+#else
+extern void __list_add(struct list_head *new,
+ struct list_head *prev,
+ struct list_head *next);
+#endif
+
+/**
+ * list_add - add a new entry
+ * @new: new entry to be added
+ * @head: list head to add it after
+ *
+ * Insert a new entry after the specified head.
+ * This is good for implementing stacks.
+ */
+static inline void list_add(struct list_head *new, struct list_head *head)
+{
+ __list_add(new, head, head->next);
+}
+
+
+/**
+ * list_add_tail - add a new entry
+ * @new: new entry to be added
+ * @head: list head to add it before
+ *
+ * Insert a new entry before the specified head.
+ * This is useful for implementing queues.
+ */
+static inline void list_add_tail(struct list_head *new, struct list_head *head)
+{
+ __list_add(new, head->prev, head);
+}
+
+/*
+ * Delete a list entry by making the prev/next entries
+ * point to each other.
+ *
+ * This is only for internal list manipulation where we know
+ * the prev/next entries already!
+ */
+static inline void __list_del(struct list_head * prev, struct list_head * next)
+{
+ next->prev = prev;
+ prev->next = next;
+}
+
+/**
+ * list_del - deletes entry from list.
+ * @entry: the element to delete from the list.
+ * Note: list_empty() on entry does not return true after this, the entry is
+ * in an undefined state.
+ */
+#ifndef CONFIG_DEBUG_LIST
+static inline void __list_del_entry(struct list_head *entry)
+{
+ __list_del(entry->prev, entry->next);
+}
+
+static inline void list_del(struct list_head *entry)
+{
+ __list_del(entry->prev, entry->next);
+ // entry->next = LIST_POISON1;
+ // entry->prev = LIST_POISON2;
+}
+#else
+extern void __list_del_entry(struct list_head *entry);
+extern void list_del(struct list_head *entry);
+#endif
+
+/**
+ * list_replace - replace old entry by new one
+ * @old : the element to be replaced
+ * @new : the new element to insert
+ *
+ * If @old was empty, it will be overwritten.
+ */
+static inline void list_replace(struct list_head *old,
+ struct list_head *new)
+{
+ new->next = old->next;
+ new->next->prev = new;
+ new->prev = old->prev;
+ new->prev->next = new;
+}
+
+static inline void list_replace_init(struct list_head *old,
+ struct list_head *new)
+{
+ list_replace(old, new);
+ INIT_LIST_HEAD(old);
+}
+
+/**
+ * list_del_init - deletes entry from list and reinitialize it.
+ * @entry: the element to delete from the list.
+ */
+static inline void list_del_init(struct list_head *entry)
+{
+ __list_del_entry(entry);
+ INIT_LIST_HEAD(entry);
+}
+
+/**
+ * list_move - delete from one list and add as another's head
+ * @list: the entry to move
+ * @head: the head that will precede our entry
+ */
+static inline void list_move(struct list_head *list, struct list_head *head)
+{
+ __list_del_entry(list);
+ list_add(list, head);
+}
+
+/**
+ * list_move_tail - delete from one list and add as another's tail
+ * @list: the entry to move
+ * @head: the head that will follow our entry
+ */
+static inline void list_move_tail(struct list_head *list,
+ struct list_head *head)
+{
+ __list_del_entry(list);
+ list_add_tail(list, head);
+}
+
+/**
+ * list_is_last - tests whether @list is the last entry in list @head
+ * @list: the entry to test
+ * @head: the head of the list
+ */
+static inline int list_is_last(const struct list_head *list,
+ const struct list_head *head)
+{
+ return list->next == head;
+}
+
+/**
+ * list_empty - tests whether a list is empty
+ * @head: the list to test.
+ */
+static inline int list_empty(const struct list_head *head)
+{
+ return head->next == head;
+}
+
+/**
+ * list_empty_careful - tests whether a list is empty and not being modified
+ * @head: the list to test
+ *
+ * Description:
+ * tests whether a list is empty _and_ checks that no other CPU might be
+ * in the process of modifying either member (next or prev)
+ *
+ * NOTE: using list_empty_careful() without synchronization
+ * can only be safe if the only activity that can happen
+ * to the list entry is list_del_init(). Eg. it cannot be used
+ * if another CPU could re-list_add() it.
+ */
+static inline int list_empty_careful(const struct list_head *head)
+{
+ struct list_head *next = head->next;
+ return (next == head) && (next == head->prev);
+}
+
+/**
+ * list_rotate_left - rotate the list to the left
+ * @head: the head of the list
+ */
+static inline void list_rotate_left(struct list_head *head)
+{
+ struct list_head *first;
+
+ if (!list_empty(head)) {
+ first = head->next;
+ list_move_tail(first, head);
+ }
+}
+
+/**
+ * list_is_singular - tests whether a list has just one entry.
+ * @head: the list to test.
+ */
+static inline int list_is_singular(const struct list_head *head)
+{
+ return !list_empty(head) && (head->next == head->prev);
+}
+
+static inline void __list_cut_position(struct list_head *list,
+ struct list_head *head, struct list_head *entry)
+{
+ struct list_head *new_first = entry->next;
+ list->next = head->next;
+ list->next->prev = list;
+ list->prev = entry;
+ entry->next = list;
+ head->next = new_first;
+ new_first->prev = head;
+}
+
+/**
+ * list_cut_position - cut a list into two
+ * @list: a new list to add all removed entries
+ * @head: a list with entries
+ * @entry: an entry within head, could be the head itself
+ * and if so we won't cut the list
+ *
+ * This helper moves the initial part of @head, up to and
+ * including @entry, from @head to @list. You should
+ * pass on @entry an element you know is on @head. @list
+ * should be an empty list or a list you do not care about
+ * losing its data.
+ *
+ */
+static inline void list_cut_position(struct list_head *list,
+ struct list_head *head, struct list_head *entry)
+{
+ if (list_empty(head))
+ return;
+ if (list_is_singular(head) &&
+ (head->next != entry && head != entry))
+ return;
+ if (entry == head)
+ INIT_LIST_HEAD(list);
+ else
+ __list_cut_position(list, head, entry);
+}
+
+static inline void __list_splice(const struct list_head *list,
+ struct list_head *prev,
+ struct list_head *next)
+{
+ struct list_head *first = list->next;
+ struct list_head *last = list->prev;
+
+ first->prev = prev;
+ prev->next = first;
+
+ last->next = next;
+ next->prev = last;
+}
+
+/**
+ * list_splice - join two lists, this is designed for stacks
+ * @list: the new list to add.
+ * @head: the place to add it in the first list.
+ */
+static inline void list_splice(const struct list_head *list,
+ struct list_head *head)
+{
+ if (!list_empty(list))
+ __list_splice(list, head, head->next);
+}
+
+/**
+ * list_splice_tail - join two lists, each list being a queue
+ * @list: the new list to add.
+ * @head: the place to add it in the first list.
+ */
+static inline void list_splice_tail(struct list_head *list,
+ struct list_head *head)
+{
+ if (!list_empty(list))
+ __list_splice(list, head->prev, head);
+}
+
+/**
+ * list_splice_init - join two lists and reinitialise the emptied list.
+ * @list: the new list to add.
+ * @head: the place to add it in the first list.
+ *
+ * The list at @list is reinitialised
+ */
+static inline void list_splice_init(struct list_head *list,
+ struct list_head *head)
+{
+ if (!list_empty(list)) {
+ __list_splice(list, head, head->next);
+ INIT_LIST_HEAD(list);
+ }
+}
+
+/**
+ * list_splice_tail_init - join two lists and reinitialise the emptied list
+ * @list: the new list to add.
+ * @head: the place to add it in the first list.
+ *
+ * Each of the lists is a queue.
+ * The list at @list is reinitialised
+ */
+static inline void list_splice_tail_init(struct list_head *list,
+ struct list_head *head)
+{
+ if (!list_empty(list)) {
+ __list_splice(list, head->prev, head);
+ INIT_LIST_HEAD(list);
+ }
+}
+
+/**
+ * list_entry - get the struct for this entry
+ * @ptr: the &struct list_head pointer.
+ * @type: the type of the struct this is embedded in.
+ * @member: the name of the list_head within the struct.
+ */
+#define list_entry(ptr, type, member) \
+ container_of(ptr, type, member)
+
+/**
+ * list_first_entry - get the first element from a list
+ * @ptr: the list head to take the element from.
+ * @type: the type of the struct this is embedded in.
+ * @member: the name of the list_head within the struct.
+ *
+ * Note, that list is expected to be not empty.
+ */
+#define list_first_entry(ptr, type, member) \
+ list_entry((ptr)->next, type, member)
+
+/**
+ * list_last_entry - get the last element from a list
+ * @ptr: the list head to take the element from.
+ * @type: the type of the struct this is embedded in.
+ * @member: the name of the list_head within the struct.
+ *
+ * Note, that list is expected to be not empty.
+ */
+#define list_last_entry(ptr, type, member) \
+ list_entry((ptr)->prev, type, member)
+
+/**
+ * list_first_entry_or_null - get the first element from a list
+ * @ptr: the list head to take the element from.
+ * @type: the type of the struct this is embedded in.
+ * @member: the name of the list_head within the struct.
+ *
+ * Note that if the list is empty, it returns NULL.
+ */
+#define list_first_entry_or_null(ptr, type, member) \
+ (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
+
+/**
+ * list_next_entry - get the next element in list
+ * @pos: the type * to cursor
+ * @member: the name of the list_head within the struct.
+ */
+#define list_next_entry(pos, type, member) \
+ list_entry((pos)->member.next, type, member)
+
+/**
+ * list_prev_entry - get the prev element in list
+ * @pos: the type * to cursor
+ * @member: the name of the list_head within the struct.
+ */
+#define list_prev_entry(pos, type, member) \
+ list_entry((pos)->member.prev, type, member)
+
+/**
+ * list_for_each - iterate over a list
+ * @pos: the &struct list_head to use as a loop cursor.
+ * @head: the head for your list.
+ */
+#define list_for_each(pos, head) \
+ for (pos = (head)->next; pos != (head); pos = pos->next)
+
+/**
+ * list_for_each_prev - iterate over a list backwards
+ * @pos: the &struct list_head to use as a loop cursor.
+ * @head: the head for your list.
+ */
+#define list_for_each_prev(pos, head) \
+ for (pos = (head)->prev; pos != (head); pos = pos->prev)
+
+/**
+ * list_for_each_safe - iterate over a list safe against removal of list entry
+ * @pos: the &struct list_head to use as a loop cursor.
+ * @n: another &struct list_head to use as temporary storage
+ * @head: the head for your list.
+ */
+#define list_for_each_safe(pos, n, head) \
+ for (pos = (head)->next, n = pos->next; pos != (head); \
+ pos = n, n = pos->next)
+
+/**
+ * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
+ * @pos: the &struct list_head to use as a loop cursor.
+ * @n: another &struct list_head to use as temporary storage
+ * @head: the head for your list.
+ */
+#define list_for_each_prev_safe(pos, n, head) \
+ for (pos = (head)->prev, n = pos->prev; \
+ pos != (head); \
+ pos = n, n = pos->prev)
+
+/**
+ * list_for_each_entry - iterate over list of given type
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ */
+#define list_for_each_entry(pos, type, head, member) \
+ for (pos = list_first_entry(head, type, member); \
+ &pos->member != (head); \
+ pos = list_next_entry(pos, type, member))
+
+/**
+ * list_for_each_entry_reverse - iterate backwards over list of given type.
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ */
+#define list_for_each_entry_reverse(pos, type, head, member) \
+ for (pos = list_last_entry(head, type, member); \
+ &pos->member != (head); \
+ pos = list_prev_entry(pos, type, member))
+
+/**
+ * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
+ * @pos: the type * to use as a start point
+ * @head: the head of the list
+ * @member: the name of the list_head within the struct.
+ *
+ * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
+ */
+#define list_prepare_entry(pos, type, head, member) \
+ ((pos) ? : list_entry(head, type, member))
+
+/**
+ * list_for_each_entry_continue - continue iteration over list of given type
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ *
+ * Continue to iterate over list of given type, continuing after
+ * the current position.
+ */
+#define list_for_each_entry_continue(pos, type, head, member) \
+ for (pos = list_next_entry(pos, type, member); \
+ &pos->member != (head); \
+ pos = list_next_entry(pos, type, member))
+
+/**
+ * list_for_each_entry_continue_reverse - iterate backwards from the given point
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ *
+ * Start to iterate over list of given type backwards, continuing after
+ * the current position.
+ */
+#define list_for_each_entry_continue_reverse(pos, type, head, member) \
+ for (pos = list_prev_entry(pos, type, member); \
+ &pos->member != (head); \
+ pos = list_prev_entry(pos, type, member))
+
+/**
+ * list_for_each_entry_from - iterate over list of given type from the current point
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ *
+ * Iterate over list of given type, continuing from current position.
+ */
+#define list_for_each_entry_from(pos, type, head, member) \
+ for (; &pos->member != (head); \
+ pos = list_next_entry(pos, type, member))
+
+/**
+ * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
+ * @pos: the type * to use as a loop cursor.
+ * @n: another type * to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ */
+#define list_for_each_entry_safe(pos, type, n, head, member) \
+ for (pos = list_first_entry(head, type, member), \
+ n = list_next_entry(pos, type, member); \
+ &pos->member != (head); \
+ pos = n, n = list_next_entry(n, type, member))
+
+/**
+ * list_for_each_entry_safe_continue - continue list iteration safe against removal
+ * @pos: the type * to use as a loop cursor.
+ * @n: another type * to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ *
+ * Iterate over list of given type, continuing after current point,
+ * safe against removal of list entry.
+ */
+#define list_for_each_entry_safe_continue(pos, type, n, head, member) \
+ for (pos = list_next_entry(pos, type, member), \
+ n = list_next_entry(pos, type, member); \
+ &pos->member != (head); \
+ pos = n, n = list_next_entry(n, type, member))
+
+/**
+ * list_for_each_entry_safe_from - iterate over list from current point safe against removal
+ * @pos: the type * to use as a loop cursor.
+ * @n: another type * to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ *
+ * Iterate over list of given type from current point, safe against
+ * removal of list entry.
+ */
+#define list_for_each_entry_safe_from(pos, type, n, head, member) \
+ for (n = list_next_entry(pos, type, member); \
+ &pos->member != (head); \
+ pos = n, n = list_next_entry(n, type, member))
+
+/**
+ * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
+ * @pos: the type * to use as a loop cursor.
+ * @n: another type * to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ *
+ * Iterate backwards over list of given type, safe against removal
+ * of list entry.
+ */
+#define list_for_each_entry_safe_reverse(pos, type, n, head, member) \
+ for (pos = list_last_entry(head, type, member), \
+ n = list_prev_entry(pos, type, member); \
+ &pos->member != (head); \
+ pos = n, n = list_prev_entry(n, type, member))
+
+/**
+ * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
+ * @pos: the loop cursor used in the list_for_each_entry_safe loop
+ * @n: temporary storage used in list_for_each_entry_safe
+ * @member: the name of the list_head within the struct.
+ *
+ * list_safe_reset_next is not safe to use in general if the list may be
+ * modified concurrently (eg. the lock is dropped in the loop body). An
+ * exception to this is if the cursor element (pos) is pinned in the list,
+ * and list_safe_reset_next is called after re-taking the lock and before
+ * completing the current iteration of the loop body.
+ */
+#define list_safe_reset_next(pos, type, n, member) \
+ n = list_next_entry(pos, type, member)
+
+#endif /* USBH_LIST_H_ */