@@ -68,86 +68,23 @@ static struct i2c_driver i2c_counter_driver = {
6868/* -- Device Addition -- */
6969MODULE_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 ,
73+ const int dev_side )
14274{
14375 int minor ;
14476 int alloc_ret = 0 ;
14577 int cdev_err = 0 ;
14678 dev_t dev ;
14779
14880 /* 空いているメジャー番号を確保する */
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+ }
15188 if (alloc_ret != 0 ) {
15289 printk (KERN_ERR "alloc_chrdev_region = %d\n" , alloc_ret );
15390 return -1 ;
@@ -172,9 +109,19 @@ static int rtcntl_i2c_create_cdev(struct rtcnt_device_info *dev_info)
172109
173110/* このデバイスのクラス登録をする(/sys/class/mydevice/ を作る) */
174111#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+ }
176119#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+ }
178125#endif
179126
180127 if (IS_ERR (dev_info -> device_class )) {
@@ -189,9 +136,17 @@ static int rtcntl_i2c_create_cdev(struct rtcnt_device_info *dev_info)
189136 minor ++ ) {
190137
191138 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+ }
195150
196151 /* デバイスファイル作成の可否を判定 */
197152 if (IS_ERR (dev_ret )) {
@@ -260,10 +215,10 @@ static int rtcnt_i2c_probe(struct i2c_client *client,
260215
261216 /* create character device */
262217 if ((int )(id -> driver_data ) == 0 ) {
263- if (rtcntl_i2c_create_cdev (dev_info ))
218+ if (rtcnt_i2c_create_cdev (dev_info , DEV_LEFT ))
264219 return - ENOMEM ;
265220 } 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 ))
267222 return - ENOMEM ;
268223 }
269224
@@ -301,10 +256,10 @@ static int rtcnt_i2c_probe(struct i2c_client *client)
301256
302257 /* create character device */
303258 if ((int )(id -> driver_data ) == 0 ) {
304- if (rtcntl_i2c_create_cdev (dev_info ))
259+ if (rtcnt_i2c_create_cdev (dev_info , DEV_LEFT ))
305260 return - ENOMEM ;
306261 } 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 ))
308263 return - ENOMEM ;
309264 }
310265
0 commit comments