⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 本源码是将述嵌入式LINUX驱动程序例程
💻 C
📖 第 1 页 / 共 2 页
字号:
        if (!dptr->data[s_pos])
            goto nomem;
        memset(dptr->data[s_pos], 0, scullc_quantum);
    }
    if (count > quantum - q_pos)
        count = quantum - q_pos; /* write only up to the end of this quantum */
    if (copy_from_user (dptr->data[s_pos]+q_pos, buf, count)) {
        retval = -EFAULT;
        goto nomem;
    }
    *f_pos += count;
 
    /* update the size */
    if (dev->size < *f_pos)
        dev->size = *f_pos;
    up (&dev->sem);
    return count;

  nomem:
    up (&dev->sem);
    return retval;
}

/*
 * The ioctl() implementation
 */

int scullc_ioctl (struct inode *inode, struct file *filp,
                 unsigned int cmd, unsigned long arg)
{

    int err= 0, ret = 0, tmp;

    /* don't even decode wrong cmds: better returning  ENOTTY than EFAULT */
    if (_IOC_TYPE(cmd) != SCULLC_IOC_MAGIC) return -ENOTTY;
    if (_IOC_NR(cmd) > SCULLC_IOC_MAXNR) return -ENOTTY;

    /*
     * the type is a bitmask, and VERIFY_WRITE catches R/W
     * transfers. Note that the type is user-oriented, while
     * verify_area is kernel-oriented, so the concept of "read" and
     * "write" is reversed
     */
    if (_IOC_DIR(cmd) & _IOC_READ)
        err = !access_ok(VERIFY_WRITE, (void *)arg, _IOC_SIZE(cmd));
    else if (_IOC_DIR(cmd) & _IOC_WRITE)
        err =  !access_ok(VERIFY_READ, (void *)arg, _IOC_SIZE(cmd));
    if (err) return -EFAULT;

    switch(cmd) {

      case SCULLC_IOCRESET:
        scullc_qset = SCULLC_QSET;
        scullc_quantum = SCULLC_QUANTUM;
        break;
        
      case SCULLC_IOCSQUANTUM: /* Set: arg points to the value */
        ret = __GET_USER(scullc_quantum, (int *) arg);
        break;

      case SCULLC_IOCTQUANTUM: /* Tell: arg is the value */
        scullc_quantum = arg;
        break;

      case SCULLC_IOCGQUANTUM: /* Get: arg is pointer to result */
        ret = __PUT_USER (scullc_quantum, (int *) arg);
        break;

      case SCULLC_IOCQQUANTUM: /* Query: return it (it's positive) */
        return scullc_quantum;

      case SCULLC_IOCXQUANTUM: /* eXchange: use arg as pointer */
        tmp = scullc_quantum;
        ret = __GET_USER(scullc_quantum, (int *) arg);
        if (ret == 0)
            ret = __PUT_USER(tmp, (int *) arg);
        break;

      case SCULLC_IOCHQUANTUM: /* sHift: like Tell + Query */
        tmp = scullc_quantum;
        scullc_quantum = arg;
        return tmp;

      case SCULLC_IOCSQSET:
        ret = __GET_USER(scullc_qset, (int *) arg);
        break;

      case SCULLC_IOCTQSET:
        scullc_qset = arg;
        break;

      case SCULLC_IOCGQSET:
        ret = __PUT_USER(scullc_qset, (int *)arg);
        break;

      case SCULLC_IOCQQSET:
        return scullc_qset;

      case SCULLC_IOCXQSET:
        tmp = scullc_qset;
        ret = __GET_USER(scullc_qset, (int *) arg);
        if (ret == 0)
            ret = __PUT_USER(tmp, (int *)arg);
        break;

      case SCULLC_IOCHQSET:
        tmp = scullc_qset;
        scullc_qset = arg;
        return tmp;

      default:  /* redundant, as cmd was checked against MAXNR */
        return -ENOTTY;
    }

    return ret;

}

/*
 * The "extended" operations
 */

loff_t scullc_llseek (struct file *filp, loff_t off, int whence)
{
    ScullC_Dev *dev = filp->private_data;
    long newpos;

    switch(whence) {
      case 0: /* SEEK_SET */
        newpos = off;
        break;

      case 1: /* SEEK_CUR */
        newpos = filp->f_pos + off;
        break;

      case 2: /* SEEK_END */
        newpos = dev->size + off;
        break;

      default: /* can't happen */
        return -EINVAL;
    }
    if (newpos<0) return -EINVAL;
    filp->f_pos = newpos;
    return newpos;
}
 
/*
 * The 2.0 wrappers
 */
#ifdef LINUX_20

int scullc_lseek_20 (struct inode *ino, struct file *f,
                off_t offset, int whence)
{
    return (int)scullc_llseek(f, offset, whence);
}

int scullc_read_20 (struct inode *ino, struct file *f, char *buf, int count)
{
    return (int)scullc_read(f, buf, count, &f->f_pos);
}

int scullc_write_20 (struct inode *ino, struct file *f, const char *b, int c)
{
    return (int)scullc_write(f, b, c, &f->f_pos);
}

void scullc_release_20 (struct inode *ino, struct file *f)
{
    scullc_release(ino, f);
}

#define scullc_llseek scullc_lseek_20
#define scullc_read scullc_read_20
#define scullc_write scullc_write_20
#define scullc_release scullc_release_20
#define llseek lseek

#endif /* LINUX_20 */

/*
 * The fops
 */

struct file_operations scullc_fops = {
    llseek: scullc_llseek,
    read: scullc_read,
    write: scullc_write,
    ioctl: scullc_ioctl,
    open: scullc_open,
    release: scullc_release,
};

int scullc_trim(ScullC_Dev *dev)
{
    ScullC_Dev *next, *dptr;
    int qset = dev->qset;   /* "dev" is not-null */
    int i;

    if (dev->vmas) /* don't trim: there are active mappings */
        return -EBUSY;

    for (dptr = dev; dptr; dptr = next) { /* all the list items */
        if (dptr->data) {
            for (i = 0; i < qset; i++)
                if (dptr->data[i])
                    kmem_cache_free(scullc_cache, dptr->data[i]);

            kfree(dptr->data);
            dptr->data=NULL;
        }
        next=dptr->next;
        if (dptr != dev) kfree(dptr); /* all of them but the first */
    }
    dev->size = 0;
    dev->qset = scullc_qset;
    dev->quantum = scullc_quantum;
    dev->next = NULL;
    return 0;
}




/*
 * Finally, the module stuff
 */

int scullc_init(void)
{
    int result, i;

    SET_MODULE_OWNER(&scullc_fops);
    /*
     * Register your major, and accept a dynamic number
     */
    result = register_chrdev(scullc_major, "scullc", &scullc_fops);
    if (result < 0) return result;
    if (scullc_major == 0) scullc_major = result; /* dynamic */

    /* 
     * allocate the devices -- we can't have them static, as the number
     * can be specified at load time
     */
    scullc_devices = kmalloc(scullc_devs * sizeof (ScullC_Dev), GFP_KERNEL);
    if (!scullc_devices) {
        result = -ENOMEM;
        goto fail_malloc;
    }
    memset(scullc_devices, 0, scullc_devs * sizeof (ScullC_Dev));
    for (i=0; i < scullc_devs; i++) {
        scullc_devices[i].quantum = scullc_quantum;
        scullc_devices[i].qset = scullc_qset;
        sema_init (&scullc_devices[i].sem, 1);
    }

    /* init_module: create a cache for our quanta */
    scullc_cache =
	kmem_cache_create("scullc", scullc_quantum,
			  0, SLAB_HWCACHE_ALIGN,
			  NULL, NULL); /* no ctor/dtor */
    if (!scullc_cache) {
        result = -ENOMEM;
        goto fail_malloc2;
    }

#ifdef SCULLC_USE_PROC /* only when available */
    create_proc_read_entry("scullcmem", 0, NULL, scullc_read_procmem, NULL);
#endif
    return 0; /* succeed */

  fail_malloc2:
    kfree(scullc_devices);
  fail_malloc:
    unregister_chrdev(scullc_major, "scullc");
    return result;
}

void scullc_cleanup(void)
{
    int i;
    unregister_chrdev(scullc_major, "scullc");

#ifdef SCULLC_USE_PROC
    remove_proc_entry("scullcmem", 0);
#endif

    for (i=0; i<scullc_devs; i++)
        scullc_trim(scullc_devices+i);
    kfree(scullc_devices);

    /* cleanup_module: release the cache of our quanta */
    kmem_cache_destroy(scullc_cache);
}

#endif /* not linux-2.0 */

module_init(scullc_init);
module_exit(scullc_cleanup);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -