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
10FILE_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 */
17struct uart {
18 /** Reference count */
19 struct refcnt refcnt;
20 /** Name */
21 const char *name;
22 /** List of registered UARTs */
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 */
80static inline __attribute__ (( always_inline )) void
81uart_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 */
92static inline __attribute__ (( always_inline )) int
93uart_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 */
104static inline __attribute__ (( always_inline )) uint8_t
105uart_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 */
116static inline __attribute__ (( always_inline )) int
117uart_init ( struct uart *uart ) {
118
119 return uart->op->init ( uart );
120}
121
122/**
123 * Flush transmitted data
124 *
125 * @v uart UART
126 */
127static inline __attribute__ (( always_inline )) void
128uart_flush ( struct uart *uart ) {
129
130 uart->op->flush ( uart );
131}
132
133extern struct list_head uarts;
135
136/**
137 * Get reference to UART
138 *
139 * @v uart UART
140 * @ret uart UART
141 */
142static inline __attribute__ (( always_inline )) struct uart *
143uart_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 */
154static inline __attribute__ (( always_inline )) void
155uart_put ( struct uart *uart ) {
156
157 ref_put ( &uart->refcnt );
158}
159
160/**
161 * Nullify UART
162 *
163 * @v uart UART
164 */
165static inline __attribute__ (( always_inline )) void
166uart_nullify ( struct uart *uart ) {
167
169}
170
171extern struct uart * alloc_uart ( size_t priv_len );
172extern int uart_register ( struct uart *uart );
173extern int uart_register_fixed ( void );
174extern void uart_unregister ( struct uart *uart );
175extern struct uart * uart_find ( const char *name );
176
177#endif /* _IPXE_UART_H */
unsigned char uint8_t
Definition stdint.h:10
const char * name
Definition ath9k_hw.c:1986
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define __attribute__(x)
Definition compiler.h:10
Linked lists.
Reference counting.
#define ref_get(refcnt)
Get additional reference to object.
Definition refcnt.h:93
#define ref_put(refcnt)
Drop reference to object.
Definition refcnt.h:107
A doubly-linked list entry (or list head)
Definition list.h:19
UART operations.
Definition uart.h:35
void(* transmit)(struct uart *uart, uint8_t byte)
Transmit byte.
Definition uart.h:43
void(* flush)(struct uart *uart)
Flush transmitted data.
Definition uart.h:70
int(* init)(struct uart *uart)
Initialise UART.
Definition uart.h:64
int(* data_ready)(struct uart *uart)
Check if data is ready.
Definition uart.h:50
uint8_t(* receive)(struct uart *uart)
Receive byte.
Definition uart.h:57
A generic UART.
Definition uart.h:17
const char * name
Name.
Definition uart.h:21
struct list_head list
List of registered UARTs.
Definition uart.h:23
unsigned int baud
Baud rate (if specified)
Definition uart.h:26
struct uart_operations * op
UART operations.
Definition uart.h:29
void * priv
Driver-private data.
Definition uart.h:31
struct refcnt refcnt
Reference count.
Definition uart.h:19
struct uart_operations null_uart_operations
Null UART operations.
Definition uart.c:60
void uart_unregister(struct uart *uart)
Unregister UART.
Definition uart.c:117
struct list_head uarts
int uart_register_fixed(void)
Register fixed UARTs (when not provided by platform)
Definition uart.c:91
struct uart * alloc_uart(size_t priv_len)
Allocate UART.
Definition uart.c:74
int uart_register(struct uart *uart)
Register UART.
Definition uart.c:102
static uint8_t byte
Definition uart.h:81
struct uart * uart_find(const char *name)
Find named UART.
Definition uart.c:130