Subversion Repositories freemyipod

Rev

Rev 799 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 799 Rev 898
Line 1... Line 1...
1
//
1
//
2
//
2
//
3
//    Copyright 2011 user890104
3
//    Copyright 2013 user890104
4
//
4
//
5
//
5
//
6
//    This file is part of emCORE.
6
//    This file is part of emCORE.
7
//
7
//
8
//    emCORE is free software: you can redistribute it and/or
8
//    emCORE is free software: you can redistribute it and/or
Line 18... Line 18...
18
//    You should have received a copy of the GNU General Public License along
18
//    You should have received a copy of the GNU General Public License along
19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
20
//
20
//
21
//
21
//
22
 
22
 
23
 
-
 
24
#include "global.h"
23
#include "global.h"
25
 
24
 
26
#include "emcore.h"
25
#include "emcore.h"
27
#include "util.h"
26
#include "util.h"
28
#include "usb.h"
27
#include "usb.h"
29
 
28
 
-
 
29
libusb_context *usb_ctx = NULL;
-
 
30
libusb_device_handle *usb_handle = NULL;
-
 
31
uint8_t usb_iface_num = 0;
30
 
32
 
31
libusb_context* usb_ctx = NULL;
-
 
32
libusb_device_handle* usb_handle = NULL;
-
 
33
 
-
 
34
extern struct emcore_usb_endpoints_addr emcore_usb_eps_addr;
-
 
35
extern struct emcore_usb_endpoints_max_packet_size emcore_usb_eps_mps;
-
 
36
 
-
 
37
int usb_init(void)
33
int32_t usb_init(void) {
38
{
-
 
39
    int res;
34
    int32_t res;
40
 
35
 
41
#ifdef DEBUG
36
#ifdef DEBUG
42
    fprintf(stderr, "Initialising USB library...\n");
37
    fprintf(stderr, "Initialising USB library...\n");
43
#endif
38
#endif
44
 
39
 
45
    res = libusb_init(&usb_ctx);
40
    res = libusb_init(&usb_ctx);
46
 
41
 
47
    if (LIBUSB_SUCCESS != res)
42
    if (res != LIBUSB_SUCCESS) {
48
    {
-
 
49
        return res;
43
        return res;
50
    }
44
    }
51
 
45
 
52
#ifdef DEBUG
46
#ifdef DEBUG
53
    fprintf(stderr, "USB library initialized!\n");
47
    fprintf(stderr, "USB library initialized!\n");
54
    libusb_set_debug(usb_ctx, 3);
48
    libusb_set_debug(usb_ctx, 3);
55
 
-
 
56
#endif
49
#endif
57
 
50
 
58
    return LIBUSB_SUCCESS;
51
    return LIBUSB_SUCCESS;
59
}
52
}
60
 
53
 
61
int usb_find(uint16_t vendor_id, uint16_t product_id, uint8_t* reattach)
54
int32_t usb_find(uint16_t vendor_id, uint16_t product_id, uint8_t *reattach) {
62
{
-
 
63
    libusb_device **devs, *dev;
55
    libusb_device **devs, *dev = NULL;
64
    ssize_t devs_cnt;
56
    ssize_t devs_cnt, i, j, k, l;
65
    int res, i, j;
57
    int32_t res;
-
 
58
    uint8_t found;
66
    struct libusb_device_descriptor dev_desc;
59
    struct libusb_device_descriptor dev_desc;
67
    uint8_t found = 0;
-
 
68
    struct libusb_config_descriptor* cfg_desc;
60
    struct libusb_config_descriptor *cfg_desc;
69
    const struct libusb_interface* iface;
61
    const struct libusb_interface *iface;
70
    const struct libusb_interface_descriptor* iface_desc;
62
    const struct libusb_interface_descriptor *iface_desc;
71
    const struct libusb_endpoint_descriptor* ep_desc;
-
 
72
 
63
 
73
#ifdef DEBUG
64
#ifdef DEBUG
74
    fprintf(stderr, "Getting USB device list...\n");
65
    fprintf(stderr, "Getting USB device list...\n");
75
#endif
66
#endif
76
 
67
 
77
    devs_cnt = libusb_get_device_list(usb_ctx, &devs);
68
    devs_cnt = libusb_get_device_list(usb_ctx, &devs);
78
 
69
 
79
    if (devs_cnt < 0)
70
    if (devs_cnt < 0) {
80
    {
-
 
81
        return devs_cnt;
71
        return devs_cnt;
82
    }
72
    }
83
 
73
 
84
#ifdef DEBUG
74
#ifdef DEBUG
85
    fprintf(stderr, "Found %Zd USB devices!\n", devs_cnt);
75
    fprintf(stderr, "Found %Zd USB devices!\n", devs_cnt);
86
#endif
76
#endif
87
    for (i = 0; i < devs_cnt; ++i)
77
    for (i = 0; i < devs_cnt; ++i) {
88
    {
-
 
89
        dev = devs[i];
78
        dev = devs[i];
90
#ifdef DEBUG
79
#ifdef DEBUG
91
        fprintf(stderr, "Getting device descriptor of USB device %d...\n", i);
80
        fprintf(stderr, "Getting device descriptor of USB device %d...\n", i);
92
#endif
81
#endif
93
        res = libusb_get_device_descriptor(dev, &dev_desc);
82
        res = libusb_get_device_descriptor(dev, &dev_desc);
94
 
83
 
95
        if (LIBUSB_SUCCESS != res)
84
        if (res != LIBUSB_SUCCESS) {
96
        {
-
 
97
#ifdef DEBUG
85
#ifdef DEBUG
98
            fprintf(stderr, "Unable to get device descriptor of device %d!\n", i);
86
            fprintf(stderr, "Unable to get device descriptor of device %d!\n", i);
99
#endif
87
#endif
100
            continue;
88
            continue;
101
        }
89
        }
Line 103... Line 91...
103
#ifdef DEBUG
91
#ifdef DEBUG
104
        fprintf(stderr, "[%04x:%04x] bus %d, device %d, USB ver. %04x\n", dev_desc.idVendor,
92
        fprintf(stderr, "[%04x:%04x] bus %d, device %d, USB ver. %04x\n", dev_desc.idVendor,
105
            dev_desc.idProduct, libusb_get_bus_number(dev),
93
            dev_desc.idProduct, libusb_get_bus_number(dev),
106
            libusb_get_device_address(dev), dev_desc.bcdUSB);
94
            libusb_get_device_address(dev), dev_desc.bcdUSB);
107
#endif
95
#endif
108
        if (vendor_id == dev_desc.idVendor && product_id == dev_desc.idProduct)
96
        if (dev_desc.idVendor != vendor_id || dev_desc.idProduct != product_id) {
-
 
97
            continue;
109
        {
98
        }
-
 
99
        
110
#ifdef DEBUG
100
#ifdef DEBUG
111
            fprintf(stderr, "Found emCORE USB device!\n");
101
        fprintf(stderr, "Found emCORE USB device!\n");
112
#endif
102
#endif
113
            if (1 != dev_desc.bNumConfigurations)
103
        if (!dev_desc.bNumConfigurations) {
114
            {
-
 
115
#ifdef DEBUG
104
#ifdef DEBUG
116
                fprintf(stderr, "Number of configs is different than 1, not the right device...\n");
105
            fprintf(stderr, "No configs found...\n");
-
 
106
#endif
-
 
107
            continue;
-
 
108
        }
-
 
109
 
-
 
110
#ifdef DEBUG
-
 
111
        fprintf(stderr, "Found %Zd configs...\n", dev_desc.bNumConfigurations);
117
#endif
112
#endif
118
                continue;
-
 
119
            }
-
 
120
 
113
 
-
 
114
        for (j = 0; j < dev_desc.bNumConfigurations; ++j) {
121
#ifdef DEBUG
115
#ifdef DEBUG
122
            fprintf(stderr, "Getting config descriptor 0 of device...\n");
116
            fprintf(stderr, "Getting config descriptor %Zd of device...\n", j);
123
#endif
117
#endif
124
 
118
 
125
            res = libusb_get_config_descriptor(dev, 0, &cfg_desc);
119
            res = libusb_get_config_descriptor(dev, j, &cfg_desc);
126
 
120
 
127
            if (LIBUSB_SUCCESS != res)
121
            if (res != LIBUSB_SUCCESS) {
128
            {
-
 
129
                return res;
122
                return res;
130
            }
123
            }
131
 
124
 
132
            if (1 != cfg_desc->bNumInterfaces)
125
            if (!cfg_desc->bNumInterfaces) {
133
            {
-
 
134
#ifdef DEBUG
126
#ifdef DEBUG
135
                fprintf(stderr, "Wrong USB device, it should have exactly 1 interface\n");
127
                fprintf(stderr, "No interfaces found...\n");
136
#endif
128
#endif
137
 
-
 
138
                continue;
129
                continue;
139
            }
130
            }
140
 
131
 
-
 
132
            for (k = 0; k < cfg_desc->bNumInterfaces; ++k) {
141
            iface = &cfg_desc->interface[0];
133
                iface = &cfg_desc->interface[k];
142
 
134
 
143
            if (1 != iface->num_altsetting)
135
                if (!iface->num_altsetting) {
144
            {
-
 
145
#ifdef DEBUG
136
#ifdef DEBUG
146
                fprintf(stderr, "Wrong USB device, it should have exactly 1 altsetting\n");
137
                    fprintf(stderr, "No altsettings found...\n");
147
#endif
138
#endif
-
 
139
                    continue;
-
 
140
                }
148
 
141
 
149
                continue;
142
                for (l = 0; l < iface->num_altsetting; ++l) {
150
            }
-
 
151
 
-
 
152
            iface_desc = &iface->altsetting[0];
143
                    iface_desc = &iface->altsetting[l];
153
 
144
 
154
            if (4 != iface_desc->bNumEndpoints)
145
                    if (
-
 
146
                        iface_desc->bInterfaceClass    != EMCORE_USB_INTERFACE_CLASS ||
-
 
147
                        iface_desc->bInterfaceSubClass != EMCORE_USB_INTERFACE_SUB_CLASS ||
-
 
148
                        iface_desc->bInterfaceProtocol != EMCORE_USB_INTERFACE_PROTOCOL
155
            {
149
                    ) {
156
#ifdef DEBUG
150
#ifdef DEBUG
157
                fprintf(stderr, "Wrong USB device, it should have exactly 4 endpoints\n");
151
                        fprintf(stderr, "Wrong interface class (%02X %02X %02X), trying next device...\n", iface_desc->bInterfaceClass, iface_desc->bInterfaceSubClass, iface_desc->bInterfaceProtocol);
158
#endif
152
#endif
159
 
153
 
160
                continue;
154
                        continue;
161
            }
155
                    }
162
 
-
 
-
 
156
                    
163
#ifdef DEBUG
157
#ifdef DEBUG
164
           fprintf(stderr, "Endpoints:");
158
                    fprintf(stderr, "emCORE Debugger interface at %Zd\n", iface_desc->bInterfaceNumber);
165
#endif
159
#endif
166
 
160
 
167
            for (j = 0; j < 4; ++j)
-
 
168
            {
-
 
169
                ep_desc = &iface_desc->endpoint[j];
161
                    usb_iface_num = iface_desc->bInterfaceNumber;
170
 
-
 
171
#ifdef DEBUG
-
 
172
                fprintf(stderr, " %d at 0x%02x", j, ep_desc->bEndpointAddress);
-
 
173
#endif
-
 
174
 
-
 
175
                switch (j) {
-
 
176
                    case 0:
-
 
177
                        emcore_usb_eps_addr.cout = ep_desc->bEndpointAddress;
-
 
178
                    break;
-
 
179
                    case 1:
162
                    found = 1;
180
                        emcore_usb_eps_addr.cin = ep_desc->bEndpointAddress;
-
 
181
                    break;
-
 
182
                    case 2:
-
 
183
                        emcore_usb_eps_addr.dout = ep_desc->bEndpointAddress;
-
 
184
                    break;
-
 
185
                    case 3:
-
 
186
                        emcore_usb_eps_addr.din = ep_desc->bEndpointAddress;
-
 
187
                    break;
163
                    goto outside_search_loop;
188
                }
164
                }
189
            }
165
            }
190
 
-
 
191
#ifdef DEBUG
-
 
192
            fprintf(stderr, "\n");
-
 
193
#endif
-
 
194
 
-
 
195
            found = 1;
-
 
196
            break;
-
 
197
        }
166
        }
198
    }
167
    }
-
 
168
    
-
 
169
    outside_search_loop:
199
 
170
 
200
    if (found)
171
    if (found) {
201
    {
-
 
202
        res = usb_open(dev, reattach);
172
        res = usb_open(dev, reattach);
203
    }
173
    }
204
    else
174
    else {
205
    {
-
 
206
        fprintf(stderr, "USB device with VID=%4x and PID=%4x not found!\n",
175
        fprintf(stderr, "emCORE Debugger interface not found!\n");
207
            vendor_id, product_id);
-
 
208
 
176
 
209
        res = EMCORE_ERROR_NO_DEVICE;
177
        res = EMCORE_ERROR_NO_DEVICE;
210
    }
178
    }
211
 
179
 
212
#ifdef DEBUG
180
#ifdef DEBUG
Line 215... Line 183...
215
    libusb_free_device_list(devs, 1);
183
    libusb_free_device_list(devs, 1);
216
 
184
 
217
    return res;
185
    return res;
218
}
186
}
219
 
