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>
19 udi_init_context_t init_context;
22 tWorkqueue CBWorkQueue;
26 ACESSUART_CB_BIND = 1,
33 ACESSUART_META_GIO = 1,
37 void acessuart_pty_output(void *Handle, size_t Length, const void *Data);
40 void acessuart_usage_ind(udi_usage_cb_t *cb, udi_ubit8_t resource_level)
42 udi_cb_t *gcb = UDI_GCB(cb);
43 rdata_t *rdata = gcb->context;
44 Workqueue_Init(&rdata->CBWorkQueue, "UDI UART TX", offsetof(udi_gio_xfer_cb_t, gcb.initiator_context));
47 void acessuart_devmgmt_req(udi_mgmt_cb_t *cb, udi_ubit8_t mgmt_op, udi_ubit8_t parent_ID)
51 void acessuart_final_cleanup_req(udi_mgmt_cb_t *cb)
56 void acessuart_channel_event_ind(udi_channel_event_cb_t *cb);
57 void acessuart_channel_event_ind__tx_cbs_allocated(udi_cb_t *gcb, udi_cb_t *first_cb);
58 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_channel_event_ind(udi_channel_event_cb_t *cb)
62 udi_cb_t *gcb = UDI_GCB(cb);
63 rdata_t *rdata = gcb->context;
67 case UDI_CHANNEL_CLOSED:
69 case UDI_CHANNEL_BOUND: {
70 rdata->active_cb = gcb;
72 udi_cb_alloc_batch(acessuart_channel_event_ind__tx_cbs_allocated, cb->params.parent_bound.bind_cb,
73 ACESSUART_CB_XFER, NUM_TX_CBS, FALSE, 0, UDI_NULL_BUF_PATH);
78 void acessuart_channel_event_ind__tx_cbs_allocated(udi_cb_t *gcb, udi_cb_t *first_cb)
80 rdata_t *rdata = gcb->context;
81 udi_gio_bind_cb_t *cb = UDI_MCB(gcb, udi_gio_bind_cb_t);
86 udi_cb_t *next = first_cb->initiator_context;
87 first_cb->initiator_context = NULL;
88 Workqueue_AddWork(&rdata->CBWorkQueue, first_cb);
93 // continued in acessuart_bind_ack
95 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)
97 udi_cb_t *gcb = UDI_GCB(cb);
98 rdata_t *rdata = gcb->context;
99 udi_channel_event_cb_t *channel_cb = UDI_MCB(rdata->active_cb, udi_channel_event_cb_t);
101 if( device_size_lo != 0 || device_size_hi != 0 ) {
102 // Oops... binding failed. UARTS should not have a size
103 udi_channel_event_complete( channel_cb, UDI_STAT_NOT_UNDERSTOOD);
107 // bound, create PTY instance
108 rdata->PTYInstance = PTY_Create("serial#", rdata, acessuart_pty_output, NULL, NULL);
109 if( !rdata->PTYInstance ) {
110 udi_channel_event_complete(channel_cb, UDI_STAT_RESOURCE_UNAVAIL);
114 udi_channel_event_complete(channel_cb, UDI_OK);
116 void acessuart_unbind_ack(udi_gio_bind_cb_t *cb)
120 void acessuart_xfer_ack(udi_gio_xfer_cb_t *cb)
122 udi_cb_t *gcb = UDI_GCB(cb);
123 rdata_t *rdata = gcb->context;
124 if( cb->op == UDI_GIO_OP_WRITE ) {
125 // Write, no action required except returning the CB to the pool
126 udi_buf_free(cb->data_buf);
127 Workqueue_AddWork(&rdata->CBWorkQueue, cb);
130 else if( cb->op == UDI_GIO_OP_READ ) {
135 // Well, that was unexpected
138 void acessuart_xfer_nak(udi_gio_xfer_cb_t *cb, udi_status_t status)
142 void acessuart_event_ind(udi_gio_event_cb_t *cb)
144 // grab the input CB, and request data
149 void acessuart_pty_output(void *Handle, size_t Length, const void *Data);
150 void acessuart_pty_output__buf_allocated(udi_cb_t *gcb, udi_buf_t *buffer);
152 void acessuart_pty_output(void *Handle, size_t Length, const void *Data)
154 LOG("Output '%.*s'", Length, Data);
156 rdata_t *rdata = Handle;
157 udi_gio_xfer_cb_t *cb = Workqueue_GetWork(&rdata->CBWorkQueue);
158 udi_cb_t *gcb = UDI_GCB(cb);
160 UDI_BUF_ALLOC(acessuart_pty_output__buf_allocated, gcb, Data, Length, UDI_NULL_BUF_PATH);
161 // don't bother waiting for tx to complete, workqueue will block when everything is in use
163 void acessuart_pty_output__buf_allocated(udi_cb_t *gcb, udi_buf_t *buffer)
165 //rdata_t *rdata = gcb->context;
166 LOG("buffer = %p\n", buffer);
167 udi_gio_xfer_cb_t *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
169 cb->op = UDI_GIO_OP_WRITE;
170 cb->tr_params = NULL;
171 cb->data_buf = buffer;
173 udi_gio_xfer_req(cb);
177 // --------------------------------------------------------------------
178 udi_mgmt_ops_t acessuart_mgmt_ops = {
180 udi_enumerate_no_children,
181 acessuart_devmgmt_req,
182 acessuart_final_cleanup_req
184 udi_ubit8_t acessuart_mgmt_ops_flags[4] = {0,0,0,0};
186 udi_primary_init_t acessuart_pri_init = {
187 .mgmt_ops = &acessuart_mgmt_ops,
188 .mgmt_op_flags = acessuart_mgmt_ops_flags,
189 .mgmt_scratch_requirement = 0,
190 .enumeration_attr_list_length = 0,
191 .rdata_size = sizeof(rdata_t),
192 .child_data_size = 0,
193 .per_parent_paths = 0
196 udi_gio_client_ops_t acessuart_gio_ops = {
197 acessuart_channel_event_ind,
199 acessuart_unbind_ack,
204 udi_ubit8_t acessuart_gio_op_flags[7] = {0};
206 udi_ops_init_t acessuart_ops_list[] = {
208 ACESSUART_OPS_GIO, ACESSUART_META_GIO, UDI_GIO_CLIENT_OPS_NUM,
209 0, (udi_ops_vector_t*)&acessuart_gio_ops, acessuart_gio_op_flags
213 udi_cb_init_t acessuart_cb_init_list[] = {
214 {ACESSUART_CB_BIND, ACESSUART_META_GIO, UDI_GIO_BIND_CB_NUM, 0, 0,NULL},
215 {ACESSUART_CB_XFER, ACESSUART_META_GIO, UDI_GIO_XFER_CB_NUM, 0, 0,NULL},
218 const udi_init_t acessuart_init = {
219 .primary_init_info = &acessuart_pri_init,
220 .ops_init_list = acessuart_ops_list,
221 .cb_init_list = acessuart_cb_init_list,
223 const char acessuart_udiprops[] =
224 "properties_version 0x101\0"
225 "message 1 Acess2 Kernel\0"
227 "message 3 Acess2 UART\0"
231 "module acess_uart\0"
232 "shortname acessuart\0"
233 "requires udi 0x101\0"
234 "requires udi_gio 0x101\0"
237 "device 101 1 gio_type string uart\0"
238 "parent_bind_ops 1 0 1 1\0"
240 size_t acessuart_udiprops_size = sizeof(acessuart_udiprops);