Creating new device node

Hi
Following code snippet used to create a node and assign major/minor number, but unable to see the device node in /dev.
when I run the RM in background as suggested in the earlier post, I get pid number and “Done” , but when I execute the pidin command to check whether RM is running it is actually not executing, all these issues started facing after modifying RM for updating major and minor please provide suggestions as I neglected it earlier and now running back to correct it. Please check code snippet below

iofunc_attr_init (&dev->attr, S_IFCHR | 0666, 0, 0);
iofunc_func_init (_RESMGR_CONNECT_NFUNCS, &dev->connect_funcs,
_RESMGR_IO_NFUNCS, &dev->io_funcs);

if((dev->attr.rdev = rsrcdbmgr_devno_attach("i2c1", -1, 0))!=-1){

    dev->connect_funcs.open = i2c_io_open;
    dev->io_funcs.close_ocb = i2c_io_close;
    dev->io_funcs.read = i2c_io_read;
    dev->io_funcs.write = i2c_io_write;
    dev->io_funcs.devctl = i2c_io_devctl;
    if ((dev->resmgr_id=resmgr_attach (dev->i2c_dispatch, &resmgr_attr, "/dev/i2c1", _FTYPE_ANY,
                      0, &dev->connect_funcs, &dev->io_funcs, &dev->attr)) == -1) {
        printf("Unable to resmgr_attach");
        return EXIT_FAILURE;
    }
    else {
        resmgr_devino( dev->resmgr_id, &dev->io_mount.dev, &dev->attr.inode );
        printf("Done\n");
        return(EOK);
    }
    rsrcdbmgr_devno_detach( dev->attr.rdev, 0 );
    // allocate a context
    dev->i2c_ctp = dispatch_context_alloc (dev->i2c_dispatch);

    // wait here forever, handling messages
    while (1) {
       if ((dev->i2c_ctp = dispatch_block (dev->i2c_ctp)) == NULL) {
           printf ("Unable to dispatch_block");
           exit (EXIT_FAILURE);
    }
    dispatch_handler (dev->i2c_ctp);
 }
}
else{
    printf("Unable to acquire devno\n");

}

See this:

if ((dev->resmgr_id=resmgr_attach (dev->i2c_dispatch, &resmgr_attr, "/dev/i2c1", _FTYPE_ANY,
0, &dev->connect_funcs, &dev->io_funcs, &dev->attr)) == -1) {
    printf("Unable to resmgr_attach");
    return EXIT_FAILURE;
}
else {
resmgr_devino( dev->resmgr_id, &dev->io_mount.dev, &dev->attr.inode );
    printf("Done\n");
    return(EOK);
}

BOTH paths of your ‘if’ statement are returning. Since you are in your ‘main’ that means you are exiting the program.

Tim

Tim,Thanks for correcting me. Removed return(EOK) after resmgr_devino, still problem persists. Please look into following log

./proc/boot/i2c-sensor &

[1] 16401
Done

ls -l /dev/i2c1

I2C Master driver opened
ls: No such process (/dev/i2c1)
[1] + Memory fault ./proc/boot/i2c-sensor

From Client program ‘open("/dev/i2c1", O_RDWR)’ also fails.

./proc/boot/i2c-sensor &

[1] 24593
Done

ls /dev/

bpf io-usb ptyp4 snd tap3 ttyp6
bpf0 mem ptyp5 socket text ttyp7
console null ptyp6 stderr tty tun0
dbgmem pipe ptyp7 stdin ttyp0 tun1
fs0 profiler sem stdout ttyp1 tun2
fs0p0 ptyp0 ser1 tap ttyp2 tun3
hd0 ptyp1 ser3 tap0 ttyp3 tymem
hd0t12 ptyp2 shmem tap1 ttyp4 zero
i2c1 ptyp3 slog tap2 ttyp5

./proc/boot/se

sensor-app setkey

./proc/boot/sensor-app

I2C Sensor MMA8451Q Application Program
I2C Master driver opened (RM open operation)
ERROR: open(-1) failed
[1] + Memory fault ./proc/boot/i2c-sensor

The ‘memory fault’ indicates your i2c-sensor program is crashing.

Why, I don’t know. You’ll have to debug the code and find out where it’s happening and then figure out why. You can do that with the debugger OR by adding printf’s to the code to see where it happens.

Tim

