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

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