編譯後的Driver 是 myLED.ko ,User AP 是 myU1.exe 。
User space side 的程式碼:
/* myU1.c */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <linux/ioctl.h>
#include "modC_inc.h"
int main()
{
static struct ioctlPara *pioctlParaVar;
int fd = open( "/dev/myChar1" , O_RDWR ) ;
pioctlParaVar = (struct ioctlPara *)malloc( sizeof( struct ioctlPara ) );
strcpy( pioctlParaVar->writeStr, "Hello Kernel by User space" );
ioctl(fd, my_IOC_RW , (void *)pioctlParaVar );
ioctl(fd, my_IOC_W , (void *)pioctlParaVar );
ioctl(fd, my_IOC_R , (void *)pioctlParaVar );
printf( "pioctlParaVar->writeStr=%s , pioctlParaVar->readStr=%s \r\n",
pioctlParaVar->writeStr, pioctlParaVar->readStr );
free(pioctlParaVar);
close( fd );
}
由於 Device Driver 程式碼比較長,僅貼上 file operations 函數的 ioctl 程式碼:

操作過程:
~/usb$ls
myLED.ko myU1.exe mydrv.ko mydrv.o test1.exe
~/usb$insmod ./myLED.ko
Using ./myLED.ko
Hello, world
world
howmany=1
~/usb$./myU1.exe
LED_open
my_IOC_RW Hello Kernel by User space
my_IOC_W Hello Kernel by User space
my_IOC_R
pioctlParaVar->writeStr=Hello Kernel by User space , pioctlParaVLED_release
ar->readStr=Hello Kernel by User space
~/usb$
~/usb$lsmod
Module Size Used by Not tainted
myLED 3980 0 - Live 0xbf000000
~/usb$rmmod myLED
Goodbye, cruel world
~/usb$
執行畫面如下:

沒有留言:
張貼留言