Skip to content

Commit 3b7a628

Browse files
ivanorlov2206gregkh
authored andcommitted
comedi: make all 'class' structures const
Now that the driver core allows for struct class to be in read-only memory, making all 'class' structures to be declared at build time placing them into read-only memory, instead of having to be dynamically allocated at load time. Cc: Ian Abbott <abbotti@mev.co.uk> Cc: H Hartley Sweeten <hsweeten@visionengravers.com> Cc: Benjamin Tissoires <benjamin.tissoires@redhat.com> Cc: Ivan Orlov <ivan.orlov0322@gmail.com> Cc: Xuezhi Zhang <zhangxuezhi1@coolpad.com> Suggested-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com> Link: https://lore.kernel.org/r/20230620144137.581406-2-gregkh@linuxfoundation.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 9ee202e commit 3b7a628

2 files changed

Lines changed: 35 additions & 35 deletions

File tree

drivers/comedi/comedi_fops.c

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ static DEFINE_MUTEX(comedi_subdevice_minor_table_lock);
9797
static struct comedi_subdevice
9898
*comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS];
9999

100-
static struct class *comedi_class;
101100
static struct cdev comedi_cdev;
102101

103102
static void comedi_device_init(struct comedi_device *dev)
@@ -187,18 +186,6 @@ static struct comedi_device *comedi_clear_board_minor(unsigned int minor)
187186
return dev;
188187
}
189188

190-
static void comedi_free_board_dev(struct comedi_device *dev)
191-
{
192-
if (dev) {
193-
comedi_device_cleanup(dev);
194-
if (dev->class_dev) {
195-
device_destroy(comedi_class,
196-
MKDEV(COMEDI_MAJOR, dev->minor));
197-
}
198-
comedi_dev_put(dev);
199-
}
200-
}
201-
202189
static struct comedi_subdevice *
203190
comedi_subdevice_from_minor(const struct comedi_device *dev, unsigned int minor)
204191
{
@@ -611,6 +598,23 @@ static struct attribute *comedi_dev_attrs[] = {
611598
};
612599
ATTRIBUTE_GROUPS(comedi_dev);
613600

601+
static const struct class comedi_class = {
602+
.name = "comedi",
603+
.dev_groups = comedi_dev_groups,
604+
};
605+
606+
static void comedi_free_board_dev(struct comedi_device *dev)
607+
{
608+
if (dev) {
609+
comedi_device_cleanup(dev);
610+
if (dev->class_dev) {
611+
device_destroy(&comedi_class,
612+
MKDEV(COMEDI_MAJOR, dev->minor));
613+
}
614+
comedi_dev_put(dev);
615+
}
616+
}
617+
614618
static void __comedi_clear_subdevice_runflags(struct comedi_subdevice *s,
615619
unsigned int bits)
616620
{
@@ -3263,7 +3267,7 @@ struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
32633267
return ERR_PTR(-EBUSY);
32643268
}
32653269
dev->minor = i;
3266-
csdev = device_create(comedi_class, hardware_device,
3270+
csdev = device_create(&comedi_class, hardware_device,
32673271
MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
32683272
if (!IS_ERR(csdev))
32693273
dev->class_dev = get_device(csdev);
@@ -3312,7 +3316,7 @@ int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
33123316
}
33133317
i += COMEDI_NUM_BOARD_MINORS;
33143318
s->minor = i;
3315-
csdev = device_create(comedi_class, dev->class_dev,
3319+
csdev = device_create(&comedi_class, dev->class_dev,
33163320
MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
33173321
dev->minor, s->index);
33183322
if (!IS_ERR(csdev))
@@ -3337,7 +3341,7 @@ void comedi_free_subdevice_minor(struct comedi_subdevice *s)
33373341
comedi_subdevice_minor_table[i] = NULL;
33383342
mutex_unlock(&comedi_subdevice_minor_table_lock);
33393343
if (s->class_dev) {
3340-
device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
3344+
device_destroy(&comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
33413345
s->class_dev = NULL;
33423346
}
33433347
}
@@ -3383,15 +3387,12 @@ static int __init comedi_init(void)
33833387
if (retval)
33843388
goto out_unregister_chrdev_region;
33853389

3386-
comedi_class = class_create("comedi");
3387-
if (IS_ERR(comedi_class)) {
3388-
retval = PTR_ERR(comedi_class);
3390+
retval = class_register(&comedi_class);
3391+
if (retval) {
33893392
pr_err("failed to create class\n");
33903393
goto out_cdev_del;
33913394
}
33923395

3393-
comedi_class->dev_groups = comedi_dev_groups;
3394-
33953396
/* create devices files for legacy/manual use */
33963397
for (i = 0; i < comedi_num_legacy_minors; i++) {
33973398
struct comedi_device *dev;
@@ -3413,7 +3414,7 @@ static int __init comedi_init(void)
34133414

34143415
out_cleanup_board_minors:
34153416
comedi_cleanup_board_minors();
3416-
class_destroy(comedi_class);
3417+
class_unregister(&comedi_class);
34173418
out_cdev_del:
34183419
cdev_del(&comedi_cdev);
34193420
out_unregister_chrdev_region:
@@ -3425,7 +3426,7 @@ module_init(comedi_init);
34253426
static void __exit comedi_cleanup(void)
34263427
{
34273428
comedi_cleanup_board_minors();
3428-
class_destroy(comedi_class);
3429+
class_unregister(&comedi_class);
34293430
cdev_del(&comedi_cdev);
34303431
unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
34313432

drivers/comedi/drivers/comedi_test.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@
6060
static bool config_mode;
6161
static unsigned int set_amplitude;
6262
static unsigned int set_period;
63-
static struct class *ctcls;
63+
static const struct class ctcls = {
64+
.name = CLASS_NAME,
65+
};
6466
static struct device *ctdev;
6567

6668
module_param_named(noauto, config_mode, bool, 0444);
@@ -795,13 +797,13 @@ static int __init comedi_test_init(void)
795797
}
796798

797799
if (!config_mode) {
798-
ctcls = class_create(CLASS_NAME);
799-
if (IS_ERR(ctcls)) {
800+
ret = class_register(&ctcls);
801+
if (ret) {
800802
pr_warn("comedi_test: unable to create class\n");
801803
goto clean3;
802804
}
803805

804-
ctdev = device_create(ctcls, NULL, MKDEV(0, 0), NULL, DEV_NAME);
806+
ctdev = device_create(&ctcls, NULL, MKDEV(0, 0), NULL, DEV_NAME);
805807
if (IS_ERR(ctdev)) {
806808
pr_warn("comedi_test: unable to create device\n");
807809
goto clean2;
@@ -817,13 +819,10 @@ static int __init comedi_test_init(void)
817819
return 0;
818820

819821
clean:
820-
device_destroy(ctcls, MKDEV(0, 0));
822+
device_destroy(&ctcls, MKDEV(0, 0));
821823
clean2:
822-
class_destroy(ctcls);
823-
ctdev = NULL;
824+
class_unregister(&ctcls);
824825
clean3:
825-
ctcls = NULL;
826-
827826
return 0;
828827
}
829828
module_init(comedi_test_init);
@@ -833,9 +832,9 @@ static void __exit comedi_test_exit(void)
833832
if (ctdev)
834833
comedi_auto_unconfig(ctdev);
835834

836-
if (ctcls) {
837-
device_destroy(ctcls, MKDEV(0, 0));
838-
class_destroy(ctcls);
835+
if (class_is_registered(&ctcls)) {
836+
device_destroy(&ctcls, MKDEV(0, 0));
837+
class_unregister(&ctcls);
839838
}
840839

841840
comedi_driver_unregister(&waveform_driver);

0 commit comments

Comments
 (0)