LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (https://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   Module with strange behavior (https://www.linuxquestions.org/questions/linux-kernel-70/module-with-strange-behavior-4175453235/)

_core 03-08-2013 03:43 AM

Module with strange behavior
 
Hi,

i wrote a kernel-module for linux-kernel (3.X). So, ever when i'm loading the module for the first time, it works fine. when i remove it and load it again, i can't use it.

That module is a high-level-driver for the serial-port. i use inputattach to bind the module to a port. It also uses the input-subsystem to create a jsX and an eventX node. yes, it is an input-device-driver for the serial port :) .

here something from syslog:

first load + bind with inputattach
Code:

connect called
input: MyDevice as /devices/pnp0/00:0a/tty/ttyS0/serio0/input/input13

endbind port (kill inputattach)
Code:

disconnect called
bind again with inputattach
Code:

connect called
input: MyDevice as /devices/pnp0/00:0a/tty/ttyS0/serio1/input/input14

endbind port (kill inputattach)
Code:

disconnect called
bind again with inputattach
Code:

connect called
input: MyDevice as /devices/pnp0/00:0a/tty/ttyS0/serio2/input/input15

i can do so for hours.

now i remove the module (after killing inputattach) and load it again + bind with inputattach
Code:

connect called
input: MyDevice as /devices/pnp0/00:0a/tty/ttyS0/serio3/input/input16
disconnect called

so right after it calles the connect function, the disconnect function is called. but i have no idea why. it looks very strange to me that it works fine at the first time, but second, third, ... time it calles disconnect right after connect.

so has anyone of you an idea? i will post my code (it will be open source anyway), if you need it.


thanks!

bsat 03-09-2013 11:20 PM

With out looking at the code my best guess is you are missing something in the disconnect part, it is not completely disconnecting, thus when the next connect is attempted it leads to immediate disconnect. But again that is just a guess.

_core 03-10-2013 08:19 AM

OK,

here are my init, exit, connect and disconnect functions:

init and exit
Code:

static int __init xj_init(void)
{
        return serio_register_driver(&xj_drv);
}

static void __exit xj_exit(void)
{
        serio_unregister_driver(&xj_drv);
}

connect
Code:

static int xj_connect(struct serio* serio, struct serio_driver* drv)
{
        struct ser_drv_xj* xj;
        struct input_dev* input_dev;
        struct ff_device* ff;
        int err = -ENOMEM;
        int ff_effects = 1;
       
        xj = kzalloc(sizeof(struct ser_drv_xj), GFP_KERNEL);
        if (!xj)
                goto fail1;

        xj->serio = serio;
        serio_set_drvdata(serio, xj);
        err = serio_open(serio, drv);
        if (err)
                goto fail2;
       
        input_dev = input_allocate_device();
        if (!input_dev)
                goto fail3;

        xj->dev = input_dev;
        input_set_drvdata(input_dev, xj);       
       
        input_dev->name      = DEVICE_NAME;
        input_dev->id.bustype = BUS_RS232;
        input_dev->id.vendor  = SERIO_UNKNOWN;
        input_dev->id.product = 0x0001;
        input_dev->id.version = 0x0100;
        input_dev->dev.parent = &xj->serio->dev;
        input_dev->evbit[0]  = BIT_MASK(EV_ABS) | BIT_MASK(EV_FF_STATUS);
       
        input_set_abs_params(input_dev, ABS_WHEEL, SCALED_ANGLE_MIN, SCALED_ANGLE_MAX, 0, 0);
        set_bit(ABS_WHEEL, input_dev->ffbit);
        set_bit(FF_CONSTANT, input_dev->ffbit);
        err = input_ff_create(input_dev, ff_effects);
        if (err)
                goto fail4;
               
        ff                  = input_dev->ff;
        ff->erase            = xj_erase_effect;
        ff->upload            = xj_upload_effect;
        ff->set_gain            = xj_set_gain;
        ff->set_autocenter = xj_set_autocenter;
        ff->playback            = xj_playback;

        err = input_register_device(xj->dev);
        if (err)
                goto fail4;
               
        xj->endstop          = STEP_DEFAULT;
        xj->event        = kzalloc(sizeof(struct ff_event), GFP_KERNEL);
        xj->event->frame = kzalloc(sizeof(serial_protocol_frame_t), GFP_KERNEL);
        spin_lock_init(&(xj->event->lock));
        xj->event->last_update = jiffies_to_msecs(jiffies);
        xj->event->cycle = 100;
 
        return 0;
       
 fail4:       
        input_free_device(input_dev);
 fail3:       
        serio_close(serio);
 fail2:       
        serio_set_drvdata(serio, NULL);
        kfree(xj);
 fail1:
        return err;
}

disconnect
Code:

static void xj_disconnect(struct serio *serio)
{
        struct ser_drv_xj *xj;
   
        xj = serio_get_drvdata(serio);

        input_unregister_device(xj->dev);
        serio_close(serio);
        serio_set_drvdata(serio, NULL);
        kfree(xj);
}

and the fops for the serial interface:
Code:

static struct serio_device_id xj_serio_ids[] = {
        {
                .type        = SERIO_RS232,
                .proto        = SERIO_UNKNOWN,
                .id        = SERIO_ANY,
                .extra        = SERIO_ANY,
        },
        { 0 }
};

MODULE_DEVICE_TABLE(serio, xj_serio_ids);

static struct serio_driver xj_drv = {
        .driver                =
        {
            .name = DRIVER_NAME,
        },
        .description        = DRIVER_DESC,
        .id_table        = xj_serio_ids,
        .interrupt        = xj_interrupt,
        .connect        = xj_connect,
        .disconnect        = xj_disconnect,
        .write_wakeup  = xj_write_wakeup
};

thanks for your effort!

bsat 03-10-2013 11:11 PM

The code looks ok.... try adding printks in connect and disconnect to figure out at which step is the disconnect being called.

_core 03-14-2013 04:55 AM

I will do it today.


All times are GMT -5. The time now is 08:54 AM.