<br><br><div class="gmail_quote">On Thu, Dec 23, 2010 at 10:05 AM, Srinivas G. <span dir="ltr"><<a href="mailto:srinivasg@esntechnologies.co.in">srinivasg@esntechnologies.co.in</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">> On 12/22/2010 5:59 PM, Hemanth Kumar wrote:<br>
> > Hi All,<br>
> ><br>
> > I have small problem with copy_to_user in read<br>
> function,below is my code,when I try to read from userspace I get<br>
> segmentation fault,<br>
> > Can any please point me where I went wrong,<br>
<br>
</div>I guess, you need to also implement open method in your driver. Because,<br>
you are opening the device in your application. Could you try this?<br>
<br>
Regards,<br>
<font color="#888888">Srinivas G<br>
</font><div><div></div><div class="h5"><br>
> ><br>
> ><br>
> > #include<linux/kernel.h><br>
> > #include<linux/module.h><br>
> > #include<linux/init.h><br>
> > #include<linux/types.h><br>
> > #include<linux/proc_fs.h><br>
> > #include<linux/fs.h><br>
> > #include<linux/kdev_t.h><br>
> > #include<linux/jiffies.h><br>
> > #include<linux/cdev.h><br>
> > #include<asm/uaccess.h><br>
> > #include<linux/mutex.h><br>
> ><br>
> > struct mutex timer;<br>
> > static struct cdev my_cdev;<br>
> > dev_t devn;<br>
> > int maj = 300;<br>
> > int min = 0;<br>
> > int count = 1;<br>
> > char modname[] = "mytimer";<br>
> > short x[10] = {1,2,3,4,5,6,7,8,9,10};<br>
> ><br>
> ><br>
> ><br>
> ><br>
> > ssize_t my_read(struct file *file,char *buf,size_t count,loff_t<br>
> *pos){<br>
> > unsigned long res;<br>
> > void *k = (void *)&x;<br>
> > void *l = (void *)&x+1;<br>
> > void *j = (void *)&x+2;<br>
> ><br>
> > mutex_lock(&timer);<br>
> > res =<br>
> copy_to_user(buf,k,sizeof(short));<br>
> > res =<br>
> copy_to_user(buf,l,sizeof(short));<br>
> > res =<br>
> copy_to_user(buf,j,sizeof(short));<br>
> ><br>
> > /* res =<br>
> copy_to_user(buf,&x+4,sizeof(short));<br>
> > res =<br>
> copy_to_user(buf,&x+5,sizeof(short));<br>
> > res =<br>
> copy_to_user(buf,&x+6,sizeof(short));<br>
> > res =<br>
> copy_to_user(buf,&x+7,sizeof(short));<br>
> > res =<br>
> copy_to_user(buf,&x+8,sizeof(short));<br>
> > res =<br>
> copy_to_user(buf,&x+9,sizeof(short));<br>
> > */<br>
> > mutex_unlock(&timer);<br>
> ><br>
> > return 20;<br>
> ><br>
> > }<br>
> ><br>
> ><br>
> > static struct file_operations my_fops = {<br>
> > .owner = THIS_MODULE,<br>
> > .read = my_read,<br>
> ><br>
> > };<br>
> ><br>
> ><br>
> > static int __init my_init(void){<br>
> > int ret;<br>
> > devn = MKDEV(maj,min);<br>
> ><br>
> > ret = register_chrdev_region(devn,count,modname);<br>
> ><br>
> > cdev_init(&my_cdev,&my_fops);<br>
> > cdev_add(&my_cdev,devn,count);<br>
> ><br>
> > printk("<1> Register timer maj = %d\n",maj);<br>
> ><br>
> ><br>
> ><br>
> ><br>
> > return 0;<br>
> > }<br>
> ><br>
> ><br>
> ><br>
> > static void __exit my_exit(void){<br>
> ><br>
> > cdev_del(&my_cdev);<br>
> > unregister_chrdev_region(devn,count);<br>
> > printk("<1> Bye Bye \n");<br>
> ><br>
> > }<br>
> ><br>
> ><br>
> > module_init(my_init);<br>
> > module_exit(my_exit);<br>
> > MODULE_LICENSE("Dual BSD/GPL");<br>
> ><br>
> ><br>
> ><br>
> ><br>
> > my userspace App:<br>
> ><br>
> > #include<stdio.h><br>
> > #include<fcntl.h><br>
> > #include<stdlib.h><br>
> > #include<unistd.h><br>
> ><br>
> > int main()<br>
> > {<br>
> > int nbytes ;<br>
> > char n[20];<br>
> > short a = *((short *)&n[0]);<br>
> > short b = *((short *)&n[2]);<br>
> > short c = *((short *)&n[4]);<br>
> ><br>
> > int fd = open( "/dev/mytimer", O_RDONLY );<br>
> > if ( fd< 0 ) { perror( "/dev/mytimer" ); exit(1); }<br>
> ><br>
> > while ( 1 )<br>
> > {<br>
> ><br>
> > nbytes = read( fd, n, 40 );<br>
> > if ( nbytes< 0 ) break;<br>
> ><br>
> > printf( "\r a = %d \n ", a);<br>
> > printf("\r b = %d \n",b);<br>
> > printf("\r c = %d \n",c);<br>
> ><br>
> > sleep(1);<br>
> > fflush( stdout );<br>
> > }<br>
> > return 0;<br>
> > }<br>
> ><br>
> ><br>
> ><br>
> ><br>
> > Best regards,<br>
> ><br>
> ><br></div></div></blockquote><div>The mutex initialization is missing. <br>Adding mutex_init(&timer); in the driver init will make it work.<br><br>Thanks,<br>Mukti<br></div><br></div><br>