kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
* Device file not appearing
@ 2021-03-17 15:52 Gregory Anders
  2021-03-17 15:59 ` Greg KH
  0 siblings, 1 reply; 9+ messages in thread
From: Gregory Anders @ 2021-03-17 15:52 UTC (permalink / raw)
  To: kernelnewbies

Hi all,

I'm writing a char device driver and am having trouble getting the file 
to appear under /dev.

This isn't my first rodeo: in fact, I've written a few other drivers in 
the past and they have all worked as expected. This driver is based on 
the source code of those other drivers, so I'm fairly confident I'm 
doing everything correctly. So I'm stumped and looking for help.

Here is what I have in my init function:

     #define DRIVER_NAME "foo"

     static int __init mod_init(void)
     {
         dev_t devno;
         my_class = class_create(THIS_MODULE, DRIVER_NAME);
         ret = alloc_chrdev_region(&devno, 0, MAX_DEVICES, DRIVER_NAME);

         my_major = MAJOR(devno);

         ...
     }

(Note that for brevity I'm omitting a lot of boilerplate/error handling, 
etc. But you can assume it's all there).

My driver is also a network driver; in fact, it's *primarily* a network 
driver and it provides a char device file that is used to configure the 
hardware. I am creating the char device whenever the network device is 
first opened (e.g. 'netdev_open' below is the 'ndo_open' field of the 
'struct netdev_ops'):

     struct private_data {
         struct cdev cdev;
         ...
     }

     static int netdev_open(struct net_device *dev)
     {
         struct private_data *priv = netdev_priv(dev);
         dev_t devno;
         int minor;
         struct device *d;

         minor = ida_alloc_max(&ida, MAX_DEVICES, GFP_KERNEL);

         devno = MKDEV(my_major, minor);
         cdev_init(&priv->cdev, &my_fops);
         cdev_add(&priv->cdev, devno, 1);

         /* This should create a device node with the same name as the
          * network interface, e.g. foo0
          */
         d = device_create(my_class, NULL, devno, priv, dev->name);
         if (IS_ERR(d)) {
             ...
         }

         ...
     }

Again, I'm omitting the error checking for the sake of brevity, but it 
is there in the actual code. This function runs successfully and the 
network device is successfully opened. The 'device_create' function does 
not return an error, but there is nothing beneath /dev as I would 
expect.

I'm really stumped here because everything I've been able to find online 
says that 'device_create' ought to create that device file. I can see my 
class under /sys/class/ and that directory contains a directory with the 
name of the device:

     $ ls -1 /sys/class/my_class/
     foo0

so it looks like the char device *is* being created, there's just no 
corresponding entry under /dev.

Anyway, I know that's a lot of info. If you've made it this far, thanks 
for reading. If you have any insight/advice/suggestions for me I'd 
greatly appreciate it!

Thanks,

Greg

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 9+ messages in thread
* Device file not appearing
@ 2021-03-16 20:25 Gregory Anders
  0 siblings, 0 replies; 9+ messages in thread
From: Gregory Anders @ 2021-03-16 20:25 UTC (permalink / raw)
  To: kernelnewbies

Hi all,

I'm writing a char device driver and am having trouble getting the file 
to appear under /dev.

This isn't my first rodeo: in fact, I've written a few other drivers in 
the past and they have all worked as expected. This driver is based on 
the source code of those other drivers, so I'm fairly confident I'm 
doing everything correctly. So I'm stumped and looking for help.

Here is what I have in my init function:

     #define DRIVER_NAME "foo"

     static int __init mod_init(void)
     {
         dev_t devno;
         my_class = class_create(THIS_MODULE, DRIVER_NAME);
         ret = alloc_chrdev_region(&devno, 0, MAX_DEVICES, DRIVER_NAME);

         my_major = MAJOR(devno);

         ...
     }

(Note that for brevity I'm omitting a lot of boilerplate/error handling, 
etc. But you can assume it's all there).

My driver is also a network driver; in fact, it's *primarily* a network 
driver and it provides a char device file that is used to configure the 
hardware. I am creating the char device whenever the network device is 
first opened (e.g. 'netdev_open' below is the 'ndo_open' field of the 
'struct netdev_ops'):

     struct private_data {
         struct cdev cdev;
         ...
     }

     static int netdev_open(struct net_device *dev)
     {
         struct private_data *priv = netdev_priv(dev);
         dev_t devno;
         int minor;
         struct device *d;

         minor = ida_alloc_max(&ida, MAX_DEVICES, GFP_KERNEL);

         devno = MKDEV(my_major, minor);
         cdev_init(&priv->cdev, &my_fops);
         cdev_add(&priv->cdev, devno, 1);

         /* This should create a device node with the same name as the
          * network interface, e.g. foo0
          */
         d = device_create(my_class, NULL, devno, priv, dev->name);
         if (IS_ERR(d)) {
             ...
         }

         ...
     }

Again, I'm omitting the error checking for the sake of brevity, but it 
is there in the actual code. This function runs successfully and the 
network device is successfully opened. The 'device_create' function does 
not return an error, but there is nothing beneath /dev as I would 
expect.

I'm really stumped here because everything I've been able to find online 
says that 'device_create' ought to create that device file. I can see my 
class under /sys/class/ and that directory contains a directory with the 
name of the device:

     $ ls -1 /sys/class/my_class/
     foo0

so it looks like the char device *is* being created, there's just no 
corresponding entry under /dev.

Anyway, I know that's a lot of info. If you've made it this far, thanks 
for reading. If you have any insight/advice/suggestions for me I'd 
greatly appreciate it!

Thanks,

Greg

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-03-21 18:48 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-17 15:52 Device file not appearing Gregory Anders
2021-03-17 15:59 ` Greg KH
2021-03-17 16:13   ` Gregory Anders
2021-03-17 16:16     ` Greg KH
2021-03-17 16:56       ` Gregory Anders
2021-03-17 17:05         ` Gregory Anders
2021-03-17 17:15         ` Greg KH
2021-03-17 18:26           ` Gregory Anders
  -- strict thread matches above, loose matches on Subject: below --
2021-03-16 20:25 Gregory Anders

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).