Subversion Repositories HelenOS

Rev

Rev 4163 | Rev 4192 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

/*
 * 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 <async.h>
#include <ddi.h>
#include <errno.h>
#include <malloc.h>
#include <mem.h>
//#include <stdio.h>
//#include <sysinfo.h>
#include <ipc/ipc.h>
#include <ipc/services.h>
//#include <sys/types.h>

#include "../../err.h"
#include "../../messages.h"
#include "../../modules.h"

#include "../../structures/packet/packet_client.h"
#include "../../structures/measured_strings.h"

#include "dp8390.h"
#include "dp8390_drv.h"
#include "dp8390_module.h"
#include "dp8390_port.h"
//#include "local.h"
#include "../device.h"
#include "../netif.h"

//TODO sync stats

#define NAME    "dp8390 network interface"

#define IPC_GET_DEVICE( call )          ( device_id_t ) IPC_GET_ARG1( * 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
    },
    {//TODO remember device_id
        .cmd = CMD_ACCEPT,
        .value = 0,
        .dstarg = 1,
    },
    {
        .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 );
int initialize( void );
int probe_auto_message( void );
int probe_message( device_id_t device_id, int irq, int io );
int send_message( device_id_t device_id, packet_t packet );
int start_message( device_id_t device_id );
int stop_message( device_id_t device_id );
measured_string_ref get_addr_message( device_id_t device_id );

static void irq_handler( ipc_callid_t iid, ipc_call_t * call );

void netif_print_name( void ){
    printf( NAME );
}

measured_string_ref get_addr_message( device_id_t device_id ){
    ERROR_DECLARE;

    device_ref  device;

    if( ERROR_OCCURRED( find_device( device_id, & device ))) return NULL;
    return (( dp_device_ref )( device->specific ))->addr;
}

static void irq_handler( ipc_callid_t iid, ipc_call_t * call )
{
//  int irq;
    device_ref  device;
    dpeth_t *   dep;

    if( find_device( IPC_GET_DEVICE( call ), & device ) == EOK ){
        dep = & (( dp_device_ref ) device->specific )->dep;
        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;
    dp_device_ref   dp_device;

    printf( "\n" );
    device = ( device_ref ) malloc( sizeof( device_t ));
    if( ! device ) return ENOMEM;
    dp_device = ( dp_device_ref ) malloc( sizeof( dp_device_t ));
    if( ! dp_device ){
        free( device );
        return ENOMEM;
    }
    bzero( device, sizeof( device_t ));
    bzero( dp_device, sizeof( dp_device_t ));
    device->device_id = device_id;
    device->nil_phone = -1;
    device->specific = ( void * ) dp_device;
    device->state = NETIF_ACTIVE;
    dp_device->dep.de_irq = irq;
//  dp_device->dep.de_base_port = io;
    dp_device->dep.de_mode = DEM_DISABLED;
    //TODO address?
    if( ERROR_OCCURRED( pio_enable(( void * ) io, DP8390_IO_SIZE, ( void ** ) & dp_device->dep.de_base_port ))
    || ERROR_OCCURRED( do_probe( & dp_device->dep ))){
        free( dp_device );
        free( device );
        return ERROR_CODE;
    }
    dp_device->addr = measured_string_create_bulk(( char * ) & dp_device->dep.de_address, CONVERT_SIZE( ether_addr_t, char, 1 ));
    if( ! dp_device->addr ){
        free( dp_device );
        free( device );
        return ENOMEM;
    }
    if( ERROR_OCCURRED( device_map_add( & netif_globals.device_map, device->device_id, device ))){
        free( dp_device->addr );
        free( dp_device );
        free( device );
        return ERROR_CODE;
    }
    return EOK;
}

int send_message( device_id_t device_id, packet_t packet ){
    // TODO send message
    return ENOTSUP;
}

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 = & (( dp_device_ref ) device->specific )->dep;
        dp8390_cmds[ 0 ].addr = ( void * ) ( uint32_t ) ( dep->de_dp8390_port + DP_ISR );
        dp8390_cmds[ 2 ].value = device->device_id;
        ERROR_PROPAGATE( ipc_register_irq( dep->de_irq, device->device_id, 0, & dp8390_code ));
        if( ERROR_OCCURRED( do_init( dep, DL_BROAD_REQ ))){
            ipc_unregister_irq( dep->de_irq, device->device_id );
            return ERROR_CODE;
        }
        device->state = NETIF_ACTIVE;
        nil_message( device, NET_NIL_DEVICE_STATE, device->state, NULL );
    }
    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 = & (( dp_device_ref ) device->specific )->dep;
        do_stop( dep );
        ipc_unregister_irq( dep->de_irq, device->device_id );
        device->state = NETIF_STOPPED;
        nil_message( device, NET_NIL_DEVICE_STATE, device->state, NULL );
    }
    return EOK;
}

int initialize( void ){
    ipcarg_t    phonehash;

    async_set_interrupt_received( irq_handler );

    return REGISTER_ME( SERVICE_DP8390, & phonehash );
}

/** @}
 */