Skip to content

Commit 2ddf4cf

Browse files
committed
rtcntl_*とrtcntr_*を一つの関数にまとめる
1 parent 5bdf8e3 commit 2ddf4cf

File tree

1 file changed

+17
-80
lines changed

1 file changed

+17
-80
lines changed

src/drivers/rtmouse_i2c.c

Lines changed: 17 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -68,77 +68,8 @@ static struct i2c_driver i2c_counter_driver = {
6868
/* -- Device Addition -- */
6969
MODULE_DEVICE_TABLE(i2c, i2c_counter_id);
7070

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-
14071
// 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, const char *devname_cnt)
14273
{
14374
int minor;
14475
int alloc_ret = 0;
@@ -147,7 +78,7 @@ static int rtcntl_i2c_create_cdev(struct rtcnt_device_info *dev_info)
14778

14879
/* 空いているメジャー番号を確保する */
14980
alloc_ret = alloc_chrdev_region(&dev, DEV_MINOR, NUM_DEV[ID_DEV_CNT],
150-
DEVNAME_CNTL);
81+
devname_cnt);
15182
if (alloc_ret != 0) {
15283
printk(KERN_ERR "alloc_chrdev_region = %d\n", alloc_ret);
15384
return -1;
@@ -172,9 +103,9 @@ static int rtcntl_i2c_create_cdev(struct rtcnt_device_info *dev_info)
172103

173104
/* このデバイスのクラス登録をする(/sys/class/mydevice/ を作る) */
174105
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
175-
dev_info->device_class = class_create(THIS_MODULE, DEVNAME_CNTL);
106+
dev_info->device_class = class_create(THIS_MODULE, devname_cnt);
176107
#else
177-
dev_info->device_class = class_create(DEVNAME_CNTL);
108+
dev_info->device_class = class_create(devname_cnt);
178109
#endif
179110

180111
if (IS_ERR(dev_info->device_class)) {
@@ -189,9 +120,15 @@ static int rtcntl_i2c_create_cdev(struct rtcnt_device_info *dev_info)
189120
minor++) {
190121

191122
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);
123+
if (devname_cnt == DEVNAME_CNTL) {
124+
dev_ret = device_create(dev_info->device_class, NULL,
125+
MKDEV(dev_info->device_major, minor),
126+
NULL, "rtcounter_l%d", minor);
127+
} else if (devname_cnt == DEVNAME_CNTR) {
128+
dev_ret = device_create(dev_info->device_class, NULL,
129+
MKDEV(dev_info->device_major, minor),
130+
NULL, "rtcounter_r%d", minor);
131+
}
195132

196133
/* デバイスファイル作成の可否を判定 */
197134
if (IS_ERR(dev_ret)) {
@@ -260,10 +197,10 @@ static int rtcnt_i2c_probe(struct i2c_client *client,
260197

261198
/* create character device */
262199
if ((int)(id->driver_data) == 0) {
263-
if (rtcntl_i2c_create_cdev(dev_info))
200+
if (rtcnt_i2c_create_cdev(dev_info, DEVNAME_CNTL))
264201
return -ENOMEM;
265202
} else if ((int)(id->driver_data) == 1) {
266-
if (rtcntr_i2c_create_cdev(dev_info))
203+
if (rtcnt_i2c_create_cdev(dev_info, DEVNAME_CNTR))
267204
return -ENOMEM;
268205
}
269206

@@ -301,10 +238,10 @@ static int rtcnt_i2c_probe(struct i2c_client *client)
301238

302239
/* create character device */
303240
if ((int)(id->driver_data) == 0) {
304-
if (rtcntl_i2c_create_cdev(dev_info))
241+
if (rtcnt_i2c_create_cdev(dev_info, DEVNAME_CNTL))
305242
return -ENOMEM;
306243
} else if ((int)(id->driver_data) == 1) {
307-
if (rtcntr_i2c_create_cdev(dev_info))
244+
if (rtcnt_i2c_create_cdev(dev_info, DEVNAME_CNTR))
308245
return -ENOMEM;
309246
}
310247

0 commit comments

Comments
 (0)