Subversion Repositories HelenOS

Rev

Rev 4166 | Go to most recent revision | Details | 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 <async.h>
38
#include <ddi.h>
39
#include <errno.h>
40
#include <malloc.h>
41
#include <mem.h>
42
//#include <stdio.h>
43
//#include <sysinfo.h>
44
#include <ipc/ipc.h>
45
#include <ipc/services.h>
46
//#include <sys/types.h>
47
 
48
#include "../../err.h"
49
#include "../../messages.h"
50
#include "../../modules.h"
51
 
52
#include "../../structures/packet/packet_client.h"
53
#include "../../structures/measured_strings.h"
54
 
55
#include "dp8390.h"
56
#include "dp8390_drv.h"
57
#include "dp8390_module.h"
58
#include "dp8390_port.h"
59
//#include "local.h"
60
#include "../device.h"
61
#include "../netif.h"
62
 
63
//TODO sync stats
64
 
65
#define NAME    "dp8390 network interface"
66
 
67
#define IPC_GET_DEVICE( call )          ( device_id_t ) IPC_GET_ARG1( * call )
68
#define IPC_GET_ISR( call )             ( int ) IPC_GET_ARG2( * call )
69
 
70
static irq_cmd_t    dp8390_cmds[] = {
71
    {   .cmd = CMD_PIO_READ_8,
72
        .addr = NULL,
73
        .dstarg = 2
74
    },
75
    {
76
        .cmd = CMD_PREDICATE,
77
        .value = 2,
78
        .srcarg = 2
79
    },
80
    {//TODO remember device_id
81
        .cmd = CMD_ACCEPT,
82
        .value = 0,
83
        .dstarg = 1,
84
    },
85
    {
86
        .cmd = CMD_ACCEPT
87
    }
88
};
89
 
90
static irq_code_t   dp8390_code = {
91
    sizeof( dp8390_cmds ) / sizeof( irq_cmd_t ),
92
    dp8390_cmds
93
};
94
 
95
netif_globals_t netif_globals;
96
 
97
void netif_print_name( void );
98
int initialize( void );
99
int probe_auto_message( void );
100
int probe_message( device_id_t device_id, int irq, int io );
101
int send_message( device_id_t device_id, packet_t packet );
102
int start_message( device_id_t device_id );
103
int stop_message( device_id_t device_id );
104
measured_string_ref get_addr_message( device_id_t device_id );
105
 
106
static void irq_handler( ipc_callid_t iid, ipc_call_t * call );
107
 
108
void netif_print_name( void ){
109
    printf( NAME );
110
}
111
 
112
measured_string_ref get_addr_message( device_id_t device_id ){
113
    ERROR_DECLARE;
114
 
115
    device_ref  device;
116
 
117
    if( ERROR_OCCURRED( find_device( device_id, & device ))) return NULL;
118
    return (( dp_device_ref )( device->specific ))->addr;
119
}
120
 
121
static void irq_handler( ipc_callid_t iid, ipc_call_t * call )
122
{
123
//  int irq;
124
    device_ref  device;
125
    dpeth_t *   dep;
126
 
127
    if( find_device( IPC_GET_DEVICE( call ), & device ) == EOK ){
128
        dep = & (( dp_device_ref ) device->specific )->dep;
129
        printf( "\ndev %d, irq %x\n", device->device_id, IPC_GET_ISR( call ));
130
        if ( dep->de_mode != DEM_ENABLED)
131
    //      continue;
132
            return;
133
        assert( dep->de_flags & DEF_ENABLED);
134
    //  irq= dep.de_irq;
135
    //  assert(irq >= 0 && irq < NR_IRQ_VECTORS);
136
        if ( dep->de_int_pending || 1)
137
        {
138
            dep->de_int_pending= 0;
139
            dp_check_ints( dep );
140
    //      do_int(dep);
141
    /*      r= sys_irqenable(&dep->de_hook);
142
            if (r != OK)
143
            {
144
                panic("DP8390",
145
                "unable enable interrupts", r);
146
            }
147
    */  }
148
    }
149
}
150
 
151
int probe_auto_message( void ){
152
    return ENOTSUP;
153
}
154
 
155
int probe_message( device_id_t device_id, int irq, int io ){
156
    ERROR_DECLARE;
157
 
158
    device_ref      device;
159
    dp_device_ref   dp_device;
160
 
161
    printf( "\n" );
162
    device = ( device_ref ) malloc( sizeof( device_t ));
163
    if( ! device ) return ENOMEM;
164
    dp_device = ( dp_device_ref ) malloc( sizeof( dp_device_t ));
165
    if( ! dp_device ){
166
        free( device );
167
        return ENOMEM;
168
    }
169
    bzero( device, sizeof( device_t ));
170
    bzero( dp_device, sizeof( dp_device_t ));
171
    device->device_id = device_id;
172
    device->nil_phone = -1;
173
    device->specific = ( void * ) dp_device;
174
    device->state = NETIF_ACTIVE;
175
    dp_device->dep.de_irq = irq;
176
    dp_device->dep.de_base_port = io;
177
    dp_device->dep.de_mode = DEM_DISABLED;
178
    //TODO address?
179
    if( ERROR_OCCURRED( iospace_enable( task_get_id(), ( void * ) ( uint32_t ) dp_device->dep.de_base_port, DP8390_IO_SIZE ))
180
    || ERROR_OCCURRED( do_probe( & dp_device->dep ))){
181
        free( dp_device );
182
        free( device );
183
        return ERROR_CODE;
184
    }
185
    dp_device->addr = measured_string_create_bulk(( char * ) & dp_device->dep.de_address, CONVERT_SIZE( ether_addr_t, char, 1 ));
186
    if( ! dp_device->addr ){
187
        free( dp_device );
188
        free( device );
189
        return ENOMEM;
190
    }
191
    if( ERROR_OCCURRED( device_map_add( & netif_globals.device_map, device->device_id, device ))){
192
        free( dp_device->addr );
193
        free( dp_device );
194
        free( device );
195
        return ERROR_CODE;
196
    }
197
    return EOK;
198
}
199
 
200
int send_message( device_id_t device_id, packet_t packet ){
201
    // TODO send message
202
    return ENOTSUP;
203
}
204
 
205
int start_message( device_id_t device_id ){
206
    ERROR_DECLARE;
207
 
208
    device_ref  device;
209
    dpeth_t *   dep;
210
 
211
    ERROR_PROPAGATE( find_device( device_id, & device ));
212
    if( device->state != NETIF_ACTIVE ){
213
        dep = & (( dp_device_ref ) device->specific )->dep;
214
        dp8390_cmds[ 0 ].addr = ( void * ) ( uint32_t ) ( dep->de_dp8390_port + DP_ISR );
215
        dp8390_cmds[ 2 ].value = device->device_id;
216
        ERROR_PROPAGATE( ipc_register_irq( dep->de_irq, device->device_id, 0, & dp8390_code ));
217
        if( ERROR_OCCURRED( do_init( dep, DL_BROAD_REQ ))){
218
            ipc_unregister_irq( dep->de_irq, device->device_id );
219
            return ERROR_CODE;
220
        }
221
        device->state = NETIF_ACTIVE;
222
        nil_message( device, NET_NIL_DEVICE_STATE, device->state, NULL );
223
    }
224
    return EOK;
225
}
226
 
227
int stop_message( device_id_t device_id ){
228
    ERROR_DECLARE;
229
 
230
    device_ref  device;
231
    dpeth_t *   dep;
232
 
233
    ERROR_PROPAGATE( find_device( device_id, & device ));
234
    if( device->state != NETIF_STOPPED ){
235
        dep = & (( dp_device_ref ) device->specific )->dep;
236
        do_stop( dep );
237
        ipc_unregister_irq( dep->de_irq, device->device_id );
238
        device->state = NETIF_STOPPED;
239
        nil_message( device, NET_NIL_DEVICE_STATE, device->state, NULL );
240
    }
241
    return EOK;
242
}
243
 
244
int initialize( void ){
245
    ipcarg_t    phonehash;
246
 
247
    async_set_interrupt_received( irq_handler );
248
 
249
    return REGISTER_ME( SERVICE_DP8390, & phonehash );
250
}
251
 
252
/** @}
253
 */