@@ -145,6 +145,104 @@ static bool jtag_libusb_match_serial(struct libusb_device_handle *device,
145145 return match ;
146146}
147147
148+ static int jtag_libusb_get_dev_location (struct libusb_device * dev , char * loc , int loc_len )
149+ {
150+ int k , len , wr ;
151+ uint8_t dev_bus = 0 ;
152+ uint8_t port_path [MAX_USB_PORTS ];
153+ int path_len = 0 ;
154+
155+ #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
156+ path_len = libusb_get_port_numbers (dev , port_path , MAX_USB_PORTS );
157+ if (path_len == LIBUSB_ERROR_OVERFLOW ) {
158+ LOG_WARNING ("cannot determine path to usb device! (more than %i ports in path)" , MAX_USB_PORTS );
159+ return ERROR_FAIL ;
160+ }
161+ dev_bus = libusb_get_bus_number (dev );
162+ #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
163+
164+ len = snprintf (loc , loc_len , "%d" , dev_bus );
165+ if (len < 0 || len >= (loc_len - len )) {
166+ * loc = 0 ;
167+ return ERROR_FAIL ;
168+ }
169+
170+ for (k = 0 ; k < path_len ; k ++ ) {
171+ wr = snprintf (& loc [len ], loc_len - len , k == 0 ? "-%d" : ".%d" , port_path [k ]);
172+ if (wr < 0 || wr >= (loc_len - len )) {
173+ * loc = 0 ;
174+ return ERROR_FAIL ;
175+ }
176+ len += wr ;
177+ }
178+ return ERROR_OK ;
179+ }
180+
181+ int jtag_libusb_get_dev_location_by_handle (struct libusb_device_handle * dev , char * loc , int loc_len )
182+ {
183+ return jtag_libusb_get_dev_location (libusb_get_device (dev ), loc , loc_len );
184+ }
185+
186+ int jtag_libusb_get_devs_locations (const uint16_t vids [], const uint16_t pids [], char * * * locations )
187+ {
188+ int cnt , idx , devs_cnt = 0 ;
189+ struct libusb_device * * list ;
190+ struct libusb_device_descriptor desc ;
191+ char * * locs ;
192+ /* <bus>-<port>[.<port>]... */
193+ #define MAX_DEV_LOCATION_LEN 128
194+
195+ cnt = libusb_get_device_list (jtag_libusb_context , & list );
196+ if (cnt <= 0 ) {
197+ LOG_WARNING ("Cannot get devices list (%d)!" , cnt );
198+ return 0 ;
199+ }
200+
201+ locs = calloc (cnt , sizeof (char * ));
202+ if (!locs ) {
203+ LOG_ERROR ("Unable to allocate space USB devices list!" );
204+ libusb_free_device_list (list , 1 );
205+ return 0 ;
206+ }
207+ for (idx = 0 ; idx < cnt ; idx ++ ) {
208+ if (libusb_get_device_descriptor (list [idx ], & desc ) != 0 )
209+ continue ;
210+ if (!jtag_libusb_match_ids (& desc , vids , pids ))
211+ continue ;
212+
213+ locs [devs_cnt ] = calloc (1 , MAX_DEV_LOCATION_LEN );
214+ if (!locs [devs_cnt ]) {
215+ LOG_ERROR ("Unable to allocate space USB device location!" );
216+ jtag_libusb_free_devs_locations (locs , devs_cnt );
217+ libusb_free_device_list (list , true);
218+ return 0 ;
219+ }
220+ if (jtag_libusb_get_dev_location (list [idx ], locs [devs_cnt ], MAX_DEV_LOCATION_LEN ) != ERROR_OK )
221+ LOG_WARNING ("Cannot get location for usb device!" );
222+
223+ devs_cnt ++ ;
224+ }
225+ * locations = locs ;
226+
227+ libusb_free_device_list (list , true);
228+
229+ return devs_cnt ;
230+ }
231+
232+ void jtag_libusb_free_devs_locations (char * locations [], int cnt )
233+ {
234+ int i ;
235+
236+ if (!locations || cnt == 0 )
237+ return ;
238+
239+ for (i = 0 ; i < cnt ; i ++ ) {
240+ if (locations [i ])
241+ free (locations [i ]);
242+ }
243+ free (locations );
244+ }
245+
148246int jtag_libusb_open (const uint16_t vids [], const uint16_t pids [],
149247 const char * product , struct libusb_device_handle * * out ,
150248 adapter_get_alternate_serial_fn adapter_get_alternate_serial )
0 commit comments