Add HID class driver

This commit is contained in:
Diego Ismirlian 2017-06-05 10:53:59 -03:00
parent 0260fd851a
commit 5bc62c93e0
7 changed files with 473 additions and 0 deletions

View File

@ -13,6 +13,7 @@ HALSRC += ${CHIBIOS_CONTRIB}/os/hal/src/hal_community.c \
${CHIBIOS_CONTRIB}/os/hal/src/usbh/hal_usbh_msd.c \
${CHIBIOS_CONTRIB}/os/hal/src/usbh/hal_usbh_ftdi.c \
${CHIBIOS_CONTRIB}/os/hal/src/usbh/hal_usbh_aoa.c \
${CHIBIOS_CONTRIB}/os/hal/src/usbh/hal_usbh_hid.c \
${CHIBIOS_CONTRIB}/os/hal/src/usbh/hal_usbh_uvc.c \
${CHIBIOS_CONTRIB}/os/hal/src/hal_ee24xx.c \
${CHIBIOS_CONTRIB}/os/hal/src/hal_ee25xx.c \

View File

@ -44,6 +44,10 @@
#define HAL_USBH_USE_AOA FALSE
#endif
#ifndef HAL_USBH_USE_HID
#define HAL_USBH_USE_HID FALSE
#endif
#define HAL_USBH_USE_IAD HAL_USBH_USE_UVC
#if (HAL_USE_USBH == TRUE) || defined(__DOXYGEN__)

View File

@ -0,0 +1,136 @@
/*
ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
Copyright (C) 2015..2016 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_HID_H_
#define USBH_HID_H_
#include "hal_usbh.h"
#if HAL_USE_USBH && HAL_USBH_USE_HID
/* TODO:
*
*/
/*===========================================================================*/
/* Driver pre-compile time settings. */
/*===========================================================================*/
#if !defined(HAL_USBHHID_USE_INTERRUPT_OUT)
#define HAL_USBHHID_USE_INTERRUPT_OUT FALSE
#endif
/*===========================================================================*/
/* Derived constants and error checks. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver data structures and types. */
/*===========================================================================*/
typedef enum {
USBHHID_STATE_UNINIT = 0,
USBHHID_STATE_STOP = 1,
USBHHID_STATE_ACTIVE = 2,
USBHHID_STATE_READY = 3
} usbhhid_state_t;
typedef enum {
USBHHID_DEVTYPE_GENERIC = 0,
USBHHID_DEVTYPE_BOOT_KEYBOARD = 1,
USBHHID_DEVTYPE_BOOT_MOUSE = 2,
} usbhhid_devtype_t;
typedef enum {
USBHHID_REPORTTYPE_INPUT = 1,
USBHHID_REPORTTYPE_OUTPUT = 2,
USBHHID_REPORTTYPE_FEATURE = 3,
} usbhhid_reporttype_t;
typedef struct USBHHIDDriver USBHHIDDriver;
typedef struct USBHHIDConfig USBHHIDConfig;
typedef void (*usbhhid_report_callback)(USBHHIDDriver *hidp);
struct USBHHIDConfig {
usbhhid_report_callback cb_report;
void *report_buffer;
};
struct USBHHIDDriver {
/* inherited from abstract class driver */
_usbh_base_classdriver_data
usbh_ep_t epin;
#if HAL_USBHHID_USE_INTERRUPT_OUT
usbh_ep_t epout;
#endif
uint8_t ifnum;
usbhhid_devtype_t type;
usbhhid_state_t state;
usbh_urb_t in_urb;
const USBHHIDConfig *config;
};
/*===========================================================================*/
/* Driver macros. */
/*===========================================================================*/
#define usbhhidGetState(hidp) ((hidp)->state)
/*===========================================================================*/
/* External declarations. */
/*===========================================================================*/
extern USBHHIDDriver USBHHID[HAL_USBHHID_MAX_INSTANCES];
#ifdef __cplusplus
extern "C" {
#endif
/* HID Driver */
void usbhHIDObjectInit(USBHHIDDriver *hidp);
/* HID Common API */
usbh_urbstatus_t usbhhidGetReport(USBHHIDDriver *hidp,
uint8_t report_id, usbhhid_reporttype_t report_type,
void *data, uint16_t len);
usbh_urbstatus_t usbhhidSetReport(USBHHIDDriver *hidp,
uint8_t report_id, usbhhid_reporttype_t report_type,
const void *data, uint16_t len);
usbh_urbstatus_t usbhhidGetIdle(USBHHIDDriver *hidp, uint8_t report_id, uint8_t *duration);
usbh_urbstatus_t usbhhidSetIdle(USBHHIDDriver *hidp, uint8_t report_id, uint8_t duration);
usbh_urbstatus_t usbhhidGetProtocol(USBHHIDDriver *hidp, uint8_t *protocol);
usbh_urbstatus_t usbhhidSetProtocol(USBHHIDDriver *hidp, uint8_t protocol);
static inline uint8_t usbhhidGetType(USBHHIDDriver *hidp) {
return hidp->type;
}
/* global initializer */
void usbhhidInit(void);
#ifdef __cplusplus
}
#endif
#endif
#endif /* USBH_HID_H_ */

