@@ -20,9 +20,9 @@ MODULE_VERSION("0.1");
20
20
#define MAX_LENGTH 92
21
21
22
22
static dev_t fib_dev = 0 ;
23
- static struct cdev * fib_cdev ;
24
23
static struct class * fib_class ;
25
24
static DEFINE_MUTEX (fib_mutex );
25
+ static int major = 0 , minor = 0 ;
26
26
27
27
static long long fib_sequence (long long k )
28
28
{
@@ -107,34 +107,17 @@ const struct file_operations fib_fops = {
107
107
static int __init init_fib_dev (void )
108
108
{
109
109
int rc = 0 ;
110
-
111
110
mutex_init (& fib_mutex );
112
111
113
112
// Let's register the device
114
113
// This will dynamically allocate the major number
115
- rc = alloc_chrdev_region (& fib_dev , 0 , 1 , DEV_FIBONACCI_NAME );
116
-
117
- if (rc < 0 ) {
118
- printk (KERN_ALERT
119
- "Failed to register the fibonacci char device. rc = %i" ,
120
- rc );
121
- return rc ;
122
- }
123
-
124
- fib_cdev = cdev_alloc ();
125
- if (fib_cdev == NULL ) {
126
- printk (KERN_ALERT "Failed to alloc cdev" );
127
- rc = -1 ;
128
- goto failed_cdev ;
129
- }
130
- fib_cdev -> ops = & fib_fops ;
131
- rc = cdev_add (fib_cdev , fib_dev , 1 );
132
-
114
+ rc = major = register_chrdev (major , DEV_FIBONACCI_NAME , & fib_fops );
133
115
if (rc < 0 ) {
134
116
printk (KERN_ALERT "Failed to add cdev" );
135
117
rc = -2 ;
136
118
goto failed_cdev ;
137
119
}
120
+ fib_dev = MKDEV (major , minor );
138
121
139
122
fib_class = class_create (THIS_MODULE , DEV_FIBONACCI_NAME );
140
123
@@ -153,9 +136,8 @@ static int __init init_fib_dev(void)
153
136
failed_device_create :
154
137
class_destroy (fib_class );
155
138
failed_class_create :
156
- cdev_del (fib_cdev );
157
139
failed_cdev :
158
- unregister_chrdev_region ( fib_dev , 1 );
140
+ unregister_chrdev ( major , DEV_FIBONACCI_NAME );
159
141
return rc ;
160
142
}
161
143
@@ -164,8 +146,7 @@ static void __exit exit_fib_dev(void)
164
146
mutex_destroy (& fib_mutex );
165
147
device_destroy (fib_class , fib_dev );
166
148
class_destroy (fib_class );
167
- cdev_del (fib_cdev );
168
- unregister_chrdev_region (fib_dev , 1 );
149
+ unregister_chrdev (major , DEV_FIBONACCI_NAME );
169
150
}
170
151
171
152
module_init (init_fib_dev );
0 commit comments