Modules/UDI - (minor) tiny fiddle to udic planning
[tpg/acess2.git] / KernelLand / Modules / Interfaces / UDI / trans / gio_uart.c
1 /*
2  * Acess2 UDI Layer
3  * - By John Hodge (thePowersGang)
4  *
5  * trans/gio_uart.c
6  * - GIO UART translation (presents "uart" type GIO drivers as serial ports)
7  */
8 #define DEBUG   0
9 #include <udi.h>
10 //#include <udi_gio.h>
11 #include <acess.h>
12 #include <drv_pty.h>
13 #include <trans_uart.h>
14 #include <workqueue.h>
15
16 #define NUM_TX_CBS      2
17 #define RX_SIZE 32      // Size of a read request
18
19 typedef struct {
20         udi_init_context_t      init_context;
21         udi_cb_t        *active_cb;
22         tPTY    *PTYInstance;
23         tWorkqueue      CBWorkQueue;
24 } rdata_t;
25
26 enum {
27         ACESSUART_CB_BIND = 1,
28         ACESSUART_CB_XFER
29 };
30 enum {
31         ACESSUART_OPS_GIO = 1
32 };
33 enum {
34         ACESSUART_META_GIO = 1,
35 };
36
37 // === PROTOTYPES ===
38 void    acessuart_pty_output(void *Handle, size_t Length, const void *Data);
39
40 // === CODE ===
41 void acessuart_usage_ind(udi_usage_cb_t *cb, udi_ubit8_t resource_level)
42 {
43         udi_cb_t        *gcb = UDI_GCB(cb);
44         rdata_t *rdata = gcb->context;
45         Workqueue_Init(&rdata->CBWorkQueue, "UDI UART TX", offsetof(udi_gio_xfer_cb_t, gcb.initiator_context));
46         udi_usage_res(cb);
47 }
48 void acessuart_devmgmt_req(udi_mgmt_cb_t *cb, udi_ubit8_t mgmt_op, udi_ubit8_t parent_ID)
49 {
50         UNIMPLEMENTED();
51 }
52 void acessuart_final_cleanup_req(udi_mgmt_cb_t *cb)
53 {
54         UNIMPLEMENTED();
55 }
56 // ----
57 void acessuart_channel_event_ind(udi_channel_event_cb_t *cb);
58 void acessuart_channel_event_ind__tx_cbs_allocated(udi_cb_t *gcb, udi_cb_t *first_cb);
59 void acessuart_bind_ack(udi_gio_bind_cb_t *cb, udi_ubit32_t device_size_lo, udi_ubit32_t device_size_hi, udi_status_t status);
60 void acessuart_event_ind__buf_allocated(udi_cb_t *gcb, udi_buf_t *buffer);
61
62 void acessuart_channel_event_ind(udi_channel_event_cb_t *cb)
63 {
64         udi_cb_t        *gcb = UDI_GCB(cb);
65         rdata_t *rdata = gcb->context;
66         ASSERT(rdata);
67         switch(cb->event)
68         {
69         case UDI_CHANNEL_CLOSED:
70                 break;
71         case UDI_CHANNEL_BOUND: {
72                 rdata->active_cb = gcb;
73                 // 
74                 udi_cb_alloc_batch(acessuart_channel_event_ind__tx_cbs_allocated, cb->params.parent_bound.bind_cb,
75                         ACESSUART_CB_XFER, NUM_TX_CBS, FALSE, 0, UDI_NULL_BUF_PATH);
76                 // V V V V
77                 break; }
78         }
79 }
80 void acessuart_channel_event_ind__tx_cbs_allocated(udi_cb_t *gcb, udi_cb_t *first_cb)
81 {
82         rdata_t *rdata = gcb->context;
83         udi_gio_bind_cb_t       *cb = UDI_MCB(gcb, udi_gio_bind_cb_t);
84         ASSERT(rdata);
85
86         while( first_cb )
87         {
88                 udi_cb_t        *next = first_cb->initiator_context;
89                 first_cb->initiator_context = NULL;
90                 Workqueue_AddWork(&rdata->CBWorkQueue, first_cb);
91                 first_cb = next;
92         }
93
94         udi_gio_bind_req(cb);
95         // continued in acessuart_bind_ack
96 }
97 void acessuart_bind_ack(udi_gio_bind_cb_t *cb, udi_ubit32_t device_size_lo, udi_ubit32_t device_size_hi, udi_status_t status)
98 {
99         udi_cb_t        *gcb = UDI_GCB(cb);
100         rdata_t *rdata = gcb->context;
101         udi_channel_event_cb_t  *channel_cb = UDI_MCB(rdata->active_cb, udi_channel_event_cb_t);
102         
103         if( device_size_lo != 0 || device_size_hi != 0 ) {
104                 // Oops... binding failed. UARTS should not have a size
105                 udi_channel_event_complete( channel_cb, UDI_STAT_NOT_UNDERSTOOD);
106                 return ;
107         }
108         
109         // We're bound on the GIO channel, create a PTY for this UART
110         struct ptydims dims = {
111                 .W = 80, .H = 25,
112                 .PW = 0, .PH = 0
113         };
114         struct ptymode mode = {
115                 .OutputMode = PTYBUFFMT_TEXT,
116                 .InputMode = PTYIMODE_CANON|PTYIMODE_ECHO
117         };
118         rdata->PTYInstance = PTY_Create("serial#", rdata, acessuart_pty_output,
119                 NULL, NULL,
120                 &dims, &mode
121                 );
122         if( !rdata->PTYInstance ) {
123                 udi_channel_event_complete(channel_cb, UDI_STAT_RESOURCE_UNAVAIL);
124                 return ;
125         }
126         
127         udi_channel_event_complete(channel_cb, UDI_OK);
128 }
129 void acessuart_unbind_ack(udi_gio_bind_cb_t *cb)
130 {
131         UNIMPLEMENTED();
132 }
133 void acessuart_xfer_ack(udi_gio_xfer_cb_t *cb)
134 {
135         udi_cb_t        *gcb = UDI_GCB(cb);
136         rdata_t *rdata = gcb->context;
137         if( cb->op == UDI_GIO_OP_WRITE ) {
138                 // Write, no action required except returning the CB to the pool
139                 udi_buf_free(cb->data_buf);
140                 //cb->data_buf = NULL;
141                 Workqueue_AddWork(&rdata->CBWorkQueue, cb);
142         }
143         else if( cb->op == UDI_GIO_OP_READ ) {
144                 // Send data to PTY
145                 UNIMPLEMENTED();
146                 // TODO: Since this was a full ACK, request more?
147         }
148         else {
149                 // Well, that was unexpected
150         }
151 }
152 void acessuart_xfer_nak(udi_gio_xfer_cb_t *cb, udi_status_t status)
153 {
154         udi_cb_t        *gcb = UDI_GCB(cb);
155         rdata_t *rdata = gcb->context;
156         if( cb->op == UDI_GIO_OP_READ && status == UDI_STAT_DATA_UNDERRUN )
157         {
158                 udi_size_t      len = cb->data_buf->buf_size;
159                 if( len == 0 )
160                 {
161                         udi_debug_printf("%s: no data read, rx buffer must be empty\n", __func__, len);
162                         ASSERT(status != UDI_OK);
163                 }
164                 else
165                 {
166                         char    tmp[len];
167                         udi_buf_read(cb->data_buf, 0, len, tmp);
168                         for( int i = 0; i < len; i ++ ) {
169                                 if( tmp[i] == '\r' )
170                                         tmp[i] = '\n';
171                         }
172                         
173                         udi_debug_printf("%s: %i bytes '%.*s'\n", __func__, len, len, tmp);
174                         PTY_SendInput(rdata->PTYInstance, tmp, len);
175                 }
176                 
177                 udi_buf_free(cb->data_buf);
178                 
179                 // if status == OK, then all bytes we requested were read.
180                 // - In which case, we want to request more
181                 if( status == UDI_OK ) {
182                         UDI_BUF_ALLOC(acessuart_event_ind__buf_allocated, gcb, NULL, RX_SIZE, UDI_NULL_BUF_PATH);
183                         return ;
184                 }
185                 
186                 Workqueue_AddWork(&rdata->CBWorkQueue, cb);
187         }
188         else {
189                 UNIMPLEMENTED();
190         }
191 }
192 void acessuart_event_ind(udi_gio_event_cb_t *cb)
193 {
194         udi_cb_t        *gcb = UDI_GCB(cb);
195         rdata_t *rdata = gcb->context;
196
197         // ACK event before requesting read
198         udi_gio_event_res(cb);
199         
200         // Begin read request
201         udi_gio_xfer_cb_t       *read_cb = Workqueue_GetWork(&rdata->CBWorkQueue);
202         UDI_BUF_ALLOC(acessuart_event_ind__buf_allocated, UDI_GCB(read_cb), NULL, RX_SIZE, UDI_NULL_BUF_PATH);
203 }
204 void acessuart_event_ind__buf_allocated(udi_cb_t *gcb, udi_buf_t *buffer)
205 {
206         udi_gio_xfer_cb_t       *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
207
208         cb->op = UDI_GIO_OP_READ;
209         cb->tr_params = NULL;
210         cb->data_buf = buffer;
211         
212         udi_gio_xfer_req(cb);
213         // Continued in acessuart_xfer_ack/nak
214 }
215
216
217 void acessuart_pty_output(void *Handle, size_t Length, const void *Data);
218 void acessuart_pty_output__buf_allocated(udi_cb_t *gcb, udi_buf_t *buffer);
219
220 void acessuart_pty_output(void *Handle, size_t Length, const void *Data)
221 {
222         LOG("Output '%.*s'", Length, Data);
223         
224         rdata_t *rdata = Handle;
225         udi_gio_xfer_cb_t       *cb = Workqueue_GetWork(&rdata->CBWorkQueue);
226         udi_cb_t        *gcb = UDI_GCB(cb);
227         
228         UDI_BUF_ALLOC(acessuart_pty_output__buf_allocated, gcb, Data, Length, UDI_NULL_BUF_PATH);
229         // don't bother waiting for tx to complete, workqueue will block when everything is in use
230         // - And once buf_alloc returns, the data is copied
231 }
232 void acessuart_pty_output__buf_allocated(udi_cb_t *gcb, udi_buf_t *buffer)
233 {
234         //rdata_t       *rdata = gcb->context;
235         LOG("buffer = %p\n", buffer);
236         udi_gio_xfer_cb_t       *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
237
238         cb->op = UDI_GIO_OP_WRITE;
239         cb->tr_params = NULL;
240         cb->data_buf = buffer;  
241
242         udi_gio_xfer_req(cb);
243 }
244
245
246 // --------------------------------------------------------------------
247 udi_mgmt_ops_t  acessuart_mgmt_ops = {
248         acessuart_usage_ind,
249         udi_enumerate_no_children,
250         acessuart_devmgmt_req,
251         acessuart_final_cleanup_req
252 };
253 udi_ubit8_t     acessuart_mgmt_ops_flags[4] = {0,0,0,0};
254
255 udi_primary_init_t      acessuart_pri_init = {
256         .mgmt_ops = &acessuart_mgmt_ops,
257         .mgmt_op_flags = acessuart_mgmt_ops_flags,
258         .mgmt_scratch_requirement = 0,
259         .enumeration_attr_list_length = 0,
260         .rdata_size = sizeof(rdata_t),
261         .child_data_size = 0,
262         .per_parent_paths = 0
263 };
264
265 udi_gio_client_ops_t    acessuart_gio_ops = {
266         acessuart_channel_event_ind,
267         acessuart_bind_ack,
268         acessuart_unbind_ack,
269         acessuart_xfer_ack,
270         acessuart_xfer_nak,
271         acessuart_event_ind
272 };
273 udi_ubit8_t     acessuart_gio_op_flags[7] = {0};
274
275 udi_ops_init_t  acessuart_ops_list[] = {
276         {
277                 ACESSUART_OPS_GIO, ACESSUART_META_GIO, UDI_GIO_CLIENT_OPS_NUM,
278                 0, (udi_ops_vector_t*)&acessuart_gio_ops, acessuart_gio_op_flags
279         },
280         {0}
281 };
282 udi_cb_init_t   acessuart_cb_init_list[] = {
283         {ACESSUART_CB_BIND, ACESSUART_META_GIO, UDI_GIO_BIND_CB_NUM, 0, 0,NULL},
284         {ACESSUART_CB_XFER, ACESSUART_META_GIO, UDI_GIO_XFER_CB_NUM, 0, 0,NULL},
285         {0}
286 };
287 const udi_init_t        acessuart_init = {
288         .primary_init_info = &acessuart_pri_init,
289         .ops_init_list = acessuart_ops_list,
290         .cb_init_list = acessuart_cb_init_list,
291 };
292 const char      acessuart_udiprops[] = 
293         "properties_version 0x101\0"
294         "message 1 Acess2 Kernel\0"
295         "message 2 John Hodge ([email protected])\0"
296         "message 3 Acess2 UART\0"
297         "supplier 1\0"
298         "contact 2\0"
299         "name 3\0"
300         "module acess_uart\0"
301         "shortname acessuart\0"
302         "requires udi 0x101\0"
303         "requires udi_gio 0x101\0"
304         "meta 1 udi_gio\0"
305         "message 101 UART\0"
306         "device 101 1 gio_type string uart\0"
307         "parent_bind_ops 1 0 1 1\0"
308         "\0";
309 size_t  acessuart_udiprops_size = sizeof(acessuart_udiprops);

UCC git Repository :: git.ucc.asn.au