Re: How can I create or read/write a file in linux device driver?

2007-01-19 Thread Helge Hafting

linux-os (Dick Johnson) wrote:

Sometimes a idiot boss will say; "You need to read or write files from
within the driver. If you don't do what I tell you, you are fired!"
  

To which the response is something like
"This is impossible/illegal/unsupported so it can't be done." 
Fortunately, civilized countries have laws against being

fired for being unable to do the impossible.

Well, nothing is really impossible.  Tell the boss about
the man-hours needed to implement a kernel file API
from scratch, and then forking the kernel
in order to maintain this community-rejected abomination
forever.

Sigh, assuming you can't walk next door and get a reasonable job, it
__is__ possible to unreliably access files within the kernel. Note
that the kernel is __designed__ to perform user-mode operations, so
it is a bit difficult.

Another way is to cheat.  The boss asks the impossible, so
make a workaround in the form of a userspace program that
does the file writing while communicationg with the driver.
The driver installer software can then be made to install
this userspace program as well. Looks like a hack - but it
is the recommended way of doing these things. Often enough you
have a startup script for the thing anyway, a good place to
launch userspace helper apps.  Either that, or userspace
will be involved somehow in using the device - launch the
helper at that time.

Try not to get in a situation where the boss explicitly asks
for files written from the kernel.  If you're making a driver and
the boss ask for a file - just write that userspace helper
because that is the way it is done on linux.  No conflict there.

Helge Hafting






-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: How can I create or read/write a file in linux device driver?

2007-01-19 Thread Helge Hafting

linux-os (Dick Johnson) wrote:

Sometimes a idiot boss will say; You need to read or write files from
within the driver. If you don't do what I tell you, you are fired!
  

To which the response is something like
This is impossible/illegal/unsupported so it can't be done. 
Fortunately, civilized countries have laws against being

fired for being unable to do the impossible.

Well, nothing is really impossible.  Tell the boss about
the man-hours needed to implement a kernel file API
from scratch, and then forking the kernel
in order to maintain this community-rejected abomination
forever.

Sigh, assuming you can't walk next door and get a reasonable job, it
__is__ possible to unreliably access files within the kernel. Note
that the kernel is __designed__ to perform user-mode operations, so
it is a bit difficult.

Another way is to cheat.  The boss asks the impossible, so
make a workaround in the form of a userspace program that
does the file writing while communicationg with the driver.
The driver installer software can then be made to install
this userspace program as well. Looks like a hack - but it
is the recommended way of doing these things. Often enough you
have a startup script for the thing anyway, a good place to
launch userspace helper apps.  Either that, or userspace
will be involved somehow in using the device - launch the
helper at that time.

Try not to get in a situation where the boss explicitly asks
for files written from the kernel.  If you're making a driver and
the boss ask for a file - just write that userspace helper
because that is the way it is done on linux.  No conflict there.

Helge Hafting






-
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: How can I create or read/write a file in linux device driver?

2007-01-12 Thread Jan Engelhardt

On Jan 12 2007 09:27, linux-os (Dick Johnson) wrote:
>
>First, since file-operations require process context, and the kernel
>is not a process, you need to create a kernel thread to handle your file
>I/O.

Not always. If you do file I/O as part of a device driver, you are fine.
quad_dsp is such an example, where writing to /dev/Qdsp_* will trigger writes
to /dev/dsp and /dev/adsp.

>Once you set up this "internal environment," you use the appropriate
>kernel function(s) such as sys_open()

What against filp_open? That avoids the unnecessary getname() stuff in most
syscalls.


