Subversion Repositories Kolibri OS

Rev

Rev 1473 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2010. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  ETHERNET.INC                                                   ;;
  7. ;;                                                                 ;;
  8. ;;  Ethernet network layer for KolibriOS                           ;;
  9. ;;                                                                 ;;
  10. ;;    Written by hidnplayr@kolibrios.org                           ;;
  11. ;;                                                                 ;;
  12. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  13. ;;             Version 2, June 1991                                ;;
  14. ;;                                                                 ;;
  15. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  16.  
  17. $Revision: 1514 $
  18.  
  19. ETH_QUEUE_SIZE          equ 16
  20.  
  21. struct  ETH_FRAME
  22.         .DstMAC         dp  ?  ; destination MAC-address
  23.         .SrcMAC         dp  ?  ; source MAC-address
  24.         .Type           dw  ?  ; type of the upper-layer protocol
  25.         .Data:                 ; data (46-1500 bytes for a normal packet)
  26. ends
  27.  
  28. virtual at NET_DEVICE.end
  29.  
  30.         ETH_DEVICE:
  31.         .unload         dd ?
  32.         .reset          dd ?
  33.         .transmit       dd ?
  34.         .set_MAC        dd ?
  35.         .get_MAC        dd ?
  36.         .set_mode       dd ?
  37.         .get_mode       dd ?
  38.  
  39.         .bytes_tx       dq ?
  40.         .bytes_rx       dq ?
  41.         .packets_tx     dd ?
  42.         .packets_rx     dd ?
  43.         .mode           dd ?
  44.         .name           dd ?
  45.         .mac            dp ?
  46.  
  47. end virtual
  48.  
  49. align 4
  50. iglobal
  51.  
  52.         ETH_BROADCAST   dp  0xffffffffffff
  53. endg
  54.  
  55. align 4
  56. uglobal
  57.         ETH_RUNNING     dd  ?
  58. endg
  59.  
  60.  
  61. ;-----------------------------------------------------------------
  62. ;
  63. ; ETH_init
  64. ;
  65. ;  This function resets all ethernet variables
  66. ;
  67. ;  IN:  /
  68. ;  OUT: /
  69. ;
  70. ;-----------------------------------------------------------------
  71. align 4
  72. ETH_init:
  73.  
  74.         mov     [ETH_RUNNING], 0
  75.  
  76.         ret
  77.  
  78.  
  79. ;-----------------------------------------------------------------
  80. ;
  81. ; ETH_Receiver:
  82. ;
  83. ;  This function is called by ethernet drivers,
  84. ;  It pushes the received ethernet packets onto the eth_in_queue
  85. ;
  86. ;  IN:   [esp]  = Pointer to buffer
  87. ;       [esp-4] = size of buffer
  88. ;         ebx   = pointer to eth_device
  89. ;  OUT: /
  90. ;
  91. ;-----------------------------------------------------------------
  92. align 4
  93. ETH_receiver:
  94.         mov     eax, [esp]
  95.         mov     ecx, [esp+4]
  96.  
  97.         DEBUGF  1,"ETH_Handler - size: %u\n", ecx
  98.         cmp     ecx, 60    ; check packet length
  99.         jl      .dump
  100.         sub     ecx, ETH_FRAME.Data
  101.  
  102.         lea     edx, [eax + ETH_FRAME.Data]
  103.         mov     ax , [eax + ETH_FRAME.Type]
  104.  
  105.         cmp     ax, ETHER_IPv4
  106.         je      IPv4_handler
  107.  
  108.         cmp     ax, ETHER_ARP
  109.         je      ARP_handler
  110.  
  111.         DEBUGF  2,"Unknown ethernet packet type %x\n", ax
  112.  
  113.   .dump:
  114.         DEBUGF  2,"ETH_Handler - dumping\n"
  115.         call    kernel_free
  116.         add     esp, 4
  117.         ret
  118.  
  119. ;-----------------------------------------------------------------
  120. ;
  121. ; ETH_create_packet
  122. ;
  123. ; IN: eax = pointer to source mac
  124. ;     ebx = pointer to destination mac
  125. ;     ecx = packet size
  126. ;     edx = device number
  127. ;      di = protocol
  128. ;
  129. ; OUT: edi = 0 on error, pointer to buffer otherwise
  130. ;      eax = buffer start
  131. ;      ebx = to device structure
  132. ;      ecx = unchanged (packet size of embedded data)
  133. ;      edx = size of complete buffer
  134. ;
  135. ;-----------------------------------------------------------------
  136. align 4
  137. ETH_create_packet:
  138.  
  139.         DEBUGF  1,"Creating Ethernet Packet (size=%u): \n", ecx
  140.  
  141.         cmp     ecx, 1500 ;;;
  142.         jg      .exit
  143.  
  144.         push    ecx di eax ebx edx
  145.  
  146.         add     ecx, ETH_FRAME.Data
  147.         push    ecx
  148.         push    ecx
  149.         call    kernel_alloc
  150.         mov     edi, eax
  151.         test    edi, edi
  152.         jz      .pop_exit
  153.  
  154.         pop     ecx
  155.         pop     edx
  156.  
  157.         pop     esi
  158.         movsd
  159.         movsw
  160.         pop     esi
  161.         movsd
  162.         movsw
  163.         pop     ax
  164.         stosw
  165.  
  166.         lea     eax, [edi - ETH_FRAME.Data]  ; Set eax to buffer start
  167.         mov     edx, ecx                     ; Set ebx to complete buffer size
  168.         pop     ecx
  169.  
  170.         xor     ebx, ebx                        ;;;; TODO: Fixme
  171.         mov     ebx, [NET_DRV_LIST + ebx]
  172.  
  173.         cmp     edx, 46 + ETH_FRAME.Data    ; If data size is less then 46, add padding bytes
  174.         jg      .continue
  175.         mov     edx, 46 + ETH_FRAME.Data
  176.        .continue:
  177.  
  178.         DEBUGF  1,"done: %x size:%u device:%x\n", eax, edx, ebx
  179.         ret
  180.  
  181.   .pop_exit:
  182.         DEBUGF  2,"Out of ram space!!\n"
  183.         add     esp, 18
  184.         and     edi, 0
  185.         ret
  186.  
  187.   .exit:
  188.         DEBUGF  2,"Packet too large!\n"
  189.         and     edi, 0
  190.         ret
  191.  
  192.  
  193.  
  194. ;-----------------------------------------------------------------
  195. ;
  196. ; ETH_API
  197. ;
  198. ; This function is called by system function 75
  199. ;
  200. ; IN:  subfunction number in bl
  201. ;      device number in bh
  202. ;      ecx, edx, .. depends on subfunction
  203. ;
  204. ; OUT:
  205. ;
  206. ;-----------------------------------------------------------------
  207. align 4
  208. ETH_API:
  209.  
  210.         cmp     bh, MAX_NET_DEVICES
  211.         jg      .error
  212.         movzx   eax, bh
  213.         shl     eax, 2
  214.  
  215.         cmp     bl, 7
  216.         jz      .out_queue
  217.         cmp     bl, 6
  218.         jz      .in_queue
  219.  
  220.         mov     eax, dword [NET_DRV_LIST + eax]
  221.         cmp     [eax + NET_DEVICE.type], NET_TYPE_ETH
  222.         jne     .error
  223.  
  224.         test    bl, bl
  225.         jz      .packets_tx     ; 0
  226.         dec     bl
  227.         jz      .packets_rx     ; 1
  228.         dec     bl
  229.         jz      .bytes_tx       ; 2
  230.         dec     bl
  231.         jz      .bytes_rx       ; 3
  232.         dec     bl
  233.         jz      .read_mac       ; 4
  234.         dec     bl
  235.         jz      .write_mac      ; 5
  236.  
  237.   .error:
  238.         DEBUGF  2,"Device is not ethernet type\n"
  239.         or      eax, -1
  240.         ret
  241.  
  242. .packets_tx:
  243.         mov     eax, dword [eax + ETH_DEVICE.packets_tx]
  244.  
  245.         ret
  246.  
  247. .packets_rx:
  248.         mov     eax, dword [eax + ETH_DEVICE.packets_rx]
  249.         ret
  250.  
  251. .bytes_tx:
  252.         mov     ebx, dword [eax + ETH_DEVICE.bytes_tx + 4]
  253.         mov     eax, dword [eax + ETH_DEVICE.bytes_tx]
  254.         mov     [esp+20+4], ebx                         ; TODO: fix this ugly code
  255.         ret
  256.  
  257. .bytes_rx:
  258.         mov     ebx, dword [eax + ETH_DEVICE.bytes_rx + 4]
  259.         mov     eax, dword [eax + ETH_DEVICE.bytes_rx]
  260.         mov     [esp+20+4], ebx                         ; TODO: fix this ugly code
  261.         ret
  262.  
  263.  
  264. .read_mac:
  265.         movzx   ebx, word [eax + ETH_DEVICE.mac]
  266.         mov     eax, dword [eax + ETH_DEVICE.mac + 2]
  267.         mov     [esp+20+4], ebx                         ; TODO: fix this ugly code
  268.         ret
  269.  
  270. .write_mac:
  271.         push    ecx
  272.         push    dx
  273.         mov     eax, [eax + ETH_DEVICE.set_MAC]
  274.         call    eax
  275.         ret
  276.  
  277. .in_queue:
  278.    if ETH_QUEUE
  279.         add     eax, ETH_IN_QUEUE
  280.         mov     eax, [eax + queue.size]
  281.    else
  282.         or      eax, -1
  283.    end if
  284.         ret
  285.  
  286. .out_queue:
  287.    if ETH_QUEUE
  288.         add     eax, ETH_OUT_QUEUE
  289.         mov     eax, [eax + queue.size]
  290.    else
  291.         or      eax, -1
  292.    end if
  293.         ret