2013-03-28 08:26:02 +01:00
|
|
|
/* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
|
|
|
|
|
|
|
|
This software may be distributed and modified under the terms of the GNU
|
|
|
|
General Public License version 2 (GPL2) as published by the Free Software
|
|
|
|
Foundation and appearing in the file GPL2.TXT included in the packaging of
|
|
|
|
this file. Please note that GPL2 Section 2[b] requires that all works based
|
|
|
|
on this software must also be made publicly available under the terms of
|
|
|
|
the GPL2 ("Copyleft").
|
|
|
|
|
|
|
|
Contact information
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
Circuits At Home, LTD
|
|
|
|
Web : http://www.circuitsathome.com
|
|
|
|
e-mail : support@circuitsathome.com
|
|
|
|
*/
|
|
|
|
#if !defined(__HIDBOOT_H__)
|
|
|
|
#define __HIDBOOT_H__
|
|
|
|
|
|
|
|
#include "hid.h"
|
|
|
|
|
2013-12-05 03:11:25 +01:00
|
|
|
#define UHS_HID_BOOT_KEY_ZERO 0x27
|
|
|
|
#define UHS_HID_BOOT_KEY_ENTER 0x28
|
|
|
|
#define UHS_HID_BOOT_KEY_SPACE 0x2c
|
|
|
|
#define UHS_HID_BOOT_KEY_CAPS_LOCK 0x39
|
|
|
|
#define UHS_HID_BOOT_KEY_SCROLL_LOCK 0x47
|
|
|
|
#define UHS_HID_BOOT_KEY_NUM_LOCK 0x53
|
|
|
|
#define UHS_HID_BOOT_KEY_ZERO2 0x62
|
|
|
|
#define UHS_HID_BOOT_KEY_PERIOD 0x63
|
2013-12-05 00:40:23 +01:00
|
|
|
|
2013-12-25 07:08:02 +01:00
|
|
|
// Don't worry, GCC will optimize the result to a final value.
|
|
|
|
#define bitsEndpoints(p) ((((p) & HID_PROTOCOL_KEYBOARD)? 2 : 0) | (((p) & HID_PROTOCOL_MOUSE)? 1 : 0))
|
|
|
|
#define totalEndpoints(p) ((bitsEndpoints(p) == 3) ? 3 : 2)
|
|
|
|
#define epMUL(p) ((((p) & HID_PROTOCOL_KEYBOARD)? 1 : 0) + (((p) & HID_PROTOCOL_MOUSE)? 1 : 0))
|
|
|
|
|
|
|
|
// Already defined in hid.h
|
|
|
|
// #define HID_MAX_HID_CLASS_DESCRIPTORS 5
|
|
|
|
|
2013-03-28 08:26:02 +01:00
|
|
|
struct MOUSEINFO {
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint8_t bmLeftButton : 1;
|
|
|
|
uint8_t bmRightButton : 1;
|
|
|
|
uint8_t bmMiddleButton : 1;
|
2013-12-25 11:24:59 +01:00
|
|
|
uint8_t bmDummy : 5;
|
2013-03-28 08:26:02 +01:00
|
|
|
};
|
|
|
|
int8_t dX;
|
|
|
|
int8_t dY;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MouseReportParser : public HIDReportParser {
|
|
|
|
|
|
|
|
union {
|
|
|
|
MOUSEINFO mouseInfo;
|
2013-12-25 10:49:01 +01:00
|
|
|
uint8_t bInfo[sizeof (MOUSEINFO)];
|
2013-03-28 08:26:02 +01:00
|
|
|
} prevState;
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual void OnMouseMove(MOUSEINFO *mi) {
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual void OnLeftButtonUp(MOUSEINFO *mi) {
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual void OnLeftButtonDown(MOUSEINFO *mi) {
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual void OnRightButtonUp(MOUSEINFO *mi) {
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual void OnRightButtonDown(MOUSEINFO *mi) {
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual void OnMiddleButtonUp(MOUSEINFO *mi) {
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual void OnMiddleButtonDown(MOUSEINFO *mi) {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MODIFIERKEYS {
|
|
|
|
uint8_t bmLeftCtrl : 1;
|
|
|
|
uint8_t bmLeftShift : 1;
|
|
|
|
uint8_t bmLeftAlt : 1;
|
|
|
|
uint8_t bmLeftGUI : 1;
|
|
|
|
uint8_t bmRightCtrl : 1;
|
|
|
|
uint8_t bmRightShift : 1;
|
|
|
|
uint8_t bmRightAlt : 1;
|
|
|
|
uint8_t bmRightGUI : 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct KBDINFO {
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint8_t bmLeftCtrl : 1;
|
|
|
|
uint8_t bmLeftShift : 1;
|
|
|
|
uint8_t bmLeftAlt : 1;
|
|
|
|
uint8_t bmLeftGUI : 1;
|
|
|
|
uint8_t bmRightCtrl : 1;
|
|
|
|
uint8_t bmRightShift : 1;
|
|
|
|
uint8_t bmRightAlt : 1;
|
|
|
|
uint8_t bmRightGUI : 1;
|
|
|
|
};
|
|
|
|
uint8_t bReserved;
|
|
|
|
uint8_t Keys[6];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct KBDLEDS {
|
|
|
|
uint8_t bmNumLock : 1;
|
|
|
|
uint8_t bmCapsLock : 1;
|
|
|
|
uint8_t bmScrollLock : 1;
|
|
|
|
uint8_t bmCompose : 1;
|
|
|
|
uint8_t bmKana : 1;
|
|
|
|
uint8_t bmReserved : 3;
|
|
|
|
};
|
|
|
|
|
|
|
|
class KeyboardReportParser : public HIDReportParser {
|
2013-12-05 19:57:50 +01:00
|
|
|
static const uint8_t numKeys[10];
|
|
|
|
static const uint8_t symKeysUp[12];
|
|
|
|
static const uint8_t symKeysLo[12];
|
|
|
|
static const uint8_t padKeys[5];
|
2013-03-28 08:26:02 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
union {
|
|
|
|
KBDINFO kbdInfo;
|
2013-12-25 10:49:01 +01:00
|
|
|
uint8_t bInfo[sizeof (KBDINFO)];
|
2013-03-28 08:26:02 +01:00
|
|
|
} prevState;
|
|
|
|
|
|
|
|
union {
|
|
|
|
KBDLEDS kbdLeds;
|
|
|
|
uint8_t bLeds;
|
|
|
|
} kbdLockingKeys;
|
|
|
|
|
|
|
|
uint8_t OemToAscii(uint8_t mod, uint8_t key);
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
KeyboardReportParser() {
|
|
|
|
kbdLockingKeys.bLeds = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
|
|
|
|
|
|
|
|
protected:
|
2013-05-02 13:34:29 +02:00
|
|
|
virtual uint8_t HandleLockingKeys(HID* hid, uint8_t key);
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2013-09-29 22:08:20 +02:00
|
|
|
virtual void OnControlKeysChanged(uint8_t before, uint8_t after) {
|
|
|
|
};
|
2013-09-03 05:01:59 +02:00
|
|
|
|
2013-09-29 22:08:20 +02:00
|
|
|
virtual void OnKeyDown(uint8_t mod, uint8_t key) {
|
2013-03-28 08:26:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
virtual void OnKeyUp(uint8_t mod, uint8_t key) {
|
|
|
|
};
|
2013-12-05 19:57:50 +01:00
|
|
|
|
|
|
|
virtual const uint8_t *getNumKeys() {
|
|
|
|
return numKeys;
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual const uint8_t *getSymKeysUp() {
|
|
|
|
return symKeysUp;
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual const uint8_t *getSymKeysLo() {
|
|
|
|
return symKeysLo;
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual const uint8_t *getPadKeys() {
|
|
|
|
return padKeys;
|
|
|
|
};
|
2013-03-28 08:26:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <const uint8_t BOOT_PROTOCOL>
|
|
|
|
class HIDBoot : public HID //public USBDeviceConfig, public UsbConfigXtracter
|
|
|
|
{
|
2013-12-19 03:58:24 +01:00
|
|
|
EpInfo epInfo[totalEndpoints(BOOT_PROTOCOL)];
|
|
|
|
HIDReportParser *pRptParser[epMUL(BOOT_PROTOCOL)];
|
2013-03-28 08:26:02 +01:00
|
|
|
|
|
|
|
uint8_t bConfNum; // configuration number
|
|
|
|
uint8_t bIfaceNum; // Interface Number
|
|
|
|
uint8_t bNumIface; // number of interfaces in the configuration
|
|
|
|
uint8_t bNumEP; // total number of EP in the configuration
|
|
|
|
uint32_t qNextPollTime; // next poll time
|
|
|
|
bool bPollEnable; // poll enable flag
|
2013-12-23 20:12:29 +01:00
|
|
|
uint8_t bInterval; // largest interval
|
2013-03-28 08:26:02 +01:00
|
|
|
|
|
|
|
void Initialize();
|
|
|
|
|
|
|
|
virtual HIDReportParser* GetReportParser(uint8_t id) {
|
2013-09-29 22:08:20 +02:00
|
|
|
return pRptParser[id];
|
2013-03-28 08:26:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
HIDBoot(USB *p);
|
|
|
|
|
|
|
|
virtual bool SetReportParser(uint8_t id, HIDReportParser *prs) {
|
2013-09-29 22:08:20 +02:00
|
|
|
pRptParser[id] = prs;
|
2013-03-28 08:26:02 +01:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// USBDeviceConfig implementation
|
|
|
|
virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
|
|
|
|
virtual uint8_t Release();
|
|
|
|
virtual uint8_t Poll();
|
|
|
|
|
|
|
|
virtual uint8_t GetAddress() {
|
|
|
|
return bAddress;
|
|
|
|
};
|
|
|
|
|
|
|
|
// UsbConfigXtracter implementation
|
|
|
|
virtual void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <const uint8_t BOOT_PROTOCOL>
|
|
|
|
HIDBoot<BOOT_PROTOCOL>::HIDBoot(USB *p) :
|
|
|
|
HID(p),
|
|
|
|
qNextPollTime(0),
|
2013-09-29 22:08:20 +02:00
|
|
|
bPollEnable(false) {
|
2013-03-28 08:26:02 +01:00
|
|
|
Initialize();
|
|
|
|
|
2013-12-19 03:58:24 +01:00
|
|
|
for(int i = 0; i < epMUL(BOOT_PROTOCOL); i++) {
|
2013-09-29 22:08:20 +02:00
|
|
|
pRptParser[i] = NULL;
|
|
|
|
}
|
2013-03-28 09:46:43 +01:00
|
|
|
if(pUsb)
|
2013-03-28 08:26:02 +01:00
|
|
|
pUsb->RegisterDeviceClass(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <const uint8_t BOOT_PROTOCOL>
|
|
|
|
void HIDBoot<BOOT_PROTOCOL>::Initialize() {
|
2013-12-19 03:58:24 +01:00
|
|
|
for(int i = 0; i < totalEndpoints(BOOT_PROTOCOL); i++) {
|
2013-03-28 08:26:02 +01:00
|
|
|
epInfo[i].epAddr = 0;
|
|
|
|
epInfo[i].maxPktSize = (i) ? 0 : 8;
|
|
|
|
epInfo[i].epAttribs = 0;
|
|
|
|
epInfo[i].bmNakPower = (i) ? USB_NAK_NOWAIT : USB_NAK_MAX_POWER;
|
|
|
|
}
|
|
|
|
bNumEP = 1;
|
|
|
|
bNumIface = 0;
|
|
|
|
bConfNum = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <const uint8_t BOOT_PROTOCOL>
|
|
|
|
uint8_t HIDBoot<BOOT_PROTOCOL>::Init(uint8_t parent, uint8_t port, bool lowspeed) {
|
2013-12-25 10:49:01 +01:00
|
|
|
const uint8_t constBufSize = sizeof (USB_DEVICE_DESCRIPTOR);
|
2013-03-28 08:26:02 +01:00
|
|
|
|
|
|
|
uint8_t buf[constBufSize];
|
|
|
|
uint8_t rcode;
|
|
|
|
UsbDevice *p = NULL;
|
|
|
|
EpInfo *oldep_ptr = NULL;
|
|
|
|
uint8_t len = 0;
|
2013-09-29 22:08:20 +02:00
|
|
|
//uint16_t cd_len = 0;
|
2013-03-28 08:26:02 +01:00
|
|
|
|
|
|
|
uint8_t num_of_conf; // number of configurations
|
2013-09-29 22:08:20 +02:00
|
|
|
//uint8_t num_of_intf; // number of interfaces
|
2013-03-28 08:26:02 +01:00
|
|
|
|
|
|
|
AddressPool &addrPool = pUsb->GetAddressPool();
|
|
|
|
|
|
|
|
USBTRACE("BM Init\r\n");
|
2013-12-23 20:12:29 +01:00
|
|
|
//USBTRACE2("totalEndpoints:", (uint8_t) (totalEndpoints(BOOT_PROTOCOL)));
|
|
|
|
//USBTRACE2("epMUL:", epMUL(BOOT_PROTOCOL));
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
if(bAddress)
|
2013-03-28 08:26:02 +01:00
|
|
|
return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
|
|
|
|
|
2013-12-23 20:12:29 +01:00
|
|
|
bInterval = 0;
|
2013-03-28 08:26:02 +01:00
|
|
|
// Get pointer to pseudo device with address 0 assigned
|
|
|
|
p = addrPool.GetUsbDevicePtr(0);
|
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
if(!p)
|
2013-03-28 08:26:02 +01:00
|
|
|
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
|
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
if(!p->epinfo) {
|
2013-03-28 08:26:02 +01:00
|
|
|
USBTRACE("epinfo\r\n");
|
|
|
|
return USB_ERROR_EPINFO_IS_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save old pointer to EP_RECORD of address 0
|
|
|
|
oldep_ptr = p->epinfo;
|
|
|
|
|
|
|
|
// Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
|
|
|
|
p->epinfo = epInfo;
|
|
|
|
|
|
|
|
p->lowspeed = lowspeed;
|
|
|
|
|
|
|
|
// Get device descriptor
|
2013-12-25 10:49:01 +01:00
|
|
|
rcode = pUsb->getDevDescr(0, 0, 8, (uint8_t*)buf);
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
if(!rcode)
|
2013-03-28 08:26:02 +01:00
|
|
|
len = (buf[0] > constBufSize) ? constBufSize : buf[0];
|
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
if(rcode) {
|
2013-03-28 08:26:02 +01:00
|
|
|
// Restore p->epinfo
|
|
|
|
p->epinfo = oldep_ptr;
|
|
|
|
|
|
|
|
goto FailGetDevDescr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore p->epinfo
|
|
|
|
p->epinfo = oldep_ptr;
|
|
|
|
|
|
|
|
// Allocate new address according to device class
|
|
|
|
bAddress = addrPool.AllocAddress(parent, false, port);
|
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
if(!bAddress)
|
2013-03-28 08:26:02 +01:00
|
|
|
return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL;
|
|
|
|
|
|
|
|
// Extract Max Packet Size from the device descriptor
|
2013-12-25 10:49:01 +01:00
|
|
|
epInfo[0].maxPktSize = (uint8_t)((USB_DEVICE_DESCRIPTOR*)buf)->bMaxPacketSize0;
|
2013-03-28 08:26:02 +01:00
|
|
|
|
|
|
|
// Assign new address to the device
|
|
|
|
rcode = pUsb->setAddr(0, 0, bAddress);
|
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
if(rcode) {
|
2013-03-28 08:26:02 +01:00
|
|
|
p->lowspeed = false;
|
|
|
|
addrPool.FreeAddress(bAddress);
|
|
|
|
bAddress = 0;
|
|
|
|
USBTRACE2("setAddr:", rcode);
|
|
|
|
return rcode;
|
|
|
|
}
|
2013-12-19 04:20:50 +01:00
|
|
|
//delay(2); //per USB 2.0 sect.9.2.6.3
|
2013-03-28 08:26:02 +01:00
|
|
|
|
|
|
|
USBTRACE2("Addr:", bAddress);
|
|
|
|
|
|
|
|
p->lowspeed = false;
|
|
|
|
|
|
|
|
p = addrPool.GetUsbDevicePtr(bAddress);
|
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
if(!p)
|
2013-03-28 08:26:02 +01:00
|
|
|
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
|
|
|
|
|
|
|
|
p->lowspeed = lowspeed;
|
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
if(len)
|
2013-12-25 10:49:01 +01:00
|
|
|
rcode = pUsb->getDevDescr(bAddress, 0, len, (uint8_t*)buf);
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
if(rcode)
|
2013-03-28 08:26:02 +01:00
|
|
|
goto FailGetDevDescr;
|
|
|
|
|
2013-12-25 10:49:01 +01:00
|
|
|
num_of_conf = ((USB_DEVICE_DESCRIPTOR*)buf)->bNumConfigurations;
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2013-12-23 20:12:29 +01:00
|
|
|
USBTRACE2("NC:", num_of_conf);
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2013-09-29 22:08:20 +02:00
|
|
|
// GCC will optimize unused stuff away.
|
2013-12-25 07:08:02 +01:00
|
|
|
if((BOOT_PROTOCOL & (HID_PROTOCOL_KEYBOARD | HID_PROTOCOL_MOUSE)) == (HID_PROTOCOL_KEYBOARD | HID_PROTOCOL_MOUSE)) {
|
|
|
|
USBTRACE("HID_PROTOCOL_KEYBOARD AND MOUSE\r\n");
|
|
|
|
ConfigDescParser<
|
|
|
|
USB_CLASS_HID,
|
|
|
|
HID_BOOT_INTF_SUBCLASS,
|
|
|
|
HID_PROTOCOL_KEYBOARD | HID_PROTOCOL_MOUSE,
|
|
|
|
CP_MASK_COMPARE_ALL > confDescrParser(this);
|
|
|
|
confDescrParser.SetOR(); // Use the OR variant.
|
2013-09-29 22:08:20 +02:00
|
|
|
for(uint8_t i = 0; i < num_of_conf; i++) {
|
2013-12-25 07:08:02 +01:00
|
|
|
pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
|
2013-12-25 10:49:01 +01:00
|
|
|
if(bNumEP == (uint8_t)(totalEndpoints(BOOT_PROTOCOL)))
|
2013-12-19 03:58:24 +01:00
|
|
|
break;
|
2013-09-29 22:08:20 +02:00
|
|
|
}
|
2013-12-25 07:08:02 +01:00
|
|
|
} else {
|
|
|
|
// GCC will optimize unused stuff away.
|
|
|
|
if(BOOT_PROTOCOL & HID_PROTOCOL_KEYBOARD) {
|
|
|
|
USBTRACE("HID_PROTOCOL_KEYBOARD\r\n");
|
|
|
|
for(uint8_t i = 0; i < num_of_conf; i++) {
|
|
|
|
ConfigDescParser<
|
|
|
|
USB_CLASS_HID,
|
|
|
|
HID_BOOT_INTF_SUBCLASS,
|
|
|
|
HID_PROTOCOL_KEYBOARD,
|
|
|
|
CP_MASK_COMPARE_ALL> confDescrParserA(this);
|
|
|
|
|
|
|
|
pUsb->getConfDescr(bAddress, 0, i, &confDescrParserA);
|
2013-12-25 10:49:01 +01:00
|
|
|
if(bNumEP == (uint8_t)(totalEndpoints(BOOT_PROTOCOL)))
|
2013-12-25 07:08:02 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-12-19 03:58:24 +01:00
|
|
|
|
2013-12-25 07:08:02 +01:00
|
|
|
// GCC will optimize unused stuff away.
|
|
|
|
if(BOOT_PROTOCOL & HID_PROTOCOL_MOUSE) {
|
|
|
|
USBTRACE("HID_PROTOCOL_MOUSE\r\n");
|
|
|
|
for(uint8_t i = 0; i < num_of_conf; i++) {
|
|
|
|
ConfigDescParser<
|
|
|
|
USB_CLASS_HID,
|
|
|
|
HID_BOOT_INTF_SUBCLASS,
|
|
|
|
HID_PROTOCOL_MOUSE,
|
|
|
|
CP_MASK_COMPARE_ALL> confDescrParserB(this);
|
|
|
|
|
|
|
|
pUsb->getConfDescr(bAddress, 0, i, &confDescrParserB);
|
2013-12-25 10:49:01 +01:00
|
|
|
if(bNumEP == ((uint8_t)(totalEndpoints(BOOT_PROTOCOL))))
|
2013-12-25 07:08:02 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
2013-09-29 22:08:20 +02:00
|
|
|
}
|
|
|
|
}
|
2013-12-18 21:18:28 +01:00
|
|
|
USBTRACE2("bNumEP:", bNumEP);
|
2013-12-19 03:58:24 +01:00
|
|
|
|
2013-12-25 10:49:01 +01:00
|
|
|
if(bNumEP != (uint8_t)(totalEndpoints(BOOT_PROTOCOL))) {
|
2013-12-23 20:12:29 +01:00
|
|
|
rcode = USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
|
|
|
|
goto Fail;
|
|
|
|
}
|
2013-03-28 08:26:02 +01:00
|
|
|
|
|
|
|
// Assign epInfo to epinfo pointer
|
|
|
|
rcode = pUsb->setEpInfoEntry(bAddress, bNumEP, epInfo);
|
2013-12-23 20:12:29 +01:00
|
|
|
//USBTRACE2("setEpInfoEntry returned ", rcode);
|
2013-12-18 21:18:28 +01:00
|
|
|
USBTRACE2("Cnf:", bConfNum);
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2013-12-19 03:58:24 +01:00
|
|
|
delay(1000);
|
|
|
|
|
2013-03-28 08:26:02 +01:00
|
|
|
// Set Configuration Value
|
|
|
|
rcode = pUsb->setConf(bAddress, 0, bConfNum);
|
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
if(rcode)
|
2013-03-28 08:26:02 +01:00
|
|
|
goto FailSetConfDescr;
|
|
|
|
|
2013-12-19 03:58:24 +01:00
|
|
|
delay(1000);
|
|
|
|
|
2013-12-23 20:12:29 +01:00
|
|
|
USBTRACE2("bIfaceNum:", bIfaceNum);
|
|
|
|
USBTRACE2("bNumIface:", bNumIface);
|
|
|
|
|
|
|
|
// Yes, mouse wants SetProtocol and SetIdle too!
|
|
|
|
for(uint8_t i = 0; i < epMUL(BOOT_PROTOCOL); i++) {
|
|
|
|
USBTRACE2("\r\nInterface:", i);
|
|
|
|
rcode = SetProtocol(i, HID_BOOT_PROTOCOL);
|
|
|
|
if(rcode) goto FailSetProtocol;
|
|
|
|
USBTRACE2("PROTOCOL SET HID_BOOT rcode:", rcode);
|
|
|
|
rcode = SetIdle(i, 0, 0);
|
|
|
|
USBTRACE2("SET_IDLE rcode:", rcode);
|
2013-12-25 10:49:01 +01:00
|
|
|
// if(rcode) goto FailSetIdle; This can fail.
|
2013-12-25 07:08:02 +01:00
|
|
|
// Get the RPIPE and just throw it away.
|
|
|
|
SinkParser<USBReadParser, uint16_t, uint16_t> sink;
|
|
|
|
rcode = GetReportDescr(i, &sink);
|
|
|
|
USBTRACE2("RPIPE rcode:", rcode);
|
2013-12-23 20:12:29 +01:00
|
|
|
}
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2013-12-25 07:08:02 +01:00
|
|
|
// Get RPIPE and throw it away.
|
|
|
|
|
2013-09-29 22:08:20 +02:00
|
|
|
if(BOOT_PROTOCOL & HID_PROTOCOL_KEYBOARD) {
|
2013-12-25 07:08:02 +01:00
|
|
|
// Wake keyboard interface by twinkling up to 5 LEDs that are in the spec.
|
|
|
|
// kana, compose, scroll, caps, num
|
|
|
|
rcode = 0x20; // Reuse rcode.
|
2013-12-23 20:12:29 +01:00
|
|
|
while(rcode) {
|
|
|
|
rcode >>= 1;
|
2013-12-25 07:08:02 +01:00
|
|
|
// Ignore any error returned, we don't care if LED is not supported
|
2013-12-23 20:12:29 +01:00
|
|
|
SetReport(0, 0, 2, 0, 1, &rcode); // Eventually becomes zero (All off)
|
|
|
|
delay(25);
|
|
|
|
}
|
2013-03-28 08:26:02 +01:00
|
|
|
}
|
|
|
|
USBTRACE("BM configured\r\n");
|
|
|
|
|
|
|
|
bPollEnable = true;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
FailGetDevDescr:
|
2013-06-12 05:11:43 +02:00
|
|
|
#ifdef DEBUG_USB_HOST
|
2013-03-30 15:29:16 +01:00
|
|
|
NotifyFailGetDevDescr();
|
2013-03-28 08:26:02 +01:00
|
|
|
goto Fail;
|
2013-05-16 22:43:51 +02:00
|
|
|
#endif
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2013-12-19 03:58:24 +01:00
|
|
|
//FailSetDevTblEntry:
|
|
|
|
//#ifdef DEBUG_USB_HOST
|
|
|
|
// NotifyFailSetDevTblEntry();
|
|
|
|
// goto Fail;
|
|
|
|
//#endif
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2013-12-19 03:58:24 +01:00
|
|
|
//FailGetConfDescr:
|
|
|
|
//#ifdef DEBUG_USB_HOST
|
|
|
|
// NotifyFailGetConfDescr();
|
|
|
|
// goto Fail;
|
|
|
|
//#endif
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2013-03-30 15:29:16 +01:00
|
|
|
FailSetConfDescr:
|
2013-06-12 05:11:43 +02:00
|
|
|
#ifdef DEBUG_USB_HOST
|
2013-03-30 15:29:16 +01:00
|
|
|
NotifyFailSetConfDescr();
|
|
|
|
goto Fail;
|
2013-05-16 22:43:51 +02:00
|
|
|
#endif
|
2013-03-30 15:29:16 +01:00
|
|
|
|
2013-03-28 08:26:02 +01:00
|
|
|
FailSetProtocol:
|
2013-06-12 05:11:43 +02:00
|
|
|
#ifdef DEBUG_USB_HOST
|
2013-03-28 08:26:02 +01:00
|
|
|
USBTRACE("SetProto:");
|
|
|
|
goto Fail;
|
2013-05-16 22:43:51 +02:00
|
|
|
#endif
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2013-12-25 10:49:01 +01:00
|
|
|
//FailSetIdle:
|
|
|
|
//#ifdef DEBUG_USB_HOST
|
|
|
|
// USBTRACE("SetIdle:");
|
|
|
|
//#endif
|
2013-03-28 08:26:02 +01:00
|
|
|
|
|
|
|
Fail:
|
2013-06-12 05:11:43 +02:00
|
|
|
#ifdef DEBUG_USB_HOST
|
2013-03-30 15:29:16 +01:00
|
|
|
NotifyFail(rcode);
|
2013-05-16 22:43:51 +02:00
|
|
|
#endif
|
2013-03-28 08:26:02 +01:00
|
|
|
Release();
|
2013-12-19 03:58:24 +01:00
|
|
|
|
2013-03-28 08:26:02 +01:00
|
|
|
return rcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <const uint8_t BOOT_PROTOCOL>
|
|
|
|
void HIDBoot<BOOT_PROTOCOL>::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *pep) {
|
2013-12-23 20:12:29 +01:00
|
|
|
|
2013-09-29 22:08:20 +02:00
|
|
|
// If the first configuration satisfies, the others are not considered.
|
2013-12-25 07:08:02 +01:00
|
|
|
//if(bNumEP > 1 && conf != bConfNum)
|
|
|
|
if(bNumEP == totalEndpoints(BOOT_PROTOCOL))
|
2013-03-28 08:26:02 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
bConfNum = conf;
|
|
|
|
bIfaceNum = iface;
|
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
if((pep->bmAttributes & 0x03) == 3 && (pep->bEndpointAddress & 0x80) == 0x80) {
|
2013-12-23 20:12:29 +01:00
|
|
|
if(pep->bInterval > bInterval) bInterval = pep->bInterval;
|
2013-03-28 08:26:02 +01:00
|
|
|
|
|
|
|
// Fill in the endpoint info structure
|
2013-12-23 20:12:29 +01:00
|
|
|
epInfo[bNumEP].epAddr = (pep->bEndpointAddress & 0x0F);
|
2013-12-25 10:49:01 +01:00
|
|
|
epInfo[bNumEP].maxPktSize = (uint8_t)pep->wMaxPacketSize;
|
2013-12-23 20:12:29 +01:00
|
|
|
epInfo[bNumEP].epAttribs = 0;
|
|
|
|
epInfo[bNumEP].bmNakPower = USB_NAK_NOWAIT;
|
2013-03-28 08:26:02 +01:00
|
|
|
bNumEP++;
|
2013-09-29 22:08:20 +02:00
|
|
|
|
2013-03-28 08:26:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <const uint8_t BOOT_PROTOCOL>
|
|
|
|
uint8_t HIDBoot<BOOT_PROTOCOL>::Release() {
|
|
|
|
pUsb->GetAddressPool().FreeAddress(bAddress);
|
|
|
|
|
|
|
|
bConfNum = 0;
|
|
|
|
bIfaceNum = 0;
|
|
|
|
bNumEP = 1;
|
|
|
|
bAddress = 0;
|
|
|
|
qNextPollTime = 0;
|
|
|
|
bPollEnable = false;
|
2013-12-19 03:58:24 +01:00
|
|
|
|
2013-03-28 08:26:02 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <const uint8_t BOOT_PROTOCOL>
|
|
|
|
uint8_t HIDBoot<BOOT_PROTOCOL>::Poll() {
|
|
|
|
uint8_t rcode = 0;
|
|
|
|
|
2014-05-23 05:36:33 +02:00
|
|
|
if(bPollEnable && ((long)(millis() - qNextPollTime) >= 0L)) {
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2013-12-25 10:49:01 +01:00
|
|
|
// To-do: optimize manually, using the for loop only if needed.
|
2013-12-19 03:58:24 +01:00
|
|
|
for(int i = 0; i < epMUL(BOOT_PROTOCOL); i++) {
|
2013-12-25 10:49:01 +01:00
|
|
|
const uint16_t const_buff_len = 16;
|
2013-09-29 22:08:20 +02:00
|
|
|
uint8_t buf[const_buff_len];
|
|
|
|
|
2013-12-18 21:18:28 +01:00
|
|
|
USBTRACE3("(hidboot.h) i=", i, 0x81);
|
|
|
|
USBTRACE3("(hidboot.h) epInfo[epInterruptInIndex + i].epAddr=", epInfo[epInterruptInIndex + i].epAddr, 0x81);
|
|
|
|
USBTRACE3("(hidboot.h) epInfo[epInterruptInIndex + i].maxPktSize=", epInfo[epInterruptInIndex + i].maxPktSize, 0x81);
|
2013-12-25 10:49:01 +01:00
|
|
|
uint16_t read = (uint16_t)epInfo[epInterruptInIndex + i].maxPktSize;
|
2013-09-29 22:08:20 +02:00
|
|
|
|
2013-12-25 10:49:01 +01:00
|
|
|
rcode = pUsb->inTransfer(bAddress, epInfo[epInterruptInIndex + i].epAddr, &read, buf);
|
|
|
|
// SOME buggy dongles report extra keys (like sleep) using a 2 byte packet on the wrong endpoint.
|
|
|
|
// Since keyboard and mice must report at least 3 bytes, we ignore the extra data.
|
|
|
|
if(!rcode && read > 2) {
|
2013-09-29 22:08:20 +02:00
|
|
|
if(pRptParser[i])
|
2013-12-25 10:49:01 +01:00
|
|
|
pRptParser[i]->Parse((HID*)this, 0, (uint8_t)read, buf);
|
|
|
|
#ifdef DEBUG_USB_HOST
|
|
|
|
// We really don't care about errors and anomalies unless we are debugging.
|
|
|
|
} else {
|
|
|
|
if(rcode != hrNAK) {
|
|
|
|
USBTRACE3("(hidboot.h) Poll:", rcode, 0x81);
|
|
|
|
}
|
|
|
|
if(!rcode && read) {
|
|
|
|
USBTRACE3("(hidboot.h) Strange read count: ", read, 0x80);
|
|
|
|
USBTRACE3("(hidboot.h) Interface:", i, 0x80);
|
|
|
|
}
|
|
|
|
}
|
2013-09-29 22:08:20 +02:00
|
|
|
|
2013-12-25 10:49:01 +01:00
|
|
|
if(!rcode && read && (UsbDEBUGlvl > 0x7f)) {
|
2013-12-19 03:58:24 +01:00
|
|
|
for(uint8_t i = 0; i < read; i++) {
|
2013-12-01 17:06:32 +01:00
|
|
|
PrintHex<uint8_t > (buf[i], 0x80);
|
2013-12-25 10:49:01 +01:00
|
|
|
USBTRACE1(" ", 0x80);
|
2013-12-01 17:06:32 +01:00
|
|
|
}
|
2013-12-19 03:58:24 +01:00
|
|
|
if(read)
|
2013-12-25 10:49:01 +01:00
|
|
|
USBTRACE1("\r\n", 0x80);
|
2013-12-01 17:06:32 +01:00
|
|
|
#endif
|
2013-09-29 22:08:20 +02:00
|
|
|
}
|
2013-12-25 10:49:01 +01:00
|
|
|
|
2013-03-28 08:26:02 +01:00
|
|
|
}
|
2013-12-23 20:12:29 +01:00
|
|
|
qNextPollTime = millis() + bInterval;
|
2013-03-28 08:26:02 +01:00
|
|
|
}
|
|
|
|
return rcode;
|
|
|
|
}
|
|
|
|
|
2013-09-29 22:08:20 +02:00
|
|
|
#endif // __HIDBOOTMOUSE_H__
|