@@ -68,86 +68,23 @@ static struct i2c_driver i2c_counter_driver = {
68
68
/* -- Device Addition -- */
69
69
MODULE_DEVICE_TABLE (i2c , i2c_counter_id );
70
70
71
- static int rtcntr_i2c_create_cdev (struct rtcnt_device_info * dev_info )
72
- {
73
- int minor ;
74
- int alloc_ret = 0 ;
75
- int cdev_err = 0 ;
76
- dev_t dev ;
77
-
78
- /* 空いているメジャー番号を確保する */
79
- alloc_ret = alloc_chrdev_region (& dev , DEV_MINOR , NUM_DEV [ID_DEV_CNT ],
80
- DEVNAME_CNTR );
81
- if (alloc_ret != 0 ) {
82
- printk (KERN_ERR "alloc_chrdev_region = %d\n" , alloc_ret );
83
- return -1 ;
84
- }
85
-
86
- /* 取得したdev( = メジャー番号 + マイナー番号)
87
- * からメジャー番号を取得して保持しておく */
88
- dev_info -> device_major = MAJOR (dev );
89
- dev = MKDEV (dev_info -> device_major , DEV_MINOR );
90
-
91
- /* cdev構造体の初期化とシステムコールハンドラテーブルの登録 */
92
- cdev_init (& dev_info -> cdev , & dev_fops [ID_DEV_CNT ]);
93
- dev_info -> cdev .owner = THIS_MODULE ;
94
-
95
- /* このデバイスドライバ(cdev)をカーネルに登録する */
96
- cdev_err = cdev_add (& dev_info -> cdev , dev , NUM_DEV [ID_DEV_CNT ]);
97
- if (cdev_err != 0 ) {
98
- printk (KERN_ERR "cdev_add = %d\n" , alloc_ret );
99
- unregister_chrdev_region (dev , NUM_DEV [ID_DEV_CNT ]);
100
- return -1 ;
101
- }
102
-
103
- /* このデバイスのクラス登録をする(/sys/class/mydevice/ を作る) */
104
- #if LINUX_VERSION_CODE < KERNEL_VERSION (6 , 4 , 0 )
105
- dev_info -> device_class = class_create (THIS_MODULE , DEVNAME_CNTR );
106
- #else
107
- dev_info -> device_class = class_create (DEVNAME_CNTR );
108
- #endif
109
-
110
- if (IS_ERR (dev_info -> device_class )) {
111
- printk (KERN_ERR "class_create\n" );
112
- cdev_del (& dev_info -> cdev );
113
- unregister_chrdev_region (dev , NUM_DEV [ID_DEV_CNT ]);
114
- return -1 ;
115
- }
116
-
117
- for (minor = DEV_MINOR ; minor < DEV_MINOR + NUM_DEV [ID_DEV_CNT ];
118
- minor ++ ) {
119
-
120
- struct device * dev_ret ;
121
- dev_ret = device_create (dev_info -> device_class , NULL ,
122
- MKDEV (dev_info -> device_major , minor ),
123
- NULL , "rtcounter_r%d" , minor );
124
-
125
- /* デバイスファイル作成の可否を判定 */
126
- if (IS_ERR (dev_ret )) {
127
- /* デバイスファイルの作成に失敗した */
128
- printk (KERN_ERR "device_create failed minor = %d\n" ,
129
- minor );
130
- /* リソースリークを避けるために登録された状態cdevを削除する
131
- */
132
- cdev_del (& (cdev_array [cdev_index ]));
133
- return PTR_ERR (dev_ret );
134
- }
135
- }
136
-
137
- return 0 ;
138
- }
139
-
140
71
// called by rtcnt_i2c_probe()
141
- static int rtcntl_i2c_create_cdev (struct rtcnt_device_info * dev_info )
72
+ static int rtcnt_i2c_create_cdev (struct rtcnt_device_info * dev_info ,
73
+ const int dev_side )
142
74
{
143
75
int minor ;
144
76
int alloc_ret = 0 ;
145
77
int cdev_err = 0 ;
146
78
dev_t dev ;
147
79
148
80
/* 空いているメジャー番号を確保する */
149
- alloc_ret = alloc_chrdev_region (& dev , DEV_MINOR , NUM_DEV [ID_DEV_CNT ],
150
- DEVNAME_CNTL );
81
+ if (dev_side == DEV_LEFT ) {
82
+ alloc_ret = alloc_chrdev_region (
83
+ & dev , DEV_MINOR , NUM_DEV [ID_DEV_CNT ], DEVNAME_CNTL );
84
+ } else if (dev_side == DEV_RIGHT ) {
85
+ alloc_ret = alloc_chrdev_region (
86
+ & dev , DEV_MINOR , NUM_DEV [ID_DEV_CNT ], DEVNAME_CNTR );
87
+ }
151
88
if (alloc_ret != 0 ) {
152
89
printk (KERN_ERR "alloc_chrdev_region = %d\n" , alloc_ret );
153
90
return -1 ;
@@ -172,9 +109,19 @@ static int rtcntl_i2c_create_cdev(struct rtcnt_device_info *dev_info)
172
109
173
110
/* このデバイスのクラス登録をする(/sys/class/mydevice/ を作る) */
174
111
#if LINUX_VERSION_CODE < KERNEL_VERSION (6 , 4 , 0 )
175
- dev_info -> device_class = class_create (THIS_MODULE , DEVNAME_CNTL );
112
+ if (dev_side == DEV_LEFT ) {
113
+ dev_info -> device_class =
114
+ class_create (THIS_MODULE , DEVNAME_CNTL );
115
+ } else if (dev_side == DEV_RIGHT ) {
116
+ dev_info -> device_class =
117
+ class_create (THIS_MODULE , DEVNAME_CNTR );
118
+ }
176
119
#else
177
- dev_info -> device_class = class_create (DEVNAME_CNTL );
120
+ if (dev_side == DEV_LEFT ) {
121
+ dev_info -> device_class = class_create (DEVNAME_CNTL );
122
+ } else if (dev_side == DEV_RIGHT ) {
123
+ dev_info -> device_class = class_create (DEVNAME_CNTR );
124
+ }
178
125
#endif
179
126
180
127
if (IS_ERR (dev_info -> device_class )) {
@@ -189,9 +136,17 @@ static int rtcntl_i2c_create_cdev(struct rtcnt_device_info *dev_info)
189
136
minor ++ ) {
190
137
191
138
struct device * dev_ret ;
192
- dev_ret = device_create (dev_info -> device_class , NULL ,
193
- MKDEV (dev_info -> device_major , minor ),
194
- NULL , "rtcounter_l%d" , minor );
139
+ if (dev_side == DEV_LEFT ) {
140
+ dev_ret =
141
+ device_create (dev_info -> device_class , NULL ,
142
+ MKDEV (dev_info -> device_major , minor ),
143
+ NULL , "rtcounter_l%d" , minor );
144
+ } else if (dev_side == DEV_RIGHT ) {
145
+ dev_ret =
146
+ device_create (dev_info -> device_class , NULL ,
147
+ MKDEV (dev_info -> device_major , minor ),
148
+ NULL , "rtcounter_r%d" , minor );
149
+ }
195
150
196
151
/* デバイスファイル作成の可否を判定 */
197
152
if (IS_ERR (dev_ret )) {
@@ -260,10 +215,10 @@ static int rtcnt_i2c_probe(struct i2c_client *client,
260
215
261
216
/* create character device */
262
217
if ((int )(id -> driver_data ) == 0 ) {
263
- if (rtcntl_i2c_create_cdev (dev_info ))
218
+ if (rtcnt_i2c_create_cdev (dev_info , DEV_LEFT ))
264
219
return - ENOMEM ;
265
220
} else if ((int )(id -> driver_data ) == 1 ) {
266
- if (rtcntr_i2c_create_cdev (dev_info ))
221
+ if (rtcnt_i2c_create_cdev (dev_info , DEV_RIGHT ))
267
222
return - ENOMEM ;
268
223
}
269
224
@@ -301,10 +256,10 @@ static int rtcnt_i2c_probe(struct i2c_client *client)
301
256
302
257
/* create character device */
303
258
if ((int )(id -> driver_data ) == 0 ) {
304
- if (rtcntl_i2c_create_cdev (dev_info ))
259
+ if (rtcnt_i2c_create_cdev (dev_info , DEV_LEFT ))
305
260
return - ENOMEM ;
306
261
} else if ((int )(id -> driver_data ) == 1 ) {
307
- if (rtcntr_i2c_create_cdev (dev_info ))
262
+ if (rtcnt_i2c_create_cdev (dev_info , DEV_RIGHT ))
308
263
return - ENOMEM ;
309
264
}
310
265
0 commit comments