Merge branch 'master' of github.com:Minep/lunaix-os
[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     /**
37      * @brief Write buffer to TX. The return code indicate
38      * the transaction is either done in synced mode (TX_DONE) or will be
39      * done asynchronously (TX_WAIT).
40      *
41      */
42     int (*write)(struct serial_dev* sdev, u8_t*, size_t);
43     int (*exec_cmd)(struct serial_dev* sdev, u32_t, va_list);
44 };
45
46 /**
47  * @brief Create a serial device.
48  *
49  *
50  * @param if_ident a string that differentiate the underlying interface of
51  * serial ports
52  * @param with_tty whether a `/dev/tty*` will be automatically created and
53  * attach to it.
54  * @return struct serial_dev*
55  */
56 struct serial_dev*
57 serial_create(struct devclass* class, char* if_ident);
58
59 void
60 serial_readone(struct serial_dev* sdev, u8_t* val);
61
62 int
63 serial_readone_nowait(struct serial_dev* sdev, u8_t* val);
64
65 size_t
66 serial_readbuf(struct serial_dev* sdev, u8_t* buf, size_t len);
67
68 int
69 serial_readbuf_nowait(struct serial_dev* sdev, u8_t* buf, size_t len);
70
71 int
72 serial_writebuf(struct serial_dev* sdev, u8_t* buf, size_t len);
73
74 int
75 serial_writebuf_nowait(struct serial_dev* sdev, u8_t* buf, size_t len);
76
77 struct serial_dev*
78 serial_get_avilable();
79
80 int
81 serial_accept_one(struct serial_dev* sdev, u8_t val);
82
83 int
84 serial_accept_buffer(struct serial_dev* sdev, void* val, size_t len);
85
86 void
87 serial_end_recv(struct serial_dev* sdev);
88
89 void
90 serial_end_xmit(struct serial_dev* sdev, size_t len);
91
92 #endif /* __LUNAIX_SERIAL_H */