LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 06-03-2018, 02:30 PM   #1
zvivered
Member
 
Registered: Sep 2007
Posts: 183

Rep: Reputation: 23
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

Last edited by zvivered; 06-03-2018 at 04:19 PM.
 
Old 06-03-2018, 06:23 PM   #2
X-LFS-2010
Member
 
Registered: Apr 2016
Posts: 382

Rep: Reputation: Disabled
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.
 
Old 06-04-2018, 02:13 AM   #3
zvivered
Member
 
Registered: Sep 2007
Posts: 183

Original Poster
Rep: Reputation: 23
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Confused about linux kernel driver and module driver amishera Linux - Kernel 5 06-11-2013 02:42 PM
Trying to install ethernet adapter driver, not finding kernel-devel or kernel-headers oneFishtwoFish Red Hat 5 08-31-2010 07:24 PM
Nvidia Driver will not install for Dual Screen LCD - Driver is mde for Xen Kernel isinkaye Linux - Software 1 02-20-2010 05:31 AM
Driver IO Busywait Kernel Driver Question skibud2 Programming 2 10-27-2008 08:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 02:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration