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