summaryrefslogtreecommitdiffstats
path: root/firmware/usbstack/core/utils.c
blob: d43bd9290fc493edc3bc107fab4cc0078d9f7d7f (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2007 by Christian Gmeiner
 *
 * Based on linux/drivers/usb/gadget/usbstring.c
 *   Copyright (C) 2003 David Brownell 
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <string.h>
#include "usbstack/core.h"

void into_usb_ctrlrequest(struct usb_ctrlrequest* request)
{
    char* type = "";
    char* req = "";
    char* extra = 0;

    logf("-usb request-");
    /* check if packet is okay */
    if (request->bRequestType == 0 &&
        request->bRequest == 0 &&
        request->wValue == 0 &&
        request->wIndex == 0 &&
        request->wLength == 0) {
        logf(" -> INVALID <-");
        return;
    }

    switch (request->bRequestType & USB_TYPE_MASK) {
    case USB_TYPE_STANDARD:
        type = "standard";

        switch (request->bRequest) {
        case USB_REQ_GET_STATUS:
            req  = "get status";
            break;
        case USB_REQ_CLEAR_FEATURE:
            req = "clear feature";
            break;
        case USB_REQ_SET_FEATURE:
            req = "set feature";
            break;
        case USB_REQ_SET_ADDRESS:
            req = "set address";
            break;
        case USB_REQ_GET_DESCRIPTOR:
            req = "get descriptor";

            switch (request->wValue >> 8) {
            case USB_DT_DEVICE:
                extra = "get device descriptor";
                break;
            case USB_DT_DEVICE_QUALIFIER:
                extra = "get device qualifier";
                break;
            case USB_DT_OTHER_SPEED_CONFIG:
                extra = "get other-speed config descriptor";
            case USB_DT_CONFIG:
                extra = "get configuration descriptor";
                break;
            case USB_DT_STRING:
                extra = "get string descriptor";
                break;
            case USB_DT_DEBUG:
                extra = "debug";
                break;
            }
            break;

            break;
        case USB_REQ_SET_DESCRIPTOR:
            req = "set descriptor";
            break;
        case USB_REQ_GET_CONFIGURATION:
            req = "get configuration";
            break;
        case USB_REQ_SET_CONFIGURATION:
            req = "set configuration";
            break;
        case USB_REQ_GET_INTERFACE:
            req = "get interface";
            break;
        case USB_REQ_SET_INTERFACE:
            req = "set interface";
            break;
        case USB_REQ_SYNCH_FRAME:
            req = "sync frame";
            break;
        default:
            req = "unkown";
            break;
        }

        break;
    case USB_TYPE_CLASS:
        type = "class";
        break;

    case USB_TYPE_VENDOR:
        type = "vendor";
        break;
    }

    logf(" -b 0x%x", request->bRequestType);
    logf(" -b 0x%x", request->bRequest);
    logf(" -b 0x%x", request->wValue);
    logf(" -b 0x%x", request->wIndex);
    logf(" -b 0x%x", request->wLength);
    logf(" -> t: %s", type);
    logf(" -> r: %s", req);
    if (extra != 0) {
        logf(" -> e: %s", extra);
    }
}

int usb_stack_get_string(struct usb_string* strings, int id, uint8_t* buf)
{
    struct usb_string* tmp;
    char* sp, *dp;
    int len;

    /* if id is 0, then we need to send back all supported
     * languages. In our case we only support one
     * language: en-us (0x0409) */
    if (id == 0) {
        buf [0] = 4;
        buf [1] = USB_DT_STRING;
        buf [2] = (uint8_t) 0x0409;
        buf [3] = (uint8_t) (0x0409 >> 8);
        return 4;
    }

    /* look for string */
    for (tmp = strings; tmp && tmp->s; tmp++) {
        if (tmp->id == id) {
            break;
        }
    }

    /* did we found it? */
    if (!tmp || !tmp->s) {
        return -EINVAL;
    }

    len = MIN ((size_t) 126, strlen (tmp->s));
    memset(buf + 2, 0, 2 * len);

    /* convert to utf-16le */
    sp = (char*)tmp->s;
    dp = (char*)&buf[2];

    while (*sp) {
        *dp++ = *sp++;
        *dp++ = 0;
    }

    /* write len and tag */
    buf [0] = (len + 1) * 2;
    buf [1] = USB_DT_STRING;

    return buf[0];
}