Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* $(CROSS_COMPILE)cc -Wall -Wextra -g -lpthread -o testusb testusb.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2002 by David Brownell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (c) 2010 by Samsung Electronics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Michal Nazarewicz <mina86@mina86.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * This program issues ioctls to perform the tests implemented by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * kernel driver.  It can generate a variety of transfer patterns; you
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * should make sure to test both regular streaming and mixes of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * transfer sizes (including short transfers).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * For more information on how this can be used and on USB testing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * refer to <URL:http://www.linux-usb.org/usbtest/>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <ftw.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <pthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/usbdevice_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) /*-------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define	TEST_CASES	30
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) // FIXME make these public somewhere; usbdevfs.h?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) struct usbtest_param {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	// inputs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	unsigned		test_num;	/* 0..(TEST_CASES-1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	unsigned		iterations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	unsigned		length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	unsigned		vary;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	unsigned		sglen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	// outputs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct timeval		duration;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define USBTEST_REQUEST	_IOWR('U', 100, struct usbtest_param)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) /*-------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) /* #include <linux/usb_ch9.h> */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #define USB_DT_DEVICE			0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #define USB_DT_INTERFACE		0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #define USB_CLASS_PER_INTERFACE		0	/* for DeviceClass */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) #define USB_CLASS_VENDOR_SPEC		0xff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) struct usb_device_descriptor {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	__u8  bLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	__u8  bDescriptorType;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	__u16 bcdUSB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	__u8  bDeviceClass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	__u8  bDeviceSubClass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	__u8  bDeviceProtocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	__u8  bMaxPacketSize0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	__u16 idVendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	__u16 idProduct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	__u16 bcdDevice;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	__u8  iManufacturer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	__u8  iProduct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	__u8  iSerialNumber;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	__u8  bNumConfigurations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) } __attribute__ ((packed));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) struct usb_interface_descriptor {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	__u8  bLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	__u8  bDescriptorType;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	__u8  bInterfaceNumber;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	__u8  bAlternateSetting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	__u8  bNumEndpoints;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	__u8  bInterfaceClass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	__u8  bInterfaceSubClass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	__u8  bInterfaceProtocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	__u8  iInterface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) } __attribute__ ((packed));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) enum usb_device_speed {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	USB_SPEED_UNKNOWN = 0,			/* enumerating */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	USB_SPEED_LOW, USB_SPEED_FULL,		/* usb 1.1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	USB_SPEED_HIGH				/* usb 2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /*-------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static char *speed (enum usb_device_speed s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	switch (s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	case USB_SPEED_UNKNOWN:	return "unknown";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	case USB_SPEED_LOW:	return "low";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	case USB_SPEED_FULL:	return "full";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	case USB_SPEED_HIGH:	return "high";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	default:		return "??";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct testdev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	struct testdev		*next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	char			*name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	pthread_t		thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	enum usb_device_speed	speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	unsigned		ifnum : 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	unsigned		forever : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	int			test;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	struct usbtest_param	param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static struct testdev		*testdevs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static int testdev_ffs_ifnum(FILE *fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		char buf[255];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		struct usb_interface_descriptor intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	} u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		if (fread(u.buf, 1, 1, fd) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		if (fread(u.buf + 1, (unsigned char)u.buf[0] - 1, 1, fd) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		if (u.intf.bLength == sizeof u.intf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		 && u.intf.bDescriptorType == USB_DT_INTERFACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		 && u.intf.bNumEndpoints == 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		 && u.intf.bInterfaceClass == USB_CLASS_VENDOR_SPEC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		 && u.intf.bInterfaceSubClass == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		 && u.intf.bInterfaceProtocol == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			return (unsigned char)u.intf.bInterfaceNumber;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static int testdev_ifnum(FILE *fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	struct usb_device_descriptor dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	if (fread(&dev, sizeof dev, 1, fd) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (dev.bLength != sizeof dev || dev.bDescriptorType != USB_DT_DEVICE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	/* FX2 with (tweaked) bulksrc firmware */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (dev.idVendor == 0x0547 && dev.idProduct == 0x1002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	/*----------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	/* devices that start up using the EZ-USB default device and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	 * which we can use after loading simple firmware.  hotplug
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	 * can fxload it, and then run this test driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	 * we return false positives in two cases:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	 * - the device has a "real" driver (maybe usb-serial) that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	 *   renumerates.  the device should vanish quickly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	 * - the device doesn't have the test firmware installed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	/* generic EZ-USB FX controller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	if (dev.idVendor == 0x0547 && dev.idProduct == 0x2235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	/* generic EZ-USB FX2 controller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (dev.idVendor == 0x04b4 && dev.idProduct == 0x8613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	/* CY3671 development board with EZ-USB FX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (dev.idVendor == 0x0547 && dev.idProduct == 0x0080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	/* Keyspan 19Qi uses an21xx (original EZ-USB) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (dev.idVendor == 0x06cd && dev.idProduct == 0x010b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	/*----------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	/* "gadget zero", Linux-USB test software */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if (dev.idVendor == 0x0525 && dev.idProduct == 0xa4a0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	/* user mode subset of that */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (dev.idVendor == 0x0525 && dev.idProduct == 0xa4a4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		return testdev_ffs_ifnum(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		/* return 0; */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	/* iso version of usermode code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	if (dev.idVendor == 0x0525 && dev.idProduct == 0xa4a3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	/* some GPL'd test firmware uses these IDs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	if (dev.idVendor == 0xfff0 && dev.idProduct == 0xfff0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	/*----------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	/* iBOT2 high speed webcam */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	if (dev.idVendor == 0x0b62 && dev.idProduct == 0x0059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	/*----------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	/* the FunctionFS gadget can have the source/sink interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	 * anywhere.  We look for an interface descriptor that match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	 * what we expect.  We ignore configuratiens thou. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (dev.idVendor == 0x0525 && dev.idProduct == 0xa4ac
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	 && (dev.bDeviceClass == USB_CLASS_PER_INTERFACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	  || dev.bDeviceClass == USB_CLASS_VENDOR_SPEC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		return testdev_ffs_ifnum(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static int find_testdev(const char *name, const struct stat *sb, int flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	FILE				*fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	int				ifnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	struct testdev			*entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	(void)sb; /* unused */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	if (flag != FTW_F)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	fd = fopen(name, "rb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (!fd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		perror(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	ifnum = testdev_ifnum(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	fclose(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	if (ifnum < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	entry = calloc(1, sizeof *entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		goto nomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	entry->name = strdup(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	if (!entry->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		free(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) nomem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		perror("malloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	entry->ifnum = ifnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	entry->next = testdevs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	testdevs = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) usbdev_ioctl (int fd, int ifno, unsigned request, void *param)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	struct usbdevfs_ioctl	wrapper;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	wrapper.ifno = ifno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	wrapper.ioctl_code = request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	wrapper.data = param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	return ioctl (fd, USBDEVFS_IOCTL, &wrapper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static void *handle_testdev (void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	struct testdev		*dev = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	int			fd, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	int			status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	if ((fd = open (dev->name, O_RDWR)) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		perror ("can't open dev file r/w");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	status  =  ioctl(fd, USBDEVFS_GET_SPEED, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		fprintf(stderr, "USBDEVFS_GET_SPEED failed %d\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		dev->speed = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	fprintf(stderr, "%s speed\t%s\t%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			speed(dev->speed), dev->name, dev->ifnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) restart:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	for (i = 0; i < TEST_CASES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		if (dev->test != -1 && dev->test != i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		dev->param.test_num = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		status = usbdev_ioctl (fd, dev->ifnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 				USBTEST_REQUEST, &dev->param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		if (status < 0 && errno == EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		/* FIXME need a "syslog it" option for background testing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		/* NOTE: each thread emits complete lines; no fragments! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			char	buf [80];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			int	err = errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			if (strerror_r (errno, buf, sizeof buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 				snprintf (buf, sizeof buf, "error %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 				errno = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 			printf ("%s test %d --> %d (%s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 				dev->name, i, errno, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			printf ("%s test %d, %4d.%.06d secs\n", dev->name, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 				(int) dev->param.duration.tv_sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 				(int) dev->param.duration.tv_usec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		fflush (stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	if (dev->forever)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	close (fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	return arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static const char *usb_dir_find(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	static char udev_usb_path[] = "/dev/bus/usb";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	if (access(udev_usb_path, F_OK) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		return udev_usb_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static int parse_num(unsigned *num, const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	char *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	errno = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	val = strtoul(str, &end, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	if (errno || *end || val > UINT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	*num = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) int main (int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	int			c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	struct testdev		*entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	char			*device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	const char		*usb_dir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	int			all = 0, forever = 0, not = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	int			test = -1 /* all */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	struct usbtest_param	param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	/* pick defaults that works with all speeds, without short packets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	 * Best per-frame data rates:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	 *     super speed,bulk      1024 * 16 * 8 = 131072
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	 *                 interrupt 1024 *  3 * 8 =  24576
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	 *     high speed, bulk       512 * 13 * 8 =  53248
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	 *                 interrupt 1024 *  3 * 8 =  24576
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	 *     full speed, bulk/intr   64 * 19     =   1216
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	 *                 interrupt   64 *  1     =     64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	 *      low speed, interrupt    8 *  1     =      8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	param.iterations = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	param.length = 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	param.vary = 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	param.sglen = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	/* for easy use when hotplugging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	device = getenv ("DEVICE");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	while ((c = getopt (argc, argv, "D:aA:c:g:hlns:t:v:")) != EOF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	switch (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	case 'D':	/* device, if only one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		device = optarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	case 'A':	/* use all devices with specified USB dir */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		usb_dir = optarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		/* FALL THROUGH */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	case 'a':	/* use all devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		device = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		all = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	case 'c':	/* count iterations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		if (parse_num(&param.iterations, optarg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 			goto usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	case 'g':	/* scatter/gather entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		if (parse_num(&param.sglen, optarg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 			goto usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	case 'l':	/* loop forever */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		forever = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	case 'n':	/* no test running! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		not = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	case 's':	/* size of packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		if (parse_num(&param.length, optarg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 			goto usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	case 't':	/* run just one test */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		test = atoi (optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		if (test < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 			goto usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	case 'v':	/* vary packet size by ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		if (parse_num(&param.vary, optarg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 			goto usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	case '?':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	case 'h':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) usage:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		fprintf (stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 			"usage: %s [options]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 			"Options:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			"\t-D dev		only test specific device\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			"\t-A usb-dir\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 			"\t-a		test all recognized devices\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 			"\t-l		loop forever(for stress test)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 			"\t-t testnum	only run specified case\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 			"\t-n		no test running, show devices to be tested\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 			"Case arguments:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 			"\t-c iterations		default 1000\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 			"\t-s transfer length	default 1024\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 			"\t-g sglen		default 32\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 			"\t-v vary			default 1024\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 			argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	if (optind != argc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		goto usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	if (!all && !device) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		fprintf (stderr, "must specify '-a' or '-D dev', "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 			"or DEVICE=/dev/bus/usb/BBB/DDD in env\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		goto usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	/* Find usb device subdirectory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	if (!usb_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		usb_dir = usb_dir_find();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		if (!usb_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			fputs ("USB device files are missing\n", stderr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	/* collect and list the test devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	if (ftw (usb_dir, find_testdev, 3) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		fputs ("ftw failed; are USB device files missing?\n", stderr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	/* quit, run single test, or create test threads */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	if (!testdevs && !device) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		fputs ("no test devices recognized\n", stderr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	if (not)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	if (testdevs && testdevs->next == 0 && !device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		device = testdevs->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	for (entry = testdevs; entry; entry = entry->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		int	status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		entry->param = param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		entry->forever = forever;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		entry->test = test;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		if (device) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 			if (strcmp (entry->name, device))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 			return handle_testdev (entry) != entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		status = pthread_create (&entry->thread, 0, handle_testdev, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 			perror ("pthread_create");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	if (device) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		struct testdev		dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		/* kernel can recognize test devices we don't */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		fprintf (stderr, "%s: %s may see only control tests\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 				argv [0], device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		memset (&dev, 0, sizeof dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		dev.name = device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		dev.param = param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		dev.forever = forever;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		dev.test = test;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		return handle_testdev (&dev) != &dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	/* wait for tests to complete */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	for (entry = testdevs; entry; entry = entry->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		void	*retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		if (pthread_join (entry->thread, &retval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 			perror ("pthread_join");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		/* testing errors discarded! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) }