LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 04-18-2014, 02:21 PM   #16
hemanth1989
Member
 
Registered: Dec 2013
Posts: 39

Rep: Reputation: Disabled

could you please provide the working solution of this question ??
do I want to use major, minor number and dev_open function ??
 
Old 04-18-2014, 02:25 PM   #17
hemanth1989
Member
 
Registered: Dec 2013
Posts: 39

Rep: Reputation: Disabled
could you please provide the working solution for this question ??
Do I want to use major number, minor number and device open function ??

http://www.linuxquestions.org/questi...fs-4175502026/


#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/fs.h>
#include<linux/cdev.h>
#include<asm/uaccess.h>
#include<linux/semaphore.h>



struct cdev *cdev;

struct device {
char array[100];
}chr_arr;

struct file_operations dev_ops = {
.owner = THIS_MODULE,
.read = dev_read,
.write = dev_write,
.open = dev_open,
.release = dev_release
};

ssize_t dev_read(struct file *filp,char *buf,size_t count,loff_t *offset)
{
int i;
i=copy_to_user(buf,chr_arr.array,count);
printk(KERN_ALERT"buff:%s",buf);
return i;
}

ssize_t dev_write(struct file *filp,const char *buf,size_t count,loff_t *offset)
{
//printk(KERN_ALERT"\nsorry,byebye");
int j;
//msg_ptr = kmalloc(count,GFP_KERNEL);
//for(j=0;j<count;j++)
if(count>100)
return -1;
j = copy_from_user(chr_arr.array,buf,count);
//printk(KERN_ALERT"msg_ptr:%s",msg_ptr);
return j;
}

static int dev_release(struct inode *inode,struct file *filp)
{
up(&chr_arr.sem);
return 0;
//module_put(THIS_MODULE);

return 0;
}
static int init_device(void)
{
int result;
dev_t dev_no,dev;
result = alloc_chrdev_region(&dev_no,0,1,"chr_dev");
if(result < 0)
{
printk("sorry no major number left");
return result;
}
major = MAJOR(dev_no);
dev = MKDEV(major,0);
cdev = cdev_alloc();
cdev->ops = &dev_ops;
sema_init(&chr_arr.sem,1);
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");
return result;
}
/*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);*/
return 0;
}

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);
cdev_del(cdev);
unregister_chrdev_region(major,1);

}
module_init(init_device);
module_exit(clean_device);


if I do only the above code then isnt it good ??

Last edited by hemanth1989; 04-18-2014 at 02:31 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
copy_from_user strangeness spong Linux - Kernel 6 02-10-2014 09:06 AM
C copy_from_user question conlonloi Programming 1 10-22-2011 11:49 PM
Using copy_from_user with 2 process wk30l Programming 1 10-25-2010 02:09 PM
copy_from_user and copy_to_user example krisonearth Programming 5 09-29-2009 05:08 PM
copy_from_user does not copy from process-heap najoshi Linux - Kernel 0 08-16-2009 12:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 11:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration