|
发表于 2003-12-6 17:32:26
|
显示全部楼层
我从一个小日本网站下的可用例子
我从一个小日本网站下的例子,在RH9下可用(I tried it), u tyr it
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#define CASE1 1
#define CASE2 2
main() {
int fd, len, wlen;
char string[132];
int data, rdata;
strcpy(string, "that is a pen pen.");
fd = open("/dev/drv1", O_RDWR);
if( fd == -1) {
printf("open error...\n");
exit(0);
}
wlen = strlen(string);
len = write(fd, string, wlen);
if( len == -1 ) {
printf("write error...\n");
exit(1);
}
len = read(fd, string, 132);
if( len == -1 ) {
printf("read error...\n");
exit(1);
}
printf ("\ndata read is %s\n", string);
data = 0x55555555;
ioctl(fd, CASE1, &data);
ioctl(fd, CASE2, &rdata);
if(rdata != data)
printf("Operation of ioctl was wrong...\n");
printf("written data is %x and read data is %x\n", data, rdata);
close(fd);
}
//gcc -c -O2 -D__KERNEL__ -DMODULE -Wall -I/usr/src/linux-2.4/include -DMODVERSIONS -include /usr/src/linux-2.4/include/linux/modversions.h drv1.c
/*# insmod skeleton.o
# mknod -m 666 /dev/skeleton c 32 0
# exit
*/
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/mm.h>
#include <asm/uaccess.h>
MODULE_LICENSE("GPL");
#define CASE1 1
#define CASE2 2
static unsigned int counter = 0;
static char string [132];
static int data;
static int skeleton_open (struct inode *inode, struct file *file)
{
MOD_INC_USE_COUNT;
return 0;
}
static int skeleton_release (struct inode *inode, struct file *file)
{
MOD_DEC_USE_COUNT;
return 0;
}
static ssize_t skeleton_read (struct file *file, char *buf, size_t count, loff_t *ppos)
{
int len, err;
printk("<5>skeleton_read called\n");
if( counter <= 0 )
return 0;
err = copy_to_user(buf,string,counter);
if (err != 0)
return -EFAULT;
len = counter;
counter = 0;
return len;
}
static ssize_t skeleton_write (struct file *file, const char *buf, size_t count, loff_t *ppos)
{
int err;
printk("<5>skeleton_write called\n");
err = copy_from_user(string,buf,count);
if (err != 0)
return -EFAULT;
counter += count;
return count;
}
static int skeleton_ioctl(struct inode *inode, struct file *file,
unsigned int cmd, unsigned long arg)
{
int retval = 0;
switch ( cmd ) {
case CASE1:/* for writing data to arg */
if (copy_from_user(&data, (int *) arg,
sizeof(int)))
return -EFAULT;
break;
case CASE2:/* for reading data from arg */
if (copy_to_user((int *) arg, &data,
sizeof(int)))
return -EFAULT;
break;
default:
retval = -EINVAL;
}
return retval;
}
static struct file_operations skeleton_fops =
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)
NULL, /* skeleton_llseek */
skeleton_read, /* skeleton_read */
skeleton_write, /* skeleton_write */
NULL, /* skeleton_readdir */
NULL, /* skeleton_poll */
skeleton_ioctl, /* skeleton_ioctl */
NULL, /* skeleton_mmap */
skeleton_open, /* skeleton_open */
NULL, /* skeleton_flush */
skeleton_release, /* skeleton_release */
NULL, /* skeleton_fsync */
NULL, /* skeleton_fasync */
NULL, /* skeleton_check_media_change */
NULL, /* skeleton_revalidate */
NULL /* skeleton_lock */
#else /* for LINUX_VERSION_CODE 2.4.0 and later */
THIS_MODULE, /* struct module *owner;*/
NULL, /* skeleton_llseek */
skeleton_read, /* skeleton_read */
skeleton_write, /* skeleton_write */
NULL, /* skeleton_readdir */
NULL, /* skeleton_poll */
skeleton_ioctl, /* skeleton_ioctl */
NULL, /* skeleton_mmap */
skeleton_open, /* skeleton_open */
NULL, /* skeleton_flush */
skeleton_release, /* skeleton_release */
NULL, /* skeleton_fsync */
NULL, /* skeleton_fasync */
NULL, /* skeleton_lock */
NULL, /* skeleton_readv */
NULL /* skeleton_writev */
#endif
};
int init_module (void)
{
int i;
i = register_chrdev (32, "skeleton", & skeleton_fops);
if (i != 0) return - EIO;
return 0;
}
void cleanup_module (void)
{
unregister_chrdev (32, "skeleton");
}
[/code] |
|