outb unable to glow leds.
Hi,
This is the code for the driver which i wrote :
//-----------------------------------------------------------/
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/init.h>
#include<linux/fs.h>
#include<linux/types.h>
#include<linux/kdev_t.h>
#include<linux/cdev.h>
#include<asm/uaccess.h>
#include<linux/sched.h>
#include<linux/ioport.h>
#include<linux/interrupt.h>
#include<asm/io.h>
#define PORT 0x378
unsigned int SHORT_IRQ = 7;
unsigned int check_reg = 0;
MODULE_LICENSE("GPL");
dev_t dev;//DEVICE_NUMBER
unsigned char *short_buffer = 0;
// Short's interrupt handler
irqreturn_t short_interrupt_handler (int irq, void *dev_id)
{
printk (KERN_ALERT "Short recieved an interrupt");
return 0;
}
/////////////////////////////
int short_open(struct inode *inode , struct file *filp)
{
short_buffer = kmalloc(1,GFP_KERNEL);
if(short_buffer == 0)
{
printk(KERN_ALERT "Unable to allocate memory");
return -1;
}
// Requesting Port
if(!request_region(PORT,1,"short"))
{
printk(KERN_INFO "short unable to use 0x378 Port");
return -1;
}
else
{
printk(KERN_INFO "short can now use 0x378 Port");
check_reg = 1;
}
////////////////////////////////////////
// Requesting the IRQ Number
if(request_irq(SHORT_IRQ , short_interrupt_handler , SA_INTERRUPT , "short" , NULL))
{
printk(KERN_INFO "short can't get assigned IRQ\n");
SHORT_IRQ = 0;
kfree(short_buffer);
release_region(PORT,1);
return -1;
}
else
{
printk("short is now enabling pnp: 0:7 device");
outb(0x10 , PORT+2);
}
////////////////////////////
return 0;
}
int short_release(struct inode * inode , struct file *filp)
{
if(short_buffer)
{
kfree(short_buffer);
}
if(check_reg)
release_region(PORT,1);
if(SHORT_IRQ == 7)
free_irq(SHORT_IRQ , NULL);
return 0;
}
ssize_t short_read(struct file *filp , char __user *user_buffer , size_t count , loff_t *f_pos)
{
int result = -1;
if(count != 1)
{
printk(KERN_ALERT "Read request failed due to improper count parameter passed");
return result;
}
if(short_buffer)
{
result = copy_to_user(user_buffer , short_buffer , count);
if(result)
{
return -1;
}
else
{
printk(KERN_INFO "\nProcess issued Read :%s\n",current->comm);
}
}
return count;
}
ssize_t short_write(struct file *filp , const char __user *user_buffer , size_t count , loff_t *f_pos)
{
int result = -1;
if(count != 1)
{
printk(KERN_ALERT "Write request failed due to improper count parameter passed");
return result;
}
else
{
printk(KERN_ALERT "Write request passed count parameter");
}
if(short_buffer)
{
result = copy_from_user(short_buffer, user_buffer , count);
if(result)
{
printk(KERN_ALERT "Write request failed due to copy_from_user");
return -1;
}
else
{
printk(KERN_INFO "\nProcess issued Write:%s\n",current->comm);
}
//Writing to port
outb(*short_buffer,PORT);
//////////////////
}
return count;
}
// Registering the file operations with VFS
struct file_operations short_fops = {
.owner = THIS_MODULE,
.read = short_read,
.write= short_write,
.open= short_open,
.release=short_release,
};
static int __init short_init(void)
{
int result;
struct cdev *short_cdev;//For registering the device
// Getting a Major Number Dynamically .....
result = alloc_chrdev_region(&dev,0,1,"short");
if(result < 0)
{
printk(KERN_WARNING "short did'nt got any Major number");
return result;
}
else
{
printk(KERN_INFO "short got Major Number as %d",MAJOR(dev));
}
/////////////////////////////////////////
// Registering the short
short_cdev = cdev_alloc();
short_cdev->owner = THIS_MODULE;
short_cdev->ops = &short_fops;
result = cdev_add(short_cdev,dev,1);
if(result)
{
printk(KERN_NOTICE "short unable to add");
return -1;
}
////////////////////////////////////////
return 0;
}
static void __exit short_exit(void)
{
unregister_chrdev_region(dev,1);
}
module_init(short_init);
module_exit(short_exit);
//----------------------------------------------------------/
1)Initially I loaded the module short.ko using insmod
2)made the file with Major Number received dynamically.
3)rmmoded the parport & parportpc.
4)then I wrote an application program that wud write on this file.
5)the printk(KERN_INFO "\nProcess issued Write:%s\n",current->comm); line
in short_write got executed but Led's did'nt glow.
Can u plz help me out.
|