Wentong Wu | c35fa6c | 2020-02-04 04:05:20 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (c) 2019 Intel Corporation. |
| 4 | # |
| 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | """ |
| 7 | Script to capture tracing data with USB backend. |
| 8 | """ |
| 9 | |
| 10 | import usb.core |
| 11 | import usb.util |
| 12 | import argparse |
| 13 | import sys |
| 14 | |
| 15 | def parse_args(): |
| 16 | global args |
| 17 | parser = argparse.ArgumentParser( |
| 18 | description=__doc__, |
Jamie McCrae | ec70444 | 2023-01-04 16:08:36 +0000 | [diff] [blame] | 19 | formatter_class=argparse.RawDescriptionHelpFormatter, allow_abbrev=False) |
Wentong Wu | c35fa6c | 2020-02-04 04:05:20 +0800 | [diff] [blame] | 20 | parser.add_argument("-v", "--vendor_id", required=True, |
| 21 | help="usb device vendor id") |
| 22 | parser.add_argument("-p", "--product_id", required=True, |
| 23 | help="usb device product id") |
| 24 | parser.add_argument("-o", "--output", default='channel0_0', |
| 25 | required=False, help="tracing data output file") |
| 26 | args = parser.parse_args() |
| 27 | |
| 28 | def main(): |
| 29 | parse_args() |
| 30 | if args.vendor_id.isdecimal(): |
| 31 | vendor_id = int(args.vendor_id) |
| 32 | else: |
| 33 | vendor_id = int(args.vendor_id, 16) |
| 34 | |
| 35 | if args.product_id.isdecimal(): |
| 36 | product_id = int(args.product_id) |
| 37 | else: |
| 38 | product_id = int(args.product_id, 16) |
| 39 | |
| 40 | output_file = args.output |
| 41 | |
| 42 | try: |
| 43 | usb_device = usb.core.find(idVendor=vendor_id, idProduct=product_id) |
| 44 | except Exception as e: |
| 45 | sys.exit("{}".format(e)) |
| 46 | |
| 47 | if usb_device is None: |
| 48 | sys.exit("No device found, check vendor_id and product_id") |
| 49 | |
| 50 | if usb_device.is_kernel_driver_active(0): |
| 51 | try: |
| 52 | usb_device.detach_kernel_driver(0) |
| 53 | except usb.core.USBError as e: |
| 54 | sys.exit("{}".format(e)) |
| 55 | |
| 56 | # set the active configuration. With no arguments, the first |
| 57 | # configuration will be the active one |
| 58 | try: |
| 59 | usb_device.set_configuration() |
| 60 | except usb.core.USBError as e: |
| 61 | sys.exit("{}".format(e)) |
| 62 | |
| 63 | configuration = usb_device[0] |
| 64 | interface = configuration[(0, 0)] |
| 65 | |
| 66 | # match the only IN endpoint |
| 67 | read_endpoint = usb.util.find_descriptor(interface, custom_match = \ |
| 68 | lambda e: \ |
| 69 | usb.util.endpoint_direction( \ |
| 70 | e.bEndpointAddress) == \ |
| 71 | usb.util.ENDPOINT_IN) |
| 72 | |
| 73 | # match the only OUT endpoint |
| 74 | write_endpoint = usb.util.find_descriptor(interface, custom_match = \ |
| 75 | lambda e: \ |
| 76 | usb.util.endpoint_direction( \ |
| 77 | e.bEndpointAddress) == \ |
| 78 | usb.util.ENDPOINT_OUT) |
| 79 | |
| 80 | usb.util.claim_interface(usb_device, interface) |
| 81 | |
| 82 | #enable device tracing |
| 83 | write_endpoint.write('enable') |
| 84 | |
| 85 | #try to read to avoid garbage mixed to useful stream data |
| 86 | buff = usb.util.create_buffer(8192) |
| 87 | read_endpoint.read(buff, 10000) |
| 88 | |
| 89 | with open(output_file, "wb") as file_desc: |
| 90 | while True: |
| 91 | buff = usb.util.create_buffer(8192) |
| 92 | length = read_endpoint.read(buff, 100000) |
| 93 | for index in range(length): |
| 94 | file_desc.write(chr(buff[index]).encode('latin1')) |
| 95 | |
| 96 | usb.util.release_interface(usb_device, interface) |
| 97 | |
| 98 | if __name__=="__main__": |
| 99 | try: |
| 100 | main() |
| 101 | except KeyboardInterrupt: |
| 102 | print('Data capture interrupted, data saved into {}'.format(args.output)) |
| 103 | sys.exit(0) |