/********************************************************************** ruby.h - $Author$ created at: Thu Jun 10 14:26:32 JST 1993 Copyright (C) 1993-2003 Yukihiro Matsumoto Copyright (C) 2000 Network Applied Communication Laboratory, Inc. Copyright (C) 2000 Information-technology Promotion Agency, Japan **********************************************************************/ #ifndef RUBY_H #define RUBY_H #if defined(__cplusplus) extern "C" { #endif #include "config.h" #include "defines.h" #ifdef HAVE_STDLIB_H # include #endif #ifdef HAVE_STRING_H # include #else # include #endif #include #include /* need to include to use these macros */ #ifndef ISPRINT #define ISASCII(c) isascii((unsigned char)(c)) #undef ISPRINT #define ISPRINT(c) (ISASCII(c) && isprint((unsigned char)(c))) #define ISSPACE(c) (ISASCII(c) && isspace((unsigned char)(c))) #define ISUPPER(c) (ISASCII(c) && isupper((unsigned char)(c))) #define ISLOWER(c) (ISASCII(c) && islower((unsigned char)(c))) #define ISALNUM(c) (ISASCII(c) && isalnum((unsigned char)(c))) #define ISALPHA(c) (ISASCII(c) && isalpha((unsigned char)(c))) #define ISDIGIT(c) (ISASCII(c) && isdigit((unsigned char)(c))) #define ISXDIGIT(c) (ISASCII(c) && isxdigit((unsigned char)(c))) #endif #define NORETURN_STYLE_NEW 1 #ifndef NORETURN # define NORETURN(x) x #endif #if defined(HAVE_ALLOCA_H) && !defined(__GNUC__) #include #endif #ifdef _AIX #pragma alloca #endif #if defined(__VMS) # pragma builtins # define alloca __alloca #endif #if SIZEOF_LONG != SIZEOF_VOIDP # error ---->> ruby requires sizeof(void*) == sizeof(long) to be compiled. <<---- #endif typedef unsigned long VALUE; typedef unsigned long ID; #ifdef __STDC__ # include #else # ifndef LONG_MAX # ifdef HAVE_LIMITS_H # include # else /* assuming 32bit(2's compliment) long */ # define LONG_MAX 2147483647 # endif # endif # ifndef LONG_MIN # define LONG_MIN (-LONG_MAX-1) # endif # ifndef CHAR_BIT # define CHAR_BIT 8 # endif #endif #if HAVE_LONG_LONG # ifndef LLONG_MAX # ifdef LONG_LONG_MAX # define LLONG_MAX LONG_LONG_MAX # else # ifdef _I64_MAX # define LLONG_MAX _I64_MAX # else /* assuming 64bit(2's complement) long long */ # define LLONG_MAX 9223372036854775807LL # endif # endif # endif # ifndef LLONG_MIN # ifdef LONG_LONG_MIN # define LLONG_MIN LONG_LONG_MIN # else # ifdef _I64_MIN # define LLONG_MIN _I64_MIN # else # define LLONG_MIN (-LLONG_MAX-1) # endif # endif # endif #endif #define FIXNUM_MAX (LONG_MAX>>1) #define FIXNUM_MIN RSHIFT((long)LONG_MIN,1) #define FIXNUM_FLAG 0x01 #define INT2FIX(i) ((VALUE)(((long)(i))<<1 | FIXNUM_FLAG)) #define LONG2FIX(i) INT2FIX(i) #define rb_fix_new(v) INT2FIX(v) VALUE rb_int2inum _((long)); #define INT2NUM(v) rb_int2inum(v) #define LONG2NUM(v) INT2NUM(v) #define rb_int_new(v) rb_int2inum(v) VALUE rb_uint2inum _((unsigned long)); #define UINT2NUM(v) rb_uint2inum(v) #define ULONG2NUM(v) UINT2NUM(v) #define rb_uint_new(v) rb_uint2inum(v) #if HAVE_LONG_LONG VALUE rb_ll2inum _((LONG_LONG)); #define LL2NUM(v) rb_ll2inum(v) VALUE rb_ull2inum _((unsigned LONG_LONG)); #define ULL2NUM(v) rb_ull2inum(v) #endif #if SIZEOF_OFF_T > SIZEOF_LONG && defined(HAVE_LONG_LONG) # define OFFT2NUM(v) LL2NUM(v) #else # define OFFT2NUM(v) INT2NUM(v) #endif #define FIX2LONG(x) RSHIFT((long)x,1) #define FIX2ULONG(x) (((unsigned long)(x))>>1) #define FIXNUM_P(f) (((long)(f))&FIXNUM_FLAG) #define POSFIXABLE(f) ((f) <= FIXNUM_MAX) #define NEGFIXABLE(f) ((f) >= FIXNUM_MIN) #define FIXABLE(f) (POSFIXABLE(f) && NEGFIXABLE(f)) #define IMMEDIATE_MASK 0x03 #define IMMEDIATE_P(x) ((VALUE)(x) & IMMEDIATE_MASK) #define SYMBOL_FLAG 0x0e #define SYMBOL_P(x) (((VALUE)(x)&0xff)==SYMBOL_FLAG) #define ID2SYM(x) ((VALUE)(((long)(x))<<8|SYMBOL_FLAG)) #define SYM2ID(x) RSHIFT((long)x,8) /* special contants - i.e. non-zero and non-fixnum constants */ #define Qfalse 0 #define Qtrue 2 #define Qnil 4 #define Qundef 6 /* undefined value for placeholder */ #define RTEST(v) (((VALUE)(v) & ~Qnil) != 0) #define NIL_P(v) ((VALUE)(v) == Qnil) #define CLASS_OF(v) rb_class_of((VALUE)(v)) #define T_NONE 0x00 #define T_NIL 0x01 #define T_OBJECT 0x02 #define T_CLASS 0x03 #define T_ICLASS 0x04 #define T_MODULE 0x05 #define T_FLOAT 0x06 #define T_STRING 0x07 #define T_REGEXP 0x08 #define T_ARRAY 0x09 #define T_FIXNUM 0x0a #define T_HASH 0x0b #define T_STRUCT 0x0c #define T_BIGNUM 0x0d #define T_FILE 0x0e #define T_TRUE 0x20 #define T_FALSE 0x21 #define T_DATA 0x22 #define T_MATCH 0x23 #define T_SYMBOL 0x24 #define T_BLKTAG 0x3b #define T_UNDEF 0x3c #define T_VARMAP 0x3d #define T_SCOPE 0x3e #define T_NODE 0x3f #define T_MASK 0x3f #define BUILTIN_TYPE(x) (((struct RBasic*)(x))->flags & T_MASK) #define TYPE(x) rb_type((VALUE)(x)) void rb_check_type _((VALUE,int)); #define Check_Type(v,t) rb_check_type((VALUE)(v),t) VALUE rb_str_to_str _((VALUE)); VALUE rb_string_value _((volatile VALUE*)); char *rb_string_value_ptr _((volatile VALUE*)); char *rb_string_value_cstr _((volatile VALUE*)); #define StringValue(v) rb_string_value(&(v)) #define StringValuePtr(v) rb_string_value_ptr(&(v)) #define StringValueCStr(v) rb_string_value_cstr(&(v)) void rb_check_safe_obj _((VALUE)); void rb_check_safe_str _((VALUE)); #define SafeStringValue(v) do {\ StringValue(v);\ rb_check_safe_obj(v);\ } while (0) /* obsolete macro - use SafeStringValue(v) */ #define Check_SafeStr(v) rb_check_safe_str((VALUE)(v)) void rb_secure _((int)); RUBY_EXTERN int ruby_safe_level; #define rb_safe_level() (ruby_safe_level) void rb_set_safe_level _((int)); void rb_secure_update _((VALUE)); long rb_num2long _((VALUE)); unsigned long rb_num2ulong _((VALUE)); #define NUM2LONG(x) (FIXNUM_P(x)?FIX2LONG(x):rb_num2long((VALUE)x)) #define NUM2ULONG(x) rb_num2ulong((VALUE)x) #if SIZEOF_INT < SIZEOF_LONG int rb_num2int _((VALUE)); #define NUM2INT(x) (FIXNUM_P(x)?FIX2INT(x):rb_num2int((VALUE)x)) int rb_fix2int _((VALUE)); #define FIX2INT(x) rb_fix2int((VALUE)x) unsigned int rb_num2uint _((VALUE)); #define NUM2UINT(x) rb_num2uint(x) unsigned int rb_fix2uint _((VALUE)); #define FIX2UINT(x) rb_fix2uint(x) #else #define NUM2INT(x) ((int)NUM2LONG(x)) #define NUM2UINT(x) ((unsigned int)NUM2ULONG(x)) #define FIX2INT(x) ((int)FIX2LONG(x)) #define FIX2UINT(x) ((unsigned int)FIX2ULONG(x)) #endif #if HAVE_LONG_LONG LONG_LONG rb_num2ll _((VALUE)); unsigned LONG_LONG rb_num2ull _((VALUE)); # define NUM2LL(x) (FIXNUM_P(x)?FIX2LONG(x):rb_num2ll((VALUE)x)) #endif #if HAVE_LONG_LONG && SIZEOF_OFF_T > SIZEOF_LONG # define NUM2OFFT(x) ((off_t)NUM2LL(x)) #else # define NUM2OFFT(x) NUM2LONG(x) #endif double rb_num2dbl _((VALUE)); #define NUM2DBL(x) rb_num2dbl((VALUE)(x)) /* obsolete API - use StringValue() */ char *rb_str2cstr _((VALUE,long*)); /* obsolete API - use StringValuePtr() */ #define STR2CSTR(x) rb_str2cstr((VALUE)(x),0) #define NUM2CHR(x) (((TYPE(x) == T_STRING)&&(RSTRING(x)->len>=1))?\ RSTRING(x)->ptr[0]:(char)(NUM2INT(x)&0xff)) #define CHR2FIX(x) INT2FIX((long)((x)&0xff)) VALUE rb_newobj _((void)); #define NEWOBJ(obj,type) type *obj = (type*)rb_newobj() #define OBJSETUP(obj,c,t) do {\ RBASIC(obj)->flags = (t);\ RBASIC(obj)->klass = (c);\ if (rb_safe_level() >= 3) FL_SET(obj, FL_TAINT);\ } while (0) #define CLONESETUP(clone,obj) do {\ OBJSETUP(clone,rb_singleton_class_clone((VALUE)obj),RBASIC(obj)->flags);\ rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone);\ if (FL_TEST(obj, FL_EXIVAR)) rb_copy_generic_ivar((VALUE)clone,(VALUE)obj);\ } while (0) #define DUPSETUP(dup,obj) do {\ OBJSETUP(dup,rb_obj_class(obj),(RBASIC(obj)->flags)&(T_MASK|FL_EXIVAR|FL_TAINT));\ if (FL_TEST(obj, FL_EXIVAR)) rb_copy_generic_ivar((VALUE)dup,(VALUE)obj);\ } while (0) struct RBasic { unsigned long flags; VALUE klass; }; struct RObject { struct RBasic basic; struct st_table *iv_tbl; }; struct RClass { struct RBasic basic; struct st_table *iv_tbl; struct st_table *m_tbl; VALUE super; }; struct RFloat { struct RBasic basic; double value; }; #define ELTS_SHARED FL_USER2 struct RString { struct RBasic basic; long len; char *ptr; union { long capa; VALUE shared; } aux; }; struct RArray { struct RBasic basic; long len; union { long capa; VALUE shared; } aux; VALUE *ptr; }; struct RRegexp { struct RBasic basic; struct re_pattern_buffer *ptr; long len; char *str; }; struct RHash { struct RBasic basic; struct st_table *tbl; int iter_lev; VALUE ifnone; }; struct RFile { struct RBasic basic; struct OpenFile *fptr; }; struct RData { struct RBasic basic; void (*dmark) _((void*)); void (*dfree) _((void*)); void *data; }; #define DATA_PTR(dta) (RDATA(dta)->data) /* #define RUBY_DATA_FUNC(func) ((void (*)_((void*)))func) */ typedef void (*RUBY_DATA_FUNC) _((void*)); VALUE rb_data_object_alloc _((VALUE,void*,RUBY_DATA_FUNC,RUBY_DATA_FUNC)); #define Data_Wrap_Struct(klass,mark,free,sval)\ rb_data_object_alloc(klass,sval,(RUBY_DATA_FUNC)mark,(RUBY_DATA_FUNC)free) #define Data_Make_Struct(klass,type,mark,free,sval) (\ sval = ALLOC(type),\ memset(sval, 0, sizeof(type)),\ Data_Wrap_Struct(klass,mark,free,sval)\ ) #define Data_Get_Struct(obj,type,sval) do {\ Check_Type(obj, T_DATA); \ sval = (type*)DATA_PTR(obj);\ } while (0) struct RStruct { struct RBasic basic; long len; VALUE *ptr; }; struct RBignum { struct RBasic basic; char sign; long len; void *digits; }; #define R_CAST(st) (struct st*) #define RBASIC(obj) (R_CAST(RBasic)(obj)) #define ROBJECT(obj) (R_CAST(RObject)(obj)) #define RCLASS(obj) (R_CAST(RClass)(obj)) #define RMODULE(obj) RCLASS(obj) #define RFLOAT(obj) (R_CAST(RFloat)(obj)) #define RSTRING(obj) (R_CAST(RString)(obj)) #define RREGEXP(obj) (R_CAST(RRegexp)(obj)) #define RARRAY(obj) (R_CAST(RArray)(obj)) #define RHASH(obj) (R_CAST(RHash)(obj)) #define RDATA(obj) (R_CAST(RData)(obj)) #define RSTRUCT(obj) (R_CAST(RStruct)(obj)) #define RBIGNUM(obj) (R_CAST(RBignum)(obj)) #define RFILE(obj) (R_CAST(RFile)(obj)) #define FL_SINGLETON FL_USER0 #define FL_MARK (1<<6) #define FL_FINALIZE (1<<7) #define FL_TAINT (1<<8) #define FL_EXIVAR (1<<9) #define FL_FREEZE (1<<10) #define FL_USHIFT 11 #define FL_USER0 (1<<(FL_USHIFT+0)) #define FL_USER1 (1<<(FL_USHIFT+1)) #define FL_USER2 (1<<(FL_USHIFT+2)) #define FL_USER3 (1<<(FL_USHIFT+3)) #define FL_USER4 (1<<(FL_USHIFT+4)) #define FL_USER5 (1<<(FL_USHIFT+5)) #define FL_USER6 (1<<(FL_USHIFT+6)) #define FL_USER7 (1<<(FL_USHIFT+7)) #define FL_UMASK (0xff<flags&(f)):0) #define FL_SET(x,f) do {if (FL_ABLE(x)) RBASIC(x)->flags |= (f);} while (0) #define FL_UNSET(x,f) do {if (FL_ABLE(x)) RBASIC(x)->flags &= ~(f);} while (0) #define FL_REVERSE(x,f) do {if (FL_ABLE(x)) RBASIC(x)->flags ^= (f);} while (0) #define OBJ_TAINTED(x) FL_TEST((x), FL_TAINT) #define OBJ_TAINT(x) FL_SET((x), FL_TAINT) #define OBJ_INFECT(x,s) do {if (FL_ABLE(x) && FL_ABLE(s)) RBASIC(x)->flags |= RBASIC(s)->flags & FL_TAINT;} while (0) #define OBJ_FROZEN(x) FL_TEST((x), FL_FREEZE) #define OBJ_FREEZE(x) FL_SET((x), FL_FREEZE) #define ALLOC_N(type,n) (type*)xmalloc(sizeof(type)*(n)) #define ALLOC(type) (type*)xmalloc(sizeof(type)) #define REALLOC_N(var,type,n) (var)=(type*)xrealloc((char*)(var),sizeof(type)*(n)) #define ALLOCA_N(type,n) (type*)alloca(sizeof(type)*(n)) #define MEMZERO(p,type,n) memset((p), 0, sizeof(type)*(n)) #define MEMCPY(p1,p2,type,n) memcpy((p1), (p2), sizeof(type)*(n)) #define MEMMOVE(p1,p2,type,n) memmove((p1), (p2), sizeof(type)*(n)) #define MEMCMP(p1,p2,type,n) memcmp((p1), (p2), sizeof(type)*(n)) void rb_obj_infect _((VALUE,VALUE)); void rb_glob _((char*,void(*)(const char*,VALUE),VALUE)); void rb_globi _((char*,void(*)(const char*,VALUE),VALUE)); VALUE rb_define_class _((const char*,VALUE)); VALUE rb_define_module _((const char*)); VALUE rb_define_class_under _((VALUE, const char*, VALUE)); VALUE rb_define_module_under _((VALUE, const char*)); void rb_include_module _((VALUE,VALUE)); void rb_extend_object _((VALUE,VALUE)); void rb_define_variable _((const char*,VALUE*)); void rb_define_virtual_variable _((const char*,VALUE(*)(ANYARGS),void(*)(ANYARGS))); void rb_define_hooked_variable _((const char*,VALUE*,VALUE(*)(ANYARGS),void(*)(ANYARGS))); void rb_define_readonly_variable _((const char*,VALUE*)); void rb_define_const _((VALUE,const char*,VALUE)); void rb_define_global_const _((const char*,VALUE)); #define RUBY_METHOD_FUNC(func) ((VALUE (*)(ANYARGS))func) void rb_define_method _((VALUE,const char*,VALUE(*)(ANYARGS),int)); void rb_define_module_function _((VALUE,const char*,VALUE(*)(ANYARGS),int)); void rb_define_global_function _((const char*,VALUE(*)(ANYARGS),int)); void rb_undef_method _((VALUE,const char*)); void rb_define_alias _((VALUE,const char*,const char*)); void rb_define_attr _((VALUE,const char*,int,int)); void rb_global_variable _((VALUE*)); void rb_gc_register_address _((VALUE*)); void rb_gc_unregister_address _((VALUE*)); ID rb_intern _((const char*)); char *rb_id2name _((ID)); ID rb_to_id _((VALUE)); char *rb_class2name _((VALUE)); char *rb_obj_classname _((VALUE)); void rb_p _((VALUE)); VALUE rb_eval_string _((const char*)); VALUE rb_eval_string_protect _((const char*, int*)); VALUE rb_eval_string_wrap _((const char*, int*)); VALUE rb_funcall __((VALUE, ID, int, ...)); VALUE rb_funcall2 _((VALUE, ID, int, const VALUE*)); VALUE rb_funcall3 _((VALUE, ID, int, const VALUE*)); int rb_scan_args __((int, const VALUE*, const char*, ...)); VALUE rb_call_super _((int, const VALUE*)); VALUE rb_gv_set _((const char*, VALUE)); VALUE rb_gv_get _((const char*)); VALUE rb_iv_get _((VALUE, const char*)); VALUE rb_iv_set _((VALUE, const char*, VALUE)); VALUE rb_equal _((VALUE,VALUE)); RUBY_EXTERN VALUE ruby_verbose, ruby_debug; NORETURN(void rb_raise __((VALUE, const char*, ...))); NORETURN(void rb_fatal __((const char*, ...))); NORETURN(void rb_bug __((const char*, ...))); NORETURN(void rb_sys_fail _((const char*))); NORETURN(void rb_iter_break _((void))); NORETURN(void rb_exit _((int))); NORETURN(void rb_notimplement _((void))); void rb_warning __((const char*, ...)); /* reports if `-w' specified */ void rb_sys_warning __((const char*, ...)); /* reports if `-w' specified */ void rb_warn __((const char*, ...)); /* reports always */ VALUE rb_each _((VALUE)); VALUE rb_yield _((VALUE)); VALUE rb_yield_values __((int n, ...)); VALUE rb_yield_splat _((VALUE)); int rb_block_given_p _((void)); VALUE rb_iterate _((VALUE(*)(VALUE),VALUE,VALUE(*)(ANYARGS),VALUE)); VALUE rb_rescue _((VALUE(*)(ANYARGS),VALUE,VALUE(*)(ANYARGS),VALUE)); VALUE rb_rescue2 __((VALUE(*)(ANYARGS),VALUE,VALUE(*)(ANYARGS),VALUE,...)); VALUE rb_ensure _((VALUE(*)(ANYARGS),VALUE,VALUE(*)(ANYARGS),VALUE)); VALUE rb_catch _((const char*,VALUE(*)(ANYARGS),VALUE)); NORETURN(void rb_throw _((const char*,VALUE))); VALUE rb_require _((const char*)); void ruby_init _((void)); void ruby_options _((int, char**)); void ruby_run _((void)); RUBY_EXTERN VALUE rb_mKernel; RUBY_EXTERN VALUE rb_mComparable; RUBY_EXTERN VALUE rb_mEnumerable; RUBY_EXTERN VALUE rb_mPrecision; RUBY_EXTERN VALUE rb_mErrno; RUBY_EXTERN VALUE rb_mFileTest; RUBY_EXTERN VALUE rb_mGC; RUBY_EXTERN VALUE rb_mMath; RUBY_EXTERN VALUE rb_mProcess; RUBY_EXTERN VALUE rb_cObject; RUBY_EXTERN VALUE rb_cArray; RUBY_EXTERN VALUE rb_cBignum; RUBY_EXTERN VALUE rb_cClass; RUBY_EXTERN VALUE rb_cDir; RUBY_EXTERN VALUE rb_cData; RUBY_EXTERN VALUE rb_cFalseClass; RUBY_EXTERN VALUE rb_cFile; RUBY_EXTERN VALUE rb_cFixnum; RUBY_EXTERN VALUE rb_cFloat; RUBY_EXTERN VALUE rb_cHash; RUBY_EXTERN VALUE rb_cInteger; RUBY_EXTERN VALUE rb_cIO; RUBY_EXTERN VALUE rb_cModule; RUBY_EXTERN VALUE rb_cNilClass; RUBY_EXTERN VALUE rb_cNumeric; RUBY_EXTERN VALUE rb_cProc; RUBY_EXTERN VALUE rb_cRange; RUBY_EXTERN VALUE rb_cRegexp; RUBY_EXTERN VALUE rb_cString; RUBY_EXTERN VALUE rb_cSymbol; RUBY_EXTERN VALUE rb_cThread; RUBY_EXTERN VALUE rb_cTime; RUBY_EXTERN VALUE rb_cTrueClass; RUBY_EXTERN VALUE rb_cStruct; RUBY_EXTERN VALUE rb_eException; RUBY_EXTERN VALUE rb_eStandardError; RUBY_EXTERN VALUE rb_eSystemExit; RUBY_EXTERN VALUE rb_eInterrupt; RUBY_EXTERN VALUE rb_eSignal; RUBY_EXTERN VALUE rb_eFatal; RUBY_EXTERN VALUE rb_eArgError; RUBY_EXTERN VALUE rb_eEOFError; RUBY_EXTERN VALUE rb_eIndexError; RUBY_EXTERN VALUE rb_eRangeError; RUBY_EXTERN VALUE rb_eIOError; RUBY_EXTERN VALUE rb_eRuntimeError; RUBY_EXTERN VALUE rb_eSecurityError; RUBY_EXTERN VALUE rb_eSystemCallError; RUBY_EXTERN VALUE rb_eTypeError; RUBY_EXTERN VALUE rb_eZeroDivError; RUBY_EXTERN VALUE rb_eNotImpError; RUBY_EXTERN VALUE rb_eNoMemError; RUBY_EXTERN VALUE rb_eNoMethodError; RUBY_EXTERN VALUE rb_eFloatDomainError; RUBY_EXTERN VALUE rb_eScriptError; RUBY_EXTERN VALUE rb_eNameError; RUBY_EXTERN VALUE rb_eSyntaxError; RUBY_EXTERN VALUE rb_eLoadError; RUBY_EXTERN VALUE rb_stdin, rb_stdout, rb_stderr; RUBY_EXTERN VALUE ruby_errinfo; static inline VALUE #if defined(HAVE_PROTOTYPES) rb_class_of(VALUE obj) #else rb_class_of(obj) VALUE obj; #endif { if (FIXNUM_P(obj)) return rb_cFixnum; if (obj == Qnil) return rb_cNilClass; if (obj == Qfalse) return rb_cFalseClass; if (obj == Qtrue) return rb_cTrueClass; if (SYMBOL_P(obj)) return rb_cSymbol; return RBASIC(obj)->klass; } static inline int #if defined(HAVE_PROTOTYPES) rb_type(VALUE obj) #else rb_type(obj) VALUE obj; #endif { if (FIXNUM_P(obj)) return T_FIXNUM; if (obj == Qnil) return T_NIL; if (obj == Qfalse) return T_FALSE; if (obj == Qtrue) return T_TRUE; if (obj == Qundef) return T_UNDEF; if (SYMBOL_P(obj)) return T_SYMBOL; return BUILTIN_TYPE(obj); } static inline int #if defined(HAVE_PROTOTYPES) rb_special_const_p(VALUE obj) #else rb_special_const_p(obj) VALUE obj; #endif { if (SPECIAL_CONST_P(obj)) return Qtrue; return Qfalse; } #include "intern.h" #include "missing.h" #if defined(EXTLIB) && defined(USE_DLN_A_OUT) /* hook for external modules */ static char *dln_libs_to_be_linked[] = { EXTLIB, 0 }; #endif #if defined(__cplusplus) } /* extern "C" { */ #endif #endif /* ifndef RUBY_H */ ='#n451'>451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
/*
Ported to U-Boot by Christian Pellegrin <chri@ascensit.com>

Based on sources from the Linux kernel (pcnet_cs.c, 8390.h) and
eCOS(if_dp83902a.c, if_dp83902a.h). Both of these 2 wonderful world
are GPL, so this is, of course, GPL.

==========================================================================

dev/if_dp83902a.c

Ethernet device driver for NS DP83902a ethernet controller

==========================================================================
####ECOSGPLCOPYRIGHTBEGIN####
-------------------------------------------
This file is part of eCos, the Embedded Configurable Operating System.
Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.

eCos is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 or (at your option) any later version.

eCos is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with eCos; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

As a special exception, if other files instantiate templates or use macros
or inline functions from this file, or you compile this file and link it
with other works to produce a work based on this file, this file does not
by itself cause the resulting work to be covered by the GNU General Public
License. However the source code for this file must still be made available
in accordance with section (3) of the GNU General Public License.

This exception does not invalidate any other reasons why a work based on
this file might be covered by the GNU General Public License.

Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
at http://sources.redhat.com/ecos/ecos-license/
-------------------------------------------
####ECOSGPLCOPYRIGHTEND####
####BSDCOPYRIGHTBEGIN####

-------------------------------------------

Portions of this software may have been derived from OpenBSD or other sources,
and are covered by the appropriate copyright disclaimers included herein.

-------------------------------------------

####BSDCOPYRIGHTEND####
==========================================================================
#####DESCRIPTIONBEGIN####

Author(s):	gthomas
Contributors:	gthomas, jskov, rsandifo
Date:		2001-06-13
Purpose:
Description:

FIXME:		Will fail if pinged with large packets (1520 bytes)
Add promisc config
Add SNMP

####DESCRIPTIONEND####

==========================================================================
*/

#include <common.h>
#include <command.h>
#include <env.h>
#include <net.h>
#include <malloc.h>
#include <linux/compiler.h>

/* forward definition of function used for the uboot interface */
void uboot_push_packet_len(int len);
void uboot_push_tx_done(int key, int val);

/* NE2000 base header file */
#include "ne2000_base.h"

#if defined(CONFIG_DRIVER_AX88796L)
/* AX88796L support */
#include "ax88796.h"
#else
/* Basic NE2000 chip support */
#include "ne2000.h"
#endif

static dp83902a_priv_data_t nic; /* just one instance of the card supported */

/**
 * This function reads the MAC address from the serial EEPROM,
 * used if PROM read fails. Does nothing for ax88796 chips (sh boards)
 */
static bool
dp83902a_init(unsigned char *enetaddr)
{
	dp83902a_priv_data_t *dp = &nic;
	u8* base;
#if defined(NE2000_BASIC_INIT)
	int i;
#endif

	DEBUG_FUNCTION();

	base = dp->base;
	if (!base)
		return false;	/* No device found */

	DEBUG_LINE();

#if defined(NE2000_BASIC_INIT)
	/* AX88796L doesn't need */
	/* Prepare ESA */
	DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE1);	/* Select page 1 */
	/* Use the address from the serial EEPROM */
	for (i = 0; i < 6; i++)
		DP_IN(base, DP_P1_PAR0+i, dp->esa[i]);
	DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE0);	/* Select page 0 */

	printf("NE2000 - %s ESA: %02x:%02x:%02x:%02x:%02x:%02x\n",
		"eeprom",
		dp->esa[0],
		dp->esa[1],
		dp->esa[2],
		dp->esa[3],
		dp->esa[4],
		dp->esa[5] );

	memcpy(enetaddr, dp->esa, 6); /* Use MAC from serial EEPROM */
