cmsis-dap: prevent hidapi to search again for the adapter
[fw/openocd] / src / jtag / drivers / cmsis_dap_usb.c
index d52d698a437263a06552f4fa9b9fffef9a495806..89158368043559385211bbc965e2b6c612f5be71 100644 (file)
@@ -229,14 +229,16 @@ static int cmsis_dap_usb_open(void)
        int i;
        struct hid_device_info *devs, *cur_dev;
        unsigned short target_vid, target_pid;
-       wchar_t *target_serial = NULL;
-
        bool found = false;
-       bool serial_found = false;
 
        target_vid = 0;
        target_pid = 0;
 
+       if (hid_init() != 0) {
+               LOG_ERROR("unable to open HIDAPI");
+               return ERROR_FAIL;
+       }
+
        /*
         * The CMSIS-DAP specification stipulates:
         * "The Product String must contain "CMSIS-DAP" somewhere in the string. This is used by the
@@ -273,7 +275,6 @@ static int cmsis_dap_usb_open(void)
                        /* check serial number matches if given */
                        if (cmsis_dap_serial != NULL) {
                                if ((cur_dev->serial_number != NULL) && wcscmp(cmsis_dap_serial, cur_dev->serial_number) == 0) {
-                                       serial_found = true;
                                        break;
                                }
                        } else
@@ -288,23 +289,16 @@ static int cmsis_dap_usb_open(void)
        if (NULL != cur_dev) {
                target_vid = cur_dev->vendor_id;
                target_pid = cur_dev->product_id;
-               if (serial_found)
-                       target_serial = cmsis_dap_serial;
        }
 
-       hid_free_enumeration(devs);
-
        if (target_vid == 0 && target_pid == 0) {
                LOG_ERROR("unable to find CMSIS-DAP device");
+               hid_free_enumeration(devs);
                return ERROR_FAIL;
        }
 
-       if (hid_init() != 0) {
-               LOG_ERROR("unable to open HIDAPI");
-               return ERROR_FAIL;
-       }
-
-       dev = hid_open(target_vid, target_pid, target_serial);
+       dev = hid_open_path(cur_dev->path);
+       hid_free_enumeration(devs);
 
        if (dev == NULL) {
                LOG_ERROR("unable to open CMSIS-DAP device 0x%x:0x%x", target_vid, target_pid);
@@ -364,8 +358,6 @@ static void cmsis_dap_usb_close(struct cmsis_dap *dap)
                free(pending_fifo[i].transfers);
                pending_fifo[i].transfers = NULL;
        }
-
-       return;
 }
 
 static int cmsis_dap_usb_write(struct cmsis_dap *dap, int txlen)
@@ -509,15 +501,15 @@ static int cmsis_dap_cmd_DAP_Info(uint8_t info, uint8_t **data)
        return ERROR_OK;
 }
 
-static int cmsis_dap_cmd_DAP_LED(uint8_t leds)
+static int cmsis_dap_cmd_DAP_LED(uint8_t led, uint8_t state)
 {
        int retval;
        uint8_t *buffer = cmsis_dap_handle->packet_buffer;
 
        buffer[0] = 0;  /* report number */
        buffer[1] = CMD_DAP_LED;
-       buffer[2] = 0x00;
-       buffer[3] = leds;
+       buffer[2] = led;
+       buffer[3] = state;
        retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 4);
 
        if (retval != ERROR_OK || buffer[1] != 0x00) {
@@ -1086,8 +1078,12 @@ static int cmsis_dap_init(void)
                if (retval != ERROR_OK)
                        return ERROR_FAIL;
        }
+       /* Both LEDs on */
+       retval = cmsis_dap_cmd_DAP_LED(LED_ID_CONNECT, LED_ON);
+       if (retval != ERROR_OK)
+               return ERROR_FAIL;
 
-       retval = cmsis_dap_cmd_DAP_LED(0x03);           /* Both LEDs on */
+       retval = cmsis_dap_cmd_DAP_LED(LED_ID_RUN, LED_ON);
        if (retval != ERROR_OK)
                return ERROR_FAIL;
 
@@ -1102,9 +1098,6 @@ static int cmsis_dap_init(void)
                        LOG_INFO("Connecting under reset");
                }
        }
-
-       cmsis_dap_cmd_DAP_LED(0x00);                    /* Both LEDs off */
-
        LOG_INFO("CMSIS-DAP: Interface ready");
 
        return ERROR_OK;
@@ -1119,28 +1112,31 @@ static int cmsis_dap_swd_init(void)
 static int cmsis_dap_quit(void)
 {
        cmsis_dap_cmd_DAP_Disconnect();
-       cmsis_dap_cmd_DAP_LED(0x00);            /* Both LEDs off */
+       /* Both LEDs off */
+       cmsis_dap_cmd_DAP_LED(LED_ID_RUN, LED_OFF);
+       cmsis_dap_cmd_DAP_LED(LED_ID_CONNECT, LED_OFF);
 
        cmsis_dap_usb_close(cmsis_dap_handle);
 
        return ERROR_OK;
 }
 
-static void cmsis_dap_execute_reset(struct jtag_command *cmd)
+static int cmsis_dap_reset(int trst, int srst)
 {
        /* Set both TRST and SRST even if they're not enabled as
         * there's no way to tristate them */
 
        output_pins = 0;
-       if (!cmd->cmd.reset->srst)
+       if (!srst)
                output_pins |= SWJ_PIN_SRST;
-       if (!cmd->cmd.reset->trst)
+       if (!trst)
                output_pins |= SWJ_PIN_TRST;
 
        int retval = cmsis_dap_cmd_DAP_SWJ_Pins(output_pins,
                        SWJ_PIN_TRST | SWJ_PIN_SRST, 0, NULL);
        if (retval != ERROR_OK)
                LOG_ERROR("CMSIS-DAP: Interface reset failed");
+       return retval;
 }
 
 static void cmsis_dap_execute_sleep(struct jtag_command *cmd)
@@ -1581,10 +1577,6 @@ static void cmsis_dap_execute_tms(struct jtag_command *cmd)
 static void cmsis_dap_execute_command(struct jtag_command *cmd)
 {
        switch (cmd->type) {
-               case JTAG_RESET:
-                       cmsis_dap_flush();
-                       cmsis_dap_execute_reset(cmd);
-                       break;
                case JTAG_SLEEP:
                        cmsis_dap_flush();
                        cmsis_dap_execute_sleep(cmd);
@@ -1631,10 +1623,10 @@ static int cmsis_dap_execute_queue(void)
 static int cmsis_dap_speed(int speed)
 {
        if (speed > DAP_MAX_CLOCK)
-               LOG_INFO("High speed (adapter_khz %d) may be limited by adapter firmware.", speed);
+               LOG_INFO("High speed (adapter speed %d) may be limited by adapter firmware.", speed);
 
        if (speed == 0) {
-               LOG_ERROR("RTCK not supported. Set nonzero adapter_khz.");
+               LOG_ERROR("RTCK not supported. Set nonzero \"adapter speed\".");
                return ERROR_JTAG_NOT_IMPLEMENTED;
        }
 
@@ -1789,17 +1781,23 @@ static const struct swd_driver cmsis_dap_swd_driver = {
 
 static const char * const cmsis_dap_transport[] = { "swd", "jtag", NULL };
 
-struct jtag_interface cmsis_dap_interface = {
-       .name = "cmsis-dap",
+static struct jtag_interface cmsis_dap_interface = {
        .supported = DEBUG_CAP_TMS_SEQ,
-       .commands = cmsis_dap_command_handlers,
-       .swd = &cmsis_dap_swd_driver,
+       .execute_queue = cmsis_dap_execute_queue,
+};
+
+struct adapter_driver cmsis_dap_adapter_driver = {
+       .name = "cmsis-dap",
        .transports = cmsis_dap_transport,
+       .commands = cmsis_dap_command_handlers,
 
-       .execute_queue = cmsis_dap_execute_queue,
-       .speed = cmsis_dap_speed,
-       .speed_div = cmsis_dap_speed_div,
-       .khz = cmsis_dap_khz,
        .init = cmsis_dap_init,
        .quit = cmsis_dap_quit,
+       .reset = cmsis_dap_reset,
+       .speed = cmsis_dap_speed,
+       .khz = cmsis_dap_khz,
+       .speed_div = cmsis_dap_speed_div,
+
+       .jtag_ops = &cmsis_dap_interface,
+       .swd_ops = &cmsis_dap_swd_driver,
 };