Thanks for the reply, but why client program ‘open’ should fail. I am just taking sample RM and executing it. Is the device node creation procedure is correct. Following code snippet,
if((dev->attr.rdev = rsrcdbmgr_devno_attach(“i2c1”, -1, 0))!=-1), should I pass i2c1 or _MAJOR_DEV?. In both the cases it tells /dev/i2c1 'No such process ’ or ‘Unable to initialize the device /dev/i2c1’ when I run ‘pdebug /dev/i2c1’

“i2c1” is correct.

If you just do a ‘ls /dev’ do you see your i2c1 entry and your RM doesn’t crash? If that’s true then the problem is in the code that handles the ‘open’ request which I don’t see (it would help if you post more of your RM code. Don’t need the I2C code).

Not sure what you expect ‘pdebug /dev/i2c1’ to do. That’s not how you debug your RM with the debugger. You have to attach to the RM process, not the /dev entry it creates.

Tim

Tim, Please see the attachment, basic code which I am trying to control the open and close functions of RM from client program.
As you suggested earlier RM is executed as background process, I am able to see i2c1 when I do ls /dev/ and RM doesn’t crash, it get crashed when I do ls /dev/i2c1.

Two things.

  1. You are assigning your custom functions after doing the resmgr_attach() call. The doc’s always show these being done before this call. Move your i2c_io_open and close call assignments before the attach.
  2. To help facilitate debugging better I suggest adding another printf in your while(1) loop. Add it after your if statement and before you do the dispatch_handler() call so you are reaching this point.

I would also suggest creating i2c_io_read() and write() functions even if they just have a printf in them. Then assign those as too where you are doing your io_open assignment.

Tim

Tim,As suggested added a printf before dispatch_handler() it never printed, added printf after 'if ’ condition it never reached

if ((dev->i2c_ctp = dispatch_block (dev->i2c_ctp)) == NULL) {
printf (“Unable to dispatch_block\n”);
exit (EXIT_FAILURE);
}

Added printf before if, it printed once

while (1) {
printf(“inside while\n”);
if ((dev->i2c_ctp = dispatch_block (dev->i2c_ctp)) == NULL) {
printf (“Unable to dispatch_block\n”);
exit (EXIT_FAILURE);
}
printf(“b4 dispatch handler\n”);
dispatch_handler (dev->i2c_ctp);
}

As suggested I have moved open/close/read/write calls before resmgr_attach(), still it fails and gives memory fault. Spent a lot of time, I am just extracting the RM code as in example code and printer RM shared to me, hope I have shared the code which I am working right now. Requesting to suggest/ correct me where things are going wrong, so that I can proceed further. Is there any dependency in Momentics 6.5 IDE?

Compiled example RM code generated using Momentics IDE6.5, it doesn’t give any fault or “No such process”

./proc/boot/NeutrinoResourceManager &

[1] 16401
null: starting…

./proc/boot/i2c-sensor &

[2] 20500
inside while

cat /dev/Null

cat /dev/i2c1

/dev/i2c1: No such process
[2] + Memory fault ./proc/boot/i2c-sensor

Since the QNX example one doesn’t crash, why don’t you just use that one instead of yours that is crashing in the dispatch_block() call?

Just add your functions into that RM and replace the NULL name with your i2c1 name. There is nothing special about the NULL name.

The only other things that I noticed is:

  1. When you malloc for your mx6x_i2c_dev_t structure you don’t memset it to 0
  2. your iofunc_attr_init() function you specify S_IFCHR vs S_IFNAM

Tim

P.S. Here’s another sample RM code you can look at that might help.
himmele.googlecode.com/svn/trunk … eManager.c

From the link shared, I wrote a client app to read from the RM, but it fails. Following is the code snippet

            memset( &data, 0, sizeof(data));
	/*Dummy read/write routine*/

	if(read(fd,&data,sizeof(data))!=sizeof(data)){
				printf("read failed\n");
				return -1;
	}

Any suggestions on this.

I am writing a module for the Linux kernel and also I want to create some device nodes in the init function

int init_module(void)
{
Major = register_chrdev(0, DEVICE_NAME, &fops);
// Now I want to create device nodes with the returned major number
}

I also want the kernel to assign a minor number for my first node, and then I will assign the other nodes’ minor numbers by myself.
How can I do this in the code. I don’t want to create devices from the shell using mknod.


#Veda
Working for resume writing service

Uhhh, this is probably not the best place to ask questions about writing a Linux driver.

By “place” I mean this website. It’s dedicated to QNX, not Linux. The two are almost entirely different at the level of writing a driver.