2 * UDI Ne2000 NIC Driver
3 * By John Hodge (thePowersGang)
8 #define UDI_VERSION 0x101
10 #include <udi_physio.h>
11 #include "uart16c550_common.h"
13 #include "uart16c550_pio.h"
16 # define DEBUG_OUT(fmt, v...) udi_debug_printf("%s: "fmt"\n", __func__ ,## v)
18 # define DEBUG_OUT(...) do{}while(0)
21 #define __EXPJOIN(a,b) a##b
22 #define _EXPJOIN(a,b) __EXPJOIN(a,b)
23 #define _EXPLODE(params...) params
24 #define _ADDGCB(params...) (udi_cb_t *gcb, params)
25 #define CONTIN(suffix, call, args, params) extern void _EXPJOIN(suffix##__,__LINE__) _ADDGCB params;\
26 call( _EXPJOIN(suffix##__,__LINE__), gcb, _EXPLODE args); } \
27 void _EXPJOIN(suffix##__,__LINE__) _ADDGCB params { \
28 rdata_t *rdata = gcb->context;
30 // --- Management Metalang
31 void uart_usage_ind(udi_usage_cb_t *cb, udi_ubit8_t resource_level)
33 rdata_t *rdata = UDI_GCB(cb)->context;
34 //udi_trace_write(rdata->init_context, UDI_TREVENT_LOCAL_PROC_ENTRY, 0, );
36 // TODO: Set up region data
41 void uart_enumerate_req(udi_enumerate_cb_t *cb, udi_ubit8_t enumeration_level)
43 rdata_t *rdata = UDI_GCB(cb)->context;
44 udi_instance_attr_list_t *attr_list = cb->attr_list;
46 switch(enumeration_level)
48 case UDI_ENUMERATE_START:
49 case UDI_ENUMERATE_START_RESCAN:
50 DPT_SET_ATTR_STRING(attr_list, "gio_type", "uart", 4);
52 cb->attr_valid_length = attr_list - cb->attr_list;
53 udi_enumerate_ack(cb, UDI_ENUMERATE_OK, UART_OPS_GIO);
55 case UDI_ENUMERATE_NEXT:
56 udi_enumerate_ack(cb, UDI_ENUMERATE_DONE, 0);
60 void uart_devmgmt_req(udi_mgmt_cb_t *cb, udi_ubit8_t mgmt_op, udi_ubit8_t parent_ID)
63 void uart_final_cleanup_req(udi_mgmt_cb_t *cb)
67 void uart_bus_dev_channel_event_ind(udi_channel_event_cb_t *cb);
68 void uart_bus_dev_bus_bind_ack(udi_bus_bind_cb_t *cb, udi_dma_constraints_t dma_constraints, udi_ubit8_t perferred_endianness, udi_status_t status);
69 void uart_bus_dev_bind__pio_map(udi_cb_t *cb, udi_pio_handle_t new_pio_handle);
70 void uart_bus_dev_bind__intr_chanel(udi_cb_t *gcb, udi_channel_t new_channel);
71 void uart_bus_dev_bus_unbind_ack(udi_bus_bind_cb_t *cb);
73 void uart_bus_dev_channel_event_ind(udi_channel_event_cb_t *cb)
75 udi_cb_t *gcb = UDI_GCB(cb);
76 rdata_t *rdata = gcb->context;
80 case UDI_CHANNEL_CLOSED:
82 case UDI_CHANNEL_BOUND: {
83 rdata->active_cb = gcb;
84 udi_bus_bind_cb_t *bus_bind_cb = UDI_MCB(cb->params.parent_bound.bind_cb, udi_bus_bind_cb_t);
85 udi_bus_bind_req( bus_bind_cb );
86 // continue at uart_bus_dev_bus_bind_ack
90 void uart_bus_dev_bus_bind_ack(udi_bus_bind_cb_t *cb,
91 udi_dma_constraints_t dma_constraints, udi_ubit8_t perferred_endianness, udi_status_t status)
93 udi_cb_t *gcb = UDI_GCB(cb);
94 rdata_t *rdata = gcb->context;
97 rdata->init.pio_index = -1;
98 uart_bus_dev_bind__pio_map(gcb, UDI_NULL_PIO_HANDLE);
101 void uart_bus_dev_bind__pio_map(udi_cb_t *gcb, udi_pio_handle_t new_pio_handle)
103 rdata_t *rdata = gcb->context;
104 if( rdata->init.pio_index != -1 )
106 rdata->pio_handles[rdata->init.pio_index] = new_pio_handle;
109 rdata->init.pio_index ++;
110 if( rdata->init.pio_index < N_PIO )
112 udi_pio_map(uart_bus_dev_bind__pio_map, gcb,
113 UDI_PCI_BAR_0, 0, 0x8, // TODO: Use system bus index and offset
114 uart_pio_ops[rdata->init.pio_index].trans_list,
115 uart_pio_ops[rdata->init.pio_index].list_length,
116 UDI_PIO_LITTLE_ENDIAN, 0, 0
121 // Spawn the interrupt channel
122 CONTIN( uart_bus_dev_bind, udi_channel_spawn, (gcb->channel, 0, UART_OPS_IRQ, rdata),
123 (udi_channel_t new_channel))
125 rdata->interrupt_channel = new_channel;
127 // Allocate an RX buffer
128 CONTIN( uart_bus_dev_bind, udi_buf_write, (NULL, 0, NULL, 0, 0, UDI_NULL_BUF_PATH),
129 (udi_buf_t *new_buf));
133 udi_channel_event_complete( UDI_MCB(rdata->active_cb, udi_channel_event_cb_t),
134 UDI_STAT_RESOURCE_UNAVAIL );
137 rdata->rx_buffer = new_buf;
139 // Create interrupt CB
140 CONTIN( uart_bus_dev_bind, udi_cb_alloc, (UART_CB_INTR, gcb->channel),
145 udi_channel_event_complete( UDI_MCB(rdata->active_cb, udi_channel_event_cb_t),
146 UDI_STAT_RESOURCE_UNAVAIL );
149 udi_intr_attach_cb_t *intr_cb = UDI_MCB(new_cb, udi_intr_attach_cb_t);
150 intr_cb->interrupt_idx = 0;
151 intr_cb->min_event_pend = 1;
152 intr_cb->preprocessing_handle = rdata->pio_handles[PIO_INTR];
155 udi_intr_attach_req(intr_cb);
156 // continued in uart_bus_dev_intr_attach_ack
158 void uart_bus_dev_bus_unbind_ack(udi_bus_bind_cb_t *cb)
161 void uart_bus_dev_intr_attach_ack(udi_intr_attach_cb_t *intr_attach_cb, udi_status_t status)
163 udi_cb_t *gcb = UDI_GCB(intr_attach_cb);
164 rdata_t *rdata = gcb->context;
166 // Allocate interrupt cbs
167 CONTIN(uart_bus_dev_intr_attach_ack, udi_cb_alloc_batch,
168 (UART_CB_INTR_EVENT, NUM_INTR_CBS, TRUE, INTR_CB_BUF_SIZE, UDI_NULL_BUF_PATH),
169 (udi_cb_t *first_new_cb))
171 while( first_new_cb )
173 udi_intr_event_cb_t *cb = UDI_MCB(first_new_cb, udi_intr_event_cb_t);
174 first_new_cb = first_new_cb->initiator_context;
175 // - Set channel (udi_cb_alloc_batch sets the channel to gcb->channel)
176 cb->gcb.channel = rdata->interrupt_channel;
177 udi_intr_event_rdy(cb);
180 udi_channel_event_cb_t *channel_cb = UDI_MCB(rdata->active_cb, udi_channel_event_cb_t);
182 udi_channel_event_complete(channel_cb, UDI_OK);
185 void uart_bus_dev_intr_detach_ack(udi_intr_detach_cb_t *intr_detach_cb)
189 void uart_bus_irq_channel_event_ind(udi_channel_event_cb_t *cb)
192 void uart_bus_irq_intr_event_ind(udi_intr_event_cb_t *cb, udi_ubit8_t flags)
194 udi_cb_t *gcb = UDI_GCB(cb);
195 rdata_t *rdata = gcb->context;
197 DEBUG_OUT("flags=%x, intr_result=%x", flags, cb->intr_result);
198 if( cb->intr_result == 0 )
200 // An IRQ from something other than RX
201 udi_intr_event_rdy(cb);
205 if( rdata->rx_buffer && rdata->rx_buffer->buf_size + cb->intr_result > MAX_RX_BUFFER_SIZE )
207 // Drop, buffer is full
208 DEBUG_OUT("dropping %i bytes, full rx buffer", cb->intr_result);
209 udi_intr_event_rdy(cb);
213 // Copy cb->intr_result bytes into the RX buffer
214 CONTIN(uart_bus_irq_intr_event_ind, udi_buf_copy,
215 (cb->event_buf, 0, cb->intr_result,
216 rdata->rx_buffer, rdata->rx_buffer->buf_size, 0,
218 (udi_buf_t *new_buf));
220 udi_intr_event_cb_t *cb = UDI_MCB(gcb, udi_intr_event_cb_t);
222 rdata->rx_buffer = new_buf;
224 DEBUG_OUT("copied %i bytes", cb->intr_result);
226 // Emit a signal to GIO client
227 if( rdata->event_cb && !rdata->event_cb_used )
229 rdata->event_pending = FALSE;
230 rdata->event_cb_used = TRUE;
231 udi_gio_event_ind(rdata->event_cb);
235 DEBUG_OUT("event_cb=%p, event_cb_used=%i - Queueing",
236 rdata->event_cb, rdata->event_cb_used);
237 rdata->event_pending = TRUE;
240 udi_intr_event_rdy(cb);
243 void uart_gio_prov_channel_event_ind(udi_channel_event_cb_t *cb);
244 void uart_gio_prov_bind_req(udi_gio_bind_cb_t *cb);
245 void uart_gio_prov_xfer_req(udi_gio_xfer_cb_t *cb);
246 void uart_gio_read_complete_short(udi_cb_t *gcb, udi_buf_t *new_buf);
247 void uart_gio_read_complete(udi_cb_t *gcb, udi_buf_t *new_buf);
248 void uart_gio_write_complete(udi_cb_t *gcb, udi_buf_t *new_buf, udi_status_t status, udi_ubit16_t res);
249 void uart_gio_prov_event_res(udi_gio_event_cb_t *cb);
251 void uart_gio_prov_channel_event_ind(udi_channel_event_cb_t *cb)
254 void uart_gio_prov_bind_req(udi_gio_bind_cb_t *cb)
256 udi_cb_t *gcb = UDI_GCB(cb);
257 rdata_t *rdata = gcb->context;
259 // TODO: Should this device allow multiple children?
260 if( rdata->event_cb ) {
261 DEBUG_OUT("only one client allowed");
262 udi_gio_bind_ack(cb, 0, 0, UDI_STAT_CANNOT_BIND_EXCL);
266 // Allocate the event CB
267 CONTIN( uart_gio_prov_bind_req, udi_cb_alloc, (UART_CB_GIO_EVENT, gcb->channel),
269 udi_gio_bind_cb_t *cb = UDI_MCB(gcb, udi_gio_bind_cb_t);
271 rdata->event_cb = UDI_MCB(new_cb, udi_gio_event_cb_t);
272 rdata->event_cb_used = FALSE;
274 udi_gio_bind_ack(cb, 0, 0, UDI_OK);
276 void uart_gio_prov_unbind_req(udi_gio_bind_cb_t *cb)
278 udi_cb_t *gcb = UDI_GCB(cb);
279 rdata_t *rdata = gcb->context;
281 rdata->event_cb = NULL;
282 rdata->event_cb_used = FALSE;
283 udi_gio_unbind_ack(cb);
285 void uart_gio_prov_xfer_req(udi_gio_xfer_cb_t *cb)
287 udi_cb_t *gcb = UDI_GCB(cb);
288 rdata_t *rdata = gcb->context;
291 case UDI_GIO_OP_READ: {
292 udi_buf_copy_call_t *callback;
294 // Read from local FIFO
295 if( rdata->rx_buffer->buf_size < cb->data_buf->buf_size ) {
296 len = rdata->rx_buffer->buf_size;
297 callback = uart_gio_read_complete_short;
300 len = cb->data_buf->buf_size;
301 callback = uart_gio_read_complete;
303 DEBUG_OUT("Read %i/%i bytes", len, rdata->rx_buffer->buf_size);
304 // Replace entire destination buffer with `len` bytes from source
305 udi_buf_copy(callback, gcb,
306 rdata->rx_buffer, 0, len,
307 cb->data_buf, 0, cb->data_buf->buf_size,
310 case UDI_GIO_OP_WRITE:
312 udi_pio_trans(uart_gio_write_complete, gcb,
313 rdata->pio_handles[PIO_TX], 0, cb->data_buf, &cb->data_buf->buf_size);
316 udi_gio_xfer_nak(cb, UDI_STAT_NOT_SUPPORTED);
320 void uart_gio_read_complete_short(udi_cb_t *gcb, udi_buf_t *new_buf)
322 rdata_t *rdata = gcb->context;
323 udi_gio_xfer_cb_t *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
324 cb->data_buf = new_buf;
326 // Delete read bytes from the RX buffer
327 CONTIN(uart_gio_read_complete, udi_buf_write,
328 (NULL, 0, rdata->rx_buffer, 0, cb->data_buf->buf_size, UDI_NULL_BUF_PATH),
329 (udi_buf_t *new_buf))
330 udi_gio_xfer_cb_t *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
331 rdata->rx_buffer = new_buf;
334 udi_gio_xfer_nak(cb, UDI_STAT_DATA_UNDERRUN);
336 void uart_gio_read_complete(udi_cb_t *gcb, udi_buf_t *new_buf)
338 rdata_t *rdata = gcb->context;
339 udi_gio_xfer_cb_t *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
340 cb->data_buf = new_buf;
341 // Delete read bytes from the RX buffer
342 CONTIN(uart_gio_read_complete, udi_buf_write,
343 (NULL, 0, rdata->rx_buffer, 0, cb->data_buf->buf_size, UDI_NULL_BUF_PATH),
344 (udi_buf_t *new_buf))
345 udi_gio_xfer_cb_t *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
346 rdata->rx_buffer = new_buf;
349 udi_gio_xfer_ack(cb);
351 void uart_gio_write_complete(udi_cb_t *gcb, udi_buf_t *new_buf, udi_status_t status, udi_ubit16_t res)
353 udi_gio_xfer_cb_t *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
354 cb->data_buf = new_buf;
355 udi_gio_xfer_ack(cb);
357 void uart_gio_prov_event_res(udi_gio_event_cb_t *cb)
359 udi_cb_t *gcb = UDI_GCB(cb);
360 rdata_t *rdata = gcb->context;
362 // If there was an event during dispatch, re-send
363 if( rdata->event_pending )
365 udi_gio_event_ind(cb);
366 rdata->event_pending = FALSE;
368 // otherwise, clear 'Event CB used' flag
371 rdata->event_cb_used = FALSE;
375 // ====================================================================
377 udi_mgmt_ops_t uart_mgmt_ops = {
381 uart_final_cleanup_req
383 udi_ubit8_t uart_mgmt_op_flags[4] = {0,0,0,0};
385 udi_bus_device_ops_t uart_bus_dev_ops = {
386 uart_bus_dev_channel_event_ind,
387 uart_bus_dev_bus_bind_ack,
388 uart_bus_dev_bus_unbind_ack,
389 uart_bus_dev_intr_attach_ack,
390 uart_bus_dev_intr_detach_ack
392 udi_ubit8_t uart_bus_dev_ops_flags[5] = {0};
394 udi_intr_handler_ops_t uart_bus_irq_ops = {
395 uart_bus_irq_channel_event_ind,
396 uart_bus_irq_intr_event_ind
398 udi_ubit8_t uart_bus_irq_ops_flags[2] = {0};
399 // - GIO provider ops
400 udi_gio_provider_ops_t uart_gio_prov_ops = {
401 uart_gio_prov_channel_event_ind,
402 uart_gio_prov_bind_req,
403 uart_gio_prov_unbind_req,
404 uart_gio_prov_xfer_req,
405 uart_gio_prov_event_res
407 udi_ubit8_t uart_gio_prov_ops_flags[5] = {0};
409 udi_primary_init_t uart_pri_init = {
410 .mgmt_ops = &uart_mgmt_ops,
411 .mgmt_op_flags = uart_mgmt_op_flags,
412 .mgmt_scratch_requirement = 0,
413 .enumeration_attr_list_length = 4,
414 .rdata_size = sizeof(rdata_t),
415 .child_data_size = 0,
416 .per_parent_paths = 0
418 udi_ops_init_t uart_ops_list[] = {
420 UART_OPS_DEV, UART_META_BUS, UDI_BUS_DEVICE_OPS_NUM,
422 (udi_ops_vector_t*)&uart_bus_dev_ops,
423 uart_bus_dev_ops_flags
426 UART_OPS_IRQ, UART_META_BUS, UDI_BUS_INTR_HANDLER_OPS_NUM,
428 (udi_ops_vector_t*)&uart_bus_irq_ops,
429 uart_bus_irq_ops_flags
432 UART_OPS_GIO, UART_META_GIO, UDI_GIO_PROVIDER_OPS_NUM,
434 (udi_ops_vector_t*)&uart_gio_prov_ops,
435 uart_gio_prov_ops_flags
439 udi_cb_init_t uart_cb_init_list[] = {
440 {UART_CB_BUS_BIND, UART_META_BUS, UDI_BUS_BIND_CB_NUM, 0, 0,NULL},
441 {UART_CB_INTR, UART_META_BUS, UDI_BUS_INTR_ATTACH_CB_NUM, 0, 0,NULL},
442 {UART_CB_INTR_EVENT, UART_META_BUS, UDI_BUS_INTR_EVENT_CB_NUM, 0, 0,NULL},
443 {UART_CB_GIO_EVENT, UART_META_GIO, UDI_GIO_EVENT_CB_NUM, 0, 0,NULL},
446 const udi_init_t udi_init_info = {
447 .primary_init_info = &uart_pri_init,
448 .ops_init_list = uart_ops_list,
449 .cb_init_list = uart_cb_init_list,