LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-11-2014, 04:27 AM   #1
tkk1988
LQ Newbie
 
Registered: Apr 2014
Posts: 3

Rep: Reputation: Disabled
Linux driver - write into alloctaed memory


Hello,

I'm developing a linux driver for a PCIe device and unfortunately I've never done this before, so I need ur help.

I want to read 128bit of a register on the PCIe device.
For that I allocated a kernel buffer (char):

rxBuffer = kmalloc(bufferSize, GFP_KERNEL);
if (NULL == rxBuffer) {
printk("Init: Unable to allocate Buffer.\n");
return (-1);
}


now since I want to read I programmed:
u32 ret* = 0;
u32 reg_addr = (u32)(baseVirt + *f_pos);
ret* = readl((void*)reg_addr);


and at the end of the reading function:
int retval = copy_to_user(buf,rxBuffer,count);

So, my problem is how to write the value of ret into the rxbuffer? Especially if I want to read 128bit?

Best regards
Thomas
 
Old 04-11-2014, 07:18 AM   #2
gengisdave
Member
 
Registered: Dec 2013
Location: Turin, Italy
Distribution: slackware
Posts: 327

Rep: Reputation: 73
read copy_to_user prototype:

Code:
unsigned long copy_to_user (void __user * to, const void * from, unsigned long n);
to : Destination address, in user space.
from : Source address, in kernel space.
n : Number of bytes to copy.

Copy data from kernel space to user space. Returns number of bytes that could not be copied. On success, this will be zero.

http://www.gnugeneration.com/mirrors...api/r4299.html
 
Old 04-11-2014, 07:50 AM   #3
tkk1988
LQ Newbie
 
Registered: Apr 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks for ur reply.
As you can see in my first post I do know this function, but the question is how to read 128bit from the device.

As far as I know I need the readl() function to get access to the registers in the device, right?
Since I need to read 128 bit and readl is only 32 bit, what do I have to do to get the rest? I haven't understand the whole addressing yet.

Last but not least, do I even need a kernel buffer? Actually I could store the values in an array, right?
 
Old 04-11-2014, 08:18 AM   #4
gengisdave
Member
 
Registered: Dec 2013
Location: Turin, Italy
Distribution: slackware
Posts: 327

Rep: Reputation: 73
use addressing:

Code:
ret* = readl((void*)reg_addr);
ret* = readl((void*)reg_addr+4);
ret* = readl((void*)reg_addr+8);
ret* = readl((void*)reg_addr+12);
there is readq on 64 bit machine:

Code:
ret* = readq((void*)reg_addr);
ret* = readq((void*)reg_addr+8);
if kernel allow you to read from kernel memory into user space, i think you can write ret directly into buf
 
Old 04-11-2014, 08:38 AM   #5
tkk1988
LQ Newbie
 
Registered: Apr 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks.

So in that case, I do need the copy_to_user() function 4 times, or how do I get the four differnt values of ret into user space buf?

In the last sentence, do u mean kernel space?
 
Old 04-11-2014, 08:55 AM   #6
gengisdave
Member
 
Registered: Dec 2013
Location: Turin, Italy
Distribution: slackware
Posts: 327

Rep: Reputation: 73
yes, i mean kernel space; you can use copy_to_user() 4 times as you said, right after readl; if you have allocated buf as a 16 byte pointer, you can do this directly

Code:
buf* = readl((void*)reg_addr);
*(buf+4) = readl((void*)reg_addr+4);
*(buf+8) = readl((void*)reg_addr+8);
*(buf+12) = readl((void*)reg_addr+12);
but i think this generates some type warning
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pci linux driver: how to get a memory address to do a DMA Write and Read ice61 Linux - Newbie 1 12-16-2012 09:05 AM
Mapping write-combining driver memory with PAT - Linux kernel 2.6.37 imoraru Linux - Kernel 0 07-13-2011 04:59 PM
write a filesystem driver for linux smeezekitty Linux - General 2 10-11-2009 01:53 AM
How to write a device driver in linux yogesh_bansal Linux - Newbie 2 01-25-2009 08:47 AM
How to write a driver for wnic in linux? supermyself Linux - Wireless Networking 3 03-30-2006 09:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration