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