Subversion Repositories Kolibri OS

Rev

Rev 4979 | Rev 4984 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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