Kévin Redon | efbcf38 | 2018-07-07 17:35:15 +0200 | [diff] [blame] | 1 | /* simtrace2_usb - host PC application to list found SIMtrace 2 USB devices |
| 2 | * |
| 3 | * (C) 2016 by Harald Welte <hwelte@hmw-consulting.de> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
Harald Welte | 822d66e | 2017-03-06 20:58:03 +0100 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdint.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <errno.h> |
| 23 | |
| 24 | #include <osmocom/core/utils.h> |
| 25 | |
| 26 | #include "libusb_util.h" |
Kévin Redon | e2f84f6 | 2018-07-01 18:19:36 +0200 | [diff] [blame] | 27 | #include "simtrace_usb.h" |
Harald Welte | 822d66e | 2017-03-06 20:58:03 +0100 | [diff] [blame] | 28 | |
| 29 | static const struct dev_id compatible_dev_ids[] = { |
| 30 | { USB_VENDOR_OPENMOKO, USB_PRODUCT_OWHW_SAM3 }, |
| 31 | { USB_VENDOR_OPENMOKO, USB_PRODUCT_QMOD_SAM3 }, |
| 32 | { USB_VENDOR_OPENMOKO, USB_PRODUCT_SIMTRACE2 }, |
| 33 | { 0, 0 } |
| 34 | }; |
| 35 | |
Harald Welte | 822d66e | 2017-03-06 20:58:03 +0100 | [diff] [blame] | 36 | static int find_devices(void) |
| 37 | { |
| 38 | struct usb_interface_match ifm[16]; |
| 39 | int rc, i, num_interfaces; |
| 40 | |
Kévin Redon | e2f84f6 | 2018-07-01 18:19:36 +0200 | [diff] [blame] | 41 | /* scan for USB devices matching SIMtrace USB ID with proprietary class */ |
Harald Welte | 822d66e | 2017-03-06 20:58:03 +0100 | [diff] [blame] | 42 | rc = usb_match_interfaces(NULL, compatible_dev_ids, |
Kévin Redon | e2f84f6 | 2018-07-01 18:19:36 +0200 | [diff] [blame] | 43 | USB_CLASS_PROPRIETARY, -1, -1, ifm, ARRAY_SIZE(ifm)); |
| 44 | printf("USB matches: %d\n", rc); |
Harald Welte | 822d66e | 2017-03-06 20:58:03 +0100 | [diff] [blame] | 45 | if (rc < 0) |
| 46 | return rc; |
| 47 | num_interfaces = rc; |
| 48 | |
| 49 | for (i = 0; i < num_interfaces; i++) { |
| 50 | struct usb_interface_match *m = &ifm[i]; |
| 51 | libusb_device_handle *dev_handle; |
| 52 | char strbuf[256]; |
| 53 | |
Harald Welte | a601635 | 2017-05-11 01:31:45 +0200 | [diff] [blame] | 54 | printf("\t%04x:%04x Addr=%u, Path=%s, Cfg=%u, Intf=%u, Alt=%u: %d/%d/%d ", |
| 55 | m->vendor, m->product, m->addr, m->path, |
Harald Welte | 822d66e | 2017-03-06 20:58:03 +0100 | [diff] [blame] | 56 | m->configuration, m->interface, m->altsetting, |
| 57 | m->class, m->sub_class, m->protocol); |
| 58 | |
| 59 | rc = libusb_open(m->usb_dev, &dev_handle); |
| 60 | if (rc < 0) { |
| 61 | printf("\n"); |
| 62 | perror("Cannot open device"); |
| 63 | continue; |
| 64 | } |
Harald Welte | 9f38ddd | 2017-05-10 22:49:02 +0200 | [diff] [blame] | 65 | rc = libusb_get_string_descriptor_ascii(dev_handle, m->string_idx, |
| 66 | (unsigned char *)strbuf, sizeof(strbuf)); |
Harald Welte | 822d66e | 2017-03-06 20:58:03 +0100 | [diff] [blame] | 67 | libusb_close(dev_handle); |
| 68 | if (rc < 0) { |
| 69 | printf("\n"); |
| 70 | perror("Cannot read string"); |
| 71 | continue; |
| 72 | } |
| 73 | printf("(%s)\n", strbuf); |
| 74 | #if 0 |
| 75 | dev_handle = usb_open_claim_interface(NULL, m); |
| 76 | printf("dev_handle=%p\n", dev_handle); |
| 77 | libusb_close(dev_handle); |
| 78 | #endif |
| 79 | } |
| 80 | |
| 81 | return num_interfaces; |
| 82 | } |
| 83 | |
| 84 | int main(int argc, char **argv) |
| 85 | { |
| 86 | libusb_init(NULL); |
| 87 | find_devices(); |
| 88 | } |