187
 
220
int usb_open(libusb_device* dev, uint8_t* reattach)
188
int32_t usb_open(libusb_device *dev, uint8_t *reattach) {
221
{
-
 
222
    int res;
189
    int32_t res;
223
 
190
 
224
#ifdef DEBUG
191
#ifdef DEBUG
225
    fprintf(stderr, "Opening USB device...\n");
192
    fprintf(stderr, "Opening USB device...\n");
226
#endif
193
#endif
227
    res = libusb_open(dev, &usb_handle);
194
    res = libusb_open(dev, &usb_handle);
228
 
195
 
229
    if (LIBUSB_SUCCESS != res)
196
    if (res != LIBUSB_SUCCESS) {
230
    {
-
 
231
        return res;
197
        return res;
232
    }
198
    }
233
#ifdef DEBUG
199
#ifdef DEBUG
234
    fprintf(stderr, "USB device opened!\n");
200
    fprintf(stderr, "USB device opened!\n");
235
    fprintf(stderr, "Setting USB configuration 1...\n");
201
    fprintf(stderr, "Setting USB configuration 1...\n");
236
#endif
202
#endif
237
 
203
 
238
    res = libusb_set_configuration(usb_handle, 1);
204
    res = libusb_set_configuration(usb_handle, 1);
239
 
205
 
240
    if (LIBUSB_SUCCESS != res)
206
    if (res != LIBUSB_SUCCESS) {
241
    {
-
 
242
        return res;
207
        return res;
243
    }
208
    }
-
 
209
    
244
#ifdef DEBUG
210
#ifdef DEBUG
245
    fprintf(stderr, "USB configuration set!\n");
211
    fprintf(stderr, "USB configuration set!\n");
246
#endif
212
#endif
247
    res = libusb_kernel_driver_active(usb_handle, 0);
213
    res = libusb_kernel_driver_active(usb_handle, 0);
248
 
214
 
249
    if (1 == res)
215
    if (res < 0) {
-
 
216
        return res;
250
    {
217
    }
-
 
218
    
-
 
219
    if (res == 1) {
251
        *reattach = 1;
220
        *reattach = 1;
252
 
221
 
253
        res = libusb_detach_kernel_driver(usb_handle, 0);
222
        res = libusb_detach_kernel_driver(usb_handle, 0);
254
    }
223
    }
255
 
224
 
256
    if (LIBUSB_SUCCESS != res)
225
    if (res != LIBUSB_SUCCESS) {
257
    {
-
 
258
        return res;
226
        return res;
259
    }
227
    }
260
 
228
 
261
#ifdef DEBUG
229
#ifdef DEBUG
262
    fprintf(stderr, "Claiming interface 0...\n");
230
    fprintf(stderr, "Claiming interface 0...\n");
263
#endif
231
#endif
264
    res = libusb_claim_interface(usb_handle, 0);
232
    res = libusb_claim_interface(usb_handle, 0);
265
 
233
 
266
    if (LIBUSB_SUCCESS != res)
234
    if (res != LIBUSB_SUCCESS) {
267
    {
-
 
268
        return res;
235
        return res;
269
    }
236
    }
270
 
237
 
271
#ifdef DEBUG
238
#ifdef DEBUG
272
    fprintf(stderr, "Interface claimed successfully!\n");
239
    fprintf(stderr, "Interface claimed successfully!\n");
273
    fprintf(stderr, "Getting endpoints max size...\n");
-
 
274
#endif
-
 
275
 
-
 
276
    res = emcore_get_packet_info(&emcore_usb_eps_mps);
-
 
277
 
-
 
278
    if (EMCORE_SUCCESS != res)
-
 
279
    {
-
 
280
        return res;
-
 
281
    }
-
 
282
 
-
 
283
#ifdef DEBUG
-
 
284
    fprintf(stderr, "Got endpoint max size!\n");
-
 
285
    fprintf(stderr, "COUT max pckt: %d, CIN max pckt: %d, DOUT max pckt: %d, DIN max pckt: %d\n",
-
 
286
        emcore_usb_eps_mps.cout, emcore_usb_eps_mps.cin, emcore_usb_eps_mps.dout, emcore_usb_eps_mps.din
-
 
287
    );
-
 
288
#endif
240
#endif
289
 
241
 
290
    return LIBUSB_SUCCESS;
242
    return LIBUSB_SUCCESS;
291
}
243
}
292
 
