summaryrefslogtreecommitdiffstats
path: root/firmware/usbstack/drivers/device/usb_storage.c
blob: 49644e58052aa383117085b3fe348eddfaf82c6f (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2007 by Christian Gmeiner
 *
 * 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 "usb_storage.h"
#include <string.h>

/*-------------------------------------------------------------------------*/

static struct usb_dcd_controller_ops* ops;

struct usb_device_driver usb_storage_driver = {
    .name =         "storage",
    .bind =         usb_storage_driver_bind,
    .unbind =       usb_storage_driver_unbind,
    .request =      usb_storage_driver_request,
    .suspend =      NULL,
    .resume =       NULL,
    .speed =        usb_storage_driver_speed,
};

/*-------------------------------------------------------------------------*/

#define PROTO_BULK 0x50    // Bulk only
#define SUBCL_SCSI 0x06    // Transparent SCSI

/* Bulk-only class specific requests */
#define USB_BULK_RESET_REQUEST          0xff
#define USB_BULK_GET_MAX_LUN_REQUEST    0xfe

/*-------------------------------------------------------------------------*/
/* usb descriptors */

static struct usb_device_descriptor storage_device_desc = {
    .bLength =              USB_DT_DEVICE_SIZE,
    .bDescriptorType =      USB_DT_DEVICE,
    .bcdUSB =               0x0200,
    .bDeviceClass =         0,
    .bDeviceSubClass =      0,
    .bDeviceProtocol =      0,
    .idVendor =             0xffff,
    .idProduct =            0x0001,
    .iManufacturer =        0,
    .iProduct =             0,
    .iSerialNumber =        0,
    .bNumConfigurations =   1,
};

static struct usb_config_descriptor storage_config_desc = {
    .bLength =              USB_DT_CONFIG_SIZE,
    .bDescriptorType =      USB_DT_CONFIG,

    .bNumInterfaces =       1,
    .bConfigurationValue =  1,
    .iConfiguration =       0,
    .bmAttributes =         USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
    .bMaxPower =            1,
};

static struct usb_interface_descriptor storage_interface_desc = {
    .bLength =              USB_DT_INTERFACE_SIZE,
    .bDescriptorType =      USB_DT_INTERFACE,
    .bInterfaceNumber =     0,
    .bNumEndpoints =        2,
    .bInterfaceClass =      USB_CLASS_MASS_STORAGE,
    .bInterfaceSubClass =   SUBCL_SCSI,
    .bInterfaceProtocol =   PROTO_BULK,
    .iInterface =           0,
};

static struct usb_endpoint_descriptor storage_fs_bulk_in_desc = {
    .bLength =              USB_DT_ENDPOINT_SIZE,
    .bDescriptorType =      USB_DT_ENDPOINT,
    .bEndpointAddress =     USB_DIR_IN,
    .bmAttributes =         USB_ENDPOINT_XFER_BULK,
    .wMaxPacketSize =       64,
};

static struct usb_endpoint_descriptor storage_fs_bulk_out_desc = {
    .bLength =              USB_DT_ENDPOINT_SIZE,
    .bDescriptorType =      USB_DT_ENDPOINT,
    .bEndpointAddress =     USB_DIR_OUT,
    .bmAttributes =         USB_ENDPOINT_XFER_BULK,
    .wMaxPacketSize =       64,
};

static struct usb_qualifier_descriptor storage_qualifier_desc = {
    .bLength =             sizeof(struct usb_qualifier_descriptor),
    .bDescriptorType =     USB_DT_DEVICE_QUALIFIER,
    .bcdUSB =              0x0200,
    .bDeviceClass =        0,
    .bNumConfigurations =  1,
};

struct usb_descriptor_header *storage_fs_function[] = {
    (struct usb_descriptor_header *) &storage_interface_desc,
    (struct usb_descriptor_header *) &storage_fs_bulk_in_desc,
    (struct usb_descriptor_header *) &storage_fs_bulk_out_desc,
    NULL,
};

/* USB 2.0 */
static struct usb_endpoint_descriptor storage_hs_bulk_in_desc = {
    .bLength =              USB_DT_ENDPOINT_SIZE,
    .bDescriptorType =      USB_DT_ENDPOINT,
    .bEndpointAddress =     USB_DIR_IN,
    .bmAttributes =         USB_ENDPOINT_XFER_BULK,
    .wMaxPacketSize =       512,
};

static struct usb_endpoint_descriptor storage_hs_bulk_out_desc = {
    .bLength =              USB_DT_ENDPOINT_SIZE,
    .bDescriptorType =      USB_DT_ENDPOINT,
    .bEndpointAddress =     USB_DIR_OUT,
    .bmAttributes =         USB_ENDPOINT_XFER_BULK,
    .wMaxPacketSize =       512,
};

struct usb_descriptor_header *storage_hs_function[] = {
    (struct usb_descriptor_header *) &storage_interface_desc,
    (struct usb_descriptor_header *) &storage_hs_bulk_in_desc,
    (struct usb_descriptor_header *) &storage_hs_bulk_out_desc,
    NULL,
};

#define BUFFER_SIZE 100
uint8_t buf[BUFFER_SIZE];

struct usb_response res;

/* helper functions */
static int config_buf(uint8_t *buf, uint8_t type, unsigned index);
static int set_config(int config);

struct device {
    struct usb_ep* in;
    struct usb_ep* out;
    struct usb_ep* intr;
    uint32_t used_config;
    struct usb_descriptor_header** descriptors;
};

static struct device dev;

/*-------------------------------------------------------------------------*/

void usb_storage_driver_init(void)
{
    logf("usb storage: register");
    usb_device_driver_register(&usb_storage_driver);
}

/*-------------------------------------------------------------------------*/
/* device driver ops */

int usb_storage_driver_bind(void* controler_ops)
{
    ops = controler_ops;

    /* serach and asign endpoints */
    usb_ep_autoconfig_reset();

    dev.in = usb_ep_autoconfig(&storage_fs_bulk_in_desc);
    if (!dev.in) {
        goto autoconf_fail;
    }
    dev.in->claimed = true;
    logf("usb storage: in: %s", dev.in->name);

    dev.out = usb_ep_autoconfig(&storage_fs_bulk_out_desc);
    if (!dev.out) {
        goto autoconf_fail;
    }
    dev.out->claimed = true;
    logf("usb storage: out: %s", dev.out->name);

    /* update device decsriptor */
    storage_device_desc.bMaxPacketSize0 = ops->ep0->maxpacket;

    /* update hs descriptors as we asume that endpoints are the same for fs and hs */
    storage_hs_bulk_in_desc.bEndpointAddress = storage_fs_bulk_in_desc.bEndpointAddress;
    storage_hs_bulk_out_desc.bEndpointAddress = storage_fs_bulk_out_desc.bEndpointAddress;

    return 0;

autoconf_fail:
    logf("failed to find endpoints");
    return -EOPNOTSUPP;
}

void usb_storage_driver_unbind(void) {

    /* disable endpoints... */
}

int usb_storage_driver_request(struct usb_ctrlrequest* request)
{
    int ret = -EOPNOTSUPP;
    logf("usb storage: request");

    res.length = 0;
    res.buf = NULL;

    switch (request->bRequestType & USB_TYPE_MASK) {
    case USB_TYPE_STANDARD:

        switch (request->bRequest) {
        case USB_REQ_GET_DESCRIPTOR:

            switch (request->wValue >> 8) {
            case USB_DT_DEVICE:
                logf("usb storage: sending device desc");
                ret = MIN(sizeof(struct usb_device_descriptor), request->wLength);
                res.buf = &storage_device_desc;
                break;

            case USB_DT_DEVICE_QUALIFIER:
                logf("usb storage: sending qualifier dec");
                ret = MIN(sizeof(struct usb_qualifier_descriptor), request->wLength);
                res.buf = &storage_qualifier_desc;
                break;

            case USB_DT_OTHER_SPEED_CONFIG:
            case USB_DT_CONFIG:
                logf("usb storage: sending config desc");

                ret = config_buf(buf, request->wValue >> 8, request->wValue & 0xff);
                if (ret >= 0) {
                    logf("%d, vs %d", request->wLength, ret);
                    ret = MIN(request->wLength, (uint16_t)ret);
                }
                res.buf = buf;
                break;
            }
            break;

        case USB_REQ_SET_CONFIGURATION:
            logf("usb storage: set configuration %d", request->wValue);
            ret = set_config(request->wValue);
            break;

        case USB_REQ_GET_CONFIGURATION:
            logf("usb storage: get configuration");
            ret = 1;
            res.buf = &dev.used_config;
            break;

        case USB_REQ_SET_INTERFACE:
            logf("usb storage: set interface");
            ret = 0;
            break;
        }

    case USB_TYPE_CLASS:

        switch (request->bRequest) {
        case USB_BULK_RESET_REQUEST:
            logf("usb storage: bulk reset");
            break;

        case USB_BULK_GET_MAX_LUN_REQUEST:
            logf("usb storage: get max lun");
            /* we support no LUNs (Logical Unit Number) */
            buf[0] = 0;
            ret = 1;
            break;
        }
        break;
    }

    if (ret >= 0) {
        res.length = ret;
        ret = ops->send(NULL, &res);
    }

    return ret;
}

void usb_storage_driver_speed(enum usb_device_speed speed)
{
    switch (speed) {
    case USB_SPEED_HIGH:
        logf("usb storage: using highspeed");
        dev.descriptors = storage_hs_function;
        break;
    default:
        logf("usb storage: using fullspeed");
        dev.descriptors = storage_fs_function;
        break;
    }
}

/*-------------------------------------------------------------------------*/
/* S/GET CONFIGURATION helpers */

static int config_buf(uint8_t *buf, uint8_t type, unsigned index)
{
    int len;

    (void)index;

    len = usb_stack_configdesc(&storage_config_desc, buf, BUFFER_SIZE, dev.descriptors);
    logf("result %d", len);
    if (len < 0) {
        return len;
    }
    ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
    return len;
}

static int set_config(int config)
{
    (void)config;

    /* enable endpoints */
    logf("setup %s", dev.in->name);
    ops->enable(dev.in, (struct usb_endpoint_descriptor*)dev.descriptors[1]);
    logf("setup %s", dev.out->name);
    ops->enable(dev.out, (struct usb_endpoint_descriptor*)dev.descriptors[2]);

    dev.used_config = config;

    /* setup buffers */

    return 0;
}