device example (device kernel module)
Ключевые слова: device, kernel, module, (найти похожие документы)
_ RU.LINUX (2:5077/15.22) ___________________________________________ RU.LINUX _
From : andrew derya/bin 2:5020/1145 19 Feb 98 23:21:14
Subj : device example
________________________________________________________________________________
Хаюшки, Vladislav!
18 Feb, Vladislav Myasnyankin был писАть:
VM> Hапомните пожалуйста, где находится _пpостой_ пpимеp pеализации
VM> устpойства (ну, того что в /dev живет).
#define __KERNEL__
#define MODULE
#include <asm/segment.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/version.h>
static int foo_read (struct inode*, struct file*, char*, int);
static int foo_write (struct inode*, struct file*, const char*, int);
static int foo_open (struct inode*, struct file*);
static void foo_close (struct inode*, struct file*);
static struct file_operations foo_fops = {
NULL,
foo_read,
foo_write,
NULL,
NULL,
NULL,
NULL,
foo_open,
foo_close,
NULL,
NULL,
NULL,
NULL
};
#define STRFOO "U stupid dumbshit goddam mazafaka!\n"
#define STRFOOLEN sizeof (STRFOO) - 1
char *strFoo;
int strFooPos;
static int foo_read (struct inode *inode, struct file *file, char *buff, int
count)
{
int portion;
if (count < 0) return -EINVAL;
if (!strFooPos) return 0;
memcpy_tofs (buff, strFoo, portion = (strFooPos <= count) ? strFooPos :
count);
strFoo += portion; strFooPos -= portion;
return portion;
}
char printk_buff[4096];
static int foo_write (struct inode *inode, struct file *file, const char *buff,
int count)
{
int portion;
memcpy_fromfs (printk_buff, buff, portion = count > 4095 ? 4095 : count);
printk_buff[portion] = 0;
printk ("Written 2 /dev/foo: %s\n", printk_buff);
return portion;
}
int stat = 0;
static int foo_open (struct inode *inode, struct file *file)
{
if (stat) return -EBUSY;
stat = 1; strFoo = STRFOO; strFooPos = STRFOOLEN; return 0;
}
static void foo_close (struct inode *inode, struct file *file)
{
stat = 0;
}
static int init_module()
{
if (register_chrdev (66, "foo", &foo_fops))
printk ("Can't register chrdev\n");
else
printk ("Device foo registered\n");
return 0;
}
void cleanup_module()
{
if (unregister_chrdev (66, "foo"))
printk ("Can't unreg foo\n");
else
printk ("Device foo unregistered\n");
}
int bad_user_access_length()
{
return 0;
}
Это для stable/old ядеp. В 2.1.xx сегментация не юзается => нет _tofs и _fromfs.
--
cu,djsf
--- Rav^Hpe this Generation
* Origin: techno oligarchy (2:5020/1145@fidonet)