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