/*
* Copyright (c) 2009 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup dp8390
* @{
*/
/** @file
*/
#include <assert.h>
#include <ddi.h>
#include <errno.h>
#include <malloc.h>
//#include <stdio.h>
#include <ipc/ipc.h>
#include <ipc/services.h>
#include "../../err.h"
#include "../../messages.h"
#include "../../modules.h"
#include "../../structures/packet/packet_client.h"
#include "../../structures/measured_strings.h"
#include "../device.h"
#include "../netif.h"
#include "../netif_interface.h"
#include "dp8390.h"
#include "dp8390_drv.h"
#include "dp8390_module.h"
#include "dp8390_port.h"
#define NAME "dp8390 network interface"
#define IPC_GET_DEVICE( call ) ( device_id_t ) IPC_GET_METHOD( * call )
#define IPC_GET_ISR( call ) ( int ) IPC_GET_ARG2( * call )
static irq_cmd_t dp8390_cmds[] = {
{ .cmd = CMD_PIO_READ_8,
.addr = NULL,
.dstarg = 2
},
{
.cmd = CMD_PREDICATE,
.value = 2,
.srcarg = 2
},
{ .cmd = CMD_PIO_WRITE_8,
.addr = NULL,
.srcarg = 2
},
{
.cmd = CMD_ACCEPT
}
};
static irq_code_t dp8390_code = {
sizeof( dp8390_cmds ) / sizeof( irq_cmd_t ),
dp8390_cmds
};
netif_globals_t netif_globals;
void netif_print_name( void );
void irq_handler( ipc_callid_t iid, ipc_call_t * call );
void change_state( device_ref device, device_state_t state );
int specific_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
return ENOTSUP;
}
int get_device_stats( device_id_t device_id, device_stats_ref stats ){
ERROR_DECLARE;
device_ref device;
eth_stat_t * de_stat;
if( ! stats ) return EBADMEM;
ERROR_PROPAGATE( find_device( device_id, & device ));
de_stat = & (( dpeth_t * ) device->specific )->de_stat;
null_device_stats( stats );
stats->rx_errors = de_stat->ets_recvErr;
stats->tx_errors = de_stat->ets_sendErr;
stats->rx_crc_errors = de_stat->ets_CRCerr;
stats->rx_frame_errors = de_stat->ets_frameAll;
stats->rx_missed_errors = de_stat->ets_missedP;
stats->rx_packets = de_stat->ets_packetR;
stats->tx_packets = de_stat->ets_packetT;
stats->collisions = de_stat->ets_collision;
stats->tx_aborted_errors = de_stat->ets_transAb;
stats->tx_carrier_errors = de_stat->ets_carrSense;
stats->tx_heartbeat_errors = de_stat->ets_CDheartbeat;
stats->tx_window_errors = de_stat->ets_OWC;
return EOK;
}
void netif_print_name( void ){
}
int get_addr_message( device_id_t device_id, measured_string_ref address ){
ERROR_DECLARE;
device_ref device;
if( ! address ) return EBADMEM;
ERROR_PROPAGATE( find_device( device_id, & device ));
address->value = ( char * ) ( & (( dpeth_t * ) device->specific )->de_address );
address->length = CONVERT_SIZE( ether_addr_t, char, 1 );
return EOK;
}
void irq_handler( ipc_callid_t iid, ipc_call_t * call )
{
// int irq;
device_ref device;
dpeth_t * dep;
printf( "\ndevice %d - irq %x", IPC_GET_DEVICE
( call
), IPC_GET_ISR
( call
));
if( find_device( IPC_GET_DEVICE( call ), & device ) != EOK ) return;
dep = ( dpeth_t * ) device->specific;
printf( "\ndev %d, irq %x\n", device
->device_id
, IPC_GET_ISR
( call
));
if ( dep->de_mode != DEM_ENABLED)
// continue;
return;
assert( dep
->de_flags
& DEF_ENABLED
);
// irq= dep.de_irq;
// assert(irq >= 0 && irq < NR_IRQ_VECTORS);
// if ( dep->de_int_pending || 1)
// {
dep->de_int_pending= 0;
dp_check_ints( dep );
// do_int(dep);
/* r= sys_irqenable(&dep->de_hook);
if (r != OK)
{
panic("DP8390",
"unable enable interrupts", r);
}
*/// }
}
int probe_auto_message( void ){
return ENOTSUP;
}
int probe_message( device_id_t device_id, int irq, int io ){
ERROR_DECLARE;
device_ref device;
dpeth_t * dep;
device
= ( device_ref
) malloc( sizeof( device_t
));
if( ! device ) return ENOMEM;
dep
= ( dpeth_t
* ) malloc( sizeof( dpeth_t
));
if( ! dep ){
return ENOMEM;
}
bzero( device, sizeof( device_t ));
bzero( dep, sizeof( dpeth_t ));
device->device_id = device_id;
device->nil_phone = -1;
device->specific = ( void * ) dep;
device->state = NETIF_STOPPED;
dep->parent = device;
dep->de_irq = irq;
dep->de_mode = DEM_DISABLED;
//TODO address?
if( ERROR_OCCURRED( pio_enable(( void * ) io, DP8390_IO_SIZE, ( void ** ) & dep->de_base_port ))
|| ERROR_OCCURRED( do_probe( dep ))){
return ERROR_CODE;
}
if( ERROR_OCCURRED( device_map_add( & netif_globals.device_map, device->device_id, device ))){
return ERROR_CODE;
}
return EOK;
}
int send_message( device_id_t device_id, packet_t packet ){
ERROR_DECLARE;
device_ref device;
dpeth_t * dep;
packet_t next;
ERROR_PROPAGATE( find_device( IPC_GET_DEVICE( call ), & device ));
dep = ( dpeth_t * ) device->specific;
// process packet queue
do{
next = pq_detach( packet );
if( do_pwrite( dep, packet, FALSE ) != EBUSY ){
netif_pq_release( packet_get_id( packet ));
}
packet = next;
}while( packet );
return EOK;
}
int start_message( device_id_t device_id ){
ERROR_DECLARE;
device_ref device;
dpeth_t * dep;
ERROR_PROPAGATE( find_device( device_id, & device ));
if( device->state != NETIF_ACTIVE ){
dep = ( dpeth_t * ) device->specific;
dp8390_cmds[ 0 ].addr = ( void * ) ( uint32_t ) ( dep->de_dp8390_port + DP_ISR );
dp8390_cmds[ 2 ].addr = dp8390_cmds[ 0 ].addr;
ERROR_PROPAGATE( ipc_register_irq( dep->de_irq, device->device_id, device->device_id, & dp8390_code ));
if( ERROR_OCCURRED( do_init( dep, DL_BROAD_REQ ))){
ipc_unregister_irq( dep->de_irq, device->device_id );
return ERROR_CODE;
}
change_state( device, NETIF_ACTIVE );
}
return EOK;
}
int stop_message( device_id_t device_id ){
ERROR_DECLARE;
device_ref device;
dpeth_t * dep;
ERROR_PROPAGATE( find_device( device_id, & device ));
if( device->state != NETIF_STOPPED ){
dep = ( dpeth_t * ) device->specific;
do_stop( dep );
ipc_unregister_irq( dep->de_irq, device->device_id );
change_state( device, NETIF_STOPPED );
}
return EOK;
}
void change_state( device_ref device, device_state_t state ){
device->state = state;
nil_message( device, NET_NIL_DEVICE_STATE, device->state, NULL );
printf( "\nState changed to %s", ( state
== NETIF_ACTIVE
) ? "ACTIVE" : "STOPPED" );
}
int initialize( void ){
ipcarg_t phonehash;
async_set_interrupt_received( irq_handler );
return REGISTER_ME( SERVICE_DP8390, & phonehash );
}
int netif_send_packet( dpeth_t * dep, packet_t packet ){
if( !( dep && dep->parent )) return EINVAL;
nil_message(( device_ref ) dep->parent, NET_NIL_RECEIVED, packet_get_id( packet ), 0 );
return EOK;
}
/** @}
*/