Skip to content
Advertisement

Custom char device linux module under stock major (like “tty”)

I’m writing my first linux kernel module. It is a standard char device to communicate over a GPIO pin.

It works like a charme. It is registered with its device name under /dev and in my own class name under /sys (it has some attributes available for configuration by the host application).

My wish is to move my device under /dev/tty/ and /sys/class/tty/ .

How should I change my below code to accomplish this?

Thank you very much!

  // Allocate major device number

  majorNumber = register_chrdev(0, DEVICE_NAME, &devFileOps);

  if (majorNumber < 0)
  {
    kobject_put(dev_kobj);

    pr_crit(
      "%s/%s: failed to register a major number.n", 
      CLASS_NAME, 
      DEVICE_NAME
    );

    return majorNumber;
  }

  pr_debug(
    "%s/%s: successfully registered with major number %d.n", 
    CLASS_NAME,
    DEVICE_NAME,
    majorNumber
  );

  // Register device class

  devClass = class_create(THIS_MODULE, CLASS_NAME);

  if (IS_ERR(devClass))
  {
    unregister_chrdev(majorNumber, CLASS_NAME); 
    kobject_put(dev_kobj);

    pr_crit(
      "%s/%s: failed to register device class.n", 
      CLASS_NAME,
      DEVICE_NAME
    );

    return PTR_ERR(devClass);
  }

  pr_debug(
    "%s/%s: device class successfully registered.n", 
    CLASS_NAME, 
    DEVICE_NAME
  );

  // Register device driver

  devDevice = device_create(
    devClass, 
    NULL, 
    MKDEV(majorNumber, 0), 
    NULL, 
    DEVICE_NAME
  );

  if (IS_ERR(devDevice))
  {
    class_destroy(devClass);
    unregister_chrdev(majorNumber, CLASS_NAME);
    kobject_put(dev_kobj);

    pr_crit("%s/%s: failed to create the device.n", CLASS_NAME, DEVICE_NAME);

    return PTR_ERR(devDevice);
  }

Advertisement

Answer

If you want your device to be exposed as a TTY, implement it as a serial driver, similar to other devices in drivers/tty/serial. The kernel will apply the TTY layer to it, and it will show up as a TTY.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement