cmd , unsigned long arg )
{
struct light_dev * dev = filp-> private_data;
switch(_cmd){
case 1:
dev->value = 1; //just a flag
light_on();
break;
case 0:
dev->value = 0; //just a flag
light_off();
break;
default: return -ENOTTY; //CMD CAN NOT SUPPORT
}
return 0;
}
static struct file_operations light_fops = {
//fill the file_operations struct
.owner = THIS_MODULE,
.read = light_read,
.write = light_write,
.ioctl = light_ioctl,
.open = light_open,
.release = light_release,
};
static void light_setup_cdev(struct light_dev * dev , int index)
{
dev_t devno = MKDEV(light_major , index); //make a device number and save it in a 32-intergred
int err = 0;
cdev_init(&dev->cdev , &light_fops ); //only fill the struct cdev
dev->cdev.owner = THIS_MODULE;
dev->cdev.ops = &light_fops; // Re
if(( err = cdev_add(&dev->cdev , devno , 1) ) == 1){ // some key !!!!!!!!!!!!!!!!
printk(KERN_NOTICE " Error adding LED ");
}
}
int light_init(void){
int result = 0;
dev_t devno = 0;
devno = MKDEV(light_major,0);
if(light_major){
if(( result = register_chrdev_region(devno,1,"LED")) < 0){
printk(KERN_NOTICE "add a register item failed");
printk(DEVICE_NAME "Allocate the device nomber failed!~");
return result;
}
}else{
if( ( result = alloc_chrdev_region(&devno,0,1,"LED") ) < 0 ){
printk(KERN_NOTICE "add a register item failed");
printk(DEVICE_NAME "Allocate the device number failed!~");
return result;
}
light_major = MAJOR(devno); // fill the majot_device_number to var light_major
}
//allocate zhe spcae to device struct
if( ( light_devp = kmalloc(sizeof(struct light_dev),GFP_KERNEL)) <0 ){
result = -ENOMEM;
printk(KERN_NOTICE "kmalloc failed!~");
goto failmalloc;
}
//Init the memory
memset(light_devp,0,sizeof(struct light_dev));
light_setup_cdev(light_devp,0);
light_GPIO_init();
s3c2410_gpio_setpin(S3C2410_GPF4 , 0);
s3c2410_gpio_setpin(S3C2410_GPF5 , 1);
s3c2410_gpio_setpin(S3C2410_GPF6 , 0);
s3c2410_gpio_setpin(S3C2410_GPF7 , 1);
//all successful~
return 0;
failmalloc: unregister_chrdev_region(devno,1);
printk(DEVICE_NAME "unregister~");
return result;
}
void light_cleanup(void){
cdev_del(&light_devp->cdev); //add the &light_devp->cdev to system as well,~Remember add the device point and del the device point
kfree(light_devp);
unregister_chrdev_region(MKDEV(light_major,0),1);
}
module_init(light_init);
module_exit(light_cleanup);
MODULE_AUTHOR("Cz.");
MODULE_LICENSE("Dual BSD/GPL");
Makefile:
#################################################
obj-m := ToggleLed.o
KERNELDIR := /usr/local/arm/3.4.1/arm-linux/yle2440_2.6.12
default:
$(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules
install:
insmod LED.ko
uninstall:
rmmod LED.ko
#################################################