LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (https://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   Kernel driver for 2 (https://www.linuxquestions.org/questions/linux-kernel-70/kernel-driver-for-2-a-4175631113/)

zvivered 06-03-2018 01:30 PM

Kernel driver for 2
 
Hello,

The following code was copied from:
http://true-random.com/homepage/proj...pci_template.c

Code:


static struct file_operations _template_fops =
{
  .owner = THIS_MODULE,
  .read = _template_read,
  .write = _template_write,
  .open = _template_open,
  .release = _template_close,
};

static struct
pci_driver pci_drv_template =
{
  .name= "pci_drv_template",
  .id_table= pci_drv_ids,
  .probe= device_init,
  .remove= device_deinit,
};

static
int __init pci_drv_init(void)
{
  int i_result=0;

  printk (KERN_CRIT "pci_chrdev_template: initialising\n");
 
  // dynamic major number allocation
  rc = register_chrdev (0, "pci_chrdev_template", &_template_fops);
  if (rc< 0)
  {
      printk (KERN_CRIT "pci_chrdev_template: Cannot register device.\n");
      return (i_result);
  }
  printk ("major=%d
  rc=pci_register_driver (&pci_drv_template);
  if (rc < 0)
    return rc;

  return rc;
}

Now I ran:
mknod /dev/pci_template0 c 248 0
mknod /dev/pci_template1 c 248 1

After opening "/dev/pci_template0", if I send IOCTL, how the kernel driver knows that I sent to minor=0 ?

Thank you,
Zvika

X-LFS-2010 06-03-2018 05:23 PM

very generally speaking, the files in /dev/ are not disk files they are sockets in a special file system mounted by the kernel. the drivers read and write from them, and their (file attributes i believe) contain the major minor. the kernel only sends the data you use to a driver who (has opened) the same major minor socket via the kernel's open function.

zvivered 06-04-2018 01:13 AM

Dear Members,

The solution I found (hope it's right):

In /dev I created 2 nodes:
mknod my0 c 248 0
mknod my1 c 248 1

In kernel I can extract the minor number with:

static int MyOpen (struct inode *inode, struct file *fl)
{
int Minor = MINOR (inode->i_rdev);
}

static long MyIoctl (strcut file *file, unsigned int IoctlCode, unsigned long IoctlParam)
{
int Minor = MINOR (file->f_inode->i_rdev);
}

In user space I open my? and send IOCTL to the driver.

Thank you,
Zvika


All times are GMT -5. The time now is 07:35 AM.