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