android - Where to find or download USB Driver folder?
Driver on

linux kernel – character device driver

The learn() and write() callback capabilities in our ‘cmosram.c’ device-driver solely switch a single byte of knowledge for every time referred to as, so it takes 128 system-calls to learn the entire RTC storage-locations!

Are you able to enhance this driver’s effectivity, by modifying its learn() and write() capabilities, in order that they’ll switch as many legitimate bytes because the provided buffer’s area may maintain?

code is as follows

char modname[] = "cmosram"; // identify of this kernel module
char devname[] = "cmos";    // identify for the system's file
int my_major = 70;      // main ID-number for driver
int cmos_size = 128;    // whole bytes of cmos reminiscence
int write_max = 9;      // largest 'writable' handle

ssize_t my_read( struct file *file, char *buf, size_t len, loff_t *pos )
{
    unsigned char   datum;

    if ( *pos >= cmos_size ) return 0;

    outb( *pos, 0x70 );  datum = inb( 0x71 );

    if ( put_user( datum, buf ) ) return -EFAULT;

    *pos += 1;
    return  1;
}

ssize_t my_write( struct file *file, const char *buf, size_t len, loff_t *pos )
{
    unsigned char   datum;

    if ( *pos >= cmos_size ) return 0;

    if ( *pos > write_max ) return -EPERM;

    if ( get_user( datum, buf ) ) return -EFAULT;

    outb( *pos, 0x70 );  outb( datum, 0x71 );

    *pos += 1;
    return  1;
}

loff_t my_llseek( struct file *file, loff_t pos, int whence )
{
    loff_t  newpos = -1;

    change ( whence )
        {
        case 0: newpos = pos; break;            // SEEK_SET
        case 1: newpos = file->f_pos + pos; break;  // SEEK_CUR
        case 2: newpos = cmos_size + pos; break;    // SEEK_END
        }

    if (( newpos < 0 )||( newpos > cmos_size )) return -EINVAL;

    file->f_pos = newpos;
    return  newpos;
}


struct file_operations my_fops = {
                proprietor:  THIS_MODULE,
                llseek: my_llseek,
                write:  my_write,
                learn:   my_read,
                };

static int __init my_init( void )
{
    printk( "<1>nInstalling '%s' module ", devname );
    printk( "(main=%d) n", my_major );
    return  register_chrdev( my_major, devname, &my_fops );
}

static void __exit my_exit(void )
{
    unregister_chrdev( my_major, devname );
    printk( "<1>Eradicating '%s' modulen", devname );
}

module_init( my_init );
module_exit( my_exit );
MODULE_LICENSE("GPL"); 

Leave a Reply

Your email address will not be published. Required fields are marked *