]> git.sur5r.net Git - openocd/blob - src/jtag/drivers/vsllink.c
Remove unnecessary casts
[openocd] / src / jtag / drivers / vsllink.c
1 /***************************************************************************
2  *   Copyright (C) 2009-2010 by Simon Qian <SimonQian@SimonQian.com>       *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
18  ***************************************************************************/
19
20 /* Versaloon is a programming tool for multiple MCUs.
21  * It's distributed under GPLv3.
22  * You can find it at http://www.Versaloon.com/.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <jtag/interface.h>
30 #include <jtag/commands.h>
31 #include "usb_common.h"
32
33 #include "versaloon/versaloon_include.h"
34 #include "versaloon/versaloon.h"
35
36 static int vsllink_tms_offset;
37
38 struct pending_scan_result {
39         int src_offset;
40         int dest_offset;
41         int length;     /* Number of bits to read */
42         struct scan_command *command;   /* Corresponding scan command */
43         uint8_t *ack;
44         uint8_t *buffer;
45         bool last;      /* indicate the last scan pending */
46 };
47
48 #define MAX_PENDING_SCAN_RESULTS 256
49
50 static int pending_scan_results_length;
51 static struct pending_scan_result
52         pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
53
54 /* Queue command functions */
55 static void vsllink_end_state(tap_state_t state);
56 static void vsllink_state_move(void);
57 static void vsllink_path_move(int num_states, tap_state_t *path);
58 static void vsllink_tms(int num_bits, const uint8_t *bits);
59 static void vsllink_runtest(int num_cycles);
60 static void vsllink_stableclocks(int num_cycles, int tms);
61 static void vsllink_scan(bool ir_scan, enum scan_type type,
62                 uint8_t *buffer, int scan_size, struct scan_command *command);
63 static void vsllink_reset(int trst, int srst);
64
65 /* VSLLink tap buffer functions */
66 static void vsllink_tap_append_step(int tms, int tdi);
67 static void vsllink_tap_init(void);
68 static int  vsllink_tap_execute(void);
69 static void vsllink_tap_ensure_pending(int scans);
70 static void vsllink_tap_append_scan(int length, uint8_t *buffer,
71                 struct scan_command *command);
72
73 /* VSLLink lowlevel functions */
74 struct vsllink {
75         struct usb_dev_handle *usb_handle;
76 };
77
78 static struct vsllink *vsllink_usb_open(void);
79 static void vsllink_usb_close(struct vsllink *vsllink);
80
81 #if defined _DEBUG_JTAG_IO_
82 static void vsllink_debug_buffer(uint8_t *buffer, int length);
83 #endif
84
85 static int tap_length;
86 static int tap_buffer_size;
87 static uint8_t *tms_buffer;
88 static uint8_t *tdi_buffer;
89 static uint8_t *tdo_buffer;
90
91 struct vsllink *vsllink_handle;
92
93 static int vsllink_execute_queue(void)
94 {
95         struct jtag_command *cmd = jtag_command_queue;
96         int scan_size;
97         enum scan_type type;
98         uint8_t *buffer;
99
100         DEBUG_JTAG_IO("-------------------------------------"
101                 " vsllink "
102                 "-------------------------------------");
103
104         while (cmd != NULL) {
105                 switch (cmd->type) {
106                         case JTAG_RUNTEST:
107                                 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
108                                                 cmd->cmd.runtest->num_cycles,
109                                                 tap_state_name(cmd->cmd.runtest->end_state));
110
111                                 vsllink_end_state(cmd->cmd.runtest->end_state);
112                                 vsllink_runtest(cmd->cmd.runtest->num_cycles);
113                                 break;
114
115                         case JTAG_TLR_RESET:
116                                 DEBUG_JTAG_IO("statemove end in %s",
117                                                 tap_state_name(cmd->cmd.statemove->end_state));
118
119                                 vsllink_end_state(cmd->cmd.statemove->end_state);
120                                 vsllink_state_move();
121                                 break;
122
123                         case JTAG_PATHMOVE:
124                                 DEBUG_JTAG_IO("pathmove: %i states, end in %s",
125                                                 cmd->cmd.pathmove->num_states,
126                                                 tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
127
128                                 vsllink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
129                                 break;
130
131                         case JTAG_SCAN:
132                                 DEBUG_JTAG_IO("JTAG Scan...");
133
134                                 vsllink_end_state(cmd->cmd.scan->end_state);
135
136                                 scan_size = jtag_build_buffer(
137                                                 cmd->cmd.scan, &buffer);
138
139                                 if (cmd->cmd.scan->ir_scan)
140                                         DEBUG_JTAG_IO(
141                                                         "JTAG Scan write IR(%d bits), "
142                                                         "end in %s:",
143                                                         scan_size,
144                                                         tap_state_name(cmd->cmd.scan->end_state));
145
146                                 else
147                                         DEBUG_JTAG_IO(
148                                                         "JTAG Scan write DR(%d bits), "
149                                                         "end in %s:",
150                                                         scan_size,
151                                                         tap_state_name(cmd->cmd.scan->end_state));
152
153 #ifdef _DEBUG_JTAG_IO_
154                                 vsllink_debug_buffer(buffer,
155                                                 DIV_ROUND_UP(scan_size, 8));
156 #endif
157
158                                 type = jtag_scan_type(cmd->cmd.scan);
159
160                                 vsllink_scan(cmd->cmd.scan->ir_scan,
161                                                 type, buffer, scan_size,
162                                                 cmd->cmd.scan);
163                                 break;
164
165                         case JTAG_RESET:
166                                 DEBUG_JTAG_IO("reset trst: %i srst %i",
167                                                 cmd->cmd.reset->trst,
168                                                 cmd->cmd.reset->srst);
169
170                                 vsllink_tap_execute();
171
172                                 if (cmd->cmd.reset->trst == 1)
173                                         tap_set_state(TAP_RESET);
174
175                                 vsllink_reset(cmd->cmd.reset->trst,
176                                                 cmd->cmd.reset->srst);
177                                 break;
178
179                         case JTAG_SLEEP:
180                                 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
181                                 vsllink_tap_execute();
182                                 jtag_sleep(cmd->cmd.sleep->us);
183                                 break;
184
185                         case JTAG_STABLECLOCKS:
186                                 DEBUG_JTAG_IO("add %d clocks",
187                                                 cmd->cmd.stableclocks->num_cycles);
188
189                                 switch (tap_get_state()) {
190                                 case TAP_RESET:
191                                         /* tms must be '1' to stay
192                                          * n TAP_RESET mode
193                                          */
194                                         scan_size = 1;
195                                         break;
196                                 case TAP_DRSHIFT:
197                                 case TAP_IDLE:
198                                 case TAP_DRPAUSE:
199                                 case TAP_IRSHIFT:
200                                 case TAP_IRPAUSE:
201                                         /* else, tms should be '0' */
202                                         scan_size = 0;
203                                         break;
204                                 /* above stable states are OK */
205                                 default:
206                                         LOG_ERROR("jtag_add_clocks() "
207                                                         "in non-stable state \"%s\"",
208                                                         tap_state_name(tap_get_state())
209                                         );
210                                         exit(-1);
211                                 }
212                                 vsllink_stableclocks(cmd->cmd.stableclocks->num_cycles, scan_size);
213                                 break;
214
215                                 case JTAG_TMS:
216                                         DEBUG_JTAG_IO("add %d jtag tms",
217                                                         cmd->cmd.tms->num_bits);
218
219                                         vsllink_tms(cmd->cmd.tms->num_bits, cmd->cmd.tms->bits);
220                                         break;
221
222                                 default:
223                                         LOG_ERROR("BUG: unknown JTAG command type "
224                                                         "encountered: %d", cmd->type);
225                                         exit(-1);
226                 }
227                 cmd = cmd->next;
228         }
229
230         return vsllink_tap_execute();
231 }
232
233 static int vsllink_speed(int speed)
234 {
235         versaloon_interface.adaptors.jtag_raw.config(0, (uint16_t)speed);
236         return versaloon_interface.adaptors.peripheral_commit();
237 }
238
239 static int vsllink_khz(int khz, int *jtag_speed)
240 {
241         *jtag_speed = khz;
242
243         return ERROR_OK;
244 }
245
246 static int vsllink_speed_div(int jtag_speed, int *khz)
247 {
248         *khz = jtag_speed;
249
250         return ERROR_OK;
251 }
252
253 static void vsllink_free_buffer(void)
254 {
255         if (tdi_buffer != NULL) {
256                 free(tdi_buffer);
257                 tdi_buffer = NULL;
258         }
259         if (tdo_buffer != NULL) {
260                 free(tdo_buffer);
261                 tdo_buffer = NULL;
262         }
263         if (tms_buffer != NULL) {
264                 free(tms_buffer);
265                 tms_buffer = NULL;
266         }
267 }
268
269 static int vsllink_quit(void)
270 {
271         versaloon_interface.adaptors.gpio.config(0, GPIO_SRST | GPIO_TRST,
272                 0, 0, GPIO_SRST | GPIO_TRST);
273         versaloon_interface.adaptors.gpio.fini(0);
274         versaloon_interface.adaptors.jtag_raw.fini(0);
275         versaloon_interface.adaptors.peripheral_commit();
276         versaloon_interface.fini();
277
278         vsllink_free_buffer();
279         vsllink_usb_close(vsllink_handle);
280
281         return ERROR_OK;
282 }
283
284 static int vsllink_init(void)
285 {
286         vsllink_handle = vsllink_usb_open();
287         if (vsllink_handle == 0) {
288                 LOG_ERROR("Can't find USB JTAG Interface!" \
289                         "Please check connection and permissions.");
290                 return ERROR_JTAG_INIT_FAILED;
291         }
292         LOG_DEBUG("vsllink found on %04X:%04X",
293                 versaloon_interface.usb_setting.vid,
294                 versaloon_interface.usb_setting.pid);
295         versaloon_usb_device_handle = vsllink_handle->usb_handle;
296
297         if (ERROR_OK != versaloon_interface.init())
298                 return ERROR_FAIL;
299         if (versaloon_interface.usb_setting.buf_size < 32) {
300                 versaloon_interface.fini();
301                 return ERROR_FAIL;
302         }
303
304         /* malloc buffer size for tap */
305         tap_buffer_size = versaloon_interface.usb_setting.buf_size / 2 - 32;
306         vsllink_free_buffer();
307         tdi_buffer = malloc(tap_buffer_size);
308         tdo_buffer = malloc(tap_buffer_size);
309         tms_buffer = malloc(tap_buffer_size);
310         if ((NULL == tdi_buffer) || (NULL == tdo_buffer) || (NULL == tms_buffer)) {
311                 vsllink_quit();
312                 return ERROR_FAIL;
313         }
314
315         versaloon_interface.adaptors.jtag_raw.init(0);
316         versaloon_interface.adaptors.jtag_raw.config(0, jtag_get_speed_khz());
317         versaloon_interface.adaptors.gpio.init(0);
318         versaloon_interface.adaptors.gpio.config(0, GPIO_SRST | GPIO_TRST,
319                 GPIO_TRST, GPIO_SRST, GPIO_SRST);
320         if (ERROR_OK != versaloon_interface.adaptors.peripheral_commit())
321                 return ERROR_FAIL;
322
323         vsllink_reset(0, 0);
324         vsllink_tap_init();
325         return ERROR_OK;
326 }
327
328 /**************************************************************************
329  * Queue command implementations */
330
331 static void vsllink_end_state(tap_state_t state)
332 {
333         if (tap_is_state_stable(state))
334                 tap_set_end_state(state);
335         else {
336                 LOG_ERROR("BUG: %i is not a valid end state", state);
337                 exit(-1);
338         }
339 }
340
341 /* Goes to the end state. */
342 static void vsllink_state_move(void)
343 {
344         int i;
345         uint8_t tms_scan = tap_get_tms_path(tap_get_state(),
346                         tap_get_end_state());
347         uint8_t tms_scan_bits = tap_get_tms_path_len(tap_get_state(),
348                         tap_get_end_state());
349
350         for (i = 0; i < tms_scan_bits; i++)
351                 vsllink_tap_append_step((tms_scan >> i) & 1, 0);
352
353         tap_set_state(tap_get_end_state());
354 }
355
356 static void vsllink_path_move(int num_states, tap_state_t *path)
357 {
358         for (int i = 0; i < num_states; i++) {
359                 if (path[i] == tap_state_transition(tap_get_state(), false))
360                         vsllink_tap_append_step(0, 0);
361                 else if (path[i] == tap_state_transition(tap_get_state(), true))
362                         vsllink_tap_append_step(1, 0);
363                 else {
364                         LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
365                                 tap_state_name(tap_get_state()),
366                                 tap_state_name(path[i]));
367                         exit(-1);
368                 }
369
370                 tap_set_state(path[i]);
371         }
372
373         tap_set_end_state(tap_get_state());
374 }
375
376 static void vsllink_tms(int num_bits, const uint8_t *bits)
377 {
378         for (int i = 0; i < num_bits; i++)
379                 vsllink_tap_append_step((bits[i / 8] >> (i % 8)) & 1, 0);
380 }
381
382 static void vsllink_stableclocks(int num_cycles, int tms)
383 {
384         while (num_cycles > 0) {
385                 vsllink_tap_append_step(tms, 0);
386                 num_cycles--;
387         }
388 }
389
390 static void vsllink_runtest(int num_cycles)
391 {
392         tap_state_t saved_end_state = tap_get_end_state();
393
394         if (tap_get_state() != TAP_IDLE) {
395                 /* enter IDLE state */
396                 vsllink_end_state(TAP_IDLE);
397                 vsllink_state_move();
398         }
399
400         vsllink_stableclocks(num_cycles, 0);
401
402         /* post-process */
403         /* set end_state */
404         vsllink_end_state(saved_end_state);
405         if (tap_get_end_state() != tap_get_end_state())
406                 vsllink_state_move();
407 }
408
409 static void vsllink_scan(bool ir_scan, enum scan_type type, uint8_t *buffer,
410         int scan_size, struct scan_command *command)
411 {
412         tap_state_t saved_end_state;
413
414         saved_end_state = tap_get_end_state();
415
416         /* Move to appropriate scan state */
417         vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
418
419         if (tap_get_state() != tap_get_end_state())
420                 vsllink_state_move();
421         vsllink_end_state(saved_end_state);
422
423         /* Scan */
424         vsllink_tap_append_scan(scan_size, buffer, command);
425
426         /* Goto Pause and record position to insert tms:0 */
427         vsllink_tap_append_step(0, 0);
428         vsllink_tms_offset = tap_length;
429
430         tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
431
432         if (tap_get_state() != tap_get_end_state())
433                 vsllink_state_move();
434 }
435
436 static void vsllink_reset(int trst, int srst)
437 {
438         LOG_DEBUG("trst: %i, srst: %i", trst, srst);
439
440         if (!srst)
441                 versaloon_interface.adaptors.gpio.config(0, GPIO_SRST, 0, GPIO_SRST, GPIO_SRST);
442         else
443                 versaloon_interface.adaptors.gpio.config(0, GPIO_SRST, GPIO_SRST, 0, 0);
444
445         if (!trst)
446                 versaloon_interface.adaptors.gpio.out(0, GPIO_TRST, GPIO_TRST);
447         else
448                 versaloon_interface.adaptors.gpio.out(0, GPIO_TRST, 0);
449         versaloon_interface.adaptors.peripheral_commit();
450 }
451
452 COMMAND_HANDLER(vsllink_handle_usb_vid_command)
453 {
454         if (CMD_ARGC != 1)
455                 return ERROR_COMMAND_SYNTAX_ERROR;
456
457         COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0],
458                 versaloon_interface.usb_setting.vid);
459         return ERROR_OK;
460 }
461
462 COMMAND_HANDLER(vsllink_handle_usb_pid_command)
463 {
464         if (CMD_ARGC != 1)
465                 return ERROR_COMMAND_SYNTAX_ERROR;
466         COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0],
467                 versaloon_interface.usb_setting.pid);
468         return ERROR_OK;
469 }
470
471 COMMAND_HANDLER(vsllink_handle_usb_bulkin_command)
472 {
473         if (CMD_ARGC != 1)
474                 return ERROR_COMMAND_SYNTAX_ERROR;
475
476         COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0],
477                 versaloon_interface.usb_setting.ep_in);
478
479         versaloon_interface.usb_setting.ep_in |= 0x80;
480
481         return ERROR_OK;
482 }
483
484 COMMAND_HANDLER(vsllink_handle_usb_bulkout_command)
485 {
486         if (CMD_ARGC != 1)
487                 return ERROR_COMMAND_SYNTAX_ERROR;
488
489         COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0],
490                 versaloon_interface.usb_setting.ep_out);
491
492         versaloon_interface.usb_setting.ep_out &= ~0x80;
493
494         return ERROR_OK;
495 }
496
497 COMMAND_HANDLER(vsllink_handle_usb_interface_command)
498 {
499         if (CMD_ARGC != 1)
500                 return ERROR_COMMAND_SYNTAX_ERROR;
501
502         COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0],
503                 versaloon_interface.usb_setting.interface);
504         return ERROR_OK;
505 }
506
507 /**************************************************************************
508  * VSLLink tap functions */
509
510 static void vsllink_tap_init(void)
511 {
512         tap_length = 0;
513         pending_scan_results_length = 0;
514         vsllink_tms_offset = 0;
515 }
516
517 static void vsllink_tap_ensure_pending(int scans)
518 {
519         int available_scans =
520                 MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
521
522         if (scans > available_scans)
523                 vsllink_tap_execute();
524 }
525
526 static void vsllink_tap_append_step(int tms, int tdi)
527 {
528         int index_var = tap_length / 8;
529
530         int bit_index = tap_length % 8;
531         uint8_t bit = 1 << bit_index;
532
533         if (tms)
534                 tms_buffer[index_var] |= bit;
535         else
536                 tms_buffer[index_var] &= ~bit;
537
538         if (tdi)
539                 tdi_buffer[index_var] |= bit;
540         else
541                 tdi_buffer[index_var] &= ~bit;
542
543         tap_length++;
544
545         if (tap_buffer_size * 8 <= tap_length)
546                 vsllink_tap_execute();
547 }
548
549 static void vsllink_tap_append_scan(int length, uint8_t *buffer,
550         struct scan_command *command)
551 {
552         struct pending_scan_result *pending_scan_result;
553         int len_tmp, len_all, i;
554
555         len_all = 0;
556         while (len_all < length) {
557                 vsllink_tap_ensure_pending(1);
558                 pending_scan_result =
559                         &pending_scan_results_buffer[
560                                 pending_scan_results_length];
561
562                 if ((length - len_all) > (tap_buffer_size * 8 - tap_length)) {
563                         /* Use all memory available
564                            vsllink_tap_append_step will commit automatically */
565                         len_tmp = tap_buffer_size * 8 - tap_length;
566                         pending_scan_result->last = false;
567                 } else {
568                         len_tmp = length - len_all;
569                         pending_scan_result->last = true;
570                 }
571                 pending_scan_result->src_offset = tap_length;
572                 pending_scan_result->dest_offset = len_all;
573                 pending_scan_result->length = len_tmp;
574                 pending_scan_result->command = command;
575                 pending_scan_result->buffer = buffer;
576                 pending_scan_results_length++;
577
578                 for (i = 0; i < len_tmp; i++) {
579                         vsllink_tap_append_step(((len_all + i) < length-1
580                                                  ? 0 : 1),
581                                 (buffer[(len_all + i)/8]
582                                  >> ((len_all + i)%8)) & 1);
583                 }
584
585                 len_all += len_tmp;
586         }
587 }
588
589 static int vsllink_jtag_execute(void)
590 {
591         int i;
592         int result;
593
594         if (tap_length <= 0)
595                 return ERROR_OK;
596
597         versaloon_interface.adaptors.jtag_raw.execute(0, tdi_buffer, tms_buffer,
598                 tdo_buffer, tap_length);
599
600         result = versaloon_interface.adaptors.peripheral_commit();
601
602         if (result == ERROR_OK) {
603                 for (i = 0; i < pending_scan_results_length; i++) {
604                         struct pending_scan_result *pending_scan_result =
605                                 &pending_scan_results_buffer[i];
606                         uint8_t *buffer = pending_scan_result->buffer;
607                         int length = pending_scan_result->length;
608                         int src_first = pending_scan_result->src_offset;
609                         int dest_first = pending_scan_result->dest_offset;
610                         bool last = pending_scan_result->last;
611
612                         struct scan_command *command;
613
614                         command = pending_scan_result->command;
615                         buf_set_buf(tdo_buffer, src_first, buffer, dest_first, length);
616
617 #ifdef _DEBUG_JTAG_IO_
618                         DEBUG_JTAG_IO(
619                                 "JTAG scan read(%d bits, from src %d bits to dest %d bits):",
620                                 length, src_first, dest_first);
621                         vsllink_debug_buffer(buffer + dest_first / 8,
622                                 DIV_ROUND_UP(length, 7));
623 #endif
624
625                         if (last) {
626                                 if (jtag_read_buffer(buffer, command)
627                                     != ERROR_OK) {
628                                         vsllink_tap_init();
629                                         return ERROR_JTAG_QUEUE_FAILED;
630                                 }
631
632                                 if (pending_scan_result->buffer != NULL)
633                                         free(pending_scan_result->buffer);
634                         }
635                 }
636         } else {
637                 LOG_ERROR("vsllink_jtag_execute failure");
638                 return ERROR_JTAG_QUEUE_FAILED;
639         }
640
641         vsllink_tap_init();
642
643         return ERROR_OK;
644 }
645
646 static int vsllink_tap_execute(void)
647 {
648         return vsllink_jtag_execute();
649 }
650
651 /****************************************************************************
652  * VSLLink USB low-level functions */
653
654 static uint8_t usb_check_string(usb_dev_handle *usb, uint8_t stringidx,
655         char *string, char *buff, uint16_t buf_size)
656 {
657         int len;
658         uint8_t alloced = 0;
659         uint8_t ret = 1;
660
661         if (NULL == buff) {
662                 buf_size = 256;
663                 buff = malloc(buf_size);
664                 if (NULL == buff) {
665                         ret = 0;
666                         goto free_and_return;
667                 }
668                 alloced = 1;
669         }
670
671         strcpy(buff, "");
672         len = usb_get_string_simple(usb, stringidx, buff, buf_size);
673         if ((len < 0) || ((size_t)len != strlen(buff))) {
674                 ret = 0;
675                 goto free_and_return;
676         }
677
678         buff[len] = '\0';
679         if ((string != NULL) && strcmp(buff, string)) {
680                 ret = 0;
681                 goto free_and_return;
682         }
683
684 free_and_return:
685         if (alloced && (buff != NULL)) {
686                 free(buff);
687                 buff = NULL;
688         }
689         return ret;
690 }
691
692 static usb_dev_handle *find_usb_device(uint16_t VID, uint16_t PID,
693         uint8_t interface, int8_t serialindex, char *serialstring,
694         int8_t productindex, char *productstring)
695 {
696         usb_dev_handle *dev_handle = NULL;
697         struct usb_bus *busses;
698         struct usb_bus *bus;
699         struct usb_device *dev;
700
701         usb_init();
702         usb_find_busses();
703         usb_find_devices();
704         busses = usb_get_busses();
705
706         for (bus = busses; bus; bus = bus->next) {
707                 for (dev = bus->devices; dev; dev = dev->next) {
708                         if ((dev->descriptor.idVendor == VID)
709                             && (dev->descriptor.idProduct == PID)) {
710                                 dev_handle = usb_open(dev);
711                                 if (NULL == dev_handle) {
712                                         LOG_ERROR("failed to open %04X:%04X, %s", VID, PID,
713                                                 usb_strerror());
714                                         continue;
715                                 }
716
717                                 /* check description string */
718                                 if (((productstring != NULL) && (productindex >= 0)
719                                                 && !usb_check_string(dev_handle, productindex,
720                                                 productstring, NULL, 0))
721                                                 || ((serialstring != NULL) && (serialindex >= 0)
722                                         && !usb_check_string(dev_handle, serialindex,
723                                                 serialstring, NULL, 0))) {
724                                         usb_close(dev_handle);
725                                         dev_handle = NULL;
726                                         continue;
727                                 }
728
729                                 if (usb_claim_interface(dev_handle, interface) != 0) {
730                                         LOG_ERROR(ERRMSG_FAILURE_OPERATION_MESSAGE,
731                                                 "claim interface", usb_strerror());
732                                         usb_close(dev_handle);
733                                         dev_handle = NULL;
734                                         continue;
735                                 }
736
737                                 if (dev_handle != NULL)
738                                         return dev_handle;
739                         }
740                 }
741         }
742
743         return dev_handle;
744 }
745
746 static struct vsllink *vsllink_usb_open(void)
747 {
748         usb_init();
749
750         struct usb_dev_handle *dev;
751
752         dev = find_usb_device(versaloon_interface.usb_setting.vid,
753                         versaloon_interface.usb_setting.pid,
754                         versaloon_interface.usb_setting.interface,
755                         0, NULL, 2, "Versaloon");
756         if (NULL == dev)
757                 return NULL;
758
759         struct vsllink *result = malloc(sizeof(struct vsllink));
760         result->usb_handle = dev;
761         return result;
762 }
763
764 static void vsllink_usb_close(struct vsllink *vsllink)
765 {
766         int ret;
767
768         ret = usb_release_interface(vsllink->usb_handle,
769                         versaloon_interface.usb_setting.interface);
770         if (ret != 0) {
771                 LOG_ERROR("fail to release interface %d, %d returned",
772                         versaloon_interface.usb_setting.interface, ret);
773                 exit(-1);
774         }
775
776         ret = usb_close(vsllink->usb_handle);
777         if (ret != 0) {
778                 LOG_ERROR("fail to close usb, %d returned", ret);
779                 exit(-1);
780         }
781
782         free(vsllink);
783 }
784
785 #define BYTES_PER_LINE  16
786
787 #if defined _DEBUG_JTAG_IO_
788 static void vsllink_debug_buffer(uint8_t *buffer, int length)
789 {
790         char line[81];
791         char s[4];
792         int i;
793         int j;
794
795         for (i = 0; i < length; i += BYTES_PER_LINE) {
796                 snprintf(line, 5, "%04x", i);
797                 for (j = i; j < i + BYTES_PER_LINE && j < length; j++) {
798                         snprintf(s, 4, " %02x", buffer[j]);
799                         strcat(line, s);
800                 }
801                 LOG_DEBUG("%s", line);
802         }
803 }
804 #endif  /* _DEBUG_JTAG_IO_ */
805
806 static const struct command_registration vsllink_command_handlers[] = {
807         {
808                 .name = "vsllink_usb_vid",
809                 .handler = &vsllink_handle_usb_vid_command,
810                 .mode = COMMAND_CONFIG,
811         },
812         {
813                 .name = "vsllink_usb_pid",
814                 .handler = &vsllink_handle_usb_pid_command,
815                 .mode = COMMAND_CONFIG,
816         },
817         {
818                 .name = "vsllink_usb_bulkin",
819                 .handler = &vsllink_handle_usb_bulkin_command,
820                 .mode = COMMAND_CONFIG,
821         },
822         {
823                 .name = "vsllink_usb_bulkout",
824                 .handler = &vsllink_handle_usb_bulkout_command,
825                 .mode = COMMAND_CONFIG,
826         },
827         {
828                 .name = "vsllink_usb_interface",
829                 .handler = &vsllink_handle_usb_interface_command,
830                 .mode = COMMAND_CONFIG,
831         },
832         COMMAND_REGISTRATION_DONE
833 };
834
835 static const char *vsllink_transports[] = {"jtag", "swd", NULL};
836
837 struct jtag_interface vsllink_interface = {
838         .name = "vsllink",
839         .supported = DEBUG_CAP_TMS_SEQ,
840         .commands = vsllink_command_handlers,
841         .transports = vsllink_transports,
842
843         .init = vsllink_init,
844         .quit = vsllink_quit,
845         .khz = vsllink_khz,
846         .speed = vsllink_speed,
847         .speed_div = vsllink_speed_div,
848         .execute_queue = vsllink_execute_queue,
849 };