ALSA: line6: Let snd_card_new() allocate private data
authorTakashi Iwai <tiwai@suse.de>
Sun, 25 Jan 2015 17:36:29 +0000 (18:36 +0100)
committerTakashi Iwai <tiwai@suse.de>
Wed, 28 Jan 2015 06:22:07 +0000 (07:22 +0100)
Instead of allocating the private data individually in each driver's
probe at first, let snd_card_new() allocate the data that is called in
line6_probe().  This simplifies the primary probe functions.

Tested-by: Chris Rorvick <chris@rorvick.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
sound/usb/line6/driver.c
sound/usb/line6/driver.h
sound/usb/line6/pod.c
sound/usb/line6/podhd.c
sound/usb/line6/toneport.c
sound/usb/line6/variax.c

index e2fbff0..c696f97 100644 (file)
@@ -418,11 +418,7 @@ EXPORT_SYMBOL_GPL(line6_read_serial_number);
 static void line6_destruct(struct snd_card *card)
 {
        struct usb_line6 *line6 = card->private_data;
-       struct usb_device *usbdev;
-
-       if (!line6)
-               return;
-       usbdev = line6->usbdev;
+       struct usb_device *usbdev = line6->usbdev;
 
        /* free buffer memory first: */
        kfree(line6->buffer_message);
@@ -431,9 +427,6 @@ static void line6_destruct(struct snd_card *card)
        /* then free URBs: */
        usb_free_urb(line6->urb_listen);
 
-       /* free interface data: */
-       kfree(line6);
-
        /* decrement reference counters: */
        usb_put_dev(usbdev);
 }
@@ -489,24 +482,27 @@ static int line6_init_cap_control(struct usb_line6 *line6)
 */
 int line6_probe(struct usb_interface *interface,
                const struct usb_device_id *id,
-               struct usb_line6 *line6,
                const struct line6_properties *properties,
-               int (*private_init)(struct usb_line6 *, const struct usb_device_id *id))
+               int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
+               size_t data_size)
 {
        struct usb_device *usbdev = interface_to_usbdev(interface);
        struct snd_card *card;
+       struct usb_line6 *line6;
        int interface_number;
        int ret;
 
+       if (WARN_ON(data_size < sizeof(*line6)))
+               return -EINVAL;
+
        ret = snd_card_new(&interface->dev,
                           SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
-                          THIS_MODULE, 0, &card);
-       if (ret < 0) {
-               kfree(line6);
+                          THIS_MODULE, data_size, &card);
+       if (ret < 0)
                return ret;
-       }
 
        /* store basic data: */
+       line6 = card->private_data;
        line6->card = card;
        line6->properties = properties;
        line6->usbdev = usbdev;
@@ -517,7 +513,6 @@ int line6_probe(struct usb_interface *interface,
        strcpy(card->shortname, line6->properties->name);
        sprintf(card->longname, "Line 6 %s at USB %s", line6->properties->name,
                dev_name(line6->ifcdev));
-       card->private_data = line6;
        card->private_free = line6_destruct;
 
        usb_set_intfdata(interface, line6);
index 4dc6c28..fce10f1 100644 (file)
@@ -181,9 +181,9 @@ extern int line6_write_data(struct usb_line6 *line6, int address, void *data,
 
 int line6_probe(struct usb_interface *interface,
                const struct usb_device_id *id,
-               struct usb_line6 *line6,
                const struct line6_properties *properties,
-               int (*private_init)(struct usb_line6 *, const struct usb_device_id *id));
+               int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
+               size_t data_size);
 
 void line6_disconnect(struct usb_interface *interface);
 
index d1e952f..6f7cd58 100644 (file)
@@ -591,14 +591,9 @@ static const struct line6_properties pod_properties_table[] = {
 static int pod_probe(struct usb_interface *interface,
                     const struct usb_device_id *id)
 {
-       struct usb_line6_pod *pod;
-
-       pod = kzalloc(sizeof(*pod), GFP_KERNEL);
-       if (!pod)
-               return -ENODEV;
-       return line6_probe(interface, id, &pod->line6,
+       return line6_probe(interface, id,
                           &pod_properties_table[id->driver_info],
-                          pod_init);
+                          pod_init, sizeof(struct usb_line6_pod));
 }
 
 static struct usb_driver pod_driver = {
index 21d7edc..43c3988 100644 (file)
@@ -177,14 +177,9 @@ static const struct line6_properties podhd_properties_table[] = {
 static int podhd_probe(struct usb_interface *interface,
                       const struct usb_device_id *id)
 {
-       struct usb_line6_podhd *podhd;
-
-       podhd = kzalloc(sizeof(*podhd), GFP_KERNEL);
-       if (!podhd)
-               return -ENODEV;
-       return line6_probe(interface, id, &podhd->line6,
+       return line6_probe(interface, id,
                           &podhd_properties_table[id->driver_info],
-                          podhd_init);
+                          podhd_init, sizeof(struct usb_line6_podhd));
 }
 
 static struct usb_driver podhd_driver = {
index 8e7020d..33d16ec 100644 (file)
@@ -558,14 +558,9 @@ static const struct line6_properties toneport_properties_table[] = {
 static int toneport_probe(struct usb_interface *interface,
                          const struct usb_device_id *id)
 {
-       struct usb_line6_toneport *toneport;
-
-       toneport = kzalloc(sizeof(*toneport), GFP_KERNEL);
-       if (!toneport)
-               return -ENODEV;
-       return line6_probe(interface, id, &toneport->line6,
+       return line6_probe(interface, id,
                           &toneport_properties_table[id->driver_info],
-                          toneport_init);
+                          toneport_init, sizeof(struct usb_line6_toneport));
 }
 
 static struct usb_driver toneport_driver = {
index ba6e85e..9701ffa 100644 (file)
@@ -296,14 +296,9 @@ static const struct line6_properties variax_properties_table[] = {
 static int variax_probe(struct usb_interface *interface,
                        const struct usb_device_id *id)
 {
-       struct usb_line6_variax *variax;
-
-       variax = kzalloc(sizeof(*variax), GFP_KERNEL);
-       if (!variax)
-               return -ENODEV;
-       return line6_probe(interface, id, &variax->line6,
+       return line6_probe(interface, id,
                           &variax_properties_table[id->driver_info],
-                          variax_init);
+                          variax_init, sizeof(struct usb_line6_variax));
 }
 
 static struct usb_driver variax_driver = {