@samitouri / QOSamiQemu / commits / f94c2a3415

hw/usb/hcd-ehci.c: Replace fprintf(stderr, ...) with qemu_log_mask(LOG_GUEST_ERROR)

Replace direct fprintf(stderr, ...) calls with qemu_log_mask() using LOG_GUEST_ERROR in the EHCI controller implementation. Direct writes to stderr are discouraged in QEMU as they bypass the logging framework and cannot be filtered or controlled at runtime. Using qemu_log_mask() ensures that guest-visible errors are properly categorized and can be managed via QEMU logging options. All affected messages correspond to guest-triggerable error conditions, so LOG_GUEST_ERROR is used consistently. This change improves integration with QEMU's logging infrastructure and aligns with upstream coding guidelines. Signed-off-by: Jamin Lin <jamin_lin@aspeedtech.com> Reviewed-by: Cédric Le Goater <clg@redhat.com> Message-ID: <20260424080508.53992-6-jamin_lin@aspeedtech.com> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>

Jamin Lin committed Apr 23, 2026 at 07:18 UTC f94c2a3415423683e9fe252798becfaca3a938d8
1 file changed +25 -21
hw/usb/hcd-ehci.c
+25 -21
@@ -36,6 +36,7 @@
36 #include "qemu/error-report.h"
37 #include "qemu/main-loop.h"
38 #include "system/runstate.h"
39 +#include "qemu/log.h"
40
41 #define FRAME_TIMER_FREQ 1000
42 #define FRAME_TIMER_NS (NANOSECONDS_PER_SECOND / FRAME_TIMER_FREQ)
@@ -425,7 +426,7 @@ static int ehci_get_pid(EHCIqtd *qtd)
426 case 2:
427 return USB_TOKEN_SETUP;
428 default:
428 - fprintf(stderr, "bad token\n");
429 + qemu_log_mask(LOG_GUEST_ERROR, "bad token\n");
430 return 0;
431 }
432 }
@@ -532,7 +533,7 @@ static void ehci_free_packet(EHCIPacket *p)
533 }
534 if (p->async == EHCI_ASYNC_FINISHED &&
535 p->packet.status == USB_RET_SUCCESS) {
535 - fprintf(stderr,
536 + qemu_log_mask(LOG_GUEST_ERROR,
537 "EHCI: Dropping completed packet from halted %s ep %02X\n",
538 (p->pid == USB_TOKEN_IN) ? "in" : "out",
539 get_field(p->queue->qh.epchar, QH_EPCHAR_EP));
@@ -1042,8 +1043,9 @@ static void ehci_opreg_write(void *ptr, hwaddr addr,
1043
1044 /* not supporting dynamic frame list size at the moment */
1045 if ((val & USBCMD_FLS) && !(s->usbcmd & USBCMD_FLS)) {
1045 - fprintf(stderr, "attempt to set frame list size -- value %d\n",
1046 - (int)val & USBCMD_FLS);
1046 + qemu_log_mask(LOG_GUEST_ERROR,
1047 + "attempt to set frame list size -- value %" PRId64
1048 + "\n", val & USBCMD_FLS);
1049 val &= ~USBCMD_FLS;
1050 }
1051
@@ -1101,7 +1103,7 @@ static void ehci_opreg_write(void *ptr, hwaddr addr,
1103
1104 case PERIODICLISTBASE:
1105 if (ehci_periodic_enabled(s)) {
1104 - fprintf(stderr,
1106 + qemu_log_mask(LOG_GUEST_ERROR,
1107 "ehci: PERIODIC list base register set while periodic schedule\n"
1108 " is enabled and HC is enabled\n");
1109 }
@@ -1109,7 +1111,7 @@ static void ehci_opreg_write(void *ptr, hwaddr addr,
1111
1112 case ASYNCLISTADDR:
1113 if (ehci_async_enabled(s)) {
1112 - fprintf(stderr,
1114 + qemu_log_mask(LOG_GUEST_ERROR,
1115 "ehci: ASYNC list address register set while async schedule\n"
1116 " is enabled and HC is enabled\n");
1117 }
@@ -1200,7 +1202,7 @@ static int ehci_init_transfer(EHCIPacket *p)
1202
1203 while (bytes > 0) {
1204 if (cpage > 4) {
1203 - fprintf(stderr, "cpage out of range (%u)\n", cpage);
1205 + qemu_log_mask(LOG_GUEST_ERROR, "cpage out of range (%u)\n", cpage);
1206 qemu_sglist_destroy(&p->sgl);
1207 return -1;
1208 }
@@ -1306,7 +1308,8 @@ static void ehci_execute_complete(EHCIQueue *q)
1308 break;
1309 default:
1310 /* should not be triggerable */
1309 - fprintf(stderr, "USB invalid response %d\n", p->packet.status);
1311 + qemu_log_mask(LOG_GUEST_ERROR, "USB invalid response %d\n",
1312 + p->packet.status);
1313 g_assert_not_reached();
1314 }
1315
@@ -1354,7 +1357,7 @@ static int ehci_execute(EHCIPacket *p, const char *action)
1357 p->async == EHCI_ASYNC_INITIALIZED);
1358
1359 if (!(p->qtd.token & QTD_TOKEN_ACTIVE)) {
1357 - fprintf(stderr, "Attempting to execute inactive qtd\n");
1360 + qemu_log_mask(LOG_GUEST_ERROR, "Attempting to execute inactive qtd\n");
1361 return -1;
1362 }
1363
@@ -1395,7 +1398,8 @@ static int ehci_execute(EHCIPacket *p, const char *action)
1398 p->packet.actual_length);
1399
1400 if (p->packet.actual_length > BUFF_SIZE) {
1398 - fprintf(stderr, "ret from usb_handle_packet > BUFF_SIZE\n");
1401 + qemu_log_mask(LOG_GUEST_ERROR,
1402 + "ret from usb_handle_packet > BUFF_SIZE\n");
1403 return -1;
1404 }
1405
@@ -1479,8 +1483,9 @@ static int ehci_process_itd(EHCIState *ehci,
1483 case USB_RET_SUCCESS:
1484 break;
1485 default:
1482 - fprintf(stderr, "Unexpected iso usb result: %d\n",
1483 - ehci->ipacket.status);
1486 + qemu_log_mask(LOG_GUEST_ERROR,
1487 + "Unexpected iso usb result: %d\n",
1488 + ehci->ipacket.status);
1489 /* Fall through */
1490 case USB_RET_IOERROR:
1491 case USB_RET_NODEV:
@@ -1584,7 +1589,8 @@ static int ehci_state_fetchentry(EHCIState *ehci, int async)
1589
1590 /* section 4.8, only QH in async schedule */
1591 if (async && (NLPTR_TYPE_GET(entry) != NLPTR_TYPE_QH)) {
1587 - fprintf(stderr, "non queue head request in async schedule\n");
1592 + qemu_log_mask(LOG_GUEST_ERROR,
1593 + "non queue head request in async schedule\n");
1594 return -1;
1595 }
1596
@@ -1606,8 +1612,10 @@ static int ehci_state_fetchentry(EHCIState *ehci, int async)
1612
1613 default:
1614 /* TODO: handle FSTN type */
1609 - fprintf(stderr, "FETCHENTRY: entry at %X is of type %u "
1610 - "which is not supported yet\n", entry, NLPTR_TYPE_GET(entry));
1615 + qemu_log_mask(LOG_GUEST_ERROR,
1616 + "FETCHENTRY: entry at 0x%x is of type %u "
1617 + "which is not supported yet\n",
1618 + entry, NLPTR_TYPE_GET(entry));
1619 return -1;
1620 }
1621
@@ -2118,13 +2126,13 @@ static void ehci_advance_state(EHCIState *ehci, int async)
2126 break;
2127
2128 default:
2121 - fprintf(stderr, "Bad state!\n");
2129 g_assert_not_reached();
2130 }
2131
2132 if (again < 0 || itd_count > 16) {
2133 /* TODO: notify guest (raise HSE irq?) */
2127 - fprintf(stderr, "processing error - resetting ehci HC\n");
2134 + qemu_log_mask(LOG_GUEST_ERROR,
2135 + "processing error - resetting ehci HC\n");
2136 ehci_reset(ehci);
2137 again = 0;
2138 }
@@ -2181,8 +2189,6 @@ static void ehci_advance_async_state(EHCIState *ehci)
2189
2190 default:
2191 /* this should only be due to a developer mistake */
2184 - fprintf(stderr, "ehci: Bad asynchronous state %d. "
2185 - "Resetting to active\n", ehci->astate);
2192 g_assert_not_reached();
2193 }
2194 }
@@ -2231,8 +2237,6 @@ static void ehci_advance_periodic_state(EHCIState *ehci)
2237
2238 default:
2239 /* this should only be due to a developer mistake */
2234 - fprintf(stderr, "ehci: Bad periodic state %d. "
2235 - "Resetting to active\n", ehci->pstate);
2240 g_assert_not_reached();
2241 }
2242 }