96e4efd343d2290afa46bbc171599caae4759274
[fw/openocd] / src / jtag / jlink.c
1 /***************************************************************************
2  *   Copyright (C) 2007 by Juergen Stuber <juergen@jstuber.net>            *
3  *   based on Dominic Rath's and Benedikt Sauter's usbprog.c               *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "replacements.h"
26
27 #include "jtag.h"
28
29 #include <usb.h>
30 #include <string.h>
31
32 #include "log.h"
33
34 /* enable this to debug communication
35  */
36 #if 0
37 #define _DEBUG_USB_COMMS_
38 #endif
39
40 #ifdef _DEBUG_JTAG_IO_
41 #define DEBUG_JTAG_IO(expr ...) LOG_DEBUG(expr)
42 #else
43 #define DEBUG_JTAG_IO(expr ...)
44 #endif
45
46 #define VID 0x1366
47 #define PID 0x0101
48
49 #define JLINK_WRITE_ENDPOINT    0x02
50 #define JLINK_READ_ENDPOINT             0x81
51
52 #define JLINK_USB_TIMEOUT               100
53
54 #define JLINK_IN_BUFFER_SIZE                                    8192
55 #define JLINK_OUT_BUFFER_SIZE                                   8192
56 #define JLINK_EMU_RESULT_BUFFER_SIZE    64
57
58
59 /* Global USB buffers */
60 static u8 usb_in_buffer[JLINK_IN_BUFFER_SIZE];
61 static u8 usb_out_buffer[JLINK_OUT_BUFFER_SIZE];
62 static u8 usb_emu_result_buffer[JLINK_EMU_RESULT_BUFFER_SIZE];
63
64 /* Constants for JLink command */
65 #define EMU_CMD_VERSION     0x01
66 #define EMU_CMD_SET_SPEED   0x05
67 #define EMU_CMD_GET_STATE   0x07
68 #define EMU_CMD_HW_JTAG3    0xcf
69 #define EMU_CMD_HW_RESET0   0xdc
70 #define EMU_CMD_HW_RESET1   0xdd
71 #define EMU_CMD_HW_TRST0    0xde
72 #define EMU_CMD_HW_TRST1    0xdf
73
74 /* max speed 12MHz v5.0 jlink */
75 #define JLINK_MAX_SPEED 12000
76
77 /* External interface functions */
78 int jlink_execute_queue(void);
79 int jlink_speed(int speed);
80 int jlink_khz(int khz, int *jtag_speed);
81 int jlink_register_commands(struct command_context_s *cmd_ctx);
82 int jlink_init(void);
83 int jlink_quit(void);
84
85 /* CLI command handler functions */
86 int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
87
88 /* Queue command functions */
89 void jlink_end_state(enum tap_state state);
90 void jlink_state_move(void);
91 void jlink_path_move(int num_states, enum tap_state *path);
92 void jlink_runtest(int num_cycles);
93 void jlink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
94 void jlink_reset(int trst, int srst);
95 void jlink_simple_command(u8 command);
96 int jlink_get_status(void);
97
98 /* J-Link tap buffer functions */
99 void jlink_tap_init();
100 int jlink_tap_execute();
101 void jlink_tap_ensure_space(int scans, int bits);
102 void jlink_tap_append_step(int tms, int tdi);
103 void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command);
104
105 /* Jlink lowlevel functions */
106 typedef struct jlink_jtag
107 {
108         struct usb_dev_handle* usb_handle;
109 } jlink_jtag_t;
110
111 jlink_jtag_t *jlink_usb_open(void);
112 void jlink_usb_close(jlink_jtag_t *jlink_jtag);
113 int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length);
114 int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length);
115 int jlink_usb_read(jlink_jtag_t *jlink_jtag);
116 int jlink_usb_read_emu_result(jlink_jtag_t *jlink_jtag);
117
118 /* helper functions */
119 int jlink_get_version_info(void);
120
121 #ifdef _DEBUG_USB_COMMS_
122 void jlink_debug_buffer(u8 *buffer, int length);
123 #endif
124
125 jlink_jtag_t* jlink_jtag_handle;
126
127 /***************************************************************************/
128 /* External interface implementation */
129
130 jtag_interface_t jlink_interface =
131 {
132         .name = "jlink",
133         .execute_queue = jlink_execute_queue,
134         .speed = jlink_speed,
135         .khz = jlink_khz,
136         .register_commands = jlink_register_commands,
137         .init = jlink_init,
138         .quit = jlink_quit
139 };
140
141 int jlink_execute_queue(void)
142 {
143         jtag_command_t *cmd = jtag_command_queue;
144         int scan_size;
145         enum scan_type type;
146         u8 *buffer;
147         
148         while (cmd != NULL)
149         {
150                 switch (cmd->type)
151                 {
152                         case JTAG_END_STATE:
153                                 DEBUG_JTAG_IO("end_state: %i", cmd->cmd.end_state->end_state);
154                         
155                                 if (cmd->cmd.end_state->end_state != -1)
156                                 {
157                                         jlink_end_state(cmd->cmd.end_state->end_state);
158                                 }
159                                 break;
160         
161                         case JTAG_RUNTEST:
162                                 DEBUG_JTAG_IO( "runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, \
163                                         cmd->cmd.runtest->end_state);
164                                 
165                                 if (cmd->cmd.runtest->end_state != -1)
166                                 {
167                                         jlink_end_state(cmd->cmd.runtest->end_state);
168                                 }
169                                 jlink_runtest(cmd->cmd.runtest->num_cycles);
170                                 break;
171         
172                         case JTAG_STATEMOVE:
173                                 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
174                         
175                                 if (cmd->cmd.statemove->end_state != -1)
176                                 {
177                                         jlink_end_state(cmd->cmd.statemove->end_state);
178                                 }
179                                 jlink_state_move();
180                                 break;
181         
182                         case JTAG_PATHMOVE:
183                                 DEBUG_JTAG_IO("pathmove: %i states, end in %i", \
184                                         cmd->cmd.pathmove->num_states, \
185                                         cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
186                         
187                                 jlink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
188                                 break;
189         
190                         case JTAG_SCAN:
191                                 DEBUG_JTAG_IO("scan end in %i", cmd->cmd.scan->end_state);
192                         
193                                 if (cmd->cmd.scan->end_state != -1)
194                                 {
195                                         jlink_end_state(cmd->cmd.scan->end_state);
196                                 }
197                         
198                                 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
199                                 DEBUG_JTAG_IO("scan input, length = %d", scan_size);
200                                 
201 #ifdef _DEBUG_USB_COMMS_
202                                 jlink_debug_buffer(buffer, (scan_size + 7) / 8);
203 #endif
204                                 type = jtag_scan_type(cmd->cmd.scan);
205                                 jlink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
206                                 break;
207         
208                         case JTAG_RESET:
209                                 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
210                         
211                                 jlink_tap_execute();
212                         
213                                 if (cmd->cmd.reset->trst == 1)
214                                 {
215                                         cur_state = TAP_TLR;
216                                 }
217                                 jlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
218                                 break;
219         
220                         case JTAG_SLEEP:
221                                 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
222                                 jlink_tap_execute();
223                                 jtag_sleep(cmd->cmd.sleep->us);
224                                 break;
225         
226                         default:
227                                 LOG_ERROR("BUG: unknown JTAG command type encountered");
228                                 exit(-1);
229                 }
230                 cmd = cmd->next;
231         }
232         
233         return jlink_tap_execute();
234 }
235
236 /* Sets speed in kHz. */
237 int jlink_speed(int speed)
238 {
239         int result;
240         
241         if (speed <= JLINK_MAX_SPEED)
242         {
243                 /* check for RTCK setting */
244                 if (speed == 0)
245                         speed = -1;
246                 
247                 usb_out_buffer[0] = EMU_CMD_SET_SPEED;
248                 usb_out_buffer[1] = (speed >> 0) & 0xff;
249                 usb_out_buffer[2] = (speed >> 8) & 0xff;
250                 
251                 result = jlink_usb_write(jlink_jtag_handle, 3);
252                 
253                 if (result == 3)
254                 {
255                         return ERROR_OK;
256                 }
257                 else
258                 {
259                         LOG_ERROR("J-Link setting speed failed (%d)", result);
260                         return ERROR_JTAG_DEVICE_ERROR;
261                 }
262         }
263         else
264         {
265                 LOG_INFO("Requested speed %dkHz exceeds maximum of %dkHz, ignored", speed, JLINK_MAX_SPEED);
266         }
267         
268         return ERROR_OK;
269 }
270
271 int jlink_khz(int khz, int *jtag_speed)
272 {
273         *jtag_speed = khz;
274         
275         return ERROR_OK;
276 }
277
278 int jlink_register_commands(struct command_context_s *cmd_ctx)
279 {
280         register_command(cmd_ctx, NULL, "jlink_info", jlink_handle_jlink_info_command, COMMAND_EXEC,
281                 "query jlink info");
282         return ERROR_OK;
283 }
284
285 int jlink_init(void)
286 {
287         int check_cnt;  
288         
289         jlink_jtag_handle = jlink_usb_open();
290         
291         if (jlink_jtag_handle == 0)
292         {
293                 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
294                 return ERROR_JTAG_INIT_FAILED;
295         }
296                 
297         check_cnt = 0;
298         while (check_cnt < 3)
299         {
300                 if (jlink_get_version_info() == ERROR_OK)
301                 {
302                         /* attempt to get status */
303                         jlink_get_status();
304                         break;
305                 }
306                 
307                 check_cnt++;
308         }
309
310         if (check_cnt == 3)
311         {
312                 LOG_INFO("J-Link initial read failed, don't worry");
313         }
314         
315         LOG_INFO("J-Link JTAG Interface ready");
316         
317         jlink_reset(0, 0);
318         jlink_tap_init();
319         
320         return ERROR_OK;
321 }
322
323 int jlink_quit(void)
324 {
325         jlink_usb_close(jlink_jtag_handle);
326         return ERROR_OK;
327 }
328
329 /***************************************************************************/
330 /* Queue command implementations */
331
332 void jlink_end_state(enum tap_state state)
333 {
334         if (tap_move_map[state] != -1)
335         {
336                 end_state = state;
337         }
338         else
339         {
340                 LOG_ERROR("BUG: %i is not a valid end state", state);
341                 exit(-1);
342         }
343 }
344
345 /* Goes to the end state. */
346 void jlink_state_move(void)
347 {
348         int i;
349         int tms = 0;
350         u8 tms_scan = TAP_MOVE(cur_state, end_state);
351         
352         for (i = 0; i < 7; i++)
353         {
354                 tms = (tms_scan >> i) & 1;
355                 jlink_tap_append_step(tms, 0);
356         }
357         
358         cur_state = end_state;
359 }
360
361 void jlink_path_move(int num_states, enum tap_state *path)
362 {
363         int i;
364         
365         for (i = 0; i < num_states; i++)
366         {
367                 if (path[i] == tap_transitions[cur_state].low)
368                 {
369                         jlink_tap_append_step(0, 0);
370                 }
371                 else if (path[i] == tap_transitions[cur_state].high)
372                 {
373                         jlink_tap_append_step(1, 0);
374                 }
375                 else
376                 {
377                         LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
378                         exit(-1);
379                 }
380                 
381                 cur_state = path[i];
382         }
383         
384         end_state = cur_state;
385 }
386
387 void jlink_runtest(int num_cycles)
388 {
389         int i;
390         
391         enum tap_state saved_end_state = end_state;
392         
393         /* only do a state_move when we're not already in RTI */
394         if (cur_state != TAP_RTI)
395         {
396                 jlink_end_state(TAP_RTI);
397                 jlink_state_move();
398         }
399         
400         /* execute num_cycles */
401         for (i = 0; i < num_cycles; i++)
402         {
403                 jlink_tap_append_step(0, 0);
404         }
405         
406         /* finish in end_state */
407         jlink_end_state(saved_end_state);
408         if (cur_state != end_state)
409         {
410                 jlink_state_move();
411         }
412 }
413
414 void jlink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
415 {
416         enum tap_state saved_end_state;
417         
418         jlink_tap_ensure_space(1, scan_size + 8);
419         
420         saved_end_state = end_state;
421         
422         /* Move to appropriate scan state */
423         jlink_end_state(ir_scan ? TAP_SI : TAP_SD);
424         
425         jlink_state_move();
426         jlink_end_state(saved_end_state);
427         
428         /* Scan */
429         jlink_tap_append_scan(scan_size, buffer, command);
430         
431         /* We are in Exit1, go to Pause */
432         jlink_tap_append_step(0, 0);
433         
434         cur_state = ir_scan ? TAP_PI : TAP_PD;
435         
436         if (cur_state != end_state)
437         {
438                 jlink_state_move();
439         }
440 }
441
442 void jlink_reset(int trst, int srst)
443 {
444         LOG_DEBUG("trst: %i, srst: %i", trst, srst);
445         
446         /* Signals are active low */
447         if (srst == 0)
448         {
449                 jlink_simple_command(EMU_CMD_HW_RESET1);
450         }
451         else if (srst == 1)
452         {
453                 jlink_simple_command(EMU_CMD_HW_RESET0);
454         }
455         
456         if (trst == 0)
457         {
458                 jlink_simple_command(EMU_CMD_HW_TRST1);
459         }
460         else if (trst == 1)
461         {
462                 jlink_simple_command(EMU_CMD_HW_TRST0);
463         }
464 }
465
466 void jlink_simple_command(u8 command)
467 {
468         int result;
469         
470         DEBUG_JTAG_IO("0x%02x", command);
471         
472         usb_out_buffer[0] = command;
473         result = jlink_usb_write(jlink_jtag_handle, 1);
474         
475         if (result != 1)
476         {
477                 LOG_ERROR("J-Link command 0x%02x failed (%d)", command, result);
478         }
479 }
480
481 int jlink_get_status(void)
482 {
483         int result;
484         
485         jlink_simple_command(EMU_CMD_GET_STATE);
486         result = jlink_usb_read(jlink_jtag_handle);
487         
488         if(result == 8)
489         {
490                 int vref = usb_in_buffer[0] + (usb_in_buffer[1] << 8);
491                 LOG_INFO("Vref = %d.%d TCK=%d TDI=%d TDO=%d TMS=%d SRST=%d TRST=%d\n", \
492                         vref / 1000, vref % 1000, \
493                         usb_in_buffer[2], usb_in_buffer[3], usb_in_buffer[4], \
494                         usb_in_buffer[5], usb_in_buffer[6], usb_in_buffer[7]);
495                 
496                 if(vref < 1500)
497                 {
498                         LOG_ERROR("Vref too low. Eventually the target isn't powered or disconnected?\n");
499                 }
500         }
501         else
502         {
503                 LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)\n", result);
504         }
505         
506         return ERROR_OK;
507 }
508
509 int jlink_get_version_info(void)
510 {
511         int result;
512         int len = 0;
513         
514         /* query hardware version */
515         jlink_simple_command(EMU_CMD_VERSION);
516         result = jlink_usb_read(jlink_jtag_handle);
517         
518         if (result == 2)
519         {
520                 len = buf_get_u32(usb_in_buffer, 0, 16);
521                 result = jlink_usb_read(jlink_jtag_handle);
522                 
523                 if(result == len)
524                 {
525                         usb_in_buffer[result] = 0;
526                         LOG_INFO(usb_in_buffer);
527                         return ERROR_OK;
528                 }
529         }
530         
531         LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n", result);
532         return ERROR_JTAG_DEVICE_ERROR;
533 }
534
535 int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
536 {
537         if (jlink_get_version_info() == ERROR_OK)
538         {
539                 /* attempt to get status */
540                 jlink_get_status();
541         }
542         
543         return ERROR_OK;
544 }
545
546 /***************************************************************************/
547 /* J-Link tap functions */
548
549 /* 2048 is the max value we can use here */
550 #define JLINK_TAP_BUFFER_SIZE 2048
551
552 static int tap_length;
553 static u8 tms_buffer[JLINK_TAP_BUFFER_SIZE];
554 static u8 tdi_buffer[JLINK_TAP_BUFFER_SIZE];
555 static u8 tdo_buffer[JLINK_TAP_BUFFER_SIZE];
556
557 typedef struct
558 {
559         int first;      /* First bit position in tdo_buffer to read */
560         int length; /* Number of bits to read */
561         scan_command_t *command; /* Corresponding scan command */
562         u8 *buffer;
563 } pending_scan_result_t;
564
565 #define MAX_PENDING_SCAN_RESULTS 256
566
567 static int pending_scan_results_length;
568 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
569
570 static int last_tms;
571
572 void jlink_tap_init()
573 {
574         tap_length = 0;
575         pending_scan_results_length = 0;
576 }
577
578 void jlink_tap_ensure_space(int scans, int bits)
579 {
580         int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
581         int available_bits = JLINK_TAP_BUFFER_SIZE * 8 - tap_length;
582         
583         if (scans > available_scans || bits > available_bits)
584         {
585                 jlink_tap_execute();
586         }
587 }
588
589 void jlink_tap_append_step(int tms, int tdi)
590 {
591         last_tms = tms;
592         int index = tap_length / 8;
593         
594         if (index < JLINK_TAP_BUFFER_SIZE)
595         {
596                 int bit_index = tap_length % 8;
597                 u8 bit = 1 << bit_index;
598                 
599                 if (tms)
600                 {
601                         tms_buffer[index] |= bit;
602                 }
603                 else
604                 {
605                         tms_buffer[index] &= ~bit;
606                 }
607                 
608                 if (tdi)
609                 {
610                         tdi_buffer[index] |= bit;
611                 }
612                 else
613                 {
614                         tdi_buffer[index] &= ~bit;
615                 }
616                 
617                 tap_length++;
618         }
619         else
620         {
621                 LOG_ERROR("jlink_tap_append_step, overflow");
622         }
623 }
624
625 void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command)
626 {
627         pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
628         int i;
629         
630         pending_scan_result->first = tap_length;
631         pending_scan_result->length = length;
632         pending_scan_result->command = command;
633         pending_scan_result->buffer = buffer;
634         
635         for (i = 0; i < length; i++)
636         {
637                 jlink_tap_append_step((i < length-1 ? 0 : 1), (buffer[i/8] >> (i%8)) & 1);
638         }
639         pending_scan_results_length++;
640 }
641
642 /* Pad and send a tap sequence to the device, and receive the answer.
643  * For the purpose of padding we assume that we are in idle or pause state. */
644 int jlink_tap_execute()
645 {
646         int byte_length;
647         int tms_offset;
648         int tdi_offset;
649         int i;
650         int result;
651         
652         if (tap_length > 0)
653         {
654                 /* Pad last byte so that tap_length is divisible by 8 */
655                 while (tap_length % 8 != 0)
656                 {
657                         /* More of the last TMS value keeps us in the same state,
658                          * analogous to free-running JTAG interfaces. */
659                         jlink_tap_append_step(last_tms, 0);
660                 }
661         
662                 byte_length = tap_length / 8;
663         
664                 usb_out_buffer[0] = EMU_CMD_HW_JTAG3;
665                 usb_out_buffer[1] = 0;
666                 usb_out_buffer[2] = (tap_length >> 0) & 0xff;
667                 usb_out_buffer[3] = (tap_length >> 8) & 0xff;
668         
669                 tms_offset = 4;
670                 for (i = 0; i < byte_length; i++)
671                 {
672                         usb_out_buffer[tms_offset + i] = tms_buffer[i];
673                 }
674                 
675                 tdi_offset = tms_offset + byte_length;
676                 for (i = 0; i < byte_length; i++)
677                 {
678                         usb_out_buffer[tdi_offset + i] = tdi_buffer[i];
679                 }
680         
681                 result = jlink_usb_message(jlink_jtag_handle, 4 + 2 * byte_length, byte_length);
682         
683                 if (result == byte_length)
684                 {
685                         for (i = 0; i < byte_length; i++)
686                         {
687                                 tdo_buffer[i] = usb_in_buffer[i];
688                         }
689                         
690                         for (i = 0; i < pending_scan_results_length; i++)
691                         {
692                                 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
693                                 u8 *buffer = pending_scan_result->buffer;
694                                 int length = pending_scan_result->length;
695                                 int first = pending_scan_result->first;
696                                 scan_command_t *command = pending_scan_result->command;
697         
698                                 /* Copy to buffer */
699                                 buf_set_buf(tdo_buffer, first, buffer, 0, length);
700         
701                                 DEBUG_JTAG_IO("pending scan result, length = %d", length);
702                                 
703 #ifdef _DEBUG_USB_COMMS_
704                                 jlink_debug_buffer(buffer, byte_length);
705 #endif
706         
707                                 if (jtag_read_buffer(buffer, command) != ERROR_OK)
708                                 {
709                                         jlink_tap_init();
710                                         return ERROR_JTAG_QUEUE_FAILED;
711                                 }
712                 
713                                 if (pending_scan_result->buffer != NULL)
714                                 {
715                                         free(pending_scan_result->buffer);
716                                 }
717                         }
718                 }
719                 else
720                 {
721                         LOG_ERROR("jlink_tap_execute, wrong result %d, expected %d", result, byte_length);
722                         return ERROR_JTAG_QUEUE_FAILED;
723                 }
724                 
725                 jlink_tap_init();
726         }
727         
728         return ERROR_OK;
729 }
730
731 /*****************************************************************************/
732 /* JLink USB low-level functions */
733
734 jlink_jtag_t* jlink_usb_open()
735 {
736         struct usb_bus *busses;
737         struct usb_bus *bus;
738         struct usb_device *dev;
739         
740         jlink_jtag_t *result;
741         
742         result = (jlink_jtag_t*) malloc(sizeof(jlink_jtag_t));
743         
744         usb_init();
745         usb_find_busses();
746         usb_find_devices();
747         
748         busses = usb_get_busses();
749         
750         /* find jlink_jtag device in usb bus */
751         
752         for (bus = busses; bus; bus = bus->next)
753         {
754                 for (dev = bus->devices; dev; dev = dev->next)
755                 {
756                         if ((dev->descriptor.idVendor == VID) && (dev->descriptor.idProduct == PID))
757                         {
758                                 result->usb_handle = usb_open(dev);
759                                 
760                                 /* usb_set_configuration required under win32 */
761                                 usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
762                                 usb_claim_interface(result->usb_handle, 0);
763                                 
764 #if 0
765                                 /* 
766                                  * This makes problems under Mac OS X. And is not needed
767                                  * under Windows. Hopefully this will not break a linux build
768                                  */
769                                 usb_set_altinterface(result->usb_handle, 0);
770 #endif                          
771                                 return result;
772                         }
773                 }
774         }
775         
776         free(result);
777         return NULL;
778 }
779
780 void jlink_usb_close(jlink_jtag_t *jlink_jtag)
781 {
782         usb_close(jlink_jtag->usb_handle);
783         free(jlink_jtag);
784 }
785
786 /* Send a message and receive the reply. */
787 int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length)
788 {
789         int result;
790         int result2;
791         
792         result = jlink_usb_write(jlink_jtag, out_length);
793         if (result == out_length)
794         {
795                 result = jlink_usb_read(jlink_jtag);
796                 if (result == in_length || result == in_length+1)
797                 {
798                         if (result == in_length)
799                         {
800                                 /* Must read the result from the EMU too */
801                                 result2 = jlink_usb_read_emu_result(jlink_jtag);
802                                 if (1 == result2)
803                                 {
804                                         /* Check the result itself */
805                                         if (0 == usb_emu_result_buffer[0])
806                                         {
807                                                 return result;
808                                         }
809                                         else
810                                         {
811                                                 LOG_ERROR("jlink_usb_read_emu_result (requested=0, result=%d)", usb_emu_result_buffer[0]);
812                                                 return -1;                              
813                                         }
814                                 }
815                                 else
816                                 {
817                                         LOG_ERROR("jlink_usb_read_emu_result len (requested=1, result=%d)", result2);
818                                         return -1;
819                                 }
820                         }
821                         else
822                         {
823                                 /* Check the result itself */
824                                 if (0 == usb_in_buffer[result-1])
825                                 {
826                                         return result-1;
827                                 }
828                                 else
829                                 {
830                                         LOG_ERROR("jlink_usb_read_emu_result (requested=0, result=%d)", usb_in_buffer[result]);
831                                         return -1;                              
832                                 }
833                         }
834                 }
835                 else
836                 {
837                         LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
838                         return -1;
839                 }
840         }
841         else
842         {
843                 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
844                 return -1;
845         }
846 }
847
848 /* Write data from out_buffer to USB. */
849 int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length)
850 {
851         int result;
852         
853         if (out_length > JLINK_OUT_BUFFER_SIZE)
854         {
855                 LOG_ERROR("jlink_jtag_write illegal out_length=%d (max=%d)", out_length, JLINK_OUT_BUFFER_SIZE);
856                 return -1;
857         }
858         
859         result = usb_bulk_write(jlink_jtag->usb_handle, JLINK_WRITE_ENDPOINT, \
860                 usb_out_buffer, out_length, JLINK_USB_TIMEOUT);
861         
862         DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d", out_length, result);
863         
864 #ifdef _DEBUG_USB_COMMS_
865         jlink_debug_buffer(usb_out_buffer, out_length);
866 #endif
867         return result;
868 }
869
870 /* Read data from USB into in_buffer. */
871 int jlink_usb_read(jlink_jtag_t *jlink_jtag)
872 {
873         int result = usb_bulk_read(jlink_jtag->usb_handle, JLINK_READ_ENDPOINT, \
874                 usb_in_buffer, JLINK_IN_BUFFER_SIZE, JLINK_USB_TIMEOUT);
875
876         DEBUG_JTAG_IO("jlink_usb_read, result = %d", result);
877         
878 #ifdef _DEBUG_USB_COMMS_
879         jlink_debug_buffer(usb_in_buffer, result);
880 #endif
881         return result;
882 }
883
884 /* Read the result from the previous EMU cmd into result_buffer. */
885 int jlink_usb_read_emu_result(jlink_jtag_t *jlink_jtag)
886 {
887         int result = usb_bulk_read(jlink_jtag->usb_handle, JLINK_READ_ENDPOINT, \
888                 usb_emu_result_buffer, JLINK_EMU_RESULT_BUFFER_SIZE, JLINK_USB_TIMEOUT);
889
890         DEBUG_JTAG_IO("jlink_usb_read_result, result = %d", result);
891         
892 #ifdef _DEBUG_USB_COMMS_
893         jlink_debug_buffer(usb_emu_result_buffer, result);
894 #endif
895         return result;
896 }
897
898 #ifdef _DEBUG_USB_COMMS_
899 #define BYTES_PER_LINE  16
900
901 void jlink_debug_buffer(u8 *buffer, int length)
902 {
903         char line[81];
904         char s[4];
905         int i;
906         int j;
907         
908         for (i = 0; i < length; i += BYTES_PER_LINE)
909         {
910                 snprintf(line, 5, "%04x", i);
911                 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
912                 {
913                         snprintf(s, 4, " %02x", buffer[j]);
914                         strcat(line, s);
915                 }
916                 LOG_DEBUG(line);
917         }
918 }
919 #endif