Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3520 | clevermous | 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 | |||
14 | public START |
||
15 | public version |
||
16 | |||
17 | ; USB constants |
||
18 | DEVICE_DESCR_TYPE = 1 |
||
19 | CONFIG_DESCR_TYPE = 2 |
||
20 | STRING_DESCR_TYPE = 3 |
||
21 | INTERFACE_DESCR_TYPE = 4 |
||
22 | ENDPOINT_DESCR_TYPE = 5 |
||
23 | DEVICE_QUALIFIER_DESCR_TYPE = 6 |
||
24 | |||
25 | CONTROL_PIPE = 0 |
||
26 | ISOCHRONOUS_PIPE = 1 |
||
27 | BULK_PIPE = 2 |
||
28 | INTERRUPT_PIPE = 3 |
||
29 | |||
30 | ; USB structures |
||
31 | virtual at 0 |
||
32 | config_descr: |
||
33 | .bLength db ? |
||
34 | .bDescriptorType db ? |
||
35 | .wTotalLength dw ? |
||
36 | .bNumInterfaces db ? |
||
37 | .bConfigurationValue db ? |
||
38 | .iConfiguration db ? |
||
39 | .bmAttributes db ? |
||
40 | .bMaxPower db ? |
||
41 | .sizeof: |
||
42 | end virtual |
||
43 | |||
44 | virtual at 0 |
||
45 | interface_descr: |
||
46 | .bLength db ? |
||
47 | .bDescriptorType db ? |
||
48 | .bInterfaceNumber db ? |
||
49 | .bAlternateSetting db ? |
||
50 | .bNumEndpoints db ? |
||
51 | .bInterfaceClass db ? |
||
52 | .bInterfaceSubClass db ? |
||
53 | .bInterfaceProtocol db ? |
||
54 | .iInterface db ? |
||
55 | .sizeof: |
||
56 | end virtual |
||
57 | |||
58 | virtual at 0 |
||
59 | endpoint_descr: |
||
60 | .bLength db ? |
||
61 | .bDescriptorType db ? |
||
62 | .bEndpointAddress db ? |
||
63 | .bmAttributes db ? |
||
64 | .wMaxPacketSize dw ? |
||
65 | .bInterval db ? |
||
66 | .sizeof: |
||
67 | end virtual |
||
68 | |||
69 | ; Driver data for all devices |
||
70 | virtual at 0 |
||
71 | device_data: |
||
72 | .type dd ? ; 1 = keyboard, 2 = mouse |
||
73 | .intpipe dd ? ; interrupt pipe handle |
||
74 | .packetsize dd ? |
||
75 | .packet rb 8 ; packet with data from device |
||
76 | .control rb 8 ; control packet to device |
||
77 | .sizeof: |
||
78 | end virtual |
||
79 | |||
80 | ; Driver data for mouse |
||
81 | virtual at device_data.sizeof |
||
82 | mouse_data: |
||
83 | ; no additional data |
||
84 | .sizeof: |
||
85 | end virtual |
||
86 | |||
87 | ; Driver data for keyboard |
||
88 | virtual at device_data.sizeof |
||
89 | keyboard_data: |
||
90 | .handle dd ? ; keyboard handle from RegKeyboard |
||
91 | .configpipe dd ? ; config pipe handle |
||
92 | .prevpacket rb 8 ; previous packet with data from device |
||
93 | .timer dd ? ; auto-repeat timer handle |
||
94 | .repeatkey db ? ; auto-repeat key code |
||
95 | .ledstate db ? ; state of LEDs |
||
96 | align 4 |
||
97 | .sizeof: |
||
98 | end virtual |
||
99 | |||
100 | section '.flat' code readable align 16 |
||
101 | ; The start procedure. |
||
102 | START: |
||
103 | ; 1. Test whether the procedure is called with the argument DRV_ENTRY. |
||
104 | ; If not, return 0. |
||
105 | xor eax, eax ; initialize return value |
||
106 | cmp dword [esp+4], 1 ; compare the argument |
||
107 | jnz .nothing |
||
108 | ; 2. Register self as a USB driver. |
||
109 | ; The name is my_driver = 'usbhid'; IOCTL interface is not supported; |
||
110 | ; usb_functions is an offset of a structure with callback functions. |
||
111 | stdcall RegUSBDriver, my_driver, eax, usb_functions |
||
112 | ; 3. Return the returned value of RegUSBDriver. |
||
113 | .nothing: |
||
114 | ret 4 |
||
115 | |||
116 | ; This procedure is called when new HID device is detected. |
||
117 | ; It initializes the device. |
||
118 | AddDevice: |
||
119 | ; Arguments are addressed through esp. In this point of the function, |
||
120 | ; [esp+4] = a handle of the config pipe, [esp+8] points to config_descr |
||
121 | ; structure, [esp+12] points to interface_descr structure. |
||
122 | ; 1. Check device type. Currently only mice and keyboards with |
||
123 | ; boot protocol are supported. |
||
124 | ; 1a. Get the subclass and the protocol. Since bInterfaceSubClass and |
||
125 | ; bInterfaceProtocol are subsequent in interface_descr, just one |
||
126 | ; memory reference is used for both. |
||
127 | mov edx, [esp+12] |
||
128 | push ebx ; save used register to be stdcall |
||
129 | mov cx, word [edx+interface_descr.bInterfaceSubClass] |
||
130 | ; 1b. For boot protocol, subclass must be 1 and protocol must be either 1 for |
||
131 | ; a keyboard or 2 for a mouse. Check. |
||
132 | cmp cx, 0x0101 |
||
133 | jz .keyboard |
||
134 | cmp cx, 0x0201 |
||
135 | jz .mouse |
||
136 | ; 1c. If the device is neither a keyboard nor a mouse, print a message and |
||
137 | ; go to 6c. |
||
138 | DEBUGF 1,'K : unknown HID device\n' |
||
139 | jmp .nothing |
||
140 | ; 1d. If the device is a keyboard or a mouse, print a message and continue |
||
141 | ; configuring. |
||
142 | .keyboard: |
||
143 | DEBUGF 1,'K : USB keyboard detected\n' |
||
144 | push keyboard_data.sizeof |
||
145 | jmp .common |
||
146 | .mouse: |
||
147 | DEBUGF 1,'K : USB mouse detected\n' |
||
148 | push mouse_data.sizeof |
||
149 | .common: |
||
150 | ; 2. Allocate memory for device data. |
||
151 | pop eax ; get size of device data |
||
152 | ; 2a. Call the kernel, saving and restoring register edx. |
||
153 | push edx |
||
154 | call Kmalloc |
||
155 | pop edx |
||
156 | ; 2b. Check result. If failed, say a message and go to 6c. |
||
157 | test eax, eax |
||
158 | jnz @f |
||
159 | DEBUGF 1,'K : no memory\n' |
||
160 | jmp .nothing |
||
161 | @@: |
||
162 | xchg eax, ebx |
||
163 | ; HID devices use one IN interrupt endpoint for polling the device |
||
164 | ; and an optional OUT interrupt endpoint. We do not use the later, |
||
165 | ; but must locate the first. Look for the IN interrupt endpoint. |
||
166 | ; 3. Get the upper bound of all descriptors' data. |
||
167 | mov eax, [esp+8+4] ; configuration descriptor |
||
168 | movzx ecx, [eax+config_descr.wTotalLength] |
||
169 | add eax, ecx |
||
170 | ; 4. Loop over all descriptors until |
||
171 | ; either end-of-data reached - this is fail |
||
172 | ; or interface descriptor found - this is fail, all further data |
||
173 | ; correspond to that interface |
||
174 | ; or endpoint descriptor found. |
||
175 | ; 4a. Loop start: eax points to the interface descriptor. |
||
176 | .lookep: |
||
177 | ; 4b. Get next descriptor. |
||
178 | movzx ecx, byte [edx] ; the first byte of all descriptors is length |
||
179 | add edx, ecx |
||
180 | ; 4c. Check that at least two bytes are readable. The opposite is an error. |
||
181 | inc edx |
||
182 | cmp edx, eax |
||
183 | jae .errorep |
||
184 | dec edx |
||
185 | ; 4d. Check that this descriptor is not interface descriptor. The opposite is |
||
186 | ; an error. |
||
187 | cmp byte [edx+endpoint_descr.bDescriptorType], INTERFACE_DESCR_TYPE |
||
188 | jz .errorep |
||
189 | ; 4e. Test whether this descriptor is an endpoint descriptor. If not, continue |
||
190 | ; the loop. |
||
191 | cmp byte [edx+endpoint_descr.bDescriptorType], ENDPOINT_DESCR_TYPE |
||
192 | jnz .lookep |
||
193 | ; 5. Check that the descriptor contains all required data and all data are |
||
194 | ; readable. If so, proceed to 7. |
||
195 | cmp byte [edx+endpoint_descr.bLength], endpoint_descr.sizeof |
||
196 | jb .errorep |
||
197 | sub eax, endpoint_descr.sizeof |
||
198 | cmp edx, eax |
||
199 | jbe @f |
||
200 | ; 6. An error occured during processing endpoint descriptor. |
||
201 | .errorep: |
||
202 | ; 6a. Print a message. |
||
203 | DEBUGF 1,'K : error: invalid endpoint descriptor\n' |
||
204 | ; 6b. Free memory allocated for device data. |
||
205 | .free: |
||
206 | xchg eax, ebx |
||
207 | call Kfree |
||
208 | .nothing: |
||
209 | ; 6c. Return an error. |
||
210 | xor eax, eax |
||
211 | pop ebx |
||
212 | ret 12 |
||
213 | @@: |
||
214 | ; 7. Check that the endpoint is IN interrupt endpoint. If not, go to 6. |
||
215 | test [edx+endpoint_descr.bEndpointAddress], 80h |
||
216 | jz .errorep |
||
217 | mov cl, [edx+endpoint_descr.bmAttributes] |
||
218 | and cl, 3 |
||
219 | cmp cl, INTERRUPT_PIPE |
||
220 | jnz .errorep |
||
221 | ; 8. Open pipe for the endpoint. |
||
222 | ; 8a. Load parameters from the descriptor. |
||
223 | movzx ecx, [edx+endpoint_descr.bEndpointAddress] |
||
224 | movzx eax, [edx+endpoint_descr.bInterval] |
||
225 | movzx edx, [edx+endpoint_descr.wMaxPacketSize] |
||
226 | ; 8b. Call the kernel, saving and restoring edx. |
||
227 | push edx |
||
228 | stdcall USBOpenPipe, [esp+4+24], ecx, edx, INTERRUPT_PIPE, eax |
||
229 | pop edx |
||
230 | ; 8c. Check result. If failed, go to 6b. |
||
231 | test eax, eax |
||
232 | jz .free |
||
233 | ; We use 12 bytes for device type, interrupt pipe and interrupt packet size, |
||
234 | ; 8 bytes for a packet and 8 bytes for previous packet, used by a keyboard. |
||
235 | ; 9. Initialize device data. |
||
236 | mov [ebx+device_data.intpipe], eax |
||
237 | push 8 |
||
238 | pop ecx |
||
239 | cmp edx, ecx |
||
240 | jb @f |
||
241 | mov edx, ecx |
||
242 | @@: |
||
243 | xor eax, eax |
||
244 | mov [ebx+device_data.packetsize], edx |
||
245 | mov dword [ebx+device_data.packet], eax |
||
246 | mov dword [ebx+device_data.packet+4], eax |
||
247 | mov edx, [esp+12+4] ; interface descriptor |
||
248 | movzx ecx, [edx+interface_descr.bInterfaceProtocol] |
||
249 | mov [ebx+device_data.type], ecx |
||
250 | cmp ecx, 1 |
||
251 | jnz @f |
||
252 | mov [ebx+keyboard_data.handle], eax |
||
253 | mov [ebx+keyboard_data.timer], eax |
||
254 | mov [ebx+keyboard_data.repeatkey], al |
||
255 | mov dword [ebx+keyboard_data.prevpacket], eax |
||
256 | mov dword [ebx+keyboard_data.prevpacket+4], eax |
||
257 | mov eax, [esp+4+4] |
||
258 | mov [ebx+keyboard_data.configpipe], eax |
||
259 | @@: |
||
260 | ; 10. Send the control packet SET_PROTOCOL(Boot Protocol) to the interface. |
||
261 | lea eax, [ebx+device_data.control] |
||
262 | mov dword [eax], 21h + (0Bh shl 8) + (0 shl 16) ; class request to interface + SET_PROTOCOL + Boot protocol |
||
263 | and dword [eax+4], 0 |
||
264 | mov dl, [edx+interface_descr.bInterfaceNumber] |
||
265 | mov [eax+4], dl |
||
266 | ; Callback function is mouse_configured for mice and keyboard_configured1 for keyboards. |
||
267 | mov edx, keyboard_configured1 |
||
268 | cmp ecx, 1 |
||
269 | jz @f |
||
270 | mov edx, mouse_configured |
||
271 | @@: |
||
272 | stdcall USBControlTransferAsync, [esp+4+28], eax, 0, 0, edx, ebx, 0 |
||
273 | ; 11. Return with pointer to device data as returned value. |
||
274 | xchg eax, ebx |
||
275 | pop ebx |
||
276 | ret 12 |
||
277 | |||
278 | ; This function is called when SET_PROTOCOL command for keyboard is done, |
||
279 | ; either successful or unsuccessful. |
||
280 | keyboard_configured1: |
||
281 | xor edx, edx |
||
282 | ; 1. Check the status of the transfer. |
||
283 | ; If the transfer was failed, go to the common error handler. |
||
284 | cmp dword [esp+8], edx ; status is zero? |
||
285 | jnz keyboard_data_ready.error |
||
286 | ; 2. Send the control packet SET_IDLE(infinity). HID auto-repeat is not useful. |
||
287 | mov eax, [esp+20] |
||
288 | push edx ; flags for USBControlTransferAsync |
||
289 | push eax ; userdata for USBControlTransferAsync |
||
290 | add eax, device_data.control |
||
291 | mov dword [eax], 21h + (0Ah shl 8) + (0 shl 24) ; class request to interface + SET_IDLE + no autorepeat |
||
292 | stdcall USBControlTransferAsync, dword [eax+keyboard_data.configpipe-device_data.control], \ |
||
293 | eax, edx, edx, keyboard_configured2; , |
||
294 | ; 3. Return. |
||
295 | ret 20 |
||
296 | |||
297 | ; This function is called when SET_IDLE command for keyboard is done, |
||
298 | ; either successful or unsuccessful. |
||
299 | keyboard_configured2: |
||
300 | ; Check the status of the transfer and go to the corresponding label |
||
301 | ; in the main handler. |
||
302 | cmp dword [esp+8], 0 |
||
303 | jnz keyboard_data_ready.error |
||
304 | mov edx, [esp+20] |
||
305 | push edx |
||
306 | stdcall RegKeyboard, usbkbd_functions, edx |
||
307 | pop edx |
||
308 | mov [edx+keyboard_data.handle], eax |
||
309 | jmp keyboard_data_ready.next |
||
310 | |||
311 | ; This function is called when another interrupt packet arrives, |
||
312 | ; processed either successfully or unsuccessfully. |
||
313 | ; It should parse the packet and initiate another transfer with |
||
314 | ; the same callback function. |
||
315 | keyboard_data_ready: |
||
316 | ; 1. Check the status of the transfer. |
||
317 | mov eax, [esp+8] |
||
318 | test eax, eax |
||
319 | jnz .error |
||
320 | ; Parse the packet, comparing with the previous packet. |
||
321 | ; For boot protocol, USB keyboard packet consists of the first byte |
||
322 | ; with status keys that are currently pressed. The second byte should |
||
323 | ; be ignored, and other 5 bytes denote keys that are currently pressed. |
||
324 | push esi ebx ; save used registers to be stdcall |
||
325 | ; 2. Process control keys. |
||
326 | ; 2a. Initialize before loop for control keys. edx = mask for control bits |
||
327 | ; that were changed. |
||
328 | mov ebx, [esp+20+8] |
||
329 | movzx edx, byte [ebx+device_data.packet] ; get state of control keys |
||
330 | xor dl, byte [ebx+keyboard_data.prevpacket] ; compare with previous state |
||
331 | ; 2b. If state of control keys has not changed, advance to 3. |
||
332 | jz .nocontrol |
||
333 | ; 2c. Otherwise, loop over control keys; esi = bit number. |
||
334 | xor esi, esi |
||
335 | .controlloop: |
||
336 | ; 2d. Skip bits that have not changed. |
||
337 | bt edx, esi |
||
338 | jnc .controlnext |
||
339 | push edx ; save register which is possibly modified by API |
||
340 | ; The state of the current control key has changed. |
||
341 | ; 2e. For extended control keys, send the prefix 0xE0. |
||
342 | mov al, [control_keys+esi] |
||
343 | test al, al |
||
344 | jns @f |
||
345 | push eax |
||
346 | mov ecx, 0xE0 |
||
347 | call SetKeyboardData |
||
348 | pop eax |
||
349 | and al, 0x7F |
||
350 | @@: |
||
351 | ; 2f. If the current state of the control key is "pressed", send normal |
||
352 | ; scancode. Otherwise, the key is released, so set the high bit in scancode. |
||
353 | movzx ecx, al |
||
354 | bt dword [ebx+device_data.packet], esi |
||
355 | jc @f |
||
356 | or cl, 0x80 |
||
357 | @@: |
||
358 | call SetKeyboardData |
||
359 | pop edx ; restore register which was possibly modified by API |
||
360 | .controlnext: |
||
361 | ; 2g. We have 8 control keys. |
||
362 | inc esi |
||
363 | cmp esi, 8 |
||
364 | jb .controlloop |
||
365 | .nocontrol: |
||
366 | ; 3. Initialize before loop for normal keys. esi = index. |
||
367 | push 2 |
||
368 | pop esi |
||
369 | .normalloop: |
||
370 | ; 4. Process one key which was pressed in the previous packet. |
||
371 | ; 4a. Get the next pressed key from the previous packet. |
||
372 | movzx eax, byte [ebx+esi+keyboard_data.prevpacket] |
||
373 | ; 4b. Ignore special codes. |
||
374 | cmp al, 3 |
||
375 | jbe .normalnext1 |
||
376 | ; 4c. Ignore keys that are still pressed in the current packet. |
||
377 | lea ecx, [ebx+device_data.packet] |
||
378 | call haskey |
||
379 | jz .normalnext1 |
||
380 | ; 4d. Say warning about keys with strange codes. |
||
381 | cmp eax, normal_keys_number |
||
382 | jae .badkey1 |
||
383 | movzx ecx, [normal_keys+eax] |
||
384 | jecxz .badkey1 |
||
385 | ; 4e. For extended keys, send the prefix 0xE0. |
||
386 | push ecx ; save keycode |
||
387 | test cl, cl |
||
388 | jns @f |
||
389 | push ecx |
||
390 | mov ecx, 0xE0 |
||
391 | call SetKeyboardData |
||
392 | pop ecx |
||
393 | @@: |
||
394 | ; 4f. Send the release event. |
||
395 | or cl, 0x80 |
||
396 | call SetKeyboardData |
||
397 | ; 4g. If this key is autorepeating, stop the timer. |
||
398 | pop ecx ; restore keycode |
||
399 | cmp cl, [ebx+keyboard_data.repeatkey] |
||
400 | jnz .normalnext1 |
||
401 | mov eax, [ebx+keyboard_data.timer] |
||
402 | test eax, eax |
||
403 | jz .normalnext1 |
||
404 | stdcall CancelTimerHS, eax |
||
405 | and [ebx+keyboard_data.timer], 0 |
||
406 | jmp .normalnext1 |
||
407 | .badkey1: |
||
408 | DEBUGF 1,'K : unknown keycode: %x\n',al |
||
409 | .normalnext1: |
||
410 | ; 5. Process one key which is pressed in the current packet. |
||
411 | ; 5a. Get the next pressed key from the current packet. |
||
412 | movzx eax, byte [ebx+esi+device_data.packet] |
||
413 | ; 5b. Ignore special codes. |
||
414 | cmp al, 3 |
||
415 | jbe .normalnext2 |
||
416 | ; 5c. Ignore keys that were already pressed in the previous packet. |
||
417 | lea ecx, [ebx+keyboard_data.prevpacket] |
||
418 | call haskey |
||
419 | jz .normalnext2 |
||
420 | ; 5d. Say warning about keys with strange codes. |
||
421 | cmp eax, normal_keys_number |
||
422 | jae .badkey2 |
||
423 | movzx ecx, [normal_keys+eax] |
||
424 | jecxz .badkey2 |
||
425 | ; 5e. For extended keys, send the prefix 0xE0. |
||
426 | push ecx ; save keycode |
||
427 | test cl, cl |
||
428 | jns @f |
||
429 | push ecx |
||
430 | mov ecx, 0xE0 |
||
431 | call SetKeyboardData |
||
432 | pop ecx |
||
433 | @@: |
||
434 | ; 5f. Send the press event. |
||
435 | and cl, not 0x80 |
||
436 | call SetKeyboardData |
||
437 | ; 5g. Stop the current auto-repeat timer, if present. |
||
438 | mov eax, [ebx+keyboard_data.timer] |
||
439 | test eax, eax |
||
440 | jz @f |
||
441 | stdcall CancelTimerHS, eax |
||
442 | @@: |
||
443 | ; 5h. Start the auto-repeat timer. |
||
444 | pop ecx ; restore keycode |
||
445 | mov [ebx+keyboard_data.repeatkey], cl |
||
446 | stdcall TimerHS, 25, 5, autorepeat_timer, ebx |
||
447 | mov [ebx+keyboard_data.timer], eax |
||
448 | jmp .normalnext2 |
||
449 | .badkey2: |
||
450 | DEBUGF 1,'K : unknown keycode: %x\n',al |
||
451 | .normalnext2: |
||
452 | ; 6. Advance to next key. |
||
453 | inc esi |
||
454 | cmp esi, 8 |
||
455 | jb .normalloop |
||
456 | ; 7. Save the packet data for future reference. |
||
457 | mov eax, dword [ebx+device_data.packet] |
||
458 | mov dword [ebx+keyboard_data.prevpacket], eax |
||
459 | mov eax, dword [ebx+device_data.packet+4] |
||
460 | mov dword [ebx+keyboard_data.prevpacket+4], eax |
||
461 | pop ebx esi ; restore registers to be stdcall |
||
462 | .next: |
||
463 | ; 8. Initiate transfer on the interrupt pipe. |
||
464 | mov eax, [esp+20] |
||
465 | push 1 ; flags for USBNormalTransferAsync |
||
466 | push eax ; userdata for USBNormalTransferAsync |
||
467 | add eax, device_data.packet |
||
468 | stdcall USBNormalTransferAsync, dword [eax+device_data.intpipe-device_data.packet], \ |
||
469 | eax, dword [eax+device_data.packetsize-device_data.packet], \ |
||
470 | keyboard_data_ready;, |
||
471 | ; 9. Return. |
||
472 | .nothing: |
||
473 | ret 20 |
||
474 | .error: |
||
475 | ; An error has occured. |
||
476 | ; 10. If an error is caused by the disconnect, do nothing, it is handled |
||
477 | ; in DeviceDisconnected. Otherwise, say a message. |
||
478 | cmp eax, 16 |
||
479 | jz @f |
||
480 | push esi |
||
481 | mov esi, errormsgkbd |
||
482 | call SysMsgBoardStr |
||
483 | pop esi |
||
484 | @@: |
||
485 | ret 20 |
||
486 | |||
487 | ; Auxiliary procedure for keyboard_data_ready. |
||
488 | haskey: |
||
489 | push 2 |
||
490 | pop edx |
||
491 | @@: |
||
492 | cmp byte [ecx+edx], al |
||
493 | jz @f |
||
494 | inc edx |
||
495 | cmp edx, 7 |
||
496 | jbe @b |
||
497 | @@: |
||
498 | ret |
||
499 | |||
500 | ; Timer function for auto-repeat. |
||
501 | autorepeat_timer: |
||
502 | mov eax, [esp+4] |
||
503 | movzx ecx, [eax+keyboard_data.repeatkey] |
||
504 | test cl, cl |
||
505 | jns @f |
||
506 | push ecx |
||
507 | mov ecx, 0xE0 |
||
508 | call SetKeyboardData |
||
509 | pop ecx |
||
510 | and cl, not 0x80 |
||
511 | @@: |
||
512 | call SetKeyboardData |
||
513 | ret 4 |
||
514 | |||
515 | ; This function is called to update LED state on the keyboard. |
||
516 | SetKeyboardLights: |
||
517 | mov eax, [esp+4] |
||
518 | add eax, device_data.control |
||
519 | mov dword [eax], 21h + (9 shl 8) + (2 shl 24) |
||
520 | ; class request to interface + SET_REPORT + Output zero report |
||
521 | mov byte [eax+6], 1 |
||
522 | mov edx, [esp+8] |
||
523 | shr dl, 1 |
||
524 | jnc @f |
||
525 | or dl, 4 |
||
526 | @@: |
||
527 | lea ecx, [eax+keyboard_data.ledstate-device_data.control] |
||
528 | mov [ecx], dl |
||
529 | stdcall USBControlTransferAsync, dword [eax+keyboard_data.configpipe-device_data.control], \ |
||
530 | eax, ecx, 1, keyboard_data_ready.nothing, 0, 0 |
||
531 | ret 8 |
||
532 | |||
533 | ; This function is called when it is safe to free keyboard data. |
||
534 | CloseKeyboard: |
||
535 | mov eax, [esp+4] |
||
536 | push ebx |
||
537 | call Kfree |
||
538 | pop ebx |
||
539 | ret 4 |
||
540 | |||
541 | ; This function is called when SET_PROTOCOL command for mouse is done, |
||
542 | ; either successful or unsuccessful. |
||
543 | mouse_configured: |
||
544 | ; Check the status of the transfer and go to the corresponding label |
||
545 | ; in the main handler. |
||
546 | cmp dword [esp+8], 0 |
||
547 | jnz mouse_data_ready.error |
||
548 | mov eax, [esp+20] |
||
549 | add eax, device_data.packet |
||
550 | jmp mouse_data_ready.next |
||
551 | |||
552 | ; This function is called when another interrupt packet arrives, |
||
553 | ; processed either successfully or unsuccessfully. |
||
554 | ; It should parse the packet and initiate another transfer with |
||
555 | ; the same callback function. |
||
556 | mouse_data_ready: |
||
557 | ; 1. Check the status of the transfer. |
||
558 | mov eax, [esp+8] |
||
559 | test eax, eax |
||
560 | jnz .error |
||
561 | mov edx, [esp+16] |
||
562 | ; 2. Parse the packet. |
||
563 | ; For boot protocol, USB mouse packet consists of at least 3 bytes. |
||
564 | ; The first byte is state of mouse buttons, the next two bytes are |
||
565 | ; x and y movements. |
||
566 | ; Normal mice do not distinguish between boot protocol and report protocol; |
||
567 | ; in this case, scroll data are also present. Advanced mice, however, |
||
568 | ; support two different protocols, boot protocol is used for compatibility |
||
569 | ; and does not contain extended buttons or scroll data. |
||
570 | mov eax, [esp+12] ; buffer |
||
571 | push eax |
||
572 | xor ecx, ecx |
||
573 | cmp edx, 4 |
||
574 | jbe @f |
||
575 | movsx ecx, byte [eax+4] |
||
576 | @@: |
||
577 | push ecx |
||
578 | xor ecx, ecx |
||
579 | cmp edx, 3 |
||
580 | jbe @f |
||
581 | movsx ecx, byte [eax+3] |
||
582 | neg ecx |
||
583 | @@: |
||
584 | push ecx |
||
585 | xor ecx, ecx |
||
586 | cmp edx, 2 |
||
587 | jbe @f |
||
588 | movsx ecx, byte [eax+2] |
||
589 | neg ecx |
||
590 | @@: |
||
591 | push ecx |
||
592 | movsx ecx, byte [eax+1] |
||
593 | push ecx |
||
594 | movzx ecx, byte [eax] |
||
595 | push ecx |
||
596 | call SetMouseData |
||
597 | pop eax |
||
598 | .next: |
||
599 | ; 3. Initiate transfer on the interrupt pipe. |
||
600 | stdcall USBNormalTransferAsync, dword [eax+device_data.intpipe-device_data.packet], \ |
||
601 | eax, dword [eax+device_data.packetsize-device_data.packet], mouse_data_ready, eax, 1 |
||
602 | ; 4. Return. |
||
603 | ret 20 |
||
604 | .error: |
||
605 | ; An error has occured. |
||
606 | ; 5. If an error is caused by the disconnect, do nothing, it is handled |
||
607 | ; in DeviceDisconnected. Otherwise, say a message. |
||
608 | cmp eax, 16 |
||
609 | jz @f |
||
610 | push esi |
||
611 | mov esi, errormsgmouse |
||
612 | call SysMsgBoardStr |
||
613 | pop esi |
||
614 | @@: |
||
615 | ret 20 |
||
616 | |||
617 | ; This function is called when the device is disconnected. |
||
618 | DeviceDisconnected: |
||
619 | push ebx ; save used register to be stdcall |
||
620 | ; 1. Say a message. Use different messages for keyboards and mice. |
||
621 | mov ebx, [esp+4+4] |
||
622 | push esi |
||
623 | mov esi, disconnectmsgk |
||
624 | cmp byte [ebx+device_data.type], 1 |
||
625 | jz @f |
||
626 | mov esi, disconnectmsgm |
||
627 | @@: |
||
628 | stdcall SysMsgBoardStr |
||
629 | pop esi |
||
630 | ; 2. If device is keyboard, then we must unregister it as a keyboard and |
||
631 | ; possibly stop the auto-repeat timer. |
||
632 | cmp byte [ebx+device_data.type], 1 |
||
633 | jnz .nokbd |
||
634 | mov eax, [ebx+keyboard_data.timer] |
||
635 | test eax, eax |
||
636 | jz @f |
||
637 | stdcall CancelTimerHS, eax |
||
638 | @@: |
||
639 | mov ecx, [ebx+keyboard_data.handle] |
||
640 | jecxz .nokbd |
||
641 | stdcall DelKeyboard, ecx |
||
642 | ; If keyboard is registered, then we should free data in CloseKeyboard, not here. |
||
643 | jmp .nothing |
||
644 | .nokbd: |
||
645 | ; 3. Free the device data. |
||
646 | xchg eax, ebx |
||
647 | call Kfree |
||
648 | ; 4. Return. |
||
649 | .nothing: |
||
650 | pop ebx ; restore used register to be stdcall |
||
651 | ret 4 ; purge one dword argument to be stdcall |
||
652 | |||
653 | ; strings |
||
654 | my_driver db 'usbhid',0 |
||
655 | errormsgmouse db 'K : USB transfer error, disabling mouse',10,0 |
||
656 | errormsgkbd db 'K : USB transfer error, disabling keyboard',10,0 |
||
657 | disconnectmsgm db 'K : USB mouse disconnected',10,0 |
||
658 | disconnectmsgk db 'K : USB keyboard disconnected',10,0 |
||
659 | |||
660 | ; data for keyboard: correspondence between HID usage keys and PS/2 scancodes. |
||
661 | EX = 80h |
||
662 | label control_keys byte |
||
663 | db 1Dh, 2Ah, 38h, 5Bh+EX, 1Dh+EX, 36h, 38h+EX, 5Ch+EX |
||
664 | label normal_keys byte |
||
665 | db 00h, 00h, 00h, 00h, 1Eh, 30h, 2Eh, 20h, 12h, 21h, 22h, 23h, 17h, 24h, 25h, 26h ; 0x |
||
666 | db 32h, 31h, 18h, 19h, 10h, 13h, 1Fh, 14h, 16h, 2Fh, 11h, 2Dh, 15h, 2Ch, 02h, 03h ; 1x |
||
667 | db 04h, 05h, 06h, 07h, 08h, 09h, 0Ah, 0Bh, 1Ch, 01h, 0Eh, 0Fh, 39h, 0Ch, 0Dh, 1Ah ; 2x |
||
668 | db 1Bh, 2Bh, 2Bh, 27h, 28h, 29h, 33h, 34h, 35h, 3Ah, 3Bh, 3Ch, 3Dh, 3Eh, 3Fh, 40h ; 3x |
||
669 | db 41h, 42h, 43h, 44h, 57h, 58h,37h+EX,46h,0,52h+EX,47h+EX,49h+EX,53h+EX,4Fh+EX,51h+EX,4Dh+EX ; 4x |
||
670 | db 4Bh+EX,50h+EX,48h+EX,45h,35h+EX,37h,4Ah,4Eh,1Ch+EX,4Fh,50h,51h,4Bh,4Ch,4Dh,47h ; 5x |
||
671 | db 48h, 49h, 52h, 53h, 56h,5Dh+EX,5Eh+EX,59h,64h,65h,66h, 67h, 68h, 69h, 6Ah, 6Bh ; 6x |
||
672 | db 6Ch, 6Dh, 6Eh, 76h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h ; 7x |
||
673 | db 00h, 00h, 00h, 00h, 00h, 7Eh, 00h, 73h, 70h, 7Dh, 79h, 7Bh, 5Ch, 00h, 00h, 00h ; 8x |
||
674 | db 0F2h,0F1h,78h, 77h, 76h |
||
675 | normal_keys_number = $ - normal_keys |
||
676 | |||
677 | ; Exported variable: kernel API version. |
||
678 | align 4 |
||
679 | version dd 50005h |
||
680 | ; Structure with callback functions. |
||
681 | usb_functions: |
||
682 | dd 12 |
||
683 | dd AddDevice |
||
684 | dd DeviceDisconnected |
||
685 | |||
686 | ; Structure with callback functions for keyboards. |
||
687 | usbkbd_functions: |
||
688 | dd 12 |
||
689 | dd CloseKeyboard |
||
690 | dd SetKeyboardLights |
||
691 | |||
692 | ; for DEBUGF macro |
||
693 | include_debug_strings |
||
694 | |||
695 | ; for uninitialized data |
||
696 | section '.data' data readable writable align 16 |