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