244
 
293
int usb_bulk_transfer(unsigned char endpoint, void* data, int length)
245
int32_t usb_control_transfer(uint8_t rq_type, void *data, size_t length) {
294
{
-
 
295
    int transferred;
-
 
296
    int res;
246
    int32_t res;
297
 
247
 
298
    res = libusb_bulk_transfer(usb_handle, endpoint, (unsigned char*)data, length, &transferred, 30000);
248
    res = libusb_control_transfer(usb_handle, rq_type, 0, 0, usb_iface_num, (unsigned char *)data, length, 10000);
299
 
249
 
300
    if (LIBUSB_SUCCESS != res)
250
    if (res < 0) {
301
    {
-
 
302
        return res;
251
        return res;
303
    }
252
    }
304
 
253
 
305
    if (transferred != length)
254
    if ((size_t)res != length) {
306
    {
-
 
307
        return EMCORE_ERROR_INCOMPLETE;
255
        return EMCORE_ERROR_INCOMPLETE;
308
    }
256
    }
309
 
257
 
310
    return LIBUSB_SUCCESS;
258
    return LIBUSB_SUCCESS;
311
}
259
}
312
 
260
 
313
int usb_close(uint8_t reattach) {
261
int32_t usb_close(uint8_t reattach) {
314
    int res;
262
    int32_t res;
315
 
263
 
316
#ifdef DEBUG
264
#ifdef DEBUG
317
    fprintf(stderr, "Releasing USB interface...\n");
265
    fprintf(stderr, "Releasing USB interface...\n");
318
#endif
266
#endif
319
    res = libusb_release_interface(usb_handle, 0);
267
    res = libusb_release_interface(usb_handle, 0);
320
 
268
 
321
    if (LIBUSB_SUCCESS != res)
269
    if (res != LIBUSB_SUCCESS) {
322
    {
-
 
323
        return res;
270
        return res;
324
    }
271
    }
325
 
272
 
326
#ifdef DEBUG
273
#ifdef DEBUG
327
    fprintf(stderr, "Released interface successfully!\n");
274
    fprintf(stderr, "Released interface successfully!\n");
328
#endif
275
#endif
329
 
276
 
330
    if (reattach)
277
    if (reattach) {
331
    {
-
 
332
#ifdef DEBUG
278
#ifdef DEBUG
333
        fprintf(stderr, "Reattaching kernel driver...\n");
279
        fprintf(stderr, "Reattaching kernel driver...\n");
334
#endif
280
#endif
335
 
281
 
336
        res = libusb_attach_kernel_driver(usb_handle, 0);
282
        res = libusb_attach_kernel_driver(usb_handle, 0);
337
 
283
 
338
        if (LIBUSB_SUCCESS == res)
284
        if (res == LIBUSB_SUCCESS) {
339
        {
-
 
340
#ifdef DEBUG
285
#ifdef DEBUG
341
            fprintf(stderr, "Reattached successfully!\n");
286
            fprintf(stderr, "Reattached successfully!\n");
342
#endif
287
#endif
343
        }
288
        }
344
        else
289
        else {
345
        {
-
 
346
            print_error(res);
290
            print_error(res);
347
 
291
 
348
            res = LIBUSB_SUCCESS;
292
            res = LIBUSB_SUCCESS;
349
        }
293
        }
350
    }
294
    }
Line 354... Line 298...
354
    libusb_close(usb_handle);
298
    libusb_close(usb_handle);
355
 
299
 
356
    return res;
300
    return res;
357
}
301
}
358
 
302
 
359
void usb_exit(void)
303
void usb_exit(void) {
360
{
-
 
361
#ifdef DEBUG
304
#ifdef DEBUG
362
    fprintf(stderr, "Deinitializing USB library...\n");
305
    fprintf(stderr, "Deinitializing USB library...\n");
363
#endif
306
#endif
364
 
307
 
365
    libusb_exit(usb_ctx);
308
    libusb_exit(usb_ctx);
366
}
309
}
367
 
310
 
368
int usb_destroy(uint8_t reattach)
311
int32_t usb_destroy(uint8_t reattach) {
369
{
-
 
370
    int res = LIBUSB_SUCCESS;
312
    int32_t res = LIBUSB_SUCCESS;
371
 
313
 
372
    if (usb_handle)
314
    if (usb_handle) {
373
    {
-
 
374
        res = usb_close(reattach);
315
        res = usb_close(reattach);
375
    }
316
    }
376
 
317
 
377
    if (usb_ctx)
318
    if (usb_ctx) {
378
    {
-
 
379
        usb_exit();
319
        usb_exit();
380
    }
320
    }
381
 
321
 
382
    return res;
322
    return res;
383
}
323
}