Midi driver programming - static address ======================================== With this example, you can write a static memory address driver, which greatly simplifies driver programming. You just need to check that driver address at rbp matches the assembled base address. General driver steps: 1) Kernel loads driver (config.mnt/audio_driver) 2) Kernel scans PCI for matching device ID's from drivers header. 3) If a match is found, kernel executes driver_entry functions 1 and 2. 4) Kernel calls play and record functions at application request. Driver is executed at ring-0 privelidge level. If needed, set page non-cacheable with system call 400. Driver memory consists of two 2MB pages. First page for code and internal driver data, the second page for data communication between driver and device. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; PCI midi card driver for MenuetOS ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; driver_base equ (0x100000*60) use64 org driver_base db 'MENUET64' ; Header identifier dq 0x102 ; PCI audio card driver dq driver_entry-driver_base ; Driver entry dq image_end ; Size of image dq 0x100000*4 ; Required memory (max 4mb) dd 0x11223344 ; Supported PCI card 1 dd 0xAABBCCDD ; Supported PCI card 2, .. dd 0x00000000 ; End db "Midi card",0 ; Device type db "Company",0 ; Manufacturer inbytes: dq 0x0 ; Received bytes outbytes: dq 0x0 ; Sent bytes status: dq 0x0 ; 0x00 = driver not initialized ; 0x01 = device is working properly ; 0xff = undefined error driver_entry: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; Description : Driver entry from OS ; ; In : rax - function ; rbp - driver physical memory address ; r10 - card IO port base ; r13 - PCI position [pci_bus] shl 16 + [pci_dev] shl 8 ; r14 - Entry point for drivers internal system calls 5,105,400 ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; cmp rbp , driver_base je addressfine mov [status],dword 0xff mov rax , 0xff ret addressfine: cmp rax , 1 je midi_probe cmp rax , 2 je midi_reset cmp rax , 3 je midi_send cmp rax , 4 je midi_poll cmp rax , 255 je midi_shutdown mov rax , 0xff ; function not supported ret midi_probe: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; Description : Verify search ; ; Out: rax = 0 - Success ; 1 - Fail ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; mov [status], dword 1 mov rax , 0 ret midi_reset: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; Description : Reset midi card ; ; Out: rax = 0 - Success ; 1 - Fail ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; If needed, set next 2M page non-cacheable with system call 400 ; ; for data transfer to/from the device ; ; mov rax , 400 ; mov rbx , 1 ; mov rcx , driver_base ; add rcx , 0x200000 ; call r14 ; syscall entry mov [status],dword 1 mov rax , 0 ret midi_send: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; Description: Midi send data ; ; In : cl = midi byte out ; ; Out: rax = 0 - Success ; 1 - Fail ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; mov rdx , 1 add [outbytes], rdx mov rax , 0 ret midi_poll: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; Description: Midi poll ; ; See syscall 117 for parameters. ; ; Out: rax = 0 - Success, bl = midi in byte ; 1 - Fail ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; mov rdx , 1 add [inbytes], rdx mov rbx , byte from midi device mov rax , 0 ret midi_shutdown: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; Description: Midi shutdown driver and card ; ; Out: rax = 0 - Success ; 1 - Fail ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; mov rax , 0 ; 0/1 = success/fail ret ; ; Driver data ; parameter1: dq 0x0 parameter2: dq 0x0 ; ; Non-cacheable data between driver and device ; org (driver_base+0x100000*2) structure1: ; .. structure2: ; .. image_end: