Rev 4197 | Rev 4271 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4163 | mejdrech | 1 | /* |
2 | * Copyright (c) 2009 Lukas Mejdrech |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * - The name of the author may not be used to endorse or promote products |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
29 | /** @addtogroup dp8390 |
||
30 | * @{ |
||
31 | */ |
||
32 | |||
33 | /** @file |
||
34 | */ |
||
35 | |||
36 | #include <assert.h> |
||
37 | #include <ddi.h> |
||
38 | #include <errno.h> |
||
39 | #include <malloc.h> |
||
40 | //#include <stdio.h> |
||
41 | #include <ipc/ipc.h> |
||
42 | #include <ipc/services.h> |
||
43 | |||
44 | #include "../../err.h" |
||
45 | #include "../../messages.h" |
||
46 | #include "../../modules.h" |
||
47 | |||
48 | #include "../../structures/packet/packet_client.h" |
||
49 | #include "../../structures/measured_strings.h" |
||
50 | |||
4243 | mejdrech | 51 | #include "../../include/device.h" |
52 | |||
4192 | mejdrech | 53 | #include "../netif.h" |
54 | #include "../netif_interface.h" |
||
55 | |||
4163 | mejdrech | 56 | #include "dp8390.h" |
57 | #include "dp8390_drv.h" |
||
58 | #include "dp8390_module.h" |
||
59 | #include "dp8390_port.h" |
||
60 | |||
61 | #define NAME "dp8390 network interface" |
||
62 | |||
4192 | mejdrech | 63 | #define IPC_GET_DEVICE( call ) ( device_id_t ) IPC_GET_METHOD( * call ) |
4163 | mejdrech | 64 | #define IPC_GET_ISR( call ) ( int ) IPC_GET_ARG2( * call ) |
65 | |||
66 | static irq_cmd_t dp8390_cmds[] = { |
||
67 | { .cmd = CMD_PIO_READ_8, |
||
68 | .addr = NULL, |
||
69 | .dstarg = 2 |
||
70 | }, |
||
71 | { |
||
72 | .cmd = CMD_PREDICATE, |
||
73 | .value = 2, |
||
74 | .srcarg = 2 |
||
75 | }, |
||
4192 | mejdrech | 76 | { .cmd = CMD_PIO_WRITE_8, |
77 | .addr = NULL, |
||
78 | .srcarg = 2 |
||
4163 | mejdrech | 79 | }, |
80 | { |
||
81 | .cmd = CMD_ACCEPT |
||
82 | } |
||
83 | }; |
||
84 | |||
85 | static irq_code_t dp8390_code = { |
||
86 | sizeof( dp8390_cmds ) / sizeof( irq_cmd_t ), |
||
87 | dp8390_cmds |
||
88 | }; |
||
89 | |||
90 | netif_globals_t netif_globals; |
||
91 | |||
4192 | mejdrech | 92 | void netif_print_name( void ); |
4163 | mejdrech | 93 | |
4192 | mejdrech | 94 | void irq_handler( ipc_callid_t iid, ipc_call_t * call ); |
95 | void change_state( device_ref device, device_state_t state ); |
||
4163 | mejdrech | 96 | |
4243 | mejdrech | 97 | int netif_specific_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
4192 | mejdrech | 98 | return ENOTSUP; |
99 | } |
||
100 | |||
4243 | mejdrech | 101 | int netif_get_device_stats( device_id_t device_id, device_stats_ref stats ){ |
4192 | mejdrech | 102 | ERROR_DECLARE; |
103 | |||
104 | device_ref device; |
||
105 | eth_stat_t * de_stat; |
||
106 | |||
107 | if( ! stats ) return EBADMEM; |
||
108 | ERROR_PROPAGATE( find_device( device_id, & device )); |
||
109 | de_stat = & (( dpeth_t * ) device->specific )->de_stat; |
||
110 | null_device_stats( stats ); |
||
111 | stats->rx_errors = de_stat->ets_recvErr; |
||
112 | stats->tx_errors = de_stat->ets_sendErr; |
||
113 | stats->rx_crc_errors = de_stat->ets_CRCerr; |
||
114 | stats->rx_frame_errors = de_stat->ets_frameAll; |
||
115 | stats->rx_missed_errors = de_stat->ets_missedP; |
||
116 | stats->rx_packets = de_stat->ets_packetR; |
||
117 | stats->tx_packets = de_stat->ets_packetT; |
||
118 | stats->collisions = de_stat->ets_collision; |
||
119 | stats->tx_aborted_errors = de_stat->ets_transAb; |
||
120 | stats->tx_carrier_errors = de_stat->ets_carrSense; |
||
121 | stats->tx_heartbeat_errors = de_stat->ets_CDheartbeat; |
||
122 | stats->tx_window_errors = de_stat->ets_OWC; |
||
123 | return EOK; |
||
124 | } |
||
125 | |||
4163 | mejdrech | 126 | void netif_print_name( void ){ |
4197 | mejdrech | 127 | printf( "%s", NAME ); |
4163 | mejdrech | 128 | } |
129 | |||
4243 | mejdrech | 130 | int netif_get_addr_message( device_id_t device_id, measured_string_ref address ){ |
4163 | mejdrech | 131 | ERROR_DECLARE; |
132 | |||
133 | device_ref device; |
||
134 | |||
4192 | mejdrech | 135 | if( ! address ) return EBADMEM; |
136 | ERROR_PROPAGATE( find_device( device_id, & device )); |
||
137 | address->value = ( char * ) ( & (( dpeth_t * ) device->specific )->de_address ); |
||
138 | address->length = CONVERT_SIZE( ether_addr_t, char, 1 ); |
||
139 | return EOK; |
||
4163 | mejdrech | 140 | } |
141 | |||
4192 | mejdrech | 142 | void irq_handler( ipc_callid_t iid, ipc_call_t * call ) |
4163 | mejdrech | 143 | { |
144 | // int irq; |
||
145 | device_ref device; |
||
146 | dpeth_t * dep; |
||
147 | |||
4192 | mejdrech | 148 | printf( "\ndevice %d - irq %x", IPC_GET_DEVICE( call ), IPC_GET_ISR( call )); |
149 | if( find_device( IPC_GET_DEVICE( call ), & device ) != EOK ) return; |
||
150 | dep = ( dpeth_t * ) device->specific; |
||
151 | printf( "\ndev %d, irq %x\n", device->device_id, IPC_GET_ISR( call )); |
||
152 | if ( dep->de_mode != DEM_ENABLED) |
||
153 | // continue; |
||
154 | return; |
||
155 | assert( dep->de_flags & DEF_ENABLED); |
||
156 | // irq= dep.de_irq; |
||
157 | // assert(irq >= 0 && irq < NR_IRQ_VECTORS); |
||
158 | // if ( dep->de_int_pending || 1) |
||
159 | // { |
||
160 | dep->de_int_pending= 0; |
||
161 | dp_check_ints( dep ); |
||
162 | // do_int(dep); |
||
163 | /* r= sys_irqenable(&dep->de_hook); |
||
164 | if (r != OK) |
||
4163 | mejdrech | 165 | { |
4192 | mejdrech | 166 | panic("DP8390", |
167 | "unable enable interrupts", r); |
||
168 | } |
||
169 | */// } |
||
4243 | mejdrech | 170 | ipc_answer_0( iid, EOK ); |
4163 | mejdrech | 171 | } |
172 | |||
4243 | mejdrech | 173 | int netif_probe_auto_message( void ){ |
4163 | mejdrech | 174 | return ENOTSUP; |
175 | } |
||
176 | |||
4243 | mejdrech | 177 | int netif_probe_message( device_id_t device_id, int irq, int io ){ |
4163 | mejdrech | 178 | ERROR_DECLARE; |
179 | |||
4192 | mejdrech | 180 | device_ref device; |
181 | dpeth_t * dep; |
||
4163 | mejdrech | 182 | |
183 | printf( "\n" ); |
||
184 | device = ( device_ref ) malloc( sizeof( device_t )); |
||
185 | if( ! device ) return ENOMEM; |
||
4192 | mejdrech | 186 | dep = ( dpeth_t * ) malloc( sizeof( dpeth_t )); |
187 | if( ! dep ){ |
||
4163 | mejdrech | 188 | free( device ); |
189 | return ENOMEM; |
||
190 | } |
||
191 | bzero( device, sizeof( device_t )); |
||
4192 | mejdrech | 192 | bzero( dep, sizeof( dpeth_t )); |
4163 | mejdrech | 193 | device->device_id = device_id; |
194 | device->nil_phone = -1; |
||
4192 | mejdrech | 195 | device->specific = ( void * ) dep; |
196 | device->state = NETIF_STOPPED; |
||
197 | dep->parent = device; |
||
198 | dep->de_irq = irq; |
||
199 | dep->de_mode = DEM_DISABLED; |
||
4163 | mejdrech | 200 | //TODO address? |
4192 | mejdrech | 201 | if( ERROR_OCCURRED( pio_enable(( void * ) io, DP8390_IO_SIZE, ( void ** ) & dep->de_base_port )) |
202 | || ERROR_OCCURRED( do_probe( dep ))){ |
||
203 | free( dep ); |
||
4163 | mejdrech | 204 | free( device ); |
205 | return ERROR_CODE; |
||
206 | } |
||
207 | if( ERROR_OCCURRED( device_map_add( & netif_globals.device_map, device->device_id, device ))){ |
||
4192 | mejdrech | 208 | free( dep ); |
4163 | mejdrech | 209 | free( device ); |
210 | return ERROR_CODE; |
||
211 | } |
||
212 | return EOK; |
||
213 | } |
||
214 | |||
4243 | mejdrech | 215 | int netif_send_message( device_id_t device_id, packet_t packet ){ |
4192 | mejdrech | 216 | ERROR_DECLARE; |
217 | |||
218 | device_ref device; |
||
219 | dpeth_t * dep; |
||
220 | packet_t next; |
||
221 | |||
4243 | mejdrech | 222 | uint8_t * data; |
223 | data = packet_get_data( packet ); |
||
224 | printf( "\nSending packet:\n\tid\t= %d\n\tlength\t= %d\n\tdata\t= %.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX\n\t\t%.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX", packet_get_id( packet ), packet_get_data_length( packet ), data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ], data[ 6 ], data[ 7 ], data[ 8 ], data[ 9 ], data[ 10 ], data[ 11 ], data[ 12 ], data[ 13 ], data[ 14 ], data[ 15 ], data[ 16 ], data[ 17 ], data[ 18 ], data[ 19 ], data[ 20 ], data[ 21 ], data[ 22 ], data[ 23 ], data[ 24 ], data[ 25 ], data[ 26 ], data[ 27 ], data[ 28 ], data[ 29 ], data[ 30 ], data[ 31 ], data[ 32 ], data[ 33 ], data[ 34 ], data[ 35 ], data[ 36 ], data[ 37 ], data[ 38 ], data[ 39 ], data[ 40 ], data[ 41 ], data[ 42 ], data[ 43 ], data[ 44 ], data[ 45 ], data[ 46 ], data[ 47 ], data[ 48 ], data[ 49 ], data[ 50 ], data[ 51 ], data[ 52 ], data[ 53 ], data[ 54 ], data[ 55 ], data[ 56 ], data[ 57 ], data[ 58 ], data[ 59 ] ); |
||
225 | |||
4197 | mejdrech | 226 | ERROR_PROPAGATE( find_device( device_id, & device )); |
4192 | mejdrech | 227 | dep = ( dpeth_t * ) device->specific; |
228 | // process packet queue |
||
229 | do{ |
||
230 | next = pq_detach( packet ); |
||
231 | if( do_pwrite( dep, packet, FALSE ) != EBUSY ){ |
||
232 | netif_pq_release( packet_get_id( packet )); |
||
233 | } |
||
234 | packet = next; |
||
235 | }while( packet ); |
||
236 | return EOK; |
||
4163 | mejdrech | 237 | } |
238 | |||
4243 | mejdrech | 239 | int netif_start_message( device_id_t device_id ){ |
4163 | mejdrech | 240 | ERROR_DECLARE; |
241 | |||
242 | device_ref device; |
||
243 | dpeth_t * dep; |
||
244 | |||
245 | ERROR_PROPAGATE( find_device( device_id, & device )); |
||
246 | if( device->state != NETIF_ACTIVE ){ |
||
4192 | mejdrech | 247 | dep = ( dpeth_t * ) device->specific; |
4163 | mejdrech | 248 | dp8390_cmds[ 0 ].addr = ( void * ) ( uint32_t ) ( dep->de_dp8390_port + DP_ISR ); |
4192 | mejdrech | 249 | dp8390_cmds[ 2 ].addr = dp8390_cmds[ 0 ].addr; |
250 | ERROR_PROPAGATE( ipc_register_irq( dep->de_irq, device->device_id, device->device_id, & dp8390_code )); |
||
4163 | mejdrech | 251 | if( ERROR_OCCURRED( do_init( dep, DL_BROAD_REQ ))){ |
252 | ipc_unregister_irq( dep->de_irq, device->device_id ); |
||
253 | return ERROR_CODE; |
||
254 | } |
||
4192 | mejdrech | 255 | change_state( device, NETIF_ACTIVE ); |
4163 | mejdrech | 256 | } |
257 | return EOK; |
||
258 | } |
||
259 | |||
4243 | mejdrech | 260 | int netif_stop_message( device_id_t device_id ){ |
4163 | mejdrech | 261 | ERROR_DECLARE; |
262 | |||
263 | device_ref device; |
||
264 | dpeth_t * dep; |
||
265 | |||
266 | ERROR_PROPAGATE( find_device( device_id, & device )); |
||
267 | if( device->state != NETIF_STOPPED ){ |
||
4192 | mejdrech | 268 | dep = ( dpeth_t * ) device->specific; |
4163 | mejdrech | 269 | do_stop( dep ); |
270 | ipc_unregister_irq( dep->de_irq, device->device_id ); |
||
4192 | mejdrech | 271 | change_state( device, NETIF_STOPPED ); |
4163 | mejdrech | 272 | } |
273 | return EOK; |
||
274 | } |
||
275 | |||
4192 | mejdrech | 276 | void change_state( device_ref device, device_state_t state ){ |
277 | device->state = state; |
||
278 | nil_message( device, NET_NIL_DEVICE_STATE, device->state, NULL ); |
||
279 | printf( "\nState changed to %s", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" ); |
||
280 | } |
||
281 | |||
4243 | mejdrech | 282 | int netif_initialize( void ){ |
4163 | mejdrech | 283 | ipcarg_t phonehash; |
284 | |||
285 | async_set_interrupt_received( irq_handler ); |
||
286 | |||
287 | return REGISTER_ME( SERVICE_DP8390, & phonehash ); |
||
288 | } |
||
289 | |||
4192 | mejdrech | 290 | int netif_send_packet( dpeth_t * dep, packet_t packet ){ |
291 | if( !( dep && dep->parent )) return EINVAL; |
||
292 | nil_message(( device_ref ) dep->parent, NET_NIL_RECEIVED, packet_get_id( packet ), 0 ); |
||
293 | return EOK; |
||
294 | } |
||
295 | |||
4163 | mejdrech | 296 | /** @} |
297 | */ |