Re: implementing ioctl's for device drivers

From: M.Kmann (glumpux_at_yahoo.de)
Date: 04/30/05

  • Next message: Bob Smith: "Re: LCD Emulator"
    Date: Sat, 30 Apr 2005 09:24:46 +0200
    
    

    hy sapna, here is a working ioctl example with copy_to_user():

    // our magic number for our IOCTLs
    #define FBUS_IOC_MAGIC 'L'
    // the LOWEST nr we define here, 0x20
    #define FBUS_IOC_OFFSET 0x20
    // the REAL maximum nr we have defined. absolute maximum is 0x3f
    #define FBUS_IOC_MAXNR 0x21
    // here we define our IOCTLs, Numbers Range from 0x20-0x21:
    #define FBUS_IOCTL_CPLD_W _IOW(FBUS_IOC_MAGIC, 0x20, data_t *)
    #define FBUS_IOCTL_CPLD_R _IOR(FBUS_IOC_MAGIC, 0x21, data_t *)

    #define EPRINT(args...) do { if (PRINTERRORS) { \
                                    printk(KERN_INFO DEVNAME ": " args); \
                                 } \
                            } while(0);

    static int fbus_ioctl(struct inode *ip, struct file *filp, unsigned int
    cmd, unsigned long data)
    {
        // this variable is needed for the IOCTLs which exchange data with
    the chip
        fbus_t *bus = filp->private_data;

        // plausibilittschecks:
        if (_IOC_TYPE(cmd) != FBUS_IOC_MAGIC) return -EINVAL;
        if (_IOC_NR(cmd) < FBUS_IOC_OFFSET) return -EINVAL;
        if (_IOC_NR(cmd) > FBUS_IOC_MAXNR) return -EINVAL;

        switch(cmd) {
        case FBUS_IOCTL_CPLD_W: { // write data from userspace to here
            data_t lb;
            copy_from_user(&lb, (data_t *)data, sizeof(data_t));
            /*
             * now your data is here in the kernelspace variable lb. use it:
             */
            break;
        }
        case FBUS_IOCTL_CPLD_R: { // copy data from here to userspace
            data_t lb;
            /*
             * set the values in "lb", then copy it to userspace:
             */
            copy_to_user((data_t *)data, &lb, sizeof(data_t));
            break;
        }
        default:
            EPRINT("unknown IOCTL-nr. 0x%04x\n", _IOC_NR(cmd));
            return -EINVAL;
        }

        return 0;
    }


  • Next message: Bob Smith: "Re: LCD Emulator"

    Relevant Pages