linux 裝置驅動開發詳解 code (1)

來源:互聯網
上載者:User

1. c code:

 

#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>

#define GLOBALMEM_SIZE  0x1000
#define MEM_CLEAR       0x01
#define GLOBALMEM_MAJOR 254

static globalmem_major = GLOBALMEM_MAJOR;

struct globalmem_dev
{
    struct cdev cdev;
    u8 mem[GLOBALMEM_SIZE];
};

struct globalmem_dev dev;

static ssize_t globalmem_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
{
    u32 p = *ppos;
    int ret = 0;
   
    if (p >= GLOBALMEM_SIZE) {
        return count ? -ENXIO : 0;
    }
   
    if (count > GLOBALMEM_SIZE - p) {
        count = GLOBALMEM_SIZE -p;
    }

    if (copy_to_user(buf, (void *)(dev.mem + p), count)) {
        ret = -EFAULT;
    } else {
        *ppos += count;
        ret = count;
        printk(KERN_INFO "read %d byte(s) from %d/n", count, p);
    }
   
    return ret;
}
   
static ssize_t globalmem_write(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
{
    u32 p = *ppos;
    int ret = 0;
   
    if (p >= GLOBALMEM_SIZE) {
        return count ? -ENXIO : 0;
    }

    if (count > GLOBALMEM_SIZE -p) {
        count = GLOBALMEM_SIZE -p;
    }

    if (copy_from_user(dev.mem + p, buf, count)) {
        ret = -EFAULT;
    } else {
        *ppos += count;
        ret = count;
        printk(KERN_INFO "written %d byte(s) from %d/n", count, p);
    }
   
    return ret;
}

static loff_t globalmem_llseek(struct file *filp, loff_t offset, int orig)
{
    loff_t ret;
    switch (orig)
    {
      case 0:
        if (offset < 0) {
            ret = -EINVAL;
            break;
        }
       
        if ((u32)offset > GLOBALMEM_SIZE) {
            ret - filp->f_pos;
            break;
        }
       
        filp->f_pos = (u32)offset;
        ret = filp->f_pos;
        break;

      case 1:
        if ((filp->f_pos + offset) > GLOBALMEM_SIZE) {
            ret = -EINVAL;
            break;
        }

        if ((filp->f_pos + offset) < 0) {
            ret = -EINVAL;
            break;
        }

        filp->f_pos += offset;
        ret = filp->f_pos;
        break;

      default:
        ret = -EINVAL;
    }
    return ret;
}

static int globalmem_ioctl(struct inode *inodep, struct file *filp, u32 cmd, u32 arg)
{
    switch (cmd)
    {
      case MEM_CLEAR:
        memset(dev.mem, 0, GLOBALMEM_SIZE);
        printk(KERN_INFO "globalmem is set to zero/n");
        break;

      default:
        return -EINVAL;
    }
   
    return 0;
}

static const struct file_operations globalmem_fops =
{
        .owner = THIS_MODULE,
        .llseek = globalmem_llseek,
        .read = globalmem_read,
        .write = globalmem_write,
        .ioctl = globalmem_ioctl,
};

static void globalmem_setup_cdev()
{
        int err, devno = MKDEV(globalmem_major, 0);

        cdev_init(&dev.cdev, &globalmem_fops);
        dev.cdev.owner = THIS_MODULE;
        dev.cdev.ops = &globalmem_fops;
        err = cdev_add(&dev.cdev, devno, 1);
        if (err) {
                printk(KERN_NOTICE "Error %d adding globalmem", err);
        }
}

int globalmem_init(void)
{
        int result;
        dev_t devno = MKDEV(globalmem_major, 0);

        if (globalmem_major) {
                result = register_chrdev_region(devno, 1, "globalmem");
        } else {
                result = alloc_chrdev_region(&devno, 0, 1, "globalmem");
        }

        if (result < 0) {
                return result;
        }

        globalmem_setup_cdev();
        return 0;
}

void globalmem_exit(void)
{
        cdev_del(&dev.cdev);
        unregister_chrdev_region(MKDEV(globalmem_major, 0), 1);
}

MODULE_AUTHOR("Alex Xia");
MODULE_LICENSE("Dual BSD/GPL");

module_init(globalmem_init);
module_exit(globalmem_exit);

2.run.sh:

 

#!/bin/sh
make -C /lib/modules/2.6.31-22-generic/build/ M=$(pwd) modules

 

 

3.clean.sh:

 

#!/bin/sh
rm *.mod.c *.o *.ko *.mar* *.sym* *.ord*     
       
           

   

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.