STLINK: swd transport renamed and jtag+swim transport added
[fw/openocd] / src / jtag / drivers / stlink_usb.c
1 /***************************************************************************
2  *   Copyright (C) 2011 by Mathias Kuester                                 *
3  *   Mathias Kuester <kesmtp@freenet.de>                                   *
4  *                                                                         *
5  *   This code is based on https://github.com/texane/stlink                *
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  *   This program is distributed in the hope that it will be useful,       *
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15  *   GNU General Public License for more details.                          *
16  *                                                                         *
17  *   You should have received a copy of the GNU General Public License     *
18  *   along with this program; if not, write to the                         *
19  *   Free Software Foundation, Inc.,                                       *
20  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
21  ***************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 /* project specific includes */
28 #include <helper/binarybuffer.h>
29 #include <jtag/interface.h>
30 #include <jtag/stlink/stlink_layout.h>
31 #include <jtag/stlink/stlink_transport.h>
32 #include <jtag/stlink/stlink_interface.h>
33 #include <target/target.h>
34
35 #include "libusb_common.h"
36
37 #define ENDPOINT_IN     0x80
38 #define ENDPOINT_OUT    0x00
39
40 #define STLINK_RX_EP    (1|ENDPOINT_IN)
41 #define STLINK_TX_EP    (2|ENDPOINT_OUT)
42 #define STLINK_CMD_SIZE (16)
43 #define STLINK_TX_SIZE  (4*128)
44 #define STLINK_RX_SIZE  (4*128)
45
46 /** */
47 struct stlink_usb_version {
48         /** */
49         int stlink;
50         /** */
51         int jtag;
52         /** */
53         int swim;
54 };
55
56 /** */
57 struct stlink_usb_handle_s {
58         /** */
59         struct jtag_libusb_device_handle *fd;
60         /** */
61         struct libusb_transfer *trans;
62         /** */
63         uint8_t txbuf[STLINK_TX_SIZE];
64         /** */
65         uint8_t rxbuf[STLINK_RX_SIZE];
66         /** */
67         enum stlink_transports transport;
68         /** */
69         struct stlink_usb_version version;
70         /** */
71         uint16_t vid;
72         /** */
73         uint16_t pid;
74 };
75
76 #define STLINK_OK                       0x80
77 #define STLINK_FALSE                    0x81
78 #define STLINK_CORE_RUNNING             0x80
79 #define STLINK_CORE_HALTED              0x81
80 #define STLINK_CORE_STAT_UNKNOWN        -1
81
82 #define STLINK_GET_VERSION              0xF1
83 #define STLINK_DEBUG_COMMAND            0xF2
84 #define STLINK_DFU_COMMAND              0xF3
85 #define STLINK_SWIM_COMMAND             0xF4
86 #define STLINK_GET_CURRENT_MODE         0xF5
87
88 #define STLINK_DEV_DFU_MODE             0x00
89 #define STLINK_DEV_MASS_MODE            0x01
90 #define STLINK_DEV_DEBUG_MODE           0x02
91 #define STLINK_DEV_SWIM_MODE            0x03
92 #define STLINK_DEV_UNKNOWN_MODE         -1
93
94 #define STLINK_DFU_EXIT                 0x07
95
96 #define STLINK_SWIM_ENTER               0x00
97 #define STLINK_SWIM_EXIT                0x01
98
99 #define STLINK_DEBUG_ENTER_JTAG         0x00
100 #define STLINK_DEBUG_GETSTATUS          0x01
101 #define STLINK_DEBUG_FORCEDEBUG         0x02
102 #define STLINK_DEBUG_RESETSYS           0x03
103 #define STLINK_DEBUG_READALLREGS        0x04
104 #define STLINK_DEBUG_READREG            0x05
105 #define STLINK_DEBUG_WRITEREG           0x06
106 #define STLINK_DEBUG_READMEM_32BIT      0x07
107 #define STLINK_DEBUG_WRITEMEM_32BIT     0x08
108 #define STLINK_DEBUG_RUNCORE            0x09
109 #define STLINK_DEBUG_STEPCORE           0x0a
110 #define STLINK_DEBUG_SETFP              0x0b
111 #define STLINK_DEBUG_READMEM_8BIT       0x0c
112 #define STLINK_DEBUG_WRITEMEM_8BIT      0x0d
113 #define STLINK_DEBUG_CLEARFP            0x0e
114 #define STLINK_DEBUG_WRITEDEBUGREG      0x0f
115
116 #define STLINK_DEBUG_ENTER_JTAG         0x00
117 #define STLINK_DEBUG_ENTER_SWD          0xa3
118
119 #define STLINK_DEBUG_ENTER              0x20
120 #define STLINK_DEBUG_EXIT               0x21
121 #define STLINK_DEBUG_READCOREID         0x22
122
123 /** */
124 enum stlink_mode {
125         STLINK_MODE_UNKNOWN = 0,
126         STLINK_MODE_DFU,
127         STLINK_MODE_MASS,
128         STLINK_MODE_DEBUG_JTAG,
129         STLINK_MODE_DEBUG_SWD,
130         STLINK_MODE_DEBUG_SWIM
131 };
132
133 /** */
134 static int stlink_usb_recv(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
135                     int rxsize)
136 {
137         struct stlink_usb_handle_s *h;
138
139         assert(handle != NULL);
140
141         h = (struct stlink_usb_handle_s *)handle;
142
143         if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)txbuf, txsize,
144                                    1000) != txsize) {
145                 return ERROR_FAIL;
146         }
147         if (rxsize && rxbuf) {
148                 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)rxbuf,
149                                           rxsize, 1000) != rxsize) {
150                         return ERROR_FAIL;
151                 }
152         }
153         return ERROR_OK;
154 }
155
156 /** */
157 static void stlink_usb_init_buffer(void *handle)
158 {
159         struct stlink_usb_handle_s *h;
160
161         assert(handle != NULL);
162
163         h = (struct stlink_usb_handle_s *)handle;
164
165         memset(h->txbuf, 0, STLINK_CMD_SIZE);
166 }
167
168 /** */
169 static int stlink_usb_version(void *handle)
170 {
171         int res;
172         uint16_t v;
173         struct stlink_usb_handle_s *h;
174
175         assert(handle != NULL);
176
177         h = (struct stlink_usb_handle_s *)handle;
178
179         stlink_usb_init_buffer(handle);
180
181         h->txbuf[0] = STLINK_GET_VERSION;
182
183         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 6);
184
185         if (res != ERROR_OK)
186                 return res;
187
188         v = (h->rxbuf[0] << 8) | h->rxbuf[1];
189
190         h->version.stlink = (v >> 12) & 0x0f;
191         h->version.jtag = (v >> 6) & 0x3f;
192         h->version.swim = v & 0x3f;
193         h->vid = buf_get_u32(h->rxbuf, 16, 16);
194         h->pid = buf_get_u32(h->rxbuf, 32, 16);
195
196         LOG_DEBUG("STLINK v%d JTAG v%d SWIM v%d VID %04X PID %04X",
197                 h->version.stlink,
198                 h->version.jtag,
199                 h->version.swim,
200                 h->vid,
201                 h->pid);
202
203         return ERROR_OK;
204 }
205
206 /** */
207 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
208 {
209         int res;
210         struct stlink_usb_handle_s *h;
211
212         assert(handle != NULL);
213
214         h = (struct stlink_usb_handle_s *)handle;
215
216         stlink_usb_init_buffer(handle);
217
218         h->txbuf[0] = STLINK_GET_CURRENT_MODE;
219
220         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
221
222         if (res != ERROR_OK)
223                 return res;
224
225         *mode = h->rxbuf[0];
226
227         return ERROR_OK;
228 }
229
230 /** */
231 static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
232 {
233         int res;
234         struct stlink_usb_handle_s *h;
235
236         assert(handle != NULL);
237
238         h = (struct stlink_usb_handle_s *)handle;
239
240         stlink_usb_init_buffer(handle);
241
242         switch (type) {
243                 case STLINK_MODE_DEBUG_JTAG:
244                         h->txbuf[0] = STLINK_DEBUG_COMMAND;
245                         h->txbuf[1] = STLINK_DEBUG_ENTER;
246                         h->txbuf[2] = STLINK_DEBUG_ENTER_JTAG;
247                         break;
248                 case STLINK_MODE_DEBUG_SWD:
249                         h->txbuf[0] = STLINK_DEBUG_COMMAND;
250                         h->txbuf[1] = STLINK_DEBUG_ENTER;
251                         h->txbuf[2] = STLINK_DEBUG_ENTER_SWD;
252                         break;
253                 case STLINK_MODE_DEBUG_SWIM:
254                         h->txbuf[0] = STLINK_SWIM_COMMAND;
255                         h->txbuf[1] = STLINK_SWIM_ENTER;
256                         break;
257                 case STLINK_MODE_DFU:
258                 case STLINK_MODE_MASS:
259                 default:
260                         return ERROR_FAIL;
261         }
262
263         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
264         if (res != ERROR_OK)
265                 return res;
266
267         return ERROR_OK;
268 }
269
270 /** */
271 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
272 {
273         int res;
274         struct stlink_usb_handle_s *h;
275
276         assert(handle != NULL);
277
278         h = (struct stlink_usb_handle_s *)handle;
279
280         stlink_usb_init_buffer(handle);
281
282         switch (type) {
283                 case STLINK_MODE_DEBUG_JTAG:
284                 case STLINK_MODE_DEBUG_SWD:
285                         h->txbuf[0] = STLINK_DEBUG_COMMAND;
286                         h->txbuf[1] = STLINK_DEBUG_EXIT;
287                         break;
288                 case STLINK_MODE_DEBUG_SWIM:
289                         h->txbuf[0] = STLINK_SWIM_COMMAND;
290                         h->txbuf[1] = STLINK_SWIM_EXIT;
291                         break;
292                 case STLINK_MODE_DFU:
293                         h->txbuf[0] = STLINK_DFU_COMMAND;
294                         h->txbuf[1] = STLINK_DFU_EXIT;
295                         break;
296                 case STLINK_MODE_MASS:
297                 default:
298                         return ERROR_FAIL;
299         }
300
301         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
302         if (res != ERROR_OK)
303                 return res;
304
305         return ERROR_OK;
306 }
307
308 /** */
309 static int stlink_usb_init_mode(void *handle)
310 {
311         int res;
312         uint8_t mode;
313         enum stlink_mode emode;
314         struct stlink_usb_handle_s *h;
315
316         assert(handle != NULL);
317
318         h = (struct stlink_usb_handle_s *)handle;
319
320         res = stlink_usb_current_mode(handle, &mode);
321
322         if (res != ERROR_OK)
323                 return res;
324
325         LOG_DEBUG("MODE: %02X", mode);
326
327         /* try to exit current mode */
328         switch (mode) {
329                 case STLINK_DEV_DFU_MODE:
330                         emode = STLINK_MODE_DFU;
331                         break;
332                 case STLINK_DEV_DEBUG_MODE:
333                         emode = STLINK_MODE_DEBUG_SWD;
334                         break;
335                 case STLINK_DEV_SWIM_MODE:
336                         emode = STLINK_MODE_DEBUG_SWIM;
337                         break;
338                 default:
339                         emode = STLINK_MODE_UNKNOWN;
340                         break;
341         }
342
343         if (emode != STLINK_MODE_UNKNOWN) {
344                 res = stlink_usb_mode_leave(handle, emode);
345
346                 if (res != ERROR_OK)
347                         return res;
348         }
349
350         res = stlink_usb_current_mode(handle, &mode);
351
352         if (res != ERROR_OK)
353                 return res;
354
355         LOG_DEBUG("MODE: %02X", mode);
356
357         /* set selected mode */
358         switch (h->transport) {
359                 case STLINK_TRANSPORT_SWD:
360                         emode = STLINK_MODE_DEBUG_SWD;
361                         break;
362                 case STLINK_TRANSPORT_JTAG:
363                         emode = STLINK_MODE_DEBUG_JTAG;
364                         break;
365                 case STLINK_TRANSPORT_SWIM:
366                 default:
367                         emode = STLINK_MODE_UNKNOWN;
368                         break;
369         }
370
371         if (emode == STLINK_MODE_UNKNOWN) {
372                 LOG_ERROR("selected mode (transport) not supported");
373                 return ERROR_FAIL;
374         }
375
376         res = stlink_usb_mode_enter(handle, emode);
377
378         if (res != ERROR_OK)
379                 return res;
380
381         res = stlink_usb_current_mode(handle, &mode);
382
383         if (res != ERROR_OK)
384                 return res;
385
386         LOG_DEBUG("MODE: %02X", mode);
387
388         return ERROR_OK;
389 }
390
391 /** */
392 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
393 {
394         int res;
395         struct stlink_usb_handle_s *h;
396
397         assert(handle != NULL);
398
399         h = (struct stlink_usb_handle_s *)handle;
400
401         stlink_usb_init_buffer(handle);
402
403         h->txbuf[0] = STLINK_DEBUG_COMMAND;
404         h->txbuf[1] = STLINK_DEBUG_READCOREID;
405
406         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 4);
407
408         if (res != ERROR_OK)
409                 return res;
410
411         *idcode = le_to_h_u32(h->rxbuf);
412
413         LOG_DEBUG("IDCODE: %08X", *idcode);
414
415         return ERROR_OK;
416 }
417
418 /** */
419 static enum target_state stlink_usb_state(void *handle)
420 {
421         int res;
422         struct stlink_usb_handle_s *h;
423
424         assert(handle != NULL);
425
426         h = (struct stlink_usb_handle_s *)handle;
427
428         stlink_usb_init_buffer(handle);
429
430         h->txbuf[0] = STLINK_DEBUG_COMMAND;
431         h->txbuf[1] = STLINK_DEBUG_GETSTATUS;
432
433         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
434
435         if (res != ERROR_OK)
436                 return TARGET_UNKNOWN;
437
438         if (h->rxbuf[0] == STLINK_CORE_RUNNING)
439                 return TARGET_RUNNING;
440         if (h->rxbuf[0] == STLINK_CORE_HALTED)
441                 return TARGET_HALTED;
442
443         return TARGET_UNKNOWN;
444 }
445
446 /** */
447 static int stlink_usb_reset(void *handle)
448 {
449         int res;
450         struct stlink_usb_handle_s *h;
451
452         assert(handle != NULL);
453
454         h = (struct stlink_usb_handle_s *)handle;
455
456         stlink_usb_init_buffer(handle);
457
458         h->txbuf[0] = STLINK_DEBUG_COMMAND;
459         h->txbuf[1] = STLINK_DEBUG_RESETSYS;
460
461         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
462
463         if (res != ERROR_OK)
464                 return res;
465
466         LOG_DEBUG("RESET: %08X", h->rxbuf[0]);
467
468         return ERROR_OK;
469 }
470
471 /** */
472 static int stlink_usb_run(void *handle)
473 {
474         int res;
475         struct stlink_usb_handle_s *h;
476
477         assert(handle != NULL);
478
479         h = (struct stlink_usb_handle_s *)handle;
480
481         stlink_usb_init_buffer(handle);
482
483         h->txbuf[0] = STLINK_DEBUG_COMMAND;
484         h->txbuf[1] = STLINK_DEBUG_RUNCORE;
485
486         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
487
488         if (res != ERROR_OK)
489                 return res;
490
491         return ERROR_OK;
492 }
493
494 /** */
495 static int stlink_usb_halt(void *handle)
496 {
497         int res;
498         struct stlink_usb_handle_s *h;
499
500         assert(handle != NULL);
501
502         h = (struct stlink_usb_handle_s *)handle;
503
504         stlink_usb_init_buffer(handle);
505
506         h->txbuf[0] = STLINK_DEBUG_COMMAND;
507         h->txbuf[1] = STLINK_DEBUG_FORCEDEBUG;
508
509         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
510
511         if (res != ERROR_OK)
512                 return res;
513
514         return ERROR_OK;
515 }
516
517 /** */
518 static int stlink_usb_step(void *handle)
519 {
520         int res;
521         struct stlink_usb_handle_s *h;
522
523         assert(handle != NULL);
524
525         h = (struct stlink_usb_handle_s *)handle;
526
527         stlink_usb_init_buffer(handle);
528
529         h->txbuf[0] = STLINK_DEBUG_COMMAND;
530         h->txbuf[1] = STLINK_DEBUG_STEPCORE;
531
532         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
533
534         if (res != ERROR_OK)
535                 return res;
536
537         return ERROR_OK;
538 }
539
540 /** */
541 static int stlink_usb_read_regs(void *handle)
542 {
543         int res;
544         struct stlink_usb_handle_s *h;
545
546         assert(handle != NULL);
547
548         h = (struct stlink_usb_handle_s *)handle;
549
550         stlink_usb_init_buffer(handle);
551
552         h->txbuf[0] = STLINK_DEBUG_COMMAND;
553         h->txbuf[1] = STLINK_DEBUG_READALLREGS;
554
555         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 84);
556
557         if (res != ERROR_OK)
558                 return res;
559
560         return ERROR_OK;
561 }
562
563 /** */
564 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
565 {
566         int res;
567         struct stlink_usb_handle_s *h;
568
569         assert(handle != NULL);
570
571         h = (struct stlink_usb_handle_s *)handle;
572
573         stlink_usb_init_buffer(handle);
574
575         h->txbuf[0] = STLINK_DEBUG_COMMAND;
576         h->txbuf[1] = STLINK_DEBUG_READREG;
577         h->txbuf[2] = num;
578
579         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 4);
580
581         if (res != ERROR_OK)
582                 return res;
583
584         *val = le_to_h_u32(h->rxbuf);
585
586         return ERROR_OK;
587 }
588
589 /** */
590 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
591 {
592         int res;
593         struct stlink_usb_handle_s *h;
594
595         assert(handle != NULL);
596
597         h = (struct stlink_usb_handle_s *)handle;
598
599         stlink_usb_init_buffer(handle);
600
601         h->txbuf[0] = STLINK_DEBUG_COMMAND;
602         h->txbuf[1] = STLINK_DEBUG_WRITEREG;
603         h->txbuf[2] = num;
604         h_u32_to_le(h->txbuf + 3, val);
605
606         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
607
608         if (res != ERROR_OK)
609                 return res;
610
611         return ERROR_OK;
612 }
613
614 /** */
615 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
616                           uint8_t *buffer)
617 {
618         int res;
619         uint16_t read_len = len;
620         struct stlink_usb_handle_s *h;
621
622         assert(handle != NULL);
623
624         h = (struct stlink_usb_handle_s *)handle;
625
626         stlink_usb_init_buffer(handle);
627
628         h->txbuf[0] = STLINK_DEBUG_COMMAND;
629         h->txbuf[1] = STLINK_DEBUG_READMEM_8BIT;
630         h_u32_to_le(h->txbuf + 2, addr);
631         h_u16_to_le(h->txbuf + 2 + 4, len);
632
633         /* we need to fix read length for single bytes */
634         if (read_len == 1)
635                 read_len++;
636
637         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, read_len);
638
639         if (res != ERROR_OK)
640                 return res;
641
642         memcpy(buffer, h->rxbuf, len);
643
644         return ERROR_OK;
645 }
646
647 /** */
648 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
649                            const uint8_t *buffer)
650 {
651         int res;
652         struct stlink_usb_handle_s *h;
653
654         assert(handle != NULL);
655
656         h = (struct stlink_usb_handle_s *)handle;
657
658         stlink_usb_init_buffer(handle);
659
660         h->txbuf[0] = STLINK_DEBUG_COMMAND;
661         h->txbuf[1] = STLINK_DEBUG_WRITEMEM_8BIT;
662         h_u32_to_le(h->txbuf + 2, addr);
663         h_u16_to_le(h->txbuf + 2 + 4, len);
664
665         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
666
667         if (res != ERROR_OK)
668                 return res;
669
670         res = stlink_usb_recv(handle, (uint8_t *) buffer, len, 0, 0);
671
672         if (res != ERROR_OK)
673                 return res;
674
675         return ERROR_OK;
676 }
677
678 /** */
679 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
680                           uint32_t *buffer)
681 {
682         int res;
683         struct stlink_usb_handle_s *h;
684
685         assert(handle != NULL);
686
687         h = (struct stlink_usb_handle_s *)handle;
688
689         stlink_usb_init_buffer(handle);
690
691         len *= 4;
692
693         h->txbuf[0] = STLINK_DEBUG_COMMAND;
694         h->txbuf[1] = STLINK_DEBUG_READMEM_32BIT;
695         h_u32_to_le(h->txbuf + 2, addr);
696         h_u16_to_le(h->txbuf + 2 + 4, len);
697
698         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, len);
699
700         if (res != ERROR_OK)
701                 return res;
702
703         memcpy(buffer, h->rxbuf, len);
704
705         return ERROR_OK;
706 }
707
708 /** */
709 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
710                            const uint32_t *buffer)
711 {
712         int res;
713         struct stlink_usb_handle_s *h;
714
715         assert(handle != NULL);
716
717         h = (struct stlink_usb_handle_s *)handle;
718
719         stlink_usb_init_buffer(handle);
720
721         len *= 4;
722
723         h->txbuf[0] = STLINK_DEBUG_COMMAND;
724         h->txbuf[1] = STLINK_DEBUG_WRITEMEM_32BIT;
725         h_u32_to_le(h->txbuf + 2, addr);
726         h_u16_to_le(h->txbuf + 2 + 4, len);
727
728         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
729
730         if (res != ERROR_OK)
731                 return res;
732
733         res = stlink_usb_recv(handle, (uint8_t *) buffer, len, 0, 0);
734
735         if (res != ERROR_OK)
736                 return res;
737
738         return ERROR_OK;
739 }
740
741 /** */
742 static int stlink_usb_open(struct stlink_interface_param_s *param, void **fd)
743 {
744         int err;
745         struct stlink_usb_handle_s *h;
746
747         LOG_DEBUG("stlink_usb_open");
748
749         h = malloc(sizeof(struct stlink_usb_handle_s));
750
751         if (h == 0) {
752                 LOG_DEBUG("malloc failed");
753                 return ERROR_FAIL;
754         }
755
756         h->transport = param->transport;
757
758         const uint16_t vids[] = { param->vid, 0 };
759         const uint16_t pids[] = { param->pid, 0 };
760
761         LOG_DEBUG("vid: %04x pid: %04x", param->vid,
762                   param->pid);
763
764         if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
765                 LOG_ERROR("open failed");
766                 return ERROR_FAIL;
767         }
768
769         jtag_libusb_set_configuration(h->fd, 0);
770
771         if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
772                 LOG_DEBUG("claim interface failed");
773                 return ERROR_FAIL;
774         }
775
776         /* get the device version */
777         err = stlink_usb_version(h);
778
779         if (err != ERROR_OK) {
780                 LOG_ERROR("read version failed");
781                 jtag_libusb_close(h->fd);
782                 free(h);
783                 return err;
784         }
785
786         /* compare usb vid/pid */
787         if ((param->vid != h->vid) || (param->pid != h->pid))
788                 LOG_INFO("vid/pid are not identical: %04X/%04X %04X/%04X",
789                         param->vid, param->pid,
790                         h->vid, h->pid);
791
792         /* check if mode is supported */
793         err = ERROR_OK;
794
795         switch (h->transport) {
796                 case STLINK_TRANSPORT_SWD:
797                 case STLINK_TRANSPORT_JTAG:
798                         if (h->version.jtag == 0)
799                                 err = ERROR_FAIL;
800                         break;
801                 case STLINK_TRANSPORT_SWIM:
802                         if (h->version.swim == 0)
803                                 err = ERROR_FAIL;
804                         break;
805                 default:
806                         err = ERROR_FAIL;
807                         break;
808         }
809
810         if (err != ERROR_OK) {
811                 LOG_ERROR("mode (transport) not supported by device");
812                 jtag_libusb_close(h->fd);
813                 free(h);
814                 return err;
815         }
816
817         err = stlink_usb_init_mode(h);
818
819         if (err != ERROR_OK) {
820                 LOG_ERROR("init mode failed");
821                 jtag_libusb_close(h->fd);
822                 free(h);
823                 return err;
824         }
825
826         *fd = h;
827
828         return ERROR_OK;
829 }
830
831 /** */
832 static int stlink_usb_close(void *fd)
833 {
834         return ERROR_OK;
835 }
836
837 /** */
838 struct stlink_layout_api_s stlink_usb_layout_api = {
839         /** */
840         .open = stlink_usb_open,
841         /** */
842         .close = stlink_usb_close,
843         /** */
844         .idcode = stlink_usb_idcode,
845         /** */
846         .state = stlink_usb_state,
847         /** */
848         .reset = stlink_usb_reset,
849         /** */
850         .run = stlink_usb_run,
851         /** */
852         .halt = stlink_usb_halt,
853         /** */
854         .step = stlink_usb_step,
855         /** */
856         .read_regs = stlink_usb_read_regs,
857         /** */
858         .read_reg = stlink_usb_read_reg,
859         /** */
860         .write_reg = stlink_usb_write_reg,
861         /** */
862         .read_mem8 = stlink_usb_read_mem8,
863         /** */
864         .write_mem8 = stlink_usb_write_mem8,
865         /** */
866         .read_mem32 = stlink_usb_read_mem32,
867         /** */
868         .write_mem32 = stlink_usb_write_mem32,
869 };