--- zzzz-none-000/linux-5.15.111/drivers/usb/core/hub.c 2023-05-11 14:00:40.000000000 +0000 +++ puma7-atom-6670-761/linux-5.15.111/drivers/usb/core/hub.c 2024-02-07 10:23:21.000000000 +0000 @@ -37,6 +37,10 @@ #include "hub.h" #include "otg_productlist.h" +/* == 20170828 AVM/VGJ - CHANGESET: AVM Power Meter == */ +#ifdef CONFIG_AVM_POWERMETER +#include +#endif /*--- #ifdef CONFIG_AVM_POWERMETER ---*/ #define USB_VENDOR_GENESYS_LOGIC 0x05e3 #define USB_VENDOR_SMSC 0x0424 @@ -53,6 +57,42 @@ #define USB_TP_TRANSMISSION_DELAY_MAX 65535 /* ns */ #define USB_PING_RESPONSE_TIME 400 /* ns */ +/* 20171012 AVM/VGJ USB KPI */ +#if defined (CONFIG_AVM_KERNEL) + +#define AVM_RECONNECT_ERROR_MS_THRESHOLD 2500 + +unsigned long disconnect_time[2]; +unsigned int disconnect_device_id[2]; + +int reconnect_count = 0; +module_param(reconnect_count, int, S_IRUGO | S_IWUSR); +MODULE_PARM_DESC(reconnect_count, "Count of reconnects"); + +int enum_error_count = 0; +module_param(enum_error_count, int, S_IRUGO | S_IWUSR); +MODULE_PARM_DESC(enum_error_count, "Count of enumeration errors"); + +int overcurrent_error_count = 0; +module_param(overcurrent_error_count, int, S_IRUGO | S_IWUSR); +MODULE_PARM_DESC(overcurrent_error_count, "Count of overcurrent errors"); + +#if defined (CONFIG_AVM_USB_PM) +int suspend_count = 0; +module_param(suspend_count, int, S_IRUGO | S_IWUSR); +MODULE_PARM_DESC(suspend_count, "Count of suspends"); + +int reset_resume_count = 0; +module_param(reset_resume_count, int, S_IRUGO | S_IWUSR); +MODULE_PARM_DESC(reset_resume_count, "Count of resets during resume"); + +int resume_error_count = 0; +module_param(resume_error_count, int, S_IRUGO | S_IWUSR); +MODULE_PARM_DESC(resume_error_count, "Count of failed resumes"); +#endif + +#endif + /* Protect struct usb_device->state and ->children members * Note: Both are also protected by ->dev.sem, except that ->state can * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */ @@ -2219,8 +2259,42 @@ */ pm_runtime_barrier(&udev->dev); +/* == 20170828 AVM/VGJ - CHANGESET: AVM Power Meter == */ +/* == 20220531 AVM/WKR - Order of powerdevices swapped for 6660 == */ +#if defined(CONFIG_AVM_POWERMETER) + if (udev->level == 1) { + unsigned nextmA = 0; + unsigned avm_powerdevice; + + avm_powerdevice = (udev->portnum & 1)? powerdevice_usb_host2 : powerdevice_usb_host; + printk (KERN_INFO "Port#%u disconnect: AVM Powermeter changed to %u mA\n", udev->portnum, nextmA); + PowerManagmentRessourceInfo(avm_powerdevice, nextmA); + } +#endif // CONFIG_AVM_POWERMETER + +/* 20171012 AVM/VGJ store disconnect timestamp to check possible reconnects */ +#if defined (CONFIG_AVM_KERNEL) + // Only check reconnects of devices directly connected to root-hub + if (udev->level == 1) { + int port_index; + unsigned int device_id; + port_index = (udev->portnum & 1)? 0 : 1; + disconnect_time[port_index] = jiffies; + device_id = (udev->descriptor.idVendor << 16) | udev->descriptor.idProduct; + disconnect_device_id[port_index] = device_id; + } +#endif + usb_lock_device(udev); +#ifdef CONFIG_AVM_USB_PM + /* == 20180305 AVM/VGJ - Blacklist device, if disconnected recently after suspend == */ + if ((udev->dev.power.runtime_status == RPM_SUSPENDED) && ((jiffies - udev->dev.power.accounting_timestamp) < msecs_to_jiffies(1000))) { + suspend_error_count++; + avm_usb_pm_add_to_blacklist(udev); + } +#endif + hub_disconnect_children(udev); /* deallocate hcd/hardware state ... nuking all pending urbs and @@ -2548,6 +2622,50 @@ /* Tell the world! */ announce_device(udev); +/* == 20170828 AVM/VGJ - CHANGESET: AVM Power Meter == */ +/* == 20220531 AVM/WKR - Order of powerdevices swapped for 6660 == */ +#if defined(CONFIG_AVM_POWERMETER) + if (udev->level == 1) { + unsigned nextmA = (udev->speed == USB_SPEED_SUPER)? 150: 100; + unsigned avm_powerdevice; + + avm_powerdevice = (udev->portnum & 1)? powerdevice_usb_host2 : powerdevice_usb_host; + printk (KERN_INFO "Port#%u connect: AVM Powermeter changed to %u mA\n", udev->portnum, nextmA); + PowerManagmentRessourceInfo(avm_powerdevice, nextmA); + } +#endif // CONFIG_AVM_POWERMETER + +/* 20171012 AVM/VGJ If connect comes too fast after disconnect -> reconnect error */ +#if defined (CONFIG_AVM_KERNEL) + // Only check reconnects of devices directly connected to root-hub + if (udev->level == 1) { + unsigned long jiffies_diff; + unsigned int device_id; + int port_index; + port_index = (udev->portnum & 1)? 0 : 1; + // Check device_id to make sure that the stored time belongs to the same device + device_id = (udev->descriptor.idVendor << 16) | udev->descriptor.idProduct; + if ( device_id == disconnect_device_id[port_index] ) { + jiffies_diff = jiffies - disconnect_time[port_index]; + if ( jiffies_diff < msecs_to_jiffies(AVM_RECONNECT_ERROR_MS_THRESHOLD) ) { + printk(KERN_ERR "Spontaneous reconnect\n"); + reconnect_count++; + } + } + } +#endif + +/* == 20171115 AVM/VGJ Prevent suspension of blacklisted devices == */ +#if defined (CONFIG_AVM_USB_PM) + if (avm_usb_pm_is_blacklisted(udev)) { + pm_runtime_set_autosuspend_delay(&udev->dev, -1); + printk(KERN_INFO "USB device %04x:%04x:%04x is blacklisted for suspension\n", + le16_to_cpu(udev->descriptor.idVendor), + le16_to_cpu(udev->descriptor.idProduct), + le16_to_cpu(udev->descriptor.bcdDevice)); + } +#endif + if (udev->serial) add_device_randomness(udev->serial, strlen(udev->serial)); if (udev->product) @@ -3135,7 +3253,12 @@ /* pass */; } /* Is the device still present? */ - else if (status || port_is_suspended(hub, portstatus) || +#if defined (CONFIG_AVM_KERNEL) + /* AVM/VGJ 20180420 Do not treat port_is_suspended as No Device Error */ + if (status || +#else + if (status || port_is_suspended(hub, portstatus) || +#endif !port_is_power_on(hub, portstatus)) { if (status >= 0) status = -ENODEV; @@ -3147,6 +3270,14 @@ goto retry; } status = -ENODEV; +#if defined (CONFIG_AVM_KERNEL) + /* AVM/VGJ 20180420 follow up port_is_suspended with reset resume */ + } else if (port_is_suspended(hub, portstatus)) { + dev_dbg(hub->intfdev, + "port %d status %04x.%04x still suspended\n", + port1, portchange, portstatus); + udev->reset_resume = 1; +#endif } /* Can't do a normal resume if the port isn't enabled, @@ -3434,12 +3565,30 @@ status = 0; } else { suspend_done: - dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n", + /* == 20170929 AVM/VGJ - Show also without CONFIG_USB_DEBUG == */ + dev_info(&udev->dev, "usb %ssuspend, wakeup %d\n", (PMSG_IS_AUTO(msg) ? "auto-" : ""), udev->do_remote_wakeup); if (really_suspend) { udev->port_is_suspended = 1; +#if defined (CONFIG_AVM_USB_PM) + suspend_count++; +#endif + +/* == 20170929 AVM/VGJ - CHANGESET: AVM Power Meter change for suspended device == */ +/* == 20220531 AVM/WKR - Order of powerdevices swapped for 6660 == */ +#if defined(CONFIG_AVM_POWERMETER) && defined (CONFIG_AVM_USB_PM) + if (udev->level == 1) { + unsigned nextmA = 50; + unsigned avm_powerdevice; + + avm_powerdevice = (udev->portnum & 1)? powerdevice_usb_host2 : powerdevice_usb_host; + printk (KERN_INFO "Port#%u suspend: AVM Powermeter changed to %u mA\n", udev->portnum, nextmA); + PowerManagmentRessourceInfo(avm_powerdevice, nextmA); + } +#endif // CONFIG_AVM_POWERMETER + /* device has up to 10 msec to fully suspend */ msleep(10); } @@ -3473,7 +3622,8 @@ u16 devstatus = 0; /* caller owns the udev device lock */ - dev_dbg(&udev->dev, "%s\n", + /* == 20170929 AVM/VGJ - Show also without CONFIG_USB_DEBUG == */ + dev_info(&udev->dev, "%s\n", udev->reset_resume ? "finish reset-resume" : "finish resume"); /* usb ch9 identifies four variants of SUSPENDED, based on what @@ -3520,8 +3670,15 @@ } } +#if defined (CONFIG_AVM_USB_PM) + if (udev->reset_resume) { + reset_resume_count++; + } +#endif + if (status) { - dev_dbg(&udev->dev, "gone after usb resume? status %d\n", + /* == 20170929 AVM/VGJ - Show also without CONFIG_USB_DEBUG == */ + dev_info(&udev->dev, "gone after usb resume? status %d\n", status); /* * There are a few quirky devices which violate the standard @@ -3666,7 +3823,8 @@ dev_dbg(&port_dev->dev, "can't resume, status %d\n", status); } else { /* drive resume for USB_RESUME_TIMEOUT msec */ - dev_dbg(&udev->dev, "usb %sresume\n", + /* == 20170929 AVM/VGJ - Show also without CONFIG_USB_DEBUG == */ + dev_info(&udev->dev, "usb %sresume\n", (PMSG_IS_AUTO(msg) ? "auto-" : "")); msleep(USB_RESUME_TIMEOUT); @@ -3692,6 +3850,32 @@ /* TRSMRCY = 10 msec */ msleep(10); + +/* == 20170929 AVM/VGJ - CHANGESET: AVM Power Meter change for resumed device == */ +/* == 20220531 AVM/WKR - Order of powerdevices swapped for 6660 == */ +#if defined(CONFIG_AVM_POWERMETER) && defined (CONFIG_AVM_USB_PM) + if (udev->level == 1) { + unsigned nextmA; + unsigned avm_powerdevice; + + if (udev->speed == USB_SPEED_SUPER) { + nextmA = (udev->config->desc.bMaxPower * 8); + if (nextmA <= 150) { + nextmA = 150; + } + } else { + nextmA = (udev->config->desc.bMaxPower * 2); + /* A device should display at least 100 mA in AVM_POWERMETER */ + if (nextmA < 100) { + nextmA = 100; + } + } + + avm_powerdevice = (udev->portnum & 1)? powerdevice_usb_host2 : powerdevice_usb_host; + printk (KERN_INFO "Port#%u resume: AVM Powermeter changed to %u mA\n", udev->portnum, nextmA); + PowerManagmentRessourceInfo(avm_powerdevice, nextmA); + } +#endif // CONFIG_AVM_POWERMETER } if (udev->persist_enabled) @@ -3703,7 +3887,12 @@ if (status == 0) status = finish_port_resume(udev); if (status < 0) { - dev_dbg(&udev->dev, "can't resume, status %d\n", status); +#if defined (CONFIG_AVM_USB_PM) + avm_usb_pm_add_to_blacklist(udev); + resume_error_count++; +#endif + /* == 20170929 AVM/VGJ - Show also without CONFIG_USB_DEBUG == */ + dev_info(&udev->dev, "can't resume, status %d\n", status); hub_port_logical_disconnect(hub, port1); } else { /* Try to enable USB2 hardware LPM */ @@ -5404,9 +5593,14 @@ if (hub->hdev->parent || !hcd->driver->port_handed_over || !(hcd->driver->port_handed_over)(hcd, port1)) { - if (status != -ENOTCONN && status != -ENODEV) + if (status != -ENOTCONN && status != -ENODEV) { dev_err(&port_dev->dev, "unable to enumerate USB device\n"); +/* 20171012 AVM/VGJ increment enumeration error */ +#if defined (CONFIG_AVM_KERNEL) + enum_error_count++; +#endif + } } done: @@ -5591,6 +5785,12 @@ msleep(100); /* Cool down */ hub_power_on(hub, true); hub_port_status(hub, port1, &status, &unused); +/* 20171012 AVM/VGJ increment overcurrent error */ +#if defined (CONFIG_AVM_KERNEL) + // Only count overcurrent events for root-hub + if(hdev->level == 0) + overcurrent_error_count++; +#endif if (status & USB_PORT_STAT_OVERCURRENT) dev_err(&port_dev->dev, "over-current condition\n"); } @@ -5757,6 +5957,12 @@ msleep(500); /* Cool down */ hub_power_on(hub, true); hub_hub_status(hub, &status, &unused); +/* 20171012 AVM/VGJ increment overcurrent error */ +#if defined (CONFIG_AVM_KERNEL) + // Only count overcurrent events for root-hub + if(hdev->level == 0) + overcurrent_error_count++; +#endif if (status & HUB_STATUS_OVERCURRENT) dev_err(hub_dev, "over-current condition\n"); }