From 2eaa35293d31075348fe606bef5a58885d8e09a9 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Fri, 4 May 2018 17:48:18 +0200 Subject: Refactor --- char.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'char.c') diff --git a/char.c b/char.c index 4efcb80..c752713 100644 --- a/char.c +++ b/char.c @@ -1,8 +1,8 @@ #include "char.h" -#include "linux/cdev.h" -#include "linux/device.h" -#include "linux/fs.h" +#include +#include +#include #include "util.h" #include "pci.h" @@ -52,16 +52,17 @@ int new_doomdev(struct pci_dev *dev) doom_data->cdev = cdev_alloc(); if (doom_data->cdev == NULL) { - return -ENOMEM; + err = -ENOMEM; + goto error_cdev; } cdev_init(doom_data->cdev, &doomdev_fops); ORFAIL(cdev_add(doom_data->cdev, first, 1), error_add); minor = next_minor++; devt = MKDEV(major, minor); - ORFAIL_PTR(device_create(doom_class, NULL, devt, NULL, + doom_data->device = device_create(doom_class, &dev->dev, devt, NULL, "doom%d", minor), - doom_data->device, error_create); + ORFAIL_PTR(doom_data->device, error_create); pci_set_drvdata(dev, doom_data); @@ -70,6 +71,7 @@ int new_doomdev(struct pci_dev *dev) error_create: cdev_del(doom_data->cdev); error_add: +error_cdev: kfree(doom_data); error_kmalloc: return err; @@ -78,6 +80,7 @@ error_kmalloc: void destroy_doomdev(struct pci_dev *dev) { struct doom_data *data; + data = pci_get_drvdata(dev); device_destroy(doom_class, data->device->devt); cdev_del(data->cdev); @@ -87,14 +90,12 @@ void destroy_doomdev(struct pci_dev *dev) int char_init(void) { int err = 0; + ORFAIL(alloc_chrdev_region(&first, 0, DOOMDEV_COUNT, DOOMDEV_NAME), error_region); major = MAJOR(first); doom_class = class_create(THIS_MODULE, "doom"); - if (IS_ERR(doom_class)) { - err = PTR_ERR(doom_class); - goto error_create; - } + ORFAIL_PTR(doom_class, error_create); return 0; -- cgit v1.2.3