Skip to content

Commit 2c10e7a

Browse files
ivanorlov2206gregkh
authored andcommitted
USB: gadget: f_printer: make usb_gadget_class a static const structure
Now that the driver core allows for struct class to be in read-only memory, move the usb_gadget_class structure to be declared at build time placing it into read-only memory, instead of having to be dynamically allocated at load time. Suggested-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com> Link: https://lore.kernel.org/r/20230620094412.508580-10-gregkh@linuxfoundation.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent e571e84 commit 2c10e7a

1 file changed

Lines changed: 11 additions & 14 deletions

File tree

drivers/usb/gadget/function/f_printer.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@
5454
#define DEFAULT_Q_LEN 10 /* same as legacy g_printer gadget */
5555

5656
static int major, minors;
57-
static struct class *usb_gadget_class;
57+
static const struct class usb_gadget_class = {
58+
.name = "usb_printer_gadget",
59+
};
60+
5861
static DEFINE_IDA(printer_ida);
5962
static DEFINE_MUTEX(printer_ida_lock); /* protects access do printer_ida */
6063

@@ -1120,7 +1123,7 @@ static int printer_func_bind(struct usb_configuration *c,
11201123

11211124
/* Setup the sysfs files for the printer gadget. */
11221125
devt = MKDEV(major, dev->minor);
1123-
pdev = device_create(usb_gadget_class, NULL, devt,
1126+
pdev = device_create(&usb_gadget_class, NULL, devt,
11241127
NULL, "g_printer%d", dev->minor);
11251128
if (IS_ERR(pdev)) {
11261129
ERROR(dev, "Failed to create device: g_printer\n");
@@ -1143,7 +1146,7 @@ static int printer_func_bind(struct usb_configuration *c,
11431146
return 0;
11441147

11451148
fail_cdev_add:
1146-
device_destroy(usb_gadget_class, devt);
1149+
device_destroy(&usb_gadget_class, devt);
11471150

11481151
fail_rx_reqs:
11491152
while (!list_empty(&dev->rx_reqs)) {
@@ -1410,7 +1413,7 @@ static void printer_func_unbind(struct usb_configuration *c,
14101413

14111414
dev = func_to_printer(f);
14121415

1413-
device_destroy(usb_gadget_class, MKDEV(major, dev->minor));
1416+
device_destroy(&usb_gadget_class, MKDEV(major, dev->minor));
14141417

14151418
/* Remove Character Device */
14161419
cdev_del(&dev->printer_cdev);
@@ -1512,19 +1515,14 @@ static int gprinter_setup(int count)
15121515
int status;
15131516
dev_t devt;
15141517

1515-
usb_gadget_class = class_create("usb_printer_gadget");
1516-
if (IS_ERR(usb_gadget_class)) {
1517-
status = PTR_ERR(usb_gadget_class);
1518-
usb_gadget_class = NULL;
1519-
pr_err("unable to create usb_gadget class %d\n", status);
1518+
status = class_register(&usb_gadget_class);
1519+
if (status)
15201520
return status;
1521-
}
15221521

15231522
status = alloc_chrdev_region(&devt, 0, count, "USB printer gadget");
15241523
if (status) {
15251524
pr_err("alloc_chrdev_region %d\n", status);
1526-
class_destroy(usb_gadget_class);
1527-
usb_gadget_class = NULL;
1525+
class_unregister(&usb_gadget_class);
15281526
return status;
15291527
}
15301528

@@ -1540,6 +1538,5 @@ static void gprinter_cleanup(void)
15401538
unregister_chrdev_region(MKDEV(major, 0), minors);
15411539
major = minors = 0;
15421540
}
1543-
class_destroy(usb_gadget_class);
1544-
usb_gadget_class = NULL;
1541+
class_unregister(&usb_gadget_class);
15451542
}

0 commit comments

Comments
 (0)