cmsis-dap: prevent hidapi to search again for the adapter
authorAntonio Borneo <borneo.antonio@gmail.com>
Mon, 29 Jun 2020 21:22:37 +0000 (23:22 +0200)
committerAntonio Borneo <borneo.antonio@gmail.com>
Mon, 13 Jul 2020 23:39:52 +0000 (00:39 +0100)
commit97b710131693bdcfdc112e4c183d8e65bd909e94
tree95aab0689b7c2514d7662239f9e1a9193198f7fd
parente466f389a9f0e824d62c5ed6092507a2812427b7
cmsis-dap: prevent hidapi to search again for the adapter

The code in cmsis_dap_usb_open() already searches for the right
HID device that corresponds to the adapter. By calling hid_open()
it asks hidapi to re-search the adapter again based on the VID:PID
and the serial string of the adapter it has just found!

Apart from being a run-time overhead, this has an additional
drawback; there are USB adapters built as composite USB devices
that, beside the cmsis-dap HID interface, have other HID
interfaces for other purposes.
A typical example is the NXP LPC-Link2, that over the 5 interfaces
0) cmsis-dap (HID)
1) VCOM-CDC
2) VCOM-CDC
3) LPCSIO (HID)
4) LPC-LINK2 DATA PORT (HID)
has 3 of them of HID class.
The code in cmsis_dap_usb_open() could select the right interface
but then cannot propagate this information to hid_open().

Replace the call to hid_open() with hid_open_path(), passing as
parameter the "unique" path of the HID device already found.

Checking in hidapi source code, the implementation of hid_open()
consists in enumerating the HID devices, scan for the first one
matching VID:PID and serial number, and opening it by calling
hid_open_path(). This analysis highlights that using directly
hid_open_path() should not introduce any regression.

While applying these changes, move hid_init() before enumerating
the HID devices. This has no real consequences because the HID API
is marked as optional but, logically, it should be called before
any other HID API.

Change-Id: I77ec01dca64223ec597f21f188f363366d0049c6
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Suggested-by: Masatoshi Tateishi <tateishim3@gmail.com>
Reviewed-on: http://openocd.zylin.com/5731
Tested-by: jenkins
src/jtag/drivers/cmsis_dap_usb.c