2 * UDI Ne2000 NIC Driver
3 * By John Hodge (thePowersGang)
8 #define UDI_VERSION 0x101
9 #define UDI_PHYSIO_VERSION 0x101
11 #include <udi_physio.h>
12 #include "uart16c550_common.h"
14 #include "uart16c550_pio.h"
17 # define DEBUG_OUT(fmt, v...) udi_debug_printf("%s: "fmt"\n", __func__ ,## v)
19 # define DEBUG_OUT(...) do{}while(0)
22 #define __EXPJOIN(a,b) a##b
23 #define _EXPJOIN(a,b) __EXPJOIN(a,b)
24 #define _EXPLODE(params...) params
25 #define _ADDGCB(params...) (udi_cb_t *gcb, params)
26 #define CONTIN(suffix, call, args, params) extern void _EXPJOIN(suffix##__,__LINE__) _ADDGCB params;\
27 call( _EXPJOIN(suffix##__,__LINE__), gcb, _EXPLODE args); } \
28 void _EXPJOIN(suffix##__,__LINE__) _ADDGCB params { \
29 rdata_t *rdata = gcb->context;
31 // --- Management Metalang
32 void uart_usage_ind(udi_usage_cb_t *cb, udi_ubit8_t resource_level)
34 rdata_t *rdata = UDI_GCB(cb)->context;
35 //udi_trace_write(rdata->init_context, UDI_TREVENT_LOCAL_PROC_ENTRY, 0, );
37 // TODO: Set up region data
42 void uart_enumerate_req(udi_enumerate_cb_t *cb, udi_ubit8_t enumeration_level)
44 rdata_t *rdata = UDI_GCB(cb)->context;
45 udi_instance_attr_list_t *attr_list = cb->attr_list;
47 switch(enumeration_level)
49 case UDI_ENUMERATE_START:
50 case UDI_ENUMERATE_START_RESCAN:
51 DPT_SET_ATTR_STRING(attr_list, "gio_type", "uart", 4);
53 cb->attr_valid_length = attr_list - cb->attr_list;
54 udi_enumerate_ack(cb, UDI_ENUMERATE_OK, UART_OPS_GIO);
56 case UDI_ENUMERATE_NEXT:
57 udi_enumerate_ack(cb, UDI_ENUMERATE_DONE, 0);
61 void uart_devmgmt_req(udi_mgmt_cb_t *cb, udi_ubit8_t mgmt_op, udi_ubit8_t parent_ID)
64 void uart_final_cleanup_req(udi_mgmt_cb_t *cb)
68 void uart_bus_dev_channel_event_ind(udi_channel_event_cb_t *cb);
69 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);
70 void uart_bus_dev_bind__pio_map(udi_cb_t *cb, udi_pio_handle_t new_pio_handle);
71 void uart_bus_dev_bind__intr_chanel(udi_cb_t *gcb, udi_channel_t new_channel);
72 void uart_bus_dev_bus_unbind_ack(udi_bus_bind_cb_t *cb);
74 void uart_bus_dev_channel_event_ind(udi_channel_event_cb_t *cb)
76 udi_cb_t *gcb = UDI_GCB(cb);
77 rdata_t *rdata = gcb->context;
81 case UDI_CHANNEL_CLOSED:
83 case UDI_CHANNEL_BOUND: {
84 rdata->active_cb = gcb;
85 udi_bus_bind_cb_t *bus_bind_cb = UDI_MCB(cb->params.parent_bound.bind_cb, udi_bus_bind_cb_t);
86 udi_bus_bind_req( bus_bind_cb );
87 // continue at uart_bus_dev_bus_bind_ack
91 void uart_bus_dev_bus_bind_ack(udi_bus_bind_cb_t *cb,
92 udi_dma_constraints_t dma_constraints, udi_ubit8_t perferred_endianness, udi_status_t status)
94 udi_cb_t *gcb = UDI_GCB(cb);
95 rdata_t *rdata = gcb->context;
98 rdata->init.pio_index = -1;
99 uart_bus_dev_bind__pio_map(gcb, UDI_NULL_PIO_HANDLE);
102 void uart_bus_dev_bind__pio_map(udi_cb_t *gcb, udi_pio_handle_t new_pio_handle)
104 rdata_t *rdata = gcb->context;
105 if( rdata->init.pio_index != -1 )
107 rdata->pio_handles[rdata->init.pio_index] = new_pio_handle;
110 rdata->init.pio_index ++;
111 if( rdata->init.pio_index < N_PIO )
113 udi_pio_map(uart_bus_dev_bind__pio_map, gcb,
114 UDI_PCI_BAR_0, 0, 0x8, // TODO: Use system bus index and offset
115 uart_pio_ops[rdata->init.pio_index].trans_list,
116 uart_pio_ops[rdata->init.pio_index].list_length,
117 UDI_PIO_LITTLE_ENDIAN, 0, 0
122 // Spawn the interrupt channel
123 CONTIN( uart_bus_dev_bind, udi_channel_spawn, (gcb->channel, 0, UART_OPS_IRQ, rdata),
124 (udi_channel_t new_channel))
126 rdata->interrupt_channel = new_channel;
128 // Allocate an RX buffer
129 CONTIN( uart_bus_dev_bind, udi_buf_write, (NULL, 0, NULL, 0, 0, UDI_NULL_BUF_PATH),
130 (udi_buf_t *new_buf));
134 udi_channel_event_complete( UDI_MCB(rdata->active_cb, udi_channel_event_cb_t),
135 UDI_STAT_RESOURCE_UNAVAIL );
138 rdata->rx_buffer = new_buf;
140 // Create interrupt CB
141 CONTIN( uart_bus_dev_bind, udi_cb_alloc, (UART_CB_INTR, gcb->channel),
146 udi_channel_event_complete( UDI_MCB(rdata->active_cb, udi_channel_event_cb_t),
147 UDI_STAT_RESOURCE_UNAVAIL );
150 udi_intr_attach_cb_t *intr_cb = UDI_MCB(new_cb, udi_intr_attach_cb_t);
151 intr_cb->interrupt_idx = 0;
152 intr_cb->min_event_pend = 1;
153 intr_cb->preprocessing_handle = rdata->pio_handles[PIO_INTR];
156 udi_intr_attach_req(intr_cb);
157 // continued in uart_bus_dev_intr_attach_ack
159 void uart_bus_dev_bus_unbind_ack(udi_bus_bind_cb_t *cb)
162 void uart_bus_dev_intr_attach_ack(udi_intr_attach_cb_t *intr_attach_cb, udi_status_t status)
164 udi_cb_t *gcb = UDI_GCB(intr_attach_cb);
165 rdata_t *rdata = gcb->context;
167 // Allocate interrupt cbs
168 CONTIN(uart_bus_dev_intr_attach_ack, udi_cb_alloc_batch,
169 (UART_CB_INTR_EVENT, NUM_INTR_CBS, TRUE, INTR_CB_BUF_SIZE, UDI_NULL_BUF_PATH),
170 (udi_cb_t *first_new_cb))
172 while( first_new_cb )
174 udi_intr_event_cb_t *cb = UDI_MCB(first_new_cb, udi_intr_event_cb_t);
175 first_new_cb = first_new_cb->initiator_context;
176 // - Set channel (udi_cb_alloc_batch sets the channel to gcb->channel)
177 cb->gcb.channel = rdata->interrupt_channel;
178 udi_intr_event_rdy(cb);
181 udi_channel_event_cb_t *channel_cb = UDI_MCB(rdata->active_cb, udi_channel_event_cb_t);
183 udi_channel_event_complete(channel_cb, UDI_OK);
186 void uart_bus_dev_intr_detach_ack(udi_intr_detach_cb_t *intr_detach_cb)
190 void uart_bus_irq_channel_event_ind(udi_channel_event_cb_t *cb)
193 void uart_bus_irq_intr_event_ind(udi_intr_event_cb_t *cb, udi_ubit8_t flags)
195 udi_cb_t *gcb = UDI_GCB(cb);
196 rdata_t *rdata = gcb->context;
198 DEBUG_OUT("flags=%x, intr_result=%x", flags, cb->intr_result);
199 if( cb->intr_result == 0 )
201 // An IRQ from something other than RX
202 udi_intr_event_rdy(cb);
206 if( rdata->rx_buffer && rdata->rx_buffer->buf_size + cb->intr_result > MAX_RX_BUFFER_SIZE )
208 // Drop, buffer is full
209 DEBUG_OUT("dropping %i bytes, full rx buffer", cb->intr_result);
210 udi_intr_event_rdy(cb);
214 // Copy cb->intr_result bytes into the RX buffer
215 CONTIN(uart_bus_irq_intr_event_ind, udi_buf_copy,
216 (cb->event_buf, 0, cb->intr_result,
217 rdata->rx_buffer, rdata->rx_buffer->buf_size, 0,
219 (udi_buf_t *new_buf));
221 udi_intr_event_cb_t *cb = UDI_MCB(gcb, udi_intr_event_cb_t);
223 rdata->rx_buffer = new_buf;
225 DEBUG_OUT("copied %i bytes", cb->intr_result);
227 // Emit a signal to GIO client
228 if( rdata->event_cb && !rdata->event_cb_used )
230 rdata->event_pending = FALSE;
231 rdata->event_cb_used = TRUE;
232 udi_gio_event_ind(rdata->event_cb);
236 DEBUG_OUT("event_cb=%p, event_cb_used=%i - Queueing",
237 rdata->event_cb, rdata->event_cb_used);
238 rdata->event_pending = TRUE;
241 udi_intr_event_rdy(cb);
244 void uart_gio_prov_channel_event_ind(udi_channel_event_cb_t *cb);
245 void uart_gio_prov_bind_req(udi_gio_bind_cb_t *cb);
246 void uart_gio_prov_xfer_req(udi_gio_xfer_cb_t *cb);
247 void uart_gio_read_complete_short(udi_cb_t *gcb, udi_buf_t *new_buf);
248 void uart_gio_read_complete(udi_cb_t *gcb, udi_buf_t *new_buf);
249 void uart_gio_write_complete(udi_cb_t *gcb, udi_buf_t *new_buf, udi_status_t status, udi_ubit16_t res);
250 void uart_gio_prov_event_res(udi_gio_event_cb_t *cb);
252 void uart_gio_prov_channel_event_ind(udi_channel_event_cb_t *cb)
255 void uart_gio_prov_bind_req(udi_gio_bind_cb_t *cb)
257 udi_cb_t *gcb = UDI_GCB(cb);
258 rdata_t *rdata = gcb->context;
260 // TODO: Should this device allow multiple children?
261 if( rdata->event_cb ) {
262 DEBUG_OUT("only one client allowed");
263 udi_gio_bind_ack(cb, 0, 0, UDI_STAT_CANNOT_BIND_EXCL);
267 // Allocate the event CB
268 CONTIN( uart_gio_prov_bind_req, udi_cb_alloc, (UART_CB_GIO_EVENT, gcb->channel),
270 udi_gio_bind_cb_t *cb = UDI_MCB(gcb, udi_gio_bind_cb_t);
272 rdata->event_cb = UDI_MCB(new_cb, udi_gio_event_cb_t);
273 rdata->event_cb_used = FALSE;
275 udi_gio_bind_ack(cb, 0, 0, UDI_OK);
277 void uart_gio_prov_unbind_req(udi_gio_bind_cb_t *cb)
279 udi_cb_t *gcb = UDI_GCB(cb);
280 rdata_t *rdata = gcb->context;
282 rdata->event_cb = NULL;
283 rdata->event_cb_used = FALSE;
284 udi_gio_unbind_ack(cb);
286 void uart_gio_prov_xfer_req(udi_gio_xfer_cb_t *cb)
288 udi_cb_t *gcb = UDI_GCB(cb);
289 rdata_t *rdata = gcb->context;
292 case UDI_GIO_OP_READ: {
293 udi_buf_copy_call_t *callback;
295 // Read from local FIFO
296 if( rdata->rx_buffer->buf_size < cb->data_buf->buf_size ) {
297 len = rdata->rx_buffer->buf_size;
298 callback = uart_gio_read_complete_short;
301 len = cb->data_buf->buf_size;
302 callback = uart_gio_read_complete;
304 DEBUG_OUT("Read %i/%i bytes", len, rdata->rx_buffer->buf_size);
305 // Replace entire destination buffer with `len` bytes from source
306 udi_buf_copy(callback, gcb,
307 rdata->rx_buffer, 0, len,
308 cb->data_buf, 0, cb->data_buf->buf_size,
311 case UDI_GIO_OP_WRITE:
313 udi_pio_trans(uart_gio_write_complete, gcb,
314 rdata->pio_handles[PIO_TX], 0, cb->data_buf, &cb->data_buf->buf_size);
317 udi_gio_xfer_nak(cb, UDI_STAT_NOT_SUPPORTED);
321 void uart_gio_read_complete_short(udi_cb_t *gcb, udi_buf_t *new_buf)
323 rdata_t *rdata = gcb->context;
324 udi_gio_xfer_cb_t *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
325 cb->data_buf = new_buf;
327 // Delete read bytes from the RX buffer
328 CONTIN(uart_gio_read_complete, udi_buf_write,
329 (NULL, 0, rdata->rx_buffer, 0, cb->data_buf->buf_size, UDI_NULL_BUF_PATH),
330 (udi_buf_t *new_buf))
331 udi_gio_xfer_cb_t *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
332 rdata->rx_buffer = new_buf;
335 udi_gio_xfer_nak(cb, UDI_STAT_DATA_UNDERRUN);
337 void uart_gio_read_complete(udi_cb_t *gcb, udi_buf_t *new_buf)
339 rdata_t *rdata = gcb->context;
340 udi_gio_xfer_cb_t *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
341 cb->data_buf = new_buf;
342 // Delete read bytes from the RX buffer
343 CONTIN(uart_gio_read_complete, udi_buf_write,
344 (NULL, 0, rdata->rx_buffer, 0, cb->data_buf->buf_size, UDI_NULL_BUF_PATH),
345 (udi_buf_t *new_buf))
346 udi_gio_xfer_cb_t *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
347 rdata->rx_buffer = new_buf;
350 udi_gio_xfer_ack(cb);
352 void uart_gio_write_complete(udi_cb_t *gcb, udi_buf_t *new_buf, udi_status_t status, udi_ubit16_t res)
354 udi_gio_xfer_cb_t *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
355 cb->data_buf = new_buf;
356 udi_gio_xfer_ack(cb);
358 void uart_gio_prov_event_res(udi_gio_event_cb_t *cb)
360 udi_cb_t *gcb = UDI_GCB(cb);
361 rdata_t *rdata = gcb->context;
363 // If there was an event during dispatch, re-send
364 if( rdata->event_pending )
366 udi_gio_event_ind(cb);
367 rdata->event_pending = FALSE;
369 // otherwise, clear 'Event CB used' flag
372 rdata->event_cb_used = FALSE;
376 // ====================================================================
378 udi_mgmt_ops_t uart_mgmt_ops = {
382 uart_final_cleanup_req
384 udi_ubit8_t uart_mgmt_op_flags[4] = {0,0,0,0};
386 udi_bus_device_ops_t uart_bus_dev_ops = {
387 uart_bus_dev_channel_event_ind,
388 uart_bus_dev_bus_bind_ack,
389 uart_bus_dev_bus_unbind_ack,
390 uart_bus_dev_intr_attach_ack,
391 uart_bus_dev_intr_detach_ack
393 udi_ubit8_t uart_bus_dev_ops_flags[5] = {0};
395 udi_intr_handler_ops_t uart_bus_irq_ops = {
396 uart_bus_irq_channel_event_ind,
397 uart_bus_irq_intr_event_ind
399 udi_ubit8_t uart_bus_irq_ops_flags[2] = {0};
400 // - GIO provider ops
401 udi_gio_provider_ops_t uart_gio_prov_ops = {
402 uart_gio_prov_channel_event_ind,
403 uart_gio_prov_bind_req,
404 uart_gio_prov_unbind_req,
405 uart_gio_prov_xfer_req,
406 uart_gio_prov_event_res
408 udi_ubit8_t uart_gio_prov_ops_flags[5] = {0};
410 udi_primary_init_t uart_pri_init = {
411 .mgmt_ops = &uart_mgmt_ops,
412 .mgmt_op_flags = uart_mgmt_op_flags,
413 .mgmt_scratch_requirement = 0,
414 .enumeration_attr_list_length = 4,
415 .rdata_size = sizeof(rdata_t),
416 .child_data_size = 0,
417 .per_parent_paths = 0
419 udi_ops_init_t uart_ops_list[] = {
421 UART_OPS_DEV, UART_META_BUS, UDI_BUS_DEVICE_OPS_NUM,
423 (udi_ops_vector_t*)&uart_bus_dev_ops,
424 uart_bus_dev_ops_flags
427 UART_OPS_IRQ, UART_META_BUS, UDI_BUS_INTR_HANDLER_OPS_NUM,
429 (udi_ops_vector_t*)&uart_bus_irq_ops,
430 uart_bus_irq_ops_flags
433 UART_OPS_GIO, UART_META_GIO, UDI_GIO_PROVIDER_OPS_NUM,
435 (udi_ops_vector_t*)&uart_gio_prov_ops,
436 uart_gio_prov_ops_flags
440 udi_cb_init_t uart_cb_init_list[] = {
441 {UART_CB_BUS_BIND, UART_META_BUS, UDI_BUS_BIND_CB_NUM, 0, 0,NULL},
442 {UART_CB_INTR, UART_META_BUS, UDI_BUS_INTR_ATTACH_CB_NUM, 0, 0,NULL},
443 {UART_CB_INTR_EVENT, UART_META_BUS, UDI_BUS_INTR_EVENT_CB_NUM, 0, 0,NULL},
444 {UART_CB_GIO_EVENT, UART_META_GIO, UDI_GIO_EVENT_CB_NUM, 0, 0,NULL},
447 const udi_init_t udi_init_info = {
448 .primary_init_info = &uart_pri_init,
449 .ops_init_list = uart_ops_list,
450 .cb_init_list = uart_cb_init_list,