UDI/ne2000 - RX code implemented
[tpg/acess2.git] / UDI / drivers / net_ne2000 / ne2000_core.c
1 /*
2  * UDI Ne2000 NIC Driver
3  * By John Hodge (thePowersGang)
4  *
5  * ne2000_core.c
6  * - UDI initialisation 
7  */
8 #include <udi.h>
9 #include <udi_nic.h>
10 #include "ne2000_common.h"
11
12 enum {
13         NE2K_META_BUS = 1,
14         NE2K_META_NIC,
15 };
16 enum {
17         NE2K_OPS_DEV = 1,
18         NE2K_OPS_CTRL,
19         NE2K_OPS_TX,
20         NE2K_OPS_RX,
21         NE2K_OPS_IRQ,
22 };
23 enum {
24         NE2K_CB_INTR = 1,
25         NE2K_CB_INTR_EVENT,
26 };
27
28 #define NE2K_NUM_INTR_EVENT_CBS 4
29
30 // === GLOBALS ===
31 #define PIO_op_RI(op, reg, sz, val)     {UDI_PIO_##op+UDI_PIO_DIRECT+UDI_PIO_##reg, UDI_PIO_##sz##BYTE, val}
32 #define PIO_MOV_RI1(reg, val)   PIO_op_RI(LOAD_IMM, reg, 1, val)
33 #define PIO_OUT_RI1(reg, ofs)   PIO_op_RI(OUT, reg, 1, ofs)
34 #define PIO_IN_RI1(reg, ofs)    PIO_op_RI(IN, reg, 1, ofs)
35 // --- Programmed IO ---
36 #include "ne2000_pio.h"
37
38 // === CODE ===
39 // --- Management
40 void ne2k_usage_ind(udi_usage_cb_t *cb, udi_ubit8_t resource_level)
41 {
42 }
43 void ne2k_enumerate_req(udi_enumerate_cb_t *cb, udi_ubit8_t enumeration_level)
44 {
45         ne2k_rdata_t    *rdata = UDI_GCB(cb)->context;
46         udi_instance_attr_list_t *attr_list = cb->attr_list;
47         
48         switch(enumeration_level)
49         {
50         case UDI_ENUMERATE_START:
51         case UDI_ENUMERATE_START_RESCAN:
52                 // Emit the ND binding
53                 DPT_SET_ATTR32(attr_list, "if_num", 0);
54                 attr_list ++;
55                 DPT_SET_ATTR_STRING(attr_list, "if_media", "eth", 3);
56                 attr_list ++;
57                 NE2K_SET_ATTR_STRFMT(attr_list, "identifier", 2*6+1, "%2X%2X%2X%2X%2X%2X",
58                         rdata->macaddr[0], rdata->macaddr[1], rdata->macaddr[2],
59                         rdata->macaddr[3], rdata->macaddr[4], rdata->macaddr[5] );
60                 attr_list ++;
61                 udi_enumerate_ack(cb, UDI_ENUMERATE_OK, NE2K_OPS_CTRL);
62                 break;
63         case UDI_ENUMERATE_NEXT:
64                 udi_enumerate_ack(cb, UDI_ENUMERATE_DONE, 0);
65                 break;
66         }
67 }
68 void ne2k_devmgmt_req(udi_mgmt_cb_t *cb, udi_ubit8_t mgmt_op, udi_ubit8_t parent_ID)
69 {
70 }
71 void ne2k_final_cleanup_req(udi_mgmt_cb_t *cb)
72 {
73 }
74 // --- Bus
75 void ne2k_bus_dev_channel_event_ind(udi_channel_event_cb_t *cb)
76 {
77         switch(cb->event)
78         {
79         case UDI_CHANNEL_CLOSED:
80                 break;
81         case UDI_CHANNEL_BOUND: {
82                 udi_bus_bind_cb_t *bus_bind_cb = UDI_MCB(cb->params.parent_bound.bind_cb, udi_bus_bind_cb_t);
83                 udi_bus_bind_req( bus_bind_cb );
84                 // continue at ne2k_bus_dev_bus_bind_ack
85                 return; }
86         }
87 }
88 void ne2k_bus_dev_bus_bind_ack(udi_bus_bind_cb_t *cb,
89         udi_dma_constraints_t dma_constraints, udi_ubit8_t perferred_endianness, udi_status_t status)
90 {
91         udi_cb_t        *gcb = UDI_GCB(cb);
92         ne2k_rdata_t    *rdata = gcb->context;
93         rdata->active_cb = gcb;
94         
95         // Set up PIO handles
96         rdata->init.pio_index = -1;
97         ne2k_bus_dev_bind__pio_map(gcb, UDI_NULL_PIO_HANDLE);
98 }
99 void ne2k_bus_dev_bind__pio_map(udi_cb_t *gcb, udi_pio_handle_t new_pio_handle)
100 {
101         ne2k_rdata_t    *rdata = gcb->context;
102         
103         if( rdata->init.pio_index != -1 )
104         {
105                 rdata->pio_handles[rdata->init.pio_index] = new_pio_handle;
106         }
107         rdata->init.pio_index ++;
108         if( rdata->init.pio_index < NE2K_NUM_PIO_OPS )
109         {
110                 udi_pio_map(ne2k_bus_dev_bind__pio_map, gcb,
111                         UDI_PCI_BAR_0, 0, 0x20,
112                         ne2k_pio_ops[rdata->init.pio_index].trans_list,
113                         ne2k_pio_ops[rdata->init.pio_index].list_length,
114                         UDI_PIO_LITTLE_ENDIAN, 0, 0
115                         );
116                 return ;
117         }
118         
119         // Next: Bind interrupt
120         udi_channel_spawn(ne2k_bus_dev_bind__intr_chanel, gcb, gcb->channel,
121                 0, NE2K_OPS_IRQ, rdata);
122 }
123 void ne2k_bus_dev_bind__intr_chanel(udi_cb_t *gcb, udi_channel_t new_channel)
124 {
125         ne2k_rdata_t    *rdata = gcb->context;
126         
127         rdata->interrupt_channel = new_channel;
128         
129         udi_cb_alloc(ne2k_bus_dev_bind__intr_attach, gcb, NE2K_CB_INTR, gcb->channel);
130 }
131 void ne2k_bus_dev_bind__intr_attach(udi_cb_t *gcb, udi_cb_t *new_cb)
132 {
133         ne2k_rdata_t    *rdata = gcb->context;
134         udi_intr_attach_cb_t    *intr_cb = UDI_MCB(new_cb, udi_intr_attach_cb_t);
135         intr_cb->interrupt_idx = 0;
136         intr_cb->min_event_pend = 2;
137         intr_cb->preprocessing_handle = rdata->pio_handles[NE2K_PIO_IRQACK];
138         udi_intr_attach_req(intr_cb);
139         // continued in ne2k_bus_dev_intr_attach_ack
140 }
141 void ne2k_bus_dev_bus_unbind_ack(udi_bus_bind_cb_t *cb)
142 {
143 }
144 void ne2k_bus_dev_intr_attach_ack(udi_intr_attach_cb_t *intr_attach_cb, udi_status_t status)
145 {
146         udi_cb_t        *gcb = UDI_GCB(intr_attach_cb);
147         ne2k_rdata_t    *rdata = gcb->context;
148         // continuing from ne2k_bus_dev_bind__intr_attach
149         if( status != UDI_OK ) {
150                 // TODO: Error
151                 udi_cb_free( UDI_GCB(intr_attach_cb) );
152                 return ;
153         }
154         
155         rdata->intr_attach_cb = intr_attach_cb;
156         
157         rdata->init.n_intr_event_cb = 0;
158         udi_cb_alloc(ne2k_bus_dev_bind__intr_event_cb, gcb, NE2K_CB_INTR_EVENT, rdata->interrupt_channel);
159         // V V V V
160 }
161 void ne2k_bus_dev_bind__intr_event_cb(udi_cb_t *gcb, udi_cb_t *new_cb)
162 {
163         ne2k_rdata_t    *rdata = gcb->context;
164         
165         udi_intr_event_cb_t *intr_event_cb = UDI_MCB(new_cb, udi_intr_event_cb_t);
166         udi_intr_event_rdy(intr_event_cb);
167         rdata->init.n_intr_event_cb ++;
168         
169         if( rdata->init.n_intr_event_cb < NE2K_NUM_INTR_EVENT_CBS )
170         {
171                 udi_cb_alloc(ne2k_bus_dev_bind__intr_event_cb, gcb,
172                         NE2K_CB_INTR_EVENT, rdata->interrupt_channel);
173                 // A A A A
174                 return ;
175         }
176
177         udi_pio_trans(ne2k_bus_dev_bind__card_reset, gcb,
178                 rdata->pio_handles[NE2K_PIO_RESET], 0, NULL, &rdata->macaddr);
179         // V V V V
180 }
181
182 void ne2k_bus_dev_bind__card_reset(udi_cb_t *gcb, udi_buf_t *new_buf, udi_status_t status, udi_ubit16_t result)
183 {       
184         ne2k_rdata_t    *rdata = gcb->context;
185         // Done! (Finally)
186         udi_channel_event_complete( UDI_MCB(rdata->active_cb, udi_channel_event_cb_t), UDI_OK );
187         // = = = =
188 }
189 void ne2k_bus_dev_intr_detach_ack(udi_intr_detach_cb_t *intr_detach_cb)
190 {
191 }
192 // --- ND Common
193 void ne2k_nd_ctrl_channel_event_ind(udi_channel_event_cb_t *cb)
194 {
195 }
196 void ne2k_nd_ctrl_bind_req(udi_nic_bind_cb_t *cb, udi_index_t tx_chan_index, udi_index_t rx_chan_index)
197 {
198         udi_cb_t        *gcb = UDI_GCB(cb);
199         ne2k_rdata_t    *rdata = gcb->context;
200         rdata->init.rx_chan_index = rx_chan_index;
201         udi_channel_spawn(ne2k_nd_ctrl_bind__tx_chan_ok, gcb, gcb->channel, tx_chan_index, NE2K_OPS_TX, rdata);
202         // V V V V
203 }
204 void ne2k_nd_ctrl_bind__tx_chan_ok(udi_cb_t *gcb, udi_channel_t new_channel)
205 {
206         ne2k_rdata_t    *rdata = gcb->context;
207         rdata->tx_channel = new_channel;
208         udi_channel_spawn(ne2k_nd_ctrl_bind__rx_chan_ok, gcb, gcb->channel,
209                 rdata->init.rx_chan_index, NE2K_OPS_RX, rdata);
210         // V V V V
211 }
212 void ne2k_nd_ctrl_bind__rx_chan_ok(udi_cb_t *cb, udi_channel_t new_channel)
213 {
214         ne2k_rdata_t    *rdata = cb->context;
215         rdata->rx_channel = new_channel;
216         udi_nsr_bind_ack( UDI_MCB(cb, udi_nic_bind_cb_t), UDI_OK );
217         // = = = =
218 }
219 void ne2k_nd_ctrl_unbind_req(udi_nic_cb_t *cb)
220 {
221 }
222 void ne2k_nd_ctrl_enable_req(udi_nic_cb_t *cb)
223 {
224         // Enable
225 }
226 void ne2k_nd_ctrl_disable_req(udi_nic_cb_t *cb)
227 {
228 }
229 void ne2k_nd_ctrl_ctrl_req(udi_nic_ctrl_cb_t *cb)
230 {
231 }
232 void ne2k_nd_ctrl_info_req(udi_nic_info_cb_t *cb, udi_boolean_t reset_statistics)
233 {
234 }
235 // --- IRQ
236 void ne2k_bus_irq_channel_event_ind(udi_channel_event_cb_t *cb)
237 {
238 }
239 void ne2k_bus_irq_intr_event_ind(udi_intr_event_cb_t *cb, udi_ubit8_t flags)
240 {
241         if( cb->intr_result & 0x01 )
242         {
243                 ne2k_intr__rx_ok( UDI_GCB(cb) );
244         }
245         // TODO: TX IRQs
246         udi_intr_event_rdy(cb);
247 }
248
249 // === Definition structures ===
250 udi_mgmt_ops_t  ne2k_mgmt_ops = {
251         ne2k_usage_ind,
252         ne2k_enumerate_req,
253         ne2k_devmgmt_req,
254         ne2k_final_cleanup_req
255 };
256 udi_ubit8_t     ne2k_mgmt_op_flags[4] = {0,0,0,0};
257 udi_bus_device_ops_t    ne2k_bus_dev_ops = {
258         ne2k_bus_dev_channel_event_ind,
259         ne2k_bus_dev_bus_bind_ack,
260         ne2k_bus_dev_bus_unbind_ack,
261         ne2k_bus_dev_intr_attach_ack,
262         ne2k_bus_dev_intr_detach_ack
263 };
264 udi_ubit8_t     ne2k_bus_dev_ops_flags[5] = {0};
265 udi_nd_ctrl_ops_t       ne2k_nd_ctrl_ops = {
266         ne2k_nd_ctrl_channel_event_ind,
267         ne2k_nd_ctrl_bind_req,
268         ne2k_nd_ctrl_unbind_req,
269         ne2k_nd_ctrl_enable_req,
270         ne2k_nd_ctrl_disable_req,
271         ne2k_nd_ctrl_ctrl_req,
272         ne2k_nd_ctrl_info_req
273 };
274 udi_ubit8_t     ne2k_nd_ctrl_ops_flags[7] = {0};
275 udi_nd_tx_ops_t ne2k_nd_tx_ops = {
276         ne2k_nd_tx_channel_event_ind,
277         ne2k_nd_tx_tx_req,
278         ne2k_nd_tx_exp_tx_req
279 };
280 udi_ubit8_t     ne2k_nd_tx_ops_flags[3] = {0};
281 udi_nd_rx_ops_t ne2k_nd_rx_ops = {
282         ne2k_nd_rx_channel_event_ind,
283         ne2k_nd_rx_rx_rdy
284 };
285 udi_ubit8_t     ne2k_nd_rx_ops_flags[2] = {0};
286 udi_intr_handler_ops_t  ne2k_bus_irq_ops = {
287         ne2k_bus_irq_channel_event_ind,
288         ne2k_bus_irq_intr_event_ind
289 };
290 udi_ubit8_t     ne2k_bus_irq_ops_flags[2] = {0};
291
292 udi_primary_init_t      ne2k_pri_init = {
293         .mgmt_ops = &ne2k_mgmt_ops,
294         .mgmt_op_flags = ne2k_mgmt_op_flags,
295         .mgmt_scratch_requirement = 0,
296         .enumeration_attr_list_length = 4,
297         .rdata_size = sizeof(ne2k_rdata_t),
298         .child_data_size = 0,
299         .per_parent_paths = 0
300 };
301 udi_ops_init_t  ne2k_ops_list[] = {
302         {
303                 NE2K_OPS_DEV, NE2K_META_BUS, UDI_BUS_DEVICE_OPS_NUM,
304                 0,
305                 (udi_ops_vector_t*)&ne2k_bus_dev_ops,
306                 ne2k_bus_dev_ops_flags
307         },
308         {
309                 NE2K_OPS_CTRL, NE2K_META_NIC, UDI_ND_CTRL_OPS_NUM,
310                 0,
311                 (udi_ops_vector_t*)&ne2k_nd_ctrl_ops,
312                 ne2k_nd_ctrl_ops_flags
313         },
314         {
315                 NE2K_OPS_TX, NE2K_META_NIC, UDI_ND_TX_OPS_NUM,
316                 0,
317                 (udi_ops_vector_t*)&ne2k_nd_tx_ops,
318                 ne2k_nd_tx_ops_flags
319         },
320         {
321                 NE2K_OPS_RX, NE2K_META_NIC, UDI_ND_RX_OPS_NUM,
322                 0,
323                 (udi_ops_vector_t*)&ne2k_nd_rx_ops,
324                 ne2k_nd_rx_ops_flags
325         },
326         {
327                 NE2K_OPS_IRQ, NE2K_META_BUS, UDI_BUS_INTR_HANDLER_OPS_NUM,
328                 0,
329                 (udi_ops_vector_t*)&ne2k_bus_irq_ops,
330                 ne2k_bus_irq_ops_flags
331         },
332         {0}
333 };
334 const udi_init_t        udi_init_info = {
335         .primary_init_info = &ne2k_pri_init,
336         .ops_init_list = ne2k_ops_list
337 };

UCC git Repository :: git.ucc.asn.au