Don't cast return value of [cm]alloc
[openocd.git] / src / jtag / drivers / cmsis_dap_usb.c
index 5cd117fc41f109450a5664a93160c75b0f29f8ff..dc59fcf9c2bcfc9e006e8c6dedfefb4a0db1645b 100644 (file)
@@ -58,8 +58,8 @@
 
 #define MAX_USB_IDS 8
 /* vid = pid = 0 marks the end of the list */
-static uint16_t cmsis_dap_vid[MAX_USB_IDS + 1] = { 0xc251, 0xc251, 0xc251, 0x0d28, 0x03eb, 0 };
-static uint16_t cmsis_dap_pid[MAX_USB_IDS + 1] = { 0xf001, 0xf002, 0x2722, 0x0204, 0x2111, 0 };
+static uint16_t cmsis_dap_vid[MAX_USB_IDS + 1] = { 0 };
+static uint16_t cmsis_dap_pid[MAX_USB_IDS + 1] = { 0 };
 
 #define PACKET_SIZE       (64 + 1)     /* 64 bytes plus report id */
 #define USB_TIMEOUT       1000
@@ -159,17 +159,58 @@ static int cmsis_dap_usb_open(void)
 {
        hid_device *dev = NULL;
        int i;
+       struct hid_device_info *devs, *cur_dev;
+       unsigned short target_vid, target_pid;
+
+       target_vid = 0;
+       target_pid = 0;
+
+       /*
+       The CMSIS-DAP specification stipulates:
+       "The Product String must contain "CMSIS-DAP" somewhere in the string. This is used by the
+       debuggers to idenify a CMSIS-DAP compliant Debug Unit that is connected to a host computer."
+       */
+       devs = hid_enumerate(0x0, 0x0);
+       cur_dev = devs;
+       while (NULL != cur_dev) {
+               if (0 == cmsis_dap_vid[0]) {
+                       if (NULL == cur_dev->product_string) {
+                               LOG_DEBUG("Cannot read product string of device 0x%x:0x%x",
+                                         cur_dev->vendor_id, cur_dev->product_id);
+                       } else {
+                               if (wcsstr(cur_dev->product_string, L"CMSIS-DAP"))
+                                       /*
+                                       if the user hasn't specified VID:PID *and*
+                                       product string contains "CMSIS-DAP", pick it
+                                       */
+                                       break;
+                       }
+               } else {
+                       /*
+                       otherwise, exhaustively compare against all VID:PID in list
+                       */
+                       for (i = 0; cmsis_dap_vid[i] || cmsis_dap_pid[i]; i++) {
+                               if ((cmsis_dap_vid[i] == cur_dev->vendor_id) && (cmsis_dap_pid[i] == cur_dev->product_id))
+                                       break;
+                       }
+               }
+
+               cur_dev = cur_dev->next;
+       }
+
+       if (NULL != cur_dev) {
+               target_vid = cur_dev->vendor_id;
+               target_pid = cur_dev->product_id;
+       }
+
+       hid_free_enumeration(devs);
 
        if (hid_init() != 0) {
                LOG_ERROR("unable to open HIDAPI");
                return ERROR_FAIL;
        }
 
-       for (i = 0; cmsis_dap_vid[i] || cmsis_dap_pid[i]; i++) {
-               dev = hid_open(cmsis_dap_vid[i], cmsis_dap_pid[i], NULL);
-               if (dev != NULL)
-                       break;
-       }
+       dev = hid_open(target_vid, target_pid, NULL);
 
        if (dev == NULL) {
                LOG_ERROR("unable to open CMSIS-DAP device");
@@ -196,7 +237,7 @@ static int cmsis_dap_usb_open(void)
        int packet_size = PACKET_SIZE;
 
        /* atmel cmsis-dap uses 512 byte reports */
-       if (cmsis_dap_vid[i] == 0x03eb && cmsis_dap_pid[i] == 0x2111)
+       if (target_vid == 0x03eb)
                packet_size = 512 + 1;
 
        cmsis_dap_handle->packet_buffer = malloc(packet_size);
@@ -249,7 +290,7 @@ static int cmsis_dap_usb_xfer(struct cmsis_dap *dap, int txlen)
        return ERROR_OK;
 }
 
-static int cmsis_dap_cmd_DAP_SWJ_Pins(uint8_t pins, uint8_t mask, uint32_t wait, uint8_t *input)
+static int cmsis_dap_cmd_DAP_SWJ_Pins(uint8_t pins, uint8_t mask, uint32_t delay, uint8_t *input)
 {
        int retval;
        uint8_t *buffer = cmsis_dap_handle->packet_buffer;
@@ -258,10 +299,10 @@ static int cmsis_dap_cmd_DAP_SWJ_Pins(uint8_t pins, uint8_t mask, uint32_t wait,
        buffer[1] = CMD_DAP_SWJ_PINS;
        buffer[2] = pins;
        buffer[3] = mask;
-       buffer[4] = wait & 0xff;
-       buffer[5] = (wait >> 8) & 0xff;
-       buffer[6] = (wait >> 16) & 0xff;
-       buffer[7] = (wait >> 24) & 0xff;
+       buffer[4] = delay & 0xff;
+       buffer[5] = (delay >> 8) & 0xff;
+       buffer[6] = (delay >> 16) & 0xff;
+       buffer[7] = (delay >> 24) & 0xff;
        retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 8);
 
        if (retval != ERROR_OK) {
@@ -377,7 +418,7 @@ static int cmsis_dap_cmd_DAP_Disconnect(void)
        return ERROR_OK;
 }
 
-static int cmsis_dap_cmd_DAP_TFER_Configure(uint8_t idle, uint16_t wait, uint16_t retry)
+static int cmsis_dap_cmd_DAP_TFER_Configure(uint8_t idle, uint16_t delay, uint16_t retry)
 {
        int retval;
        uint8_t *buffer = cmsis_dap_handle->packet_buffer;
@@ -385,8 +426,8 @@ static int cmsis_dap_cmd_DAP_TFER_Configure(uint8_t idle, uint16_t wait, uint16_
        buffer[0] = 0;  /* report number */
        buffer[1] = CMD_DAP_TFER_CONFIGURE;
        buffer[2] = idle;
-       buffer[3] = wait & 0xff;
-       buffer[4] = (wait >> 8) & 0xff;
+       buffer[3] = delay & 0xff;
+       buffer[4] = (delay >> 8) & 0xff;
        buffer[5] = retry & 0xff;
        buffer[6] = (retry >> 8) & 0xff;
        retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 7);

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)