|
看着创龙的驱动教程,写了一个led的驱动,板子的led是gpio3_19,我这个哪里出了问题,想要询问一下- static struct class *my_dev_class;
- static struct device *my_dev_class_dev;
- static char val;
- static int gpio_led_pin = 126;
- static int my_dev_open(struct inode *inode, struct file *file)
- {
- return 0;
- }
- static ssize_t my_dev_write(struct file *file, const char __user *buf,
- size_t count, loff_t *offset)
- {
- #if 0
- int ret;
- ret = copy_from_user(&val, buf, count);
- if(ret<0)
- {
- printk("ret = %d \n",ret);
- return ret;
- }
- printk("my_dev_write\n");
- return 0;
- #endif
- int ret;
- char temp[16];
- char status = 0;
- memset(temp,0,16);
- ret = copy_from_user(temp, buf, count);
- if(ret<0)
- {
- printk("ret = %d \n",ret);
- status = -EFAULT;
- goto err;
- }
- val = simple_strtoul(temp,NULL,0);
- if(val >= 1)
- gpio_set_value(gpio_led_pin,1);
- else
- gpio_set_value(gpio_led_pin,0);
- if(!status)
- status = count;
- err:
- return status;
- }
- static ssize_t my_dev_read(struct file *file, char __user *buf, size_t count,
- loff_t *offset)
- {
- #if 0
- int ret;
- ret = copy_to_user(buf, &val, count);
- if(ret<0)
- {
- printk("ret %d \n",ret);
- return ret;
- }
- printk("my_dev_read\n");
- return 0;
- #endif
- int ret;
- char temp[16];
- char status;
- if(*offset>0)
- return 0;
- count = sprintf(temp, "%u\n", val);
- ret = copy_to_user(buf, temp, count+1);
- if(ret<0)
- {
- printk("ret = %d \n",ret);
- status = -EFAULT;
- }
- else{
- *offset += count;
- status = count;
- }
- return status;
- }
- static const struct file_operations my_dev_fops = {
- .owner = THIS_MODULE,
- .open = my_dev_open,
- .read = my_dev_read,
- .write = my_dev_write,
- };
- int major;
- static int my_dev_init(void)
- {
- if(gpio_request(gpio_led_pin,"gpio_led_pin") != 0)
- printk(KERN_ALERT "GPIO request err!\n");
- gpio_direction_output(gpio_led_pin,1);
- printk(KERN_ALERT "LED install!\n");
- major = register_chrdev(0,"my_led_dev",&my_dev_fops);
- my_dev_class = class_create(THIS_MODULE, "my_led_dev"); // /sys/class/my_dev
- my_dev_class_dev = device_create(my_dev_class, NULL, MKDEV(major,0), NULL, "my_led_dev"); // /dev/my_led_dec
- return 0;
- };
- static void my_dev_exit(void)
- {
- gpio_free(gpio_led_pin);
- device_unregister(my_dev_class_dev);
- class_destroy(my_dev_class);
- unregister_chrdev(major, "my_led_dev");
- printk(KERN_ALERT "LED unstall!\n");
- }
复制代码
|
|