#endif	/* NE2000_BASIC_INIT */
	return true;
}

static void
dp83902a_stop(void)
{
	dp83902a_priv_data_t *dp = &nic;
	u8 *base = dp->base;

	DEBUG_FUNCTION();

	DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_STOP);	/* Brutal */
	DP_OUT(base, DP_ISR, 0xFF);		/* Clear any pending interrupts */
	DP_OUT(base, DP_IMR, 0x00);		/* Disable all interrupts */

	dp->running = false;
}

/*
 * This function is called to "start up" the interface. It may be called
 * multiple times, even when the hardware is already running. It will be
 * called whenever something "hardware oriented" changes and should leave
 * the hardware ready to send/receive packets.
 */
static void
dp83902a_start(u8 * enaddr)
{
	dp83902a_priv_data_t *dp = &nic;
	u8 *base = dp->base;
	int i;

	debug("The MAC is %pM\n", enaddr);

	DEBUG_FUNCTION();

	DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_STOP); /* Brutal */
	DP_OUT(base, DP_DCR, DP_DCR_INIT);
	DP_OUT(base, DP_RBCH, 0);		/* Remote byte count */
	DP_OUT(base, DP_RBCL, 0);
	DP_OUT(base, DP_RCR, DP_RCR_MON);	/* Accept no packets */
	DP_OUT(base, DP_TCR, DP_TCR_LOCAL);	/* Transmitter [virtually] off */
	DP_OUT(base, DP_TPSR, dp->tx_buf1);	/* Transmitter start page */
	dp->tx1 = dp->tx2 = 0;
	dp->tx_next = dp->tx_buf1;
	dp->tx_started = false;
	dp->running = true;
	DP_OUT(base, DP_PSTART, dp->rx_buf_start); /* Receive ring start page */
	DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1); /* Receive ring boundary */
	DP_OUT(base, DP_PSTOP, dp->rx_buf_end);	/* Receive ring end page */
	dp->rx_next = dp->rx_buf_start - 1;
	dp->running = true;
	DP_OUT(base, DP_ISR, 0xFF);		/* Clear any pending interrupts */
	DP_OUT(base, DP_IMR, DP_IMR_All);	/* Enable all interrupts */
	DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE1 | DP_CR_STOP);	/* Select page 1 */
	DP_OUT(base, DP_P1_CURP, dp->rx_buf_start);	/* Current page - next free page for Rx */
	dp->running = true;
	for (i = 0; i < ETHER_ADDR_LEN; i++) {
		/* FIXME */
		/*((vu_short*)( base + ((DP_P1_PAR0 + i) * 2) +
		 * 0x1400)) = enaddr[i];*/
		DP_OUT(base, DP_P1_PAR0+i, enaddr[i]);
	}
	/* Enable and start device */
	DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
	DP_OUT(base, DP_TCR, DP_TCR_NORMAL); /* Normal transmit operations */
	DP_OUT(base, DP_RCR, DP_RCR_AB); /* Accept broadcast, no errors, no multicast */
	dp->running = true;
}