View File

@ -35,6 +35,9 @@ extern const usbh_classdriverinfo_t usbhaoaClassDriverInfo;
#if HAL_USBH_USE_MSD
extern const usbh_classdriverinfo_t usbhmsdClassDriverInfo;
#endif
#if HAL_USBH_USE_HID
extern const usbh_classdriverinfo_t usbhhidClassDriverInfo;
#endif
#if HAL_USBH_USE_UVC
extern const usbh_classdriverinfo_t usbhuvcClassDriverInfo;
#endif

View File

@ -27,6 +27,7 @@
#include "usbh/dev/aoa.h"
#include "usbh/dev/ftdi.h"
#include "usbh/dev/msd.h"
#include "usbh/dev/hid.h"
#if USBH_DEBUG_ENABLE_TRACE
#define udbgf(f, ...) usbDbgPrintf(f, ##__VA_ARGS__)
@ -121,6 +122,9 @@ void usbhInit(void) {
#if HAL_USBH_USE_MSD
usbhmsdInit();
#endif
#if HAL_USBH_USE_HID
usbhhidInit();
#endif
#if HAL_USBH_USE_HUB
usbhhubInit();
#endif
@ -1306,6 +1310,9 @@ static const usbh_classdriverinfo_t *usbh_classdrivers_lookup[] = {
#if HAL_USBH_USE_MSD
&usbhmsdClassDriverInfo,
#endif
#if HAL_USBH_USE_HID
&usbhhidClassDriverInfo,
#endif
#if HAL_USBH_USE_HUB
&usbhhubClassDriverInfo,
#endif

View File

@ -0,0 +1,311 @@
/*
ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
Copyright (C) 2015..2016 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.
*/
#include "hal.h"
#if HAL_USBH_USE_HID
#if !HAL_USE_USBH
#error "USBHHID needs USBH"
#endif
#include <string.h>
#include "usbh/dev/hid.h"
#include "usbh/internal.h"
#if USBHHID_DEBUG_ENABLE_TRACE
#define udbgf(f, ...) usbDbgPrintf(f, ##__VA_ARGS__)
#define udbg(f, ...) usbDbgPuts(f, ##__VA_ARGS__)
#else
#define udbgf(f, ...) do {} while(0)
#define udbg(f, ...) do {} while(0)
#endif
#if USBHHID_DEBUG_ENABLE_INFO
#define uinfof(f, ...) usbDbgPrintf(f, ##__VA_ARGS__)
#define uinfo(f, ...) usbDbgPuts(f, ##__VA_ARGS__)
#else
#define uinfof(f, ...) do {} while(0)
#define uinfo(f, ...) do {} while(0)
#endif
#if USBHHID_DEBUG_ENABLE_WARNINGS
#define uwarnf(f, ...) usbDbgPrintf(f, ##__VA_ARGS__)
#define uwarn(f, ...) usbDbgPuts(f, ##__VA_ARGS__)
#else
#define uwarnf(f, ...) do {} while(0)
#define uwarn(f, ...) do {} while(0)
#endif
#if USBHHID_DEBUG_ENABLE_ERRORS
#define uerrf(f, ...) usbDbgPrintf(f, ##__VA_ARGS__)
#define uerr(f, ...) usbDbgPuts(f, ##__VA_ARGS__)
#else
#define uerrf(f, ...) do {} while(0)
#define uerr(f, ...) do {} while(0)
#endif
#define USBH_HID_REQ_GET_REPORT 0x01
#define USBH_HID_REQ_GET_IDLE 0x02
#define USBH_HID_REQ_GET_PROTOCOL 0x03
#define USBH_HID_REQ_SET_REPORT 0x09
#define USBH_HID_REQ_SET_IDLE 0x0A
#define USBH_HID_REQ_SET_PROTOCOL 0x0B
/*===========================================================================*/
/* USB Class driver loader for MSD */
/*===========================================================================*/
USBHHIDDriver USBHHID[HAL_USBHHID_MAX_INSTANCES];
static usbh_baseclassdriver_t *_hid_load(usbh_device_t *dev, const uint8_t *descriptor, uint16_t rem);
static void _hid_unload(usbh_baseclassdriver_t *drv);
static const usbh_classdriver_vmt_t class_driver_vmt = {
_hid_load,
_hid_unload
};
const usbh_classdriverinfo_t usbhhidClassDriverInfo = {
0x03, -1, -1, "HID", &class_driver_vmt
};
static usbh_baseclassdriver_t *_hid_load(usbh_device_t *dev, const uint8_t *descriptor, uint16_t rem) {
int i;
USBHHIDDriver *hidp;
if ((rem < descriptor[0]) || (descriptor[1] != USBH_DT_INTERFACE))
return NULL;
const usbh_interface_descriptor_t * const ifdesc = (const usbh_interface_descriptor_t *)descriptor;
if ((ifdesc->bAlternateSetting != 0)
|| (ifdesc->bNumEndpoints < 1)) {
return NULL;
}
/* alloc driver */
for (i = 0; i < HAL_USBHHID_MAX_INSTANCES; i++) {
if (USBHHID[i].dev == NULL) {
hidp = &USBHHID[i];
goto alloc_ok;
}
}
uwarn("Can't alloc HID driver");
/* can't alloc */
return NULL;
alloc_ok:
/* initialize the driver's variables */
hidp->epin.status = USBH_EPSTATUS_UNINITIALIZED;
#if HAL_USBHHID_USE_INTERRUPT_OUT
hidp->epout.status = USBH_EPSTATUS_UNINITIALIZED;
#endif
hidp->ifnum = ifdesc->bInterfaceNumber;
usbhEPSetName(&dev->ctrl, "HID[CTRL]");
/* parse the configuration descriptor */
if_iterator_t iif;
generic_iterator_t iep;
iif.iad = 0;
iif.curr = descriptor;
iif.rem = rem;
for (ep_iter_init(&iep, &iif); iep.valid; ep_iter_next(&iep)) {
const usbh_endpoint_descriptor_t *const epdesc = ep_get(&iep);
if ((epdesc->bEndpointAddress & 0x80) && (epdesc->bmAttributes == USBH_EPTYPE_INT)) {
uinfof("INT IN endpoint found: bEndpointAddress=%02x", epdesc->bEndpointAddress);
usbhEPObjectInit(&hidp->epin, dev, epdesc);
usbhEPSetName(&hidp->epin, "HID[IIN ]");
#if HAL_USBHHID_USE_INTERRUPT_OUT
} else if (((epdesc->bEndpointAddress & 0x80) == 0)
&& (epdesc->bmAttributes == USBH_EPTYPE_INT)) {
uinfof("INT OUT endpoint found: bEndpointAddress=%02x", epdesc->bEndpointAddress);
usbhEPObjectInit(&hidp->epout, dev, epdesc);
usbhEPSetName(&hidp->epout, "HID[IOUT]");
#endif
} else {
uinfof("unsupported endpoint found: bEndpointAddress=%02x, bmAttributes=%02x",
epdesc->bEndpointAddress, epdesc->bmAttributes);
}
}
if (hidp->epin.status != USBH_EPSTATUS_CLOSED) {
goto deinit;
}
if (ifdesc->bInterfaceSubClass != 0x01) {
hidp->type = USBHHID_DEVTYPE_GENERIC;
uinfof("HID: bInterfaceSubClass=%02x, generic HID");
if (ifdesc->bInterfaceSubClass != 0x00) {
uinfof("HID: bInterfaceSubClass=%02x is an invalid bInterfaceSubClass value");
}
} else if (ifdesc->bInterfaceProtocol == 0x01) {
hidp->type = USBHHID_DEVTYPE_BOOT_KEYBOARD;
uinfo("HID: BOOT protocol keyboard found");
} else if (ifdesc->bInterfaceProtocol == 0x02) {
hidp->type = USBHHID_DEVTYPE_BOOT_MOUSE;
uinfo("HID: BOOT protocol mouse found");
} else {
uerrf("HID: bInterfaceProtocol=%02x is an invalid boot protocol, abort");
goto deinit;
}
hidp->state = USBHHID_STATE_ACTIVE;
return (usbh_baseclassdriver_t *)hidp;
deinit:
/* Here, the enpoints are closed, and the driver is unlinked */
return NULL;
}
static void _hid_unload(usbh_baseclassdriver_t *drv) {
(void)drv;
}
static void _in_cb(usbh_urb_t *urb) {
USBHHIDDriver *const hidp = (USBHHIDDriver *)urb->userData;
switch (urb->status) {
case USBH_URBSTATUS_OK:
break;
case USBH_URBSTATUS_DISCONNECTED:
uwarn("HID: URB IN disconnected");
return;
default:
uerrf("HID: URB IN status unexpected = %d", urb->status);
break;
}
usbhURBObjectResetI(&hidp->in_urb);
usbhURBSubmitI(&hidp->in_urb);
}
void usbhhidStart(USBHHIDDriver *hidp, const USBHHIDConfig *cfg) {
osalDbgCheck(hidp && cfg && cfg->report_buffer);
osalDbgCheck((hidp->state == USBHHID_STATE_ACTIVE)
|| (hidp->state == USBHHID_STATE_READY));
if (hidp->state == USBHHID_STATE_READY)
return;
hidp->config = cfg;
/* init the URBs */
usbhURBObjectInit(&hidp->in_urb, &hidp->epin, _in_cb, hidp, hidp->config->report_buffer, 64);
/* open the int IN/OUT endpoints */
usbhEPOpen(&hidp->epin);
#if HAL_USBHHID_USE_INTERRUPT_OUT
if (hidp->epout.status == USBH_EPSTATUS_CLOSED) {
usbhEPOpen(&hidp->epout);
}
#endif
osalSysLock();
usbhURBSubmitI(&hidp->in_urb);
osalSysUnlock();
hidp->state = USBHHID_STATE_READY;
}
void usbhhidStop(USBHHIDDriver *hidp) {
osalDbgCheck((hidp->state == USBHHID_STATE_ACTIVE)
|| (hidp->state == USBHHID_STATE_READY));
if (hidp->state != USBHHID_STATE_READY)
return;
osalSysLock();
usbhEPCloseS(&hidp->epin);
#if HAL_USBHHID_USE_INTERRUPT_OUT
if (hidp->epout.status != USBH_EPSTATUS_UNINITIALIZED) {
usbhEPCloseS(&hidp->epout);
}
#endif
hidp->state = USBHHID_STATE_ACTIVE;
osalSysUnlock();
}
usbh_urbstatus_t usbhhidGetReport(USBHHIDDriver *hidp,
uint8_t report_id, usbhhid_reporttype_t report_type,
void *data, uint16_t len) {
osalDbgCheck(hidp);
osalDbgAssert((uint8_t)report_type <= USBHHID_REPORTTYPE_FEATURE, "wrong report type");
return usbhControlRequest(hidp->dev,
USBH_CLASSIN(USBH_REQTYPE_INTERFACE, USBH_HID_REQ_GET_REPORT, ((uint8_t)report_type << 8) | report_id, hidp->ifnum),
len, data);
}
usbh_urbstatus_t usbhhidSetReport(USBHHIDDriver *hidp,
uint8_t report_id, usbhhid_reporttype_t report_type,
const void *data, uint16_t len) {
osalDbgCheck(hidp);
osalDbgAssert((uint8_t)report_type <= USBHHID_REPORTTYPE_FEATURE, "wrong report type");
return usbhControlRequest(hidp->dev,
USBH_CLASSOUT(USBH_REQTYPE_INTERFACE, USBH_HID_REQ_SET_REPORT, ((uint8_t)report_type << 8) | report_id, hidp->ifnum),
len, (void *)data);
}
usbh_urbstatus_t usbhhidGetIdle(USBHHIDDriver *hidp, uint8_t report_id, uint8_t *duration) {
osalDbgCheck(hidp);
return usbhControlRequest(hidp->dev,
USBH_CLASSIN(USBH_REQTYPE_INTERFACE, USBH_HID_REQ_GET_IDLE, report_id, hidp->ifnum),
1, duration);
}
usbh_urbstatus_t usbhhidSetIdle(USBHHIDDriver *hidp, uint8_t report_id, uint8_t duration) {
osalDbgCheck(hidp);
return usbhControlRequest(hidp->dev,
USBH_CLASSOUT(USBH_REQTYPE_INTERFACE, USBH_HID_REQ_SET_IDLE, (duration << 8) | report_id, hidp->ifnum),
0, NULL);
}
usbh_urbstatus_t usbhhidGetProtocol(USBHHIDDriver *hidp, uint8_t *protocol) {
osalDbgCheck(hidp);
return usbhControlRequest(hidp->dev,
USBH_CLASSIN(USBH_REQTYPE_INTERFACE, USBH_HID_REQ_GET_PROTOCOL, 0, hidp->ifnum),
1, protocol);
}
usbh_urbstatus_t usbhhidSetProtocol(USBHHIDDriver *hidp, uint8_t protocol) {
osalDbgCheck(hidp);
osalDbgAssert(protocol <= 1, "invalid protocol");
return usbhControlRequest(hidp->dev,
USBH_CLASSOUT(USBH_REQTYPE_INTERFACE, USBH_HID_REQ_SET_PROTOCOL, protocol, hidp->ifnum),
0, NULL);
}
void usbhhidObjectInit(USBHHIDDriver *hidp) {
osalDbgCheck(hidp != NULL);
memset(hidp, 0, sizeof(*hidp));
hidp->info = &usbhhidClassDriverInfo;
hidp->state = USBHHID_STATE_STOP;
}
void usbhhidInit(void) {
uint8_t i;
for (i = 0; i < HAL_USBHHID_MAX_INSTANCES; i++) {
usbhhidObjectInit(&USBHHID[i]);
}
}
#endif

View File

@ -130,6 +130,12 @@
#define HAL_USBHAOA_DEFAULT_SERIAL NULL
#define HAL_USBHAOA_DEFAULT_AUDIO_MODE USBHAOA_AUDIO_MODE_DISABLED
/* HID */
#define HAL_USBH_USE_HID TRUE
#define HAL_USBHHID_MAX_INSTANCES 2
#define HAL_USBHHID_USE_INTERRUPT_OUT FALSE
/* HUB */
#define HAL_USBH_USE_HUB TRUE
@ -178,6 +184,11 @@
#define USBHAOA_DEBUG_ENABLE_WARNINGS TRUE
#define USBHAOA_DEBUG_ENABLE_ERRORS TRUE
#define USBHHID_DEBUG_ENABLE_TRACE TRUE
#define USBHHID_DEBUG_ENABLE_INFO TRUE
#define USBHHID_DEBUG_ENABLE_WARNINGS TRUE
#define USBHHID_DEBUG_ENABLE_ERRORS TRUE
/*===========================================================================*/
/* FSMCNAND driver related settings. */
/*===========================================================================*/