ALSA: usb-audio: Simplify usb_audio_make_shortname()
PATCH 1/2 ALSA: usb-audio: Simplify usb_audio_make_shortname()
Copy the preset and quirk product names directly and remove the local char s pointer. Return after trimming the device product name and use the USB device ID as a fallback.
No functional change.
--- sound/usb/card.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-)
sound/usb/card.c +8 -14
@@ -618,30 +618,24 @@ static void usb_audio_make_shortname(struct usb_device *dev, {struct snd_card *card = chip->card;const struct usb_audio_device_name *preset; -const char *s =NULL ; + *card->shortname =0 ; preset =lookup_device_name (chip->usb_id);if (preset && preset->product_name) - s = preset->product_name; +strscpy (card->shortname, preset->product_name);else if (quirk && quirk->product_name) - s = quirk->product_name; -if (s && *s) { -strscpy (card->shortname, s,sizeof (card->shortname)); +strscpy (card->shortname, quirk->product_name); +if (*card->shortname)return ; - } -/* retrieve the device string as shortname */ if (dev->product && *dev->product) {strscpy (card->shortname, dev->product); - }else { -/* no name available from anywhere, so use ID */ -scnprintf (card->shortname,sizeof (card->shortname), -"USB Device %#04x:%#04x" , -USB_ID_VENDOR (chip->usb_id), -USB_ID_PRODUCT (chip->usb_id)); +strim (card->shortname); +return ; } -strim (card->shortname); +scnprintf (card->shortname,sizeof (card->shortname),"USB Device %#04x:%#04x" , +USB_ID_VENDOR (chip->usb_id),USB_ID_PRODUCT (chip->usb_id)); } static void usb_audio_make_longname(struct usb_device *dev,
PATCH 2/2 ALSA: usb-audio: Simplify usb_audio_make_longname()
To prepare for removing strlcat() from the kernel [1], track the output length and replace strlcat() with scnprintf() and strscpy().
Replace the speed switch statement with a lookup table.
No functional change.
--- sound/usb/card.c | 69 ++++++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 43 deletions(-)
sound/usb/card.c +26 -43
@@ -642,63 +642,46 @@ static void usb_audio_make_longname(struct usb_device *dev, struct snd_usb_audio *chip, const struct snd_usb_audio_quirk *quirk) { +static const char *const speed_suffixes[] = { + [USB_SPEED_LOW ] =", low speed" , + [USB_SPEED_FULL ] =", full speed" , + [USB_SPEED_HIGH ] =", high speed" , + [USB_SPEED_SUPER ] =", super speed" , + [USB_SPEED_SUPER_PLUS ] =", super speed plus" , + };struct snd_card *card = chip->card;const struct usb_audio_device_name *preset; -const char *s =NULL ; -int len; +enum usb_device_speed speed =snd_usb_get_speed (dev); +int len, ret; preset =lookup_device_name (chip->usb_id);/* shortcut - if any pre-defined string is given, use it */ -if (preset && preset->profile_name) - s = preset->profile_name; -if (s && *s) { -strscpy (card->longname, s,sizeof (card->longname)); +if (preset && preset->profile_name && *preset->profile_name) { +strscpy (card->longname, preset->profile_name);return ; } + *card->longname =0 ;if (preset && preset->vendor_name) - s = preset->vendor_name; +strscpy (card->longname, preset->vendor_name);else if (quirk && quirk->vendor_name) - s = quirk->vendor_name; - *card->longname =0 ; -if (s && *s) -strscpy (card->longname, s); -else if (dev->manufacturer && *dev->manufacturer) -strscpy (card->longname, dev->manufacturer); - -if (*card->longname) { -strim (card->longname); -if (*card->longname) -strlcat (card->longname," " ,sizeof (card->longname)); - } +strscpy (card->longname, quirk->vendor_name); +if (!*card->longname) +strscpy (card->longname, dev->manufacturer ?:"" ); -strlcat (card->longname, card->shortname,sizeof (card->longname)); +strim (card->longname); + len =strlen (card->longname); + len +=scnprintf (card->longname + len,sizeof (card->longname) - len, +"%s%s at " , len ?" " :"" , card->shortname); - len =strlcat (card->longname," at " ,sizeof (card->longname)); - -if (len <sizeof (card->longname)) -usb_make_path (dev, card->longname + len,sizeof (card->longname) - len); + ret =usb_make_path (dev, card->longname + len,sizeof (card->longname) - len); +if (ret <0 ) +return ; + len += ret; -switch (snd_usb_get_speed (dev)) { -case USB_SPEED_LOW : -strlcat (card->longname,", low speed" ,sizeof (card->longname)); -break ; -case USB_SPEED_FULL : -strlcat (card->longname,", full speed" ,sizeof (card->longname)); -break ; -case USB_SPEED_HIGH : -strlcat (card->longname,", high speed" ,sizeof (card->longname)); -break ; -case USB_SPEED_SUPER : -strlcat (card->longname,", super speed" ,sizeof (card->longname)); -break ; -case USB_SPEED_SUPER_PLUS : -strlcat (card->longname,", super speed plus" ,sizeof (card->longname)); -break ; -default : -break ; - } +if (speed <ARRAY_SIZE (speed_suffixes) && speed_suffixes[speed]) +strscpy (card->longname + len, speed_suffixes[speed],sizeof (card->longname) - len); }static void snd_usb_init_quirk_flags (int idx,struct snd_usb_audio *chip)
Takashi Iwai
AppliedQuoted 8 lines · Thorsten Blum
Copy the preset and quirk product names directly and remove the local char s pointer. Return after trimming the device product name and use the USB device ID as a fallback.
No functional change.
Signed-off-by Thorsten Blum <[email protected]>
Applied both patches now. Thanks.
Takashi