aboutsummaryrefslogtreecommitdiffstats
path: root/src/usb.c
blob: 710899166cf12ca7f153c818ba7c0630c299c036 (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
// Main code for handling USB controllers and devices.
//
// Copyright (C) 2009  Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU LGPLv3 license.

#include "util.h" // dprintf
#include "pci.h" // foreachpci
#include "config.h" // CONFIG_*
#include "pci_regs.h" // PCI_CLASS_REVISION
#include "pci_ids.h" // PCI_CLASS_SERIAL_USB_UHCI
#include "usb-uhci.h" // uhci_init
#include "usb-ohci.h" // ohci_init
#include "usb-hid.h" // usb_keyboard_setup
#include "usb.h" // struct usb_s
#include "biosvar.h" // GET_GLOBAL

struct usb_s USBControllers[16] VAR16VISIBLE;

static int
send_control(u32 endp, int dir, const void *cmd, int cmdsize
             , void *data, int datasize)
{
    struct usb_s *cntl = endp2cntl(endp);
    switch (cntl->type) {
    default:
    case USB_TYPE_UHCI:
        return uhci_control(endp, dir, cmd, cmdsize, data, datasize);
    case USB_TYPE_OHCI:
        return ohci_control(endp, dir, cmd, cmdsize, data, datasize);
    }
}

struct usb_pipe *
alloc_intr_pipe(u32 endp, int period)
{
    struct usb_s *cntl = endp2cntl(endp);
    // Find the exponential period of the requested time.
    if (period <= 0)
        period = 1;
    int frameexp = __fls(period);
    switch (cntl->type) {
    default:
    case USB_TYPE_UHCI:
        return uhci_alloc_intr_pipe(endp, frameexp);
    case USB_TYPE_OHCI:
        return ohci_alloc_intr_pipe(endp, frameexp);
    }
}

int noinline
usb_poll_intr(struct usb_pipe *pipe, void *data)
{
    u32 endp = GET_FLATPTR(pipe->endp);
    struct usb_s *cntl = endp2cntl(endp);
    switch (GET_GLOBAL(cntl->type)) {
    default:
    case USB_TYPE_UHCI:
        return uhci_poll_intr(pipe, data);
    case USB_TYPE_OHCI:
        return ohci_poll_intr(pipe, data);
    }
}

int
send_default_control(u32 endp, const struct usb_ctrlrequest *req, void *data)
{
    return send_control(endp, req->bRequestType & USB_DIR_IN
                        , req, sizeof(*req), data, req->wLength);
}

// Get the first 8 bytes of the device descriptor.
static int
get_device_info8(struct usb_device_descriptor *dinfo, u32 endp)
{
    struct usb_ctrlrequest req;
    req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
    req.bRequest = USB_REQ_GET_DESCRIPTOR;
    req.wValue = USB_DT_DEVICE<<8;
    req.wIndex = 0;
    req.wLength = 8;
    return send_default_control(endp, &req, dinfo);
}

static struct usb_config_descriptor *
get_device_config(u32 endp)
{
    struct usb_config_descriptor cfg;

    struct usb_ctrlrequest req;
    req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
    req.bRequest = USB_REQ_GET_DESCRIPTOR;
    req.wValue = USB_DT_CONFIG<<8;
    req.wIndex = 0;
    req.wLength = sizeof(cfg);
    int ret = send_default_control(endp, &req, &cfg);
    if (ret)
        return NULL;

    void *config = malloc_tmphigh(cfg.wTotalLength);
    if (!config)
        return NULL;
    req.wLength = cfg.wTotalLength;
    ret = send_default_control(endp, &req, config);
    if (ret)
        return NULL;
    //hexdump(config, cfg.wTotalLength);
    return config;
}

static u32
set_address(u32 endp)
{
    dprintf(3, "set_address %x\n", endp);
    struct usb_s *cntl = endp2cntl(endp);
    if (cntl->maxaddr >= USB_MAXADDR)
        return 0;

    struct usb_ctrlrequest req;
    req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
    req.bRequest = USB_REQ_SET_ADDRESS;
    req.wValue = cntl->maxaddr + 1;
    req.wIndex = 0;
    req.wLength = 0;
    int ret = send_default_control(endp, &req, NULL);
    if (ret)
        return 0;
    msleep(USB_TIME_SETADDR_RECOVERY);

    cntl->maxaddr++;
    return mkendp(cntl, cntl->maxaddr, 0, endp2speed(endp), endp2maxsize(endp));
}

static int
set_configuration(u32 endp, u16 val)
{
    struct usb_ctrlrequest req;
    req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
    req.bRequest = USB_REQ_SET_CONFIGURATION;
    req.wValue = val;
    req.wIndex = 0;
    req.wLength = 0;
    return send_default_control(endp, &req, NULL);
}

// Called for every found device - see if a driver is available for
// this device and do setup if so.
int
configure_usb_device(struct usb_s *cntl, int lowspeed)
{
    dprintf(3, "config_usb: %p %d\n", cntl, lowspeed);

    // Get device info
    u32 endp = mkendp(cntl, 0, 0, lowspeed, 8);
    struct usb_device_descriptor dinfo;
    int ret = get_device_info8(&dinfo, endp);
    if (ret)
        return 0;
    dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%02x\n"
            , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
            , dinfo.bDeviceProtocol, dinfo.bMaxPacketSize0);
    if (dinfo.bMaxPacketSize0 < 8 || dinfo.bMaxPacketSize0 > 64)
        return 0;
    endp = mkendp(cntl, 0, 0, lowspeed, dinfo.bMaxPacketSize0);

    // Get configuration
    struct usb_config_descriptor *config = get_device_config(endp);
    if (!config)
        return 0;

    // Determine if a driver exists for this device - only look at the
    // first interface of the first configuration.
    struct usb_interface_descriptor *iface = (void*)(&config[1]);
    if (iface->bInterfaceClass != USB_CLASS_HID
        || iface->bInterfaceSubClass != USB_INTERFACE_SUBCLASS_BOOT
        || iface->bInterfaceProtocol != USB_INTERFACE_PROTOCOL_KEYBOARD)
        // Not a "boot" keyboard
        goto fail;

    // Set the address and configure device.
    endp = set_address(endp);
    if (!endp)
        goto fail;
    ret = set_configuration(endp, config->bConfigurationValue);
    if (ret)
        goto fail;

    // Configure driver.
    ret = usb_keyboard_init(endp, iface, ((void*)config + config->wTotalLength
                                          - (void*)iface));
    if (ret)
        goto fail;

    free(config);
    return 1;
fail:
    free(config);
    return 0;
}

void
usb_setup(void)
{
    ASSERT32FLAT();
    if (! CONFIG_USB)
        return;

    dprintf(3, "init usb\n");

    usb_keyboard_setup();

    // Look for USB controllers
    int count = 0;
    int bdf, max;
    foreachpci(bdf, max) {
        u32 code = pci_config_readl(bdf, PCI_CLASS_REVISION) >> 8;

        if (code >> 8 != PCI_CLASS_SERIAL_USB)
            continue;

        struct usb_s *cntl = &USBControllers[count];
        cntl->bdf = bdf;

        if (code == PCI_CLASS_SERIAL_USB_UHCI)
            run_thread(uhci_init, cntl);
        else if (code == PCI_CLASS_SERIAL_USB_OHCI)
            run_thread(ohci_init, cntl);
        else
            continue;

        count++;
        if (count >= ARRAY_SIZE(USBControllers))
            break;
    }
}