-`J'
-- 
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: How can I create or read/write a file in linux device driver?

2007-01-12 Thread Erik Mouw
On Fri, Jan 12, 2007 at 09:27:01AM -0500, linux-os (Dick Johnson) wrote:
> Sometimes a idiot boss will say; "You need to read or write files from
> within the driver. If you don't do what I tell you, you are fired!"

Sometimes PHBs want you to break the laws of physics. I suggest you
read Dilbert about that.


Erik

-- 
+-- Erik Mouw -- www.harddisk-recovery.com -- +31 70 370 12 90 --
| Lab address: Delftechpark 26, 2628 XH, Delft, The Netherlands
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: How can I create or read/write a file in linux device driver?

2007-01-12 Thread linux-os \(Dick Johnson\)

On Fri, 12 Jan 2007, Erik Mouw wrote:

> On Fri, Jan 12, 2007 at 11:27:01AM +0100, Jesper Juhl wrote:
>> On 12/01/07, congwen <[EMAIL PROTECTED]> wrote:
>>> Hello everyone, I want to create and read/write a file in Linux kernel or
>>> device driver,
>>
>> Don't read/write user space files from kernel space.
>>
>> Please search the archives, this get asked a lot and it has been
>> explained a million times why it's a bad idea.
>> You can also read http://www.linuxjournal.com/article/8110
>
> Rather point to
>
>  http://kernelnewbies.org/FAQ/WhyWritingFilesFromKernelIsBad
>
>
> Erik
>
> -- 
> +-- Erik Mouw -- www.harddisk-recovery.com -- +31 70 370 12 90 --
> | Lab address: Delftechpark 26, 2628 XH, Delft, The Netherlands
> -

Sometimes a idiot boss will say; "You need to read or write files from
within the driver. If you don't do what I tell you, you are fired!"
Sigh, assuming you can't walk next door and get a reasonable job, it
__is__ possible to unreliably access files within the kernel. Note
that the kernel is __designed__ to perform user-mode operations, so
it is a bit difficult.

First, since file-operations require process context, and the kernel
is not a process, you need to create a kernel thread to handle your file
I/O. You write code to properly start up and shut down the kernel thread
before you do anything else. There are drivers in the kernel tree that
can be used as templates. The code that the kernel thread executes, needs
to be written so that it can receive commands to open/close/read/write
files, perhaps from semaphores or other communications methods. You can't
just create the thread and let it spin. It will eat up most all the CPU time!

Once you set up this "internal environment," you use the appropriate
kernel function(s) such as sys_open(), etc. You need to look at the code
and figure out what the parameters are. This is all very scary stuff and
it will take a long time to get it right. Once you have that working, there
is no guarantee that it will work with another kernel version simply by 
recompiling it. This is because some kernel versions lock portions of kernel 
code that you need. It's a pain.

In the unlikely event that you get a reasonably bug-free system running,
please publish the code on some web-site so it can be referenced in the
future by people whose bosses are idiots.


Cheers,
Dick Johnson
Penguin : Linux version 2.6.16.24 on an i686 machine (5592.72 BogoMips).
New book: http://www.AbominableFirebug.com/
_



The information transmitted in this message is confidential and may be 
privileged.  Any review, retransmission, dissemination, or other use of this 
information by persons or entities other than the intended recipient is 
prohibited.  If you are not the intended recipient, please notify Analogic 
Corporation immediately - by replying to this message or by sending an email to 
[EMAIL PROTECTED] - and destroy all copies of this information, including any 
attachments, without reading or disclosing them.

Thank you.
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: How can I create or read/write a file in linux device driver?

2007-01-12 Thread Erik Mouw
On Fri, Jan 12, 2007 at 11:27:01AM +0100, Jesper Juhl wrote:
> On 12/01/07, congwen <[EMAIL PROTECTED]> wrote:
> >Hello everyone, I want to create and read/write a file in Linux kernel or 
> >device driver,
> 
> Don't read/write user space files from kernel space.
> 
> Please search the archives, this get asked a lot and it has been
> explained a million times why it's a bad idea.
> You can also read http://www.linuxjournal.com/article/8110

Rather point to

  http://kernelnewbies.org/FAQ/WhyWritingFilesFromKernelIsBad


Erik

-- 
+-- Erik Mouw -- www.harddisk-recovery.com -- +31 70 370 12 90 --
| Lab address: Delftechpark 26, 2628 XH, Delft, The Netherlands
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: How can I create or read/write a file in linux device driver?

2007-01-12 Thread Jan Engelhardt

On Jan 12 2007 11:54, Jesper Juhl wrote:
>> 
>> The article does it the bad way. IMHO filp_open() and
>> vfs_read/vfs_write() are much less problematic wrt. to userspace.
>> FWIW see
>> ftp://ftp-1.gwdg.de/pub/linux/misc/suser-jengelh/kernel/quad_dsp-1.5.1.tar.bz2
>
> There is no good way.  You simply should NOT do it.

I never said there is a good way, just that some are better than others ;-)
As for quad_dsp, well, the reason why it's done in kernel-space is because
userspace wrapping with LD_PRELOAD does not always work, esp. with statically
compiled apps.


-`J'
-- 
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: How can I create or read/write a file in linux device driver?

2007-01-12 Thread Jesper Juhl

On 12/01/07, Jan Engelhardt <[EMAIL PROTECTED]> wrote:


On Jan 12 2007 11:27, Jesper Juhl wrote:
> On 12/01/07, congwen <[EMAIL PROTECTED]> wrote:
>> Hello everyone, I want to create and read/write a file in Linux kernel or
>> device driver,
>
> Don't read/write user space files from kernel space.
>
> Please search the archives, this get asked a lot and it has been
> explained a million times why it's a bad idea.
> You can also read http://www.linuxjournal.com/article/8110

The article does it the bad way. IMHO filp_open() and
vfs_read/vfs_write() are much less problematic wrt. to userspace.
FWIW see
ftp://ftp-1.gwdg.de/pub/linux/misc/suser-jengelh/kernel/quad_dsp-1.5.1.tar.bz2



There is no good way.  You simply should NOT do it.

--
Jesper Juhl <[EMAIL PROTECTED]>
Don't top-post  http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please  http://www.expita.com/nomime.html
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: How can I create or read/write a file in linux device driver?

2007-01-12 Thread Jan Engelhardt

On Jan 12 2007 11:27, Jesper Juhl wrote:
> On 12/01/07, congwen <[EMAIL PROTECTED]> wrote:
>> Hello everyone, I want to create and read/write a file in Linux kernel or
>> device driver,
>
> Don't read/write user space files from kernel space.
>
> Please search the archives, this get asked a lot and it has been
> explained a million times why it's a bad idea.
> You can also read http://www.linuxjournal.com/article/8110

The article does it the bad way. IMHO filp_open() and 
vfs_read/vfs_write() are much less problematic wrt. to userspace.
FWIW see 
ftp://ftp-1.gwdg.de/pub/linux/misc/suser-jengelh/kernel/quad_dsp-1.5.1.tar.bz2


-`J'
-- 
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: How can I create or read/write a file in linux device driver?

2007-01-12 Thread Jesper Juhl

On 12/01/07, congwen <[EMAIL PROTECTED]> wrote:

Hello everyone, I want to create and read/write a file in Linux kernel or 
device driver,


Don't read/write user space files from kernel space.

Please search the archives, this get asked a lot and it has been
explained a million times why it's a bad idea.
You can also read http://www.linuxjournal.com/article/8110

--
Jesper Juhl <[EMAIL PROTECTED]>
Don't top-post  http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please  http://www.expita.com/nomime.html
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: How can I create or read/write a file in linux device driver?

2007-01-12 Thread Jesper Juhl

On 12/01/07, congwen [EMAIL PROTECTED] wrote:

Hello everyone, I want to create and read/write a file in Linux kernel or 
device driver,


Don't read/write user space files from kernel space.

Please search the archives, this get asked a lot and it has been
explained a million times why it's a bad idea.
You can also read http://www.linuxjournal.com/article/8110

--
Jesper Juhl [EMAIL PROTECTED]
Don't top-post  http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please  http://www.expita.com/nomime.html
-
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: How can I create or read/write a file in linux device driver?

2007-01-12 Thread Jan Engelhardt

On Jan 12 2007 11:27, Jesper Juhl wrote:
 On 12/01/07, congwen [EMAIL PROTECTED] wrote:
 Hello everyone, I want to create and read/write a file in Linux kernel or
 device driver,

 Don't read/write user space files from kernel space.

 Please search the archives, this get asked a lot and it has been
 explained a million times why it's a bad idea.
 You can also read http://www.linuxjournal.com/article/8110

The article does it the bad way. IMHO filp_open() and 
vfs_read/vfs_write() are much less problematic wrt. to userspace.
FWIW see 
ftp://ftp-1.gwdg.de/pub/linux/misc/suser-jengelh/kernel/quad_dsp-1.5.1.tar.bz2


-`J'
-- 
-
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: How can I create or read/write a file in linux device driver?

2007-01-12 Thread Jesper Juhl

On 12/01/07, Jan Engelhardt [EMAIL PROTECTED] wrote:


On Jan 12 2007 11:27, Jesper Juhl wrote:
 On 12/01/07, congwen [EMAIL PROTECTED] wrote:
 Hello everyone, I want to create and read/write a file in Linux kernel or
 device driver,

 Don't read/write user space files from kernel space.

 Please search the archives, this get asked a lot and it has been
 explained a million times why it's a bad idea.
 You can also read http://www.linuxjournal.com/article/8110

The article does it the bad way. IMHO filp_open() and
vfs_read/vfs_write() are much less problematic wrt. to userspace.
FWIW see
ftp://ftp-1.gwdg.de/pub/linux/misc/suser-jengelh/kernel/quad_dsp-1.5.1.tar.bz2



There is no good way.  You simply should NOT do it.

--
Jesper Juhl [EMAIL PROTECTED]
Don't top-post  http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please  http://www.expita.com/nomime.html
-
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: How can I create or read/write a file in linux device driver?

2007-01-12 Thread Jan Engelhardt

On Jan 12 2007 11:54, Jesper Juhl wrote:
 
 The article does it the bad way. IMHO filp_open() and
 vfs_read/vfs_write() are much less problematic wrt. to userspace.
 FWIW see
 ftp://ftp-1.gwdg.de/pub/linux/misc/suser-jengelh/kernel/quad_dsp-1.5.1.tar.bz2

 There is no good way.  You simply should NOT do it.

I never said there is a good way, just that some are better than others ;-)
As for quad_dsp, well, the reason why it's done in kernel-space is because
userspace wrapping with LD_PRELOAD does not always work, esp. with statically
compiled apps.


-`J'
-- 
-
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: How can I create or read/write a file in linux device driver?

2007-01-12 Thread Erik Mouw
On Fri, Jan 12, 2007 at 11:27:01AM +0100, Jesper Juhl wrote:
 On 12/01/07, congwen [EMAIL PROTECTED] wrote:
 Hello everyone, I want to create and read/write a file in Linux kernel or 
 device driver,
 
 Don't read/write user space files from kernel space.
 
 Please search the archives, this get asked a lot and it has been
 explained a million times why it's a bad idea.
 You can also read http://www.linuxjournal.com/article/8110

Rather point to

  http://kernelnewbies.org/FAQ/WhyWritingFilesFromKernelIsBad


Erik

-- 
+-- Erik Mouw -- www.harddisk-recovery.com -- +31 70 370 12 90 --
| Lab address: Delftechpark 26, 2628 XH, Delft, The Netherlands
-
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: How can I create or read/write a file in linux device driver?

2007-01-12 Thread linux-os \(Dick Johnson\)

On Fri, 12 Jan 2007, Erik Mouw wrote:

 On Fri, Jan 12, 2007 at 11:27:01AM +0100, Jesper Juhl wrote:
 On 12/01/07, congwen [EMAIL PROTECTED] wrote:
 Hello everyone, I want to create and read/write a file in Linux kernel or
 device driver,

 Don't read/write user space files from kernel space.

 Please search the archives, this get asked a lot and it has been
 explained a million times why it's a bad idea.
 You can also read http://www.linuxjournal.com/article/8110

 Rather point to

  http://kernelnewbies.org/FAQ/WhyWritingFilesFromKernelIsBad


 Erik

 -- 
 +-- Erik Mouw -- www.harddisk-recovery.com -- +31 70 370 12 90 --
 | Lab address: Delftechpark 26, 2628 XH, Delft, The Netherlands
 -

Sometimes a idiot boss will say; You need to read or write files from
within the driver. If you don't do what I tell you, you are fired!
Sigh, assuming you can't walk next door and get a reasonable job, it
__is__ possible to unreliably access files within the kernel. Note
that the kernel is __designed__ to perform user-mode operations, so
it is a bit difficult.

First, since file-operations require process context, and the kernel
is not a process, you need to create a kernel thread to handle your file
I/O. You write code to properly start up and shut down the kernel thread
before you do anything else. There are drivers in the kernel tree that
can be used as templates. The code that the kernel thread executes, needs
to be written so that it can receive commands to open/close/read/write
files, perhaps from semaphores or other communications methods. You can't
just create the thread and let it spin. It will eat up most all the CPU time!

Once you set up this internal environment, you use the appropriate
kernel function(s) such as sys_open(), etc. You need to look at the code
and figure out what the parameters are. This is all very scary stuff and
it will take a long time to get it right. Once you have that working, there
is no guarantee that it will work with another kernel version simply by 
recompiling it. This is because some kernel versions lock portions of kernel 
code that you need. It's a pain.

In the unlikely event that you get a reasonably bug-free system running,
please publish the code on some web-site so it can be referenced in the
future by people whose bosses are idiots.


Cheers,
Dick Johnson
Penguin : Linux version 2.6.16.24 on an i686 machine (5592.72 BogoMips).
New book: http://www.AbominableFirebug.com/
_



The information transmitted in this message is confidential and may be 
privileged.  Any review, retransmission, dissemination, or other use of this 
information by persons or entities other than the intended recipient is 
prohibited.  If you are not the intended recipient, please notify Analogic 
Corporation immediately - by replying to this message or by sending an email to 
[EMAIL PROTECTED] - and destroy all copies of this information, including any 
attachments, without reading or disclosing them.

Thank you.
-
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: How can I create or read/write a file in linux device driver?

2007-01-12 Thread Erik Mouw
On Fri, Jan 12, 2007 at 09:27:01AM -0500, linux-os (Dick Johnson) wrote:
 Sometimes a idiot boss will say; You need to read or write files from
 within the driver. If you don't do what I tell you, you are fired!

Sometimes PHBs want you to break the laws of physics. I suggest you
read Dilbert about that.


Erik

-- 
+-- Erik Mouw -- www.harddisk-recovery.com -- +31 70 370 12 90 --
| Lab address: Delftechpark 26, 2628 XH, Delft, The Netherlands
-
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: How can I create or read/write a file in linux device driver?

2007-01-12 Thread Jan Engelhardt

On Jan 12 2007 09:27, linux-os (Dick Johnson) wrote:

First, since file-operations require process context, and the kernel
is not a process, you need to create a kernel thread to handle your file
I/O.

Not always. If you do file I/O as part of a device driver, you are fine.
quad_dsp is such an example, where writing to /dev/Qdsp_* will trigger writes
to /dev/dsp and /dev/adsp.

Once you set up this internal environment, you use the appropriate
kernel function(s) such as sys_open()

What against filp_open? That avoids the unnecessary getname() stuff in most
syscalls.


-`J'
-- 
-
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/