/*
 * This routine is called to start the transmitter. It is split out from the
 * data handling routine so it may be called either when data becomes first
 * available or when an Tx interrupt occurs
 */

static void
dp83902a_start_xmit(int start_page, int len)
{
	dp83902a_priv_data_t *dp = (dp83902a_priv_data_t *) &nic;
	u8 *base = dp->base;

	DEBUG_FUNCTION();

#if DEBUG & 1
	printf("Tx pkt %d len %d\n", start_page, len);
	if (dp->tx_started)
		printf("TX already started?!?\n");
#endif

	DP_OUT(base, DP_ISR, (DP_ISR_TxP | DP_ISR_TxE));
	DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
	DP_OUT(base, DP_TBCL, len & 0xFF);
	DP_OUT(base, DP_TBCH, len >> 8);
	DP_OUT(base, DP_TPSR, start_page);
	DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_TXPKT | DP_CR_START);

	dp->tx_started = true;
}

/*
 * This routine is called to send data to the hardware. It is known a-priori
 * that there is free buffer space (dp->tx_next).
 */
static void
dp83902a_send(u8 *data, int total_len, u32 key)
{
	struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
	u8 *base = dp->base;
	int len, start_page, pkt_len, i, isr;
#if DEBUG & 4
	int dx;
#endif

	DEBUG_FUNCTION();

	len = pkt_len = total_len;
	if (pkt_len < IEEE_8023_MIN_FRAME)
		pkt_len = IEEE_8023_MIN_FRAME;

	start_page = dp->tx_next;
	if (dp->tx_next == dp->tx_buf1) {
		dp->tx1 = start_page;
		dp->tx1_len = pkt_len;
		dp->tx1_key = key;
		dp->tx_next = dp->tx_buf2;
	} else {
		dp->tx2 = start_page;
		dp->tx2_len = pkt_len;
		dp->tx2_key = key;
		dp->tx_next = dp->tx_buf1;
	}

#if DEBUG & 5
	printf("TX prep page %d len %d\n", start_page, pkt_len);
#endif

	DP_OUT(base, DP_ISR, DP_ISR_RDC);	/* Clear end of DMA */
	{
		/*
		 * Dummy read. The manual sez something slightly different,
		 * but the code is extended a bit to do what Hitachi's monitor
		 * does (i.e., also read data).
		 */

		__maybe_unused u16 tmp;
		int len = 1;

		DP_OUT(base, DP_RSAL, 0x100 - len);
		DP_OUT(base, DP_RSAH, (start_page - 1) & 0xff);
		DP_OUT(base, DP_RBCL, len);
		DP_OUT(base, DP_RBCH, 0);
		DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_RDMA | DP_CR_START);
		DP_IN_DATA(dp->data, tmp);
	}

#ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_TX_DMA
	/*
	 * Stall for a bit before continuing to work around random data
	 * corruption problems on some platforms.
	 */
	CYGACC_CALL_IF_DELAY_US(1);
#endif

	/* Send data to device buffer(s) */
	DP_OUT(base, DP_RSAL, 0);
	DP_OUT(base, DP_RSAH, start_page);
	DP_OUT(base, DP_RBCL, pkt_len & 0xFF);
	DP_OUT(base, DP_RBCH, pkt_len >> 8);
	DP_OUT(base, DP_CR, DP_CR_WDMA | DP_CR_START);

	/* Put data into buffer */
#if DEBUG & 4
	printf(" sg buf %08lx len %08x\n ", (u32)data, len);
	dx = 0;
#endif
	while (len > 0) {
#if DEBUG & 4
		printf(" %02x", *data);
		if (0 == (++dx % 16)) printf("\n ");
#endif

		DP_OUT_DATA(dp->data, *data++);
		len--;
	}
#if DEBUG & 4
	printf("\n");
#endif
	if (total_len < pkt_len) {
#if DEBUG & 4
		printf("  + %d bytes of padding\n", pkt_len - total_len);
#endif
		/* Padding to 802.3 length was required */
		for (i = total_len; i < pkt_len;) {
			i++;
			DP_OUT_DATA(dp->data, 0);
		}
	}

#ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_TX_DMA
	/*
	 * After last data write, delay for a bit before accessing the
	 * device again, or we may get random data corruption in the last
	 * datum (on some platforms).
	 */
	CYGACC_CALL_IF_DELAY_US(1);
#endif

	/* Wait for DMA to complete */
	do {
		DP_IN(base, DP_ISR, isr);
	} while ((isr & DP_ISR_RDC) == 0);

	/* Then disable DMA */
	DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);

	/* Start transmit if not already going */
	if (!dp->tx_started) {
		if (start_page == dp->tx1) {
			dp->tx_int = 1; /* Expecting interrupt from BUF1 */
		} else {
			dp->tx_int = 2; /* Expecting interrupt from BUF2 */
		}
		dp83902a_start_xmit(start_page, pkt_len);
	}
}

/*
 * This function is called when a packet has been received. It's job is
 * to prepare to unload the packet from the hardware. Once the length of
 * the packet is known, the upper layer of the driver can be told. When
 * the upper layer is ready to unload the packet, the internal function
 * 'dp83902a_recv' will be called to actually fetch it from the hardware.
 */
static void
dp83902a_RxEvent(void)
{
	struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
	u8 *base = dp->base;
	__maybe_unused u8 rsr;
	u8 rcv_hdr[4];
	int i, len, pkt, cur;

	DEBUG_FUNCTION();

	DP_IN(base, DP_RSR, rsr);
	while (true) {
		/* Read incoming packet header */
		DP_OUT(base, DP_CR, DP_CR_PAGE1 | DP_CR_NODMA | DP_CR_START);
		DP_IN(base, DP_P1_CURP, cur);
		DP_OUT(base, DP_P1_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
		DP_IN(base, DP_BNDRY, pkt);

		pkt += 1;
		if (pkt == dp->rx_buf_end)
			pkt = dp->rx_buf_start;

		if (pkt == cur) {
			break;
		}
		DP_OUT(base, DP_RBCL, sizeof(rcv_hdr));
		DP_OUT(base, DP_RBCH, 0);
		DP_OUT(base, DP_RSAL, 0);
		DP_OUT(base, DP_RSAH, pkt);
		if (dp->rx_next == pkt) {
			if (cur == dp->rx_buf_start)
				DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1);
			else
				DP_OUT(base, DP_BNDRY, cur - 1); /* Update pointer */
			return;
		}
		dp->rx_next = pkt;
		DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */
		DP_OUT(base, DP_CR, DP_CR_RDMA | DP_CR_START);
#ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_RX_DMA
		CYGACC_CALL_IF_DELAY_US(10);
#endif

		/* read header (get data size)*/
		for (i = 0; i < sizeof(rcv_hdr);) {
			DP_IN_DATA(dp->data, rcv_hdr[i++]);
		}

#if DEBUG & 5
		printf("rx hdr %02x %02x %02x %02x\n",
			rcv_hdr[0], rcv_hdr[1], rcv_hdr[2], rcv_hdr[3]);
#endif
		len = ((rcv_hdr[3] << 8) | rcv_hdr[2]) - sizeof(rcv_hdr);

		/* data read */
		uboot_push_packet_len(len);

		if (rcv_hdr[1] == dp->rx_buf_start)
			DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1);
		else
			DP_OUT(base, DP_BNDRY, rcv_hdr[1] - 1); /* Update pointer */
	}
}

/*
 * This function is called as a result of the "eth_drv_recv()" call above.
 * It's job is to actually fetch data for a packet from the hardware once
 * memory buffers have been allocated for the packet. Note that the buffers
 * may come in pieces, using a scatter-gather list. This allows for more
 * efficient processing in the upper layers of the stack.
 */
static void
dp83902a_recv(u8 *data, int len)
{
	struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
	u8 *base = dp->base;
	int i, mlen;
	u8 saved_char = 0;
	bool saved;
#if DEBUG & 4
	int dx;
#endif

	DEBUG_FUNCTION();

#if DEBUG & 5
	printf("Rx packet %d length %d\n", dp->rx_next, len);
#endif

	/* Read incoming packet data */
	DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
	DP_OUT(base, DP_RBCL, len & 0xFF);
	DP_OUT(base, DP_RBCH, len >> 8);
	DP_OUT(base, DP_RSAL, 4);		/* Past header */
	DP_OUT(base, DP_RSAH, dp->rx_next);
	DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */
	DP_OUT(base, DP_CR, DP_CR_RDMA | DP_CR_START);
#ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_RX_DMA
	CYGACC_CALL_IF_DELAY_US(10);
#endif

	saved = false;
	for (i = 0; i < 1; i++) {
		if (data) {
			mlen = len;
#if DEBUG & 4
			printf(" sg buf %08lx len %08x \n", (u32) data, mlen);
			dx = 0;
#endif
			while (0 < mlen) {
				/* Saved byte from previous loop? */
				if (saved) {
					*data++ = saved_char;
					mlen--;
					saved = false;
					continue;
				}

				{
					u8 tmp;
					DP_IN_DATA(dp->data, tmp);
#if DEBUG & 4
					printf(" %02x", tmp);
					if (0 == (++dx % 16)) printf("\n ");
#endif
					*data++ = tmp;
					mlen--;
				}
			}
#if DEBUG & 4
			printf("\n");
#endif
		}
	}
}

static void
dp83902a_TxEvent(void)
{
	struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
	u8 *base = dp->base;
	__maybe_unused u8 tsr;
	u32 key;

	DEBUG_FUNCTION();

	DP_IN(base, DP_TSR, tsr);
	if (dp->tx_int == 1) {
		key = dp->tx1_key;
		dp->tx1 = 0;
	} else {
		key = dp->tx2_key;
		dp->tx2 = 0;
	}
	/* Start next packet if one is ready */
	dp->tx_started = false;
	if (dp->tx1) {
		dp83902a_start_xmit(dp->tx1, dp->tx1_len);
		dp->tx_int = 1;
	} else if (dp->tx2) {
		dp83902a_start_xmit(dp->tx2, dp->tx2_len);
		dp->tx_int = 2;
	} else {
		dp->tx_int = 0;
	}
	/* Tell higher level we sent this packet */
	uboot_push_tx_done(key, 0);
}

/*
 * Read the tally counters to clear them. Called in response to a CNT
 * interrupt.
 */
static void
dp83902a_ClearCounters(void)
{
	struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
	u8 *base = dp->base;
	__maybe_unused u8 cnt1, cnt2, cnt3;

	DP_IN(base, DP_FER, cnt1);
	DP_IN(base, DP_CER, cnt2);
	DP_IN(base, DP_MISSED, cnt3);
	DP_OUT(base, DP_ISR, DP_ISR_CNT);
}

/*
 * Deal with an overflow condition. This code follows the procedure set
 * out in section 7.0 of the datasheet.
 */
