2 * UDI Ne2000 NIC Driver
3 * By John Hodge (thePowersGang)
9 #include <udi_physio.h>
10 #include "uart16c550_common.h"
13 #include "uart16c550_pio.h"
15 #define __EXPJOIN(a,b) a##b
16 #define _EXPJOIN(a,b) __EXPJOIN(a,b)
17 #define _EXPLODE(params...) params
18 #define _ADDGCB(params...) (udi_cb_t *gcb, params)
19 #define CONTIN(suffix, call, args, params) extern void _EXPJOIN(suffix##__,__LINE__) _ADDGCB params;\
20 call( _EXPJOIN(suffix##__,__LINE__), gcb, _EXPLODE args); } \
21 void _EXPJOIN(suffix##__,__LINE__) _ADDGCB params { \
22 rdata_t *rdata = gcb->context;
24 // --- Management Metalang
25 void uart_usage_ind(udi_usage_cb_t *cb, udi_ubit8_t resource_level)
27 rdata_t *rdata = UDI_GCB(cb)->context;
29 // TODO: Set up region data
34 void uart_enumerate_req(udi_enumerate_cb_t *cb, udi_ubit8_t enumeration_level)
36 rdata_t *rdata = UDI_GCB(cb)->context;
37 udi_instance_attr_list_t *attr_list = cb->attr_list;
39 switch(enumeration_level)
41 case UDI_ENUMERATE_START:
42 case UDI_ENUMERATE_START_RESCAN:
43 DPT_SET_ATTR_STRING(attr_list, "gio_type", "uart", 4);
45 cb->attr_valid_length = attr_list - cb->attr_list;
46 udi_enumerate_ack(cb, UDI_ENUMERATE_OK, UART_OPS_GIO);
48 case UDI_ENUMERATE_NEXT:
49 udi_enumerate_ack(cb, UDI_ENUMERATE_DONE, 0);
53 void uart_devmgmt_req(udi_mgmt_cb_t *cb, udi_ubit8_t mgmt_op, udi_ubit8_t parent_ID)
56 void uart_final_cleanup_req(udi_mgmt_cb_t *cb)
60 void uart_bus_dev_channel_event_ind(udi_channel_event_cb_t *cb);
61 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);
62 void uart_bus_dev_bind__pio_map(udi_cb_t *cb, udi_pio_handle_t new_pio_handle);
63 void uart_bus_dev_bind__intr_chanel(udi_cb_t *gcb, udi_channel_t new_channel);
64 void uart_bus_dev_bus_unbind_ack(udi_bus_bind_cb_t *cb);
66 void uart_bus_dev_channel_event_ind(udi_channel_event_cb_t *cb)
68 udi_cb_t *gcb = UDI_GCB(cb);
69 rdata_t *rdata = gcb->context;
73 case UDI_CHANNEL_CLOSED:
75 case UDI_CHANNEL_BOUND: {
76 rdata->active_cb = gcb;
77 udi_bus_bind_cb_t *bus_bind_cb = UDI_MCB(cb->params.parent_bound.bind_cb, udi_bus_bind_cb_t);
78 udi_bus_bind_req( bus_bind_cb );
79 // continue at uart_bus_dev_bus_bind_ack
83 void uart_bus_dev_bus_bind_ack(udi_bus_bind_cb_t *cb,
84 udi_dma_constraints_t dma_constraints, udi_ubit8_t perferred_endianness, udi_status_t status)
86 udi_cb_t *gcb = UDI_GCB(cb);
87 rdata_t *rdata = gcb->context;
90 rdata->init.pio_index = -1;
91 uart_bus_dev_bind__pio_map(gcb, UDI_NULL_PIO_HANDLE);
94 void uart_bus_dev_bind__pio_map(udi_cb_t *gcb, udi_pio_handle_t new_pio_handle)
96 rdata_t *rdata = gcb->context;
97 if( rdata->init.pio_index != -1 )
99 rdata->pio_handles[rdata->init.pio_index] = new_pio_handle;
102 rdata->init.pio_index ++;
103 if( rdata->init.pio_index < N_PIO )
105 udi_pio_map(uart_bus_dev_bind__pio_map, gcb,
106 UDI_PCI_BAR_0, 0, 0x8, // TODO: Use system bus index and offset
107 uart_pio_ops[rdata->init.pio_index].trans_list,
108 uart_pio_ops[rdata->init.pio_index].list_length,
109 UDI_PIO_LITTLE_ENDIAN, 0, 0
114 // Spawn the interrupt channel
115 CONTIN( uart_bus_dev_bind, udi_channel_spawn, (gcb->channel, 0, UART_OPS_IRQ, rdata),
116 (udi_channel_t new_channel))
118 rdata->interrupt_channel = new_channel;
120 // Allocate an RX buffer
121 CONTIN( uart_bus_dev_bind, udi_buf_write, (NULL, 0, NULL, 0, 0, UDI_NULL_BUF_PATH),
122 (udi_buf_t *new_buf));
126 udi_channel_event_complete( UDI_MCB(rdata->active_cb, udi_channel_event_cb_t),
127 UDI_STAT_RESOURCE_UNAVAIL );
130 rdata->rx_buffer = new_buf;
132 // Create interrupt CB
133 CONTIN( uart_bus_dev_bind, udi_cb_alloc, (UART_CB_INTR, gcb->channel),
138 udi_channel_event_complete( UDI_MCB(rdata->active_cb, udi_channel_event_cb_t),
139 UDI_STAT_RESOURCE_UNAVAIL );
142 udi_intr_attach_cb_t *intr_cb = UDI_MCB(new_cb, udi_intr_attach_cb_t);
143 intr_cb->interrupt_idx = 0;
144 intr_cb->min_event_pend = 1;
145 intr_cb->preprocessing_handle = rdata->pio_handles[PIO_INTR];
148 udi_intr_attach_req(intr_cb);
149 // continued in uart_bus_dev_intr_attach_ack
151 void uart_bus_dev_bus_unbind_ack(udi_bus_bind_cb_t *cb)
154 void uart_bus_dev_intr_attach_ack(udi_intr_attach_cb_t *intr_attach_cb, udi_status_t status)
156 udi_cb_t *gcb = UDI_GCB(intr_attach_cb);
157 rdata_t *rdata = gcb->context;
159 // Allocate interrupt cbs
160 CONTIN(uart_bus_dev_intr_attach_ack, udi_cb_alloc_batch,
161 (UART_CB_INTR_EVENT, NUM_INTR_CBS, TRUE, INTR_CB_BUF_SIZE, UDI_NULL_BUF_PATH),
162 (udi_cb_t *first_new_cb))
164 while( first_new_cb )
166 udi_intr_event_cb_t *cb = UDI_MCB(first_new_cb, udi_intr_event_cb_t);
167 first_new_cb = first_new_cb->initiator_context;
168 // - Set channel (udi_cb_alloc_batch sets the channel to gcb->channel)
169 cb->gcb.channel = rdata->interrupt_channel;
170 udi_intr_event_rdy(cb);
173 udi_channel_event_cb_t *channel_cb = UDI_MCB(rdata->active_cb, udi_channel_event_cb_t);
175 udi_channel_event_complete(channel_cb, UDI_OK);
178 void uart_bus_dev_intr_detach_ack(udi_intr_detach_cb_t *intr_detach_cb)
182 void uart_bus_irq_channel_event_ind(udi_channel_event_cb_t *cb)
185 void uart_bus_irq_intr_event_ind(udi_intr_event_cb_t *cb, udi_ubit8_t flags)
187 udi_cb_t *gcb = UDI_GCB(cb);
188 rdata_t *rdata = gcb->context;
190 udi_debug_printf("%s: flags=%x, intr_result=%x\n", __func__, flags, cb->intr_result);
191 if( cb->intr_result == 0 )
193 // An IRQ from something other than RX
194 udi_intr_event_rdy(cb);
198 if( rdata->rx_buffer && rdata->rx_buffer->buf_size + cb->intr_result > MAX_RX_BUFFER_SIZE )
200 // Drop, buffer is full
201 udi_debug_printf("%s: dropping %i bytes, full rx buffer\n", __func__, cb->intr_result);
202 udi_intr_event_rdy(cb);
206 // Copy cb->intr_result bytes into the RX buffer
207 CONTIN(uart_bus_irq_intr_event_ind, udi_buf_copy,
208 (cb->event_buf, 0, cb->intr_result,
209 rdata->rx_buffer, rdata->rx_buffer->buf_size, 0,
211 (udi_buf_t *new_buf));
213 udi_intr_event_cb_t *cb = UDI_MCB(gcb, udi_intr_event_cb_t);
215 rdata->rx_buffer = new_buf;
217 udi_debug_printf("%s: copied %i bytes\n", __func__, cb->intr_result);
219 // Emit a signal to GIO client
220 if( rdata->event_cb && !rdata->event_cb_used )
222 rdata->event_pending = FALSE;
223 rdata->event_cb_used = TRUE;
224 udi_gio_event_ind(rdata->event_cb);
228 udi_debug_printf("%s: event_cb=%p, event_cb_used=%i - Queueing\n",
229 __func__, rdata->event_cb, rdata->event_cb_used);
230 rdata->event_pending = TRUE;
233 udi_intr_event_rdy(cb);
236 void uart_gio_prov_channel_event_ind(udi_channel_event_cb_t *cb);
237 void uart_gio_prov_bind_req(udi_gio_bind_cb_t *cb);
238 void uart_gio_prov_xfer_req(udi_gio_xfer_cb_t *cb);
239 void uart_gio_read_complete_short(udi_cb_t *gcb, udi_buf_t *new_buf);
240 void uart_gio_read_complete(udi_cb_t *gcb, udi_buf_t *new_buf);
241 void uart_gio_write_complete(udi_cb_t *gcb, udi_buf_t *new_buf, udi_status_t status, udi_ubit16_t res);
242 void uart_gio_prov_event_res(udi_gio_event_cb_t *cb);
244 void uart_gio_prov_channel_event_ind(udi_channel_event_cb_t *cb)
247 void uart_gio_prov_bind_req(udi_gio_bind_cb_t *cb)
249 udi_cb_t *gcb = UDI_GCB(cb);
250 rdata_t *rdata = gcb->context;
252 // TODO: Should this device allow multiple children?
253 if( rdata->event_cb ) {
254 udi_debug_printf("%s: only one client allowed\n", __func__);
255 udi_gio_bind_ack(cb, 0, 0, UDI_STAT_CANNOT_BIND_EXCL);
259 // Allocate the event CB
260 CONTIN( uart_gio_prov_bind_req, udi_cb_alloc, (UART_CB_GIO_EVENT, gcb->channel),
262 udi_gio_bind_cb_t *cb = UDI_MCB(gcb, udi_gio_bind_cb_t);
264 rdata->event_cb = UDI_MCB(new_cb, udi_gio_event_cb_t);
265 rdata->event_cb_used = FALSE;
267 udi_gio_bind_ack(cb, 0, 0, UDI_OK);
269 void uart_gio_prov_unbind_req(udi_gio_bind_cb_t *cb)
271 udi_cb_t *gcb = UDI_GCB(cb);
272 rdata_t *rdata = gcb->context;
274 rdata->event_cb = NULL;
275 rdata->event_cb_used = FALSE;
276 udi_gio_unbind_ack(cb);
278 void uart_gio_prov_xfer_req(udi_gio_xfer_cb_t *cb)
280 udi_cb_t *gcb = UDI_GCB(cb);
281 rdata_t *rdata = gcb->context;
284 case UDI_GIO_OP_READ: {
285 udi_buf_copy_call_t *callback;
287 // Read from local FIFO
288 if( rdata->rx_buffer->buf_size < cb->data_buf->buf_size ) {
289 len = rdata->rx_buffer->buf_size;
290 callback = uart_gio_read_complete_short;
293 len = cb->data_buf->buf_size;
294 callback = uart_gio_read_complete;
296 udi_debug_printf("%s: Read %i/%i bytes\n", __func__, len, rdata->rx_buffer->buf_size);
297 // Replace entire destination buffer with `len` bytes from source
298 udi_buf_copy(callback, gcb,
299 rdata->rx_buffer, 0, len,
300 cb->data_buf, 0, cb->data_buf->buf_size,
303 case UDI_GIO_OP_WRITE:
305 udi_pio_trans(uart_gio_write_complete, gcb,
306 rdata->pio_handles[PIO_TX], 0, cb->data_buf, &cb->data_buf->buf_size);
309 udi_gio_xfer_nak(cb, UDI_STAT_NOT_SUPPORTED);
313 void uart_gio_read_complete_short(udi_cb_t *gcb, udi_buf_t *new_buf)
315 rdata_t *rdata = gcb->context;
316 udi_gio_xfer_cb_t *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
317 cb->data_buf = new_buf;
319 // Delete read bytes from the RX buffer
320 CONTIN(uart_gio_read_complete, udi_buf_write,
321 (NULL, 0, rdata->rx_buffer, 0, cb->data_buf->buf_size, UDI_NULL_BUF_PATH),
322 (udi_buf_t *new_buf))
323 udi_gio_xfer_cb_t *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
324 rdata->rx_buffer = new_buf;
327 udi_gio_xfer_nak(cb, UDI_STAT_DATA_UNDERRUN);
329 void uart_gio_read_complete(udi_cb_t *gcb, udi_buf_t *new_buf)
331 rdata_t *rdata = gcb->context;
332 udi_gio_xfer_cb_t *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
333 cb->data_buf = new_buf;
334 // Delete read bytes from the RX buffer
335 CONTIN(uart_gio_read_complete, udi_buf_write,
336 (NULL, 0, rdata->rx_buffer, 0, cb->data_buf->buf_size, UDI_NULL_BUF_PATH),
337 (udi_buf_t *new_buf))
338 udi_gio_xfer_cb_t *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
339 rdata->rx_buffer = new_buf;
342 udi_gio_xfer_ack(cb);
344 void uart_gio_write_complete(udi_cb_t *gcb, udi_buf_t *new_buf, udi_status_t status, udi_ubit16_t res)
346 udi_gio_xfer_cb_t *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
347 cb->data_buf = new_buf;
348 udi_gio_xfer_ack(cb);
350 void uart_gio_prov_event_res(udi_gio_event_cb_t *cb)
352 udi_cb_t *gcb = UDI_GCB(cb);
353 rdata_t *rdata = gcb->context;
355 // If there was an event during dispatch, re-send
356 if( rdata->event_pending )
358 udi_gio_event_ind(cb);
359 rdata->event_pending = FALSE;
361 // otherwise, clear 'Event CB used' flag
364 rdata->event_cb_used = FALSE;
368 // ====================================================================
370 udi_mgmt_ops_t uart_mgmt_ops = {
374 uart_final_cleanup_req
376 udi_ubit8_t uart_mgmt_op_flags[4] = {0,0,0,0};
378 udi_bus_device_ops_t uart_bus_dev_ops = {
379 uart_bus_dev_channel_event_ind,
380 uart_bus_dev_bus_bind_ack,
381 uart_bus_dev_bus_unbind_ack,
382 uart_bus_dev_intr_attach_ack,
383 uart_bus_dev_intr_detach_ack
385 udi_ubit8_t uart_bus_dev_ops_flags[5] = {0};
387 udi_intr_handler_ops_t uart_bus_irq_ops = {
388 uart_bus_irq_channel_event_ind,
389 uart_bus_irq_intr_event_ind
391 udi_ubit8_t uart_bus_irq_ops_flags[2] = {0};
392 // - GIO provider ops
393 udi_gio_provider_ops_t uart_gio_prov_ops = {
394 uart_gio_prov_channel_event_ind,
395 uart_gio_prov_bind_req,
396 uart_gio_prov_unbind_req,
397 uart_gio_prov_xfer_req,
398 uart_gio_prov_event_res
400 udi_ubit8_t uart_gio_prov_ops_flags[5] = {0};
402 udi_primary_init_t uart_pri_init = {
403 .mgmt_ops = &uart_mgmt_ops,
404 .mgmt_op_flags = uart_mgmt_op_flags,
405 .mgmt_scratch_requirement = 0,
406 .enumeration_attr_list_length = 4,
407 .rdata_size = sizeof(rdata_t),
408 .child_data_size = 0,
409 .per_parent_paths = 0
411 udi_ops_init_t uart_ops_list[] = {
413 UART_OPS_DEV, UART_META_BUS, UDI_BUS_DEVICE_OPS_NUM,
415 (udi_ops_vector_t*)&uart_bus_dev_ops,
416 uart_bus_dev_ops_flags
419 UART_OPS_IRQ, UART_META_BUS, UDI_BUS_INTR_HANDLER_OPS_NUM,
421 (udi_ops_vector_t*)&uart_bus_irq_ops,
422 uart_bus_irq_ops_flags
425 UART_OPS_GIO, UART_META_GIO, UDI_GIO_PROVIDER_OPS_NUM,
427 (udi_ops_vector_t*)&uart_gio_prov_ops,
428 uart_gio_prov_ops_flags
432 udi_cb_init_t uart_cb_init_list[] = {
433 {UART_CB_BUS_BIND, UART_META_BUS, UDI_BUS_BIND_CB_NUM, 0, 0,NULL},
434 {UART_CB_INTR, UART_META_BUS, UDI_BUS_INTR_ATTACH_CB_NUM, 0, 0,NULL},
435 {UART_CB_INTR_EVENT, UART_META_BUS, UDI_BUS_INTR_EVENT_CB_NUM, 0, 0,NULL},
436 {UART_CB_GIO_EVENT, UART_META_GIO, UDI_GIO_EVENT_CB_NUM, 0, 0,NULL},
439 const udi_init_t udi_init_info = {
440 .primary_init_info = &uart_pri_init,
441 .ops_init_list = uart_ops_list,
442 .cb_init_list = uart_cb_init_list,