1 /*
2  * Copyright (C) 2015-2021 Alibaba Group Holding Limited
3  */
4 
5 #ifndef _U_CDEV_MSG_H_
6 #define _U_CDEV_MSG_H_
7 
8 #define U_CDEV_RPC_BUFFER_SIZE (4 * 1024 + 64)
9 
10 typedef enum fops_msg_id {
11     FOPS_MIN = 0,
12 
13     /* Application -> C */
14     FOPS_OPEN,    /* open operation */
15     FOPS_READ,    /* read operation */
16     FOPS_WRITE,   /* write operation */
17     FOPS_IOCTL,   /* ioctl operation */
18     FOPS_POLL,    /* poll operation */
19     FOPS_LSEEK,    /* lseek operation */
20     FOPS_CLOSE,   /* close operation */
21 
22     FOPS_MAX
23 } fops_msg_id_e;
24 
25 typedef enum u_cdev_msg_id {
26     /* VFS<->char device */
27     CDEV_MSG_FOPS,     /* file operation message id */
28 
29     /* Others */
30     CDEV_MSG_LOOP,
31     CDEV_MSG_MAX
32 } u_cdev_msg_id_e;
33 
34 #pragma pack(1)
35 /**
36  * file operation arguments
37  * open.flags: equeals to flags when calling open("path", flags)
38  * open.path: file's full pathname
39  * read.farg: equals to the open.arg in u_fops_result returned after open operation
40  * read.len: target length of the read operation
41  * write.farg: equals to the open.arg in u_fops_result returned after open operation
42  * write.len: target length of the write operation
43  * write.buf: the data to be write
44  * ioctl.farg: equals to the open.arg in u_fops_result returned after open operation
45  * ioctl.cmd: cmd equals to ioctl(fd, cmd, arg)
46  * ioctl.arg: arg equals to ioctl(fd, cmd, arg)
47  * poll.farg: equals to the open.arg in u_fops_result returned after open operation
48  * poll.flag: read/write flags
49  */
50 typedef union u_fops_arg {
51     struct {
52         int flags;
53         char path[0];
54     } open;
55 
56     struct {
57         void *farg;
58         int len;
59     } read;
60 
61     struct {
62         void *farg;
63         int len;
64         char buf[0];
65     } write;
66 
67     struct {
68         void *farg;
69         int cmd;
70         unsigned long arg;
71     } ioctl;
72 
73     struct {
74         void *farg;
75         int flag;
76     } poll;
77 
78     struct {
79         void *farg;
80         off_t off;
81         int whence;
82     } lseek;
83 
84     struct {
85         void *farg;
86     } close;
87 
88     struct {
89         void *farg;
90     } priv;
91 } u_fops_arg_u;
92 
93 /**
94  * file ops result
95  * status: 0 means operation success
96  * open.farg: special pointer used in read/write/ioctl/poll operations
97  * read.len: the actual read bytes, while data is stored into read.data
98  * read.data: pointed to the buffer where the read data is stored
99  * write.len: actual length written to the file succefully
100  * poll.events: value of fd.events
101  *
102  */
103 typedef union u_fops_result {
104 
105     struct {
106         int status;
107         void *farg;
108     } open;
109 
110     struct {
111         int status;
112         ssize_t len;
113         char data[0];
114     } read;
115 
116     struct {
117         int status;
118         ssize_t len;
119     } write;
120 
121     struct {
122         int status;
123         short events;
124     } poll;
125 
126     struct {
127         int status;
128         off_t off;
129     } lseek;
130 
131     struct {
132         int status;
133     } ioctl;
134 
135     struct {
136         int status;
137     } close;
138 
139     struct {
140         int status;
141     } priv;
142 } u_fops_result_u;
143 
144 typedef struct u_cdev_msg_ie {
145     unsigned short t;
146     unsigned short l;
147     u_fops_arg_u v[0];
148 } u_cdev_msg_ie_t;
149 
150 typedef u_cdev_msg_ie_t u_fops_ie_t;
151 
152 /**
153  * struct dev_drv_msg - driver related IPC message format
154  * @msg_type: message
155  * */
156 typedef struct u_cdev_msg {
157     u_cdev_msg_id_e t;
158     unsigned short l;
159     u_cdev_msg_ie_t v[0];
160 } u_cdev_msg_t;
161 
162 #pragma pack()
163 
164 typedef int (*u_cdev_msg_handler)(int crpc_handle, u_cdev_msg_t *p_msg);
165 
166 #define IE_FOR_EACH(p_msg) \
167 	for (ie = (u_cdev_msg_ie_t *)p_msg->v; \
168 		ie < (p_msg->v + sizeof(p_msg->l) + p_msg->l); \
169 		ie = (u_cdev_msg_ie_t *)(ie->v + ie->l))
170 
171 #endif //_U_CDEV_MSG_H_
172