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

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