summaryrefslogtreecommitdiff
path: root/tinyusb/class/hid.c
blob: ec2df615641947ef35b8136226c3c66fdec3c9ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * hid.c
 *
 *  Created on: Nov 27, 2012
 *      Author: hathach
 */

/*
 * Software License Agreement (BSD License)
 * Copyright (c) 2012, hathach (tinyusb.net)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 *
 * This file is part of the tinyUSB stack.
 */

#include "hid.h"

#if defined DEVICE_CLASS_HID && defined TUSB_CFG_DEVICE

#ifdef TUSB_CFG_DEVICE_HID_KEYBOARD
USB_HID_KeyboardReport_t hid_keyboard_report;
static volatile bool bKeyChanged = false;
#endif

#ifdef TUSB_CFG_DEVICE_HID_MOUSE
USB_HID_MouseReport_t hid_mouse_report;
static volatile bool bMouseChanged = false;
#endif

/**************************************************************************/
/*!
    @brief Handler for HID_GetReport in the USB ROM driver
*/
/**************************************************************************/
ErrorCode_t HID_GetReport( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuffer, uint16_t* plength)
{
  USB_HID_CTRL_T* pHidCtrl = (USB_HID_CTRL_T*) hHid;

  /* ReportID = SetupPacket.wValue.WB.L; */
  if (pSetup->wValue.WB.H == HID_REPORT_INPUT)
    return (ERR_USBD_STALL);          /* Not Supported */

  switch (pHidCtrl->protocol)
  {
    #ifdef TUSB_CFG_DEVICE_HID_KEYBOARD
      case HID_PROTOCOL_KEYBOARD:
        *pBuffer = (uint8_t*) &hid_keyboard_report;
        *plength = sizeof(USB_HID_KeyboardReport_t);

        if (!bKeyChanged)
        {
          memset(pBuffer, 0, *plength);
        }
        bKeyChanged = false;
      break;
    #endif

    #ifdef TUSB_CFG_DEVICE_HID_MOUSE
      case HID_PROTOCOL_MOUSE:
        *pBuffer = (uint8_t*) &hid_mouse_report;
        *plength = sizeof(USB_HID_MouseReport_t);

        if (!bMouseChanged)
        {
          memset(pBuffer, 0, *plength);
        }
        bMouseChanged = false;
      break;
    #endif

    default:
      break;
  }

  return (LPC_OK);
}

/**************************************************************************/
/*!
    @brief Handler for HIS_SetReport in the USB ROM driver
*/
/**************************************************************************/
ErrorCode_t HID_SetReport( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuffer, uint16_t length)
{
  /* we will reuse standard EP0Buf */
  if (length == 0)
    return LPC_OK;

  /* ReportID = SetupPacket.wValue.WB.L; */
  if (pSetup->wValue.WB.H != HID_REPORT_OUTPUT)
    return (ERR_USBD_STALL);          /* Not Supported */

  return (LPC_OK);
}

/**************************************************************************/
/*!
    @brief HID endpoint in handler for the USB ROM driver
*/
/**************************************************************************/
ErrorCode_t HID_EpIn_Hdlr (USBD_HANDLE_T hUsb, void* data, uint32_t event)
{
  if (USB_EVT_IN == event)
  {
    USB_HID_CTRL_T* pHidCtrl = (USB_HID_CTRL_T*)data;
    switch(pHidCtrl->protocol)
    {
      #ifdef TUSB_CFG_DEVICE_HID_KEYBOARD
        case HID_PROTOCOL_KEYBOARD:
          if (!bKeyChanged)
          {
            memset(&hid_keyboard_report, 0, sizeof(USB_HID_KeyboardReport_t));
          }
          USBD_API->hw->WriteEP(hUsb, pHidCtrl->epin_adr, (uint8_t*) &hid_keyboard_report, sizeof(USB_HID_KeyboardReport_t));
          bKeyChanged = false;
        break;
      #endif

      #ifdef TUSB_CFG_DEVICE_HID_MOUSE
        case HID_PROTOCOL_MOUSE:
          if (!bMouseChanged)
          {
            memset(&hid_mouse_report, 0, sizeof(USB_HID_MouseReport_t));
          }
          USBD_API->hw->WriteEP(hUsb, pHidCtrl->epin_adr, (uint8_t*) &hid_mouse_report, sizeof(USB_HID_MouseReport_t));
          bMouseChanged = false;
        break;
      #endif

      default:
        break;
    }
  }

  return LPC_OK;
}

/**************************************************************************/
/*!
    @brief HID endpoint out handler for the USB ROM driver
*/
/**************************************************************************/
ErrorCode_t HID_EpOut_Hdlr (USBD_HANDLE_T hUsb, void* data, uint32_t event)
{
  if (USB_EVT_OUT == event)
  {
    // not used yet
    // uint8_t outreport[8];
    // USB_HID_CTRL_T* pHidCtrl = (USB_HID_CTRL_T*)data;
    // USBD_API->hw->ReadEP(hUsb, pHidCtrl->epout_adr, outreport);
  }
  return LPC_OK;
}

