RE: Character device driver problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi ,

if you print the parameters that are passed to "moduleRead" when you issue cat command, 
it'll show len=4096 (thats what it gives on my machine) , and so the function will be called repeatedly till that count is reached or EOF is reached.

i think in function "moduleRead" when you come out of while loop check if 'cnt' is zero,
i yes then return EOF or else return 'len'

Dhanashri

> -----Original Message-----
> From: kernelnewbies-bounce@xxxxxxxxxxxx
> [mailto:kernelnewbies-bounce@xxxxxxxxxxxx]On Behalf Of 
> Siddharth Taneja
> Sent: Monday, March 21, 2005 2:54 PM
> To: kernelnewbies@xxxxxxxxxxxx
> Subject: Character device driver problem
> 
> 
> Hi,
> 
> I am trying to build a simple character device driver which supports 
> only the open, read and release interface. The code compiles fine and 
> the module gets inserted also fine but when I do cat <device 
> name>, the 
> read function is entered multiple times, resulting in 
> multiple and same 
> outputs("we did it" repeated continously). I am using Suse 9.2 with 
> kernel 2.6.8-24.11. The following is the code and outputs.
> 
> Thanks
> 
> Siddharth
> 
> 
> //Building a character device driver
> 
> #include <linux/module.h>
> #include <linux/kernel.h>
> #include <linux/fs.h>
> #include <asm/uaccess.h>
> 
> static int Major;
> static int devOpen;
> static char msg[80];
> static char*msgPtr;
> static int moduleOpen(struct inode *,struct file *);
> static ssize_t moduleRead(struct file *,char *,size_t,loff_t *);
> static int moduleRelease(struct inode *,struct file *);
> 
> #define DEV_NAME "myModule"
> 
> static struct file_operations fops = {
>     .read=moduleRead,
>     .open=moduleOpen,
>     .release=moduleRelease
> };
> 
> int startup(void) {
>     printk(KERN_EMERG "I am in\n");
>     devOpen=0;
>     Major=register_chrdev(0,DEV_NAME,&fops);
>     if (Major <0) {
>         printk(KERN_EMERG "wtf!?\n");
>         return Major;
>     }
>     printk(KERN_EMERG "Heres ur maj no:%d\n",Major);
>     return 0;
> }
> 
> 
> void cleanup(void) {
>     printk(KERN_EMERG "Have to go..\n");
>     unregister_chrdev(Major,DEV_NAME);
> }
> 
> static int moduleOpen(struct inode *in,struct file *fi) {
>     if(devOpen==1)
>         return -EBUSY;
>     devOpen++;
>     sprintf(msg,"we did it");
>     msgPtr=msg;
>     printk(KERN_EMERG "Open seeeaa..\n");
>     return 0;
> }
> 
> static ssize_t moduleRead(struct file *fi,char *buff, size_t 
> len, loff_t 
> * offs){
>     static int cnt=8;
>     printk(KERN_EMERG "Reading %d\n",len);
> 
>     while(cnt>=0&&len) {
>         put_user(*msgPtr,buff);
>         msgPtr++;
>         buff++;
>         cnt--;
>     }
>     //put_user('\0',buff);
>     return len;
> }
> 
> static int moduleRelease(struct inode *in,struct file* fi) {
>     printk(KERN_EMERG "Release me...\n");
>     devOpen--;
>     return 0;
> }
> 
> module_init(startup);
> module_exit(cleanup);
> MODULE_LICENSE("GPL and additional rights");
> 
> --------------------------------------------------------------
> --------------------------------------------------------------
> ----------------------
> 
> Output (log messages)
> 
> Mar 21 01:19:32 linux kernel: module first_module unsupported by 
> SUSE/Novell, tainting kernel.
> Mar 21 01:19:32 linux kernel: I am in
> Mar 21 01:19:32 linux kernel: Heres ur maj no:253
> Mar 21 01:19:46 linux kernel: Open seeeaa..
> Mar 21 01:19:46 linux kernel: Reading 4096
> Mar 21 01:19:46 linux last message repeated 177 times
> Mar 21 01:19:46 linux kernel: Release me...
> Mar 21 01:19:56 linux kernel: Have to go..
> 
> 
> 
> 
> 
> --
> Kernelnewbies: Help each other learn about the Linux kernel.
> Archive:       http://mail.nl.linux.org/kernelnewbies/
> FAQ:           http://kernelnewbies.org/faq/
> 
> 

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux