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

/* Define this to include the DFU APP interface. */
const struct usb_device_descriptor dev = {
  .bLength = USB_DT_DEVICE_SIZE,
  .bDescriptorType = USB_DT_DEVICE,
  .bcdUSB = 0x0200,
  .bDeviceClass = 0,
  .bDeviceSubClass = 0,
  .bDeviceProtocol = 0,
  .bMaxPacketSize0 = 64,
  .idVendor = 0x1d6b,
  .idProduct = ID_PRODUCT,
  .bcdDevice = 0x0200,
  .iManufacturer = 1,
  .iProduct = 2,
  .iSerialNumber = 3,
  .bNumConfigurations = 1,
};

const struct usb_interface ifaces[] = {
  {
   .num_altsetting = 1,
   .altsetting = &keyboard_iface,
   },
  {
   .num_altsetting = 1,
   .altsetting = &consumer_iface,
   },
  {
   .num_altsetting = 1,
   .altsetting = &tablet_iface,
   },
  {
   .num_altsetting = 1,
   .altsetting = &mouse_iface,
   },
#ifdef INCLUDE_DFU_INTERFACE
  {
   .num_altsetting = 1,
   .altsetting = &dfu_iface,
   },
#endif
};

const struct usb_config_descriptor config = {
  .bLength = USB_DT_CONFIGURATION_SIZE,
  .bDescriptorType = USB_DT_CONFIGURATION,
  .wTotalLength = 0,
#ifdef INCLUDE_DFU_INTERFACE
  .bNumInterfaces = 5,
#else
  .bNumInterfaces = 4,
#endif
  .bConfigurationValue = 1,
  .iConfiguration = 4,
  .bmAttributes = 0xa0,
  .bMaxPower = 0x31,

  .interface = ifaces,
};

static const char *usb_strings[] = {
  "Cabbages are good for you",
  "fish",
  "soup",
  "kvm composite",
  "kvm keyboard",
  "kvm consumer",
  "kvm tablet",
  "kvm mouse",
  "kvm dfu device",
};



usbd_device *usbd_dev;


int usb_is_suspended = 0;

static void
usb_suspended (void)
{
  *USB_CNTR_REG |= USB_CNTR_FSUSP;

  usb_is_suspended = 1;
}


static void
usb_resumed (void)
{

  *USB_CNTR_REG &= ~USB_CNTR_FSUSP;

  usb_is_suspended = 0;
}

int
usb_wakeup_host (void)
{
  if (!usb_is_suspended)
    return 1;


  *USB_CNTR_REG |= USB_CNTR_RESUME;

  delay_us (1000);
  delay_us (1000);
  delay_us (1000);
  delay_us (1000);
  delay_us (1000);

  *USB_CNTR_REG &= ~USB_CNTR_RESUME;

  return 0;
}


static int
usb_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))
{

  (void) complete;
  (void) usbd_dev;

  if ((req->bmRequestType != 0x81) ||
      (req->bRequest != USB_REQ_GET_DESCRIPTOR) || (req->wValue != 0x2200))
    return 0;

  switch (req->wIndex)
    {
    case 0:
      keyboard_get_descriptor (buf, len);
      return 1;
    case 1:
      consumer_get_descriptor (buf, len);
      return 1;
    case 2:
      tablet_get_descriptor (buf, len);
      return 1;
    case 3:
      mouse_get_descriptor (buf, len);
      return 1;
    }

  *len = 0;
  return 0;
}


void
usb_set_config (usbd_device * usbd_dev, uint16_t wValue)
{
  (void) wValue;
  (void) usbd_dev;

  usbd_ep_setup (usbd_dev, 0x81, USB_ENDPOINT_ATTR_INTERRUPT, 4, NULL);
  usbd_ep_setup (usbd_dev, 0x82, USB_ENDPOINT_ATTR_INTERRUPT, 4, NULL);
  usbd_ep_setup (usbd_dev, 0x83, USB_ENDPOINT_ATTR_INTERRUPT, 4, NULL);
  usbd_ep_setup (usbd_dev, 0x84, USB_ENDPOINT_ATTR_INTERRUPT, 4, NULL);

  usbd_register_control_callback (usbd_dev,
                                  USB_REQ_TYPE_STANDARD |
                                  USB_REQ_TYPE_INTERFACE,
                                  USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
                                  usb_control_request);

#ifdef INCLUDE_DFU_INTERFACE
  usbd_register_control_callback (usbd_dev,
                                  USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
                                  USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
                                  dfu_control_request);
#endif


}

/* Buffer to be used for control requests. */
static uint8_t usbd_control_buffer[128];

void
usb_init (void)
{

  usbd_dev =
    usbd_init (&stm32f103_usb_driver, &dev, &config, usb_strings, 8,
               usbd_control_buffer, sizeof (usbd_control_buffer));

  usbd_register_set_config_callback (usbd_dev, usb_set_config);
  usbd_register_suspend_callback (usbd_dev, usb_suspended);
  usbd_register_resume_callback (usbd_dev, usb_resumed);

}

void
usb_run (void)
{
  for (;;)
    usbd_poll (usbd_dev);
}