From 77c090b85d2dc085dce3712d4a5faf8452531d58 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Thu, 3 May 2018 20:06:14 +0200 Subject: Store and use minor number --- char.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'char.c') diff --git a/char.c b/char.c index 7fe7652..b13b08a 100644 --- a/char.c +++ b/char.c @@ -5,6 +5,7 @@ #include "linux/fs.h" #include "util.h" +#include "pci.h" #define DOOMDEV_COUNT 256 #define DOOMDEV_NAME "doom" @@ -32,13 +33,14 @@ struct file_operations doomdev_fops = { struct class *doom_class; -int new_doomdev(void) +int new_doomdev(struct pci_dev *dev) { int err = 0; int minor; dev_t devt; struct cdev *doom_cdev; struct device *doomdev; + struct doom_data *doom_data; if (next_minor >= DOOMDEV_COUNT) { return -ENOMEM; } @@ -51,6 +53,9 @@ int new_doomdev(void) doom_cdev->ops = &doomdev_fops; ORFAIL(cdev_add(doom_cdev, first, 1), error_add); minor = next_minor++; + doom_data = kmalloc(sizeof(*doom_data), GFP_KERNEL); + doom_data->minor = minor; + pci_set_drvdata(dev, doom_data); devt = MKDEV(major, minor); ORFAIL_PTR(device_create(doom_class, NULL, devt, NULL, "doom%d", minor), @@ -62,9 +67,12 @@ error_add: return err; } -void destroy_doomdev(void) +void destroy_doomdev(struct pci_dev *dev) { - device_destroy(doom_class, first); + struct doom_data *data; + data = pci_get_drvdata(dev); + device_destroy(doom_class, MKDEV(major, data->minor)); + kfree(data); } int char_init(void) -- cgit v1.2.3