user1610075
user1610075

Reputation: 1641

Read from serial port in a kernel module

For my assignment I need to read some strings coming from a serial port. This has to be done in a kernel module, so I can't use stdio library. I'm trying in this way:

#include <linux/module.h>
#include <linux/unistd.h>
#include <asm/io.h>
#include <asm/fcntl.h>
#define SERIAL_PORT "/dev/ttyACM0"

void myfun(void){
  int fd = open(SERIAL_PORT,O_RDONLY | O_NOCTTY);
  ..reading...

}

but it gives me "implicit declaration of function open"

Upvotes: 2

Views: 2397

Answers (3)

cdarke
cdarke

Reputation: 44354

Include the header file for open, usually #include <unistd.h>

Edit:

Not sure about kernel use, but do a man 2 open.

On my version of linux you might need all following:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

depending on the macros you are using.

Upvotes: -2

Mike
Mike

Reputation: 49403

You want to use the filp_open() function, it is pretty much a helper to open a file in kernelspace. You can find the man on it here

The file pointer from filp_open() is of type struct file and don't forget to close it with filp_close() when you're done:

#include <linux/fs.h>
//other includes...

//other code...    

struct file *filp = filp_open("/dev/ttyS0");
//do serial stuff...
filp_close(filp);

Upvotes: 2

dave
dave

Reputation: 4922

Finding your way around the kernel source can be pretty frightening since it's so large. Here's my favorite command: find . -iname "*.[chs]" -print0 | xargs -0 grep -i "<search term>.

A quick description:

  • find (obvious)
  • dot is the root directory of the kernel
  • iname is find the name ignoring case
  • .c .h and .s files contain code - look in them
  • print0 prints them out null terminated as they are found
  • xargs takes an input and uses it as an argument to another command (-0 is to use null terminated)
  • grep - search for string (ignoring case).

So for this search: "int open(" and you'll get some hits with tty in the name (those will be for consoles) - have a look at the code and see if they are what you want.

Upvotes: 1

Related Questions