static void
dp83902a_Overflow(void)
{
	struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *)&nic;
	u8 *base = dp->base;
	u8 isr;

	/* Issue a stop command and wait 1.6ms for it to complete. */
	DP_OUT(base, DP_CR, DP_CR_STOP | DP_CR_NODMA);
	CYGACC_CALL_IF_DELAY_US(1600);

	/* Clear the remote byte counter registers. */
	DP_OUT(base, DP_RBCL, 0);
	DP_OUT(base, DP_RBCH, 0);

	/* Enter loopback mode while we clear the buffer. */
	DP_OUT(base, DP_TCR, DP_TCR_LOCAL);
	DP_OUT(base, DP_CR, DP_CR_START | DP_CR_NODMA);

	/*
	 * Read in as many packets as we can and acknowledge any and receive
	 * interrupts. Since the buffer has overflowed, a receive event of
	 * some kind will have occurred.
	 */
	dp83902a_RxEvent();
	DP_OUT(base, DP_ISR, DP_ISR_RxP|DP_ISR_RxE);

	/* Clear the overflow condition and leave loopback mode. */
	DP_OUT(base, DP_ISR, DP_ISR_OFLW);
	DP_OUT(base, DP_TCR, DP_TCR_NORMAL);

	/*
	 * If a transmit command was issued, but no transmit event has occurred,
	 * restart it here.
	 */
	DP_IN(base, DP_ISR, isr);
	if (dp->tx_started && !(isr & (DP_ISR_TxP|DP_ISR_TxE))) {
		DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_TXPKT | DP_CR_START);
	}
}

static void
dp83902a_poll(void)
{
	struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
	u8 *base = dp->base;
	u8 isr;

	DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE0 | DP_CR_START);
	DP_IN(base, DP_ISR, isr);
	while (0 != isr) {
		/*
		 * The CNT interrupt triggers when the MSB of one of the error
		 * counters is set. We don't much care about these counters, but
		 * we should read their values to reset them.
		 */
		if (isr & DP_ISR_CNT) {
			dp83902a_ClearCounters();
		}
		/*
		 * Check for overflow. It's a special case, since there's a
		 * particular procedure that must be followed to get back into
		 * a running state.a
		 */
		if (isr & DP_ISR_OFLW) {
			dp83902a_Overflow();
		} else {
			/*
			 * Other kinds of interrupts can be acknowledged simply by
			 * clearing the relevant bits of the ISR. Do that now, then
			 * handle the interrupts we care about.
			 */
			DP_OUT(base, DP_ISR, isr);	/* Clear set bits */
			if (!dp->running) break;	/* Is this necessary? */
			/*
			 * Check for tx_started on TX event since these may happen
			 * spuriously it seems.
			 */
			if (isr & (DP_ISR_TxP|DP_ISR_TxE) && dp->tx_started) {
				dp83902a_TxEvent();
			}
			if (isr & (DP_ISR_RxP|DP_ISR_RxE)) {
				dp83902a_RxEvent();
			}
		}
		DP_IN(base, DP_ISR, isr);
	}
}


/* U-Boot specific routines */
static u8 *pbuf = NULL;

static int pkey = -1;
static int initialized = 0;

void uboot_push_packet_len(int len) {
	PRINTK("pushed len = %d\n", len);
	if (len >= 2000) {
		printf("NE2000: packet too big\n");
		return;
	}
	dp83902a_recv(&pbuf[0], len);

	/*Just pass it to the upper layer*/
	net_process_received_packet(&pbuf[0], len);
}

void uboot_push_tx_done(int key, int val) {
	PRINTK("pushed key = %d\n", key);
	pkey = key;
}

/**
 * Setup the driver and init MAC address according to doc/README.enetaddr
 * Called by ne2k_register() before registering the driver @eth layer
 *
 * @param struct ethdevice of this instance of the driver for dev->enetaddr
 * @return 0 on success, -1 on error (causing caller to print error msg)
 */
static int ne2k_setup_driver(struct eth_device *dev)
{
	PRINTK("### ne2k_setup_driver\n");

	if (!pbuf) {
		pbuf = malloc(2000);
		if (!pbuf) {
			printf("Cannot allocate rx buffer\n");
			return -1;
		}
	}

	nic.base = (u8 *) CONFIG_DRIVER_NE2000_BASE;

	nic.data = nic.base + DP_DATA;
	nic.tx_buf1 = START_PG;
	nic.tx_buf2 = START_PG2;
	nic.rx_buf_start = RX_START;
	nic.rx_buf_end = RX_END;

	/*
	 * According to doc/README.enetaddr, drivers shall give priority
	 * to the MAC address value in the environment, so we do not read
	 * it from the prom or eeprom if it is specified in the environment.
	 */
	if (!eth_env_get_enetaddr("ethaddr", dev->enetaddr)) {
		/* If the MAC address is not in the environment, get it: */
		if (!get_prom(dev->enetaddr, nic.base)) /* get MAC from prom */
			dp83902a_init(dev->enetaddr);   /* fallback: seeprom */
		/* And write it into the environment otherwise eth_write_hwaddr
		 * returns -1 due to eth_env_get_enetaddr_by_index() failing,
		 * and this causes "Warning: failed to set MAC address", and
		 * cmd_bdinfo has no ethaddr value which it can show: */
		eth_env_set_enetaddr("ethaddr", dev->enetaddr);
	}
	return 0;
}

static int ne2k_init(struct eth_device *dev, bd_t *bd)
{
	dp83902a_start(dev->enetaddr);
	initialized = 1;
	return 0;
}

static void ne2k_halt(struct eth_device *dev)
{
	debug("### ne2k_halt\n");
	if(initialized)
		dp83902a_stop();
	initialized = 0;
}

static int ne2k_recv(struct eth_device *dev)
{
	dp83902a_poll();
	return 1;
}

static int ne2k_send(struct eth_device *dev, void *packet, int length)
{
	int tmo;

	debug("### ne2k_send\n");

	pkey = -1;

	dp83902a_send((u8 *) packet, length, 666);
	tmo = get_timer (0) + TOUT * CONFIG_SYS_HZ;
	while(1) {
		dp83902a_poll();
		if (pkey != -1) {
			PRINTK("Packet sucesfully sent\n");
			return 0;
		}
		if (get_timer (0) >= tmo) {
			printf("transmission error (timoeut)\n");
			return 0;
		}

	}
	return 0;
}

/**
 * Setup the driver for use and register it with the eth layer
 * @return 0 on success, -1 on error (causing caller to print error msg)
 */
int ne2k_register(void)
{
	struct eth_device *dev;

	dev = calloc(sizeof(*dev), 1);
	if (dev == NULL)
		return -1;

	if (ne2k_setup_driver(dev))
		return -1;

	dev->init = ne2k_init;
	dev->halt = ne2k_halt;
	dev->send = ne2k_send;
	dev->recv = ne2k_recv;

	strcpy(dev->name, "NE2000");

	return eth_register(dev);
}