Merge upstream texane/master
[fw/stlink] / src / stlink-usb.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdint.h>
5 #include <time.h>
6 #include <sys/types.h>
7 #include <libusb-1.0/libusb.h>
8 #include "stlink-common.h"
9 #include "stlink-usb.h"
10
11 enum SCSI_Generic_Direction {SG_DXFER_TO_DEV=0, SG_DXFER_FROM_DEV=0x80};
12
13 void _stlink_usb_close(stlink_t* sl) {
14     struct stlink_libusb * const handle = sl->backend_data;
15     // maybe we couldn't even get the usb device?
16     if (handle != NULL) {
17         if (handle->req_trans != NULL)
18             libusb_free_transfer(handle->req_trans);
19
20         if (handle->rep_trans != NULL)
21             libusb_free_transfer(handle->rep_trans);
22
23         if (handle->usb_handle != NULL) {
24             libusb_close(handle->usb_handle);
25         }
26
27         libusb_exit(handle->libusb_ctx);
28         free(handle);
29     }
30 }
31
32
33 struct trans_ctx {
34 #define TRANS_FLAGS_IS_DONE (1 << 0)
35 #define TRANS_FLAGS_HAS_ERROR (1 << 1)
36     volatile unsigned long flags;
37 };
38
39 static void on_trans_done(struct libusb_transfer * trans) {
40     struct trans_ctx * const ctx = trans->user_data;
41
42     if (trans->status != LIBUSB_TRANSFER_COMPLETED)
43         ctx->flags |= TRANS_FLAGS_HAS_ERROR;
44
45     ctx->flags |= TRANS_FLAGS_IS_DONE;
46 }
47
48 int submit_wait(struct stlink_libusb *slu, struct libusb_transfer * trans) {
49     struct timeval start;
50     struct timeval now;
51     struct timeval diff;
52     struct trans_ctx trans_ctx;
53     enum libusb_error error;
54
55     trans_ctx.flags = 0;
56
57     /* brief intrusion inside the libusb interface */
58     trans->callback = on_trans_done;
59     trans->user_data = &trans_ctx;
60
61     if ((error = libusb_submit_transfer(trans))) {
62         printf("libusb_submit_transfer(%d)\n", error);
63         return -1;
64     }
65
66     gettimeofday(&start, NULL);
67
68     while (trans_ctx.flags == 0) {
69         struct timeval timeout;
70         timeout.tv_sec = 3;
71         timeout.tv_usec = 0;
72         if (libusb_handle_events_timeout(slu->libusb_ctx, &timeout)) {
73             printf("libusb_handle_events()\n");
74             return -1;
75         }
76
77         gettimeofday(&now, NULL);
78         timersub(&now, &start, &diff);
79         if (diff.tv_sec >= 3) {
80             printf("libusb_handle_events() timeout\n");
81             return -1;
82         }
83     }
84
85     if (trans_ctx.flags & TRANS_FLAGS_HAS_ERROR) {
86         printf("libusb_handle_events() | has_error\n");
87         return -1;
88     }
89
90     return 0;
91 }
92
93 ssize_t send_recv(struct stlink_libusb* handle, int terminate,
94         unsigned char* txbuf, size_t txsize,
95         unsigned char* rxbuf, size_t rxsize) {
96     /* note: txbuf and rxbuf can point to the same area */
97     int res = 0;
98
99     libusb_fill_bulk_transfer(handle->req_trans, handle->usb_handle,
100             handle->ep_req,
101             txbuf, txsize,
102             NULL, NULL,
103             0
104             );
105
106     if (submit_wait(handle, handle->req_trans)) return -1;
107
108     /* send_only */
109     if (rxsize != 0) {
110
111         /* read the response */
112         
113         libusb_fill_bulk_transfer(handle->rep_trans, handle->usb_handle,
114                                   handle->ep_rep, rxbuf, rxsize, NULL, NULL, 0);
115         
116         if (submit_wait(handle, handle->rep_trans)) return -1;
117         res = handle->rep_trans->actual_length;
118     }
119     if ((handle->protocoll == 1) && terminate) {
120         /* Read the SG reply */
121         unsigned char sg_buf[13];
122         libusb_fill_bulk_transfer
123             (handle->rep_trans, handle->usb_handle,
124              handle->ep_rep, sg_buf, 13, NULL, NULL, 0);
125         res = submit_wait(handle, handle->rep_trans);
126         /* The STLink doesn't seem to evaluate the sequence number */
127         handle->sg_transfer_idx++;
128         if (res ) return -1;
129     }
130
131     return handle->rep_trans->actual_length;
132 }
133
134 static inline int send_only
135 (struct stlink_libusb* handle, int terminate,
136  unsigned char* txbuf, size_t txsize) {
137     return send_recv(handle, terminate, txbuf, txsize, NULL, 0);
138 }
139
140
141 /* Search for a STLINK device, either any or teh one with the given PID
142  * Return the protocoll version
143  */
144 static int is_stlink_device(libusb_device * dev, uint16_t pid) {
145     struct libusb_device_descriptor desc;
146     int version;
147
148     if (libusb_get_device_descriptor(dev, &desc))
149         return 0;
150
151     if (desc.idVendor != USB_ST_VID)
152         return 0;
153
154     if ((desc.idProduct != USB_STLINK_32L_PID) && 
155         (desc.idProduct != USB_STLINK_PID ))
156         return 0;
157
158     if(pid && (pid != desc.idProduct))
159         return 0;
160     if (desc.idProduct == USB_STLINK_PID )
161         version = 1;
162     else
163         version = 2;
164
165     return version;
166 }
167
168 static int fill_command
169 (stlink_t * sl, enum SCSI_Generic_Direction dir, uint32_t len) {
170     struct stlink_libusb * const slu = sl->backend_data;
171     unsigned char* const cmd = sl->c_buf;
172     int i = 0;
173     memset(cmd, 0, sizeof (sl->c_buf));
174     if(slu->protocoll == 1) {
175         cmd[i++] = 'U';
176         cmd[i++] = 'S';
177         cmd[i++] = 'B';
178         cmd[i++] = 'C';
179         write_uint32(&cmd[i], slu->sg_transfer_idx);
180         write_uint32(&cmd[i + 4], len);
181         i += 8;
182         cmd[i++] = (dir == SG_DXFER_FROM_DEV)?0x80:0;
183         cmd[i++] = 0; /* Logical unit */
184         cmd[i++] = 0xa; /* Command length */
185     }
186     return i;
187 }
188
189 void _stlink_usb_version(stlink_t *sl) {
190     struct stlink_libusb * const slu = sl->backend_data;
191     unsigned char* const data = sl->q_buf;
192     unsigned char* const cmd  = sl->c_buf;
193     ssize_t size;
194     uint32_t rep_len = 6;
195     int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
196
197     cmd[i++] = STLINK_GET_VERSION;
198
199     size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
200     if (size == -1) {
201         printf("[!] send_recv\n");
202         return;
203     }
204 }
205
206 void _stlink_usb_write_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
207     struct stlink_libusb * const slu = sl->backend_data;
208     unsigned char* const data = sl->q_buf;
209     unsigned char* const cmd  = sl->c_buf;
210
211     int i = fill_command(sl, SG_DXFER_TO_DEV, len);
212     cmd[i++] = STLINK_DEBUG_COMMAND;
213     cmd[i++] = STLINK_DEBUG_WRITEMEM_32BIT;
214     write_uint32(&cmd[i], addr);
215     write_uint16(&cmd[i + 4], len);
216     send_only(slu, 0, cmd, slu->cmd_len);
217
218     send_only(slu, 1, data, len);
219 }
220
221 void _stlink_usb_write_mem8(stlink_t *sl, uint32_t addr, uint16_t len) {
222     struct stlink_libusb * const slu = sl->backend_data;
223     unsigned char* const data = sl->q_buf;
224     unsigned char* const cmd  = sl->c_buf;
225
226     int i = fill_command(sl, SG_DXFER_TO_DEV, 0);
227     cmd[i++] = STLINK_DEBUG_COMMAND;
228     cmd[i++] = STLINK_DEBUG_WRITEMEM_8BIT;
229     write_uint32(&cmd[i], addr);
230     write_uint16(&cmd[i + 4], len);
231     send_only(slu, 0, cmd, slu->cmd_len);
232     send_only(slu, 1, data, len);
233 }
234
235
236 int _stlink_usb_current_mode(stlink_t * sl) {
237     struct stlink_libusb * const slu = sl->backend_data;
238     unsigned char* const cmd  = sl->c_buf;
239     unsigned char* const data = sl->q_buf;
240     ssize_t size;
241     int rep_len = 2;
242     int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
243     
244     cmd[i++] = STLINK_GET_CURRENT_MODE;
245     size = send_recv(slu, 1, cmd,  slu->cmd_len, data, rep_len);
246     if (size == -1) {
247         printf("[!] send_recv\n");
248         return -1;
249     }
250     return sl->q_buf[0];
251 }
252
253 void _stlink_usb_core_id(stlink_t * sl) {
254     struct stlink_libusb * const slu = sl->backend_data;
255     unsigned char* const cmd  = sl->c_buf;
256     unsigned char* const data = sl->q_buf;
257     ssize_t size;
258     int rep_len = 4;
259     int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
260
261     cmd[i++] = STLINK_DEBUG_COMMAND;
262     cmd[i++] = STLINK_DEBUG_READCOREID;
263
264     size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
265     if (size == -1) {
266         printf("[!] send_recv\n");
267         return;
268     }
269
270     sl->core_id = read_uint32(data, 0);
271 }
272
273 void _stlink_usb_status(stlink_t * sl) {
274     struct stlink_libusb * const slu = sl->backend_data;
275     unsigned char* const data = sl->q_buf;
276     unsigned char* const cmd  = sl->c_buf;
277     ssize_t size;
278     int rep_len = 2;
279     int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
280
281     cmd[i++] = STLINK_DEBUG_COMMAND;
282     cmd[i++] = STLINK_DEBUG_GETSTATUS;
283
284     size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
285     if (size == -1) {
286         printf("[!] send_recv\n");
287         return;
288     }
289 }
290
291 void _stlink_usb_force_debug(stlink_t *sl) {
292     struct stlink_libusb *slu = sl->backend_data;
293     unsigned char* const data = sl->q_buf;
294     unsigned char* const cmd  = sl->c_buf;
295     ssize_t size;
296     int rep_len = 2;
297     int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
298
299     cmd[i++] = STLINK_DEBUG_COMMAND;
300     cmd[i++] = STLINK_DEBUG_FORCEDEBUG;
301     size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
302     if (size == -1) {
303         printf("[!] send_recv\n");
304         return;
305     }
306 }
307
308 void _stlink_usb_enter_swd_mode(stlink_t * sl) {
309     struct stlink_libusb * const slu = sl->backend_data;
310     unsigned char* const cmd  = sl->c_buf;
311     ssize_t size;
312     const int rep_len = 0;
313     int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
314
315     cmd[i++] = STLINK_DEBUG_COMMAND;
316     cmd[i++] = STLINK_DEBUG_ENTER;
317     cmd[i++] = STLINK_DEBUG_ENTER_SWD;
318
319     size = send_only(slu, 1, cmd, slu->cmd_len);
320     if (size == -1) {
321         printf("[!] send_recv\n");
322         return;
323     }
324 }
325
326 void _stlink_usb_exit_dfu_mode(stlink_t* sl) {
327     struct stlink_libusb * const slu = sl->backend_data;
328     unsigned char* const cmd = sl->c_buf;
329     ssize_t size;
330     int i = fill_command(sl, SG_DXFER_FROM_DEV, 0);
331
332     cmd[i++] = STLINK_DFU_COMMAND;
333     cmd[i++] = STLINK_DFU_EXIT;
334
335     size = send_only(slu, 1, cmd, slu->cmd_len);
336     if (size == -1) {
337         printf("[!] send_recv\n");
338         return;
339     }
340 }
341
342 /**
343  * TODO - not convinced this does anything...
344  * @param sl
345  */
346 void _stlink_usb_reset(stlink_t * sl) {
347     struct stlink_libusb * const slu = sl->backend_data;
348     unsigned char* const data = sl->q_buf;
349     unsigned char* const cmd = sl->c_buf;
350     ssize_t size;
351     int rep_len = 2;
352     int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
353
354     cmd[i++] = STLINK_DEBUG_COMMAND;
355     cmd[i++] = STLINK_DEBUG_RESETSYS;
356
357     size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
358     if (size == -1) {
359         printf("[!] send_recv\n");
360         return;
361     }
362 }
363
364
365 void _stlink_usb_step(stlink_t* sl) {
366     struct stlink_libusb * const slu = sl->backend_data;
367     unsigned char* const data = sl->q_buf;
368     unsigned char* const cmd = sl->c_buf;
369     ssize_t size;
370     int rep_len = 2;
371     int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
372
373     cmd[i++] = STLINK_DEBUG_COMMAND;
374     cmd[i++] = STLINK_DEBUG_STEPCORE;
375
376     size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
377     if (size == -1) {
378         printf("[!] send_recv\n");
379         return;
380     }
381 }
382
383 /**
384  * This seems to do a good job of restarting things from the beginning?
385  * @param sl
386  */
387 void _stlink_usb_run(stlink_t* sl) {
388     struct stlink_libusb * const slu = sl->backend_data;
389     unsigned char* const data = sl->q_buf;
390     unsigned char* const cmd = sl->c_buf;
391     ssize_t size;
392     int rep_len = 2;
393     int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
394
395     cmd[i++] = STLINK_DEBUG_COMMAND;
396     cmd[i++] = STLINK_DEBUG_RUNCORE;
397
398     size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
399     if (size == -1) {
400         printf("[!] send_recv\n");
401         return;
402     }
403 }
404
405 void _stlink_usb_exit_debug_mode(stlink_t *sl) {
406     struct stlink_libusb * const slu = sl->backend_data;
407     unsigned char* const cmd = sl->c_buf;
408     ssize_t size;
409     int i = fill_command(sl, SG_DXFER_FROM_DEV, 0);
410
411     cmd[i++] = STLINK_DEBUG_COMMAND;
412     cmd[i++] = STLINK_DEBUG_EXIT;
413
414     size = send_only(slu, 1, cmd, slu->cmd_len);
415     if (size == -1) {
416         printf("[!] send_only\n");
417         return;
418     }
419 }
420
421 void _stlink_usb_read_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
422     struct stlink_libusb * const slu = sl->backend_data;
423     unsigned char* const data = sl->q_buf;
424     unsigned char* const cmd = sl->c_buf;
425     ssize_t size;
426     int i = fill_command(sl, SG_DXFER_FROM_DEV, len);
427
428     cmd[i++] = STLINK_DEBUG_COMMAND;
429     cmd[i++] = STLINK_DEBUG_READMEM_32BIT;
430     write_uint32(&cmd[i], addr);
431     write_uint16(&cmd[i + 4], len);
432
433     size = send_recv(slu, 1, cmd, slu->cmd_len, data, len);
434     if (size == -1) {
435         printf("[!] send_recv\n");
436         return;
437     }
438
439     sl->q_len = (size_t) size;
440
441     stlink_print_data(sl);
442 }
443
444 void _stlink_usb_read_all_regs(stlink_t *sl, reg *regp) {
445     struct stlink_libusb * const slu = sl->backend_data;
446     unsigned char* const cmd = sl->c_buf;
447     unsigned char* const data = sl->q_buf;
448     ssize_t size;
449     uint32_t rep_len = 84;
450     int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
451
452     cmd[i++] = STLINK_DEBUG_COMMAND;
453     cmd[i++] = STLINK_DEBUG_READALLREGS;
454     size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
455     if (size == -1) {
456         printf("[!] send_recv\n");
457         return;
458     }
459     sl->q_len = (size_t) size;
460     stlink_print_data(sl);
461     for(i=0; i<16; i++)
462         regp->r[i]= read_uint32(sl->q_buf, i*4);
463     regp->xpsr       = read_uint32(sl->q_buf, 64);
464     regp->main_sp    = read_uint32(sl->q_buf, 68);
465     regp->process_sp = read_uint32(sl->q_buf, 72);
466     regp->rw         = read_uint32(sl->q_buf, 76);
467     regp->rw2        = read_uint32(sl->q_buf, 80);
468     if (sl->verbose < 2)
469         return;
470
471     DD(sl, "xpsr       = 0x%08x\n", read_uint32(sl->q_buf, 64));
472     DD(sl, "main_sp    = 0x%08x\n", read_uint32(sl->q_buf, 68));
473     DD(sl, "process_sp = 0x%08x\n", read_uint32(sl->q_buf, 72));
474     DD(sl, "rw         = 0x%08x\n", read_uint32(sl->q_buf, 76));
475     DD(sl, "rw2        = 0x%08x\n", read_uint32(sl->q_buf, 80));
476 }
477
478 void _stlink_usb_read_reg(stlink_t *sl, int r_idx, reg *regp) {
479     struct stlink_libusb * const slu = sl->backend_data;
480     unsigned char* const data = sl->q_buf;
481     unsigned char* const cmd  = sl->c_buf;
482     ssize_t size;
483     uint32_t r;
484     uint32_t rep_len = 4;
485     int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
486
487     cmd[i++] = STLINK_DEBUG_COMMAND;
488     cmd[i++] = STLINK_DEBUG_READREG;
489     cmd[i++] = (uint8_t) r_idx;
490     size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
491     if (size == -1) {
492         printf("[!] send_recv\n");
493         return;
494     }
495     sl->q_len = (size_t) size;
496     stlink_print_data(sl);
497     r = read_uint32(sl->q_buf, 0);
498     DD(sl, "r_idx (%2d) = 0x%08x\n", r_idx, r);
499     
500     switch (r_idx) {
501     case 16:
502         regp->xpsr = r;
503         break;
504     case 17:
505         regp->main_sp = r;
506         break;
507     case 18:
508         regp->process_sp = r;
509         break;
510     case 19:
511         regp->rw = r; /* XXX ?(primask, basemask etc.) */
512         break;
513     case 20:
514         regp->rw2 = r; /* XXX ?(primask, basemask etc.) */
515         break;
516     default:
517         regp->r[r_idx] = r;
518     }
519 }
520
521 void _stlink_usb_write_reg(stlink_t *sl, uint32_t reg, int idx) {
522     struct stlink_libusb * const slu = sl->backend_data;
523     unsigned char* const data = sl->q_buf;
524     unsigned char* const cmd  = sl->c_buf;
525     ssize_t size;
526     uint32_t rep_len = 2;
527     int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
528
529     cmd[i++] = STLINK_DEBUG_COMMAND;
530     cmd[i++] = STLINK_DEBUG_WRITEREG;
531     cmd[i++] = idx;
532     write_uint32(&cmd[i], reg);
533     size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
534     if (size == -1) {
535         printf("[!] send_recv\n");
536         return;
537     }
538     sl->q_len = (size_t) size;
539     stlink_print_data(sl);
540 }
541
542 stlink_backend_t _stlink_usb_backend = {
543     _stlink_usb_close,
544     _stlink_usb_exit_debug_mode,
545     _stlink_usb_enter_swd_mode,
546     NULL,  // no enter_jtag_mode here...
547     _stlink_usb_exit_dfu_mode,
548     _stlink_usb_core_id,
549     _stlink_usb_reset,
550     _stlink_usb_run,
551     _stlink_usb_status,
552     _stlink_usb_version,
553     _stlink_usb_read_mem32,
554     _stlink_usb_write_mem32,
555     _stlink_usb_write_mem8,
556     _stlink_usb_read_all_regs,
557     _stlink_usb_read_reg,
558     _stlink_usb_write_reg,
559     _stlink_usb_step,
560     _stlink_usb_current_mode,
561     _stlink_usb_force_debug
562 };
563
564
565 stlink_t* stlink_open_usb(const int verbose) {
566     stlink_t* sl = NULL;
567     struct stlink_libusb* slu = NULL;
568     int error = -1;
569     libusb_device** devs = NULL;
570     libusb_device* dev;
571     ssize_t i;
572     ssize_t count;
573     int config;
574     char *iSerial = NULL;
575
576     sl = malloc(sizeof (stlink_t));
577     slu = malloc(sizeof (struct stlink_libusb));
578     if (sl == NULL) goto on_error;
579     if (slu == NULL) goto on_error;
580     memset(sl, 0, sizeof (stlink_t));
581     memset(slu, 0, sizeof (struct stlink_libusb));
582
583     sl->verbose = verbose;
584     sl->backend = &_stlink_usb_backend;
585     sl->backend_data = slu;
586     
587     sl->core_stat = STLINK_CORE_STAT_UNKNOWN;
588
589     /* flash memory settings */
590     sl->flash_base = STM32_FLASH_BASE;
591     sl->flash_size = STM32_FLASH_SIZE;
592     sl->flash_pgsz = STM32_FLASH_PGSZ;
593
594     /* system memory */
595     sl->sys_base = STM32_SYSTEM_BASE;
596     sl->sys_size = STM32_SYSTEM_SIZE;
597
598     /* sram memory settings */
599     sl->sram_base = STM32_SRAM_BASE;
600     sl->sram_size = STM32L_SRAM_SIZE;
601
602     if (libusb_init(&(slu->libusb_ctx))) {
603         fprintf(stderr, "failed to init libusb context, wrong version of libraries?\n");
604         goto on_error;
605     }
606     
607     count = libusb_get_device_list(slu->libusb_ctx, &devs);
608     if (count < 0) {
609         printf("libusb_get_device_list\n");
610         goto on_libusb_error;
611     }
612
613     for (i = 0; i < count; ++i) {
614         dev = devs[i];
615         slu->protocoll = is_stlink_device(dev, 0);
616         if (slu->protocoll > 0) break;
617     }
618     if (i == count) goto on_libusb_error;
619
620     if (libusb_open(dev, &(slu->usb_handle))) {
621         printf("libusb_open()\n");
622         goto on_libusb_error;
623     }
624     
625     if (iSerial) {
626         unsigned char serial[256];
627         struct libusb_device_descriptor desc;
628         int r;
629
630         r = libusb_get_device_descriptor(dev, &desc);
631         if (r<0) {
632             printf("Can't get descriptor to match Iserial\n");
633             goto on_libusb_error;
634         }
635         r = libusb_get_string_descriptor_ascii
636             (slu->usb_handle, desc.iSerialNumber, serial, 256);
637         if (r<0) {
638             printf("Can't get Serialnumber to match Iserial\n");
639             goto on_libusb_error;
640         }
641         if (strcmp((char*)serial, iSerial)) {
642             printf("Mismatch in serial numbers, dev %s vs given %s\n",
643                    serial, iSerial);
644             goto on_libusb_error;
645         }
646     }
647
648     if (libusb_kernel_driver_active(slu->usb_handle, 0) == 1) {
649         int r;
650         
651         r = libusb_detach_kernel_driver(slu->usb_handle, 0);
652         if (r<0)
653             printf("libusb_detach_kernel_driver(() error %s\n", strerror(-r));
654         goto on_libusb_error;
655     }
656
657     if (libusb_get_configuration(slu->usb_handle, &config)) {
658         /* this may fail for a previous configured device */
659         printf("libusb_get_configuration()\n");
660         goto on_libusb_error;
661     }
662
663     if (config != 1) {
664         printf("setting new configuration (%d -> 1)\n", config);
665         if (libusb_set_configuration(slu->usb_handle, 1)) {
666             /* this may fail for a previous configured device */
667             printf("libusb_set_configuration()\n");
668             goto on_libusb_error;
669         }
670     }
671
672     if (libusb_claim_interface(slu->usb_handle, 0)) {
673         printf("libusb_claim_interface()\n");
674         goto on_libusb_error;
675     }
676
677     slu->req_trans = libusb_alloc_transfer(0);
678     if (slu->req_trans == NULL) {
679         printf("libusb_alloc_transfer\n");
680         goto on_libusb_error;
681     }
682
683     slu->rep_trans = libusb_alloc_transfer(0);
684     if (slu->rep_trans == NULL) {
685         printf("libusb_alloc_transfer\n");
686         goto on_libusb_error;
687     }
688
689     slu->ep_rep = 1 /* ep rep */ | LIBUSB_ENDPOINT_IN;
690     slu->ep_req = 2 /* ep req */ | LIBUSB_ENDPOINT_OUT;
691
692     slu->sg_transfer_idx = 0;
693     slu->cmd_len = (slu->protocoll == 1)? STLINK_SG_SIZE: STLINK_CMD_SIZE;
694
695     /* success */
696     if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE) {
697       printf("-- exit_dfu_mode\n");
698       stlink_exit_dfu_mode(sl);
699     }
700     stlink_version(sl);
701     error = 0;
702
703 on_libusb_error:
704     if (devs != NULL) {
705         libusb_free_device_list(devs, 1);
706     }
707
708     if (error == -1) {
709         stlink_close(sl);
710         return NULL;
711     }
712
713     /* success */
714     return sl;
715
716 on_error:
717     if (sl != NULL) free(sl);
718     if (slu != NULL) free(slu);
719     return 0;
720 }
721