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