summaryrefslogtreecommitdiffstats
path: root/app/keyboard.c
blob: cecbd582caa7a89c78a0b7ca71535f69a4d6190b (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
#include "project.h"

#define KEY_LIST_LEN	6

static const uint8_t keyboard_report_descriptor[] = {
  0x05, 0x01,                   /* Usage Page (Generic Desktop) */
  0x09, 0x06,                   /* Usage (Keyboard) */
  0xA1, 0x01,                   /* Collection (Application) */
  0x05, 0x07,                   /*   Usage page (Key Codes) */
  0x19, 0xE0,                   /*     Usage minimum (224) */
  0x29, 0xE7,                   /*     Usage maximum (231) */
  0x15, 0x00,                   /*     Logical minimum (0) */
  0x25, 0x01,                   /*     Logical maximum (1) */
  0x75, 0x01,                   /*     Report size (1) */
  0x95, 0x08,                   /*     Report count (8) */
  0x81, 0x02,                   /*     Input (data, variable, absolute) */
  0x95, 0x01,                   /*     Report count (1) */
  0x75, 0x08,                   /*     Report size (8) */
  0x81, 0x01,                   /*     Input (constant) */
  0x95, 0x06,                   /*     Report count (6) */
  0x75, 0x08,                   /*     Report size (8) */
  0x15, 0x00,                   /*     Logical minimum (0) */
  //0x25, 0x65,                   /*     Logical maximum (101) */
  0x26, 0xff, 0x00,             /*     Logical maximum (255) */
  //0x26, 0x7f, 0x00,             /*     Logical maximum (255) */
  0x05, 0x07,                   /*   Usage page (key codes) */
  0x19, 0x00,                   /*     Usage minimum (0) */
  0x2A, 0xff, 0x00,             /*     Usage maximum (255) */
//  0x29, 0x65,                   /*     Usage maximum (101) */
//  0x2A, 0xff, 0x03,             /*     Usage maximum (1023) */
  0x81, 0x00,                   /*   Input (data, array) */

  0x05, 0x08,                   /* Usage page (leds) */
  0x75, 0x01,                   /*     Report size (1) */
  0x95, 0x03,                   /*     Report count (3) */
  0x19, 0x01,                   /*     Usage minimum (1) */
  0x29, 0x03,                   /*     Usage maximum (3) */
  0x91, 0x02,                   /*     Output (data, variable, absolute ) */
  0x75, 0x05,                   /*     Report size (5) */
  0x95, 0x01,                   /*     Report count (1) */
  0x91, 0x01,                   /*     Output (constant ) */

  0x05, 0x01,                   /*   USAGE_PAGE (Generic Desktop)      */
  0x09, 0x80,                   /*   USAGE (System Control)            */

  0xA1, 0x01,                   /*   COLLECTION (Application)          */
  0x75, 0x02,                   /*   REPORT_SIZE (2)                   */
  0x95, 0x01,                   /*   REPORT_COUNT (1)                  */
  0x15, 0x01,                   /*   LOGICAL_MIN (1)                   */
  0x25, 0x03,                   /*   LOGICAL_MAX (3)                   */
  0x09, 0x82,                   /*   USAGE (System Sleep)              */
  0x09, 0x81,                   /*   USAGE (System Power Down)         */
  0x09, 0x83,                   /*   USAGE (System Wake Up)            */
  0x81, 0x60,                   /*   INPUT (Data Ary Abs NPrf Null)    */
  0x75, 0x06,                   /*   REPORT_SIZE (6)                   */
  0x81, 0x03,                   /*   INPUT (Cnst Var Abs)              */
  0xc0,                         /*   END COLLECTION                    */


  0xC0                          /* End Collection */
};


static const struct
{
  struct usb_hid_descriptor hid_descriptor;
  struct
  {
    uint8_t bReportDescriptorType;
    uint16_t wDescriptorLength;
  } __attribute__ ((packed)) hid_report;
} __attribute__ ((packed)) keyboard_function =
{
  .hid_descriptor =
  {
  .bLength = sizeof (keyboard_function),.bDescriptorType =
      USB_DT_HID,.bcdHID = 0x0100,.bCountryCode = 0,.bNumDescriptors = 1,}
  ,.hid_report =
  {
  .bReportDescriptorType = USB_DT_REPORT,.wDescriptorLength =
      sizeof (keyboard_report_descriptor),}
,};

const struct usb_endpoint_descriptor keyboard_endpoint = {
  .bLength = USB_DT_ENDPOINT_SIZE,
  .bDescriptorType = USB_DT_ENDPOINT,
  .bEndpointAddress = KEYBOARD_EP,
  .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT,
  .wMaxPacketSize = 9,
  .bInterval = 0x1              //0x20,
};

const struct usb_interface_descriptor keyboard_iface = {
  .bLength = USB_DT_INTERFACE_SIZE,
  .bDescriptorType = USB_DT_INTERFACE,
  .bInterfaceNumber = 0,
  .bAlternateSetting = 0,
  .bNumEndpoints = 1,
  .bInterfaceClass = USB_CLASS_HID,
  .bInterfaceSubClass = 1,      /* boot */
  .bInterfaceProtocol = 1,      /* keyboard */
  .iInterface = 5,

  .endpoint = &keyboard_endpoint,

  .extra = &keyboard_function,
  .extralen = sizeof (keyboard_function),
};


void
keyboard_get_descriptor (uint8_t ** buf, uint16_t * len)
{
  /* Handle the HID report descriptor. */
  *buf = (uint8_t *) keyboard_report_descriptor;
  *len = sizeof (keyboard_report_descriptor);
}



static void
keyboard_send (uint8_t modifiers, uint8_t * key_list)
{
  /*Last byte is the power wakeup stuff that we don't yet support */
  uint8_t buf[KEYBOARD_EP_TXN_SIZE] =
    { modifiers, 0, key_list[0], key_list[1], key_list[2], key_list[3],
key_list[4], key_list[5], 0 };

  usbd_ep_write_packet (usbd_dev, KEYBOARD_EP, buf, sizeof (buf));
}

void
keyboard_dispatch (int sc, int updown)
{
  static uint8_t modifiers;
  static uint8_t key_list[KEY_LIST_LEN];
  int i;

/*Windows needs the modifiers spliting out, so we do this as per our
 * descriptor, others need them in the list so we do that as well */

  switch (sc)
    {
    case USB_K_SC_LEFTCTRL:
    case USB_K_SC_LEFTSHIFT:
    case USB_K_SC_LEFTALT:
    case USB_K_SC_LEFTMETA:
    case USB_K_SC_RIGHTCTRL:
    case USB_K_SC_RIGHTSHIFT:
    case USB_K_SC_RIGHTALT:
    case USB_K_SC_RIGHTMETA:
      if (updown)
        modifiers |= 1 << (sc - USB_K_SC_LEFTCTRL);
      else
        modifiers &= ~(1 << (sc - USB_K_SC_LEFTCTRL));
    }


/* Some oses are picky and need these not to move about  so we make 
 * a list of the down keys */

  if (updown)
    {
      for (i = 0; i < KEY_LIST_LEN; ++i)
        {
          if ((key_list[i] == 0) || (key_list[i] == sc))
            {
              key_list[i] = sc;
              break;
            }
        }
    }
  else
    {
      for (i = 0; i < KEY_LIST_LEN; ++i)
        {
          if (key_list[i] == sc)
            {
              key_list[i] = 0;
              break;
            }
        }
    }


#ifdef DEBUG
  printf ("KBD> %02x %02x %02x %02x %02x %02x %02x\r\n", modifiers,
          key_list[0], key_list[1], key_list[2], key_list[3], key_list[4],
          key_list[5]);
#endif

  keyboard_send (modifiers, key_list);


}

int
keyboard_control_request (usbd_device * usbd_dev, struct usb_setup_data *req,
                          uint8_t ** buf, uint16_t * len,
                          void (**complete) (usbd_device * usbd_dev,
                                             struct usb_setup_data * req))
{
  uint8_t at_leds = 0;

  if (req->bmRequestType != (USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE))
    return 0;
  if (req->bRequest != USB_REQ_SET_CONFIGURATION)
    return 0;
  if (req->wValue != 0x200)
    return 0;
  if (req->wIndex)
    return 0;
  if (!len)
    return 0;
  if (*len != 1)
    return 0;
  if (!buf || !*buf)
    return 0;


  if (**buf & USB_LED_CAPS)
    at_leds |= AT_LED_CAPS;
  if (**buf & USB_LED_NUMLOCK)
    at_leds |= AT_LED_NUMLOCK;
  if (**buf & USB_LED_SCROLLLOCK)
    at_leds |= AT_LED_SCROLLLOCK;


#ifdef DEBUG
  printf ("LEDS> %x\r\n", at_leds);
#endif

  atkbd_set_leds (at_leds);

  return 1;
}