/**************************************************************************/
/*!
    @brief Initialises USB HID using the ROM based drivers
*/
/**************************************************************************/
TUSB_Error_t tusb_hid_init(USBD_HANDLE_T hUsb, USB_INTERFACE_DESCRIPTOR const *const pIntfDesc, uint8_t const * const pHIDReportDesc, uint32_t ReportDescLength, uint32_t* mem_base, uint32_t* mem_size)
{
  USB_HID_REPORT_T reports_data =
  {
      .desc      = (uint8_t*) pHIDReportDesc,
      .len       = ReportDescLength,
      .idle_time = 0,
  };

  USBD_HID_INIT_PARAM_T hid_param =
  {
      .mem_base       = *mem_base,
      .mem_size       = *mem_size,

      .intf_desc      = (uint8_t*)pIntfDesc,
      .report_data    = &reports_data,
      .max_reports    = 1,

      /* user defined functions */
      .HID_GetReport  = HID_GetReport,
      .HID_SetReport  = HID_SetReport,
      .HID_EpIn_Hdlr  = HID_EpIn_Hdlr,
      .HID_EpOut_Hdlr = HID_EpOut_Hdlr
  };

  ASSERT( (pIntfDesc != NULL) && (pIntfDesc->bInterfaceClass == USB_DEVICE_CLASS_HUMAN_INTERFACE), ERR_FAILED);

  ASSERT( LPC_OK == USBD_API->hid->init(hUsb, &hid_param), tERROR_FAILED );

  /* update memory variables */
  *mem_base += (*mem_size - hid_param.mem_size);
  *mem_size = hid_param.mem_size;

  return tERROR_NONE;
}

/**************************************************************************/
/*!

*/
/**************************************************************************/
TUSB_Error_t tusb_hid_configured(USBD_HANDLE_T hUsb)
{
  #ifdef  TUSB_CFG_DEVICE_HID_KEYBOARD
    USBD_API->hw->WriteEP(hUsb , HID_KEYBOARD_EP_IN , (uint8_t* ) &hid_keyboard_report , sizeof(USB_HID_KeyboardReport_t) ); // initial packet for IN endpoint , will not work if omitted
  #endif

  #ifdef  TUSB_CFG_DEVICE_HID_MOUSE
    USBD_API->hw->WriteEP(hUsb , HID_MOUSE_EP_IN    , (uint8_t* ) &hid_mouse_report    , sizeof(USB_HID_MouseReport_t) ); // initial packet for IN endpoint, will not work if omitted
  #endif

  return tERROR_NONE;
}

#ifdef TUSB_CFG_DEVICE_HID_KEYBOARD
/**************************************************************************/
/*!
    @brief Send the supplied key codes out via HID USB keyboard emulation

    @param[in]  modifier
                KB modifier code bits (see USB_HID_KB_KEYMODIFIER_CODE)
    @param[in]  keycodes
                A buffer containing up to six keycodes
    @param[in]  numkey
                The number of keys to send (max 6)

    @note Note that for HID KBs, letter codes are not case sensitive. To
          create an upper-case letter, you need to include the correct
          KB modifier code(s), for ex: (1 << HID_KEYMODIFIER_LEFTSHIFT)

    @section EXAMPLE

    @code

    // Send an unmodified 'a' character
    if (usb_isConfigured())
    {
      uint8_t keys[6] = {HID_USAGE_KEYBOARD_aA};
      tusb_hid_keyboard_sendKeys(0x00, keys, 1);
    }

    // Send Windows + 'e' (shortcut for 'explorer.exe')
    if (usb_isConfigured())
    {
      uint8_t keys[6] = {HID_USAGE_KEYBOARD_aA + 'e' - 'a'};
      tusb_hid_keyboard_sendKeys((1<<HID_KEYMODIFIER_LEFTGUI), keys, 1);
    }

    @endcode
*/
/**************************************************************************/
TUSB_Error_t tusb_hid_keyboard_sendKeys(uint8_t modifier, uint8_t keycodes[], uint8_t numkey)
{
//  uint32_t start_time = systickGetSecondsActive();
//  while (bKeyChanged) // TODO blocking while previous key has yet sent - can use fifo to improve this
//  {
//    ASSERT_MESSAGE(systickGetSecondsActive() - start_time < 5, ERR_FAILED, "HID Keyboard Timeout");
//  }

  if (bKeyChanged)
  {
    return tERROR_FAILED;
  }

  ASSERT(keycodes && numkey && numkey <=6, ERR_FAILED);

  hid_keyboard_report.Modifier = modifier;
  memset(hid_keyboard_report.KeyCode, 0, 6);
  memcpy(hid_keyboard_report.KeyCode, keycodes, numkey);

  bKeyChanged = true;

  return tERROR_NONE;
}
#endif

#ifdef TUSB_CFG_DEVICE_HID_MOUSE
/**************************************************************************/
/*!
    @brief Send the supplied mouse event out via HID USB mouse emulation

    @param[in]  buttons
                Indicate which button(s) are being pressed (see
                USB_HID_MOUSE_BUTTON_CODE)
    @param[in]  x
                Position adjustment on the X scale
    @param[in]  y
                Position adjustment on the Y scale

    @section EXAMPLE

    @code

    if (usb_isConfigured())
    {
      // Move the mouse +10 in the X direction and + 10 in the Y direction
      tusb_hid_mouse_send(0x00, 10, 10);
    }

    @endcode
*/
/**************************************************************************/
TUSB_Error_t tusb_hid_mouse_send(uint8_t buttons, int8_t x, int8_t y)
{
//  uint32_t start_time = systickGetSecondsActive();
//  while (bMouseChanged) // TODO Block while previous key hasn't been sent - can use fifo to improve this
//  {
//    ASSERT_MESSAGE(systickGetSecondsActive() - start_time < 5, ERR_FAILED, "HID Mouse Timeout");
//  }

  if (bMouseChanged)
  {
    return tERROR_FAILED;
  }

  hid_mouse_report.Button = buttons;
  hid_mouse_report.X = x;
  hid_mouse_report.Y = y;

  bMouseChanged = true;

  return tERROR_NONE;
}
#endif

#endif