[Show all top banners]

codeme

More by codeme
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 plz help in Device Driver code
[VIEWED 1552 TIMES]
SAVE! for ease of future access.
Posted on 05-02-12 10:33 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

I have to write a program in c or c++  for Device driver. It should open, read, write and excute. so far I have come up with these.
if anybody know any links that can help me to write it or anybody know the actual program plz help me . it is very important to me.
Thanks in advance.


// Header files needed to load the driver
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#DEFINE BUFF_SIZE 16

MODULE_AUTHOR("OS student");
MODULE_DESCRIPTION("Box's assignment");

static int hari_open = 0;
static char buff_size[BUFF_SIZE];
static int index = 0;

static char *DEVICE_NAME = "hari";
static int major_no = 0;

struct file_operations fops = {
read:
    hari_read,
write:
    hari_write,
open:
    hari_open,
release:
    hari_release
};

int hari_open(struct inode *inode, struct file *file) {
    if (hari_open) {
        // Allowing only one process to open this device at a time.
        return -EBUSY;
    }

    hari_open++;

    // This will prevent rmmod to successfully remove
    // this module when the device is in use.
    try_module_get(THIS_MODULE);

    // on open, reset the read index to 0
    index = 0;

    return 0;
}

int hari_release(struct inode *inode, struct file *file) {
    hari_open--;

    // releaseing this module so an admin may rmmod it.
    module_put(THIS_MODULE);

    return 0;
}

ssize_t hari_read(struct file *f, char *buffer, size_t length, loff_t *offset) {
    int i;
    int bytes_read = 0;

    // This will just simply copy device buffer to the reader

    for(i = 0; index < BUFF_SIZE && i < length; i++) {
        buffer[i] = box_buff[index];
        index++;
        bytes_read++;
    }

    return bytes_read;
}

ssize_t hari_write(struct file *f, const char *buffer, size_t length, loff_t *offset) {
    // This will just overwrite stuffs to the buffer
    //  but the device will not reset the index everytime the 'hari_write'
    //  is called, resulting in writing only at maximum of 16 bytes
    //  on each open.
    //  (a little bit different from
    //  what I showed in the class where we can write 17 bytes
    //  per one open -- with automatic two writes by system)

    // Feel free to modify this to match the assignment requirement.

    int bytes_written = 0;
    int i;

    for(i = 0; index < BUFF_SIZE && i < length; i++) {
        box_buff[index++] = buffer[i];
        bytes_written++;
    }

    if(bytes_written == 0) {
        // We cannot write anything here ...
        // meaning that the buffer is full :)
        // return error
        return -ENOSPC; // no space left
        // NOTE: please see 'man errno' for more information
        // you may change this to some other error as you see fit.
    }

    // if the device can write something, return the number of written bytes.
    return bytes_written;
}

int init_module(void) {
    int i;
    printk(KERN_INFO "DEVICE: module initialization\n");

    // initializing the device buffer
    for (i = 0; i < BUFF_SIZE; i++) {
        box_buff[i] = 'a';
    }

    // registering device ... if the major number 999 has been occupied,
    //  the register_chrdev function will fail (see messages in
    //  /var/log/kern.log for Ubuntu; please note that the kernel message
    //  can be stored in different location, depending on the system
    //  you're using)
    major_no = register_chrdev(999, DEV_NAME, &fops);

    if (major_no < 0) {
        printk(KERN_ALERT "Registering virtual device failed with %d\n", major_no);
        return major_no;
    }

    return 0;
}

void cleanup_module() {
    printk(KERN_INFO "DEVICE: Unloaded\n");

    // Finally, don't forget to unregister the device ... or you will not
    //  be able to insmod your module again.
    unregister_chrdev(999, DEV_NAME);
}

 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 90 days
Recommended Popular Threads Controvertial Threads
TPS To F-1 COS
TPS to F1 Status.
Nepal TPS has been Extended !!!
Got my F1 reinstatement approved within 3 months(was out of F1 for almost 2 years)
ICE kidnapping people off the streets over op eds
#MAGA#FAFO is delicious
TPS of Nepal to be automatically extended for 6 months based on South Sudan decision
Nepal TPS decision
Has anyone here successfully reinstated to F-1 status after a year-long gap following a drop from F-1?
US citizen Petitioning my wife who was out of status when she was in H1B. What to do ?
TPS Sakiyo Tara Case is in Court.
Supreme Court allows Trump to end TPS for Venezuelans
Genuine Question.... Why so many folks still in TPS after 10 years. Is the statistics wrong?
Any input on remote jobs(IT related or Sales or Marketing)?
Nepal Likely to Get 60-Day TPS Notice
मुद्दा हाल्छन होला र ?
TPS cancel bho bhane k garne?
नेपाल मा B. sc गरियो यहाँ फेरी ७० -८० क्रेडिट पढ्नु पर्ने भो
EAD HELP NEEDED URGENTLY!
Don’t Just Read—Join the Conversation
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters