accessing files, devices, in kernel modules - Linux

This is a discussion on accessing files, devices, in kernel modules - Linux ; hi there! I have just started studying the linux kernel, version 2.6. i read some manuals, howtos, have already made some really simple modules, but when I tried to do a little more complex work, I stumbled across some dificulties. ...

+ Reply to Thread
Results 1 to 10 of 10

Thread: accessing files, devices, in kernel modules

  1. accessing files, devices, in kernel modules

    hi there!

    I have just started studying the linux kernel, version 2.6. i read some
    manuals, howtos, have already made some really simple modules, but when
    I tried to do a little more complex work, I stumbled across some
    dificulties.
    well, what I want to is a kernel module that opens a regular file,
    writes something to it, and closes it. easy, isn't it? I also thought
    that

    but then I tried making a simple open over the file, but the module
    complains it can't find the symbols open, close and write. Is there
    anything unusual I must do to work with files in the kernel? none of
    the manuals I read mentioned how to open and do some I/O over it.

    now, to my real problem: I must record some sound, and then play it!
    since in LInux everything is a file, I'm able to do it by means of
    /dev/dsp. that's why I asked abou t how to work with regular files. but
    I don't know if this way of accessing devices hold for modules. and I
    can't open that file!

    I looked at /proc/kallsyms, and open is there. I don't have a clue why
    the module complains it couldn't find the open.

    thanks for any help you could provide, and sorry for such noob question


  2. Re: accessing files, devices, in kernel modules

    Yellow wrote:

    > now, to my real problem: I must record some sound, and then play it!
    > since in LInux everything is a file, I'm able to do it by means of
    > /dev/dsp. that's why I asked abou t how to work with regular files. but
    > I don't know if this way of accessing devices hold for modules. and I
    > can't open that file!


    No; that way of accessing files is not used from within the kernel
    (though it is possible to access regular files). Why do you think you
    ought to do this from within a kernel module?

    The usual way of doing such things is to write a driver -- which may or
    may not be contained within a loadable kernel module -- that interfaces
    to a particular type of device and provides the ability to read from or
    write to the device (or both). Then one uses an application
    (non-kernel) program to read data from one device and write it to
    another (or from a device to a file, or from a file to a device, and so
    forth). "Everything is a file" in Linux only because there is a driver
    for each device that makes it look that way.

    GH


  3. Re: accessing files, devices, in kernel modules

    Try using these calls,
    filp_open and filp_close
    regards,
    samir
    Yellow wrote:
    > hi there!
    >
    > I have just started studying the linux kernel, version 2.6. i read some
    > manuals, howtos, have already made some really simple modules, but when
    > I tried to do a little more complex work, I stumbled across some
    > dificulties.
    > well, what I want to is a kernel module that opens a regular file,
    > writes something to it, and closes it. easy, isn't it? I also thought
    > that
    >
    > but then I tried making a simple open over the file, but the module
    > complains it can't find the symbols open, close and write. Is there
    > anything unusual I must do to work with files in the kernel? none of
    > the manuals I read mentioned how to open and do some I/O over it.
    >
    > now, to my real problem: I must record some sound, and then play it!
    > since in LInux everything is a file, I'm able to do it by means of
    > /dev/dsp. that's why I asked abou t how to work with regular files. but
    > I don't know if this way of accessing devices hold for modules. and I
    > can't open that file!
    >
    > I looked at /proc/kallsyms, and open is there. I don't have a clue why
    > the module complains it couldn't find the open.
    >
    > thanks for any help you could provide, and sorry for such noob question



  4. Re: accessing files, devices, in kernel modules


    Yellow wrote:

    > hi there!
    >
    > I have just started studying the linux kernel, version 2.6. i read some
    > manuals, howtos, have already made some really simple modules, but when
    > I tried to do a little more complex work, I stumbled across some
    > dificulties.
    > well, what I want to is a kernel module that opens a regular file,
    > writes something to it, and closes it. easy, isn't it? I also thought
    > that
    >
    > but then I tried making a simple open over the file, but the module
    > complains it can't find the symbols open, close and write. Is there

    Are you doing OPEN(filename, flags)?
    > anything unusual I must do to work with files in the kernel? none of
    > the manuals I read mentioned how to open and do some I/O over it.
    >
    > now, to my real problem: I must record some sound, and then play it!
    > since in LInux everything is a file, I'm able to do it by means of
    > /dev/dsp. that's why I asked abou t how to work with regular files. but
    > I don't know if this way of accessing devices hold for modules. and I
    > can't open that file!
    >
    > I looked at /proc/kallsyms, and open is there. I don't have a clue why
    > the module complains it couldn't find the open.

    Kernel opens the files with sys_open, similary sys_close, sys_read,
    sys_write and so on.
    >
    > thanks for any help you could provide, and sorry for such noob question



  5. Re: accessing files, devices, in kernel modules

    thanks for all the replies. I tried using sys_open, and "aparently" I
    could open the file, but I couldn't write or read it using sys_write or
    sys_read. Linux complains it can't find the sys_read and sys_write
    symbols.
    I also tried using filp_open and filp_open, but got a segmentation
    fault .
    here's my code, and what I want to do first:

    int init_module(void)
    {
    int fd;
    printk(KERN_ALERT "module loaded\n");
    fd = open("/home/alisson/texte.txt", 02);
    if(fd ==-1)
    printk(KERN_ALERT "error opening file");
    /* write something here */
    close(fd);
    return 0;
    }

    but what I'd like to know is: can I use the sound card the same way I
    use it in user space programs? like reading and opening a file? my
    final goal is record some sound from the microphone for 3 seconds, and
    then play it.

    thanks


    miline wrote:
    > Yellow wrote:
    >
    > > hi there!
    > >
    > > I have just started studying the linux kernel, version 2.6. i read some
    > > manuals, howtos, have already made some really simple modules, but when
    > > I tried to do a little more complex work, I stumbled across some
    > > dificulties.
    > > well, what I want to is a kernel module that opens a regular file,
    > > writes something to it, and closes it. easy, isn't it? I also thought
    > > that
    > >
    > > but then I tried making a simple open over the file, but the module
    > > complains it can't find the symbols open, close and write. Is there

    > Are you doing OPEN(filename, flags)?
    > > anything unusual I must do to work with files in the kernel? none of
    > > the manuals I read mentioned how to open and do some I/O over it.
    > >
    > > now, to my real problem: I must record some sound, and then play it!
    > > since in LInux everything is a file, I'm able to do it by means of
    > > /dev/dsp. that's why I asked abou t how to work with regular files. but
    > > I don't know if this way of accessing devices hold for modules. and I
    > > can't open that file!
    > >
    > > I looked at /proc/kallsyms, and open is there. I don't have a clue why
    > > the module complains it couldn't find the open.

    > Kernel opens the files with sys_open, similary sys_close, sys_read,
    > sys_write and so on.
    > >
    > > thanks for any help you could provide, and sorry for such noob question



  6. Re: accessing files, devices, in kernel modules

    oh, sorry, but maybe I didn't make myself clear last post: what I'd
    like to know is how can I use the soundcard in kernel space. if it
    doesn't involve any operations with device files, please, don't stick
    to the reading/writing to file issue.
    regards

    Yellow wrote:
    > thanks for all the replies. I tried using sys_open, and "aparently" I
    > could open the file, but I couldn't write or read it using sys_write or
    > sys_read. Linux complains it can't find the sys_read and sys_write
    > symbols.
    > I also tried using filp_open and filp_open, but got a segmentation
    > fault .
    > here's my code, and what I want to do first:
    >
    > int init_module(void)
    > {
    > int fd;
    > printk(KERN_ALERT "module loaded\n");
    > fd = open("/home/alisson/texte.txt", 02);
    > if(fd ==-1)
    > printk(KERN_ALERT "error opening file");
    > /* write something here */
    > close(fd);
    > return 0;
    > }
    >
    > but what I'd like to know is: can I use the sound card the same way I
    > use it in user space programs? like reading and opening a file? my
    > final goal is record some sound from the microphone for 3 seconds, and
    > then play it.
    >
    > thanks
    >
    >
    > miline wrote:
    > > Yellow wrote:
    > >
    > > > hi there!
    > > >
    > > > I have just started studying the linux kernel, version 2.6. i read some
    > > > manuals, howtos, have already made some really simple modules, but when
    > > > I tried to do a little more complex work, I stumbled across some
    > > > dificulties.
    > > > well, what I want to is a kernel module that opens a regular file,
    > > > writes something to it, and closes it. easy, isn't it? I also thought
    > > > that
    > > >
    > > > but then I tried making a simple open over the file, but the module
    > > > complains it can't find the symbols open, close and write. Is there

    > > Are you doing OPEN(filename, flags)?
    > > > anything unusual I must do to work with files in the kernel? none of
    > > > the manuals I read mentioned how to open and do some I/O over it.
    > > >
    > > > now, to my real problem: I must record some sound, and then play it!
    > > > since in LInux everything is a file, I'm able to do it by means of
    > > > /dev/dsp. that's why I asked abou t how to work with regular files. but
    > > > I don't know if this way of accessing devices hold for modules. and I
    > > > can't open that file!
    > > >
    > > > I looked at /proc/kallsyms, and open is there. I don't have a clue why
    > > > the module complains it couldn't find the open.

    > > Kernel opens the files with sys_open, similary sys_close, sys_read,
    > > sys_write and so on.
    > > >
    > > > thanks for any help you could provide, and sorry for such noob question



  7. Re: accessing files, devices, in kernel modules

    There is one link in the reference link at this place, just what you
    want:how to read file from kernel:

    http://www.rtprogramming.blogspot.com/




    Yellow wrote:
    > oh, sorry, but maybe I didn't make myself clear last post: what I'd
    > like to know is how can I use the soundcard in kernel space. if it
    > doesn't involve any operations with device files, please, don't stick
    > to the reading/writing to file issue.
    > regards
    >
    > Yellow wrote:
    > > thanks for all the replies. I tried using sys_open, and "aparently" I
    > > could open the file, but I couldn't write or read it using sys_write or
    > > sys_read. Linux complains it can't find the sys_read and sys_write
    > > symbols.
    > > I also tried using filp_open and filp_open, but got a segmentation
    > > fault .
    > > here's my code, and what I want to do first:
    > >
    > > int init_module(void)
    > > {
    > > int fd;
    > > printk(KERN_ALERT "module loaded\n");
    > > fd = open("/home/alisson/texte.txt", 02);
    > > if(fd ==-1)
    > > printk(KERN_ALERT "error opening file");
    > > /* write something here */
    > > close(fd);
    > > return 0;
    > > }
    > >
    > > but what I'd like to know is: can I use the sound card the same way I
    > > use it in user space programs? like reading and opening a file? my
    > > final goal is record some sound from the microphone for 3 seconds, and
    > > then play it.
    > >
    > > thanks
    > >
    > >
    > > miline wrote:
    > > > Yellow wrote:
    > > >
    > > > > hi there!
    > > > >
    > > > > I have just started studying the linux kernel, version 2.6. i read some
    > > > > manuals, howtos, have already made some really simple modules, but when
    > > > > I tried to do a little more complex work, I stumbled across some
    > > > > dificulties.
    > > > > well, what I want to is a kernel module that opens a regular file,
    > > > > writes something to it, and closes it. easy, isn't it? I also thought
    > > > > that
    > > > >
    > > > > but then I tried making a simple open over the file, but the module
    > > > > complains it can't find the symbols open, close and write. Is there
    > > > Are you doing OPEN(filename, flags)?
    > > > > anything unusual I must do to work with files in the kernel? none of
    > > > > the manuals I read mentioned how to open and do some I/O over it.
    > > > >
    > > > > now, to my real problem: I must record some sound, and then play it!
    > > > > since in LInux everything is a file, I'm able to do it by means of
    > > > > /dev/dsp. that's why I asked abou t how to work with regular files. but
    > > > > I don't know if this way of accessing devices hold for modules. and I
    > > > > can't open that file!
    > > > >
    > > > > I looked at /proc/kallsyms, and open is there. I don't have a clue why
    > > > > the module complains it couldn't find the open.
    > > > Kernel opens the files with sys_open, similary sys_close, sys_read,
    > > > sys_write and so on.
    > > > >
    > > > > thanks for any help you could provide, and sorry for such noob question



  8. Re: accessing files, devices, in kernel modules

    thank you. now I understand that I shouldn't read/write files from a
    module. even because there's no sense about the kernel dealing with
    files, and it's dangerous. well, there's some time I studied operating
    systems in college .
    and with this, I know I can't use the soudcard as a file(at least this
    is not the usual way to do). probably I need to use some kernel
    structure?
    regards,

    ask wrote:
    > There is one link in the reference link at this place, just what you
    > want:how to read file from kernel:
    >
    > http://www.rtprogramming.blogspot.com/
    >
    >
    >
    >
    > Yellow wrote:
    > > oh, sorry, but maybe I didn't make myself clear last post: what I'd
    > > like to know is how can I use the soundcard in kernel space. if it
    > > doesn't involve any operations with device files, please, don't stick
    > > to the reading/writing to file issue.
    > > regards
    > >
    > > Yellow wrote:
    > > > thanks for all the replies. I tried using sys_open, and "aparently" I
    > > > could open the file, but I couldn't write or read it using sys_write or
    > > > sys_read. Linux complains it can't find the sys_read and sys_write
    > > > symbols.
    > > > I also tried using filp_open and filp_open, but got a segmentation
    > > > fault .
    > > > here's my code, and what I want to do first:
    > > >
    > > > int init_module(void)
    > > > {
    > > > int fd;
    > > > printk(KERN_ALERT "module loaded\n");
    > > > fd = open("/home/alisson/texte.txt", 02);
    > > > if(fd ==-1)
    > > > printk(KERN_ALERT "error opening file");
    > > > /* write something here */
    > > > close(fd);
    > > > return 0;
    > > > }
    > > >
    > > > but what I'd like to know is: can I use the sound card the same way I
    > > > use it in user space programs? like reading and opening a file? my
    > > > final goal is record some sound from the microphone for 3 seconds, and
    > > > then play it.
    > > >
    > > > thanks
    > > >
    > > >
    > > > miline wrote:
    > > > > Yellow wrote:
    > > > >
    > > > > > hi there!
    > > > > >
    > > > > > I have just started studying the linux kernel, version 2.6. i read some
    > > > > > manuals, howtos, have already made some really simple modules, but when
    > > > > > I tried to do a little more complex work, I stumbled across some
    > > > > > dificulties.
    > > > > > well, what I want to is a kernel module that opens a regular file,
    > > > > > writes something to it, and closes it. easy, isn't it? I also thought
    > > > > > that
    > > > > >
    > > > > > but then I tried making a simple open over the file, but the module
    > > > > > complains it can't find the symbols open, close and write. Is there
    > > > > Are you doing OPEN(filename, flags)?
    > > > > > anything unusual I must do to work with files in the kernel? none of
    > > > > > the manuals I read mentioned how to open and do some I/O over it.
    > > > > >
    > > > > > now, to my real problem: I must record some sound, and then play it!
    > > > > > since in LInux everything is a file, I'm able to do it by means of
    > > > > > /dev/dsp. that's why I asked abou t how to work with regular files. but
    > > > > > I don't know if this way of accessing devices hold for modules. and I
    > > > > > can't open that file!
    > > > > >
    > > > > > I looked at /proc/kallsyms, and open is there. I don't have a clue why
    > > > > > the module complains it couldn't find the open.
    > > > > Kernel opens the files with sys_open, similary sys_close, sys_read,
    > > > > sys_write and so on.
    > > > > >
    > > > > > thanks for any help you could provide, and sorry for such noob question



  9. Re: accessing files, devices, in kernel modules

    Yellow wrote:

    >>>> but what I'd like to know is: can I use the sound card the same way I
    >>>> use it in user space programs? like reading and opening a file? my
    >>>> final goal is record some sound from the microphone for 3 seconds, and
    >>>> then play it.


    > thank you. now I understand that I shouldn't read/write files from a
    > module.


    Why do that in the kernel? That sounds like an user level operation.

  10. Re: accessing files, devices, in kernel modules

    yes, that's indeed a user level operation. the soundcard thing was just
    an abstraction. to the real problem: I need to acquire the signal of an
    A/D converter. this operation is analog to acquiring sound from a sound
    card. this A/D will be plugged in a sort of device that measures
    vibrations from machines that has engine. this device is called
    accelerometer. then, I must pass the data to other device. everything
    must be real time. probably hard real time. and everything will be done
    using an ARM architecture.
    I got Rtai (https://www.rtai.org/), which consists of a patch to the
    kernel and a library to create real time programs. I compiled it.
    They've done quite a good job. I can make some user level programs hard
    real time, but they can't make any system call. So it won't work for
    me, because I need access to the A/D converter.
    If I want to access devices (make system calls), I must make my program
    a kernel module. that's what is said in the rtai manuals. this is my
    little story

    I may also use uclinux, which is just linux without the MMU
    implemented. it's not a real time OS, but it's quite fast, because it
    won't need any switch of program address space. obviously, it makes
    programming a little trickier.
    but my doubt with uclinux is if its programming model is user level
    programs, instead of kernel module.

    sorry for such a long post
    regards,


    Joe Beanfish wrote:
    > Yellow wrote:
    >
    > >>>> but what I'd like to know is: can I use the sound card the same way I
    > >>>> use it in user space programs? like reading and opening a file? my
    > >>>> final goal is record some sound from the microphone for 3 seconds, and
    > >>>> then play it.

    >
    > > thank you. now I understand that I shouldn't read/write files from a
    > > module.

    >
    > Why do that in the kernel? That sounds like an user level operation.



+ Reply to Thread