1 /*
2  *  Digital Audio (PCM) abstract layer
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.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 <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/time.h>
26 #include <linux/mutex.h>
27 #include <linux/device.h>
28 #include <sound/core.h>
29 #include <sound/minors.h>
30 #include <sound/pcm.h>
31 #include <sound/control.h>
32 #include <sound/info.h>
33 
34 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
35 MODULE_DESCRIPTION("Midlevel PCM code for ALSA.");
36 MODULE_LICENSE("GPL");
37 
38 static LIST_HEAD(snd_pcm_devices);
39 static LIST_HEAD(snd_pcm_notify_list);
40 static DEFINE_MUTEX(register_mutex);
41 
42 static int snd_pcm_free(struct snd_pcm *pcm);
43 static int snd_pcm_dev_free(struct snd_device *device);
44 static int snd_pcm_dev_register(struct snd_device *device);
45 static int snd_pcm_dev_disconnect(struct snd_device *device);
46 
snd_pcm_get(struct snd_card * card,int device)47 static struct snd_pcm *snd_pcm_get(struct snd_card *card, int device)
48 {
49 	struct snd_pcm *pcm;
50 
51 	list_for_each_entry(pcm, &snd_pcm_devices, list) {
52 		if (pcm->internal)
53 			continue;
54 		if (pcm->card == card && pcm->device == device)
55 			return pcm;
56 	}
57 	return NULL;
58 }
59 
snd_pcm_next(struct snd_card * card,int device)60 static int snd_pcm_next(struct snd_card *card, int device)
61 {
62 	struct snd_pcm *pcm;
63 
64 	list_for_each_entry(pcm, &snd_pcm_devices, list) {
65 		if (pcm->internal)
66 			continue;
67 		if (pcm->card == card && pcm->device > device)
68 			return pcm->device;
69 		else if (pcm->card->number > card->number)
70 			return -1;
71 	}
72 	return -1;
73 }
74 
snd_pcm_add(struct snd_pcm * newpcm)75 static int snd_pcm_add(struct snd_pcm *newpcm)
76 {
77 	struct snd_pcm *pcm;
78 
79 	list_for_each_entry(pcm, &snd_pcm_devices, list) {
80 		if (pcm->card == newpcm->card && pcm->device == newpcm->device)
81 			return -EBUSY;
82 		if (pcm->card->number > newpcm->card->number ||
83 				(pcm->card == newpcm->card &&
84 				pcm->device > newpcm->device)) {
85 			list_add(&newpcm->list, pcm->list.prev);
86 			return 0;
87 		}
88 	}
89 	list_add_tail(&newpcm->list, &snd_pcm_devices);
90 	return 0;
91 }
92 
snd_pcm_control_ioctl(struct snd_card * card,struct snd_ctl_file * control,unsigned int cmd,unsigned long arg)93 static int snd_pcm_control_ioctl(struct snd_card *card,
94 				 struct snd_ctl_file *control,
95 				 unsigned int cmd, unsigned long arg)
96 {
97 	switch (cmd) {
98 	case SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE:
99 		{
100 			int device;
101 
102 			if (get_user(device, (int __user *)arg))
103 				return -EFAULT;
104 			mutex_lock(&register_mutex);
105 			device = snd_pcm_next(card, device);
106 			mutex_unlock(&register_mutex);
107 			if (put_user(device, (int __user *)arg))
108 				return -EFAULT;
109 			return 0;
110 		}
111 	case SNDRV_CTL_IOCTL_PCM_INFO:
112 		{
113 			struct snd_pcm_info __user *info;
114 			unsigned int device, subdevice;
115 			int stream;
116 			struct snd_pcm *pcm;
117 			struct snd_pcm_str *pstr;
118 			struct snd_pcm_substream *substream;
119 			int err;
120 
121 			info = (struct snd_pcm_info __user *)arg;
122 			if (get_user(device, &info->device))
123 				return -EFAULT;
124 			if (get_user(stream, &info->stream))
125 				return -EFAULT;
126 			if (stream < 0 || stream > 1)
127 				return -EINVAL;
128 			if (get_user(subdevice, &info->subdevice))
129 				return -EFAULT;
130 			mutex_lock(&register_mutex);
131 			pcm = snd_pcm_get(card, device);
132 			if (pcm == NULL) {
133 				err = -ENXIO;
134 				goto _error;
135 			}
136 			pstr = &pcm->streams[stream];
137 			if (pstr->substream_count == 0) {
138 				err = -ENOENT;
139 				goto _error;
140 			}
141 			if (subdevice >= pstr->substream_count) {
142 				err = -ENXIO;
143 				goto _error;
144 			}
145 			for (substream = pstr->substream; substream;
146 			     substream = substream->next)
147 				if (substream->number == (int)subdevice)
148 					break;
149 			if (substream == NULL) {
150 				err = -ENXIO;
151 				goto _error;
152 			}
153 			err = snd_pcm_info_user(substream, info);
154 		_error:
155 			mutex_unlock(&register_mutex);
156 			return err;
157 		}
158 	case SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE:
159 		{
160 			int val;
161 
162 			if (get_user(val, (int __user *)arg))
163 				return -EFAULT;
164 			control->prefer_pcm_subdevice = val;
165 			return 0;
166 		}
167 	}
168 	return -ENOIOCTLCMD;
169 }
170 
171 #define FORMAT(v) [SNDRV_PCM_FORMAT_##v] = #v
172 
173 static char *snd_pcm_format_names[] = {
174 	FORMAT(S8),
175 	FORMAT(U8),
176 	FORMAT(S16_LE),
177 	FORMAT(S16_BE),
178 	FORMAT(U16_LE),
179 	FORMAT(U16_BE),
180 	FORMAT(S24_LE),
181 	FORMAT(S24_BE),
182 	FORMAT(U24_LE),
183 	FORMAT(U24_BE),
184 	FORMAT(S32_LE),
185 	FORMAT(S32_BE),
186 	FORMAT(U32_LE),
187 	FORMAT(U32_BE),
188 	FORMAT(FLOAT_LE),
189 	FORMAT(FLOAT_BE),
190 	FORMAT(FLOAT64_LE),
191 	FORMAT(FLOAT64_BE),
192 	FORMAT(IEC958_SUBFRAME_LE),
193 	FORMAT(IEC958_SUBFRAME_BE),
194 	FORMAT(MU_LAW),
195 	FORMAT(A_LAW),
196 	FORMAT(IMA_ADPCM),
197 	FORMAT(MPEG),
198 	FORMAT(GSM),
199 	FORMAT(SPECIAL),
200 	FORMAT(S24_3LE),
201 	FORMAT(S24_3BE),
202 	FORMAT(U24_3LE),
203 	FORMAT(U24_3BE),
204 	FORMAT(S20_3LE),
205 	FORMAT(S20_3BE),
206 	FORMAT(U20_3LE),
207 	FORMAT(U20_3BE),
208 	FORMAT(S18_3LE),
209 	FORMAT(S18_3BE),
210 	FORMAT(U18_3LE),
211 	FORMAT(U18_3BE),
212 	FORMAT(G723_24),
213 	FORMAT(G723_24_1B),
214 	FORMAT(G723_40),
215 	FORMAT(G723_40_1B),
216 };
217 
snd_pcm_format_name(snd_pcm_format_t format)218 const char *snd_pcm_format_name(snd_pcm_format_t format)
219 {
220 	if ((__force unsigned int)format >= ARRAY_SIZE(snd_pcm_format_names))
221 		return "Unknown";
222 	return snd_pcm_format_names[(__force unsigned int)format];
223 }
224 EXPORT_SYMBOL_GPL(snd_pcm_format_name);
225 
226 #ifdef CONFIG_SND_VERBOSE_PROCFS
227 
228 #define STATE(v) [SNDRV_PCM_STATE_##v] = #v
229 #define STREAM(v) [SNDRV_PCM_STREAM_##v] = #v
230 #define READY(v) [SNDRV_PCM_READY_##v] = #v
231 #define XRUN(v) [SNDRV_PCM_XRUN_##v] = #v
232 #define SILENCE(v) [SNDRV_PCM_SILENCE_##v] = #v
233 #define TSTAMP(v) [SNDRV_PCM_TSTAMP_##v] = #v
234 #define ACCESS(v) [SNDRV_PCM_ACCESS_##v] = #v
235 #define START(v) [SNDRV_PCM_START_##v] = #v
236 #define SUBFORMAT(v) [SNDRV_PCM_SUBFORMAT_##v] = #v
237 
238 static char *snd_pcm_stream_names[] = {
239 	STREAM(PLAYBACK),
240 	STREAM(CAPTURE),
241 };
242 
243 static char *snd_pcm_state_names[] = {
244 	STATE(OPEN),
245 	STATE(SETUP),
246 	STATE(PREPARED),
247 	STATE(RUNNING),
248 	STATE(XRUN),
249 	STATE(DRAINING),
250 	STATE(PAUSED),
251 	STATE(SUSPENDED),
252 };
253 
254 static char *snd_pcm_access_names[] = {
255 	ACCESS(MMAP_INTERLEAVED),
256 	ACCESS(MMAP_NONINTERLEAVED),
257 	ACCESS(MMAP_COMPLEX),
258 	ACCESS(RW_INTERLEAVED),
259 	ACCESS(RW_NONINTERLEAVED),
260 };
261 
262 static char *snd_pcm_subformat_names[] = {
263 	SUBFORMAT(STD),
264 };
265 
266 static char *snd_pcm_tstamp_mode_names[] = {
267 	TSTAMP(NONE),
268 	TSTAMP(ENABLE),
269 };
270 
snd_pcm_stream_name(int stream)271 static const char *snd_pcm_stream_name(int stream)
272 {
273 	return snd_pcm_stream_names[stream];
274 }
275 
snd_pcm_access_name(snd_pcm_access_t access)276 static const char *snd_pcm_access_name(snd_pcm_access_t access)
277 {
278 	return snd_pcm_access_names[(__force int)access];
279 }
280 
snd_pcm_subformat_name(snd_pcm_subformat_t subformat)281 static const char *snd_pcm_subformat_name(snd_pcm_subformat_t subformat)
282 {
283 	return snd_pcm_subformat_names[(__force int)subformat];
284 }
285 
snd_pcm_tstamp_mode_name(int mode)286 static const char *snd_pcm_tstamp_mode_name(int mode)
287 {
288 	return snd_pcm_tstamp_mode_names[mode];
289 }
290 
snd_pcm_state_name(snd_pcm_state_t state)291 static const char *snd_pcm_state_name(snd_pcm_state_t state)
292 {
293 	return snd_pcm_state_names[(__force int)state];
294 }
295 
296 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
297 #include <linux/soundcard.h>
298 
snd_pcm_oss_format_name(int format)299 static const char *snd_pcm_oss_format_name(int format)
300 {
301 	switch (format) {
302 	case AFMT_MU_LAW:
303 		return "MU_LAW";
304 	case AFMT_A_LAW:
305 		return "A_LAW";
306 	case AFMT_IMA_ADPCM:
307 		return "IMA_ADPCM";
308 	case AFMT_U8:
309 		return "U8";
310 	case AFMT_S16_LE:
311 		return "S16_LE";
312 	case AFMT_S16_BE:
313 		return "S16_BE";
314 	case AFMT_S8:
315 		return "S8";
316 	case AFMT_U16_LE:
317 		return "U16_LE";
318 	case AFMT_U16_BE:
319 		return "U16_BE";
320 	case AFMT_MPEG:
321 		return "MPEG";
322 	default:
323 		return "unknown";
324 	}
325 }
326 #endif
327 
snd_pcm_proc_info_read(struct snd_pcm_substream * substream,struct snd_info_buffer * buffer)328 static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream,
329 				   struct snd_info_buffer *buffer)
330 {
331 	struct snd_pcm_info *info;
332 	int err;
333 
334 	if (! substream)
335 		return;
336 
337 	info = kmalloc(sizeof(*info), GFP_KERNEL);
338 	if (! info) {
339 		printk(KERN_DEBUG "snd_pcm_proc_info_read: cannot malloc\n");
340 		return;
341 	}
342 
343 	err = snd_pcm_info(substream, info);
344 	if (err < 0) {
345 		snd_iprintf(buffer, "error %d\n", err);
346 		kfree(info);
347 		return;
348 	}
349 	snd_iprintf(buffer, "card: %d\n", info->card);
350 	snd_iprintf(buffer, "device: %d\n", info->device);
351 	snd_iprintf(buffer, "subdevice: %d\n", info->subdevice);
352 	snd_iprintf(buffer, "stream: %s\n", snd_pcm_stream_name(info->stream));
353 	snd_iprintf(buffer, "id: %s\n", info->id);
354 	snd_iprintf(buffer, "name: %s\n", info->name);
355 	snd_iprintf(buffer, "subname: %s\n", info->subname);
356 	snd_iprintf(buffer, "class: %d\n", info->dev_class);
357 	snd_iprintf(buffer, "subclass: %d\n", info->dev_subclass);
358 	snd_iprintf(buffer, "subdevices_count: %d\n", info->subdevices_count);
359 	snd_iprintf(buffer, "subdevices_avail: %d\n", info->subdevices_avail);
360 	kfree(info);
361 }
362 
snd_pcm_stream_proc_info_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)363 static void snd_pcm_stream_proc_info_read(struct snd_info_entry *entry,
364 					  struct snd_info_buffer *buffer)
365 {
366 	snd_pcm_proc_info_read(((struct snd_pcm_str *)entry->private_data)->substream,
367 			       buffer);
368 }
369 
snd_pcm_substream_proc_info_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)370 static void snd_pcm_substream_proc_info_read(struct snd_info_entry *entry,
371 					     struct snd_info_buffer *buffer)
372 {
373 	snd_pcm_proc_info_read(entry->private_data, buffer);
374 }
375 
snd_pcm_substream_proc_hw_params_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)376 static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,
377 						  struct snd_info_buffer *buffer)
378 {
379 	struct snd_pcm_substream *substream = entry->private_data;
380 	struct snd_pcm_runtime *runtime;
381 
382 	mutex_lock(&substream->pcm->open_mutex);
383 	runtime = substream->runtime;
384 	if (!runtime) {
385 		snd_iprintf(buffer, "closed\n");
386 		goto unlock;
387 	}
388 	if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
389 		snd_iprintf(buffer, "no setup\n");
390 		goto unlock;
391 	}
392 	snd_iprintf(buffer, "access: %s\n", snd_pcm_access_name(runtime->access));
393 	snd_iprintf(buffer, "format: %s\n", snd_pcm_format_name(runtime->format));
394 	snd_iprintf(buffer, "subformat: %s\n", snd_pcm_subformat_name(runtime->subformat));
395 	snd_iprintf(buffer, "channels: %u\n", runtime->channels);
396 	snd_iprintf(buffer, "rate: %u (%u/%u)\n", runtime->rate, runtime->rate_num, runtime->rate_den);
397 	snd_iprintf(buffer, "period_size: %lu\n", runtime->period_size);
398 	snd_iprintf(buffer, "buffer_size: %lu\n", runtime->buffer_size);
399 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
400 	if (substream->oss.oss) {
401 		snd_iprintf(buffer, "OSS format: %s\n", snd_pcm_oss_format_name(runtime->oss.format));
402 		snd_iprintf(buffer, "OSS channels: %u\n", runtime->oss.channels);
403 		snd_iprintf(buffer, "OSS rate: %u\n", runtime->oss.rate);
404 		snd_iprintf(buffer, "OSS period bytes: %lu\n", (unsigned long)runtime->oss.period_bytes);
405 		snd_iprintf(buffer, "OSS periods: %u\n", runtime->oss.periods);
406 		snd_iprintf(buffer, "OSS period frames: %lu\n", (unsigned long)runtime->oss.period_frames);
407 	}
408 #endif
409  unlock:
410 	mutex_unlock(&substream->pcm->open_mutex);
411 }
412 
snd_pcm_substream_proc_sw_params_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)413 static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,
414 						  struct snd_info_buffer *buffer)
415 {
416 	struct snd_pcm_substream *substream = entry->private_data;
417 	struct snd_pcm_runtime *runtime;
418 
419 	mutex_lock(&substream->pcm->open_mutex);
420 	runtime = substream->runtime;
421 	if (!runtime) {
422 		snd_iprintf(buffer, "closed\n");
423 		goto unlock;
424 	}
425 	if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
426 		snd_iprintf(buffer, "no setup\n");
427 		goto unlock;
428 	}
429 	snd_iprintf(buffer, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(runtime->tstamp_mode));
430 	snd_iprintf(buffer, "period_step: %u\n", runtime->period_step);
431 	snd_iprintf(buffer, "avail_min: %lu\n", runtime->control->avail_min);
432 	snd_iprintf(buffer, "start_threshold: %lu\n", runtime->start_threshold);
433 	snd_iprintf(buffer, "stop_threshold: %lu\n", runtime->stop_threshold);
434 	snd_iprintf(buffer, "silence_threshold: %lu\n", runtime->silence_threshold);
435 	snd_iprintf(buffer, "silence_size: %lu\n", runtime->silence_size);
436 	snd_iprintf(buffer, "boundary: %lu\n", runtime->boundary);
437  unlock:
438 	mutex_unlock(&substream->pcm->open_mutex);
439 }
440 
snd_pcm_substream_proc_status_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)441 static void snd_pcm_substream_proc_status_read(struct snd_info_entry *entry,
442 					       struct snd_info_buffer *buffer)
443 {
444 	struct snd_pcm_substream *substream = entry->private_data;
445 	struct snd_pcm_runtime *runtime;
446 	struct snd_pcm_status status;
447 	int err;
448 
449 	mutex_lock(&substream->pcm->open_mutex);
450 	runtime = substream->runtime;
451 	if (!runtime) {
452 		snd_iprintf(buffer, "closed\n");
453 		goto unlock;
454 	}
455 	memset(&status, 0, sizeof(status));
456 	err = snd_pcm_status(substream, &status);
457 	if (err < 0) {
458 		snd_iprintf(buffer, "error %d\n", err);
459 		goto unlock;
460 	}
461 	snd_iprintf(buffer, "state: %s\n", snd_pcm_state_name(status.state));
462 	snd_iprintf(buffer, "owner_pid   : %d\n", pid_vnr(substream->pid));
463 	snd_iprintf(buffer, "trigger_time: %ld.%09ld\n",
464 		status.trigger_tstamp.tv_sec, status.trigger_tstamp.tv_nsec);
465 	snd_iprintf(buffer, "tstamp      : %ld.%09ld\n",
466 		status.tstamp.tv_sec, status.tstamp.tv_nsec);
467 	snd_iprintf(buffer, "delay       : %ld\n", status.delay);
468 	snd_iprintf(buffer, "avail       : %ld\n", status.avail);
469 	snd_iprintf(buffer, "avail_max   : %ld\n", status.avail_max);
470 	snd_iprintf(buffer, "-----\n");
471 	snd_iprintf(buffer, "hw_ptr      : %ld\n", runtime->status->hw_ptr);
472 	snd_iprintf(buffer, "appl_ptr    : %ld\n", runtime->control->appl_ptr);
473  unlock:
474 	mutex_unlock(&substream->pcm->open_mutex);
475 }
476 
477 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
snd_pcm_xrun_debug_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)478 static void snd_pcm_xrun_debug_read(struct snd_info_entry *entry,
479 				    struct snd_info_buffer *buffer)
480 {
481 	struct snd_pcm_str *pstr = entry->private_data;
482 	snd_iprintf(buffer, "%d\n", pstr->xrun_debug);
483 }
484 
snd_pcm_xrun_debug_write(struct snd_info_entry * entry,struct snd_info_buffer * buffer)485 static void snd_pcm_xrun_debug_write(struct snd_info_entry *entry,
486 				     struct snd_info_buffer *buffer)
487 {
488 	struct snd_pcm_str *pstr = entry->private_data;
489 	char line[64];
490 	if (!snd_info_get_line(buffer, line, sizeof(line)))
491 		pstr->xrun_debug = simple_strtoul(line, NULL, 10);
492 }
493 #endif
494 
snd_pcm_stream_proc_init(struct snd_pcm_str * pstr)495 static int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr)
496 {
497 	struct snd_pcm *pcm = pstr->pcm;
498 	struct snd_info_entry *entry;
499 	char name[16];
500 
501 	sprintf(name, "pcm%i%c", pcm->device,
502 		pstr->stream == SNDRV_PCM_STREAM_PLAYBACK ? 'p' : 'c');
503 	if ((entry = snd_info_create_card_entry(pcm->card, name, pcm->card->proc_root)) == NULL)
504 		return -ENOMEM;
505 	entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
506 	if (snd_info_register(entry) < 0) {
507 		snd_info_free_entry(entry);
508 		return -ENOMEM;
509 	}
510 	pstr->proc_root = entry;
511 
512 	if ((entry = snd_info_create_card_entry(pcm->card, "info", pstr->proc_root)) != NULL) {
513 		snd_info_set_text_ops(entry, pstr, snd_pcm_stream_proc_info_read);
514 		if (snd_info_register(entry) < 0) {
515 			snd_info_free_entry(entry);
516 			entry = NULL;
517 		}
518 	}
519 	pstr->proc_info_entry = entry;
520 
521 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
522 	if ((entry = snd_info_create_card_entry(pcm->card, "xrun_debug",
523 						pstr->proc_root)) != NULL) {
524 		entry->c.text.read = snd_pcm_xrun_debug_read;
525 		entry->c.text.write = snd_pcm_xrun_debug_write;
526 		entry->mode |= S_IWUSR;
527 		entry->private_data = pstr;
528 		if (snd_info_register(entry) < 0) {
529 			snd_info_free_entry(entry);
530 			entry = NULL;
531 		}
532 	}
533 	pstr->proc_xrun_debug_entry = entry;
534 #endif
535 	return 0;
536 }
537 
snd_pcm_stream_proc_done(struct snd_pcm_str * pstr)538 static int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr)
539 {
540 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
541 	snd_info_free_entry(pstr->proc_xrun_debug_entry);
542 	pstr->proc_xrun_debug_entry = NULL;
543 #endif
544 	snd_info_free_entry(pstr->proc_info_entry);
545 	pstr->proc_info_entry = NULL;
546 	snd_info_free_entry(pstr->proc_root);
547 	pstr->proc_root = NULL;
548 	return 0;
549 }
550 
snd_pcm_substream_proc_init(struct snd_pcm_substream * substream)551 static int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream)
552 {
553 	struct snd_info_entry *entry;
554 	struct snd_card *card;
555 	char name[16];
556 
557 	card = substream->pcm->card;
558 
559 	sprintf(name, "sub%i", substream->number);
560 	if ((entry = snd_info_create_card_entry(card, name, substream->pstr->proc_root)) == NULL)
561 		return -ENOMEM;
562 	entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
563 	if (snd_info_register(entry) < 0) {
564 		snd_info_free_entry(entry);
565 		return -ENOMEM;
566 	}
567 	substream->proc_root = entry;
568 
569 	if ((entry = snd_info_create_card_entry(card, "info", substream->proc_root)) != NULL) {
570 		snd_info_set_text_ops(entry, substream,
571 				      snd_pcm_substream_proc_info_read);
572 		if (snd_info_register(entry) < 0) {
573 			snd_info_free_entry(entry);
574 			entry = NULL;
575 		}
576 	}
577 	substream->proc_info_entry = entry;
578 
579 	if ((entry = snd_info_create_card_entry(card, "hw_params", substream->proc_root)) != NULL) {
580 		snd_info_set_text_ops(entry, substream,
581 				      snd_pcm_substream_proc_hw_params_read);
582 		if (snd_info_register(entry) < 0) {
583 			snd_info_free_entry(entry);
584 			entry = NULL;
585 		}
586 	}
587 	substream->proc_hw_params_entry = entry;
588 
589 	if ((entry = snd_info_create_card_entry(card, "sw_params", substream->proc_root)) != NULL) {
590 		snd_info_set_text_ops(entry, substream,
591 				      snd_pcm_substream_proc_sw_params_read);
592 		if (snd_info_register(entry) < 0) {
593 			snd_info_free_entry(entry);
594 			entry = NULL;
595 		}
596 	}
597 	substream->proc_sw_params_entry = entry;
598 
599 	if ((entry = snd_info_create_card_entry(card, "status", substream->proc_root)) != NULL) {
600 		snd_info_set_text_ops(entry, substream,
601 				      snd_pcm_substream_proc_status_read);
602 		if (snd_info_register(entry) < 0) {
603 			snd_info_free_entry(entry);
604 			entry = NULL;
605 		}
606 	}
607 	substream->proc_status_entry = entry;
608 
609 	return 0;
610 }
611 
snd_pcm_substream_proc_done(struct snd_pcm_substream * substream)612 static int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream)
613 {
614 	snd_info_free_entry(substream->proc_info_entry);
615 	substream->proc_info_entry = NULL;
616 	snd_info_free_entry(substream->proc_hw_params_entry);
617 	substream->proc_hw_params_entry = NULL;
618 	snd_info_free_entry(substream->proc_sw_params_entry);
619 	substream->proc_sw_params_entry = NULL;
620 	snd_info_free_entry(substream->proc_status_entry);
621 	substream->proc_status_entry = NULL;
622 	snd_info_free_entry(substream->proc_root);
623 	substream->proc_root = NULL;
624 	return 0;
625 }
626 #else /* !CONFIG_SND_VERBOSE_PROCFS */
snd_pcm_stream_proc_init(struct snd_pcm_str * pstr)627 static inline int snd_pcm_stream_proc_init(struct snd_pcm_str *pstr) { return 0; }
snd_pcm_stream_proc_done(struct snd_pcm_str * pstr)628 static inline int snd_pcm_stream_proc_done(struct snd_pcm_str *pstr) { return 0; }
snd_pcm_substream_proc_init(struct snd_pcm_substream * substream)629 static inline int snd_pcm_substream_proc_init(struct snd_pcm_substream *substream) { return 0; }
snd_pcm_substream_proc_done(struct snd_pcm_substream * substream)630 static inline int snd_pcm_substream_proc_done(struct snd_pcm_substream *substream) { return 0; }
631 #endif /* CONFIG_SND_VERBOSE_PROCFS */
632 
633 /**
634  * snd_pcm_new_stream - create a new PCM stream
635  * @pcm: the pcm instance
636  * @stream: the stream direction, SNDRV_PCM_STREAM_XXX
637  * @substream_count: the number of substreams
638  *
639  * Creates a new stream for the pcm.
640  * The corresponding stream on the pcm must have been empty before
641  * calling this, i.e. zero must be given to the argument of
642  * snd_pcm_new().
643  *
644  * Returns zero if successful, or a negative error code on failure.
645  */
snd_pcm_new_stream(struct snd_pcm * pcm,int stream,int substream_count)646 int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)
647 {
648 	int idx, err;
649 	struct snd_pcm_str *pstr = &pcm->streams[stream];
650 	struct snd_pcm_substream *substream, *prev;
651 
652 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
653 	mutex_init(&pstr->oss.setup_mutex);
654 #endif
655 	pstr->stream = stream;
656 	pstr->pcm = pcm;
657 	pstr->substream_count = substream_count;
658 	if (substream_count > 0 && !pcm->internal) {
659 		err = snd_pcm_stream_proc_init(pstr);
660 		if (err < 0) {
661 			snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
662 			return err;
663 		}
664 	}
665 	prev = NULL;
666 	for (idx = 0, prev = NULL; idx < substream_count; idx++) {
667 		substream = kzalloc(sizeof(*substream), GFP_KERNEL);
668 		if (substream == NULL) {
669 			snd_printk(KERN_ERR "Cannot allocate PCM substream\n");
670 			return -ENOMEM;
671 		}
672 		substream->pcm = pcm;
673 		substream->pstr = pstr;
674 		substream->number = idx;
675 		substream->stream = stream;
676 		sprintf(substream->name, "subdevice #%i", idx);
677 		substream->buffer_bytes_max = UINT_MAX;
678 		if (prev == NULL)
679 			pstr->substream = substream;
680 		else
681 			prev->next = substream;
682 
683 		if (!pcm->internal) {
684 			err = snd_pcm_substream_proc_init(substream);
685 			if (err < 0) {
686 				snd_printk(KERN_ERR "Error in snd_pcm_stream_proc_init\n");
687 				if (prev == NULL)
688 					pstr->substream = NULL;
689 				else
690 					prev->next = NULL;
691 				kfree(substream);
692 				return err;
693 			}
694 		}
695 		substream->group = &substream->self_group;
696 		spin_lock_init(&substream->self_group.lock);
697 		INIT_LIST_HEAD(&substream->self_group.substreams);
698 		list_add_tail(&substream->link_list, &substream->self_group.substreams);
699 		atomic_set(&substream->mmap_count, 0);
700 		prev = substream;
701 	}
702 	return 0;
703 }
704 
705 EXPORT_SYMBOL(snd_pcm_new_stream);
706 
_snd_pcm_new(struct snd_card * card,const char * id,int device,int playback_count,int capture_count,bool internal,struct snd_pcm ** rpcm)707 static int _snd_pcm_new(struct snd_card *card, const char *id, int device,
708 		int playback_count, int capture_count, bool internal,
709 		struct snd_pcm **rpcm)
710 {
711 	struct snd_pcm *pcm;
712 	int err;
713 	static struct snd_device_ops ops = {
714 		.dev_free = snd_pcm_dev_free,
715 		.dev_register =	snd_pcm_dev_register,
716 		.dev_disconnect = snd_pcm_dev_disconnect,
717 	};
718 
719 	if (snd_BUG_ON(!card))
720 		return -ENXIO;
721 	if (rpcm)
722 		*rpcm = NULL;
723 	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
724 	if (pcm == NULL) {
725 		snd_printk(KERN_ERR "Cannot allocate PCM\n");
726 		return -ENOMEM;
727 	}
728 	pcm->card = card;
729 	pcm->device = device;
730 	pcm->internal = internal;
731 	if (id)
732 		strlcpy(pcm->id, id, sizeof(pcm->id));
733 	if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
734 		snd_pcm_free(pcm);
735 		return err;
736 	}
737 	if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
738 		snd_pcm_free(pcm);
739 		return err;
740 	}
741 	mutex_init(&pcm->open_mutex);
742 	init_waitqueue_head(&pcm->open_wait);
743 	if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
744 		snd_pcm_free(pcm);
745 		return err;
746 	}
747 	if (rpcm)
748 		*rpcm = pcm;
749 	return 0;
750 }
751 
752 /**
753  * snd_pcm_new - create a new PCM instance
754  * @card: the card instance
755  * @id: the id string
756  * @device: the device index (zero based)
757  * @playback_count: the number of substreams for playback
758  * @capture_count: the number of substreams for capture
759  * @rpcm: the pointer to store the new pcm instance
760  *
761  * Creates a new PCM instance.
762  *
763  * The pcm operators have to be set afterwards to the new instance
764  * via snd_pcm_set_ops().
765  *
766  * Returns zero if successful, or a negative error code on failure.
767  */
snd_pcm_new(struct snd_card * card,const char * id,int device,int playback_count,int capture_count,struct snd_pcm ** rpcm)768 int snd_pcm_new(struct snd_card *card, const char *id, int device,
769 		int playback_count, int capture_count, struct snd_pcm **rpcm)
770 {
771 	return _snd_pcm_new(card, id, device, playback_count, capture_count,
772 			false, rpcm);
773 }
774 EXPORT_SYMBOL(snd_pcm_new);
775 
776 /**
777  * snd_pcm_new_internal - create a new internal PCM instance
778  * @card: the card instance
779  * @id: the id string
780  * @device: the device index (zero based - shared with normal PCMs)
781  * @playback_count: the number of substreams for playback
782  * @capture_count: the number of substreams for capture
783  * @rpcm: the pointer to store the new pcm instance
784  *
785  * Creates a new internal PCM instance with no userspace device or procfs
786  * entries. This is used by ASoC Back End PCMs in order to create a PCM that
787  * will only be used internally by kernel drivers. i.e. it cannot be opened
788  * by userspace. It provides existing ASoC components drivers with a substream
789  * and access to any private data.
790  *
791  * The pcm operators have to be set afterwards to the new instance
792  * via snd_pcm_set_ops().
793  *
794  * Returns zero if successful, or a negative error code on failure.
795  */
snd_pcm_new_internal(struct snd_card * card,const char * id,int device,int playback_count,int capture_count,struct snd_pcm ** rpcm)796 int snd_pcm_new_internal(struct snd_card *card, const char *id, int device,
797 	int playback_count, int capture_count,
798 	struct snd_pcm **rpcm)
799 {
800 	return _snd_pcm_new(card, id, device, playback_count, capture_count,
801 			true, rpcm);
802 }
803 EXPORT_SYMBOL(snd_pcm_new_internal);
804 
snd_pcm_free_stream(struct snd_pcm_str * pstr)805 static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
806 {
807 	struct snd_pcm_substream *substream, *substream_next;
808 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
809 	struct snd_pcm_oss_setup *setup, *setupn;
810 #endif
811 	substream = pstr->substream;
812 	while (substream) {
813 		substream_next = substream->next;
814 		snd_pcm_timer_done(substream);
815 		snd_pcm_substream_proc_done(substream);
816 		kfree(substream);
817 		substream = substream_next;
818 	}
819 	snd_pcm_stream_proc_done(pstr);
820 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
821 	for (setup = pstr->oss.setup_list; setup; setup = setupn) {
822 		setupn = setup->next;
823 		kfree(setup->task_name);
824 		kfree(setup);
825 	}
826 #endif
827 }
828 
snd_pcm_free(struct snd_pcm * pcm)829 static int snd_pcm_free(struct snd_pcm *pcm)
830 {
831 	struct snd_pcm_notify *notify;
832 
833 	if (!pcm)
834 		return 0;
835 	list_for_each_entry(notify, &snd_pcm_notify_list, list) {
836 		notify->n_unregister(pcm);
837 	}
838 	if (pcm->private_free)
839 		pcm->private_free(pcm);
840 	snd_pcm_lib_preallocate_free_for_all(pcm);
841 	snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
842 	snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
843 	kfree(pcm);
844 	return 0;
845 }
846 
snd_pcm_dev_free(struct snd_device * device)847 static int snd_pcm_dev_free(struct snd_device *device)
848 {
849 	struct snd_pcm *pcm = device->device_data;
850 	return snd_pcm_free(pcm);
851 }
852 
snd_pcm_attach_substream(struct snd_pcm * pcm,int stream,struct file * file,struct snd_pcm_substream ** rsubstream)853 int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
854 			     struct file *file,
855 			     struct snd_pcm_substream **rsubstream)
856 {
857 	struct snd_pcm_str * pstr;
858 	struct snd_pcm_substream *substream;
859 	struct snd_pcm_runtime *runtime;
860 	struct snd_ctl_file *kctl;
861 	struct snd_card *card;
862 	int prefer_subdevice = -1;
863 	size_t size;
864 
865 	if (snd_BUG_ON(!pcm || !rsubstream))
866 		return -ENXIO;
867 	*rsubstream = NULL;
868 	pstr = &pcm->streams[stream];
869 	if (pstr->substream == NULL || pstr->substream_count == 0)
870 		return -ENODEV;
871 
872 	card = pcm->card;
873 	read_lock(&card->ctl_files_rwlock);
874 	list_for_each_entry(kctl, &card->ctl_files, list) {
875 		if (kctl->pid == task_pid(current)) {
876 			prefer_subdevice = kctl->prefer_pcm_subdevice;
877 			if (prefer_subdevice != -1)
878 				break;
879 		}
880 	}
881 	read_unlock(&card->ctl_files_rwlock);
882 
883 	switch (stream) {
884 	case SNDRV_PCM_STREAM_PLAYBACK:
885 		if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
886 			for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next) {
887 				if (SUBSTREAM_BUSY(substream))
888 					return -EAGAIN;
889 			}
890 		}
891 		break;
892 	case SNDRV_PCM_STREAM_CAPTURE:
893 		if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
894 			for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) {
895 				if (SUBSTREAM_BUSY(substream))
896 					return -EAGAIN;
897 			}
898 		}
899 		break;
900 	default:
901 		return -EINVAL;
902 	}
903 
904 	if (file->f_flags & O_APPEND) {
905 		if (prefer_subdevice < 0) {
906 			if (pstr->substream_count > 1)
907 				return -EINVAL; /* must be unique */
908 			substream = pstr->substream;
909 		} else {
910 			for (substream = pstr->substream; substream;
911 			     substream = substream->next)
912 				if (substream->number == prefer_subdevice)
913 					break;
914 		}
915 		if (! substream)
916 			return -ENODEV;
917 		if (! SUBSTREAM_BUSY(substream))
918 			return -EBADFD;
919 		substream->ref_count++;
920 		*rsubstream = substream;
921 		return 0;
922 	}
923 
924 	if (prefer_subdevice >= 0) {
925 		for (substream = pstr->substream; substream; substream = substream->next)
926 			if (!SUBSTREAM_BUSY(substream) && substream->number == prefer_subdevice)
927 				goto __ok;
928 	}
929 	for (substream = pstr->substream; substream; substream = substream->next)
930 		if (!SUBSTREAM_BUSY(substream))
931 			break;
932       __ok:
933 	if (substream == NULL)
934 		return -EAGAIN;
935 
936 	runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
937 	if (runtime == NULL)
938 		return -ENOMEM;
939 
940 	size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status));
941 	runtime->status = snd_malloc_pages(size, GFP_KERNEL);
942 	if (runtime->status == NULL) {
943 		kfree(runtime);
944 		return -ENOMEM;
945 	}
946 	memset((void*)runtime->status, 0, size);
947 
948 	size = PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control));
949 	runtime->control = snd_malloc_pages(size, GFP_KERNEL);
950 	if (runtime->control == NULL) {
951 		snd_free_pages((void*)runtime->status,
952 			       PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
953 		kfree(runtime);
954 		return -ENOMEM;
955 	}
956 	memset((void*)runtime->control, 0, size);
957 
958 	init_waitqueue_head(&runtime->sleep);
959 	init_waitqueue_head(&runtime->tsleep);
960 
961 	runtime->status->state = SNDRV_PCM_STATE_OPEN;
962 
963 	substream->runtime = runtime;
964 	substream->private_data = pcm->private_data;
965 	substream->ref_count = 1;
966 	substream->f_flags = file->f_flags;
967 	substream->pid = get_pid(task_pid(current));
968 	pstr->substream_opened++;
969 	*rsubstream = substream;
970 	return 0;
971 }
972 
snd_pcm_detach_substream(struct snd_pcm_substream * substream)973 void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
974 {
975 	struct snd_pcm_runtime *runtime;
976 
977 	if (PCM_RUNTIME_CHECK(substream))
978 		return;
979 	runtime = substream->runtime;
980 	if (runtime->private_free != NULL)
981 		runtime->private_free(runtime);
982 	snd_free_pages((void*)runtime->status,
983 		       PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)));
984 	snd_free_pages((void*)runtime->control,
985 		       PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)));
986 	kfree(runtime->hw_constraints.rules);
987 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
988 	if (runtime->hwptr_log)
989 		kfree(runtime->hwptr_log);
990 #endif
991 	kfree(runtime);
992 	substream->runtime = NULL;
993 	put_pid(substream->pid);
994 	substream->pid = NULL;
995 	substream->pstr->substream_opened--;
996 }
997 
show_pcm_class(struct device * dev,struct device_attribute * attr,char * buf)998 static ssize_t show_pcm_class(struct device *dev,
999 			      struct device_attribute *attr, char *buf)
1000 {
1001 	struct snd_pcm *pcm;
1002 	const char *str;
1003 	static const char *strs[SNDRV_PCM_CLASS_LAST + 1] = {
1004 		[SNDRV_PCM_CLASS_GENERIC] = "generic",
1005 		[SNDRV_PCM_CLASS_MULTI] = "multi",
1006 		[SNDRV_PCM_CLASS_MODEM] = "modem",
1007 		[SNDRV_PCM_CLASS_DIGITIZER] = "digitizer",
1008 	};
1009 
1010 	if (! (pcm = dev_get_drvdata(dev)) ||
1011 	    pcm->dev_class > SNDRV_PCM_CLASS_LAST)
1012 		str = "none";
1013 	else
1014 		str = strs[pcm->dev_class];
1015         return snprintf(buf, PAGE_SIZE, "%s\n", str);
1016 }
1017 
1018 static struct device_attribute pcm_attrs =
1019 	__ATTR(pcm_class, S_IRUGO, show_pcm_class, NULL);
1020 
snd_pcm_dev_register(struct snd_device * device)1021 static int snd_pcm_dev_register(struct snd_device *device)
1022 {
1023 	int cidx, err;
1024 	struct snd_pcm_substream *substream;
1025 	struct snd_pcm_notify *notify;
1026 	char str[16];
1027 	struct snd_pcm *pcm;
1028 	struct device *dev;
1029 
1030 	if (snd_BUG_ON(!device || !device->device_data))
1031 		return -ENXIO;
1032 	pcm = device->device_data;
1033 	mutex_lock(&register_mutex);
1034 	err = snd_pcm_add(pcm);
1035 	if (err) {
1036 		mutex_unlock(&register_mutex);
1037 		return err;
1038 	}
1039 	for (cidx = 0; cidx < 2; cidx++) {
1040 		int devtype = -1;
1041 		if (pcm->streams[cidx].substream == NULL || pcm->internal)
1042 			continue;
1043 		switch (cidx) {
1044 		case SNDRV_PCM_STREAM_PLAYBACK:
1045 			sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device);
1046 			devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
1047 			break;
1048 		case SNDRV_PCM_STREAM_CAPTURE:
1049 			sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device);
1050 			devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
1051 			break;
1052 		}
1053 		/* device pointer to use, pcm->dev takes precedence if
1054 		 * it is assigned, otherwise fall back to card's device
1055 		 * if possible */
1056 		dev = pcm->dev;
1057 		if (!dev)
1058 			dev = snd_card_get_device_link(pcm->card);
1059 		/* register pcm */
1060 		err = snd_register_device_for_dev(devtype, pcm->card,
1061 						  pcm->device,
1062 						  &snd_pcm_f_ops[cidx],
1063 						  pcm, str, dev);
1064 		if (err < 0) {
1065 			list_del(&pcm->list);
1066 			mutex_unlock(&register_mutex);
1067 			return err;
1068 		}
1069 		snd_add_device_sysfs_file(devtype, pcm->card, pcm->device,
1070 					  &pcm_attrs);
1071 		for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
1072 			snd_pcm_timer_init(substream);
1073 	}
1074 
1075 	list_for_each_entry(notify, &snd_pcm_notify_list, list)
1076 		notify->n_register(pcm);
1077 
1078 	mutex_unlock(&register_mutex);
1079 	return 0;
1080 }
1081 
snd_pcm_dev_disconnect(struct snd_device * device)1082 static int snd_pcm_dev_disconnect(struct snd_device *device)
1083 {
1084 	struct snd_pcm *pcm = device->device_data;
1085 	struct snd_pcm_notify *notify;
1086 	struct snd_pcm_substream *substream;
1087 	int cidx, devtype;
1088 
1089 	mutex_lock(&register_mutex);
1090 	if (list_empty(&pcm->list))
1091 		goto unlock;
1092 
1093 	mutex_lock(&pcm->open_mutex);
1094 	wake_up(&pcm->open_wait);
1095 	list_del_init(&pcm->list);
1096 	for (cidx = 0; cidx < 2; cidx++)
1097 		for (substream = pcm->streams[cidx].substream; substream; substream = substream->next) {
1098 			snd_pcm_stream_lock_irq(substream);
1099 			if (substream->runtime) {
1100 				substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
1101 				wake_up(&substream->runtime->sleep);
1102 				wake_up(&substream->runtime->tsleep);
1103 			}
1104 			snd_pcm_stream_unlock_irq(substream);
1105 		}
1106 	list_for_each_entry(notify, &snd_pcm_notify_list, list) {
1107 		notify->n_disconnect(pcm);
1108 	}
1109 	for (cidx = 0; cidx < 2; cidx++) {
1110 		devtype = -1;
1111 		switch (cidx) {
1112 		case SNDRV_PCM_STREAM_PLAYBACK:
1113 			devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
1114 			break;
1115 		case SNDRV_PCM_STREAM_CAPTURE:
1116 			devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
1117 			break;
1118 		}
1119 		snd_unregister_device(devtype, pcm->card, pcm->device);
1120 	}
1121 	mutex_unlock(&pcm->open_mutex);
1122  unlock:
1123 	mutex_unlock(&register_mutex);
1124 	return 0;
1125 }
1126 
snd_pcm_notify(struct snd_pcm_notify * notify,int nfree)1127 int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree)
1128 {
1129 	struct snd_pcm *pcm;
1130 
1131 	if (snd_BUG_ON(!notify ||
1132 		       !notify->n_register ||
1133 		       !notify->n_unregister ||
1134 		       !notify->n_disconnect))
1135 		return -EINVAL;
1136 	mutex_lock(&register_mutex);
1137 	if (nfree) {
1138 		list_del(&notify->list);
1139 		list_for_each_entry(pcm, &snd_pcm_devices, list)
1140 			notify->n_unregister(pcm);
1141 	} else {
1142 		list_add_tail(&notify->list, &snd_pcm_notify_list);
1143 		list_for_each_entry(pcm, &snd_pcm_devices, list)
1144 			notify->n_register(pcm);
1145 	}
1146 	mutex_unlock(&register_mutex);
1147 	return 0;
1148 }
1149 
1150 EXPORT_SYMBOL(snd_pcm_notify);
1151 
1152 #ifdef CONFIG_PROC_FS
1153 /*
1154  *  Info interface
1155  */
1156 
snd_pcm_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)1157 static void snd_pcm_proc_read(struct snd_info_entry *entry,
1158 			      struct snd_info_buffer *buffer)
1159 {
1160 	struct snd_pcm *pcm;
1161 
1162 	mutex_lock(&register_mutex);
1163 	list_for_each_entry(pcm, &snd_pcm_devices, list) {
1164 		snd_iprintf(buffer, "%02i-%02i: %s : %s",
1165 			    pcm->card->number, pcm->device, pcm->id, pcm->name);
1166 		if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
1167 			snd_iprintf(buffer, " : playback %i",
1168 				    pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count);
1169 		if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
1170 			snd_iprintf(buffer, " : capture %i",
1171 				    pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count);
1172 		snd_iprintf(buffer, "\n");
1173 	}
1174 	mutex_unlock(&register_mutex);
1175 }
1176 
1177 static struct snd_info_entry *snd_pcm_proc_entry;
1178 
snd_pcm_proc_init(void)1179 static void snd_pcm_proc_init(void)
1180 {
1181 	struct snd_info_entry *entry;
1182 
1183 	if ((entry = snd_info_create_module_entry(THIS_MODULE, "pcm", NULL)) != NULL) {
1184 		snd_info_set_text_ops(entry, NULL, snd_pcm_proc_read);
1185 		if (snd_info_register(entry) < 0) {
1186 			snd_info_free_entry(entry);
1187 			entry = NULL;
1188 		}
1189 	}
1190 	snd_pcm_proc_entry = entry;
1191 }
1192 
snd_pcm_proc_done(void)1193 static void snd_pcm_proc_done(void)
1194 {
1195 	snd_info_free_entry(snd_pcm_proc_entry);
1196 }
1197 
1198 #else /* !CONFIG_PROC_FS */
1199 #define snd_pcm_proc_init()
1200 #define snd_pcm_proc_done()
1201 #endif /* CONFIG_PROC_FS */
1202 
1203 
1204 /*
1205  *  ENTRY functions
1206  */
1207 
alsa_pcm_init(void)1208 static int __init alsa_pcm_init(void)
1209 {
1210 	snd_ctl_register_ioctl(snd_pcm_control_ioctl);
1211 	snd_ctl_register_ioctl_compat(snd_pcm_control_ioctl);
1212 	snd_pcm_proc_init();
1213 	return 0;
1214 }
1215 
alsa_pcm_exit(void)1216 static void __exit alsa_pcm_exit(void)
1217 {
1218 	snd_ctl_unregister_ioctl(snd_pcm_control_ioctl);
1219 	snd_ctl_unregister_ioctl_compat(snd_pcm_control_ioctl);
1220 	snd_pcm_proc_done();
1221 }
1222 
1223 module_init(alsa_pcm_init)
1224 module_exit(alsa_pcm_exit)
1225