Skip to content

Commit ab2f06c

Browse files
committed
Merge branch 'topic/caiaq' into for-linus
* topic/caiaq: ALSA: snd_usb_caiaq: bump version number ALSA: snd_usb_caiaq: give better shortname ALSA: Core - add snd_card_set_id() function ALSA: snd_usb_caiaq: give better longname ALSA: snd_usb_caiaq: use strlcpy ALSA: snd_usb_caiaq: clean whitespaces
2 parents a963203 + c6e24d4 commit ab2f06c

6 files changed

Lines changed: 152 additions & 132 deletions

File tree

‎include/sound/core.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ struct snd_card *snd_card_new(int idx, const char *id,
313313
int snd_card_disconnect(struct snd_card *card);
314314
int snd_card_free(struct snd_card *card);
315315
int snd_card_free_when_closed(struct snd_card *card);
316+
void snd_card_set_id(struct snd_card *card, const char *id);
316317
int snd_card_register(struct snd_card *card);
317318
int snd_card_info_init(void);
318319
int snd_card_info_done(void);

‎sound/core/init.c‎

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,8 @@ int snd_card_create(int idx, const char *xid,
152152
card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
153153
if (!card)
154154
return -ENOMEM;
155-
if (xid) {
156-
if (!snd_info_check_reserved_words(xid)) {
157-
snd_printk(KERN_ERR
158-
"given id string '%s' is reserved.\n", xid);
159-
err = -EBUSY;
160-
goto __error;
161-
}
162-
strlcpy(card->id, xid, sizeof(card->id));
163-
}
155+
if (xid)
156+
snd_card_set_id(card, xid);
164157
err = 0;
165158
mutex_lock(&snd_card_mutex);
166159
if (idx < 0) {
@@ -483,22 +476,39 @@ int snd_card_free(struct snd_card *card)
483476

484477
EXPORT_SYMBOL(snd_card_free);
485478

486-
static void choose_default_id(struct snd_card *card)
479+
/**
480+
* snd_card_set_id - set card identification name
481+
* @card: soundcard structure
482+
* @nid: new identification string
483+
*
484+
* This function sets the card identification and checks for name
485+
* collisions.
486+
*/
487+
void snd_card_set_id(struct snd_card *card, const char *nid)
487488
{
488489
int i, len, idx_flag = 0, loops = SNDRV_CARDS;
489-
char *id, *spos;
490+
const char *spos, *src;
491+
char *id;
490492

491-
id = spos = card->shortname;
492-
while (*id != '\0') {
493-
if (*id == ' ')
494-
spos = id + 1;
495-
id++;
493+
/* check if user specified own card->id */
494+
if (card->id[0] != '\0')
495+
return;
496+
if (nid == NULL) {
497+
id = card->shortname;
498+
spos = src = id;
499+
while (*id != '\0') {
500+
if (*id == ' ')
501+
spos = id + 1;
502+
id++;
503+
}
504+
} else {
505+
spos = src = nid;
496506
}
497507
id = card->id;
498508
while (*spos != '\0' && !isalnum(*spos))
499509
spos++;
500510
if (isdigit(*spos))
501-
*id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D';
511+
*id++ = isalpha(src[0]) ? src[0] : 'D';
502512
while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
503513
if (isalnum(*spos))
504514
*id++ = *spos;
@@ -513,16 +523,20 @@ static void choose_default_id(struct snd_card *card)
513523

514524
while (1) {
515525
if (loops-- == 0) {
516-
snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id);
526+
snd_printk(KERN_ERR "unable to set card id (%s)\n", id);
517527
strcpy(card->id, card->proc_root->name);
518528
return;
519529
}
520530
if (!snd_info_check_reserved_words(id))
521531
goto __change;
532+
mutex_lock(&snd_card_mutex);
522533
for (i = 0; i < snd_ecards_limit; i++) {
523-
if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
534+
if (snd_cards[i] && !strcmp(snd_cards[i]->id, id)) {
535+
mutex_unlock(&snd_card_mutex);
524536
goto __change;
537+
}
525538
}
539+
mutex_unlock(&snd_card_mutex);
526540
break;
527541

528542
__change:
@@ -539,14 +553,16 @@ static void choose_default_id(struct snd_card *card)
539553
spos = id + len - 2;
540554
if ((size_t)len <= sizeof(card->id) - 2)
541555
spos++;
542-
*spos++ = '_';
543-
*spos++ = '1';
544-
*spos++ = '\0';
556+
*(char *)spos++ = '_';
557+
*(char *)spos++ = '1';
558+
*(char *)spos++ = '\0';
545559
idx_flag++;
546560
}
547561
}
548562
}
549563

564+
EXPORT_SYMBOL(snd_card_set_id);
565+
550566
#ifndef CONFIG_SYSFS_DEPRECATED
551567
static ssize_t
552568
card_id_show_attr(struct device *dev,
@@ -641,7 +657,7 @@ int snd_card_register(struct snd_card *card)
641657
return 0;
642658
}
643659
if (card->id[0] == '\0')
644-
choose_default_id(card);
660+
snd_card_set_id(card, NULL);
645661
snd_cards[card->number] = card;
646662
mutex_unlock(&snd_card_mutex);
647663
init_info_for_card(card);

0 commit comments

Comments
 (0)