1 /* 2 * Initialization routines 3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz> 4 * 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * 20 */ 21 22 #include <sound/driver.h> 23 #include <linux/init.h> 24 #include <linux/sched.h> 25 #include <linux/file.h> 26 #include <linux/slab.h> 27 #include <linux/time.h> 28 #include <linux/ctype.h> 29 #include <linux/pci.h> 30 #include <linux/pm.h> 31 #include <linux/platform_device.h> 32 33 #include <sound/core.h> 34 #include <sound/control.h> 35 #include <sound/info.h> 36 37 struct snd_shutdown_f_ops { 38 struct file_operations f_ops; 39 struct snd_shutdown_f_ops *next; 40 }; 41 42 unsigned int snd_cards_lock = 0; /* locked for registering/using */ 43 struct snd_card *snd_cards[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = NULL}; 44 DEFINE_RWLOCK(snd_card_rwlock); 45 46 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE) 47 int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag); 48 #endif 49 50 static void snd_card_id_read(struct snd_info_entry *entry, 51 struct snd_info_buffer *buffer) 52 { 53 snd_iprintf(buffer, "%s\n", entry->card->id); 54 } 55 56 static void snd_card_free_thread(void * __card); 57 58 /** 59 * snd_card_new - create and initialize a soundcard structure 60 * @idx: card index (address) [0 ... (SNDRV_CARDS-1)] 61 * @xid: card identification (ASCII string) 62 * @module: top level module for locking 63 * @extra_size: allocate this extra size after the main soundcard structure 64 * 65 * Creates and initializes a soundcard structure. 66 * 67 * Returns kmallocated snd_card structure. Creates the ALSA control interface 68 * (which is blocked until snd_card_register function is called). 69 */ 70 struct snd_card *snd_card_new(int idx, const char *xid, 71 struct module *module, int extra_size) 72 { 73 struct snd_card *card; 74 int err; 75 76 if (extra_size < 0) 77 extra_size = 0; 78 card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL); 79 if (card == NULL) 80 return NULL; 81 if (xid) { 82 if (!snd_info_check_reserved_words(xid)) 83 goto __error; 84 strlcpy(card->id, xid, sizeof(card->id)); 85 } 86 err = 0; 87 write_lock(&snd_card_rwlock); 88 if (idx < 0) { 89 int idx2; 90 for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++) 91 if (~snd_cards_lock & idx & 1<<idx2) { 92 idx = idx2; 93 if (idx >= snd_ecards_limit) 94 snd_ecards_limit = idx + 1; 95 break; 96 } 97 } else if (idx < snd_ecards_limit) { 98 if (snd_cards_lock & (1 << idx)) 99 err = -ENODEV; /* invalid */ 100 } else if (idx < SNDRV_CARDS) 101 snd_ecards_limit = idx + 1; /* increase the limit */ 102 else 103 err = -ENODEV; 104 if (idx < 0 || err < 0) { 105 write_unlock(&snd_card_rwlock); 106 snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i)\n", idx, snd_ecards_limit - 1); 107 goto __error; 108 } 109 snd_cards_lock |= 1 << idx; /* lock it */ 110 write_unlock(&snd_card_rwlock); 111 card->number = idx; 112 card->module = module; 113 INIT_LIST_HEAD(&card->devices); 114 init_rwsem(&card->controls_rwsem); 115 rwlock_init(&card->ctl_files_rwlock); 116 INIT_LIST_HEAD(&card->controls); 117 INIT_LIST_HEAD(&card->ctl_files); 118 spin_lock_init(&card->files_lock); 119 init_waitqueue_head(&card->shutdown_sleep); 120 INIT_WORK(&card->free_workq, snd_card_free_thread, card); 121 #ifdef CONFIG_PM 122 init_MUTEX(&card->power_lock); 123 init_waitqueue_head(&card->power_sleep); 124 #endif 125 /* the control interface cannot be accessed from the user space until */ 126 /* snd_cards_bitmask and snd_cards are set with snd_card_register */ 127 if ((err = snd_ctl_create(card)) < 0) { 128 snd_printd("unable to register control minors\n"); 129 goto __error; 130 } 131 if ((err = snd_info_card_create(card)) < 0) { 132 snd_printd("unable to create card info\n"); 133 goto __error_ctl; 134 } 135 if (extra_size > 0) 136 card->private_data = (char *)card + sizeof(struct snd_card); 137 return card; 138 139 __error_ctl: 140 snd_device_free_all(card, SNDRV_DEV_CMD_PRE); 141 __error: 142 kfree(card); 143 return NULL; 144 } 145 146 static unsigned int snd_disconnect_poll(struct file * file, poll_table * wait) 147 { 148 return POLLERR | POLLNVAL; 149 } 150 151 /** 152 * snd_card_disconnect - disconnect all APIs from the file-operations (user space) 153 * @card: soundcard structure 154 * 155 * Disconnects all APIs from the file-operations (user space). 156 * 157 * Returns zero, otherwise a negative error code. 158 * 159 * Note: The current implementation replaces all active file->f_op with special 160 * dummy file operations (they do nothing except release). 161 */ 162 int snd_card_disconnect(struct snd_card *card) 163 { 164 struct snd_monitor_file *mfile; 165 struct file *file; 166 struct snd_shutdown_f_ops *s_f_ops; 167 struct file_operations *f_ops, *old_f_ops; 168 int err; 169 170 spin_lock(&card->files_lock); 171 if (card->shutdown) { 172 spin_unlock(&card->files_lock); 173 return 0; 174 } 175 card->shutdown = 1; 176 spin_unlock(&card->files_lock); 177 178 /* phase 1: disable fops (user space) operations for ALSA API */ 179 write_lock(&snd_card_rwlock); 180 snd_cards[card->number] = NULL; 181 write_unlock(&snd_card_rwlock); 182 183 /* phase 2: replace file->f_op with special dummy operations */ 184 185 spin_lock(&card->files_lock); 186 mfile = card->files; 187 while (mfile) { 188 file = mfile->file; 189 190 /* it's critical part, use endless loop */ 191 /* we have no room to fail */ 192 s_f_ops = kmalloc(sizeof(struct snd_shutdown_f_ops), GFP_ATOMIC); 193 if (s_f_ops == NULL) 194 panic("Atomic allocation failed for snd_shutdown_f_ops!"); 195 196 f_ops = &s_f_ops->f_ops; 197 198 memset(f_ops, 0, sizeof(*f_ops)); 199 f_ops->owner = file->f_op->owner; 200 f_ops->release = file->f_op->release; 201 f_ops->poll = snd_disconnect_poll; 202 203 s_f_ops->next = card->s_f_ops; 204 card->s_f_ops = s_f_ops; 205 206 f_ops = fops_get(f_ops); 207 208 old_f_ops = file->f_op; 209 file->f_op = f_ops; /* must be atomic */ 210 fops_put(old_f_ops); 211 212 mfile = mfile->next; 213 } 214 spin_unlock(&card->files_lock); 215 216 /* phase 3: notify all connected devices about disconnection */ 217 /* at this point, they cannot respond to any calls except release() */ 218 219 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE) 220 if (snd_mixer_oss_notify_callback) 221 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT); 222 #endif 223 224 /* notify all devices that we are disconnected */ 225 err = snd_device_disconnect_all(card); 226 if (err < 0) 227 snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number); 228 229 return 0; 230 } 231 232 #ifdef CONFIG_SND_GENERIC_DRIVER 233 static void snd_generic_device_unregister(struct snd_card *card); 234 #else 235 #define snd_generic_device_unregister(x) /*NOP*/ 236 #endif 237 238 /** 239 * snd_card_free - frees given soundcard structure 240 * @card: soundcard structure 241 * 242 * This function releases the soundcard structure and the all assigned 243 * devices automatically. That is, you don't have to release the devices 244 * by yourself. 245 * 246 * Returns zero. Frees all associated devices and frees the control 247 * interface associated to given soundcard. 248 */ 249 int snd_card_free(struct snd_card *card) 250 { 251 struct snd_shutdown_f_ops *s_f_ops; 252 253 if (card == NULL) 254 return -EINVAL; 255 write_lock(&snd_card_rwlock); 256 snd_cards[card->number] = NULL; 257 write_unlock(&snd_card_rwlock); 258 259 #ifdef CONFIG_PM 260 wake_up(&card->power_sleep); 261 #endif 262 /* wait, until all devices are ready for the free operation */ 263 wait_event(card->shutdown_sleep, card->files == NULL); 264 265 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE) 266 if (snd_mixer_oss_notify_callback) 267 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE); 268 #endif 269 if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) { 270 snd_printk(KERN_ERR "unable to free all devices (pre)\n"); 271 /* Fatal, but this situation should never occur */ 272 } 273 if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) { 274 snd_printk(KERN_ERR "unable to free all devices (normal)\n"); 275 /* Fatal, but this situation should never occur */ 276 } 277 if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) { 278 snd_printk(KERN_ERR "unable to free all devices (post)\n"); 279 /* Fatal, but this situation should never occur */ 280 } 281 if (card->private_free) 282 card->private_free(card); 283 if (card->proc_id) 284 snd_info_unregister(card->proc_id); 285 if (snd_info_card_free(card) < 0) { 286 snd_printk(KERN_WARNING "unable to free card info\n"); 287 /* Not fatal error */ 288 } 289 snd_generic_device_unregister(card); 290 while (card->s_f_ops) { 291 s_f_ops = card->s_f_ops; 292 card->s_f_ops = s_f_ops->next; 293 kfree(s_f_ops); 294 } 295 write_lock(&snd_card_rwlock); 296 snd_cards_lock &= ~(1 << card->number); 297 write_unlock(&snd_card_rwlock); 298 kfree(card); 299 return 0; 300 } 301 302 static void snd_card_free_thread(void * __card) 303 { 304 struct snd_card *card = __card; 305 struct module * module = card->module; 306 307 if (!try_module_get(module)) { 308 snd_printk(KERN_ERR "unable to lock toplevel module for card %i in free thread\n", card->number); 309 module = NULL; 310 } 311 312 snd_card_free(card); 313 314 module_put(module); 315 } 316 317 /** 318 * snd_card_free_in_thread - call snd_card_free() in thread 319 * @card: soundcard structure 320 * 321 * This function schedules the call of snd_card_free() function in a 322 * work queue. When all devices are released (non-busy), the work 323 * is woken up and calls snd_card_free(). 324 * 325 * When a card can be disconnected at any time by hotplug service, 326 * this function should be used in disconnect (or detach) callback 327 * instead of calling snd_card_free() directly. 328 * 329 * Returns - zero otherwise a negative error code if the start of thread failed. 330 */ 331 int snd_card_free_in_thread(struct snd_card *card) 332 { 333 if (card->files == NULL) { 334 snd_card_free(card); 335 return 0; 336 } 337 338 if (schedule_work(&card->free_workq)) 339 return 0; 340 341 snd_printk(KERN_ERR "schedule_work() failed in snd_card_free_in_thread for card %i\n", card->number); 342 /* try to free the structure immediately */ 343 snd_card_free(card); 344 return -EFAULT; 345 } 346 347 static void choose_default_id(struct snd_card *card) 348 { 349 int i, len, idx_flag = 0, loops = 8; 350 char *id, *spos; 351 352 id = spos = card->shortname; 353 while (*id != '\0') { 354 if (*id == ' ') 355 spos = id + 1; 356 id++; 357 } 358 id = card->id; 359 while (*spos != '\0' && !isalnum(*spos)) 360 spos++; 361 if (isdigit(*spos)) 362 *id++ = isalpha(card->shortname[0]) ? card->shortname[0] : 'D'; 363 while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) { 364 if (isalnum(*spos)) 365 *id++ = *spos; 366 spos++; 367 } 368 *id = '\0'; 369 370 id = card->id; 371 372 if (*id == '\0') 373 strcpy(id, "default"); 374 375 while (1) { 376 if (loops-- == 0) { 377 snd_printk(KERN_ERR "unable to choose default card id (%s)\n", id); 378 strcpy(card->id, card->proc_root->name); 379 return; 380 } 381 if (!snd_info_check_reserved_words(id)) 382 goto __change; 383 for (i = 0; i < snd_ecards_limit; i++) { 384 if (snd_cards[i] && !strcmp(snd_cards[i]->id, id)) 385 goto __change; 386 } 387 break; 388 389 __change: 390 len = strlen(id); 391 if (idx_flag) 392 id[len-1]++; 393 else if ((size_t)len <= sizeof(card->id) - 3) { 394 strcat(id, "_1"); 395 idx_flag++; 396 } else { 397 spos = id + len - 2; 398 if ((size_t)len <= sizeof(card->id) - 2) 399 spos++; 400 *spos++ = '_'; 401 *spos++ = '1'; 402 *spos++ = '\0'; 403 idx_flag++; 404 } 405 } 406 } 407 408 /** 409 * snd_card_register - register the soundcard 410 * @card: soundcard structure 411 * 412 * This function registers all the devices assigned to the soundcard. 413 * Until calling this, the ALSA control interface is blocked from the 414 * external accesses. Thus, you should call this function at the end 415 * of the initialization of the card. 416 * 417 * Returns zero otherwise a negative error code if the registrain failed. 418 */ 419 int snd_card_register(struct snd_card *card) 420 { 421 int err; 422 struct snd_info_entry *entry; 423 424 snd_assert(card != NULL, return -EINVAL); 425 if ((err = snd_device_register_all(card)) < 0) 426 return err; 427 write_lock(&snd_card_rwlock); 428 if (snd_cards[card->number]) { 429 /* already registered */ 430 write_unlock(&snd_card_rwlock); 431 return 0; 432 } 433 if (card->id[0] == '\0') 434 choose_default_id(card); 435 snd_cards[card->number] = card; 436 write_unlock(&snd_card_rwlock); 437 if ((err = snd_info_card_register(card)) < 0) { 438 snd_printd("unable to create card info\n"); 439 goto __skip_info; 440 } 441 if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) { 442 snd_printd("unable to create card entry\n"); 443 goto __skip_info; 444 } 445 entry->c.text.read_size = PAGE_SIZE; 446 entry->c.text.read = snd_card_id_read; 447 if (snd_info_register(entry) < 0) { 448 snd_info_free_entry(entry); 449 entry = NULL; 450 } 451 card->proc_id = entry; 452 __skip_info: 453 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE) 454 if (snd_mixer_oss_notify_callback) 455 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER); 456 #endif 457 return 0; 458 } 459 460 static struct snd_info_entry *snd_card_info_entry = NULL; 461 462 static void snd_card_info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) 463 { 464 int idx, count; 465 struct snd_card *card; 466 467 for (idx = count = 0; idx < SNDRV_CARDS; idx++) { 468 read_lock(&snd_card_rwlock); 469 if ((card = snd_cards[idx]) != NULL) { 470 count++; 471 snd_iprintf(buffer, "%i [%-15s]: %s - %s\n", 472 idx, 473 card->id, 474 card->driver, 475 card->shortname); 476 snd_iprintf(buffer, " %s\n", 477 card->longname); 478 } 479 read_unlock(&snd_card_rwlock); 480 } 481 if (!count) 482 snd_iprintf(buffer, "--- no soundcards ---\n"); 483 } 484 485 #if defined(CONFIG_SND_OSSEMUL) && defined(CONFIG_PROC_FS) 486 487 void snd_card_info_read_oss(struct snd_info_buffer *buffer) 488 { 489 int idx, count; 490 struct snd_card *card; 491 492 for (idx = count = 0; idx < SNDRV_CARDS; idx++) { 493 read_lock(&snd_card_rwlock); 494 if ((card = snd_cards[idx]) != NULL) { 495 count++; 496 snd_iprintf(buffer, "%s\n", card->longname); 497 } 498 read_unlock(&snd_card_rwlock); 499 } 500 if (!count) { 501 snd_iprintf(buffer, "--- no soundcards ---\n"); 502 } 503 } 504 505 #endif 506 507 #ifdef MODULE 508 static struct snd_info_entry *snd_card_module_info_entry; 509 static void snd_card_module_info_read(struct snd_info_entry *entry, 510 struct snd_info_buffer *buffer) 511 { 512 int idx; 513 struct snd_card *card; 514 515 for (idx = 0; idx < SNDRV_CARDS; idx++) { 516 read_lock(&snd_card_rwlock); 517 if ((card = snd_cards[idx]) != NULL) 518 snd_iprintf(buffer, "%i %s\n", idx, card->module->name); 519 read_unlock(&snd_card_rwlock); 520 } 521 } 522 #endif 523 524 int __init snd_card_info_init(void) 525 { 526 struct snd_info_entry *entry; 527 528 entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL); 529 if (! entry) 530 return -ENOMEM; 531 entry->c.text.read_size = PAGE_SIZE; 532 entry->c.text.read = snd_card_info_read; 533 if (snd_info_register(entry) < 0) { 534 snd_info_free_entry(entry); 535 return -ENOMEM; 536 } 537 snd_card_info_entry = entry; 538 539 #ifdef MODULE 540 entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL); 541 if (entry) { 542 entry->c.text.read_size = PAGE_SIZE; 543 entry->c.text.read = snd_card_module_info_read; 544 if (snd_info_register(entry) < 0) 545 snd_info_free_entry(entry); 546 else 547 snd_card_module_info_entry = entry; 548 } 549 #endif 550 551 return 0; 552 } 553 554 int __exit snd_card_info_done(void) 555 { 556 if (snd_card_info_entry) 557 snd_info_unregister(snd_card_info_entry); 558 #ifdef MODULE 559 if (snd_card_module_info_entry) 560 snd_info_unregister(snd_card_module_info_entry); 561 #endif 562 return 0; 563 } 564 565 /** 566 * snd_component_add - add a component string 567 * @card: soundcard structure 568 * @component: the component id string 569 * 570 * This function adds the component id string to the supported list. 571 * The component can be referred from the alsa-lib. 572 * 573 * Returns zero otherwise a negative error code. 574 */ 575 576 int snd_component_add(struct snd_card *card, const char *component) 577 { 578 char *ptr; 579 int len = strlen(component); 580 581 ptr = strstr(card->components, component); 582 if (ptr != NULL) { 583 if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */ 584 return 1; 585 } 586 if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) { 587 snd_BUG(); 588 return -ENOMEM; 589 } 590 if (card->components[0] != '\0') 591 strcat(card->components, " "); 592 strcat(card->components, component); 593 return 0; 594 } 595 596 /** 597 * snd_card_file_add - add the file to the file list of the card 598 * @card: soundcard structure 599 * @file: file pointer 600 * 601 * This function adds the file to the file linked-list of the card. 602 * This linked-list is used to keep tracking the connection state, 603 * and to avoid the release of busy resources by hotplug. 604 * 605 * Returns zero or a negative error code. 606 */ 607 int snd_card_file_add(struct snd_card *card, struct file *file) 608 { 609 struct snd_monitor_file *mfile; 610 611 mfile = kmalloc(sizeof(*mfile), GFP_KERNEL); 612 if (mfile == NULL) 613 return -ENOMEM; 614 mfile->file = file; 615 mfile->next = NULL; 616 spin_lock(&card->files_lock); 617 if (card->shutdown) { 618 spin_unlock(&card->files_lock); 619 kfree(mfile); 620 return -ENODEV; 621 } 622 mfile->next = card->files; 623 card->files = mfile; 624 spin_unlock(&card->files_lock); 625 return 0; 626 } 627 628 /** 629 * snd_card_file_remove - remove the file from the file list 630 * @card: soundcard structure 631 * @file: file pointer 632 * 633 * This function removes the file formerly added to the card via 634 * snd_card_file_add() function. 635 * If all files are removed and the release of the card is 636 * scheduled, it will wake up the the thread to call snd_card_free() 637 * (see snd_card_free_in_thread() function). 638 * 639 * Returns zero or a negative error code. 640 */ 641 int snd_card_file_remove(struct snd_card *card, struct file *file) 642 { 643 struct snd_monitor_file *mfile, *pfile = NULL; 644 645 spin_lock(&card->files_lock); 646 mfile = card->files; 647 while (mfile) { 648 if (mfile->file == file) { 649 if (pfile) 650 pfile->next = mfile->next; 651 else 652 card->files = mfile->next; 653 break; 654 } 655 pfile = mfile; 656 mfile = mfile->next; 657 } 658 spin_unlock(&card->files_lock); 659 if (card->files == NULL) 660 wake_up(&card->shutdown_sleep); 661 if (!mfile) { 662 snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file); 663 return -ENOENT; 664 } 665 kfree(mfile); 666 return 0; 667 } 668 669 #ifdef CONFIG_SND_GENERIC_DRIVER 670 /* 671 * generic device without a proper bus using platform_device 672 * (e.g. ISA) 673 */ 674 struct snd_generic_device { 675 struct platform_device pdev; 676 struct snd_card *card; 677 }; 678 679 #define get_snd_generic_card(dev) container_of(dev, struct snd_generic_device, pdev)->card 680 681 #define SND_GENERIC_NAME "snd_generic" 682 683 #ifdef CONFIG_PM 684 static int snd_generic_suspend(struct platform_device *dev, pm_message_t state); 685 static int snd_generic_resume(struct platform_device *dev); 686 #endif 687 688 /* initialized in sound.c */ 689 struct platform_driver snd_generic_driver = { 690 #ifdef CONFIG_PM 691 .suspend = snd_generic_suspend, 692 .resume = snd_generic_resume, 693 #endif 694 .driver = { 695 .name = SND_GENERIC_NAME, 696 }, 697 }; 698 699 void snd_generic_device_release(struct device *dev) 700 { 701 } 702 703 static int snd_generic_device_register(struct snd_card *card) 704 { 705 struct snd_generic_device *dev; 706 int err; 707 708 if (card->generic_dev) 709 return 0; /* already registered */ 710 711 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 712 if (! dev) { 713 snd_printk(KERN_ERR "can't allocate generic_device\n"); 714 return -ENOMEM; 715 } 716 717 dev->pdev.name = SND_GENERIC_NAME; 718 dev->pdev.id = card->number; 719 dev->pdev.dev.release = snd_generic_device_release; 720 dev->card = card; 721 if ((err = platform_device_register(&dev->pdev)) < 0) { 722 kfree(dev); 723 return err; 724 } 725 card->generic_dev = dev; 726 return 0; 727 } 728 729 static void snd_generic_device_unregister(struct snd_card *card) 730 { 731 struct snd_generic_device *dev = card->generic_dev; 732 if (dev) { 733 platform_device_unregister(&dev->pdev); 734 kfree(dev); 735 card->generic_dev = NULL; 736 } 737 } 738 739 /** 740 * snd_card_set_generic_dev - assign the generic device to the card 741 * @card: soundcard structure 742 * 743 * Assigns a generic device to the card. This function is provided as the 744 * last resort, for devices without any proper bus. Thus this won't override 745 * the device already assigned to the card. 746 * 747 * Returns zero if successful, or a negative error code. 748 */ 749 int snd_card_set_generic_dev(struct snd_card *card) 750 { 751 int err; 752 if ((err = snd_generic_device_register(card)) < 0) 753 return err; 754 if (! card->dev) 755 snd_card_set_dev(card, &card->generic_dev->pdev.dev); 756 return 0; 757 } 758 #endif /* CONFIG_SND_GENERIC_DRIVER */ 759 760 #ifdef CONFIG_PM 761 /** 762 * snd_power_wait - wait until the power-state is changed. 763 * @card: soundcard structure 764 * @power_state: expected power state 765 * @file: file structure for the O_NONBLOCK check (optional) 766 * 767 * Waits until the power-state is changed. 768 * 769 * Note: the power lock must be active before call. 770 */ 771 int snd_power_wait(struct snd_card *card, unsigned int power_state, struct file *file) 772 { 773 wait_queue_t wait; 774 int result = 0; 775 776 /* fastpath */ 777 if (snd_power_get_state(card) == power_state) 778 return 0; 779 init_waitqueue_entry(&wait, current); 780 add_wait_queue(&card->power_sleep, &wait); 781 while (1) { 782 if (card->shutdown) { 783 result = -ENODEV; 784 break; 785 } 786 if (snd_power_get_state(card) == power_state) 787 break; 788 #if 0 /* block all devices */ 789 if (file && (file->f_flags & O_NONBLOCK)) { 790 result = -EAGAIN; 791 break; 792 } 793 #endif 794 set_current_state(TASK_UNINTERRUPTIBLE); 795 snd_power_unlock(card); 796 schedule_timeout(30 * HZ); 797 snd_power_lock(card); 798 } 799 remove_wait_queue(&card->power_sleep, &wait); 800 return result; 801 } 802 803 /** 804 * snd_card_set_pm_callback - set the PCI power-management callbacks 805 * @card: soundcard structure 806 * @suspend: suspend callback function 807 * @resume: resume callback function 808 * @private_data: private data to pass to the callback functions 809 * 810 * Sets the power-management callback functions of the card. 811 * These callbacks are called from ALSA's common PCI suspend/resume 812 * handler and from the control API. 813 */ 814 int snd_card_set_pm_callback(struct snd_card *card, 815 int (*suspend)(struct snd_card *, pm_message_t), 816 int (*resume)(struct snd_card *), 817 void *private_data) 818 { 819 card->pm_suspend = suspend; 820 card->pm_resume = resume; 821 card->pm_private_data = private_data; 822 return 0; 823 } 824 825 #ifdef CONFIG_SND_GENERIC_DRIVER 826 /* suspend/resume callbacks for snd_generic platform device */ 827 static int snd_generic_suspend(struct platform_device *dev, pm_message_t state) 828 { 829 struct snd_card *card; 830 831 card = get_snd_generic_card(dev); 832 if (card->power_state == SNDRV_CTL_POWER_D3hot) 833 return 0; 834 if (card->pm_suspend) 835 card->pm_suspend(card, PMSG_SUSPEND); 836 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); 837 return 0; 838 } 839 840 static int snd_generic_resume(struct platform_device *dev) 841 { 842 struct snd_card *card; 843 844 card = get_snd_generic_card(dev); 845 if (card->power_state == SNDRV_CTL_POWER_D0) 846 return 0; 847 if (card->pm_resume) 848 card->pm_resume(card); 849 snd_power_change_state(card, SNDRV_CTL_POWER_D0); 850 return 0; 851 } 852 853 /** 854 * snd_card_set_generic_pm_callback - set the generic power-management callbacks 855 * @card: soundcard structure 856 * @suspend: suspend callback function 857 * @resume: resume callback function 858 * @private_data: private data to pass to the callback functions 859 * 860 * Registers the power-management and sets the lowlevel callbacks for 861 * the given card. These callbacks are called from the ALSA's common 862 * PM handler and from the control API. 863 */ 864 int snd_card_set_generic_pm_callback(struct snd_card *card, 865 int (*suspend)(struct snd_card *, pm_message_t), 866 int (*resume)(struct snd_card *), 867 void *private_data) 868 { 869 int err; 870 if ((err = snd_generic_device_register(card)) < 0) 871 return err; 872 return snd_card_set_pm_callback(card, suspend, resume, private_data); 873 } 874 #endif /* CONFIG_SND_GENERIC_DRIVER */ 875 876 #ifdef CONFIG_PCI 877 int snd_card_pci_suspend(struct pci_dev *dev, pm_message_t state) 878 { 879 struct snd_card *card = pci_get_drvdata(dev); 880 int err; 881 if (! card || ! card->pm_suspend) 882 return 0; 883 if (card->power_state == SNDRV_CTL_POWER_D3hot) 884 return 0; 885 err = card->pm_suspend(card, PMSG_SUSPEND); 886 pci_save_state(dev); 887 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); 888 return err; 889 } 890 891 int snd_card_pci_resume(struct pci_dev *dev) 892 { 893 struct snd_card *card = pci_get_drvdata(dev); 894 if (! card || ! card->pm_resume) 895 return 0; 896 if (card->power_state == SNDRV_CTL_POWER_D0) 897 return 0; 898 /* restore the PCI config space */ 899 pci_restore_state(dev); 900 card->pm_resume(card); 901 snd_power_change_state(card, SNDRV_CTL_POWER_D0); 902 return 0; 903 } 904 #endif 905 906 #endif /* CONFIG_PM */ 907