Subversion Repositories Kolibri OS

Rev

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

  1. ; standard driver stuff
  2. format MS COFF
  3.  
  4. DEBUG = 1
  5.  
  6. ; this is for DEBUGF macro from 'fdo.inc'
  7. __DEBUG__ = 1
  8. __DEBUG_LEVEL__ = 1
  9.  
  10. include '../../proc32.inc'
  11. include '../../imports.inc'
  12. include '../../fdo.inc'
  13. include '../../struct.inc'
  14.  
  15. public START
  16. public version
  17.  
  18. ; USB constants
  19. DEVICE_DESCR_TYPE           = 1
  20. CONFIG_DESCR_TYPE           = 2
  21. STRING_DESCR_TYPE           = 3
  22. INTERFACE_DESCR_TYPE        = 4
  23. ENDPOINT_DESCR_TYPE         = 5
  24. DEVICE_QUALIFIER_DESCR_TYPE = 6
  25.  
  26. CONTROL_PIPE     = 0
  27. ISOCHRONOUS_PIPE = 1
  28. BULK_PIPE        = 2
  29. INTERRUPT_PIPE   = 3
  30.  
  31. ; USB HID constants
  32. HID_DESCR_TYPE      = 21h
  33. REPORT_DESCR_TYPE   = 22h
  34. PHYSICAL_DESCR_TYPE = 23h
  35.  
  36.  
  37. ; LibUSB constatnts
  38. LIBUSB_REQUEST_TYPE_STANDARD = (0x00 shl 5)
  39. LIBUSB_REQUEST_TYPE_CLASS = (0x01 shl 5)
  40. LIBUSB_REQUEST_TYPE_VENDOR = (0x02 shl 5)
  41. LIBUSB_REQUEST_TYPE_RESERVED = (0x03 shl 5)
  42.  
  43. LIBUSB_RECIPIENT_DEVICE = 0x00
  44. LIBUSB_RECIPIENT_INTERFACE = 0x01
  45. LIBUSB_RECIPIENT_ENDPOINT = 0x02
  46. LIBUSB_RECIPIENT_OTHER = 0x03
  47.  
  48. LIBUSB_ENDPOINT_IN = 0x80
  49. LIBUSB_ENDPOINT_OUT = 0x00
  50.  
  51. ; FTDI Constants
  52. FTDI_DEVICE_OUT_REQTYPE = (LIBUSB_REQUEST_TYPE_VENDOR or LIBUSB_RECIPIENT_DEVICE or LIBUSB_ENDPOINT_OUT)
  53. FTDI_DEVICE_IN_REQTYPE = (LIBUSB_REQUEST_TYPE_VENDOR or LIBUSB_RECIPIENT_DEVICE or LIBUSB_ENDPOINT_IN)
  54.  
  55. ; Requests
  56. ;Definitions for flow control
  57. SIO_RESET         =0  ;Reset the port
  58. SIO_MODEM_CTRL    =1  ;Set the modem control register
  59. SIO_SET_FLOW_CTRL =2  ;Set flow control register
  60. SIO_SET_BAUD_RATE =3  ;Set baud rate
  61. SIO_SET_DATA      =4  ;Set the data characteristics of the port
  62.  
  63. SIO_RESET_REQUEST            =SIO_RESET
  64. SIO_SET_BAUDRATE_REQUEST     =SIO_SET_BAUD_RATE
  65. SIO_SET_DATA_REQUEST         =SIO_SET_DATA
  66. SIO_SET_FLOW_CTRL_REQUEST    =SIO_SET_FLOW_CTRL
  67. SIO_SET_MODEM_CTRL_REQUEST   =SIO_MODEM_CTRL
  68. SIO_POLL_MODEM_STATUS_REQUEST=0x05
  69. SIO_SET_EVENT_CHAR_REQUEST   =0x06
  70. SIO_SET_ERROR_CHAR_REQUEST   =0x07
  71. SIO_SET_LATENCY_TIMER_REQUEST=0x09
  72. SIO_GET_LATENCY_TIMER_REQUEST=0x0A
  73. SIO_SET_BITMODE_REQUEST      =0x0B
  74. SIO_READ_PINS_REQUEST        =0x0C
  75. SIO_READ_EEPROM_REQUEST      =0x90
  76. SIO_WRITE_EEPROM_REQUEST     =0x91
  77. SIO_ERASE_EEPROM_REQUEST     =0x92
  78.  
  79.  
  80. SIO_RESET_SIO=0
  81. SIO_RESET_PURGE_RX=1
  82. SIO_RESET_PURGE_TX=2
  83.  
  84. SIO_DISABLE_FLOW_CTRL=0x0
  85. SIO_RTS_CTS_HS =(0x1 shl 8)
  86. SIO_DTR_DSR_HS =(0x2 shl 8)
  87. SIO_XON_XOFF_HS=(0x4 shl 8)
  88.  
  89. SIO_SET_DTR_MASK=0x1
  90. SIO_SET_DTR_HIGH=( 1 or ( SIO_SET_DTR_MASK  shl 8))
  91. SIO_SET_DTR_LOW =( 0 or ( SIO_SET_DTR_MASK  shl 8))
  92. SIO_SET_RTS_MASK=0x2
  93. SIO_SET_RTS_HIGH=( 2 or ( SIO_SET_RTS_MASK shl 8 ))
  94. SIO_SET_RTS_LOW =( 0 or ( SIO_SET_RTS_MASK shl 8 ))
  95.  
  96. SIO_RTS_CTS_HS =(0x1 shl 8)
  97.  
  98. ;strings
  99. my_driver       db      'usbother',0
  100. nomemory_msg    db      'K : no memory',13,10,0
  101.  
  102. ; Structures
  103. struct ftdi_context
  104. chipType                db      ?
  105. baudrate                dd      ?
  106. bitbangEnabled          db      ?
  107. readBufPtr              dd      ?
  108. readBufOffs             dd      ?
  109. readBufChunkSize        dd      ?
  110. writeBufChunkSize       dd      ?
  111. interface               dd      ?
  112. index                   dd      ?
  113. inEP                    dd      ?
  114. outEP                   dd      ?
  115. nullP                   dd      ?
  116. next_context            dd      ?
  117. ends
  118.  
  119. struct IOCTL
  120. handle                  dd      ?
  121. io_code                 dd      ?
  122. input                   dd      ?
  123. inp_size                dd      ?
  124. output                  dd      ?
  125. out_size                dd      ?
  126. ends
  127.  
  128. struct usb_descr
  129. bLength                 db      ?
  130. bDescriptorType         db      ?
  131. bcdUSB                  dw      ?
  132. bDeviceClass            db      ?
  133. bDeviceSubClass         db      ?
  134. bDeviceProtocol         db      ?
  135. bMaxPacketSize0         db      ?
  136. idVendor                dw      ?
  137. idProduct               dw      ?
  138. bcdDevice               dw      ?
  139. iManufacturer           db      ?
  140. iProduct                db      ?
  141. iSerialNumber           db      ?
  142. bNumConfigurations      db      ?
  143. ends
  144.  
  145. struct conf_packet
  146. bmRequestType           db      ?
  147. bRequest                db      ?
  148. wValue                  dw      ?
  149. wIndex                  dw      ?
  150. wLength                 dw      ?
  151. ends
  152.  
  153. section '.flat' code readable align 16
  154. ; The start procedure.
  155. proc START stdcall, .reason:DWORD
  156.  
  157.         xor     eax, eax        ; initialize return value
  158.         cmp     [.reason], 1    ; compare the argument
  159.         jnz     .nothing                
  160.         stdcall RegUSBDriver, my_driver, service_proc, usb_functions
  161.  
  162. .nothing:
  163.         ret
  164. endp
  165.  
  166.  
  167. proc AddDevice stdcall uses ebx, .config_pipe:DWORD, .config_descr:DWORD, .interface:DWORD
  168.        
  169.         stdcall USBGetParam, [.config_pipe], 0
  170.         DEBUGF 1,'K : Device detected Vendor: %x\n', [eax+usb_descr.idVendor]
  171.         cmp     word[eax+usb_descr.idVendor], 0x0403
  172.         jnz     .notftdi
  173.         DEBUGF 1,'K : FTDI USB device detected\n'
  174.         movi    eax, sizeof.ftdi_context
  175.         call    Kmalloc
  176.         test    eax, eax
  177.         jnz     @f
  178.         mov     esi, nomemory_msg
  179.         call    SysMsgBoardStr
  180.         xor     eax, eax
  181.         jmp     .nothing
  182. @@:
  183.         DEBUGF 1,'K : Adding struct to list %x\n', eax
  184.         call    linkedlist.add
  185.        
  186.         mov     ebx, [.config_pipe]
  187.         mov     [eax + ftdi_context.nullP], ebx
  188.        
  189.         DEBUGF 1,'K : Open first pipe\n'
  190.         mov     ebx, eax
  191.         stdcall USBOpenPipe, [.config_pipe],  0x81,  0x40,  BULK_PIPE, 0
  192.         mov     [ebx + ftdi_context.inEP], eax
  193.         DEBUGF 1,'K : Open second pipe\n'
  194.         stdcall USBOpenPipe, [.config_pipe],  0x02,  0x40,  BULK_PIPE, 0
  195.         mov     [ebx + ftdi_context.outEP], eax        
  196.  
  197.   .nothing:
  198.         ret            
  199.   .notftdi:
  200.         DEBUGF 1,'K : Skipping not FTDI device\n'
  201.         xor     eax, eax
  202.         ret
  203. endp
  204.  
  205.  
  206. handle     equ  IOCTL.handle
  207. io_code    equ  IOCTL.io_code
  208. input      equ  IOCTL.input
  209. inp_size   equ  IOCTL.inp_size
  210. output     equ  IOCTL.output
  211. out_size   equ  IOCTL.out_size
  212.  
  213. align 4
  214. proc service_proc stdcall uses ebx esi edi, ioctl:DWORD
  215. locals
  216. ConfPacket  rb  8
  217. EventData   rd  2
  218. endl
  219.         mov     edi, [ioctl]
  220.         mov     eax, [edi + io_code]
  221.         DEBUGF 1,'K : FTDI got the request: %d\n', eax
  222.         test    eax, eax           ;0
  223.         jz      .version
  224.         dec     eax                 ;1
  225.         jz      .ftdi_get_list
  226.         dec     eax                 ;2
  227.         jz      .ftdi_set_bitmode
  228.         dec     eax                 ;3
  229.         jz      .ftdi_setrtshigh  
  230.         dec     eax                 ;4
  231.         jz      .ftdi_setrtslow    
  232.        
  233.   .version:    
  234.   .endswitch:
  235.         xor     eax, eax
  236.             ret
  237.  
  238.   .ftdi_set_bitmode:
  239.         DEBUGF 1,'K : FTDI Seting bitmode\n'        
  240.         xor     ecx, ecx
  241.         xor     esi, esi
  242.         call    CreateEvent
  243.         mov     edi, [ioctl]
  244.         mov     [EventData], eax
  245.         mov     [EventData+4], edx      
  246.         mov     word[ConfPacket], (FTDI_DEVICE_OUT_REQTYPE) + (SIO_SET_BITMODE_REQUEST shl 8)
  247.         mov     edi, [edi+input]        
  248.         mov     dx, word[edi+4]                
  249.         mov     word[ConfPacket+2], dx
  250.         mov     dword[ConfPacket+4], 0  
  251.         DEBUGF 1,'K : ConfPacket %x %x\n', [ConfPacket], [ConfPacket+4]                                
  252.         mov     ebx, [edi]
  253.         lea     esi, [ConfPacket]
  254.         lea     edi, [EventData]        
  255.         stdcall USBControlTransferAsync, [ebx + ftdi_context.nullP],  esi, 0, 0, control_callback, edi, 0
  256.         DEBUGF 1, 'K : Returned value is %d\n', eax
  257.         mov     eax, [EventData]
  258.         mov     ebx, [EventData+4]
  259.         call    WaitEvent    
  260.         jmp     .endswitch
  261.  
  262.   .ftdi_setrtshigh:
  263.         DEBUGF 1,'K : FTDI Setting RTS pin HIGH\n'        
  264.         xor     ecx, ecx
  265.         xor     esi, esi
  266.         call    CreateEvent
  267.         mov     edi, [ioctl]
  268.         mov     [EventData], eax
  269.         mov     [EventData+4], edx      
  270.         mov     dword[ConfPacket], (FTDI_DEVICE_OUT_REQTYPE) + (SIO_SET_MODEM_CTRL_REQUEST shl 8) + (SIO_SET_RTS_HIGH shl 16)
  271.         mov     dword[ConfPacket+4], 0
  272.         DEBUGF 1,'K : ConfPacket %x %x\n', [ConfPacket], [ConfPacket+4]
  273.         mov     edi, [edi+input]                                
  274.         mov     ebx, [edi]
  275.         lea     esi, [ConfPacket]
  276.         lea     edi, [EventData]        
  277.         stdcall USBControlTransferAsync, [ebx + ftdi_context.nullP],  esi, 0, 0, control_callback, edi, 0
  278.         DEBUGF 1, 'K : Returned value is %d\n', eax
  279.         mov     eax, [EventData]
  280.         mov     ebx, [EventData+4]
  281.         call    WaitEvent    
  282.         jmp     .endswitch
  283.  
  284.   .ftdi_setrtslow:
  285.         DEBUGF 1,'K : FTDI Setting RTS pin HIGH\n'        
  286.         xor     ecx, ecx
  287.         xor     esi, esi
  288.         call    CreateEvent
  289.         mov     edi, [ioctl]
  290.         mov     [EventData], eax
  291.         mov     [EventData+4], edx      
  292.         mov     dword[ConfPacket], (FTDI_DEVICE_OUT_REQTYPE) + (SIO_SET_MODEM_CTRL_REQUEST shl 8) + (SIO_SET_RTS_LOW shl 16)
  293.         mov     dword[ConfPacket+4], 0
  294.         DEBUGF 1,'K : ConfPacket %x %x\n', [ConfPacket], [ConfPacket+4]
  295.         mov     edi, [edi+input]                                  
  296.         mov     ebx, [edi]
  297.         lea     esi, [ConfPacket]
  298.         lea     edi, [EventData]        
  299.         stdcall USBControlTransferAsync, [ebx + ftdi_context.nullP],  esi, 0, 0, control_callback, edi, 0
  300.         DEBUGF 1, 'K : Returned value is %d\n', eax
  301.         mov     eax, [EventData]
  302.         mov     ebx, [EventData+4]
  303.         call    WaitEvent    
  304.         jmp     .endswitch
  305.  
  306.   .ftdi_read_pins:
  307.         DEBUGF 1,'K : FTDI Reading pins\n'
  308.         call    CreateEvent
  309.         mov     [EventData], eax
  310.         mov     [EventData+4], edx
  311.         mov     eax, FTDI_DEVICE_IN_REQTYPE + (SIO_READ_PINS_REQUEST shl 8)
  312.         mov     dword[ConfPacket], eax
  313.         jmp     .endswitch
  314.  
  315.    .ftdi_get_list:
  316.         call    linkedlist.gethead                        
  317.         mov     edi, [edi+output]
  318.         mov     [edi], eax
  319.         DEBUGF 1, 'K : FTDI Device pointer %x\n', [edi]
  320.         mov     eax, 4        
  321.         mov     [edi+out_size], eax
  322.         jmp     .endswitch         
  323. endp
  324. restore   handle
  325. restore   io_code
  326. restore   input
  327. restore   inp_size
  328. restore   output
  329. restore   out_size
  330.  
  331.  
  332. align 4
  333. proc control_callback stdcall uses ebx edi esi, .pipe:DWORD, .status:DWORD, .buffer:DWORD, .length:DWORD, .calldata:DWORD  
  334.    
  335.         DEBUGF 1, 'K : status is %d\n', [.status+24h]
  336.         mov     ecx, [.calldata]
  337.         mov     eax, [ecx]
  338.         mov     ebx, [ecx+4]
  339.         xor     edx, edx
  340.         call    RaiseEvent
  341.              
  342.         ret
  343. endp
  344.  
  345.  
  346. proc DeviceDisconnected stdcall uses  ebx esi edi, .device_data:DWORD
  347.  
  348.         DEBUGF 1, 'K : FTDI deleting device data\n'
  349.         mov     eax, [.device_data]
  350.         call    linkedlist.delete
  351.         ret          
  352. endp
  353.  
  354. include 'linkedlist.inc'
  355.        
  356. ; Exported variable: kernel API version.
  357. align 4
  358. version dd      50005h
  359. ; Structure with callback functions.
  360. usb_functions:
  361.         dd      12
  362.         dd      AddDevice
  363.         dd      DeviceDisconnected
  364.  
  365. ;for DEBUGF macro
  366. include_debug_strings
  367.  
  368.  
  369.  
  370.  
  371. ; for uninitialized data
  372. ;section '.data' data readable writable align 16
  373.