Re: simple character device driver

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

 



On Saturday 11 September 2010 12:04:37 fabio de francesco wrote:
> On Friday 10 September 2010 20:23:13 Josh Cartwright wrote:
> > On Fri, Sep 10, 2010 at 05:51:41PM +0200, fabio de francesco wrote:
> > > static ssize_t
> > > mycdrv_read (struct file *file, char __user * buf, size_t lbuf, loff_t
> > > * ppos) {
> > > 
> > >     int nbytes, maxbytes, bytes_to_do;
> > >     maxbytes = KBUF_SIZE - *ppos;
> > >     bytes_to_do = lbuf <= maxbytes ? lbuf : maxbytes;
> > >     nbytes = lbuf - copy_to_user (buf, kbuf + *ppos, bytes_to_do);
> > >     *ppos += nbytes;
> > >     printk (KERN_INFO "\n READING function, nbytes=%d, pos=%d\n",
> > >     nbytes,
> > >     
> > >             (int)*ppos);
> > >     
> > >     return nbytes;
> > > 
> > > }
> > 
> > I'm not going to flat out answer your query, but help you work towards
> > the solution yourself.
> > 
> > Think about the termination case.  In order to terminate, you'll need to
> > return 0 to signal an end of file to the user-mode read.
> > 
> > In what case could your code return 0?
> 
> I think I now see your point... Here is the mycdrv_read() function
> modified:
> 
> static ssize_t
> mycdrv_read (struct file *file, char __user * buf, size_t lbuf, loff_t *
> ppos) {
>     char *kbuf = file->private_data;
>     long nbytes, maxbytes, bytes_to_do;
>     maxbytes = KBUF_SIZE - *ppos;
>     bytes_to_do = lbuf < maxbytes ? lbuf : maxbytes;
>         (long)*ppos, maxbytes, bytes_to_do);
>     nbytes = bytes_to_do - copy_to_user (buf, kbuf + *ppos, bytes_to_do);
>     *ppos += nbytes;
>     return nbytes;
> }
> 
> I think now it is working. What do you think about it?
> 
> Anyway I have another problem. I suppose the write function doesn't work
> because after a writing with a "echo" I don't read anything with "cat":
> 
> static ssize_t
> mycdrv_write (struct file *file, const char __user * buf, size_t lbuf,
>               loff_t * ppos)
> {
>     char *kbuf = file->private_data;
>     long nbytes, maxbytes, bytes_to_do;
>     maxbytes = KBUF_SIZE - *ppos;
>     bytes_to_do = lbuf < maxbytes ? lbuf : maxbytes;
>     nbytes = bytes_to_do - copy_from_user (kbuf + *ppos, buf, lbuf);
>     *ppos += nbytes;
>     printk (KERN_INFO "\n WRITING function, nbytes=%ld, pos=%ld\n", nbytes,
> (long)*ppos);
>     return nbytes;
> }
> 
> Can you please help with this too?
> 
> Thanks in advance.
> 
> fabio

I have also reworked the write() function following what I have done to the 
read() one:

static ssize_t
mycdrv_write (struct file *file, const char __user * buf, size_t lbuf,
              loff_t * ppos)
{
    char *kbuf = file->private_data;
    long nbytes, maxbytes, bytes_to_do;
    maxbytes = KBUF_SIZE - *ppos;
    bytes_to_do = lbuf < maxbytes ? lbuf : maxbytes;
    nbytes = bytes_to_do - copy_from_user( kbuf + *ppos, buf, bytes_to_do );
    *ppos += nbytes;
    printk (KERN_INFO "\n WRITING function, nbytes=%ld, pos=%ld\n", nbytes, 
(long)*ppos);
    return nbytes;
}

Unfortunatelly I still can't read back what I have echoed to the device. I 
don't understand whether the bug is still in the read() function or the 
write() one.

fabio

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at 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