Hi,<div>I am new to kernel device drivers.</div><div><br></div><div>I wrote a simple character driver built statically having the fops as shown:</div><div><br></div><div><div><b>static const struct file_operations mfcfpga65_usb_fops = {</b></div>
<div><b><span class="Apple-tab-span" style="white-space:pre">        </span>.owner<span class="Apple-tab-span" style="white-space:pre">                </span>= THIS_MODULE,</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>.open<span class="Apple-tab-span" style="white-space:pre">                </span>= mfcfpga65_usb_open,</b></div>
<div><b><span class="Apple-tab-span" style="white-space:pre">        </span>.read<span class="Apple-tab-span" style="white-space:pre">                </span>= mfcfpga65_usb_read,</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>.unlocked_ioctl<span class="Apple-tab-span" style="white-space:pre">        </span>= mfcfpga65_usb_ioctl,</b></div>
<div><b><span class="Apple-tab-span" style="white-space:pre">        </span>.release<span class="Apple-tab-span" style="white-space:pre">        </span>= mfcfpga65_usb_close,</b></div><div><b>};</b></div></div><div><br></div><div>All the driver registrations are done properly.</div>
<div><br></div><div>I wrote a sample application to test these drivers implementing open,read and ioctl calls.</div><div><br></div><div>open and read are working fine whereas ioctl is not behaving as expected.</div><div><br>
</div><div>The application code is as shown:</div><div><br></div><div><div><b>int main()</b></div><div><b>{</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>printf("Enterred main function : \n");</b></div>
<div><b><span class="Apple-tab-span" style="white-space:pre">        </span>int fd=0,ret=0;</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>char buff[80]="";</b></div><div><span class="Apple-tab-span" style="white-space:pre"><b>        </b></span></div>
<div><b><span class="Apple-tab-span" style="white-space:pre">        </span>fd=open("/dev/usbmod",O_RDONLY);</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>if(fd<0)<span class="Apple-tab-span" style="white-space:pre">        </span></b></div>
<div><b><span class="Apple-tab-span" style="white-space:pre">        </span>{</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span> printf("fd value is less than 0\n");</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>}</b></div>
<div><span class="Apple-tab-span" style="white-space:pre"><b>        </b></span></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>printf("fd :%d\n",fd);</b></div><div><span class="Apple-tab-span" style="white-space:pre"><b>        </b></span></div>
<div><b><span class="Apple-tab-span" style="white-space:pre">        </span>ret=read(fd,buff,10);</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>buff[ret]='\0';</b></div><div><b><br></b></div>
<div><b> printf("The commands passed are %d and %d\n",IOCTL_USB_READ,IOCTL_USB_OPEN );</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>ret = ioctl(fd,IOCTL_USB_READ);</b></div>
<div><b><span class="Apple-tab-span" style="white-space:pre">        </span>ret = ioctl(fd,IOCTL_USB_OPEN);<span class="Apple-tab-span" style="white-space:pre">        </span></b></div><div><span style="white-space:pre"><b><br></b></span></div>
<div><b><span class="Apple-tab-span" style="white-space:pre">        </span>close(fd);</b></div><div><b>}</b></div></div><div><br></div><div>The cmds in ioctl are defined in seperate header file which is included in driver as well as in the applicatin.That definations are as shown</div>
<div><br></div><div><div><b>#define MFCFPGA65_NUM 254</b></div><div><b><br></b></div><div><b>#define IOCTL_USB_OPEN _IO(MFCFPGA65_NUM,0)</b></div><div><b>#define IOCTL_USB_READ _IO(MFCFPGA65_NUM,1)</b></div>
<div><b>#define IOCTL_USB_WRITE _IO(MFCFPGA65_NUM,2)</b></div></div><div><br></div><div><br></div><div>The IOCTL call in driver does nothing except printk in each switch case showing to which case statement it entered .This ioctls implementation in driver is as shown below:</div>
<div><br></div><div><div><b>static long mfcfpga65_usb_ioctl(struct inode *inode, struct file *file, unsigned int iocmd,</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">                                                        </span>unsigned long ioarg)</b></div>
<div><b>{</b></div><div><b> </b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>void __user *arg = (void __user *)ioarg;</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>unsigned int* stream;</b></div>
<div><b><span class="Apple-tab-span" style="white-space:pre">        </span>unsigned int size;</b></div><div><span class="Apple-tab-span" style="white-space:pre"><b>        </b></span></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>printk(KERN_INFO "In IOCTL function of USB character driver with cmd as %d : \n",iocmd);</b></div>
<div><span class="Apple-tab-span" style="white-space:pre"><b>        </b></span></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>printk(KERN_INFO "The switch case cmds are %d %d %d\n",IOCTL_USB_OPEN,IOCTL_USB_READ,IOCTL_USB_WRITE);</b></div>
<div><b><br></b></div><div><b><span class="Apple-tab-span" style="white-space:pre">        </span>switch (iocmd) {</b></div><div><b><br></b></div><div><b><span class="Apple-tab-span" style="white-space:pre">                </span>case IOCTL_USB_OPEN:</b></div>
<div><span class="Apple-tab-span" style="white-space:pre"><b>                        </b></span></div><div><b><span class="Apple-tab-span" style="white-space:pre">                        </span>printk(KERN_INFO "In IOCTL case1 of USB character driver: \n");</b></div>
<div><b><span class="Apple-tab-span" style="white-space:pre">                        </span>return 0;</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">                </span>break;</b></div><div><span class="Apple-tab-span" style="white-space:pre"><b>        </b></span></div>
<div><b><span class="Apple-tab-span" style="white-space:pre">                </span>case IOCTL_USB_READ:</b></div><div><span class="Apple-tab-span" style="white-space:pre"><b>                        </b></span></div><div><b><span class="Apple-tab-span" style="white-space:pre">                        </span>printk(KERN_INFO "In IOCTL case2 of USB character driver: \n");</b></div>
<div><b><span class="Apple-tab-span" style="white-space:pre">                        </span>return 0;</b></div><div><b><span class="Apple-tab-span" style="white-space:pre">                </span> break;</b></div><div><span class="Apple-tab-span" style="white-space:pre"><b>        </b></span></div>
<div><b><span class="Apple-tab-span" style="white-space:pre">                </span>case IOCTL_USB_WRITE:</b></div><div><span class="Apple-tab-span" style="white-space:pre"><b>                        </b></span></div><div><b><span class="Apple-tab-span" style="white-space:pre">                        </span>printk(KERN_INFO "In IOCTL case3 of USB character driver: \n");</b></div>
<div><b><span class="Apple-tab-span" style="white-space:pre">                        </span>return 0;</b></div><div><b> <span class="Apple-tab-span" style="white-space:pre">                </span>break;</b></div><div><span class="Apple-tab-span" style="white-space:pre"><b>                </b></span></div>
<div><b> }</b></div><div><b>}</b></div></div><div><br></div><div>From application i made sure that i am passing proper cmd arguments.The control goes to the ioctl function call in driver but doesn't go to the proper switch cases.</div>
<div><br></div><div>This is because the iocmd argument received in the ioctl implementation is getting corrupted i.e eventhough from application i am passing <b>IOCTL_USB_READ , </b><b>IOCTL_USB_OPEN </b>parameters,when i check them in </div>
<div>ioctl function in drivers the values are changed and through printk in the ioctl i could see that the iocmd received in the function is 4096 value which doesn't match to IOCTL_USB_READ or </div><div>IOCTL_USB_OPEN.</div>
<div><br></div><div>Please let me know how can the command argument passes via ioctl call in application is modified/corrupted before it reaches the actual ioctl implementation in drivers.</div><div><br></div><div>thanks,</div>
<div>Karthik.</div><div><br></div>