Sponsored Content
Top Forums Programming help with copy_to/from_user char device driver Post 302468735 by dragonpoint on Wednesday 3rd of November 2010 05:11:30 PM
Old 11-03-2010
help with copy_to/from_user char device driver

hi,

I need to copy strings from kernel space to user space and vice versa.
Currently if I do the following on the shell

Write from user--> kernel :echo -n abcedef > /dev/stringdrvr
read from kernel-->user :cat /dev/stringdrvr

It only returns the last character 'f' and not entire string.

Also how do i copy 2 dimensional array data


Here is my code.
Code:
size_t count=99;

ssize_t stringdrvr_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
    copy_to_user(buf,stringdrvr_buffer,99);
    if (*f_pos == 0) { *f_pos+=1; return 1; }
    else { return 0; }
}

ssize_t stringdrvr_write( struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
    char *tmp;
    tmp=buf+count-1;
    copy_from_user(stringdrvr_buffer,tmp,99);
    return 1;
}

---------- Post updated at 04:11 PM ---------- Previous update was at 03:32 PM ----------

oops...i knw what went wrong.... here is workin code
Code:
 read:
	int len, err;

	if( counter <= 0 )
		return 0;
	err = copy_to_user(buf,string,counter);
	if (err != 0)
		return -EFAULT;
	len  = counter;
	counter = 0;
	return len;

write:
  int err;
	err = copy_from_user(string,buf,count);
	if (err != 0)
		return -EFAULT;
	counter += count;
    //printk("<1>Counted = %d\n",count);
	return count;


Last edited by pludi; 11-03-2010 at 06:40 PM.. Reason: Please use code tags
 

4 More Discussions You Might Find Interesting

1. Solaris

SUNWglmr -- rasctrl environment monitoring driver for i2c or SCSI device driver ?

I've been researching minimizeing Solaris 8 and found that on the web page http://www.sun.com/bigadmin/content/packagelist/s8u7PkgList/p2.html the package SUNWglmr is listed as "rasctrl environment monitoring driver for i2c, (Root) (32-bit)" while in the document "Solaris 8 minimize-updt1.pdf"... (1 Reply)
Discussion started by: roygoodwin
1 Replies

2. Programming

Network device driver

HI, I am writing a network device driver for RTL8139c card on 2.6.18 kernel ... I am facing few queries listed below 1. Can i able to at all write a driver for RTL8139C or Realtek had designed new chip for 2.6 series kernel? 2. If no then which driver file 2.6.18 uses .. Is it 8139too.c or... (1 Reply)
Discussion started by: niketan
1 Replies

3. UNIX for Advanced & Expert Users

help regarding device driver

Hi All, I have a device driver that uses UARTserial port to write/read to-from a device. That device driver is working fine on FC3 machine( kernel version 2.6.12)... Now I am switching to FC9 (kernel version 2.6.25.11-97).I have changed the interrupt flag SA_INTERRUPT to IRQF_DISABLED... (0 Replies)
Discussion started by: rajuprade
0 Replies

4. Programming

regarding device driver

Hi All, I have a device driver that uses UARTserial port to write/read to-from a device. That device driver is working fine on FC3 machine( kernel version 2.6.12)... Now I am switching to FC9 (kernel version 2.6.25.11-97).I have changed the interrupt flag SA_INTERRUPT to IRQF_DISABLED... (0 Replies)
Discussion started by: rajuprade
0 Replies
COPY(9) 						   BSD Kernel Developer's Manual						   COPY(9)

NAME
copy, copyin, copyout, copystr, copyinstr -- kernel copy functions SYNOPSIS
#include <sys/types.h> #include <sys/systm.h> int copyin(const void *uaddr, void *kaddr, size_t len); int copyout(const void *kaddr, void *uaddr, size_t len); int copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done); int copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done); DESCRIPTION
The copy functions are designed to copy contiguous data from one address to another. All but copystr() copy data from user-space to kernel- space or vice-versa. The copy routines provide the following functionality: copyin() Copies len bytes of data from the user-space address uaddr to the kernel-space address kaddr. copyout() Copies len bytes of data from the kernel-space address kaddr to the user-space address uaddr. copystr() Copies a NUL-terminated string, at most len bytes long, from kernel-space address kfaddr to kernel-space address kdaddr. The number of bytes actually copied, including the terminating NUL, is returned in *done. copyinstr() Copies a NUL-terminated string, at most len bytes long, from user-space address uaddr to kernel-space address kaddr. The num- ber of bytes actually copied, including the terminating NUL, is returned in *done. RETURN VALUES
The copy functions return 0 on success or EFAULT if a bad address is encountered. In addition, the copystr(), and copyinstr() functions return ENAMETOOLONG if the string is longer than len bytes. SEE ALSO
fetch(9), store(9) BSD
January 7, 1996 BSD
All times are GMT -4. The time now is 08:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy