iPXE
uart.h
Go to the documentation of this file.
1 #ifndef _IPXE_UART_H
2 #define _IPXE_UART_H
3 
4 /** @file
5  *
6  * Generic UART
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 
12 #include <stdint.h>
13 #include <ipxe/refcnt.h>
14 #include <ipxe/list.h>
15 
16 /** A generic UART */
17 struct uart {
18  /** Reference count */
19  struct refcnt refcnt;
20  /** Name */
21  const char *name;
22  /** List of registered UARTs */
23  struct list_head list;
24 
25  /** Baud rate (if specified) */
26  unsigned int baud;
27 
28  /** UART operations */
30  /** Driver-private data */
31  void *priv;
32 };
33 
34 /** UART operations */
36  /**
37  * Transmit byte
38  *
39  * @v uart UART
40  * @v byte Byte to transmit
41  * @ret rc Return status code
42  */
43  void ( * transmit ) ( struct uart *uart, uint8_t byte );
44  /**
45  * Check if data is ready
46  *
47  * @v uart UART
48  * @ret ready Data is ready
49  */
50  int ( * data_ready ) ( struct uart *uart );
51  /**
52  * Receive byte
53  *
54  * @v uart UART
55  * @ret byte Received byte
56  */
57  uint8_t ( * receive ) ( struct uart *uart );
58  /**
59  * Initialise UART
60  *
61  * @v uart UART
62  * @ret rc Return status code
63  */
64  int ( * init ) ( struct uart *uart );
65  /**
66  * Flush transmitted data
67  *
68  * @v uart UART
69  */
70  void ( * flush ) ( struct uart *uart );
71 };
72 
73 /**
74  * Transmit byte
75  *
76  * @v uart UART
77  * @v byte Byte to transmit
78  * @ret rc Return status code
79  */
80 static inline __attribute__ (( always_inline )) void
81 uart_transmit ( struct uart *uart, uint8_t byte ) {
82 
83  uart->op->transmit ( uart, byte );
84 }
85 
86 /**
87  * Check if data is ready
88  *
89  * @v uart UART
90  * @ret ready Data is ready
91  */
92 static inline __attribute__ (( always_inline )) int
93 uart_data_ready ( struct uart *uart ) {
94 
95  return uart->op->data_ready ( uart );
96 }
97 
98 /**
99  * Receive byte
100  *
101  * @v uart UART
102  * @ret byte Received byte
103  */
104 static inline __attribute__ (( always_inline )) uint8_t
105 uart_receive ( struct uart *uart ) {
106 
107  return uart->op->receive ( uart );
108 }
109 
110 /**
111  * Initialise UART
112  *
113  * @v uart UART
114  * @ret rc Return status code
115  */
116 static inline __attribute__ (( always_inline )) int
117 uart_init ( struct uart *uart ) {
118 
119  return uart->op->init ( uart );
120 }
121 
122 /**
123  * Flush transmitted data
124  *
125  * @v uart UART
126  */
127 static inline __attribute__ (( always_inline )) void
128 uart_flush ( struct uart *uart ) {
129 
130  uart->op->flush ( uart );
131 }
132 
133 extern struct list_head uarts;
135 
136 /**
137  * Get reference to UART
138  *
139  * @v uart UART
140  * @ret uart UART
141  */
142 static inline __attribute__ (( always_inline )) struct uart *
143 uart_get ( struct uart *uart ) {
144 
145  ref_get ( &uart->refcnt );
146  return uart;
147 }
148 
149 /**
150  * Drop reference to UART
151  *
152  * @v uart UART
153  */
154 static inline __attribute__ (( always_inline )) void
155 uart_put ( struct uart *uart ) {
156 
157  ref_put ( &uart->refcnt );
158 }
159 
160 /**
161  * Nullify UART
162  *
163  * @v uart UART
164  */
165 static inline __attribute__ (( always_inline )) void
166 uart_nullify ( struct uart *uart ) {
167 
169 }
170 
171 extern struct uart * alloc_uart ( size_t priv_len );
172 extern int uart_register ( struct uart *uart );
173 extern int uart_register_fixed ( void );
174 extern void uart_unregister ( struct uart *uart );
175 extern struct uart * uart_find ( const char *name );
176 
177 #endif /* _IPXE_UART_H */
const char * name
Definition: ath9k_hw.c:1984
A generic UART.
Definition: uart.h:17
static __attribute__((always_inline)) void uart_transmit(struct uart *uart
Transmit byte.
Definition: uart.h:92
int(* data_ready)(struct uart *uart)
Check if data is ready.
Definition: uart.h:50
int(* init)(struct uart *uart)
Initialise UART.
Definition: uart.h:64
A doubly-linked list entry (or list head)
Definition: list.h:18
A reference counter.
Definition: refcnt.h:26
const char * name
Name.
Definition: uart.h:21
struct list_head list
List of registered UARTs.
Definition: uart.h:23
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
unsigned int baud
Baud rate (if specified)
Definition: uart.h:26
void(* transmit)(struct uart *uart, uint8_t byte)
Transmit byte.
Definition: uart.h:43
struct uart * uart_find(const char *name)
Find named UART.
Definition: uart.c:130
UART operations.
Definition: uart.h:35
int uart_register_fixed(void)
Register fixed UARTs (when not provided by platform)
Definition: uart.c:91
static uint8_t byte
Definition: uart.h:81
Linked lists.
void(* flush)(struct uart *uart)
Flush transmitted data.
Definition: uart.h:70
#define ref_get(refcnt)
Get additional reference to object.
Definition: refcnt.h:92
void * priv
Driver-private data.
Definition: uart.h:31
unsigned char uint8_t
Definition: stdint.h:10
struct list_head uarts
struct uart * alloc_uart(size_t priv_len)
Allocate UART.
Definition: uart.c:74
void uart_unregister(struct uart *uart)
Unregister UART.
Definition: uart.c:117
int uart_register(struct uart *uart)
Register UART.
Definition: uart.c:102
struct uart_operations * op
UART operations.
Definition: uart.h:29
Reference counting.
struct refcnt refcnt
Reference count.
Definition: uart.h:19
uint8_t(* receive)(struct uart *uart)
Receive byte.
Definition: uart.h:57
struct uart_operations null_uart_operations
Null UART operations.
Definition: uart.c:60
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106