[SOLVED] how to write on a device file using copy_from_user()
Linux - KernelThis forum is for all discussion relating to the Linux kernel.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
hi have gone through your links and had made changes like kamlloc(20* sizeof(char),GFP_KERNEL) and copy_from_user(msg_ptr,buf,count). but still theer is the same problem . could please reply any more suggestions
ssize_t dev_write(struct file *filp,const char *buf,size_t count,loff_t *offset)
//msg_ptr = kmalloc(count,GFP_KERNEL);
j = copy_from_user(chr_arr.array,buf,count);
static int dev_open(struct inode *inode,struct file *filp)
filp->private_data = inode->i_cdev;
printk(KERN_INFO " could not hold semaphore");
//printk(KERN_ALERT"ah ha the device is open !now we can go further");
static int dev_release(struct inode *inode,struct file *filp)
static int init_device(void)
result = alloc_chrdev_region(&dev_no,0,1,"chr_dev");
if(result < 0)
printk("sorry no major number left");
major = MAJOR(dev_no);
dev = MKDEV(major,0);
cdev = cdev_alloc();
cdev->ops = &dev_ops;
printk("the major number allocated is %d\n",major);
result = cdev_add(cdev,dev,1);
if(result < 0 )
printk(KERN_INFO "Unable to allocate cdev");
/*dev_t dev =0;
dev_major = register_chrdev(dev,"chr_dev",&dev_ops);
printk(KERN_ALERT"MAJOR NUMBER IS:%d and minor is:%d",dev_major,dev_minor);*/
static void clean_device(void)
//dev_t devno = MKDEV(dev_major, dev_minor);
//printk(KERN_ALERT"removing MAJOR NUMBER IS:%d and removing minor is:%d",dev_major,dev_minor);
I have following the book ldd (and i think this example is taken from there). its bit complex. i just wanted to see how we can use copy_to_user and copy_from_user for reading and writing . I have solved my problem of writing function as now i am return count and putting last bit to null in array. it is as follow what i have done.
but now the problem lies in read function now whenever i try to cat /dev/my_dev it runs for infintely.further for bsat, i have checked the major numbers from /var/log/messages and its not a problem. further any help would be great.