4

I'm learning how to develop a simple driver for a card connected to the USB1 serial. I wanted to know how the kernel understand that my device needs to be mapped as /dev/ebbchar. How to change to /dev/ebbGV? and if i connect another device why i dont view /dev/ebbchar but /dev/tty0? thanks. In short how kernel do to understand that board connect to USB1 is ebbchar or another device??

My driver's init function is:

static int __init ebbchar_init(void){
   printk(KERN_INFO "EBBChar: Initializing the EBBChar LKM\n");

   // Try to dynamically allocate a major number for the device -- more difficult but worth it
   majorNumber = register_chrdev(0, ebbchar, &fops);
   if (majorNumber<0){
      printk(KERN_ALERT "EBBChar failed to register a major number\n");
      return majorNumber;
   }
   printk(KERN_INFO "EBBChar: registered correctly with major number %d\n", majorNumber);

   // Register the device class
   ebbcharClass = class_create(THIS_MODULE, ebb);
   if (IS_ERR(ebbcharClass)){                // Check for error and clean up if there is
      unregister_chrdev(majorNumber, ebbchar);
      printk(KERN_ALERT "Failed to register device class\n");
      return PTR_ERR(ebbcharClass);          // Correct way to return an error on a pointer
   }
   printk(KERN_INFO "EBBChar: device class registered correctly\n");

   // Register the device driver
   ebbcharDevice = device_create(ebbcharClass, NULL, MKDEV(majorNumber, 0), NULL, DEVICE_NAME);
   if (IS_ERR(ebbcharDevice)){               // Clean up if there is an error
      class_destroy(ebbcharClass);           // Repeated code but the alternative is goto statements
      unregister_chrdev(majorNumber, ebbchar);
      printk(KERN_ALERT "Failed to create the device\n");
      return PTR_ERR(ebbcharDevice);
   }
   printk(KERN_INFO "EBBChar: device class created correctly\n"); // Made it! device was initialized
   return 0;
}

THANKS

user3589887
  • 139
  • 2
  • 13
  • 2
    Well, to begin with, what's that `DEVICE_NAME` stuck in the `device_create()` call? – user May 19 '17 at 12:48
  • yes for take file.. but how kernel understands that device plugged into usb is my device for this driver or no? – user3589887 May 19 '17 at 13:01
  • 1
    Before you try to solve your kernel programming problem, I recommend that you read about how things work from the userland perspective. [This post about udev](https://unix.stackexchange.com/questions/45791/understand-output-of-udevadm-info-a-n-dev-sdb/45794#45794) and [this post about module aliases](https://unix.stackexchange.com/questions/43699/debian-does-not-detect-serial-pci-card-after-reboot/43723#43723) should be useful background. – Gilles 'SO- stop being evil' May 19 '17 at 21:48

0 Answers0