3 * - By John Hodge (thePowersGang)
6 * - GIO UART translation (presents "uart" type GIO drivers as serial ports)
10 //#include <udi_gio.h>
13 #include <trans_uart.h>
14 #include <workqueue.h>
17 #define RX_SIZE 32 // Size of a read request
20 udi_init_context_t init_context;
23 tWorkqueue CBWorkQueue;
27 ACESSUART_CB_BIND = 1,
34 ACESSUART_META_GIO = 1,
38 void acessuart_pty_output(void *Handle, size_t Length, const void *Data);
41 void acessuart_usage_ind(udi_usage_cb_t *cb, udi_ubit8_t resource_level)
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));
48 void acessuart_devmgmt_req(udi_mgmt_cb_t *cb, udi_ubit8_t mgmt_op, udi_ubit8_t parent_ID)
52 void acessuart_final_cleanup_req(udi_mgmt_cb_t *cb)
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);
62 void acessuart_channel_event_ind(udi_channel_event_cb_t *cb)
64 udi_cb_t *gcb = UDI_GCB(cb);
65 rdata_t *rdata = gcb->context;
69 case UDI_CHANNEL_CLOSED:
71 case UDI_CHANNEL_BOUND: {
72 rdata->active_cb = gcb;
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);
80 void acessuart_channel_event_ind__tx_cbs_allocated(udi_cb_t *gcb, udi_cb_t *first_cb)
82 rdata_t *rdata = gcb->context;
83 udi_gio_bind_cb_t *cb = UDI_MCB(gcb, udi_gio_bind_cb_t);
88 udi_cb_t *next = first_cb->initiator_context;
89 first_cb->initiator_context = NULL;
90 Workqueue_AddWork(&rdata->CBWorkQueue, first_cb);
95 // continued in acessuart_bind_ack
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)
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);
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);
109 // We're bound on the GIO channel, create a PTY for this UART
110 struct ptydims dims = {
114 struct ptymode mode = {
115 .OutputMode = PTYBUFFMT_TEXT,
116 .InputMode = PTYIMODE_CANON|PTYIMODE_ECHO
118 rdata->PTYInstance = PTY_Create("serial#", rdata, acessuart_pty_output,
122 if( !rdata->PTYInstance ) {
123 udi_channel_event_complete(channel_cb, UDI_STAT_RESOURCE_UNAVAIL);
127 udi_channel_event_complete(channel_cb, UDI_OK);
129 void acessuart_unbind_ack(udi_gio_bind_cb_t *cb)
133 void acessuart_xfer_ack(udi_gio_xfer_cb_t *cb)
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);
143 else if( cb->op == UDI_GIO_OP_READ ) {
146 // TODO: Since this was a full ACK, request more?
149 // Well, that was unexpected
152 void acessuart_xfer_nak(udi_gio_xfer_cb_t *cb, udi_status_t status)
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 )
158 udi_size_t len = cb->data_buf->buf_size;
161 udi_debug_printf("%s: no data read, rx buffer must be empty\n", __func__, len);
162 ASSERT(status != UDI_OK);
167 udi_buf_read(cb->data_buf, 0, len, tmp);
168 for( int i = 0; i < len; i ++ ) {
173 udi_debug_printf("%s: %i bytes '%.*s'\n", __func__, len, len, tmp);
174 PTY_SendInput(rdata->PTYInstance, tmp, len);
177 udi_buf_free(cb->data_buf);
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);
186 Workqueue_AddWork(&rdata->CBWorkQueue, cb);
192 void acessuart_event_ind(udi_gio_event_cb_t *cb)
194 udi_cb_t *gcb = UDI_GCB(cb);
195 rdata_t *rdata = gcb->context;
197 // ACK event before requesting read
198 udi_gio_event_res(cb);
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);
204 void acessuart_event_ind__buf_allocated(udi_cb_t *gcb, udi_buf_t *buffer)
206 udi_gio_xfer_cb_t *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
208 cb->op = UDI_GIO_OP_READ;
209 cb->tr_params = NULL;
210 cb->data_buf = buffer;
212 udi_gio_xfer_req(cb);
213 // Continued in acessuart_xfer_ack/nak
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);
220 void acessuart_pty_output(void *Handle, size_t Length, const void *Data)
222 LOG("Output '%.*s'", Length, Data);
224 rdata_t *rdata = Handle;
225 udi_gio_xfer_cb_t *cb = Workqueue_GetWork(&rdata->CBWorkQueue);
226 udi_cb_t *gcb = UDI_GCB(cb);
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
232 void acessuart_pty_output__buf_allocated(udi_cb_t *gcb, udi_buf_t *buffer)
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);
238 cb->op = UDI_GIO_OP_WRITE;
239 cb->tr_params = NULL;
240 cb->data_buf = buffer;
242 udi_gio_xfer_req(cb);
246 // --------------------------------------------------------------------
247 udi_mgmt_ops_t acessuart_mgmt_ops = {
249 udi_enumerate_no_children,
250 acessuart_devmgmt_req,
251 acessuart_final_cleanup_req
253 udi_ubit8_t acessuart_mgmt_ops_flags[4] = {0,0,0,0};
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
265 udi_gio_client_ops_t acessuart_gio_ops = {
266 acessuart_channel_event_ind,
268 acessuart_unbind_ack,
273 udi_ubit8_t acessuart_gio_op_flags[7] = {0};
275 udi_ops_init_t acessuart_ops_list[] = {
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
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},
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,
292 const char acessuart_udiprops[] =
293 "properties_version 0x101\0"
294 "message 1 Acess2 Kernel\0"
296 "message 3 Acess2 UART\0"
300 "module acess_uart\0"
301 "shortname acessuart\0"
302 "requires udi 0x101\0"
303 "requires udi_gio 0x101\0"
306 "device 101 1 gio_type string uart\0"
307 "parent_bind_ops 1 0 1 1\0"
309 size_t acessuart_udiprops_size = sizeof(acessuart_udiprops);