xref: /aosp_15_r20/external/coreboot/src/drivers/usb/console.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <console/usb.h>
4 #include "ehci_debug.h"
5 
usbdebug_tx_byte(struct dbgp_pipe * pipe,unsigned char data)6 static void usbdebug_tx_byte(struct dbgp_pipe *pipe, unsigned char data)
7 {
8 	if (!dbgp_try_get(pipe))
9 		return;
10 	pipe->buf[pipe->bufidx++] = data;
11 	if (pipe->bufidx >= 8) {
12 		dbgp_bulk_write_x(pipe, pipe->buf, pipe->bufidx);
13 		pipe->bufidx = 0;
14 	}
15 	dbgp_put(pipe);
16 }
17 
usbdebug_tx_flush(struct dbgp_pipe * pipe)18 static void usbdebug_tx_flush(struct dbgp_pipe *pipe)
19 {
20 	if (!dbgp_try_get(pipe))
21 		return;
22 	if (pipe->bufidx > 0) {
23 		dbgp_bulk_write_x(pipe, pipe->buf, pipe->bufidx);
24 		pipe->bufidx = 0;
25 	}
26 	dbgp_put(pipe);
27 }
28 
usbdebug_rx_byte(struct dbgp_pipe * pipe)29 static unsigned char usbdebug_rx_byte(struct dbgp_pipe *pipe)
30 {
31 	unsigned char data = 0xff;
32 	if (!dbgp_try_get(pipe))
33 		return 0xff;
34 	while (pipe->bufidx >= pipe->buflen) {
35 		pipe->buflen = 0;
36 		pipe->bufidx = 0;
37 		int count = dbgp_bulk_read_x(pipe, pipe->buf, 8);
38 		if (count>0)
39 			pipe->buflen = count;
40 	}
41 	data = pipe->buf[pipe->bufidx++];
42 	dbgp_put(pipe);
43 	return data;
44 }
45 
usb_tx_byte(int idx,unsigned char data)46 void usb_tx_byte(int idx, unsigned char data)
47 {
48 	usbdebug_tx_byte(dbgp_console_output(), data);
49 }
50 
usb_tx_flush(int idx)51 void usb_tx_flush(int idx)
52 {
53 	usbdebug_tx_flush(dbgp_console_output());
54 }
55 
usb_rx_byte(int idx)56 unsigned char usb_rx_byte(int idx)
57 {
58 	return usbdebug_rx_byte(dbgp_console_input());
59 }
60 
usb_can_rx_byte(int idx)61 int usb_can_rx_byte(int idx)
62 {
63 	return dbgp_ep_is_active(dbgp_console_input());
64 }
65