[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 1316 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
What are your first memories of when Nepal Television Began?
निगुरो थाहा छ ??
TPS Re-registration case still pending ..
Basnet or Basnyat ??
Sajha has turned into MAGATs nest
NRN card pros and cons?
Nas and The Bokas: Coming to a Night Club near you
Will MAGA really start shooting people?
मन भित्र को पत्रै पत्र!
Top 10 Anti-vaxxers Who Got Owned by COVID
काेराेना सङ्क्रमणबाट बच्न Immunity बढाउन के के खाने ?How to increase immunity against COVID - 19?
TPS Work Permit/How long your took?
Breathe in. Breathe out.
3 most corrupt politicians in the world
Guess how many vaccines a one year old baby is given
अमेरिकामा बस्ने प्राय जस्तो नेपालीहरु सबै मध्यम बर्गीय अथवा माथि (higher than middle class)
चितवनको होस्टलमा १३ वर्षीया शालिन पोखरेल झुण्डिएको अवस्था - बलात्कार पछि हत्याको शंका - होस्टेलहरु असुरक्षित
शीर्षक जे पनि हुन सक्छ।
Disinformation for profit - scammers cash in on conspiracy theories
someone please tell me TPS is here to stay :(
Nas and The Bokas: Coming to a Night Club near you
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