cherry pick device tree modifications from isa/arm64
[lunaix-os.git] / lunaix-os / includes / hal / serial.h
1 #ifndef __LUNAIX_SERIAL_H
2 #define __LUNAIX_SERIAL_H
3
4 #include <lunaix/device.h>
5 #include <lunaix/ds/llist.h>
6 #include <lunaix/ds/mutex.h>
7 #include <lunaix/ds/waitq.h>
8 #include <lunaix/ds/rbuffer.h>
9
10 #include <usr/lunaix/serial.h>
11
12 #define SERIAL_RW_RX 0x0
13 #define SERIAL_RW_TX 0x1
14 #define io_dir(flags) ((flags) & SERIAL_RW_TX)
15
16 #define RXTX_DONE 0x1
17 #define RXTX_WAIT 0x2
18
19 #define SERIAL_AGAIN 0x1
20 #define SERIAL_DONE 0x0
21
22 struct serial_dev;
23 typedef int (*rxtx_cb)(struct serial_dev*);
24
25 struct serial_dev
26 {
27     struct llist_header sdev_list;
28     struct device* dev;
29     struct waitq wq_rxdone;
30     struct waitq wq_txdone;
31     void* backend;
32
33     struct rbuffer rxbuf;
34     int wr_len;
35
36     struct capability_meta* tp_cap;
37
38     /**
39      * @brief Write buffer to TX. The return code indicate
40      * the transaction is either done in synced mode (TX_DONE) or will be
41      * done asynchronously (TX_WAIT).
42      *
43      */
44     int (*write)(struct serial_dev* sdev, u8_t*, size_t);
45     int (*exec_cmd)(struct serial_dev* sdev, u32_t, va_list);
46 };
47
48 /**
49  * @brief Create a serial device.
50  *
51  *
52  * @param if_ident a string that differentiate the underlying interface of
53  * serial ports
54  * @param with_tty whether a `/dev/tty*` will be automatically created and
55  * attach to it.
56  * @return struct serial_dev*
57  */
58 struct serial_dev*
59 serial_create(struct devclass* class, char* if_ident);
60
61 void
62 serial_readone(struct serial_dev* sdev, u8_t* val);
63
64 int
65 serial_readone_nowait(struct serial_dev* sdev, u8_t* val);
66
67 size_t
68 serial_readbuf(struct serial_dev* sdev, u8_t* buf, size_t len);
69
70 int
71 serial_readbuf_nowait(struct serial_dev* sdev, u8_t* buf, size_t len);
72
73 int
74 serial_writebuf(struct serial_dev* sdev, u8_t* buf, size_t len);
75
76 int
77 serial_writebuf_nowait(struct serial_dev* sdev, u8_t* buf, size_t len);
78
79 struct serial_dev*
80 serial_get_avilable();
81
82 int
83 serial_accept_one(struct serial_dev* sdev, u8_t val);
84
85 int
86 serial_accept_buffer(struct serial_dev* sdev, void* val, size_t len);
87
88 void
89 serial_end_recv(struct serial_dev* sdev);
90
91 void
92 serial_end_xmit(struct serial_dev* sdev, size_t len);
93
94 #endif /* __LUNAIX_SERIAL_H */